diff --git a/sources/ElementsCollection/elementcollectionitem.cpp b/sources/ElementsCollection/elementcollectionitem.cpp index 91137cd21..a8979010f 100644 --- a/sources/ElementsCollection/elementcollectionitem.cpp +++ b/sources/ElementsCollection/elementcollectionitem.cpp @@ -63,6 +63,14 @@ bool ElementCollectionItem::removeChild(int row, int count) return true; } +bool ElementCollectionItem::insertChild(int row, ElementCollectionItem *item) +{ + if (m_child_items.contains(item)) return false; + + m_child_items.insert(row, item); + return true; +} + /** * @brief ElementCollectionItem::child * @param row diff --git a/sources/ElementsCollection/elementcollectionitem.h b/sources/ElementsCollection/elementcollectionitem.h index d76d9f50f..9e276848e 100644 --- a/sources/ElementsCollection/elementcollectionitem.h +++ b/sources/ElementsCollection/elementcollectionitem.h @@ -41,6 +41,7 @@ class ElementCollectionItem void appendChild (ElementCollectionItem *item); bool removeChild (int row, int count); + bool insertChild (int row, ElementCollectionItem *item); ElementCollectionItem *child(int row); int childCount() const; int columnCount() const; diff --git a/sources/ElementsCollection/elementscollectionmodel.cpp b/sources/ElementsCollection/elementscollectionmodel.cpp index d2e618c6e..65494b804 100644 --- a/sources/ElementsCollection/elementscollectionmodel.cpp +++ b/sources/ElementsCollection/elementscollectionmodel.cpp @@ -19,6 +19,7 @@ #include "elementcollectionitem.h" #include "qetapp.h" #include "fileelementcollectionitem.h" +#include "xmlprojectelementcollectionitem.h" /** * @brief ElementsCollectionModel::ElementsCollectionModel @@ -262,3 +263,46 @@ void ElementsCollectionModel::addCustomCollection() else delete feci; } + +/** + * @brief ElementsCollectionModel::addProject + * Add @project to the disalyed collection + * @param project + * @return true if project was successfully added. If project is already + * handled, return false. + */ +bool ElementsCollectionModel::addProject(QETProject *project) +{ + if (m_project_list.contains(project)) return false; + + m_project_list.append(project); + int row = m_project_list.indexOf(project); + beginInsertRows(QModelIndex(), row, row); + XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(project, m_root_item); + bool r = m_root_item->insertChild(row, xpeci); + endInsertRows(); + + return r; +} + +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())) + { + m_project_list.removeOne(project); + return true; + } + else + return false; +} + +/** + * @brief ElementsCollectionModel::project + * @return A list of project handled by this model + */ +QList ElementsCollectionModel::project() const { + return m_project_list; +} diff --git a/sources/ElementsCollection/elementscollectionmodel.h b/sources/ElementsCollection/elementscollectionmodel.h index 1844f9e3c..6125f86b4 100644 --- a/sources/ElementsCollection/elementscollectionmodel.h +++ b/sources/ElementsCollection/elementscollectionmodel.h @@ -21,10 +21,12 @@ #include class ElementCollectionItem; +class QETProject; +class QList; /** * @brief The ElementsCollectionModel class - * Provide a data model for collection of elements. + * Provide a data model for co;llection of elements. */ class ElementsCollectionModel : public QAbstractItemModel { @@ -52,9 +54,13 @@ class ElementsCollectionModel : public QAbstractItemModel void addCommonCollection(); void addCustomCollection(); + bool addProject(QETProject *project); + bool removeProject(QETProject *project); + QList project() const; private: ElementCollectionItem *m_root_item; + QList m_project_list; }; #endif // ELEMENTSCOLLECTIONMODEL_H diff --git a/sources/ElementsCollection/elementscollectionwidget.cpp b/sources/ElementsCollection/elementscollectionwidget.cpp index edbf0b28a..3925991ba 100644 --- a/sources/ElementsCollection/elementscollectionwidget.cpp +++ b/sources/ElementsCollection/elementscollectionwidget.cpp @@ -26,6 +26,8 @@ #include "elementscategoryeditor.h" #include "newelementwizard.h" #include "elementscategory.h" +#include "xmlprojectelementcollectionitem.h" +#include "qetproject.h" #include #include @@ -40,8 +42,7 @@ */ ElementsCollectionWidget::ElementsCollectionWidget(QWidget *parent): QWidget(parent), - m_model(nullptr), - m_item_at_context_menu(nullptr) + m_model(nullptr) { setUpWidget(); setUpAction(); @@ -68,6 +69,19 @@ ElementsCollectionModel *ElementsCollectionWidget::model() const { return m_model; } +/** + * @brief ElementsCollectionWidget::addProject + * Add @project to be displayed + * @param project + */ +void ElementsCollectionWidget::addProject(QETProject *project) { + m_model->addProject(project); +} + +void ElementsCollectionWidget::removeProject(QETProject *project) { + m_model->removeProject(project); +} + void ElementsCollectionWidget::setUpAction() { m_open_dir = new QAction(QET::Icons::DocumentOpen, tr("Ouvrir le dossier correspondant"), this); @@ -130,7 +144,7 @@ void ElementsCollectionWidget::setUpConnection() connect(m_new_element, &QAction::triggered, this, &ElementsCollectionWidget::newElement); connect(m_tree_view, &QTreeView::doubleClicked, [this](const QModelIndex &index) { - this->m_item_at_context_menu = static_cast(index.internalPointer()); + this->m_index_at_context_menu = index ; this->editElement();}); } @@ -145,14 +159,16 @@ void ElementsCollectionWidget::customContextMenu(const QPoint &point) if (!m_index_at_context_menu.isValid()) return; m_context_menu->clear(); - ElementCollectionItem *eci = static_cast(m_index_at_context_menu.internalPointer()); - m_item_at_context_menu = eci; + + ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); + bool add_open_dir = false; if (eci->isElement()) m_context_menu->addAction(m_edit_element); if (eci->type() == FileElementCollectionItem::Type) { + add_open_dir = true; FileElementCollectionItem *feci = static_cast(eci); if (!feci->isCommonCollection()) { @@ -170,9 +186,16 @@ void ElementsCollectionWidget::customContextMenu(const QPoint &point) m_context_menu->addAction(m_delete_element); } } + if (eci->type() == XmlProjectElementCollectionItem::Type) + { + XmlProjectElementCollectionItem *xpeci = static_cast(eci); + if (xpeci->isCollectionRoot()) + add_open_dir = true; + } m_context_menu->addSeparator(); - m_context_menu->addAction(m_open_dir); + if (add_open_dir) + m_context_menu->addAction(m_open_dir); m_context_menu->addAction(m_reload); m_context_menu->popup(mapToGlobal(m_tree_view->mapToParent(point))); @@ -184,12 +207,13 @@ void ElementsCollectionWidget::customContextMenu(const QPoint &point) */ void ElementsCollectionWidget::openDir() { - ElementCollectionItem *eci = m_item_at_context_menu; - m_item_at_context_menu = nullptr; + ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); + if (!eci) return; - if (!eci || (eci->type() != FileElementCollectionItem::Type)) return; - - QDesktopServices::openUrl(static_cast(eci)->dirPath()); + if (eci->type() == FileElementCollectionItem::Type) + QDesktopServices::openUrl(static_cast(eci)->dirPath()); + else if (eci->type() == XmlProjectElementCollectionItem::Type) + QDesktopServices::openUrl(static_cast(eci)->project()->currentDir()); } /** @@ -198,8 +222,7 @@ void ElementsCollectionWidget::openDir() */ void ElementsCollectionWidget::editElement() { - ElementCollectionItem *eci = m_item_at_context_menu; - m_item_at_context_menu = nullptr; + ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); if (!eci || !eci->isElement() || @@ -215,8 +238,7 @@ void ElementsCollectionWidget::editElement() */ void ElementsCollectionWidget::deleteElement() { - ElementCollectionItem *eci = m_item_at_context_menu; - m_item_at_context_menu = nullptr; + ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); if (!eci) return; if (!(eci->isElement() && eci->canRemoveContent())) return; @@ -243,8 +265,7 @@ void ElementsCollectionWidget::deleteElement() */ void ElementsCollectionWidget::deleteDirectory() { - ElementCollectionItem *eci = m_item_at_context_menu; - m_item_at_context_menu = nullptr; + ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); if (!eci) return; if (!(eci->isDir() && eci->canRemoveContent())) return; @@ -273,8 +294,7 @@ void ElementsCollectionWidget::deleteDirectory() */ void ElementsCollectionWidget::editDirectory() { - ElementCollectionItem *eci = m_item_at_context_menu; - m_item_at_context_menu = nullptr; + ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); if (eci->type() != FileElementCollectionItem::Type) return; @@ -293,8 +313,7 @@ void ElementsCollectionWidget::editDirectory() */ void ElementsCollectionWidget::newDirectory() { - ElementCollectionItem *eci = m_item_at_context_menu; - m_item_at_context_menu = nullptr; + ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); if (eci->type() != FileElementCollectionItem::Type) return; @@ -313,8 +332,7 @@ void ElementsCollectionWidget::newDirectory() */ void ElementsCollectionWidget::newElement() { - ElementCollectionItem *eci = m_item_at_context_menu; - m_item_at_context_menu = nullptr; + ElementCollectionItem *eci = elementCollectionItemForIndex(m_index_at_context_menu); if (eci->type() != FileElementCollectionItem::Type) return; @@ -340,6 +358,10 @@ void ElementsCollectionWidget::reload() new_model->addCommonCollection(); new_model->addCustomCollection(); + if (m_model) + foreach (QETProject *project, m_model->project()) + new_model->addProject(project); + QList list = new_model->items(); m_progress_bar->setMaximum(list.size()); m_progress_bar->setValue(0); @@ -429,3 +451,12 @@ void ElementsCollectionWidget::showAndExpandItem(const QModelIndex &index, bool m_tree_view->setRowHidden(index.row(), index.parent(), false); m_tree_view->expand(index); } + +/** + * @brief ElementsCollectionWidget::elementCollectionItemForIndex + * @param index + * @return The internal pointer of index casted to ElementCollectionItem; + */ +ElementCollectionItem *ElementsCollectionWidget::elementCollectionItemForIndex(const QModelIndex &index) { + return static_cast(index.internalPointer()); +} diff --git a/sources/ElementsCollection/elementscollectionwidget.h b/sources/ElementsCollection/elementscollectionwidget.h index bb54e1227..bc2566630 100644 --- a/sources/ElementsCollection/elementscollectionwidget.h +++ b/sources/ElementsCollection/elementscollectionwidget.h @@ -28,6 +28,7 @@ class QMenu; class QLineEdit; class ElementCollectionItem; class QProgressBar; +class QETProject; /** * @brief The ElementsCollectionWidget class @@ -45,6 +46,9 @@ class ElementsCollectionWidget : public QWidget void expandFirstItems(); ElementsCollectionModel *model() const; + void addProject (QETProject *project); + void removeProject (QETProject *project); + private: void setUpAction(); void setUpWidget(); @@ -62,6 +66,7 @@ class ElementsCollectionWidget : public QWidget void hideCollection(bool hide = true); void hideItem(bool hide, const QModelIndex &index = QModelIndex(), bool recursive = true); void showAndExpandItem (const QModelIndex &index, bool recursive = true); + ElementCollectionItem *elementCollectionItemForIndex (const QModelIndex &index); private: ElementsCollectionModel *m_model; @@ -69,7 +74,6 @@ class ElementsCollectionWidget : public QWidget QTreeView *m_tree_view; QVBoxLayout *m_main_vlayout; QMenu *m_context_menu; - ElementCollectionItem *m_item_at_context_menu; QModelIndex m_index_at_context_menu; QProgressBar *m_progress_bar; diff --git a/sources/ElementsCollection/fileelementcollectionitem.cpp b/sources/ElementsCollection/fileelementcollectionitem.cpp index bcdb71405..e717ed412 100644 --- a/sources/ElementsCollection/fileelementcollectionitem.cpp +++ b/sources/ElementsCollection/fileelementcollectionitem.cpp @@ -130,7 +130,7 @@ QString FileElementCollectionItem::collectionPath() const QVariant FileElementCollectionItem::data(int column, int role) { //element collection have only one column - if (column > 1) + if (column > 0) return QVariant(); switch (role) @@ -169,8 +169,6 @@ QVariant FileElementCollectionItem::data(int column, int role) return QVariant(); break; } - - return QVariant(); } /** diff --git a/sources/ElementsCollection/xmlelementcollection.cpp b/sources/ElementsCollection/xmlelementcollection.cpp new file mode 100644 index 000000000..9b01e7a11 --- /dev/null +++ b/sources/ElementsCollection/xmlelementcollection.cpp @@ -0,0 +1,132 @@ +/* + Copyright 2006-2015 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 "xmlelementcollection.h" +#include "qdebug.h" +#include "nameslist.h" + +/** + * @brief XmlElementCollection::XmlElementCollection + * Build an empty collection + * @param parent + */ +XmlElementCollection::XmlElementCollection(QObject *parent) : + QObject(parent) +{ + QDomElement collection = m_dom_document.createElement("collection"); + m_dom_document.appendChild(collection); + QDomElement import = m_dom_document.createElement("category"); + import.setAttribute("name", "import"); + collection.appendChild(import); + + NamesList names; + + const QChar russian_data[24] = { 0x0418, 0x043C, 0x043F, 0x043E, 0x0440, 0x0442, 0x0438, 0x0440, 0x043E, 0x0432, 0x0430, 0x043D, 0x043D, 0x044B, 0x0435, 0x0020, 0x044D, 0x043B, 0x0435, 0x043C, 0x0435, 0x043D, 0x0442, 0x044B }; + const QChar greek_data[18] = { 0x0395, 0x03b9, 0x03c3, 0x03b7, 0x03b3, 0x03bc, 0x03ad, 0x03bd, 0x03b1, 0x0020, 0x03c3, 0x03c4, 0x03bf, 0x03b9, 0x03c7, 0x03b5, 0x03af, 0x03b1 }; + + names.addName("fr", "Éléments importés"); + names.addName("en", "Imported elements"); + names.addName("de", "Importierte elemente"); + names.addName("es", "Elementos importados"); + names.addName("ru", QString(russian_data, 24)); + names.addName("cs", "Zavedené prvky"); + names.addName("pl", "Elementy importowane"); + names.addName("pt", "elementos importados"); + names.addName("it", "Elementi importati"); + names.addName("el", QString(greek_data, 18)); + names.addName("nl", "Elementen geïmporteerd"); + names.addName("hr", "Uvezeni elementi"); + names.addName("ca", "Elements importats"); + names.addName("ro", "Elemente importate"); + + import.appendChild(names.toXml(m_dom_document)); +} + +/** + * @brief XmlElementCollection::XmlElementCollection + * Constructor with an collection. The tagName of @dom_element must be "collection" + * @param dom_element -the collection in a dom_element (the dom element in cloned) + * @param parent -parent QObject + */ +XmlElementCollection::XmlElementCollection(const QDomElement &dom_element, QObject *parent) : + QObject(parent) +{ + QDomElement collection = m_dom_document.createElement("collection"); + m_dom_document.appendChild(collection); + collection.appendChild(dom_element.firstChildElement("category").cloneNode(true)); +} + +/** + * @brief XmlElementCollection::root + * @return The root QDomElement of the collection + */ +QDomElement XmlElementCollection::root() const { + return m_dom_document.documentElement(); +} + +/** + * @brief XmlElementCollection::childs + * @param parent_element + * @return All childs element in the @parent_element tree + */ +QDomNodeList XmlElementCollection::childs(const QDomElement &parent_element) +{ + if (parent_element.ownerDocument() != m_dom_document) return QDomNodeList(); + return parent_element.childNodes(); +} + +/** + * @brief XmlElementCollection::directory + * @param parent_element + * @return A list of directory stored in @parent_element + */ +QList XmlElementCollection::directory(const QDomElement &parent_element) +{ + QList directory_list; + QDomNodeList node_list = childs(parent_element); + if (node_list.isEmpty()) return directory_list; + + for (int i=0 ; i < node_list.count() ; i++) + { + QDomNode node = node_list.at(i); + if (node.isElement() && node.toElement().tagName() == "category") + directory_list << node.toElement(); + } + + return directory_list; +} + +/** + * @brief XmlElementCollection::elements + * @param parent_element + * @return A list of element stored in @parent_element + */ +QList XmlElementCollection::elements(const QDomElement &parent_element) +{ + QList element_list; + QDomNodeList node_list = childs(parent_element); + if (node_list.isEmpty()) return element_list; + + for (int i=0 ; i < node_list.count() ; i++) + { + QDomNode node = node_list.at(i); + if (node.isElement() && node.toElement().tagName() == "element") + element_list << node.toElement(); + } + + return element_list; +} diff --git a/sources/ElementsCollection/xmlelementcollection.h b/sources/ElementsCollection/xmlelementcollection.h new file mode 100644 index 000000000..84ad91dc6 --- /dev/null +++ b/sources/ElementsCollection/xmlelementcollection.h @@ -0,0 +1,46 @@ +/* + Copyright 2006-2015 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 XMLELEMENTCOLLECTION_H +#define XMLELEMENTCOLLECTION_H + +#include +#include + +class QDomElement; + +class XmlElementCollection : public QObject +{ + Q_OBJECT + public: + XmlElementCollection (QObject *parent = nullptr); + XmlElementCollection (const QDomElement &dom_element, QObject *parent = nullptr); + QDomElement root() const; + QDomNodeList childs(const QDomElement &parent_element); + QList directory(const QDomElement &parent_element); + QList elements(const QDomElement &parent_element); + + signals: + + public slots: + + private: + QDomDocument m_dom_document; + +}; + +#endif // XMLELEMENTCOLLECTION_H diff --git a/sources/ElementsCollection/xmlprojectelementcollectionitem.cpp b/sources/ElementsCollection/xmlprojectelementcollectionitem.cpp new file mode 100644 index 000000000..51ab4e6c1 --- /dev/null +++ b/sources/ElementsCollection/xmlprojectelementcollectionitem.cpp @@ -0,0 +1,256 @@ +/* + Copyright 2006-2015 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 "xmlprojectelementcollectionitem.h" +#include "qetproject.h" +#include "qeticons.h" +#include "xmlelementcollection.h" +#include "nameslist.h" +#include "qetapp.h" +#include + +/** + * @brief XmlProjectElementCollectionItem::XmlProjectElementCollectionItem + * Default constructor. + * @param project -project for this item + * @param parent -paretn item + */ +XmlProjectElementCollectionItem::XmlProjectElementCollectionItem(QETProject *project, ElementCollectionItem *parent) : + ElementCollectionItem(parent), + m_project(project) +{ + m_dom_element = project->embeddedElementCollection()->root(); + populate(); +} + +/** + * @brief XmlProjectElementCollectionItem::XmlProjectElementCollectionItem + * Private constructor + * @param project -project for this item + * @param dom_element: the dom_element must represent this item + * @param parent + */ +XmlProjectElementCollectionItem::XmlProjectElementCollectionItem(QETProject *project, const QDomElement &dom_element, ElementCollectionItem *parent) : + ElementCollectionItem(parent), + m_project(project), + m_dom_element(dom_element) +{ + populate(); +} + +/** + * @brief XmlProjectElementCollectionItem::~XmlProjectElementCollectionItem + */ +XmlProjectElementCollectionItem::~XmlProjectElementCollectionItem() +{} + +/** + * @brief XmlProjectElementCollectionItem::data + * The data used by the view who display this item through the model + * @param column + * @param role + * @return + */ +QVariant XmlProjectElementCollectionItem::data(int column, int role) +{ + if (column > 0) + return QVariant(); + + switch (role) + { + case Qt::DisplayRole: + return name(); + break; + case Qt::DecorationRole: + if (isCollectionRoot()) + return QIcon(QET::Icons::ProjectFileGP); + else if (isDir()) + return QET::Icons::Folder; + else + return QET::Icons::Element; + break; + case Qt::ToolTipRole: + if (isCollectionRoot()) + return m_project->filePath(); + else + return collectionPath(); + break; + default: + return QVariant(); + } +} + +/** + * @brief XmlProjectElementCollectionItem::mimeData + * @return The mimedata of this item + */ +QMimeData *XmlProjectElementCollectionItem::mimeData() +{ + QMimeData *mime_data = new QMimeData(); + mime_data->setText(collectionPath()); + + if (isElement()) + mime_data->setData("application/x-qet-element-uri", collectionPath().toLatin1()); + else + mime_data->setData("application/x-qet-category-uri", collectionPath().toLatin1()); + + return mime_data; +} + +/** + * @brief XmlProjectElementCollectionItem::flags + * @return The flags of this item + */ +Qt::ItemFlags XmlProjectElementCollectionItem::flags() +{ + if (isDir()) + return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled); + else + return (Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled); +} + +/** + * @brief XmlProjectElementCollectionItem::isCollectionRoot + * @return True if this item represent the root collection of a project + */ +bool XmlProjectElementCollectionItem::isCollectionRoot() const +{ + if (!m_parent_item) return true; + else if (m_parent_item->type() != XmlProjectElementCollectionItem::Type) return true; + else return false; +} + +/** + * @brief XmlProjectElementCollectionItem::name + * @return The name of this item, name is notably use for Qt::DisplayRole data + */ +QString XmlProjectElementCollectionItem::name() +{ + if (!m_name.isNull()) return m_name; + + if (isCollectionRoot()) + { + if (m_project->title().isEmpty()) + return QString("Projet sans titre"); + else + return m_project->title(); + } + else + { + NamesList nl; + if (isDir()) + { + nl.fromXml(m_dom_element); + if (nl.name().isEmpty()) + m_name = m_dom_element.attribute("name"); + else + m_name = nl.name(); + } + else + { + nl.fromXml(m_dom_element.firstChildElement("definition")); + if (nl.name().isEmpty()) + m_name = m_dom_element.attribute("name"); + else + m_name = nl.name(); + } + return m_name; + } +} + +/** + * @brief XmlProjectElementCollectionItem::isValid + * @return Always true + */ +bool XmlProjectElementCollectionItem::isValid() const { + return true; +} + +/** + * @brief XmlProjectElementCollectionItem::project + * @return The project for this collection item + */ +QETProject *XmlProjectElementCollectionItem::project() const { + return m_project; +} + +/** + * @brief XmlProjectElementCollectionItem::isDir + * @return true if this item represent a directory + */ +bool XmlProjectElementCollectionItem::isDir() const +{ + if (m_dom_element.tagName() == "category") return true; + else return false; +} + +/** + * @brief XmlProjectElementCollectionItem::isElement + * @return true if this item represent an element + */ +bool XmlProjectElementCollectionItem::isElement() const +{ + if (m_dom_element.tagName() == "element") return true; + else return false; +} + +/** + * @brief XmlProjectElementCollectionItem::collectionPath + * @return The collection path of this item + */ +QString XmlProjectElementCollectionItem::collectionPath() const +{ + if (isCollectionRoot()) + { + QString path; + return path + "project" + QString::number(QETApp::projectId(m_project)) + "+embed://"; + } + else + { + XmlProjectElementCollectionItem *parent = static_cast(m_parent_item); + + if (parent->isCollectionRoot()) + return parent->collectionPath() + m_dom_element.attribute("name"); + else + return parent->collectionPath() + "/" + m_dom_element.attribute("name"); + } +} + +/** + * @brief XmlProjectElementCollectionItem::populate + * Populate this item + */ +void XmlProjectElementCollectionItem::populate() +{ + QList dom_category = m_project->embeddedElementCollection()->directory(m_dom_element); + std::sort(dom_category.begin(), dom_category.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));}); + + foreach (QDomElement element, dom_category) + { + XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(m_project, element, this); + this->appendChild(xpeci); + } + + QList dom_elements = m_project->embeddedElementCollection()->elements(m_dom_element); + std::sort(dom_elements.begin(), dom_elements.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));}); + + foreach (QDomElement element, dom_elements) + { + XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem(m_project, element, this); + this->appendChild(xpeci); + } +} diff --git a/sources/ElementsCollection/xmlprojectelementcollectionitem.h b/sources/ElementsCollection/xmlprojectelementcollectionitem.h new file mode 100644 index 000000000..1c4b352c7 --- /dev/null +++ b/sources/ElementsCollection/xmlprojectelementcollectionitem.h @@ -0,0 +1,63 @@ +/* + Copyright 2006-2015 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 XMLPROJECTELEMENTCOLLECTIONITEM_H +#define XMLPROJECTELEMENTCOLLECTIONITEM_H + +#include "elementcollectionitem.h" + +#include + +class QETProject; + +/** + * @brief The XmlProjectElementCollectionItem class + * This class specialise ElementCollectionItem for manage an xml collection embedded in a project. + */ +class XmlProjectElementCollectionItem : public ElementCollectionItem +{ + public: + XmlProjectElementCollectionItem(QETProject *project, ElementCollectionItem *parent = nullptr); + private: + XmlProjectElementCollectionItem (QETProject *project, const QDomElement &dom_element, ElementCollectionItem *parent = nullptr); + public: + ~XmlProjectElementCollectionItem(); + + enum {Type = UserType + 2}; + virtual int type() const {return Type;} + + virtual QVariant data(int column, int role); + virtual QMimeData *mimeData(); + virtual Qt::ItemFlags flags(); + + virtual bool isCollectionRoot() const; + virtual QString name(); + virtual bool isValid() const; + QETProject *project() const; + virtual bool isDir() const; + virtual bool isElement() const; + QString collectionPath() const; + + private: + void populate(); + + private: + QETProject *m_project; + QDomElement m_dom_element; +}; + +#endif // XMLPROJECTELEMENTCOLLECTIONITEM_H diff --git a/sources/qetdiagrameditor.cpp b/sources/qetdiagrameditor.cpp index 0a0712938..435455e56 100644 --- a/sources/qetdiagrameditor.cpp +++ b/sources/qetdiagrameditor.cpp @@ -931,6 +931,8 @@ bool QETDiagramEditor::addProject(QETProject *project, bool update_panel) { addProjectView(project_view); undo_group.addStack(project -> undoStack()); + + m_element_collection_widget->addProject(project); // met a jour le panel d'elements if (update_panel) { @@ -1720,6 +1722,7 @@ void QETDiagramEditor::projectWasClosed(ProjectView *project_view) { QETProject *project = project_view -> project(); if (project) { pa -> elementsPanel().projectWasClosed(project); + m_element_collection_widget->removeProject(project); undo_group.removeStack(project -> undoStack()); QETApp::unregisterProject(project); } diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 0b281b626..2d378f74e 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -32,6 +32,7 @@ #include "numerotationcontext.h" #include "reportproperties.h" #include "integrationmovetemplateshandler.h" +#include "xmlelementcollection.h" #include @@ -51,7 +52,8 @@ QETProject::QETProject(int diagrams, QObject *parent) : read_only_ (false ), titleblocks_ (this ), folioSheetsQuantity (0 ), - m_auto_conductor (true ) + m_auto_conductor (true ), + m_elements_collection (nullptr) { // 0 a n schema(s) vide(s) int diagrams_count = qMax(0, diagrams); @@ -64,6 +66,8 @@ QETProject::QETProject(int diagrams, QObject *parent) : collection_ -> setProtocol("embed"); collection_ -> setProject(this); connect(collection_, SIGNAL(written()), this, SLOT(componentWritten())); + + m_elements_collection = new XmlElementCollection(this); // une categorie dediee aux elements integres automatiquement ensureIntegrationCategoryExists(); @@ -87,7 +91,8 @@ QETProject::QETProject(const QString &path, QObject *parent) : read_only_ (false ), titleblocks_ (this ), folioSheetsQuantity (0 ), - m_auto_conductor (true ) + m_auto_conductor (true ), + m_elements_collection (nullptr) { //Open the file QFile project_file(path); @@ -204,6 +209,14 @@ ElementsCollection *QETProject::embeddedCollection() const { return(collection_); } +/** + * @brief QETProject::embeddedCollection + * @return The embedded collection + */ +XmlElementCollection *QETProject::embeddedElementCollection() const { + return m_elements_collection; +} + /** @return the title block templates collection enbeedded within this project */ @@ -1141,9 +1154,15 @@ void QETProject::readElementsCollectionXml(QDomDocument &xml_project) } if (collection_root.isNull()) //Make an empty collection + { collection_ = new XmlElementsCollection(); + m_elements_collection = new XmlElementCollection(this); + } else //Read the collection + { collection_ = new XmlElementsCollection(collection_root); + m_elements_collection = new XmlElementCollection(collection_root, this); + } collection_ -> setProtocol("embed"); collection_ -> setProject(this); diff --git a/sources/qetproject.h b/sources/qetproject.h index cc8c6773c..12b4c89a9 100644 --- a/sources/qetproject.h +++ b/sources/qetproject.h @@ -38,6 +38,7 @@ class MoveElementsHandler; class MoveTitleBlockTemplatesHandler; class NumerotationContext; class QUndoStack; +class XmlElementCollection; /** This class represents a QET project. Typically saved as a .qet file, it @@ -85,6 +86,7 @@ class QETProject : public QObject void setFolioSheetsQuantity(int); /// set the folio sheets quantity for this project int folioIndex(const Diagram *) const; ElementsCollection *embeddedCollection() const; + XmlElementCollection *embeddedElementCollection()const; TitleBlockTemplatesProjectCollection *embeddedTitleBlockTemplatesCollection(); QString filePath(); void setFilePath(const QString &); @@ -119,7 +121,6 @@ class QETProject : public QObject bool autoConductor () const; void setAutoConductor (bool ac); - QDomDocument toXml(); bool close(); QETResult write(); @@ -230,6 +231,7 @@ class QETProject : public QObject /// Folio List Sheets quantity for this project. int folioSheetsQuantity; bool m_auto_conductor; + XmlElementCollection *m_elements_collection; }; Q_DECLARE_METATYPE(QETProject *) #endif