QElectroTech  0.70
fileelementcollectionitem.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 
20 #include "elementslocation.h"
21 #include "qetapp.h"
22 #include "qeticons.h"
23 
24 #include <QDir>
25 
31 {}
32 
40 bool FileElementCollectionItem::setRootPath(const QString& path, bool set_data, bool hide_element)
41 {
42  QDir dir(path);
43  if (dir.exists()) {
44  m_path = path;
45  populate(set_data, hide_element);
46  return true;
47  }
48 
49  return false;
50 }
51 
57 {
58  if (isCollectionRoot())
59  return m_path;
60 
61  FileElementCollectionItem *feci = static_cast<FileElementCollectionItem *> (parent());
62  if (feci)
63  return feci->fileSystemPath() + "/" + m_path;
64  else
65  return QString();
66 }
67 
74 {
75  if (isDir())
76  return fileSystemPath();
77  else if (parent() && parent()->type() == FileElementCollectionItem::Type)
78  return static_cast<FileElementCollectionItem*>(parent())->fileSystemPath();
79  else
80  return QString();
81 }
82 
88 {
89  if (m_path.endsWith(".elmt"))
90  return false;
91  else
92  return true;
93 
94 }
95 
101 {
102  return (!isDir());
103 }
104 
110 {
111  if (!text().isNull())
112  return text();
113 
114  else if (isDir()) {
115  if (isCollectionRoot()) {
117  setText(QObject::tr("Collection QET"));
118  else if (m_path == QETApp::customElementsDirN())
119  setText(QObject::tr("Collection utilisateur"));
120  else
121  setText(QObject::tr("Collection inconnue"));
122  }
123  else {
124  //Open the qet_directory file, to get the traductions name of this dir
125  QFile dir_conf(fileSystemPath() + "/qet_directory");
126 
127  if (dir_conf.exists() && dir_conf.open(QIODevice::ReadOnly | QIODevice::Text)) {
128 
129  //Get the content of the file
130  QDomDocument document;
131  if (document.setContent(&dir_conf)) {
132  QDomElement root = document.documentElement();
133  if (root.tagName() == "qet-directory") {
134  NamesList nl;
135  nl.fromXml(root);
136  setText(nl.name());
137  }
138  }
139  }
140  }
141  }
142  else if (isElement()) {
144  setText(loc.name());
145  }
146 
147  return text();
148 }
149 
155 {
156  if (isCollectionRoot())
157  return QString();
158  else
159  return m_path;
160 }
161 
167 {
168  if (isCollectionRoot()) {
170  return "common://";
171  else
172  return "custom://";
173  }
174  else if (parent() && parent()->type() == FileElementCollectionItem::Type) {
175  ElementCollectionItem *eci = static_cast<ElementCollectionItem*>(parent());
176  if (eci->isCollectionRoot())
177  return eci->collectionPath() + m_path;
178  else
179  return eci->collectionPath() + "/" + m_path;
180  }
181  else
182  return QString();
183 }
184 
190 {
192  return true;
193  else
194  return false;
195 }
196 
202 {
203  return fileSystemPath().startsWith(QETApp::commonElementsDirN());
204 }
205 
211 {
212  return fileSystemPath().startsWith(QETApp::customElementsDirN());
213 }
214 
220 void FileElementCollectionItem::addChildAtPath(const QString &collection_name)
221 {
222  if (collection_name.isEmpty())
223  return;
224 
226  insertRow(rowForInsertItem(collection_name), feci);
227  feci->setPathName(collection_name);
228  feci->setUpData();
229 }
230 
236 {
237  //Setup the displayed name
238  localName();
239 
240  if (isDir())
241  setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
242  else
243  {
244  setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
245 
246  //Set the local name and all informations of the element
247  //in the data Qt::UserRole+1, these data will be use for search.
248  ElementsLocation location(collectionPath());
249  DiagramContext context = location.elementInformations();
250  QStringList search_list;
251  for (QString key : context.keys()) {
252  search_list.append(context.value(key).toString());
253  }
254  search_list.append(localName());
255  setData(search_list.join(" "));
256  }
257 
258  setToolTip(collectionPath());
259 }
260 
267 {
268  if (!icon().isNull())
269  return;
270 
271  if (isCollectionRoot()) {
273  setIcon(QIcon(":/ico/16x16/qet.png"));
274  else
275  setIcon(QIcon(":/ico/16x16/go-home.png"));
276  }
277  else {
278  if (isDir())
279  setIcon(QET::Icons::Folder);
280  else {
282  setIcon(loc.icon());
283  }
284  }
285 }
286 
294 void FileElementCollectionItem::setPathName(const QString& path_name, bool set_data, bool hide_element)
295 {
296  m_path = path_name;
297 
298  //This isn't an element, we create the childs
299  if (!path_name.endsWith(".elmt"))
300  populate(set_data, hide_element);
301 }
302 
308 void FileElementCollectionItem::populate(bool set_data, bool hide_element)
309 {
310  QDir dir (fileSystemPath());
311 
312  //Get all directory in this directory.
313  foreach(QString str, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
314  {
316  appendRow(feci);
317  feci->setPathName(str, set_data, hide_element);
318  if (set_data)
319  feci->setUpData();
320  }
321 
322  if (hide_element)
323  return;
324 
325  //Get all elmt file in this directory
326  dir.setNameFilters(QStringList() << "*.elmt");
327  foreach(QString str, dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
328  {
330  appendRow(feci);
331  feci->setPathName(str, set_data);
332  if (set_data)
333  feci->setUpData();
334  }
335 }
The ElementCollectionItem class This class represent a item (a directory or an element) in a element ...
virtual bool isCollectionRoot() const =0
bool isCustomCollection() const
FileElementCollectionItem::isCustomCollection.
QIcon Folder
Definition: qeticons.cpp:94
QVariant value(const QString &key) const
QString collectionPath() const override
FileElementCollectionItem::collectionPath.
QIcon icon() const
ElementLocation::icon.
DiagramContext elementInformations() const
ElementsLocation::elementInformations.
QList< QString > keys(KeyOrder=None) const
QIcon nl
Definition: qeticons.cpp:202
void addChildAtPath(const QString &collection_name) override
FileElementCollectionItem::addChildAtPath Ask to this item item to add a child with collection name ...
void populate(bool set_data=true, bool hide_element=false)
FileElementCollectionItem::populate Create the childs of this item.
void setPathName(const QString &path_name, bool set_data=true, bool hide_element=false)
FileElementCollectionItem::setPathName Set the name of this item in the file system path...
void setUpIcon() override
FileElementCollectionItem::setUpIcon SetUp the icon of this item. Because icon use several memory...
FileElementCollectionItem()
FileElementCollectionItem::FileElementCollectionItem Constructor.
bool isCommonCollection() const
FileElementCollectionItem::isCommonCollection.
QIcon tr
Definition: qeticons.cpp:204
QString name() const
ElementLocation::name.
void setUpData() override
FileElementCollectionItem::setUpData SetUp the data of this item.
QString name() const override
FileElementCollectionItem::name.
The FileElementCollectionItem class This class specialise ElementCollectionItem for manage a collecti...
virtual QString collectionPath() const =0
int rowForInsertItem(const QString &name)
ElementCollectionItem::rowForInsertItem Return the row for insert a new child item to this item with ...
bool setRootPath(const QString &path, bool set_data=true, bool hide_element=false)
FileElementCollectionItem::setRootPath Set path has root path for this file item. Use this function o...
static QString customElementsDirN()
QETApp::customElementsDirN like QString QETApp::customElementsDir but without "/" at the end...
Definition: qetapp.cpp:598
QString localName() override
FileElementCollectionItem::localName.
QString dirPath() const
FileElementCollectionItem::dirPath.
QString fileSystemPath() const
FileElementCollectionItem::fileSystemPath.
static QString commonElementsDirN()
QETApp::commonElementsDirN like QString QETApp::commonElementsDir but without "/" at the end...
Definition: qetapp.cpp:586
bool isCollectionRoot() const override
FileElementCollectionItem::isCollectionRoot.
bool isElement() const override
FileElementCollectionItem::isElement.
bool isDir() const override
FileElementCollectionItem::isDir.