Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYAddLibraryDialog.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 <qcursor.h>
22 #include <qdir.h>
23 #include <qmessagebox.h>
24 #include <qpushbutton.h>
25 #include <qlineedit.h>
26 #include <qlabel.h>
27 // Added by qt3to4:
28 #include <QGridLayout>
29 #include <QBoxLayout>
30 #include <QHBoxLayout>
31 #include <QList>
32 
33 #include "Tympan/core/config.h"
38 #include "TYAddLibraryDialog.h"
39 
40 #define TR(id) OLocalizator::getString("TYAddLibraryDialog", (id))
41 
42 TYAddLibraryDialog::TYAddLibraryDialog(QWidget* parent, const char* name, Qt::WindowFlags f)
43  : QDialog(parent, f), _element(0)
44 {
45  setObjectName(name);
46  setWindowTitle(TR("id_caption"));
47 
48  // Layout
49  QGridLayout* pLayout = new QGridLayout();
50  setLayout(pLayout);
51 
52  // On affiche la bibliotheque
54  pLayout->addWidget(_pLibrary, 0, 0);
55 
56  // Nom de sauvegarde
57  QBoxLayout* pNameLayout = new QHBoxLayout(this);
58  pNameLayout->setContentsMargins(5, 5, 5, 5);
59  pNameLayout->setSpacing(5);
60  pLayout->addLayout(pNameLayout, 1, 0);
61  pNameLayout->setAlignment(Qt::AlignLeft);
62 
63  _labelName = new QLabel(this);
64  _labelName->setText(TR("id_name_label"));
65  pNameLayout->addWidget(_labelName);
66 
67  _lineEditName = new QLineEdit(this);
68  pNameLayout->addWidget(_lineEditName);
69  QObject::connect(_pLibrary, &TYLibraryWidget::highlighted, _lineEditName, &QLineEdit::setText);
70 
71  // Push buttons
72  QBoxLayout* pBtnLayout = new QHBoxLayout();
73  pLayout->addLayout(pBtnLayout, 2, 0);
74  pBtnLayout->addStretch(1);
75 
76  _pSaveBtn = new QPushButton(TR("id_ok_btn"), this);
77  _pSaveBtn->setEnabled(false);
78  QObject::connect(_pSaveBtn, &QPushButton::clicked, this, &TYAddLibraryDialog::save);
79  pBtnLayout->addWidget(_pSaveBtn);
80 
81  QPushButton* pCloseBtn = new QPushButton(TR("id_cancel_btn"), this);
82  pCloseBtn->setShortcut(Qt::Key_Escape);
83  QObject::connect(pCloseBtn, &QPushButton::clicked, this, &TYAddLibraryDialog::reject);
84  pBtnLayout->addWidget(pCloseBtn);
85 
86  QObject::connect(_lineEditName, &QLineEdit::textChanged, this, &TYAddLibraryDialog::setEnableButton);
87 
88  _lineEditName->clear();
89 }
90 
92 {
93  QObject::disconnect(_pLibrary, &TYLibraryWidget::highlighted, _lineEditName, &QLineEdit::setText);
94  QObject::disconnect(_lineEditName, &QLineEdit::textChanged, this, &TYAddLibraryDialog::setEnableButton);
95 }
96 
98 {
99  _element = element;
100 }
101 
103 {
104  // Securite
105  if (!_element)
106  {
107  return;
108  }
109 
110  // Si il n'ya pas de texte, on retourne
111  if (_lineEditName->text().isEmpty())
112  {
113  return;
114  }
115 
116  // Si le nom se termine par l'extension ".xml", on la retire.
117  QString name = _lineEditName->text();
118  if (name.section(".", -1, -1) == "xml")
119  {
120  name = name.section(".", -2, -2);
121  }
122 
123  // Si l'element existe deja dans la liste, on demande si on doit le remplacer
124  QList<QString> eltsList = _pLibrary->getEltsList();
125  int i = eltsList.indexOf(name);
126  if (i != -1)
127  {
128  if (QMessageBox::question(this, TR("id_box_caption"), TR("id_box_text").arg(name), TR("id_box_yes"),
129  TR("id_box_no"), QString(), 0, 1))
130  {
131  return;
132  }
133  }
134 
135  TYApplication::setOverrideCursor(Qt::WaitCursor);
136 
137  QString filename = QDir::toNativeSeparators(_pLibrary->getCurrentDir() + "/" + name + ".xml");
138 
139  TYXMLManager xmlManager;
140 
141  QString version(TY_PRODUCT_VERSION_);
142  QString tiret(" - ");
143  QString licencie(TY_CUSTOMER_);
144  QString licenceNumber(TY_LICENCE_NUMBER_);
145 
146  QString messageVersion = version + tiret + licencie + tiret + licenceNumber;
147 
148  xmlManager.createDoc(TY_PRODUCT_XMLTAG_, messageVersion);
149  xmlManager.addElement(_element);
150 
151  if (xmlManager.save(filename) == 0)
152  {
153  QMessageBox::information(this, "Tympan", TR("id_export_ok").arg(name));
154  }
155  else
156  {
157  QMessageBox::warning(this, "Tympan", TR("id_export_failed").arg(name));
158  }
159 
160  TYApplication::restoreOverrideCursor();
161 
162  accept();
163 }
164 
165 void TYAddLibraryDialog::setEnableButton(const QString& str)
166 {
167  if (str.isEmpty())
168  {
169  _pSaveBtn->setEnabled(false);
170  }
171  else
172  {
173  _pSaveBtn->setEnabled(true);
174  }
175 }
#define TR(id)
Boite de dialogue pour l'ajout d'un element dans la bibliotheque (fichier header)
pour l'application Tympan (fichier header)
Widget pour lister les elements de la bibilotheque (fichier header)
@ eNoneFilter
const char * name
TYAddLibraryDialog(QWidget *parent=0, const char *name=0, Qt::WindowFlags f=QFlag(0))
Constructeur par defaut.
QPushButton * _pSaveBtn
Le bouton de sauvegarde.
QLineEdit * _lineEditName
Le nom de sauvegarde.
LPTYElement _element
Element a sauvegarder.
void setElement(LPTYElement element)
Acces a l'element.
virtual ~TYAddLibraryDialog()
Destructeur.
TYLibraryWidget * _pLibrary
La librarie.
void setEnableButton(const QString &)
Active ou non le bouton _pSaveBtn.
void save()
Sauvegarde.
Widget pour lister les elements de la bibilotheque.
const QString getCurrentDir() const
void highlighted(const QString &)
const QList< QString > & getEltsList() const
int save(QString fileName)
void createDoc(QString docName, QString version)
int addElement(TYElement *pElt)