Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYLineEdit.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 
23 #include <QKeyEvent>
24 
26 #include "TYLineEdit.h"
27 
28 TYLineEdit::TYLineEdit(const QString& contents, QWidget* parent) : QLineEdit(contents, parent)
29 {
30  init();
31 }
32 
33 TYLineEdit::TYLineEdit(QWidget* parent) : QLineEdit(parent)
34 {
35  init();
36 }
37 
38 TYLineEdit::TYLineEdit(const QString& contents, bool isZeroOK, bool canBeNegative, QWidget* parent)
39 {
40  _isZeroOK = isZeroOK;
41  _canBeNegative = canBeNegative;
42  init();
43 }
44 
46 {
47  QDoubleValidator* validator = new QDoubleValidator();
48  QLocale* locale = new QLocale(QLocale::C);
49  locale->setNumberOptions(QLocale::RejectGroupSeparator);
50  validator->setLocale(*locale);
51 
52  validator->setNotation(QDoubleValidator::StandardNotation);
53 
54  if (!_canBeNegative)
55  {
56  if (_isZeroOK)
57  {
58  validator->setBottom(0.0);
59  }
60  else
61  {
62  validator->setBottom(0.0001);
63  }
64  }
65  setValidator(validator);
66  connect();
67 }
68 
70 {
71  QObject::connect(this, &QLineEdit::textChanged, this, &TYLineEdit::adjustTextColor);
72 }
73 
74 void TYLineEdit::keyPressEvent(QKeyEvent* evt)
75 {
76 
77  if (evt->key() == Qt::Key_Comma)
78  {
79  evt = new QKeyEvent(QEvent::KeyPress, Qt::Key_Period, Qt::NoModifier, 0, 0, 0, ".");
80  }
81 
82  QLineEdit::keyPressEvent(evt);
83 }
84 
86 {
87  if (!this->hasAcceptableInput())
88  {
89  this->setStyleSheet("QLineEdit { color: red;}");
90  }
91  else
92  {
93  this->setStyleSheet("QLineEdit { color: black;}");
94  }
95 
96  // Validate if parent or grand parent (case of Tabs) is TYFormDialog
97  TYFormDialog* formDialog = nullptr;
98  QObject* parent = this->parent();
99  formDialog = dynamic_cast<TYFormDialog*>(parent);
100  while (formDialog == nullptr && parent != nullptr)
101  {
102  parent = parent->parent();
103  formDialog = dynamic_cast<TYFormDialog*>(parent);
104  }
105 
106  if (formDialog != nullptr)
107  {
108  formDialog->validate();
109  }
110 }
Parent class of Tympan Qt dialogs of type form (geader file)
outil IHM pour une entrée utilisateur (fichier header)
void init()
Definition: TYLineEdit.cpp:45
TYLineEdit(const QString &contents, bool isZeroOK=true, bool canBeNegative=true, QWidget *parent=nullptr)
Definition: TYLineEdit.cpp:38
bool _isZeroOK
Definition: TYLineEdit.h:74
void keyPressEvent(QKeyEvent *evt)
Definition: TYLineEdit.cpp:74
bool _canBeNegative
Definition: TYLineEdit.h:75
void adjustTextColor()
Definition: TYLineEdit.cpp:85
void connect()
Definition: TYLineEdit.cpp:69