From 168467abb0f6077c2b3ce7210c42da7489f49bf7 Mon Sep 17 00:00:00 2001 From: blacksun Date: Thu, 5 May 2016 13:31:04 +0000 Subject: [PATCH] Improve the way how an element is updated in the new element panel. Now Qet only use the new embbeded collection (XmlElementCollection). No need to reload the old element panel for add a new element created by the new element panel Elements are always imported to the embbeded collection of a project git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4468 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- .../elementcollectionitem.cpp | 43 ++-- .../elementcollectionitem.h | 5 +- .../elementscollectionmodel.cpp | 96 +++++-- .../elementscollectionmodel.h | 3 +- .../elementscollectionwidget.cpp | 33 ++- .../ElementsCollection/elementslocation.cpp | 16 ++ sources/ElementsCollection/elementslocation.h | 3 + .../fileelementcollectionitem.cpp | 34 --- .../fileelementcollectionitem.h | 2 - .../xmlelementcollection.cpp | 21 +- .../ElementsCollection/xmlelementcollection.h | 6 + .../diagramevent/diagrameventaddelement.cpp | 20 +- sources/diagramview.cpp | 20 +- sources/qetproject.cpp | 240 +++++++----------- sources/qetproject.h | 5 +- sources/ui/importelementdialog.cpp | 53 ++++ sources/ui/importelementdialog.h | 46 ++++ sources/ui/importelementdialog.ui | 147 +++++++++++ 18 files changed, 522 insertions(+), 271 deletions(-) create mode 100644 sources/ui/importelementdialog.cpp create mode 100644 sources/ui/importelementdialog.h create mode 100644 sources/ui/importelementdialog.ui diff --git a/sources/ElementsCollection/elementcollectionitem.cpp b/sources/ElementsCollection/elementcollectionitem.cpp index 6b37fe68c..51df2ac9d 100644 --- a/sources/ElementsCollection/elementcollectionitem.cpp +++ b/sources/ElementsCollection/elementcollectionitem.cpp @@ -142,6 +142,30 @@ ElementCollectionItem *ElementCollectionItem::lastItemForPath(const QString &pat return nullptr; } +/** + * @brief ElementCollectionItem::itemAtPath + * @param path + * @return the item at path or nullptr if doesn't exist + */ +ElementCollectionItem *ElementCollectionItem::itemAtPath(const QString &path) +{ + QStringList str_list = path.split("/"); + if (str_list.isEmpty()) return nullptr; + + ElementCollectionItem *match_eci = this; + foreach (QString str, str_list) { + ElementCollectionItem *eci = match_eci->childWithCollectionName(str); + if (!eci) { + return nullptr; + } + else { + match_eci = eci; + } + } + + return match_eci; +} + /** * @brief ElementCollectionItem::rowForInsertItem * Return the row for insert a new child item to this item with name @collection_name. @@ -396,22 +420,3 @@ void ElementCollectionItem::setBackgroundColor(Qt::GlobalColor color, bool show) m_bg_color = color; m_show_bg_color = show; } - -/** - * @brief ElementCollectionItem::canRemoveContent - * @return true if this item can remove the content that he represent - * By default return false. - */ -bool ElementCollectionItem::canRemoveContent() { - return false; -} - -/** - * @brief ElementCollectionItem::removeContent - * Remove the content that he represent this item (a directory or an element). - * This method do nothing and return false. Inherit it, to handle removing - * @return true if the content was successfully removed - */ -bool ElementCollectionItem::removeContent() { - return false; -} diff --git a/sources/ElementsCollection/elementcollectionitem.h b/sources/ElementsCollection/elementcollectionitem.h index 35d989a42..db7658d9c 100644 --- a/sources/ElementsCollection/elementcollectionitem.h +++ b/sources/ElementsCollection/elementcollectionitem.h @@ -47,6 +47,7 @@ class ElementCollectionItem : public QObject ElementCollectionItem *child(int row) const; ElementCollectionItem *childWithCollectionName(QString name) const; ElementCollectionItem *lastItemForPath(const QString &path, QString &newt_item); + ElementCollectionItem *itemAtPath(const QString &path); int rowForInsertItem(const QString &collection_name); virtual void insertNewItem(const QString &collection_name); int childCount() const; @@ -72,10 +73,6 @@ class ElementCollectionItem : public QObject int indexOfChild(ElementCollectionItem *child) const; void setBackgroundColor(Qt::GlobalColor color, bool show); - - virtual bool canRemoveContent(); - virtual bool removeContent(); - signals: void beginInsertRows(ElementCollectionItem *parent, int first, int last); void endInsertRows(); diff --git a/sources/ElementsCollection/elementscollectionmodel.cpp b/sources/ElementsCollection/elementscollectionmodel.cpp index 49a226db9..93f1d4827 100644 --- a/sources/ElementsCollection/elementscollectionmodel.cpp +++ b/sources/ElementsCollection/elementscollectionmodel.cpp @@ -21,6 +21,7 @@ #include "fileelementcollectionitem.h" #include "xmlprojectelementcollectionitem.h" #include "qetproject.h" +#include "xmlelementcollection.h" /** * @brief ElementsCollectionModel::ElementsCollectionModel @@ -273,7 +274,7 @@ void ElementsCollectionModel::addCustomCollection() /** * @brief ElementsCollectionModel::addProject - * Add @project to the disalyed collection + * Add @project to the displayed collection * @param project * @return true if project was successfully added. If project is already * handled, return false. @@ -288,21 +289,27 @@ bool ElementsCollectionModel::addProject(QETProject *project) XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(project, m_root_item); bool r = m_root_item->insertChild(row, xpeci); endInsertRows(); - connect(project, &QETProject::elementIntegratedToCollection, this, &ElementsCollectionModel::elementIntegratedToCollection); - + connect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection); + connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem); return r; } +/** + * @brief ElementsCollectionModel::removeProject + * Remove @project from this model + * @param project + * @return true if the project was successfully removed, false if not (or project doesn't managed) + */ bool ElementsCollectionModel::removeProject(QETProject *project) { if (!m_project_list.contains(project)) return false; int row = m_project_list.indexOf(project); - if (removeRows(row, 1, QModelIndex())) - { + if (removeRows(row, 1, QModelIndex())) { m_project_list.removeOne(project); - disconnect(project, &QETProject::elementIntegratedToCollection, this, &ElementsCollectionModel::elementIntegratedToCollection); + disconnect(project->embeddedElementCollection(), &XmlElementCollection::elementAdded, this, &ElementsCollectionModel::elementIntegratedToCollection); + connect(project->embeddedElementCollection(), &XmlElementCollection::elementChanged, this, &ElementsCollectionModel::updateItem); return true; } else @@ -339,24 +346,73 @@ XmlProjectElementCollectionItem *ElementsCollectionModel::itemForProject(QETProj * @brief ElementsCollectionModel::elementAddedToEmbeddedCollection * When an element is added to embedded collection of a project, * this method create and display the new element - * @param project -The project where new element was added. - * @param path -The path of the new element in the embedded collection of project + * @param path -The path of the new element in the embedded collection of a project */ -void ElementsCollectionModel::elementIntegratedToCollection(QETProject *project, QString path) +void ElementsCollectionModel::elementIntegratedToCollection (QString path) { - XmlProjectElementCollectionItem *xpeci = itemForProject(project); - if (!xpeci) return; + QObject *object = sender(); + XmlElementCollection *collection = static_cast (object); + if (!collection) return; - QString collection_name; - ElementCollectionItem *eci = xpeci->lastItemForPath(path, collection_name); - if (!eci) return; + QETProject *project = nullptr; - int new_row = eci->rowForInsertItem(collection_name); - if (new_row <= -1) return; - QModelIndex parent_index = createIndex(eci->row(), 0, eci); - beginInsertRows(parent_index, new_row, new_row); - eci->insertNewItem(collection_name); - endInsertRows(); + //Get the owner project of the collection + foreach (QETProject *prj, m_project_list) { + if (prj->embeddedElementCollection() == collection) { + project = prj; + } + } + + if (project) { + XmlProjectElementCollectionItem *xpeci = itemForProject(project); + if (!xpeci) return; + + QString collection_name; + ElementCollectionItem *eci = xpeci->lastItemForPath(path, collection_name); + if (!eci) return; + + int new_row = eci->rowForInsertItem(collection_name); + if (new_row <= -1) return; + QModelIndex parent_index = createIndex(eci->row(), 0, eci); + beginInsertRows(parent_index, new_row, new_row); + eci->insertNewItem(collection_name); + endInsertRows(); + } +} + +/** + * @brief ElementsCollectionModel::updateItem + * Update the item at path + * @param path + */ +void ElementsCollectionModel::updateItem(QString path) +{ + QObject *object = sender(); + XmlElementCollection *collection = static_cast (object); + if (!collection) return; + + QETProject *project = nullptr; + + //Get the owner project of the collection + foreach (QETProject *prj, m_project_list) { + if (prj->embeddedElementCollection() == collection) { + project = prj; + } + } + + if (project) { + XmlProjectElementCollectionItem *xpeci = itemForProject(project); + if (!xpeci) { + return; + } + + ElementCollectionItem *eci = xpeci->itemAtPath(path); + if (!eci) { + return; + } + + eci->clearData(); + } } void ElementsCollectionModel::bir(ElementCollectionItem *eci, int first, int last) diff --git a/sources/ElementsCollection/elementscollectionmodel.h b/sources/ElementsCollection/elementscollectionmodel.h index 2e2610e95..c3d7608c1 100644 --- a/sources/ElementsCollection/elementscollectionmodel.h +++ b/sources/ElementsCollection/elementscollectionmodel.h @@ -61,7 +61,8 @@ class ElementsCollectionModel : public QAbstractItemModel private: XmlProjectElementCollectionItem *itemForProject(QETProject *project); - void elementIntegratedToCollection (QETProject *project, QString path); + void elementIntegratedToCollection (QString path); + void updateItem (QString path); //Use as slot in method drop mime data void bir(ElementCollectionItem *eci, int first, int last); void brr(ElementCollectionItem *eci, int first, int last); diff --git a/sources/ElementsCollection/elementscollectionwidget.cpp b/sources/ElementsCollection/elementscollectionwidget.cpp index 3e2738946..fe32a54d3 100644 --- a/sources/ElementsCollection/elementscollectionwidget.cpp +++ b/sources/ElementsCollection/elementscollectionwidget.cpp @@ -263,21 +263,26 @@ void ElementsCollectionWidget::deleteElement() ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); if (!eci) return; - if (!(eci->isElement() && eci->canRemoveContent())) return; + + ElementsLocation loc(eci->collectionPath()); + if (! (loc.isElement() && loc.exist() && loc.isFileSystem() && loc.collectionPath().startsWith("custom://")) ) return; if (QET::QetMessageBox::question(this, tr("Supprimer l'élément ?", "message box title"), tr("Êtes-vous sûr de vouloir supprimer cet élément ?\n", "message box content"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { - if (!eci->removeContent()) + QFile file(loc.fileSystemPath()); + if (file.remove()) + { + m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent()); + } + else { QET::QetMessageBox::warning(this, tr("Suppression de l'élément", "message box title"), tr("La suppression de l'élément a échoué.", "message box content")); - } - else - m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent()); + } } } @@ -290,7 +295,9 @@ void ElementsCollectionWidget::deleteDirectory() ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); if (!eci) return; - if (!(eci->isDir() && eci->canRemoveContent())) return; + + ElementsLocation loc (eci->collectionPath()); + if (! (loc.isDirectory() && loc.exist() && loc.isFileSystem() && loc.collectionPath().startsWith("custom://")) ) return; if (QET::QetMessageBox::question(this, tr("Supprimer le dossier?", "message box title"), @@ -299,14 +306,17 @@ void ElementsCollectionWidget::deleteDirectory() "message box content"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { - if (!eci->removeContent()) + QDir dir (loc.fileSystemPath()); + if (dir.removeRecursively()) + { + m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent()); + } + else { QET::QetMessageBox::warning(this, tr("Suppression du dossier", "message box title"), tr("La suppression du dossier a échoué.", "message box content")); } - else - m_model->removeRows(m_index_at_context_menu.row(), 1, m_index_at_context_menu.parent()); } } @@ -344,8 +354,9 @@ void ElementsCollectionWidget::newDirectory() ElementsLocation location(feci->collectionPath()); ElementsCategoryEditor new_dir_editor(location, false, this); - if (new_dir_editor.exec() == QDialog::Accepted) - reload();; + if (new_dir_editor.exec() == QDialog::Accepted) { + reload(); + } } /** diff --git a/sources/ElementsCollection/elementslocation.cpp b/sources/ElementsCollection/elementslocation.cpp index 9c0534dd3..6c517dd2e 100644 --- a/sources/ElementsCollection/elementslocation.cpp +++ b/sources/ElementsCollection/elementslocation.cpp @@ -677,3 +677,19 @@ QString ElementsLocation::fileName() const uint qHash(const ElementsLocation &location) { return(qHash(location.toString())); } + +QDebug operator<< (QDebug debug, const ElementsLocation &location) +{ + QDebugStateSaver saver(debug); + debug.noquote(); + + QString msg; + msg += "ElementsLocation("; + msg += (location.isProject()? location.projectCollectionPath() : location.collectionPath(true)); + msg += location.exist()? ", true" : ", false"; + msg +=")"; + + debug << msg; + + return debug; +} diff --git a/sources/ElementsCollection/elementslocation.h b/sources/ElementsCollection/elementslocation.h index 9bf79c738..a308cd399 100644 --- a/sources/ElementsCollection/elementslocation.h +++ b/sources/ElementsCollection/elementslocation.h @@ -83,6 +83,9 @@ class ElementsLocation public: static int MetaTypeId; ///< Id of the corresponding Qt meta type }; + +QDebug operator<<(QDebug debug, const ElementsLocation &location); + Q_DECLARE_METATYPE(ElementsLocation) uint qHash(const ElementsLocation &); #endif diff --git a/sources/ElementsCollection/fileelementcollectionitem.cpp b/sources/ElementsCollection/fileelementcollectionitem.cpp index f981d9a5b..500bb8b43 100644 --- a/sources/ElementsCollection/fileelementcollectionitem.cpp +++ b/sources/ElementsCollection/fileelementcollectionitem.cpp @@ -391,40 +391,6 @@ QString FileElementCollectionItem::name() return m_name; } -/** - * @brief FileElementCollectionItem::canRemoveContent - * Reimplemented from ElementCollectionItem - * @return - */ -bool FileElementCollectionItem::canRemoveContent() -{ - if (isCommonCollection()) return false; - else if (isDir() && isCollectionRoot()) return false; - else return true; -} - -/** - * @brief FileElementCollectionItem::removeContent - * Reimplemented from ElementCollectionItem - * @return - */ -bool FileElementCollectionItem::removeContent() -{ - if (!canRemoveContent()) return false; - - if (isElement()) - { - QFile file(fileSystemPath()); - return file.remove(); - } - else if (isDir() && !isCollectionRoot()) - { - QDir dir (fileSystemPath()); - return dir.removeRecursively(); - } - return false; -} - void FileElementCollectionItem::insertNewItem(const QString &collection_name) { if (collection_name.isEmpty()) return; diff --git a/sources/ElementsCollection/fileelementcollectionitem.h b/sources/ElementsCollection/fileelementcollectionitem.h index 4cdf247f1..a43e3739c 100644 --- a/sources/ElementsCollection/fileelementcollectionitem.h +++ b/sources/ElementsCollection/fileelementcollectionitem.h @@ -60,8 +60,6 @@ class FileElementCollectionItem : public ElementCollectionItem virtual bool isValid() const; virtual QString name(); - virtual bool canRemoveContent(); - virtual bool removeContent(); virtual void insertNewItem(const QString &collection_name); private: diff --git a/sources/ElementsCollection/xmlelementcollection.cpp b/sources/ElementsCollection/xmlelementcollection.cpp index 2f95ce933..518ccc34d 100644 --- a/sources/ElementsCollection/xmlelementcollection.cpp +++ b/sources/ElementsCollection/xmlelementcollection.cpp @@ -433,6 +433,7 @@ bool XmlElementCollection::addElementDefinition(const QString &dir_path, const Q * @brief XmlElementCollection::copy * Copy the content represented by source (an element or a directory) to destination. * Destination must be a directory of this collection. + * If the destination already have an item at the same path of source, he will be replaced by source. * @param source : content to copy * @param destination : destination of the copy, must be a directory of this collection * @param rename : rename the copy with @rename else use the name of source @@ -545,10 +546,12 @@ ElementsLocation XmlElementCollection::copyDirectory(ElementsLocation &source, E /** * @brief XmlElementCollection::copyElement + * Copy the element represented by source to destination (must be a directory) + * If element already exist in destination he will be replaced by the new. * @param source : element to copy * @param destination : destination of the copy * @param rename : rename the copy with @rename else use the name of source - * @return + * @return The ElementsLocation of the copy */ ElementsLocation XmlElementCollection::copyElement(ElementsLocation &source, ElementsLocation &destination, QString rename) { @@ -575,13 +578,25 @@ ElementsLocation XmlElementCollection::copyElement(ElementsLocation &source, Ele //Remove the previous element with the same path QDomElement element = child(destination.collectionPath(false) + "/" + new_elmt_name); - if (!element.isNull()) + bool removed = false; + if (!element.isNull()) { element.parentNode().removeChild(element); + removed = true; + } //Get the xml directory where the new element must be added QDomElement dir_dom = directory(destination.collectionPath(false)); if (dir_dom.isNull()) return ElementsLocation(); dir_dom.appendChild(elmt_dom); - return ElementsLocation(destination.projectCollectionPath() + "/" + new_elmt_name); + ElementsLocation copy_loc(destination.projectCollectionPath() + "/" + new_elmt_name); + + if (removed) { + emit elementChanged(copy_loc.collectionPath(false)); + } + else { + emit elementAdded(copy_loc.collectionPath(false)); + } + + return copy_loc; } diff --git a/sources/ElementsCollection/xmlelementcollection.h b/sources/ElementsCollection/xmlelementcollection.h index 31149b313..62018364c 100644 --- a/sources/ElementsCollection/xmlelementcollection.h +++ b/sources/ElementsCollection/xmlelementcollection.h @@ -62,6 +62,12 @@ class XmlElementCollection : public QObject * @param collection_path, the path of element in this collection */ void elementAdded(QString collection_path); + /** + * @brief elementChanged + * This signal is emited when the defintion of the element at path was changed + * @param collection_path, the path of this element in this collection + */ + void elementChanged (QString collection_path); public slots: diff --git a/sources/diagramevent/diagrameventaddelement.cpp b/sources/diagramevent/diagrameventaddelement.cpp index 00a7697b0..bbe125347 100644 --- a/sources/diagramevent/diagrameventaddelement.cpp +++ b/sources/diagramevent/diagrameventaddelement.cpp @@ -172,22 +172,22 @@ bool DiagramEventAddElement::buildElement() { if (QETProject::integrateElementToProject(m_location, m_diagram -> project())) { - QString error_msg; - IntegrationMoveElementsHandler *integ_handler = new IntegrationMoveElementsHandler(); - m_integrate_path = m_diagram -> project() -> integrateElement(m_location.toString(), integ_handler, error_msg); - delete integ_handler; - if (m_integrate_path.isEmpty()) - { - qDebug() << "DiagramView::addDroppedElement : Impossible d'ajouter l'element. Motif : " << qPrintable(error_msg); + ElementsLocation loc = m_diagram->project()->importElement(m_location); + if (loc.exist()) { + m_integrate_path = loc.projectCollectionPath(); + } + else { + qDebug() << "DiagramView::addDroppedElement : Impossible d'ajouter l'element."; return false; } + } int state; - m_element = ElementFactory::Instance() -> createElement(m_location, 0, &state); + ElementsLocation loc(m_integrate_path); + m_element = ElementFactory::Instance() -> createElement(loc, 0, &state); //The creation of element failed, we delete it - if (state) - { + if (state) { delete m_element; return(false); } diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index d59674be5..52b9243a5 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -279,18 +279,18 @@ void DiagramView::dropEvent(QDropEvent *e) { Handle the drop of an element. @param e the QDropEvent describing the current drag'n drop */ -void DiagramView::handleElementDrop(QDropEvent *e) +void DiagramView::handleElementDrop(QDropEvent *event) { - //fetch the element location from the drop event - QString elmt_path = e -> mimeData() -> text(); - - ElementsLocation location(elmt_path); - - // verifie qu'il existe un element correspondant a cet emplacement - ElementsCollectionItem *dropped_item = QETApp::collectionItem(location); - if (!dropped_item) return; + //Build an element from the text of the mime data + ElementsLocation location(event->mimeData()->text()); - diagram()->setEventInterface(new DiagramEventAddElement(location, diagram(), mapToScene(e->pos()))); + if ( !(location.isElement() && location.exist()) ) + { + qDebug() << "DiagramView::handleElementDrop, location can't be use : " << location; + return; + } + + diagram()->setEventInterface(new DiagramEventAddElement(location, diagram(), mapToScene(event->pos()))); //Set focus to the view to get event this->setFocus(); } diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 8dee14680..a80b2f813 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -33,6 +33,7 @@ #include "reportproperties.h" #include "integrationmovetemplateshandler.h" #include "xmlelementcollection.h" +#include "importelementdialog.h" #include @@ -68,9 +69,7 @@ QETProject::QETProject(int diagrams, QObject *parent) : connect(collection_, SIGNAL(written()), this, SLOT(componentWritten())); m_elements_collection = new XmlElementCollection(this); - - // une categorie dediee aux elements integres automatiquement - ensureIntegrationCategoryExists(); + setupTitleBlockTemplatesCollection(); undo_stack_ = new QUndoStack(); @@ -146,17 +145,10 @@ QETProject::~QETProject() */ bool QETProject::integrateElementToProject(const ElementsLocation &location, const QETProject *project) { - //Integration element must be enable - QSettings settings; - bool auto_integration_enabled = settings.value("diagrameditor/integrate-elements", true).toBool(); + if (location.isFileSystem()) {return true;} + if (location.isProject() && (location.project() != project)) {return true;} - //the element belongs there a project and if so, is this another project of the project given by parameter? - bool elmt_from_project = location.project(); - bool elmt_from_another_project = elmt_from_project && location.project() != project; - - bool must_integrate_element = (elmt_from_another_project || (auto_integration_enabled && !elmt_from_project)); - - return(must_integrate_element); + return false; } /** @@ -638,24 +630,6 @@ bool QETProject::isEmpty() const { return(pertinent_diagrams > 0); } -/** - Cree une categorie dediee aux elements integres automatiquement dans le - projet si celle-ci n'existe pas deja. - @return true si tout s'est bien passe, false sinon -*/ -bool QETProject::ensureIntegrationCategoryExists() { - ElementsCategory *root_cat = rootCategory(); - if (!root_cat) return(false); - - if (root_cat -> category(integration_category_name)) return(true); - - ElementsCategory *integration_category = root_cat -> createCategory(integration_category_name); - if (!integration_category) return(false); - - integration_category -> setNames(namesListForIntegrationCategory()); - return(true); -} - /** @return la categorie dediee aux elements integres automatiquement dans le projet ou 0 si celle-ci n'a pu etre creee. @@ -669,134 +643,94 @@ ElementsCategory *QETProject::integrationCategory() const { } /** - Integre un element dans le projet. - Cette methode delegue son travail a la methode - integrateElement(const QString &, MoveElementsHandler *, QString &) - en lui passant un MoveElementsHandler approprie. - @param elmt_location Emplacement de l'element a integrer - @param error_msg Reference vers une chaine de caractere qui contiendra - eventuellement un message d'erreur - @return L'emplacement de l'element apres integration, ou une chaine vide si - l'integration a echoue. -*/ -QString QETProject::integrateElement(const QString &elmt_location, QString &error_msg) { - // handler dedie a l'integration d'element - IntegrationMoveElementsHandler *integ_handler = new IntegrationMoveElementsHandler(0); - QString integ_path = integrateElement(elmt_location, integ_handler, error_msg); - delete integ_handler; - - return(integ_path); -} + * @brief QETProject::importElement + * Import the element represented by @location to the embbeded collection of this project + * @param location + * @return the location of the imported element, location can be null. + */ +ElementsLocation QETProject::importElement(ElementsLocation &location) +{ + //Location isn't an element or doesn't exist + if (! (location.isElement() && location.exist()) ) { + return ElementsLocation(); + } -/** - Integre un element dans le projet. - Cette methode prend en parametre l'emplacement d'un element a integrer. - Chaque categorie mentionnee dans le chemin de cet element sera copiee de - maniere non recursive sous la categorie dediee a l'integration si elle - n'existe pas deja. - L'element sera ensuite copiee dans cette copie de la hierarchie d'origine. - En cas de probleme, error_message sera modifiee de facon a contenir un - message decrivant l'erreur rencontree. - @param elmt_path Emplacement de l'element a integrer - @param handler Gestionnaire a utiliser pour gerer les copies d'elements et categories - @param error_message Reference vers une chaine de caractere qui contiendra - eventuellement un message d'erreur - @return L'emplacement de l'element apres integration, ou une chaine vide si - l'integration a echoue. -*/ -QString QETProject::integrateElement(const QString &elmt_path, MoveElementsHandler *handler, QString &error_message) { - // on s'assure que le projet a une categorie dediee aux elements importes automatiquement - if (!ensureIntegrationCategoryExists()) - { - error_message = tr("Impossible de créer la catégorie pour l'intégration des éléments"); - return(QString()); + //Get the path where the element must be imported + QString import_path; + if (location.isFileSystem()) { + import_path = "import/" + location.collectionPath(false); } - - // accede a la categorie d'integration - ElementsCategory *integ_cat = integrationCategory(); - - // accede a l'element a integrer - ElementsCollectionItem *integ_item = QETApp::collectionItem(ElementsLocation(elmt_path)); - ElementDefinition *integ_elmt = integ_item ? integ_item -> toElement() : 0; - if (!integ_item || !integ_elmt) - { - error_message = tr("Impossible d'accéder à l'élément à intégrer"); - return(QString()); - } - - // recopie l'arborescence de l'element de facon non recursive - QList integ_par_cat = integ_elmt -> parentCategories(); - ElementsCategory *target_cat = integ_cat; - foreach(ElementsCategory *par_cat, integ_par_cat) - { - if (par_cat -> isRootCategory()) continue; - - if (ElementsCategory *existing_cat = target_cat -> category(par_cat -> pathName())) - { - // la categorie cible existe deja : on continue la progression - target_cat = existing_cat; + + if (location.isProject()) { + if (location.project() == this) { + return location; } - else - { - // la categorie cible n'existe pas : on la cree par recopie - ElementsCollectionItem *result_cat = par_cat -> copy(target_cat, handler, false); - if (!result_cat || !result_cat -> isCategory()) - { - error_message = QString(tr("Un problème s'est produit pendant la copie de la catégorie %1")).arg(par_cat -> location().toString()); - return(QString()); + + import_path = location.collectionPath(false); + } + + //Element already exist in the embedded collection, we ask what to do to user + if (m_elements_collection->exist(import_path)) { + ElementsLocation existing_location(import_path, this); + + //@existing_location and @location have the same uuid, so it is the same element + if (existing_location.uuid() == location.uuid()) { + return existing_location; + } + + ImportElementDialog ied; + if (ied.exec() == QDialog::Accepted) { + QET::Action action = ied.action(); + + //Use the exisitng element + if (action == QET::Ignore) { + return existing_location; } - target_cat = result_cat -> toCategory(); - } - } - - // recopie l'element - ElementsLocation result; - if (ElementDefinition *existing_elmt = target_cat -> element(integ_item -> pathName())) - { - - // l'element existe deja - on demande au handler ce que l'on doit faire - QET::Action action = handler -> elementAlreadyExists(integ_elmt, existing_elmt); - - if (action == QET::Ignore) - { - // il faut conserver et utiliser l'element deja integre - result = existing_elmt -> location(); - } - else if (action == QET::Erase) - { - // il faut ecraser l'element deja integre - BasicMoveElementsHandler *erase_handler = new BasicMoveElementsHandler(); - result = copyElementWithHandler(integ_elmt, target_cat, erase_handler, error_message); - delete erase_handler; - } - else if (action == QET::Rename) - { - // il faut faire cohabiter les deux elements en renommant le nouveau - QString integ_element_name = handler -> nameForRenamingOperation(); - BasicMoveElementsHandler *rename_handler = new BasicMoveElementsHandler(); - rename_handler -> setActionIfItemAlreadyExists(QET::Rename); - rename_handler -> setNameForRenamingOperation(integ_element_name); - result = copyElementWithHandler(integ_elmt, target_cat, rename_handler, error_message); - delete rename_handler; - } - else - { - // il faut annuler la pose de l'element - result = ElementsLocation(); - } - } - else - { - // integre l'element normalement - result = copyElementWithHandler(integ_elmt, target_cat, handler, error_message); + //Erase the existing element, and use the newer instead + else if (action == QET::Erase) { + ElementsLocation parent_loc = existing_location.parent(); + return m_elements_collection->copy(location, parent_loc); + } + //Add the new element with an other name. + else if (action == QET::Rename) { + int a = 0; + QString parent_path = existing_location.parent().projectCollectionPath(); + QString name_ = existing_location.fileName(); + name_.remove(".elmt"); - ElementsLocation location(elmt_path); - QString xml_path = m_elements_collection->addElement(location); - if (!xml_path.isNull()) emit elementIntegratedToCollection(this, xml_path); + ElementsLocation loc; + do + { + a++; + QString new_path = parent_path + "/" + name_ + QString::number(a) + ".elmt"; + loc = ElementsLocation (new_path); + } while (loc.exist()); + + ElementsLocation parent_loc = existing_location.parent(); + return m_elements_collection->copy(location, parent_loc, loc.fileName()); + } + else { + return ElementsLocation(); + } + } + else { + return ElementsLocation(); + } } - - if (!result.isNull()) emit(elementIntegrated(this, result)); - return(result.toString()); + //Element doesn't exist in the collection, we just import it + else { + ElementsLocation loc(m_elements_collection->addElement(location), this); + + if (!loc.exist()) { + qDebug() << "QETProject::importElement : failed to import location. " << location; + return ElementsLocation(); + } + else { + return loc; + } + } + + return ElementsLocation(); } /** diff --git a/sources/qetproject.h b/sources/qetproject.h index becd48fc5..5633cfc83 100644 --- a/sources/qetproject.h +++ b/sources/qetproject.h @@ -127,10 +127,8 @@ class QETProject : public QObject bool isReadOnly() const; void setReadOnly(bool); bool isEmpty() const; - bool ensureIntegrationCategoryExists(); ElementsCategory *integrationCategory() const; - QString integrateElement(const QString &, QString &); - QString integrateElement(const QString &, MoveElementsHandler *, QString &); + ElementsLocation importElement(ElementsLocation &location); QString integrateTitleBlockTemplate(const TitleBlockTemplateLocation &, MoveTitleBlockTemplatesHandler *handler); bool usesElement(const ElementsLocation &); bool usesTitleBlockTemplate(const TitleBlockTemplateLocation &); @@ -164,7 +162,6 @@ class QETProject : public QObject void readOnlyChanged(QETProject *, bool); void reportPropertiesChanged(QString); void XRefPropertiesChanged (); - void elementIntegratedToCollection(QETProject *project, QString path); private slots: void updateDiagramsFolioData(); diff --git a/sources/ui/importelementdialog.cpp b/sources/ui/importelementdialog.cpp new file mode 100644 index 000000000..fb77d167c --- /dev/null +++ b/sources/ui/importelementdialog.cpp @@ -0,0 +1,53 @@ +/* + Copyright 2006-2016 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "importelementdialog.h" +#include "ui_importelementdialog.h" + +ImportElementDialog::ImportElementDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::ImportElementDialog) +{ + ui->setupUi(this); + setUpWidget(); +} + +ImportElementDialog::~ImportElementDialog() +{ + delete ui; +} + +QET::Action ImportElementDialog::action() const +{ + if (ui->m_use_actual_rd->isChecked()) { return QET::Ignore; } + else if (ui->m_erase_actual_rb->isChecked()) { return QET::Erase; } + else if (ui->m_use_both_rb->isChecked()) { return QET::Rename; } + else return QET::Abort; +} + +void ImportElementDialog::setUpWidget() +{ + QButtonGroup *button_group = new QButtonGroup(this); + button_group->addButton(ui->m_use_actual_rd); + button_group->addButton(ui->m_use_drop_rb); + QButtonGroup *button_group_drop = new QButtonGroup(this); + button_group_drop->addButton(ui->m_erase_actual_rb); + button_group_drop->addButton(ui->m_use_both_rb); + + ui->m_use_drop_rb->setChecked(true); + ui->m_use_both_rb->setChecked(true); +} diff --git a/sources/ui/importelementdialog.h b/sources/ui/importelementdialog.h new file mode 100644 index 000000000..18ab33486 --- /dev/null +++ b/sources/ui/importelementdialog.h @@ -0,0 +1,46 @@ +/* + Copyright 2006-2016 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef IMPORTELEMENTDIALOG_H +#define IMPORTELEMENTDIALOG_H + +#include +#include "qet.h" + +namespace Ui { + class ImportElementDialog; +} + +class ImportElementDialog : public QDialog +{ + Q_OBJECT + + public: + explicit ImportElementDialog(QWidget *parent = 0); + ~ImportElementDialog(); + + QET::Action action() const; + + private: + void setUpWidget(); + void setUpConnection(); + + private: + Ui::ImportElementDialog *ui; +}; + +#endif // IMPORTELEMENTDIALOG_H diff --git a/sources/ui/importelementdialog.ui b/sources/ui/importelementdialog.ui new file mode 100644 index 000000000..47c086c70 --- /dev/null +++ b/sources/ui/importelementdialog.ui @@ -0,0 +1,147 @@ + + + ImportElementDialog + + + + 0 + 0 + 754 + 176 + + + + Intégration d'un élément + + + + + + L'élément a déjà été intégré dans le projet. Toutefois, la version que vous tentez de poser semble différente. Que souhaitez-vous faire ? + + + + + + + Utiliser l'élément déjà integré + + + + + + + Intégrer l'élément déposé + + + true + + + false + + + + + + + 15 + + + + + Écraser l'élément déjà intégé + + + + + + + Faire cohabiter les deux éléments + + + false + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + ImportElementDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + ImportElementDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + m_use_drop_rb + toggled(bool) + m_erase_actual_rb + setEnabled(bool) + + + 375 + 162 + + + 375 + 188 + + + + + m_use_drop_rb + toggled(bool) + m_use_both_rb + setEnabled(bool) + + + 375 + 162 + + + 375 + 214 + + + + +