Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYWidget.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
23 #include <qscrollarea.h>
24 #include <qstring.h>
25 
26 #include <QBoxLayout>
31 
32 #include "TYWidget.h"
33 
34 #define TR(id) OLocalizator::getString("TYWidget", (id))
35 
36 TYWidget::TYWidget(TYElement* pElement, QWidget* parent, const char* name, Qt::WindowFlags f)
37  : QWidget(parent, f), _pElement(pElement)
38 {
39  setObjectName(QString(name));
40  _locked = false;
41 }
42 
44 
45 /*static*/ int TYWidget::edit(TYElement* pElement, QWidget* pParent /*=NULL*/)
46 {
47  TYFormDialog* pDlg = new TYFormDialog(pParent);
48  pDlg->setModal(true);
49 
50  int ret = QDialog::Rejected;
51 
52  if (pElement != nullptr)
53  {
54  pDlg->setWindowTitle(getDisplayName(pElement));
55 
56  QWidget* pMainWidget = pElement->getEditWidget();
57  pMainWidget->setParent(pDlg);
58 
59  QBoxLayout* pLayout = new QVBoxLayout();
60  pDlg->setLayout(pLayout);
61 
62  auto scrollArea = new QScrollArea(pParent);
63  scrollArea->setWidgetResizable(true);
64  scrollArea->setWidget(pMainWidget);
65 
66  pLayout->addWidget(scrollArea);
67 
68  // On recupere les settings
69  TYPreferenceManager::loadGeometryFromPreferences(pMainWidget->metaObject()->className(), pDlg);
70 
71  QPushButton* pButtonOK = new QPushButton(TR("id_ok_btn"), pDlg);
72  pButtonOK->setDefault(true);
73  connect(pButtonOK, &QPushButton::clicked, pDlg, &QDialog::accept);
74 
75  QPushButton* pButtonCancel = new QPushButton(TR("id_cancel_btn"), pDlg);
76  pButtonCancel->setShortcut(Qt::Key_Escape);
77  connect(pButtonCancel, &QPushButton::clicked, pDlg, &QDialog::reject);
78 
79  pLayout->addSpacing(5);
80  QBoxLayout* pBtnLayout = new QHBoxLayout();
81  pBtnLayout->setContentsMargins(10, 10, 10, 10);
82  pLayout->addLayout(pBtnLayout);
83 
84  pBtnLayout->addStretch();
85  pBtnLayout->addWidget(pButtonOK);
86  pBtnLayout->addSpacing(5);
87  pBtnLayout->addWidget(pButtonCancel);
88 
89  // Affiche la boite de dialogue
90  ret = pDlg->exec();
91 
92  // Applique les modificatins si necessaire
93  if (ret == QDialog::Accepted)
94  {
95  ((TYWidget*)pMainWidget)->apply();
96  }
97  else // Reject
98  {
99  ((TYWidget*)pMainWidget)->reject();
100  }
101 
102  // On sauve les settings
103  TYPreferenceManager::saveGeometryToPreferences(pMainWidget->metaObject()->className(), pDlg);
104 
105  // Liberation de la memoire
106  if (pParent)
107  {
108  pDlg->setParent(0);
109  }
110 
111  disconnect(pButtonOK, &QPushButton::clicked, pDlg, &QDialog::accept);
112  disconnect(pButtonCancel, &QPushButton::clicked, pDlg, &QDialog::reject);
113  }
114  else
115  {
116  ret = QDialog::Rejected;
117  ;
118  }
119 
120  delete pDlg;
121 
122  return ret;
123 }
124 
125 /*static*/ QString TYWidget::getDisplayName(TYElement* pElt)
126 {
127  if (pElt == nullptr)
128  {
129  return QString();
130  }
131 
132  return OLocalizator::getString("DisplayName", pElt->getClassName());
133 }
Parent class of Tympan Qt dialogs of type form (geader file)
const char * name
#define TR(id)
Definition: TYWidget.cpp:34
outil IHM pour un objet metier de type TYElement (fichier header)
static QString getString(const QString &classname, const QString &stringId)
virtual const char * getClassName() const
Definition: TYElement.h:248
void setParent(TYElement *pParent)
Definition: TYElement.h:690
classe de l'objet IHM pour un objet metier de type TYElement
Definition: TYWidget.h:43
TYWidget(TYElement *pElement, QWidget *parent=0, const char *name=0, Qt::WindowFlags f=QFlag(0))
Definition: TYWidget.cpp:36
virtual ~TYWidget()
Definition: TYWidget.cpp:43
bool _locked
Definition: TYWidget.h:117
static QString getDisplayName(TYElement *pElt)
Definition: TYWidget.cpp:125
static int edit(TYElement *pElement, QWidget *pParent=NULL)
Definition: TYWidget.cpp:45