Added not yet working vtkinteractorstyle subclass + fixed sigabrt when switching...
[engrid.git] / dialogoperation.h
blobaecce29bb172dae881c84afd8d5233eb8aea7e7c
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #ifndef dialogoperation_H
24 #define dialogoperation_H
26 template <class UI>
27 class DialogOperation;
29 #include "operation.h"
31 #include <QDialog>
32 #include <QListWidget>
33 #include <QTextStream>
35 #include <vtkUnstructuredGrid.h>
36 #include <vtkIntArray.h>
37 #include <vtkCellData.h>
39 template <class UI>
40 class DialogOperation : public QDialog,
41 public Operation
44 protected: // attributes
46 /** The user interface definition from QtDesigner */
47 UI ui;
49 public: // methods
51 /** Generic constructor to set up the user interface */
52 DialogOperation();
54 /**
55 * Create a checkable QListWidgetItem and add it to a QListWidget.
56 * @param lw the QListWidget to add the item to
57 * @param item the text of the item
58 * @param checked the status of the item checked/unchecked
60 template <class T>
61 void addListItem(QListWidget *lw, T item, bool checked = false);
63 /**
64 * Check if a certain text item is checked within a QListWidget.
65 * @param lw the QListWidget to check
66 * @param item the item to check
67 * @return true if the item is checked and false if not
69 template <class T>
70 bool checkListItem(QListWidget *lw, T item);
72 /**
73 * Get a set with all seleceted items from a QListWidget.
74 * @param lw The QListWidget.
75 * @param sel On return, this will hold all items.
77 void getSelectedItems(QListWidget *lw, QSet<int> &sel);
79 /**
80 * Get a set with all seleceted items from a QListWidget.
81 * @param lw The QListWidget.
82 * @param sel On return, this will hold all items.
84 void getSelectedItems(QListWidget *lw, QSet<QString> &sel);
86 virtual void before() {};
87 virtual void operator()();
89 //connect(const QObject* a, const char* b, const QObject* c, const char* d) { QObject::connect(a,b,c,d); };
93 template <class UI>
94 DialogOperation<UI>::DialogOperation()
96 ui.setupUi(this);
99 template <class UI>
100 template <class T>
101 void DialogOperation<UI>::addListItem(QListWidget *lw, T item, bool checked)
103 QListWidgetItem *lwi = new QListWidgetItem(lw);
104 if (checked) lwi->setCheckState(Qt::Checked);
105 else lwi->setCheckState(Qt::Unchecked);
106 QString text = "";
107 QTextStream ts(&text);
108 ts << item;
109 lwi->setText(text);
110 lwi->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
113 template <class UI>
114 template <class T>
115 bool DialogOperation<UI>::checkListItem(QListWidget *lw, T item)
117 QString text = "";
118 QTextStream ts(&text);
119 ts << item;
120 for (int i = 0; i < lw->count(); ++i) {
121 if (lw->item(i)->text() == text) {
122 if (lw->item(i)->checkState() == Qt::Checked) return true;
125 return false;
128 template <class UI>
129 void DialogOperation<UI>::getSelectedItems(QListWidget *lw, QSet<QString> &sel)
131 sel.clear();
132 for (int i = 0; i < lw->count(); ++i) {
133 if (lw->item(i)->checkState() == Qt::Checked) {
134 QString item = lw->item(i)->text();
135 sel.insert(item);
140 template <class UI>
141 void DialogOperation<UI>::getSelectedItems(QListWidget *lw, QSet<int> &sel)
143 sel.clear();
144 for (int i = 0; i < lw->count(); ++i) {
145 if (lw->item(i)->checkState() == Qt::Checked) {
146 int item = lw->item(i)->text().toInt();
147 sel.insert(item);
152 template <class UI>
153 void DialogOperation<UI>::operator()()
155 bool ok = true;
156 try {
157 checkGrid();
158 before();
159 } catch (Error err) {
160 err.display();
161 ok = false;
163 if (ok) {
164 if (QDialog::exec()) {
165 try {
166 Operation::operator()();
167 } catch (Error err) {
168 err.display();
174 #endif