Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYOpenElementDialog.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 <qlistwidget.h>
22 #include <qlayout.h>
23 #include <qmessagebox.h>
24 #include <qfiledialog.h>
25 #include <qfileinfo.h>
26 #include <qlabel.h>
27 #include <qpushbutton.h>
28 #include <qcursor.h>
29 // Added by qt3to4:
30 #include <QHBoxLayout>
31 #include <QBoxLayout>
32 #include <QGridLayout>
33 
34 #include "Tympan/core/config.h"
35 #include "Tympan/core/logging.h"
43 #include "TYOpenElementDialog.h"
44 
45 #define TR(id) OLocalizator::getString("TYOpenElementDialog", (id))
46 
47 TYOpenElementDialog::TYOpenElementDialog(QWidget* parent, const char* name, Qt::WindowFlags f)
48  : QDialog(parent, f)
49 {
50  setObjectName(name);
51  setWindowTitle(TR("id_caption"));
52 
53  // Layout
54  QGridLayout* pLayout = new QGridLayout();
55  setLayout(pLayout);
56 
57  _pElementChoiceListBox = new QListWidget(this);
58  _pElementChoiceListBox->setSelectionMode(QAbstractItemView::SingleSelection);
59  pLayout->addWidget(_pElementChoiceListBox, 0, 0);
60 
61  QBoxLayout* pBtnLayout = new QHBoxLayout();
62  pLayout->addLayout(pBtnLayout, 1, 0);
63 
64  pBtnLayout->addStretch(1);
65 
66  _pOpenBtn = new QPushButton(TR("id_open_btn"), this);
67  QObject::connect(_pOpenBtn, &QPushButton::clicked, this, &TYOpenElementDialog::openSelected);
68  pBtnLayout->addWidget(_pOpenBtn);
69 
70  QPushButton* pCloseBtn = new QPushButton(TR("id_close_btn"), this);
71  pCloseBtn->setShortcut(Qt::Key_Escape);
72  QObject::connect(pCloseBtn, &QPushButton::clicked, this, &TYOpenElementDialog::reject);
73  pBtnLayout->addWidget(pCloseBtn);
74 
75  QObject::connect(_pElementChoiceListBox, &QListWidget::itemSelectionChanged, this,
77 
78  TYPreferenceManager::loadGeometryFromPreferences(metaObject()->className(), this);
79 
80  _isReadOnly = false;
81  _multiSelect = false;
82 
83  updateFrame();
84 }
85 
87 {
88  QObject::disconnect(_pOpenBtn, &QPushButton::clicked, this, &TYOpenElementDialog::openSelected);
89  QObject::disconnect(_pElementChoiceListBox, &QListWidget::itemSelectionChanged, this,
91 
92  TYPreferenceManager::saveGeometryToPreferences(metaObject()->className(), this);
93 }
94 
96 {
97  TYXMLManager xmlManager;
98 
99  // NOUVELLE VERSION POUR RECUPERER LE REPERTOIRE
100  QFileDialog* pDialog = new QFileDialog(parentWidget(), "Choose a file", _dirName, "XML (*.xml)");
101  pDialog->setFileMode(QFileDialog::ExistingFile); // Oblige a choisir un fichier existant
102 
103  pDialog->exec();
104 
105  QStringList list = pDialog->selectedFiles(); // Il existe un selectedFile, mais il n'est pas documente
106  if ((pDialog->result() == QDialog::Accepted) && (!list.isEmpty()))
107  {
108  _fileName = list.first();
109 
110  QDir dir = pDialog->directory();
111  _dirName = dir.absolutePath();
112  QDir::setCurrent(_dirName);
113 
114  pDialog = NULL;
115 
116  TYApplication::setOverrideCursor(Qt::WaitCursor);
117 
118  if (!_fileName.endsWith(".xml"))
119  {
120  _fileName += ".xml";
121  }
122 
123  QString itemName;
124 
125  _pElementChoiceListBox->clear();
126  _elements.clear();
127 
128  // Verification de l'etat du fichier
129  QFileInfo fi(_fileName);
130  if (!(fi.isWritable()))
131  {
132  _isReadOnly = true;
133  }
134 
135  // Chargement effectif du fichier
136  int resuChargement = xmlManager.load(_fileName.toUtf8(), _elements);
137 
138  // Backup du fichier (si le fichier est valide)
139  if (resuChargement == 1)
140  {
141  bool bBackupOk = getTYMainWnd()->backupFile(_fileName);
142 
143  if (!bBackupOk)
144  {
145  OMessageManager::get()->info("***********************************************");
146  OMessageManager::get()->info("* *");
147  OMessageManager::get()->info("* La copie de sauvegarde n'a pas fonctionne ! *");
148  OMessageManager::get()->info("* *");
149  OMessageManager::get()->info("***********************************************");
150  }
151  }
152  else
153  {
154  OMessageManager::get()->info("************************************************");
155  OMessageManager::get()->info("* *");
156  OMessageManager::get()->info("* Le document n est pas valide. *");
157  OMessageManager::get()->info("* La copie de sauvegarde du dernier document *");
158  OMessageManager::get()->info("* valide a ete preservee avec la terminaison *");
159  OMessageManager::get()->info("* .bak *");
160  OMessageManager::get()->info("* *");
161  OMessageManager::get()->info("************************************************");
162  }
163 
164  // On rempli la liste
165  for (unsigned int i = 0; i < _elements.size(); i++)
166  {
167  itemName = TYWidget::getDisplayName(_elements[i]);
168 
169  if (!_elements[i]->getName().isEmpty())
170  {
171  itemName += QString(" : %1").arg(_elements[i]->getName());
172  }
173 
174  _pElementChoiceListBox->insertItem(i, new QListWidgetItem(itemName));
175  }
176 
177  updateFrame();
178  TYApplication::restoreOverrideCursor();
179 
180  // TO DO
181  // RECUPERER LA DIRECTORY DU FICHIER CHOISI, LA COMMUNIQUER A L'APPLICATION QUI LA STOCKE
182  accept(); // Changement d'etat de la boite de dialogue
183  }
184  else
185  {
186  // L'action a ete annulee, on ferme la dialog
187  TYApplication::restoreOverrideCursor();
188  reject();
189  }
190 }
191 
193 {
194  if (_multiSelect)
195  {
196  openSelectedElements(); // Plusieurs elements potentiels
197  }
198  else
199  {
200  openSelectedElement(); // Un seul element
201  }
202 }
203 
205 {
206  if (_selectedElts.size() == 1)
207  {
209  }
210 
211  reject();
212 }
213 
215 {
216  _tabElem.clear();
217  if (_selectedElts.size() == 0)
218  {
219  return;
220  } // Pas d'element selectionne
221 
222  for (unsigned int i = 0; i < _selectedElts.size(); i++)
223  {
224  _tabElem.push_back(_elements[_selectedElts[i]]);
225  }
226 
227  reject();
228 }
229 
231 {
232  if (pElt)
233  {
234  writeDebugMsg(QString("Ouverture d'un element de type : %1").arg(pElt->getClassName()));
235 
236  LPTYProjet pProjet = dynamic_cast<TYProjet*>(pElt._pObj);
237  // L'element est un Projet
238  if (pProjet._pObj != nullptr)
239  {
240  // Mise a jour des elements du projet
241  // Directement projet courant, la "place" etant libre
242  getTYApp()->setCurProjet(pProjet);
243  }
244  // L'element est un SiteNode
245  else
246  {
247  // Directement site courant, la "place" etant libre
248  LPTYSiteNode pSite = dynamic_cast<TYSiteNode*>(pElt._pObj);
249  if (pSite._pObj != nullptr)
250  {
251  getTYApp()->setCurSiteNode(pSite);
252  }
253  else if (dynamic_cast<TYBatiment*>(pElt._pObj) != nullptr)
254  {
255  // Modeler directement
256  }
257  else if (dynamic_cast<TYMachine*>(pElt._pObj) != nullptr)
258  {
259  // Modeler directement
260  }
261  // Dans tous les autres cas on edite l'element cree
262  else if (pElt->edit(this) == Accepted)
263  {
264  // Si les modifications sont acceptes on demande ce qu'on fait de l'element
265  save(pElt);
266  }
267  }
268  // Si c'est un element modifiable graphiquement, on ouvre un modeleur
269  getTYMainWnd()->makeModeler(pElt);
270  }
271 }
272 
274 {
275  _selectedElts.clear();
276 
277  for (unsigned int i = 0; i < _pElementChoiceListBox->count(); i++)
278  {
279  if (_pElementChoiceListBox->item(i)->isSelected())
280  {
281  _selectedElts.append(i);
282  }
283  }
284 
285  _pOpenBtn->setEnabled(false);
286 
287  if (_selectedElts.size() >= 1)
288  {
289  _pOpenBtn->setEnabled(true);
290  }
291 }
292 
294 {
295  Q_CHECK_PTR(pElt);
296 
297  QMessageBox* pMsgBox = new QMessageBox(TR("id_savebox_caption"), TR("id_savebox_text"),
298  QMessageBox::NoIcon, QMessageBox::Yes, QMessageBox::NoButton,
299  QMessageBox::Cancel | QMessageBox::Escape, this);
300 
301  pMsgBox->setButtonText(QMessageBox::Yes, TR("id_xml_btn"));
302  pMsgBox->setButtonText(QMessageBox::Cancel, TR("id_cancel_btn"));
303 
304  int code = pMsgBox->exec();
305 
306  if (code == QMessageBox::No)
307  {
308  // XML
309  QString _fileName = QFileDialog::getSaveFileName(this, "", "", "XML (*.xml)");
310 
311  if (!_fileName.isEmpty())
312  {
313  if (!_fileName.endsWith(".xml"))
314  {
315  _fileName += ".xml";
316  }
317 
318  TYXMLManager xmlManager;
319  xmlManager.createDoc(TY_PRODUCT_XMLTAG_, TY_PRODUCT_VERSION_);
320  xmlManager.addElement(pElt);
321 
322  if (xmlManager.save(_fileName) == 0)
323  {
324  QMessageBox::information(this, TR("id_savebox_caption"), TR("id_export_ok").arg(_fileName));
325  }
326  else
327  {
328  QMessageBox::warning(this, TR("id_savebox_caption"), TR("id_export_failed").arg(_fileName));
329  }
330  }
331  }
332 }
333 
334 /*virtual*/ void TYOpenElementDialog::accept()
335 {
336  QDialog::accept();
337 }
338 
339 /*virtual*/ void TYOpenElementDialog::reject()
340 {
341  QDialog::reject();
342 }
343 
345 {
346  _multiSelect = b;
347 
348  if (_multiSelect)
349  {
350  _pElementChoiceListBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
351  }
352  else
353  {
354  _pElementChoiceListBox->setSelectionMode(QAbstractItemView::SingleSelection);
355  }
356 }
TYApplication * getTYApp()
Retourne le pointeur sur l'application.
void writeDebugMsg(QString msg)
Affiche un message de debug dans la fenetre de sortie.
TYMainWindow * getTYMainWnd()
Retourne le pointeur sur la fenetre principale.
pour l'application Tympan (fichier header)
Fenetre principale de l'application Tympan (fichier header)
#define TR(id)
Boite de dialogue pour le chargement d'un element metier (fichier header)
const char * name
static OMessageManager * get()
Definition: logging.cpp:108
virtual void info(const char *message,...)
Definition: logging.cpp:143
virtual const char * getClassName() const
Definition: TYElement.h:249
T * _pObj
The real pointer, must derived IRefCount.
Definition: smartptr.h:307
void setCurSiteNode(LPTYSiteNode pSiteNode)
Set/Get du site node courant.
void setCurProjet(LPTYProjet pProjet)
Set/Get du projet courant.
bool backupFile(const QString &fileName)
bool makeModeler(TYElement *pElt)
void openSelected()
Lit la selection.
void openSelectedElement()
Lit le premier element selectionne.
TYOpenElementDialog(QWidget *parent=0, const char *name=0, Qt::WindowFlags f=QFlag(0))
void openElement(LPTYElement pElt)
QList< int > _selectedElts
La liste des indices des elements selectionnes.
void setMultiSelect(const bool &b)
void save(LPTYElement pElt)
QListWidget * _pElementChoiceListBox
void openSelectedElements()
Lit l'ensemble des elements selectionnes.
std::vector< LPTYElement > _tabElem
LPTYElementArray _elements
La collection d'elements dans la liste.
classe de definition d'un projet.
Definition: TYProjet.h:45
static QString getDisplayName(TYElement *pElt)
Definition: TYWidget.cpp:118
int save(QString fileName)
int load(const QString &fileName, LPTYElementArray &eltCollection)
void createDoc(QString docName, QString version)
int addElement(TYElement *pElt)