-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkView.cpp
333 lines (272 loc) · 8.31 KB
/
vtkView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <QMouseEvent>
#include "mainWindow.h" // contains GLEW.H
#include "vtkWidget.h"
#include "vtkView.h"
// constructors
VtkSplitter::VtkSplitter( QWidget * parent):QSplitter(parent)
{
setAttribute(Qt::WA_DeleteOnClose);
}
VtkSplitter::VtkSplitter(Qt::Orientation orientation, QWidget *parent):QSplitter(orientation,parent)
{
setAttribute(Qt::WA_DeleteOnClose);
}
QSplitterHandle *VtkSplitter::createHandle()
{
return new VtkSplitterHandle(orientation(), this);
}
VtkView *VtkSplitter::getRootContainer()
{
VtkSplitter * parentVtkSplitter = qobject_cast<VtkSplitter *>(this);
VtkView* mvc = qobject_cast<VtkView *>(parentVtkSplitter);
while(!mvc)
{
parentVtkSplitter = qobject_cast<VtkSplitter *>(parentVtkSplitter->parent());
mvc= qobject_cast<VtkView *>(parentVtkSplitter);
}
return mvc;
}
VtkSplitterHandle::VtkSplitterHandle(Qt::Orientation orientation, QSplitter *parent):QSplitterHandle(orientation, parent){}
void VtkSplitterHandle::mousePressEvent ( QMouseEvent * e )
{
QSplitterHandle::mousePressEvent(e);
if(e->button()== Qt::RightButton)
{
MainWindow *window = qobject_cast<MainWindow *>(QApplication::activeWindow());
if (window) window->setHandleMenu(mapToGlobal(e->pos()), orientation(), splitter());
}
}
VtkView::VtkView(QWidget *parent)
: VtkSplitter(parent)
{
// this->setParent(parent);
setAttribute(Qt::WA_DeleteOnClose);//MK
setChildrenCollapsible(false);
currentId=-1;
}
VtkView::~VtkView()
{
//MK: we don't have anything to delete
// foreach(VtkWidget* viewer, viewerList)
// {
// delete viewer;
// }
}
int VtkView::getNextViewerId(){
int newId=-1;
foreach(VtkWidget* view, viewerList)
{
if(newId < view->getId()) newId = view->getId();
}
return ++newId;
}
void VtkView::addOrphanView(VtkWidget* viewer){
viewerList.append(viewer);
addWidget(viewer);
currentId = viewer->getId();
//action for new viewer
connect(viewer, SIGNAL(currentViewerChanged(int)), this, SLOT(updateCurrent(int)));
return;
}
void VtkView::initializeView(VtkWidget* viewer){
if (viewerCounter()>0){
viewerList.clear();
}
viewerList.append(viewer);
addWidget(viewer);
currentId = viewer->getId();
//action for new viewer
connect(viewer, SIGNAL(currentViewerChanged(int)), this, SLOT(updateCurrent(int)));
return;
}
void VtkView::addView(VtkWidget* viewer,Qt::Orientation orient){
/* The Viewers are organized like a BSP tree.
Every new viewer is added within an Horizontal splitter. Its orientation could change according to next insertions.
HSplit
/ \
View1 VSplit
/ \
View2 HSplit
/
View3
In the GUI, when a viewer is splitted, the new one appears on its right (the space is split in two equal portions).
*/
//CASE 0: only when the first viewer is opened, just add it and return;
if (viewerCounter()==0)
{
viewerList.append(viewer);
addWidget(viewer);
currentId = viewer->getId();
//action for new viewer
connect(viewer, SIGNAL(currentViewerChanged(int)), this, SLOT(updateCurrent(int)));
return;
}
//CASE 1: happens only at the FIRST split;
if (viewerCounter()==1)
{
//MK: add a new viewer class
viewerList.append(viewer);
addWidget(viewer);
//MK: separating window
this->setOrientation(orient);
QList<int> sizes;
if(this->orientation()== Qt::Horizontal){
sizes.append(this->width()/2);
sizes.append(this->width()/2);
}
else{
sizes.append(this->height()/2);
sizes.append(this->height()/2);
}
this->setSizes(sizes);
this->setHandleWidth(2);
this->setChildrenCollapsible(false);
//MK: give a new id
currentId = viewer->getId();
//action for new viewer
connect(viewer, SIGNAL(currentViewerChanged(int)), this, SLOT(updateCurrent(int)));
return;
}
// Generic Case: Each splitter Has ALWAYS two children.
viewerList.append(viewer);
VtkWidget* currentGLA = this->currentView();
VtkSplitter* currentVtkSplitter = qobject_cast<VtkSplitter *>(currentGLA->parent());
QList<int> parentSizes = currentVtkSplitter->sizes();
int splittedIndex = currentVtkSplitter->indexOf(currentGLA);
VtkSplitter* newVtkSplitter = new VtkSplitter(orient);
currentVtkSplitter->insertWidget(splittedIndex,newVtkSplitter);
newVtkSplitter->addWidget(viewer);
newVtkSplitter->addWidget(currentGLA);
QList<int> sizes;
if(orient== Qt::Horizontal){
sizes.append(currentVtkSplitter->width()/2);
sizes.append(currentVtkSplitter->width()/2);
}
else{
sizes.append(currentVtkSplitter->height()/2);
sizes.append(currentVtkSplitter->height()/2);
}
currentVtkSplitter->setSizes(parentSizes);
newVtkSplitter->setSizes(sizes);
newVtkSplitter->setHandleWidth(2);
newVtkSplitter->setChildrenCollapsible(false);
currentId = viewer->getId();
//action for new viewer
connect(viewer, SIGNAL(currentViewerChanged(int)), this, SLOT(updateCurrent(int)));
return;
}
void VtkView::removeView(int viewerId)
{
VtkWidget* viewer;
for (int i=0; i< viewerList.count(); i++)
{
if(viewerList.at(i)->getId() == viewerId)
viewer = viewerList.at(i);
}
//assert(viewer);
VtkSplitter* parentVtkSplitter = qobject_cast<VtkSplitter *>(viewer->parent());
int currentIndex = parentVtkSplitter->indexOf(viewer);
viewer->deleteLater();
// Very basic case of just two son of the MultiviewContainer.
if(viewerList.count()==2)
{
viewerList.removeAll(viewer);
currentId = viewerList.first()->getId();
return;
}
// generic tree with more of two leaves (some splitter involved)
// two cases
// 1) the deleted object is a direct child of the root
// 2) otherwise; e.g. parent->parent exists.
// First Case: deleting the direct son of the root (the mvc)
// the sibling content (that is a splitter) surely will be moved up
if(parentVtkSplitter == this)
{
int insertIndex;
if(currentIndex == 0) insertIndex = 1;
else insertIndex = 0;
VtkSplitter *siblingVtkSplitter = qobject_cast<VtkSplitter *>(this->widget(insertIndex));
//assert(siblingVtkSplitter);
QWidget *sonLeft = siblingVtkSplitter->widget(0);
QWidget *sonRight = siblingVtkSplitter->widget(1);
this->setOrientation(siblingVtkSplitter->orientation());
this->insertWidget(0,sonLeft);
this->insertWidget(1,sonRight);
siblingVtkSplitter->deleteLater();
viewerList.removeAll(viewer);
currentId = viewerList.first()->getId();
return;
}
// Final case. Very generic, not son of the root.
VtkSplitter* parentParentVtkSplitter = qobject_cast<VtkSplitter *>(parentVtkSplitter->parent());
//assert(parentParentVtkSplitter);
int parentIndex= parentParentVtkSplitter->indexOf(parentVtkSplitter);
int siblingIndex;
if(currentIndex == 0) siblingIndex = 1;
else siblingIndex = 0;
QWidget *siblingWidget = parentVtkSplitter->widget(siblingIndex);
//assert(siblingWidget);
parentParentVtkSplitter->insertWidget(parentIndex,siblingWidget);
parentVtkSplitter->deleteLater();
viewerList.removeAll(viewer);
currentId = viewerList.first()->getId();
}
void VtkView::updateCurrent(int current){
int previousCurrentId = currentId;
currentId=current;
if(getViewer(previousCurrentId))
update(previousCurrentId);
emit updateMainWindowMenus();
}
VtkWidget * VtkView::getViewer(int id)
{
foreach ( VtkWidget* viewer, viewerList)
if (viewer->getId() == id)
return viewer;
return 0;
}
int VtkView::getViewerByPicking(QPoint p){
foreach ( VtkWidget* viewer, viewerList){
QPoint pViewer = viewer->mapFromGlobal(p);
if(viewer->visibleRegion().contains(pViewer))
return viewer->getId();
}
return -1;
}
VtkWidget* VtkView::currentView(){
return getViewer(currentId);
}
int VtkView::viewerCounter(){
return viewerList.count();
}
void VtkView::updateAllViewer(){
foreach ( VtkWidget* viewer, viewerList)
viewer->update();
}
void VtkView::update(int id){
getViewer(id)->update();
}
MainWindow * VtkView::findmw()
{
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
MainWindow* mainwindow = dynamic_cast<MainWindow*>(widget);
if (mainwindow)
{
return mainwindow;
}
}
}
void VtkView::closeEvent( QCloseEvent *event )
{
// bool close = true;
// int ii = 0;
// while(close && (ii < viewerList.size()))
// {
// close = viewerList.at(ii)->readyToClose();
// ++ii;
// }
// if (close)
// event->accept();
// else
// event->ignore();
}