fixed edge display for volume cells
[engrid-github.git] / src / libengrid / dialogoperation.h
blob2854e407870f061ab73f8cece62092a9b759bc99
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #ifndef dialogoperation_H
22 #define dialogoperation_H
24 template <typename UI, typename OP>
25 class DialogOperation;
27 #include "operation.h"
28 // #include "guimainwindow.h"
30 #include <QDialog>
31 #include <QListWidget>
32 #include <QTextStream>
33 #include <QLineEdit>
35 #include <vtkUnstructuredGrid.h>
36 #include <vtkIntArray.h>
37 #include <vtkCellData.h>
39 template <typename UI, typename OP>
40 class DialogOperation : public QDialog,
41 public OP
44 protected: // attributes
46 /** The user interface definition from QtDesigner */
47 UI m_Ui;
49 protected: // methods
51 EgVtkObject::l2g_t getPartNodes() { return this->m_Part.getNodes(); }
52 EgVtkObject::l2g_t getPartCells()const { return this->m_Part.getCells(); }
53 EgVtkObject::g2l_t getPartLocalNodes() { return this->m_Part.getLocalNodes(); }
54 EgVtkObject::g2l_t getPartLocalCells() { return this->m_Part.getLocalCells(); }
55 EgVtkObject::l2l_t getPartN2N() { return this->m_Part.getN2N(); }
56 EgVtkObject::l2l_t getPartN2C() { return this->m_Part.getN2C(); }
57 EgVtkObject::l2l_t getPartC2C() { return this->m_Part.getC2C(); }
59 public: // methods
61 /** Generic constructor to set up the user interface */
62 DialogOperation();
64 /**
65 * Create a checkable QListWidgetItem and add it to a QListWidget.
66 * @param lw the QListWidget to add the item to
67 * @param item the text of the item
68 * @param checked the status of the item checked/unchecked
70 template <class T>
71 void addListItem(QListWidget *lw, T item, bool checked = false);
73 /**
74 * Check if a certain text item is checked within a QListWidget.
75 * @param lw the QListWidget to check
76 * @param item the item to check
77 * @return true if the item is checked and false if not
79 template <class T>
80 bool checkListItem(QListWidget *lw, T item);
82 /**
83 * Get the name of the selected volume.
84 * @param lw the QListWidget for volume selection
85 * @return the name of the selected volume
87 QString getSelectedVolume(QListWidget *lw);
89 /**
90 * Get a set with all seleceted items from a QListWidget.
91 * @param lw The QListWidget.
92 * @param sel On return, this will hold all items.
94 void getSelectedItems(QListWidget *lw, QSet<int> &sel);
96 /**
97 * Get a set with all seleceted items from a QListWidget.
98 * @param lw The QListWidget.
99 * @param sel On return, this will hold all items.
101 void getSelectedItems(QListWidget *lw, QSet<QString> &sel);
104 * @brief set a line edit to a double value
105 * @param value the value to set
106 * @param line_edit the QLineEdit to use
108 void setDouble(double value, QLineEdit *line_edit);
111 * @brief set the text of a QLineEdit widget to represent a vector in the form "x, y, z"
112 * @param value the vector to set
113 * @param line_edit the QLineEdit to use
115 void setVector(vec3_t value, QLineEdit *line_edit);
118 * @brief get a vector from a QLineEdit widget which represents it in the form "x, y, z"
119 * @param line_edit the QLineEdit to use
120 * @return the vector
122 vec3_t getVector(QLineEdit *line_edit);
124 virtual void before() {}
125 virtual void operator()();
127 //connect(const QObject* a, const char* b, const QObject* c, const char* d) { QObject::connect(a,b,c,d); };
131 template <typename UI, typename OP>
132 DialogOperation<UI,OP>::DialogOperation()
134 m_Ui.setupUi(this);
137 template <typename UI, typename OP>
138 template <class T>
139 void DialogOperation<UI,OP>::addListItem(QListWidget *lw, T item, bool checked)
141 QListWidgetItem *lwi = new QListWidgetItem(lw);
142 if (checked) lwi->setCheckState(Qt::Checked);
143 else lwi->setCheckState(Qt::Unchecked);
144 QString text = "";
145 QTextStream ts(&text);
146 ts << item;
147 lwi->setText(text);
148 lwi->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
151 template <typename UI, typename OP>
152 template <class T>
153 bool DialogOperation<UI,OP>::checkListItem(QListWidget *lw, T item)
155 QString text = "";
156 QTextStream ts(&text);
157 ts << item;
158 for (int i = 0; i < lw->count(); ++i) {
159 if (lw->item(i)->text() == text) {
160 if (lw->item(i)->checkState() == Qt::Checked) return true;
163 return false;
166 template <typename UI, typename OP>
167 QString DialogOperation<UI,OP>::getSelectedVolume(QListWidget *lw)
169 QString volume_name;
170 for (int i = 0; i < lw->count(); ++i) {
171 if (lw->item(i)->isSelected()) {
172 volume_name = lw->item(i)->text();
175 return volume_name;
178 template <typename UI, typename OP>
179 void DialogOperation<UI,OP>::getSelectedItems(QListWidget *lw, QSet<QString> &sel)
181 sel.clear();
182 for (int i = 0; i < lw->count(); ++i) {
183 if (lw->item(i)->checkState() == Qt::Checked) {
184 QString item = lw->item(i)->text();
185 sel.insert(item);
190 template <typename UI, typename OP>
191 void DialogOperation<UI,OP>::getSelectedItems(QListWidget *lw, QSet<int> &sel)
193 sel.clear();
194 for (int i = 0; i < lw->count(); ++i) {
195 if (lw->item(i)->checkState() == Qt::Checked) {
196 QString item_txt = lw->item(i)->text();
197 QStringList items = item_txt.split(":");
198 int item = items[0].toInt(); ///\todo UiUiUi
199 sel.insert(item);
204 template <typename UI, typename OP>
205 void DialogOperation<UI,OP>::operator()()
207 bool ok = true;
208 //Prepare the GUI
209 try {
210 OP::checkGrid();
211 before();
212 } catch (Error err) {
213 err.display();
214 ok = false;
216 //Run the GUI
217 if (ok) {
218 try {
219 if (!QDialog::exec()) {
220 ok = false;
222 } catch (Error err) {
223 err.display();
226 //Run the operation
227 if (ok) {
228 try {
229 OP::operator()();
230 } catch (Error err) {
231 err.display();
236 template <typename UI, typename OP>
237 void DialogOperation<UI,OP>::setDouble(double value, QLineEdit *line_edit)
239 QString num;
240 num.setNum(value);
241 line_edit->setText(num);
244 template <typename UI, typename OP>
245 void DialogOperation<UI,OP>::setVector(vec3_t value, QLineEdit *line_edit)
247 QString x, y, z;
248 x.setNum(value[0]);
249 y.setNum(value[1]);
250 z.setNum(value[2]);
251 line_edit->setText(x + ", " + y + ", " + z);
254 template <typename UI, typename OP>
255 vec3_t DialogOperation<UI,OP>::getVector(QLineEdit *line_edit)
257 QStringList items = line_edit->text().split(",");
258 vec3_t value(0,0,0);
259 if (items.size() == 3) {
260 for (int i = 0; i < 3; ++i) {
261 value[i] = items[i].toDouble();
264 return value;
267 #endif