QElectroTech  0.70
xmlprojectelementcollectionitem.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "qetproject.h"
19 #include "xmlelementcollection.h"
20 #include "qeticons.h"
22 
28 {}
29 
35 {
36  if (m_dom_element.tagName() == "category") return true;
37  else return false;
38 }
39 
45 {
46  if (m_dom_element.tagName() == "element") return true;
47  else return false;
48 }
49 
55 {
56  if (!text().isNull())
57  return text();
58 
59  if (isCollectionRoot()) {
60  if (m_project->title().isEmpty())
61  setText(QObject::tr("Projet sans titre"));
62  else
63  setText(m_project->title());
64  }
65  else {
67  setText(location.name());
68  }
69 
70  return text();
71 }
72 
78 {
79  return m_dom_element.attribute("name");
80 }
81 
87 {
89  QString p = loc.projectCollectionPath();
90  if (p.isEmpty())
91  p = QObject::tr("Collection");
92  return p;
93 }
94 
101 {
102  if (isCollectionRoot())
103  return "embed://";
104  else if (parent()){
105  XmlProjectElementCollectionItem *xpeci = static_cast<XmlProjectElementCollectionItem *>(parent());
106 
107  if (xpeci->isCollectionRoot())
108  return xpeci->embeddedPath() + name();
109  else
110  return xpeci->embeddedPath() + "/" + name();
111  }
112  else
113  return QString();
114 }
115 
121 {
122  if (!parent())
123  return true;
124  else if (parent()->type() != XmlProjectElementCollectionItem::Type)
125  return true;
126  else
127  return false;
128 }
129 
136 void XmlProjectElementCollectionItem::addChildAtPath(const QString &collection_name)
137 {
138  if (collection_name.isEmpty())
139  return;
140 
141  QString str (collection_name.endsWith(".elmt")? "element" : "category");
142  QDomElement child_element = m_dom_element.firstChildElement(str);
143 
144  while (!child_element.isNull()) {
145  if (child_element.attribute("name") == collection_name) {
147  insertRow(rowForInsertItem(collection_name), xpeci);
148  xpeci->setXmlElement(child_element, m_project);
149  xpeci->setUpData();
150  return;
151  }
152  else
153  child_element = child_element.nextSiblingElement(str);
154  }
155 }
156 
162 {
163  return m_project;
164 }
165 
173 void XmlProjectElementCollectionItem::setProject(QETProject *project, bool set_data, bool hide_element)
174 {
175  if (m_project)
176  return;
177 
178  m_project = project;
180  populate(set_data, hide_element);
181 }
182 
188 {
189  //Setup the displayed name
190  localName();
191 
192  if (isDir()) {
193  setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
194  }
195  else
196  {
197  setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
198 
199  //Set the local name and all informations of the element
200  //in the data Qt::UserRole+1, these data will be use for search.
202  DiagramContext context = location.elementInformations();
203  QStringList search_list;
204  for (QString key : context.keys()) {
205  search_list.append(context.value(key).toString());
206  }
207  search_list.append(localName());
208  setData(search_list.join(" "));
209  }
210 
211  setToolTip(collectionPath());
212 }
213 
220 {
221  if (!icon().isNull())
222  return;
223 
224  if (isCollectionRoot())
225  setIcon(QET::Icons::ProjectFileGP);
226  else if (isDir())
227  setIcon(QET::Icons::Folder);
228  else {
230  setIcon(loc.icon());
231  }
232 }
233 
239 void XmlProjectElementCollectionItem::populate(bool set_data, bool hide_element)
240 {
241  QList <QDomElement> dom_category = m_project->embeddedElementCollection()->directories(m_dom_element);
242  std::sort(dom_category.begin(), dom_category.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));});
243 
244  foreach (QDomElement element, dom_category)
245  {
247  appendRow(xpeci);
248  xpeci->setXmlElement(element, m_project, set_data, hide_element);
249  if (set_data)
250  xpeci->setUpData();
251  }
252 
253  if (hide_element)
254  return;
255 
256  QList <QDomElement> dom_elements = m_project->embeddedElementCollection()->elements(m_dom_element);
257  std::sort(dom_elements.begin(), dom_elements.end(), [](QDomElement a, QDomElement b){return (a.attribute("name") < b.attribute("name"));});
258 
259  foreach (QDomElement element, dom_elements)
260  {
262  appendRow(xpeci);
263  xpeci->setXmlElement(element, m_project, set_data);
264  if (set_data)
265  xpeci->setUpData();
266  }
267 }
268 
276 void XmlProjectElementCollectionItem::setXmlElement(const QDomElement& element, QETProject *project, bool set_data, bool hide_element)
277 {
278  m_dom_element = element;
279  m_project = project;
280  populate(set_data, hide_element);
281 }
QString title() const
Definition: qetproject.cpp:365
QString projectCollectionPath() const
ElementsLocation::projectCollectionPath.
XmlProjectElementCollectionItem()
XmlProjectElementCollectionItem::XmlProjectElementCollectionItem Constructor.
bool isDir() const override
XmlProjectElementCollectionItem::isDir.
QString collectionPath() const override
XmlProjectElementCollectionItem::collectionPath.
QIcon Folder
Definition: qeticons.cpp:94
void setUpData() override
XmlProjectElementCollectionItem::setUpData SetUp the data of this item.
QVariant value(const QString &key) const
QDomElement root() const
XmlElementCollection::root The root is the first DOM-Element the xml collection, the tag name of the ...
QIcon icon() const
ElementLocation::icon.
void setUpIcon() override
XmlProjectElementCollectionItem::setUpIcon SetUp the icon of this item. Because icon use several memo...
The XmlProjectElementCollectionItem class This class specialise ElementCollectionItem for manage an x...
XmlElementCollection * embeddedElementCollection() const
QETProject::embeddedCollection.
Definition: qetproject.cpp:229
DiagramContext elementInformations() const
ElementsLocation::elementInformations.
QList< QString > keys(KeyOrder=None) const
QList< QDomElement > directories(const QDomElement &parent_element) const
XmlElementCollection::directories.
QString name() const override
XmlProjectElementCollectionItem::name.
QETProject * project() const
XmlProjectElementCollectionItem::project.
QList< QDomElement > elements(const QDomElement &parent_element) const
XmlElementCollection::elements.
QIcon tr
Definition: qeticons.cpp:204
QIcon ProjectFileGP
Definition: qeticons.cpp:148
QString name() const
ElementLocation::name.
void setXmlElement(const QDomElement &element, QETProject *project, bool set_data=true, bool hide_element=false)
XmlProjectElementCollectionItem::setXmlElement Set the managed content of this item.
void setProject(QETProject *project, bool set_data=true, bool hide_element=false)
XmlProjectElementCollectionItem::setProject Set the project for this item. Use this method for set th...
int rowForInsertItem(const QString &name)
ElementCollectionItem::rowForInsertItem Return the row for insert a new child item to this item with ...
void addChildAtPath(const QString &collection_name) override
XmlProjectElementCollectionItem::addChildAtPath Ask to this item item to add a new child with collect...
QString localName() override
XmlProjectElementCollectionItem::localName.
bool isCollectionRoot() const override
XmlProjectElementCollectionItem::isCollectionRoot.
virtual QString embeddedPath() const
XmlProjectElementCollectionItem::embeddedPath.
bool isElement() const override
XmlProjectElementCollectionItem::isElement.
void populate(bool set_data=true, bool hide_element=false)
XmlProjectElementCollectionItem::populate Create the childs of this item.