Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/Application/twod/curveeditor.cpp
3203 views
1
/*****************************************************************************
2
* *
3
* Elmer, A Finite Element Software for Multiphysical Problems *
4
* *
5
* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *
6
* *
7
* This program is free software; you can redistribute it and/or *
8
* modify it under the terms of the GNU General Public License *
9
* as published by the Free Software Foundation; either version 2 *
10
* of the License, or (at your option) any later version. *
11
* *
12
* This program 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 this program (in file fem/GPL-2); if not, write to the *
19
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
20
* Boston, MA 02110-1301, USA. *
21
* *
22
*****************************************************************************/
23
24
/*****************************************************************************
25
* *
26
* ElmerGUI CurveEditor *
27
* *
28
*****************************************************************************
29
* *
30
* Authors: Mikko Lyly, Juha Ruokolainen and Peter RÃ¥back *
31
* Email: [email protected] *
32
* Web: http://www.csc.fi/elmer *
33
* Address: CSC - IT Center for Science Ltd. *
34
* Keilaranta 14 *
35
* 02101 Espoo, Finland *
36
* *
37
* Original Date: 15 Mar 2008 *
38
* *
39
*****************************************************************************/
40
#include <QTableWidget>
41
#include <QModelIndex>
42
#include <iostream>
43
#include "curveeditor.h"
44
#include "renderarea.h"
45
46
using namespace std;
47
48
CurveEditor::CurveEditor(QWidget *parent)
49
: QTabWidget(parent)
50
{
51
pTable = new QTableWidget(0, 3, this);
52
cTable = new QTableWidget(0, 6, this);
53
54
addTab(pTable, tr("Points"));
55
addTab(cTable, tr("Curves"));
56
57
connect(cTable, SIGNAL(cellChanged(int, int)), this, SLOT(cCellChanged(int, int)));
58
connect(pTable, SIGNAL(cellChanged(int, int)), this, SLOT(pCellChanged(int, int)));
59
60
clearAll();
61
}
62
63
CurveEditor::~CurveEditor()
64
{
65
}
66
67
void CurveEditor::addPoint(int idx, double x, double y)
68
{
69
int i = pTable->rowCount();
70
71
pTable->insertRow(i);
72
pTable->setRowHeight(i, 20);
73
QTableWidgetItem *item;
74
75
item = new QTableWidgetItem;
76
item->setText(QString::number(idx));
77
pTable->setItem(i, 0, item);
78
79
item = new QTableWidgetItem;
80
item->setText(QString::number(x));
81
pTable->setItem(i, 1, item);
82
83
item = new QTableWidgetItem;
84
item->setText(QString::number(y));
85
pTable->setItem(i, 2, item);
86
}
87
88
void CurveEditor::addCurve(int in, int out, int pts, int *p)
89
{
90
int i = cTable->rowCount();
91
92
cTable->insertRow(i);
93
cTable->setRowHeight(i, 20);
94
QTableWidgetItem *item;
95
96
item = new QTableWidgetItem;
97
item->setText(QString::number(in));
98
cTable->setItem(i, 0, item);
99
100
item = new QTableWidgetItem;
101
item->setText(QString::number(out));
102
cTable->setItem(i, 1, item);
103
104
item = new QTableWidgetItem;
105
item->setText(QString::number(pts));
106
cTable->setItem(i, 2, item);
107
108
item = new QTableWidgetItem;
109
item->setText(QString::number(p[0]));
110
cTable->setItem(i, 3, item);
111
112
item = new QTableWidgetItem;
113
item->setText(QString::number(p[1]));
114
cTable->setItem(i, 4, item);
115
116
item = new QTableWidgetItem;
117
if(pts == 3) {
118
item->setText(QString::number(p[2]));
119
} else {
120
item->setText("-");
121
}
122
cTable->setItem(i, 5, item);
123
}
124
125
void CurveEditor::clearAll()
126
{
127
pTable->clear();
128
pTable->setRowCount(0);
129
130
cTable->clear();
131
cTable->setRowCount(0);
132
133
QStringList pHeaders;
134
pHeaders << "idx" << "x" << "y";
135
pTable->setHorizontalHeaderLabels(pHeaders);
136
137
pTable->setColumnWidth(0, 40);
138
pTable->setColumnWidth(1, 80);
139
pTable->setColumnWidth(2, 80);
140
141
QStringList cHeaders;
142
cHeaders << "out" << "in" << "pts" << "p1" << "p2" << "p3";
143
cTable->setHorizontalHeaderLabels(cHeaders);
144
cTable->setColumnWidth(0, 40);
145
cTable->setColumnWidth(1, 40);
146
cTable->setColumnWidth(2, 40);
147
cTable->setColumnWidth(3, 40);
148
cTable->setColumnWidth(4, 40);
149
cTable->setColumnWidth(5, 40);
150
}
151
152
void CurveEditor::modifyPoint(int idx, double x, double y)
153
{
154
for(int i = 0; i < pTable->rowCount(); i++) {
155
QTableWidgetItem *item = pTable->item(i, 0);
156
157
if(item) {
158
if(item->text().toInt() == idx) {
159
QTableWidgetItem *itemX = pTable->item(i, 1);
160
if(itemX)
161
itemX->setText(QString::number(x));
162
163
QTableWidgetItem *itemY = pTable->item(i, 2);
164
if(itemY)
165
itemY->setText(QString::number(y));
166
}
167
}
168
}
169
}
170
171
void CurveEditor::setRenderArea(RenderArea *renderArea)
172
{
173
this->renderArea = renderArea;
174
}
175
176
void CurveEditor::pCellChanged(int row, int col)
177
{
178
QTableWidgetItem *item0 = pTable->item(row, 0);
179
QTableWidgetItem *item1 = pTable->item(row, 1);
180
QTableWidgetItem *item2 = pTable->item(row, 2);
181
182
int idx;
183
double x, y;
184
185
if(item0)
186
idx = item0->text().toInt();
187
else
188
return;
189
190
if(item1)
191
x = item1->text().toDouble();
192
else
193
return;
194
195
if(item2)
196
y = item2->text().toDouble();
197
else
198
return;
199
200
renderArea->modifyPoint(idx, x, y);
201
}
202
203
void CurveEditor::cCellChanged(int row, int col)
204
{
205
QTableWidgetItem *item0 = cTable->item(row, 0);
206
QTableWidgetItem *item1 = cTable->item(row, 1);
207
QTableWidgetItem *item2 = cTable->item(row, 2);
208
QTableWidgetItem *item3 = cTable->item(row, 3);
209
QTableWidgetItem *item4 = cTable->item(row, 4);
210
QTableWidgetItem *item5 = cTable->item(row, 5);
211
212
int in, out, np, p0, p1, p2;
213
214
if(item0)
215
in = item0->text().toInt();
216
else
217
return;
218
219
if(item1)
220
out = item1->text().toInt();
221
else
222
return;
223
224
if(item2)
225
np = item2->text().toInt();
226
else
227
return;
228
229
if(item3)
230
p0 = item3->text().toInt();
231
else
232
return;
233
234
if(item4)
235
p1 = item4->text().toInt();
236
else
237
return;
238
239
p2 = 0;
240
if(item5)
241
p2 = item5->text().toInt();
242
243
renderArea->modifyCurve(row, in, out, np, p0, p1, p2);
244
}
245
246
void CurveEditor::addPoint()
247
{
248
int i = pTable->rowCount();
249
250
pTable->insertRow(i);
251
pTable->setRowHeight(i, 20);
252
QTableWidgetItem *item;
253
254
for(int j = 0; j < 3; j++) {
255
item = new QTableWidgetItem;
256
item->setText("-");
257
pTable->setItem(i, j, item);
258
}
259
}
260
261
void CurveEditor::addCurve()
262
{
263
int i = cTable->rowCount();
264
265
cTable->insertRow(i);
266
cTable->setRowHeight(i, 20);
267
QTableWidgetItem *item;
268
269
for(int j = 0; j < 6; j++) {
270
item = new QTableWidgetItem;
271
item->setText("-");
272
cTable->setItem(i, j, item);
273
}
274
}
275
276
void CurveEditor::deletePoint()
277
{
278
QModelIndex index = pTable->currentIndex();
279
int row = index.row();
280
281
// Check if the point is attached to a curve:
282
//--------------------------------------------
283
bool attached = false;
284
int curve = -1;
285
int idx = pTable->item(row, 0)->text().toInt();
286
for(int i = 0; i < cTable->rowCount(); i++) {
287
int p0 = cTable->item(i, 3)->text().toInt();
288
int p1 = cTable->item(i, 4)->text().toInt();
289
int p2 = cTable->item(i, 5)->text().toInt();
290
if((p0 == idx) || (p1 == idx) || (p2 == idx)) {
291
curve = i + 1;
292
attached = true;
293
break;
294
}
295
}
296
297
// Do not delete if attached:
298
//---------------------------
299
if(attached) {
300
QString message = "Point is attached to curve " + QString::number(curve);
301
#if WITH_QT5 || WITH_QT6
302
cout << message.toLatin1().data() << endl;
303
#else
304
cout << message.toAscii().data() << endl;
305
#endif
306
emit(statusMessage(message));
307
return;
308
}
309
310
pTable->removeRow(row);
311
renderArea->updatePoints(pTable);
312
}
313
314
void CurveEditor::deleteCurve()
315
{
316
QModelIndex index = cTable->currentIndex();
317
int row = index.row();
318
cTable->removeRow(row);
319
renderArea->updateCurves(cTable);
320
}
321
322