Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYLibraryWidget.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
21 #include <qlabel.h>
22 #include <qcombobox.h>
23 #include <qlistwidget.h>
24 #include <qmessagebox.h>
25 // Added by qt3to4:
26 #include <QGridLayout>
27 #include <QList>
28 #include <QKeyEvent>
29 
34 #include "TYLibraryWidget.h"
35 
36 #define TR(id) OLocalizator::getString("TYLibraryWidget", (id))
37 
38 TYLibraryWidget::TYLibraryWidget(int filter, QWidget* parent, const char* name, Qt::WindowFlags f)
39  : QWidget(parent, f)
40 {
41  setObjectName(name);
42  setWindowTitle(TR("id_caption"));
43  QGridLayout* pLayout = new QGridLayout();
44  setLayout(pLayout);
45 
46  _pCategoryCurrentLabel = new QLabel(this);
47  pLayout->addWidget(_pCategoryCurrentLabel, 0, 0);
48 
49  _pCategoryChoiceListBox = new QComboBox(this);
50  pLayout->addWidget(_pCategoryChoiceListBox, 1, 0);
51 
52  _pElementChoiceListBox = new QListWidget(this);
53  _pElementChoiceListBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
54  pLayout->addWidget(_pElementChoiceListBox, 2, 0);
55 
56  // Recuperation du dossier contenant la bibliotheque
57  QString dirPath;
58  if (TYPreferenceManager::exists(TYDIRPREFERENCEMANAGER, "BiblioPath"))
59  {
60  dirPath = TYPreferenceManager::getString(TYDIRPREFERENCEMANAGER, "BiblioPath");
61  }
62  else
63  {
64  dirPath = QDir::toNativeSeparators(getTYApp()->tympanUserDir() + "/library/");
65  TYPreferenceManager::setString(TYDIRPREFERENCEMANAGER, "BiblioPath", dirPath);
66  }
67 
68  QDir dir(dirPath);
69 
70  _rootDir = dir.path();
72  _currentFilter = filter;
73 
74  // Si la bibliotheque n'existe pas, on la cree
75  if (!dir.exists())
76  {
77  dir.mkdir(dirPath);
78  }
79 
80  // On rempli la liste
81  filtering(dir, filter);
82 
83  QObject::connect(_pCategoryChoiceListBox, SIGNAL(currentTextChanged(QString)), this,
84  SLOT(comboboxItemChanged(QString)));
85 
86  QObject::connect(_pElementChoiceListBox, &QListWidget::itemSelectionChanged, this,
88  // QListWidget::highlighted does not exist and connecting TYLibraryWidget::highlighted and
89  // TYLibraryWidget::updateHighlight together would only result in an infinite signal/slot loop
90  // QObject::connect(_pElementChoiceListBox, &QListWidget::highlighted, this,
91  // &TYLibraryWidget::updateHighlight);
92 
93  updateFrame();
94 }
95 
97 {
98  QObject::disconnect(_pCategoryChoiceListBox, SIGNAL(currentTextChanged(QString)), this,
99  SLOT(comboboxItemChanged(QString)));
100 
101  QObject::connect(_pElementChoiceListBox, &QListWidget::itemSelectionChanged, this,
103  // QObject::connect(_pElementChoiceListBox, &QListWidget::highlighted, this,
104  // &TYLibraryWidget::updateHighlight);
105 }
106 
107 const QList<QString>& TYLibraryWidget::getSelectedEltsList() const
108 {
109  return _selectedEltsList;
110 }
111 
112 const QList<QString>& TYLibraryWidget::getEltsList() const
113 {
114  return _eltsList;
115 }
116 
117 const QString TYLibraryWidget::getCurrentDir() const
118 {
119  return _currentDir;
120 }
121 
123 {
124  _selectedEltsList.clear();
125 
126  for (unsigned int i = 0; i < _pElementChoiceListBox->count(); ++i)
127  {
128  if (_pElementChoiceListBox->item(i)->isSelected())
129  {
130  _selectedEltsList.append(_pElementChoiceListBox->item(i)->text());
131  }
132  }
133 
134  if (_selectedEltsList.isEmpty())
135  {
136  emit highlighted(QString());
137  }
138  else
139  {
140  emit highlighted("ok");
141  }
142 }
143 
144 void TYLibraryWidget::updateHighlight(const QString& str)
145 {
146  if (_selectedEltsList.isEmpty())
147  {
148  emit highlighted(QString());
149  }
150  else
151  {
152  emit highlighted(str);
153  }
154 }
155 
156 void TYLibraryWidget::updateCategory(const QString& str)
157 {
158  if (!_isFiltering)
159  {
160  QDir new_dir(_currentDir + "/" + str);
161  filtering(new_dir, _currentFilter);
162  emit highlighted(QString());
163  }
164 }
165 
166 void TYLibraryWidget::filtering(const QDir& dir, int filter)
167 {
168  _isFiltering = true;
169 
170  // Affichage de la liste des sous-repertoires (categrorie d'objets)
171  QStringList subdirs_filters;
172  subdirs_filters.append("*");
173  QStringList subdirsList = dir.entryList(subdirs_filters, QDir::Dirs, QDir::Name);
174  _pCategoryChoiceListBox->clear();
175  _currentDir = dir.path();
176  for (unsigned int j = 0; j < subdirsList.count(); ++j)
177  {
178  QString subdirname = subdirsList[j];
179  if (subdirname == ".." && _currentDir != _rootDir)
180  // on ne peut pas remonter depuis le repertoire racine de library
181  {
182  _pCategoryChoiceListBox->addItem(subdirname);
183  }
184  else
185  {
186  _pCategoryChoiceListBox->addItem(subdirname);
187  }
188  }
189 
190  QString categName = dir.canonicalPath();
191  QDir rootDir(_rootDir);
192  categName.remove(0, rootDir.canonicalPath().length());
193  _pCategoryCurrentLabel->setText("." + categName);
194 
195  // Affichage de la liste des objets filtree par type
196  QStringList filters;
197  filters.append("*.xml");
198  QStringList fileList = dir.entryList(filters, QDir::Files, QDir::Name);
199 
200  _pElementChoiceListBox->clear();
201  _eltsList.clear();
202 
203  int count = 0;
204  for (unsigned int i = 0; i < fileList.count(); ++i)
205  {
206  TYXMLManager xmlManager;
207 
208  QString filename = QDir::toNativeSeparators(dir.path() + "/" + fileList[i]);
209  QString eltName;
210  xmlManager.getEltType(filename, eltName);
211 
212  switch (filter)
213  {
214  case eNoneFilter:
215  {
216  QString item = fileList[i].section(".", -2, -2);
217  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
218  _eltsList.push_back(item);
219  }
220  break;
221  case eSiteFilter:
222  {
223  if ((eltName == "TYSiteNode") || (eltName == "TYInfrastructure") ||
224  (eltName == "TYUserSourcePonctuelle") || (eltName == "TYBatiment") ||
225  (eltName == "TYMachine") || (eltName == "TYReseauTransport") || (eltName == "TYRoute") ||
226  (eltName == "TYTopographie") || (eltName == "TYPlanEau") || (eltName == "TYCoursEau") ||
227  (eltName == "TYCourbeNiveau") || (eltName == "TYTerrain"))
228  {
229  QString item = fileList[i].section(".", -2, -2);
230  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
231  _eltsList.push_back(item);
232  }
233  }
234  break;
235  case eInfraFilter:
236  {
237  if ((eltName == "TYInfrastructure") || (eltName == "TYUserSourcePonctuelle") ||
238  (eltName == "TYBatiment") || (eltName == "TYMachine") ||
239  (eltName == "TYReseauTransport") || (eltName == "TYRoute"))
240  {
241  QString item = fileList[i].section(".", -2, -2);
242  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
243  _eltsList.push_back(item);
244  }
245  }
246  break;
247  case eSourceFilter:
248  {
249  if ((eltName == "TYUserSourcePonctuelle"))
250  {
251  QString item = fileList[i].section(".", -2, -2);
252  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
253  _eltsList.push_back(item);
254  }
255  }
256  break;
257  case eConstructionFilter:
258  {
259  if ((eltName == "TYBatiment"))
260  {
261  QString item = fileList[i].section(".", -2, -2);
262  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
263  _eltsList.push_back(item);
264  }
265  }
266  break;
267  case eBatimentFilter:
268  {
269  if ((eltName == "TYEtage") || (eltName == "TYAcousticCylinder"))
270  {
271  QString item = fileList[i].section(".", -2, -2);
272  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
273  _eltsList.push_back(item);
274  }
275  }
276  break;
277  case eEtageFilter:
278  {
279  if ((eltName == "TYUserSourcePonctuelle") || (eltName == "TYMachine"))
280  {
281  QString item = fileList[i].section(".", -2, -2);
282  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
283  _eltsList.push_back(item);
284  }
285  }
286  break;
287  case eMachineFilter:
288  {
289  if ((eltName == "TYMachine"))
290  {
291  QString item = fileList[i].section(".", -2, -2);
292  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
293  _eltsList.push_back(item);
294  }
295  }
296  break;
298  {
299  if ((eltName == "TYReseauTransport"))
300  {
301  QString item = fileList[i].section(".", -2, -2);
302  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
303  _eltsList.push_back(item);
304  }
305  }
306  break;
307  case eRouteFilter:
308  {
309  if ((eltName == "TYRoute"))
310  {
311  QString item = fileList[i].section(".", -2, -2);
312  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
313  _eltsList.push_back(item);
314  }
315  }
316  break;
317  case eTopoFilter:
318  {
319  if ((eltName == "TYTopographie") || (eltName == "TYPlanEau") || (eltName == "TYCoursEau") ||
320  (eltName == "TYCourbeNiveau") || (eltName == "TYTerrain"))
321  {
322  QString item = fileList[i].section(".", -2, -2);
323  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
324  _eltsList.push_back(item);
325  }
326  }
327  break;
328  case ePlanEauFilter:
329  {
330  if ((eltName == "TYPlanEau"))
331  {
332  QString item = fileList[i].section(".", -2, -2);
333  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
334  _eltsList.push_back(item);
335  }
336  }
337  break;
338  case eCoursEauFilter:
339  {
340  if ((eltName == "TYCoursEau"))
341  {
342  QString item = fileList[i].section(".", -2, -2);
343  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
344  _eltsList.push_back(item);
345  }
346  }
347  break;
348  case eCourbeNiveauFilter:
349  {
350  if ((eltName == "TYCourbeNiveau"))
351  {
352  QString item = fileList[i].section(".", -2, -2);
353  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
354  _eltsList.push_back(item);
355  }
356  }
357  break;
358  case eTerrainFilter:
359  {
360  if ((eltName == "TYTerrain"))
361  {
362  QString item = fileList[i].section(".", -2, -2);
363  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
364  _eltsList.push_back(item);
365  }
366  }
367  break;
368  case eSitesFilter:
369  {
370  if ((eltName == "TYSiteNode"))
371  {
372  QString item = fileList[i].section(".", -2, -2);
373  _pElementChoiceListBox->insertItem(count++, new QListWidgetItem(item));
374  _eltsList.push_back(item);
375  }
376  }
377  break;
378  default:
379  break;
380  }
381  }
382 
383  _isFiltering = false;
384 }
385 
387 {
388  if (!_selectedEltsList.isEmpty())
389  {
390  if (e->key() == Qt::Key_Delete)
391  {
392  // Recuperation du dossier contenant la bibliotheque
393  QString dirPath = QDir::toNativeSeparators(getTYApp()->tympanUserDir() + "/library/");
394  QDir dir(dirPath);
395 
396  for (unsigned int i = 0; i < _selectedEltsList.count(); ++i)
397  {
398  if (!QMessageBox::question(this, TR("id_delete_box_caption"),
399  TR("id_delete_box_text").arg(_selectedEltsList[i]),
400  TR("id_delete_box_yes"), TR("id_delete_box_no"), QString(), 0, 1))
401  {
402  if (dir.remove(QDir::toNativeSeparators(_selectedEltsList[i] + ".xml")))
403  {
404  QMessageBox::information(this, "Tympan",
405  TR("id_delete_ok").arg(_selectedEltsList[i]));
406 
407  // Remove item from choice list box
408  QObject::disconnect(_pElementChoiceListBox, &QListWidget::itemSelectionChanged, this,
410  if (_pElementChoiceListBox->findItems(_selectedEltsList[i], Qt::MatchExactly)
411  .count() > 0)
412  {
413  _pElementChoiceListBox->takeItem(
415  _selectedEltsList[i], Qt::MatchExactly)[0]));
416  }
417  QObject::connect(_pElementChoiceListBox, &QListWidget::itemSelectionChanged, this,
419 
420  // Remove item from elements list
421  _eltsList.removeAll(_selectedEltsList[i]);
422  }
423  else
424  {
425  QMessageBox::warning(this, "Tympan",
426  TR("id_delete_failed").arg(_selectedEltsList[i]));
427  }
428  }
429  }
430 
431  updateFrame();
432  }
433  }
434 }
TYApplication * getTYApp()
Retourne le pointeur sur l'application.
pour l'application Tympan (fichier header)
#define TYDIRPREFERENCEMANAGER
Definition: TYElement.h:52
#define TR(id)
Widget pour lister les elements de la bibilotheque (fichier header)
@ eCourbeNiveauFilter
@ ePlanEauFilter
@ eMachineFilter
@ eRouteFilter
@ eNoneFilter
@ eTerrainFilter
@ eSourceFilter
@ eCoursEauFilter
@ eSiteFilter
@ eReseauTransportFilter
@ eSitesFilter
@ eTopoFilter
@ eBatimentFilter
@ eConstructionFilter
@ eEtageFilter
@ eInfraFilter
const char * name
QList< QString > _selectedEltsList
La liste des elements selectionnes.
const QList< QString > & getSelectedEltsList() const
virtual ~TYLibraryWidget()
const QString getCurrentDir() const
void keyPressEvent(QKeyEvent *e)
QLabel * _pCategoryCurrentLabel
QListWidget * _pElementChoiceListBox
void updateCategory(const QString &)
void filtering(const QDir &dir, int filter=eSiteFilter)
QComboBox * _pCategoryChoiceListBox
QList< QString > _eltsList
La liste des elements de la liste.
void updateHighlight(const QString &)
void highlighted(const QString &)
const QList< QString > & getEltsList() const
TYLibraryWidget(int filter, QWidget *parent=0, const char *name=0, Qt::WindowFlags f=QFlag(0))
int getEltType(const QString &fileName, QString &eltType)