Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TabPointsWidget.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 
16 #include <qmenu.h>
17 #include <QStringList>
18 #include <QApplication>
19 #include <QKeyEvent>
20 
24 
25 #define TR(id) OLocalizator::getString("TabPointsWidget", (id))
26 
27 bool ItemDelegate::eventFilter(QObject* object, QEvent* event)
28 {
29  if (event->type() == QEvent::KeyPress)
30  {
31  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
32  if (keyEvent->key() == Qt::Key_Comma)
33  {
34  keyEvent = new QKeyEvent(QEvent::KeyPress, Qt::Key_Period, Qt::NoModifier, 0, 0, 0, ".");
35  QApplication::sendEvent(object, keyEvent);
36  return true;
37  }
38  }
39  return QStyledItemDelegate::eventFilter(object, event);
40 }
41 
42 TabPointsWidget::TabPointsWidget(TYTabPoint& listPoints, QWidget* parent)
43  : QTableWidget(parent), _listPoints(listPoints)
44 {
45  setColumnCount(3);
46 
47  // Set horizontal header
48  QStringList stringList;
49  stringList.append("X");
50  stringList.append("Y");
51  stringList.append("Z");
52 
53  setHorizontalHeaderLabels(stringList);
54 
55  setSelectionBehavior(QAbstractItemView::SelectRows);
56 
57  connect(this, &TabPointsWidget::cellChanged, this, &TabPointsWidget::tabValueChanged);
58  this->setItemDelegate(new ItemDelegate(this));
59 }
60 
62 {
63  // Filling the table
64  disconnect(this, &TabPointsWidget::cellChanged, this, &TabPointsWidget::tabValueChanged);
65 
66  clearContents();
67  setRowCount(static_cast<int>(_listPoints.size()));
68  for (int row = 0; row < _listPoints.size(); row++)
69  {
70  setItem(row, 0, new QTableWidgetItem((QString().setNum(_listPoints[row]._x, 'f', 2))));
71  setItem(row, 1, new QTableWidgetItem((QString().setNum(_listPoints[row]._y, 'f', 2))));
72  setItem(row, 2, new QTableWidgetItem((QString().setNum(_listPoints[row]._z, 'f', 2))));
73 
74  setAttributes(row);
75  }
76 
77  connect(this, &TabPointsWidget::cellChanged, this, &TabPointsWidget::tabValueChanged);
78 }
79 
81 {
82  bool ok(false);
83  double x(0.), y(0.), z(0.);
84 
85  // Filling the list
86  _listPoints.clear();
87  for (int row = 0; row < rowCount(); row++)
88  {
89  x = item(row, 0)->text().toDouble(&ok);
90  y = item(row, 1)->text().toDouble(&ok);
91  z = item(row, 2)->text().toDouble(&ok);
92 
93  if (ok)
94  {
95  _listPoints.push_back(TYPoint(x, y, z));
96  }
97  }
98 }
99 
100 void TabPointsWidget::tabValueChanged(int row, int col)
101 {
102  // Translate input value to double to see if it's a correct number
103  bool ok = false;
104  item(row, col)->text().toDouble(&ok);
105 
106  // If value is not a valid number, get the original value in points list
107  if (!ok)
108  {
109  item(row, col)->setText(QString().setNum(0.0, 'f', 2));
110  }
111 }
112 
114 {
115  item(row, 0)->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
116  item(row, 1)->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
117  item(row, 2)->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
118 }
119 
121 {
122  setItem(row, 0, new QTableWidgetItem((QString().setNum(0.0, 'f', 2))));
123  setItem(row, 1, new QTableWidgetItem((QString().setNum(0.0, 'f', 2))));
124  setItem(row, 2, new QTableWidgetItem((QString().setNum(0.0, 'f', 2))));
125 }
126 
127 void TabPointsWidget::contextMenuEvent(QContextMenuEvent* e)
128 {
129  QPoint point = mapFrom(this, e->pos());
130  if ((point.x() >= 0) && (point.y() >= 0) && (point.x() <= width()) && (point.y() <= height()))
131  {
132  QPoint resPoint = QPoint(point.x(), point.y());
133  QTableWidgetItem* element = itemAt(resPoint);
134  if (element)
135  {
136  int row = indexAt(resPoint).row();
137  row = row >= 0 ? row : 0; // Securite
138  row = row < rowCount() ? row : rowCount() - 1;
139 
140  int col = indexAt(resPoint).column();
141  col = col >= 0 ? col : 0; // Securite
142  col = col < columnCount() ? col : columnCount() - 1;
143 
144  QAction *insertLine = NULL, *appendLine = NULL, *deleteLine = NULL;
145 
146  QMenu* pPopup = new QMenu(this);
147 
148  insertLine = pPopup->addAction(TR("id_insert_row_item"));
149  appendLine = pPopup->addAction(TR("id_append_row_item"));
150  deleteLine = pPopup->addAction(TR("id_delete_row_item"));
151 
152  QAction* ret = pPopup->exec(mapToGlobal(point));
153 
154  if (ret == insertLine)
155  {
156  insertRow(row);
157 
158  initRow(row);
159  setAttributes(row);
160  }
161  else if (ret == appendLine)
162  {
163  setRowCount(rowCount() + 1);
164  int lastRow = rowCount() - 1;
165 
166  initRow(lastRow);
167  setAttributes(lastRow);
168  }
169  else if (ret == deleteLine)
170  {
171  removeRow(row);
172  }
173  }
174  }
175 }
std::vector< TYPoint > TYTabPoint
Collection de TYPoint.
Definition: TYDefines.h:340
#define TR(id)
bool eventFilter(QObject *object, QEvent *event) override
void tabValueChanged(int, int)
virtual void apply()
void setAttributes(int row)
virtual void update()
virtual void contextMenuEvent(QContextMenuEvent *e)
void initRow(int row)
TYTabPoint & _listPoints
TabPointsWidget(TYTabPoint &listPoints, QWidget *parent=nullptr)