mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 20:50:34 +01:00
Element dialog : Remove ElementsCategoriesList and use QTreeView with ElementsCollectionModel instead
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4456 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -238,26 +238,8 @@ void ElementsLocation::setPath(const QString &path)
|
|||||||
p = QETApp::customElementsDirN() + "/" + tmp_path;
|
p = QETApp::customElementsDirN() + "/" + tmp_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
//This is an element
|
m_file_system_path = p;
|
||||||
if (path.endsWith(".elmt"))
|
m_collection_path = path;
|
||||||
{
|
|
||||||
QFile file(p);
|
|
||||||
if (file.exists())
|
|
||||||
{
|
|
||||||
m_file_system_path = p;
|
|
||||||
m_collection_path = path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//They must be a directory
|
|
||||||
else
|
|
||||||
{
|
|
||||||
QDir dir(p);
|
|
||||||
if(dir.exists())
|
|
||||||
{
|
|
||||||
m_file_system_path = p;
|
|
||||||
m_collection_path = path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//In this case, the path is supposed to be relative to the file system.
|
//In this case, the path is supposed to be relative to the file system.
|
||||||
else
|
else
|
||||||
@@ -567,22 +549,39 @@ bool ElementsLocation::setXml(const QDomDocument &xml_document) const
|
|||||||
QString error;
|
QString error;
|
||||||
QETXML::writeXmlFile(xml_document, fileSystemPath(), &error);
|
QETXML::writeXmlFile(xml_document, fileSystemPath(), &error);
|
||||||
|
|
||||||
if (!error.isEmpty())
|
if (!error.isEmpty()) {
|
||||||
{
|
|
||||||
qDebug() << "ElementsLocation::setXml error : " << error;
|
qDebug() << "ElementsLocation::setXml error : " << error;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (isProject() && exist())
|
else if (isProject())
|
||||||
{
|
{
|
||||||
QDomElement dom_element = xml();
|
//Element exist, we overwrite the existing element.
|
||||||
QDomNode parent_node = dom_element.parentNode();
|
if (exist())
|
||||||
parent_node.removeChild(dom_element);
|
{
|
||||||
parent_node.appendChild(xml_document.documentElement().cloneNode(true));
|
QDomElement dom_element = xml();
|
||||||
|
QDomNode parent_node = dom_element.parentNode();
|
||||||
|
parent_node.removeChild(dom_element);
|
||||||
|
parent_node.appendChild(xml_document.documentElement().cloneNode(true));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//Element doesn't exist, we create the element
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QString path_ = collectionPath(false);
|
||||||
|
QRegExp rx ("^(.*)/(.*\\.elmt)$");
|
||||||
|
|
||||||
return true;
|
if (rx.exactMatch(path_)) {
|
||||||
|
return project()->embeddedElementCollection()->addElementDefinition(rx.cap(1), rx.cap(2), xml_document.documentElement());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
qDebug() << "ElementsLocation::setXml : rx don't match";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -391,6 +391,44 @@ QString XmlElementCollection::addElement(ElementsLocation &location)
|
|||||||
return integrated_path;
|
return integrated_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief XmlElementCollection::addElementDefinition
|
||||||
|
* Add the élément defintion @xml_definition in the directory at path @dir_path with the name @elmt_name.
|
||||||
|
* @param dir_path : the path of the directory where we must add the element.
|
||||||
|
* The path must be an existing directory of this collection.
|
||||||
|
* @param elmt_name : The name used to store the element (the name must end with .elmt, if not, .elmt will be append to @elmt_name)
|
||||||
|
* @param xml_definition : The xml definition of the element.
|
||||||
|
* The tag name of @xml_definition must be "definition".
|
||||||
|
* @return True if the element is added with success.
|
||||||
|
*/
|
||||||
|
bool XmlElementCollection::addElementDefinition(const QString &dir_path, const QString &elmt_name, const QDomElement &xml_definition)
|
||||||
|
{
|
||||||
|
QDomElement dom_dir = directory(dir_path);
|
||||||
|
if (dom_dir.isNull()) {
|
||||||
|
qDebug() << "XmlElementCollection::addElementDefinition : No directory at path : " << dir_path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (xml_definition.tagName() != "definition") {
|
||||||
|
qDebug() << "XmlElementCollection::addElementDefinition : xml_defintion tag name is not \"definition\"";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString name = elmt_name;
|
||||||
|
if (!name.endsWith(".elmt")) {
|
||||||
|
name += ".elmt";
|
||||||
|
}
|
||||||
|
|
||||||
|
QDomElement dom_elmt = m_dom_document.createElement("element");
|
||||||
|
dom_elmt.setAttribute("name", name);
|
||||||
|
dom_elmt.appendChild(xml_definition.cloneNode(true));
|
||||||
|
dom_dir.appendChild(dom_elmt);
|
||||||
|
|
||||||
|
emit elementAdded(dir_path + "/" + name);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief XmlElementCollection::copy
|
* @brief XmlElementCollection::copy
|
||||||
* Copy the content represented by source (an element or a directory) to destination.
|
* Copy the content represented by source (an element or a directory) to destination.
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ class XmlElementCollection : public QObject
|
|||||||
QDomElement element(const QString &path);
|
QDomElement element(const QString &path);
|
||||||
QDomElement directory(const QString &path);
|
QDomElement directory(const QString &path);
|
||||||
QString addElement (ElementsLocation &location);
|
QString addElement (ElementsLocation &location);
|
||||||
|
bool addElementDefinition (const QString &dir_path, const QString &elmt_name, const QDomElement &xml_definition);
|
||||||
ElementsLocation copy (ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
|
ElementsLocation copy (ElementsLocation &source, ElementsLocation &destination, QString rename = QString(), bool deep_copy = true);
|
||||||
bool exist (const QString &path);
|
bool exist (const QString &path);
|
||||||
|
|
||||||
|
|||||||
@@ -858,7 +858,6 @@ bool QETElementEditor::toLocation(const ElementsLocation &location)
|
|||||||
tr("Impossible d'enregistrer l'élément", "message box content"));
|
tr("Impossible d'enregistrer l'élément", "message box content"));
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,362 +16,283 @@
|
|||||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include "elementdialog.h"
|
#include "elementdialog.h"
|
||||||
#include <QtWidgets>
|
|
||||||
#include "qetapp.h"
|
#include "qetapp.h"
|
||||||
#include "elementscategorieslist.h"
|
|
||||||
#include "elementscollectionitem.h"
|
|
||||||
#include "qfilenameedit.h"
|
#include "qfilenameedit.h"
|
||||||
|
#include "elementcollectionitem.h"
|
||||||
|
#include "elementscollectionmodel.h"
|
||||||
#include "qetmessagebox.h"
|
#include "qetmessagebox.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Constructeur par defaut.
|
* @brief ElementDialog::ElementDialog
|
||||||
Construit un dialogue permettant d'ouvrir un element
|
* @param mode
|
||||||
@param mode Mode du dialogue
|
* @param parent
|
||||||
@see ElementDialog::Mode
|
*/
|
||||||
@param parentWidget QWidget parent
|
ElementDialog::ElementDialog(uint mode, QWidget *parent) :
|
||||||
@param parent QObject parent
|
QDialog(parent),
|
||||||
|
m_mode(mode)
|
||||||
*/
|
|
||||||
ElementDialog::ElementDialog(uint mode, QWidget *parentWidget, QObject *parent) :
|
|
||||||
QObject(parent),
|
|
||||||
mode_(mode),
|
|
||||||
buttons_(0),
|
|
||||||
list_(0),
|
|
||||||
textfield_(0)
|
|
||||||
{
|
{
|
||||||
dialog_ = new QDialog(parentWidget);
|
setUpWidget();
|
||||||
dialog_ -> setWindowModality(Qt::WindowModal);
|
setUpConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief ElementDialog::setUpWidget
|
||||||
|
* Build and setup the widgets of this dialog
|
||||||
|
*/
|
||||||
|
void ElementDialog::setUpWidget()
|
||||||
|
{
|
||||||
|
setWindowModality(Qt::WindowModal);
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
dialog_ -> setWindowFlags(Qt::Sheet);
|
setWindowFlags(Qt::Sheet);
|
||||||
#endif
|
#endif
|
||||||
buttons_ = new QDialogButtonBox();
|
|
||||||
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
||||||
// types selectionnables dans la liste
|
|
||||||
bool display_elements = (mode_ == OpenElement || mode_ == SaveElement);
|
QString title_, label_;
|
||||||
int selectables = 0;
|
switch (m_mode)
|
||||||
switch(mode_) {
|
{
|
||||||
case OpenElement: selectables = QET::Element; break;
|
case OpenElement:
|
||||||
case SaveElement: selectables = QET::ElementsCollectionItem; break;
|
title_ = tr("Ouvrir un élément", "dialog title");
|
||||||
case OpenCategory: selectables = QET::ElementsContainer; break;
|
label_ = tr("Choisissez l'élément que vous souhaitez ouvrir.", "dialog content");
|
||||||
case SaveCategory: selectables = QET::ElementsContainer; break;
|
break;
|
||||||
|
case SaveElement:
|
||||||
|
title_ = tr("Enregistrer un élément", "dialog title");
|
||||||
|
label_ = tr("Choisissez l'élément dans lequel vous souhaitez enregistrer votre définition.", "dialog content");
|
||||||
|
break;
|
||||||
|
case OpenCategory:
|
||||||
|
title_ = tr("Ouvrir une catégorie", "dialog title");
|
||||||
|
label_ = tr("Choisissez une catégorie.", "dialog content");
|
||||||
|
break;
|
||||||
|
case SaveCategory:
|
||||||
|
title_ = tr("Enregistrer une catégorie", "dialog title");
|
||||||
|
label_ = tr("Choisissez une catégorie.", "dialog content");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
title_ = tr("Titre");
|
||||||
|
label_ = tr("Label");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
list_ = new ElementsCategoriesList(display_elements, selectables);
|
setWindowTitle(title_);
|
||||||
connect(list_, SIGNAL(locationChanged(const ElementsLocation &)), this, SLOT(locationChanged(const ElementsLocation &)));
|
|
||||||
|
layout->addWidget(new QLabel(label_));
|
||||||
// titre et label
|
|
||||||
if (!mode) {
|
|
||||||
title_ = tr("Ouvrir un élément", "dialog title");
|
m_tree_view = new QTreeView(this);
|
||||||
label_ = tr("Choisissez l'élément que vous souhaitez ouvrir.", "dialog content");
|
|
||||||
} else if (mode == 1) {
|
ElementsCollectionModel *model = new ElementsCollectionModel(m_tree_view);
|
||||||
title_ = tr("Enregistrer un élément", "dialog title");
|
if (m_mode == OpenElement) {model->addCommonCollection();}
|
||||||
label_ = tr("Choisissez l'élément dans lequel vous souhaitez enregistrer votre définition.", "dialog content");
|
model->addCustomCollection();
|
||||||
} else if (mode == 2) {
|
foreach (QETProject *project, QETApp::registeredProjects()) {
|
||||||
title_ = tr("Ouvrir une catégorie", "dialog title");
|
model->addProject(project);
|
||||||
label_ = tr("Choisissez une catégorie.", "dialog content");
|
|
||||||
} else {
|
|
||||||
title_ = tr("Enregistrer une catégorie", "dialog title");
|
|
||||||
label_ = tr("Choisissez une catégorie.", "dialog content");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// mode ouverture / enregistrement
|
m_tree_view->setModel(model);
|
||||||
if (mode_ == SaveCategory || mode_ == SaveElement) {
|
m_tree_view->setHeaderHidden(true);
|
||||||
buttons_ -> setStandardButtons(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
|
layout->addWidget(m_tree_view);
|
||||||
textfield_ = new QFileNameEdit();
|
|
||||||
connect(textfield_, SIGNAL(textChanged(const QString &)), this, SLOT(textFieldChanged(const QString &)));
|
|
||||||
} else {
|
m_buttons_box = new QDialogButtonBox(this);
|
||||||
buttons_ -> setStandardButtons(QDialogButtonBox::Open | QDialogButtonBox::Cancel);
|
|
||||||
|
if (m_mode == SaveCategory || m_mode == SaveElement)
|
||||||
|
{
|
||||||
|
m_buttons_box->setStandardButtons(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
|
||||||
|
m_buttons_box->button(QDialogButtonBox::Save)->setDisabled(true);
|
||||||
|
|
||||||
|
m_text_field = new QFileNameEdit();
|
||||||
|
m_text_field->setDisabled(true);
|
||||||
|
m_text_field->setPlaceholderText(m_mode == SaveCategory? tr("Nom du nouveau dossier") : tr("Nom du nouvel élément"));
|
||||||
|
|
||||||
|
layout->addWidget(m_text_field);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// connexions boutons -> dialogue
|
{
|
||||||
connect(buttons_, SIGNAL(accepted()), this, SLOT(checkDialog()));
|
m_buttons_box->setStandardButtons(QDialogButtonBox::Open | QDialogButtonBox::Cancel);
|
||||||
connect(buttons_, SIGNAL(rejected()), dialog_, SLOT(reject()));
|
m_buttons_box->button(QDialogButtonBox::Open)->setDisabled(true);
|
||||||
|
}
|
||||||
// connexions dialogue -> classe
|
|
||||||
connect(dialog_, SIGNAL(accepted()), this, SLOT(dialogAccepted()));
|
layout->addWidget(m_buttons_box);
|
||||||
connect(dialog_, SIGNAL(rejected()), this, SLOT(dialogRejected()));
|
|
||||||
|
|
||||||
makeInterface();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Destructeur
|
* @brief ElementDialog::setUpConnection
|
||||||
*/
|
* Setup connection of this dialog
|
||||||
ElementDialog::~ElementDialog() {
|
*/
|
||||||
dialog_ -> setParent(0);
|
void ElementDialog::setUpConnection()
|
||||||
delete dialog_;
|
{
|
||||||
|
connect(m_tree_view, &QTreeView::clicked, this, &ElementDialog::indexClicked);
|
||||||
|
connect(m_buttons_box, &QDialogButtonBox::accepted, this, &ElementDialog::checkAccept);
|
||||||
|
connect(m_buttons_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
||||||
|
|
||||||
|
if (m_text_field) { connect(m_text_field, &QFileNameEdit::textChanged, this, &ElementDialog::checkCurrentLocation); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Affiche un dialogue permettant a l'utilisateur de selectionner une categorie existant deja
|
* @brief ElementDialog::indexClicked
|
||||||
@param parentWidget QWidget parent
|
* @param index
|
||||||
@return le chemin virtuel de cette categorie
|
*/
|
||||||
*/
|
void ElementDialog::indexClicked(const QModelIndex &index)
|
||||||
ElementsLocation ElementDialog::getExistingCategoryLocation(QWidget *parentWidget) {
|
{
|
||||||
return(ElementDialog::execConfiguredDialog(ElementDialog::OpenCategory, parentWidget));
|
ElementCollectionItem *eci = static_cast<ElementCollectionItem*> (index.internalPointer());
|
||||||
|
m_location = ElementsLocation(eci->collectionPath());
|
||||||
|
checkCurrentLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Affiche un dialogue permettant a l'utilisateur de selectionner une nouvelle categorie
|
* @brief ElementDialog::checkCurrentLocation
|
||||||
@param parentWidget QWidget parent
|
* Update this dialog according to the current selected location and the current mode
|
||||||
@return le chemin virtuel de cette categorie
|
*/
|
||||||
*/
|
void ElementDialog::checkCurrentLocation()
|
||||||
ElementsLocation ElementDialog::getNewCategoryLocation(QWidget *parentWidget) {
|
{
|
||||||
return(ElementDialog::execConfiguredDialog(ElementDialog::SaveCategory, parentWidget));
|
if (m_mode == OpenElement) {
|
||||||
|
m_buttons_box->button(QDialogButtonBox::Open)->setEnabled(m_location.isElement() && m_location.exist());
|
||||||
|
}
|
||||||
|
else if (m_mode == SaveElement)
|
||||||
|
{
|
||||||
|
m_buttons_box->button(QDialogButtonBox::Save)->setDisabled(true);
|
||||||
|
|
||||||
|
//Location doesn't exist
|
||||||
|
if (!m_location.exist()) { return; }
|
||||||
|
|
||||||
|
if (m_location.isElement())
|
||||||
|
{
|
||||||
|
m_text_field->setDisabled(true);
|
||||||
|
m_buttons_box->button(QDialogButtonBox::Save)->setEnabled(true);
|
||||||
|
}
|
||||||
|
else if (m_location.isDirectory())
|
||||||
|
{
|
||||||
|
m_text_field->setEnabled(true);
|
||||||
|
|
||||||
|
if (m_text_field->text().isEmpty()) { return; }
|
||||||
|
|
||||||
|
//Only enable save button if the location at path :
|
||||||
|
//m_location.collectionPath + m_text_filed.text doesn't exist.
|
||||||
|
QString new_path = m_text_field->text();
|
||||||
|
if (!new_path.endsWith(".elmt")) new_path += ".elmt";
|
||||||
|
|
||||||
|
ElementsLocation loc = m_location;
|
||||||
|
loc.addToPath(new_path);
|
||||||
|
|
||||||
|
m_buttons_box->button(QDialogButtonBox::Save)->setDisabled(loc.exist() ? true : false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ElementDialog::checkAccept()
|
||||||
|
{
|
||||||
|
ElementsLocation loc = location();
|
||||||
|
|
||||||
|
if (m_mode == OpenElement)
|
||||||
|
{
|
||||||
|
if (loc.isElement() && loc.exist()) {accept();}
|
||||||
|
if (!loc.exist())
|
||||||
|
{
|
||||||
|
QET::QetMessageBox::critical(this,
|
||||||
|
tr("Sélection inexistante", "message box title"),
|
||||||
|
tr("La sélection n'existe pas.", "message box content"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (!loc.isElement())
|
||||||
|
{
|
||||||
|
QET::QetMessageBox::critical(this,
|
||||||
|
tr("Sélection incorrecte", "message box title"),
|
||||||
|
tr("La sélection n'est pas un élément.", "message box content"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (m_mode == SaveElement)
|
||||||
|
{
|
||||||
|
if (loc.isElement())
|
||||||
|
{
|
||||||
|
if (loc.exist())
|
||||||
|
{
|
||||||
|
QMessageBox::StandardButton answer = QET::QetMessageBox::question(this,
|
||||||
|
tr("Écraser l'élément ?", "message box title"),
|
||||||
|
tr("L'élément existe déjà. Voulez-vous l'écraser ?", "message box content"),
|
||||||
|
QMessageBox::Yes | QMessageBox::No,
|
||||||
|
QMessageBox::No);
|
||||||
|
if (answer == QMessageBox::Yes) {accept();}
|
||||||
|
else {return;}
|
||||||
|
}
|
||||||
|
else {accept();}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QET::QetMessageBox::critical(this,
|
||||||
|
tr("Sélection incorrecte", "message box title"),
|
||||||
|
tr("Vous devez sélectionner un élément ou une catégorie avec un nom pour l'élément.", "message box content"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Affiche un dialogue permettant a l'utilisateur de selectionner un element a ouvrir
|
* @brief ElementDialog::location
|
||||||
@param parentWidget QWidget parent
|
* @return The selected location or a null location if user has selected nothing
|
||||||
@return le chemin virtuel de cet element
|
* or selection isn't compatible with the curent mode
|
||||||
*/
|
*/
|
||||||
|
ElementsLocation ElementDialog::location() const
|
||||||
|
{
|
||||||
|
if (m_mode == OpenElement)
|
||||||
|
{
|
||||||
|
if (m_location.isElement()) { return m_location; }
|
||||||
|
else {return ElementsLocation(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (m_mode == SaveElement)
|
||||||
|
{
|
||||||
|
//Current selected location is element, we return this location
|
||||||
|
if (m_location.isElement()) { return m_location; }
|
||||||
|
|
||||||
|
//Current selected location is directory, we return a location at path :
|
||||||
|
//m_location->collectionPath + m_text_field->text
|
||||||
|
else if (m_location.isDirectory())
|
||||||
|
{
|
||||||
|
QString new_path = m_text_field->text();
|
||||||
|
if (new_path.isEmpty()) { return ElementsLocation(); }
|
||||||
|
|
||||||
|
if (!new_path.endsWith(".elmt")) { new_path += ".elmt"; }
|
||||||
|
|
||||||
|
ElementsLocation loc = m_location;
|
||||||
|
loc.addToPath(new_path);
|
||||||
|
return loc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ElementsLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief ElementDialog::getOpenElementLocation
|
||||||
|
* Display a dialog for open an element through her location
|
||||||
|
* @param parentWidget
|
||||||
|
* @return The location of the selected element
|
||||||
|
*/
|
||||||
ElementsLocation ElementDialog::getOpenElementLocation(QWidget *parentWidget) {
|
ElementsLocation ElementDialog::getOpenElementLocation(QWidget *parentWidget) {
|
||||||
return(ElementDialog::execConfiguredDialog(ElementDialog::OpenElement, parentWidget));
|
return(ElementDialog::execConfiguredDialog(ElementDialog::OpenElement, parentWidget));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Affiche un dialogue permettant a l'utilisateur de selectionner un element (existant ou non)
|
* @brief ElementDialog::getSaveElementLocation
|
||||||
qu'il souhaite enregistrer
|
* Display a dialog that allow to user to select an element (existing or not) who he want to save
|
||||||
@param parentWidget QWidget parent
|
* @param parentWidget
|
||||||
@return le chemin virtuel de cet element
|
* @return The location where the element must be save
|
||||||
*/
|
*/
|
||||||
ElementsLocation ElementDialog::getSaveElementLocation(QWidget *parentWidget) {
|
ElementsLocation ElementDialog::getSaveElementLocation(QWidget *parentWidget) {
|
||||||
return(ElementDialog::execConfiguredDialog(ElementDialog::SaveElement, parentWidget));
|
return(ElementDialog::execConfiguredDialog(ElementDialog::SaveElement, parentWidget));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Lance un dialogue selon la configuration mode
|
* @brief ElementDialog::execConfiguredDialog
|
||||||
@param mode Mode du dialogue
|
* launch a dialog with the chosen mode
|
||||||
@param parentWidget QWidget parent
|
* @param mode : mode of the dialog
|
||||||
*/
|
* @param parentWidget : parent widget of the dialog
|
||||||
ElementsLocation ElementDialog::execConfiguredDialog(int mode, QWidget *parentWidget) {
|
* @return the chosen location
|
||||||
ElementDialog element_dialog(mode, parentWidget);
|
*/
|
||||||
element_dialog.exec();
|
ElementsLocation ElementDialog::execConfiguredDialog(int mode, QWidget *parentWidget)
|
||||||
return(element_dialog.location());
|
{
|
||||||
}
|
ElementDialog *element_dialog = new ElementDialog(mode, parentWidget);
|
||||||
|
element_dialog->exec();
|
||||||
/**
|
ElementsLocation location = element_dialog->location();
|
||||||
Assemble les widgets pour obtenir le dialogue final
|
delete element_dialog;
|
||||||
*/
|
return(location);
|
||||||
void ElementDialog::makeInterface() {
|
|
||||||
dialog_ -> setWindowTitle(title_);
|
|
||||||
|
|
||||||
// disposition verticale
|
|
||||||
QVBoxLayout *layout = new QVBoxLayout(dialog_);
|
|
||||||
layout -> addWidget(new QLabel(label_));
|
|
||||||
layout -> addWidget(list_);
|
|
||||||
if (textfield_) {
|
|
||||||
QHBoxLayout *filename_layout = new QHBoxLayout();
|
|
||||||
filename_layout -> addWidget(new QLabel(tr("Nom :")));
|
|
||||||
filename_layout -> addWidget(textfield_);
|
|
||||||
layout -> addLayout(filename_layout);
|
|
||||||
}
|
|
||||||
layout -> addWidget(buttons_);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Execute le dialogue
|
|
||||||
@return QDialog::Accepted si le dialogue a ete accepte, false sinon
|
|
||||||
@see DialogCode
|
|
||||||
*/
|
|
||||||
int ElementDialog::exec() {
|
|
||||||
return(dialog_ -> exec());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
@return le chemin virtuel choisi via le dialogue
|
|
||||||
Si l'utilisateur n'a pas pu faire son choix, une chaine vide est retournee.
|
|
||||||
*/
|
|
||||||
ElementsLocation ElementDialog::location() const {
|
|
||||||
return(location_);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
gere le changement de chemin virtuel par l'utilisateur
|
|
||||||
@param new_loc le nouveau chemin virtuel choisi par l'utilisateur
|
|
||||||
*/
|
|
||||||
void ElementDialog::locationChanged(const ElementsLocation &new_loc) {
|
|
||||||
ElementsCollectionItem *item = QETApp::collectionItem(new_loc);
|
|
||||||
if (!item) return;
|
|
||||||
if (mode_ == OpenElement) {
|
|
||||||
buttons_ -> button(QDialogButtonBox::Open) -> setEnabled(item -> isElement());
|
|
||||||
} else if (mode_ == SaveElement) {
|
|
||||||
// si l'utilisateur choisit un element existant, on desactive le champ
|
|
||||||
textfield_ -> setEnabled(!item -> isElement());
|
|
||||||
// il faut soit un element selectionne soit une categorie et un nom
|
|
||||||
buttons_ -> button(QDialogButtonBox::Save) -> setEnabled(
|
|
||||||
((item -> isCategory() || item -> isCollection()) && !textfield_ -> text().isEmpty()) ||\
|
|
||||||
item -> isElement()
|
|
||||||
);
|
|
||||||
} else if (mode_ == OpenCategory) {
|
|
||||||
/// @todo
|
|
||||||
} else if (mode_ == SaveCategory) {
|
|
||||||
/// @todo
|
|
||||||
}
|
|
||||||
location_ = new_loc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Gere le changement de contenu dans le champ de texte
|
|
||||||
@param text Contenu du champ de texte
|
|
||||||
*/
|
|
||||||
void ElementDialog::textFieldChanged(const QString &text) {
|
|
||||||
ElementsCollectionItem *item = QETApp::collectionItem(list_ -> selectedLocation());
|
|
||||||
if (!item) return;
|
|
||||||
if (mode_ == SaveElement) {
|
|
||||||
// il faut soit un element selectionne soit une categorie et un nom
|
|
||||||
buttons_ -> button(QDialogButtonBox::Save) -> setEnabled(
|
|
||||||
((item -> isCategory() || item -> isCollection()) && !text.isEmpty()) ||\
|
|
||||||
item -> isElement()
|
|
||||||
);
|
|
||||||
} else if (mode_ == SaveCategory) {
|
|
||||||
// il faut forcement un nom pour la nouvelle categorie
|
|
||||||
buttons_ -> button(QDialogButtonBox::Save) -> setEnabled(!text.isEmpty());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Verifie l'etat du dialogue au moment ou l'utilisateur le valide.
|
|
||||||
*/
|
|
||||||
void ElementDialog::checkDialog() {
|
|
||||||
// verifie si ce qui a ete selectionne par l'utilisateur correspond au mode du widget
|
|
||||||
if (mode_ == OpenElement) {
|
|
||||||
// l'utilisateur doit avoir choisi un element existant
|
|
||||||
|
|
||||||
// on verifie d'abord que l'utilisateur a choisi quelque chose
|
|
||||||
ElementsLocation location = list_ -> selectedLocation();
|
|
||||||
if (location.isNull()) {
|
|
||||||
QET::QetMessageBox::critical(
|
|
||||||
dialog_,
|
|
||||||
tr("Pas de sélection", "message box title"),
|
|
||||||
tr("Vous devez sélectionner un élément.", "message box content")
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// on verifie donc que la selection existe
|
|
||||||
ElementsCollectionItem *item = QETApp::collectionItem(location);
|
|
||||||
if (!item) {
|
|
||||||
QET::QetMessageBox::critical(
|
|
||||||
dialog_,
|
|
||||||
tr("Sélection inexistante", "message box title"),
|
|
||||||
tr("La sélection n'existe pas.", "message box content")
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// puis on verifie qu'il s'agit bien d'un element
|
|
||||||
if (!item -> isElement()) {
|
|
||||||
QET::QetMessageBox::critical(
|
|
||||||
dialog_,
|
|
||||||
tr("Sélection incorrecte", "message box title"),
|
|
||||||
tr("La sélection n'est pas un élément.", "message box content")
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
location_ = location;
|
|
||||||
} else if (mode_ == SaveElement) {
|
|
||||||
/*
|
|
||||||
l'utilisateur doit avoir choisi soit :
|
|
||||||
-une categorie et un nom d'element
|
|
||||||
-un element existant
|
|
||||||
*/
|
|
||||||
ElementsLocation location = list_ -> selectedLocation();
|
|
||||||
if (location.isNull()) {
|
|
||||||
QET::QetMessageBox::critical(
|
|
||||||
dialog_,
|
|
||||||
tr("Pas de sélection", "message box title"),
|
|
||||||
tr("Vous devez sélectionner une catégorie ou un élément.", "message box content")
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// on verifie donc que la selection existe
|
|
||||||
ElementsCollectionItem *item = QETApp::collectionItem(location);
|
|
||||||
if (!item) {
|
|
||||||
QET::QetMessageBox::critical(
|
|
||||||
dialog_,
|
|
||||||
tr("Sélection inexistante", "message box title"),
|
|
||||||
tr("La sélection n'existe pas.", "message box content")
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ElementsLocation final_location(location);
|
|
||||||
if (!item -> isElement()) {
|
|
||||||
QString element_name(textfield_ -> text());
|
|
||||||
// si on a une categorie (ou une collection), il nous faut un nom d'element
|
|
||||||
if (element_name.isEmpty()) {
|
|
||||||
QET::QetMessageBox::critical(
|
|
||||||
dialog_,
|
|
||||||
tr("Nom manquant", "message box title"),
|
|
||||||
tr("Vous devez entrer un nom pour l'élément", "message box content")
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ce nom d'element doit etre valide
|
|
||||||
if (QET::containsForbiddenCharacters(element_name)) {
|
|
||||||
QET::QetMessageBox::critical(
|
|
||||||
dialog_,
|
|
||||||
tr("Nom invalide", "message box title"),
|
|
||||||
QString(
|
|
||||||
tr(
|
|
||||||
"Vous ne pouvez pas utiliser les caractères "
|
|
||||||
"suivants dans le nom de l'élément : %1"
|
|
||||||
)
|
|
||||||
).arg(QET::forbiddenCharactersString(true))
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ajoute .elmt a la fin du nom si necessaire
|
|
||||||
if (!element_name.endsWith(".elmt", Qt::CaseInsensitive)) {
|
|
||||||
element_name += ".elmt";
|
|
||||||
}
|
|
||||||
final_location.addToPath(element_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// determine si l'element n'existe pas deja
|
|
||||||
bool element_already_exists = (
|
|
||||||
item -> isElement() ||\
|
|
||||||
QETApp::collectionItem(final_location)
|
|
||||||
);
|
|
||||||
|
|
||||||
// si l'element existe, on demande confirmation pour son ecrasement
|
|
||||||
if (element_already_exists) {
|
|
||||||
QMessageBox::StandardButton answer = QET::QetMessageBox::question(
|
|
||||||
dialog_,
|
|
||||||
tr("Écraser l'élément ?", "message box title"),
|
|
||||||
tr("L'élément existe déjà. Voulez-vous l'écraser ?", "message box content"),
|
|
||||||
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
|
|
||||||
QMessageBox::No
|
|
||||||
);
|
|
||||||
if (answer != QMessageBox::Yes) return;
|
|
||||||
}
|
|
||||||
|
|
||||||
location_ = final_location;
|
|
||||||
} else if (mode_ == OpenCategory) {
|
|
||||||
// l'utilisateur doit avoir choisi une categorie ou une collection existante
|
|
||||||
/// @todo effectuer les verifications necessaires
|
|
||||||
} else if (mode_ == SaveCategory) {
|
|
||||||
// l'utilisateur doit avoir choisi une categorie inexistante
|
|
||||||
/// @todo effectuer les verifications necessaires
|
|
||||||
}
|
|
||||||
|
|
||||||
// le dialogue est verifie, il peut etre accepte
|
|
||||||
dialog_ -> accept();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Slot execute apres la validation du dialogue par l'utilisateur
|
|
||||||
*/
|
|
||||||
void ElementDialog::dialogAccepted() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Gere le rejet du dialogue par l'utilisateur.
|
|
||||||
*/
|
|
||||||
void ElementDialog::dialogRejected() {
|
|
||||||
location_ = ElementsLocation();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,17 +17,19 @@
|
|||||||
*/
|
*/
|
||||||
#ifndef ELEMENT_DIALOG_H
|
#ifndef ELEMENT_DIALOG_H
|
||||||
#define ELEMENT_DIALOG_H
|
#define ELEMENT_DIALOG_H
|
||||||
#include <QtCore>
|
|
||||||
|
#include <QDialog>
|
||||||
#include "elementslocation.h"
|
#include "elementslocation.h"
|
||||||
class QDialog;
|
|
||||||
class QDialogButtonBox;
|
class QDialogButtonBox;
|
||||||
class ElementsCategoriesList;
|
|
||||||
class QFileNameEdit;
|
class QFileNameEdit;
|
||||||
|
class QTreeView;
|
||||||
/**
|
/**
|
||||||
This class provides several dialogs to select an element or a category
|
This class provides several dialogs to select an element or a category
|
||||||
(e.g. new or existing, for opening or for saving...).
|
(e.g. new or existing, for opening or for saving...).
|
||||||
*/
|
*/
|
||||||
class ElementDialog : public QObject {
|
class ElementDialog : public QDialog
|
||||||
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
// enumerations
|
// enumerations
|
||||||
/**
|
/**
|
||||||
@@ -40,44 +42,36 @@ class ElementDialog : public QObject {
|
|||||||
SaveCategory = 3 ///< The dialog should select a category for saving
|
SaveCategory = 3 ///< The dialog should select a category for saving
|
||||||
};
|
};
|
||||||
|
|
||||||
// constructors, destructor
|
// constructors, destructor
|
||||||
public:
|
public:
|
||||||
ElementDialog(uint = ElementDialog::OpenElement, QWidget * = 0, QObject * = 0);
|
ElementDialog(uint = ElementDialog::OpenElement, QWidget *parent = nullptr);
|
||||||
virtual ~ElementDialog();
|
|
||||||
private:
|
private:
|
||||||
ElementDialog(const ElementDialog &);
|
ElementDialog(const ElementDialog &);
|
||||||
|
|
||||||
// methods
|
|
||||||
public:
|
public:
|
||||||
int exec();
|
ElementsLocation location() const;
|
||||||
ElementsLocation location() const;
|
|
||||||
static ElementsLocation getExistingCategoryLocation(QWidget * = 0);
|
|
||||||
static ElementsLocation getNewCategoryLocation(QWidget * = 0);
|
|
||||||
static ElementsLocation getOpenElementLocation(QWidget * = 0);
|
|
||||||
static ElementsLocation getSaveElementLocation(QWidget * = 0);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static ElementsLocation execConfiguredDialog(int, QWidget * = 0);
|
void setUpWidget();
|
||||||
|
void setUpConnection();
|
||||||
private slots:
|
void indexClicked(const QModelIndex &index);
|
||||||
void locationChanged(const ElementsLocation &);
|
void updateWidget();
|
||||||
void textFieldChanged(const QString &);
|
void checkCurrentLocation();
|
||||||
void dialogAccepted();
|
void checkAccept();
|
||||||
void dialogRejected();
|
|
||||||
void checkDialog();
|
|
||||||
|
|
||||||
|
// attributes
|
||||||
private:
|
private:
|
||||||
void makeInterface();
|
uint m_mode;
|
||||||
|
ElementsLocation m_location;
|
||||||
// attributes
|
QDialogButtonBox *m_buttons_box = nullptr;
|
||||||
|
QFileNameEdit *m_text_field = nullptr;
|
||||||
|
QTreeView *m_tree_view = nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static ElementsLocation getOpenElementLocation(QWidget *parent = nullptr);
|
||||||
|
static ElementsLocation getSaveElementLocation(QWidget *parent = nullptr);
|
||||||
private:
|
private:
|
||||||
uint mode_;
|
static ElementsLocation execConfiguredDialog(int, QWidget *parent = nullptr);
|
||||||
ElementsLocation location_;
|
|
||||||
QString title_;
|
|
||||||
QString label_;
|
|
||||||
QDialog *dialog_;
|
|
||||||
QDialogButtonBox *buttons_;
|
|
||||||
ElementsCategoriesList *list_;
|
|
||||||
QFileNameEdit *textfield_;
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user