forked from Kitware/VTK
/
vtkQtTreeView.cxx
543 lines (461 loc) · 16.9 KB
/
vtkQtTreeView.cxx
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*=========================================================================
Program: Visualization Toolkit
Module: vtkQtTreeView.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkQtTreeView.h"
#include <QAbstractItemView>
#include <QColumnView>
#include <QHeaderView>
#include <QItemSelection>
#include <QItemSelectionModel>
#include <QTreeView>
#include <QVBoxLayout>
#include "vtkAlgorithm.h"
#include "vtkAlgorithmOutput.h"
#include "vtkAnnotation.h"
#include "vtkAnnotationLayers.h"
#include "vtkAnnotationLink.h"
#include "vtkApplyColors.h"
#include "vtkConvertSelection.h"
#include "vtkDataRepresentation.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkLookupTable.h"
#include "vtkObjectFactory.h"
#include "vtkQtTreeModelAdapter.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkTree.h"
#include "vtkViewTheme.h"
vtkStandardNewMacro(vtkQtTreeView);
//----------------------------------------------------------------------------
vtkQtTreeView::vtkQtTreeView()
{
this->ApplyColors = vtkSmartPointer<vtkApplyColors>::New();
this->Widget = new QWidget();
this->TreeView = new QTreeView();
this->ColumnView = new QColumnView();
this->TreeAdapter = new vtkQtTreeModelAdapter();
this->TreeFilter = new QFilterTreeProxyModel();
this->TreeFilter->setSourceModel(this->TreeAdapter);
this->TreeFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
this->TreeView->setModel(this->TreeFilter);
this->ColumnView->setModel(this->TreeFilter);
this->SelectionModel = new QItemSelectionModel(this->TreeAdapter);
this->TreeView->setSelectionModel(this->SelectionModel);
this->ColumnView->setSelectionModel(this->SelectionModel);
this->Layout = new QVBoxLayout(this->GetWidget());
this->Layout->setContentsMargins(0,0,0,0);
// Add both widgets to the layout and then hide one
this->Layout->addWidget(this->TreeView);
this->Layout->addWidget(this->ColumnView);
this->ColumnView->hide();
// Set up some default properties
this->TreeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->TreeView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->ColumnView->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->ColumnView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->SetUseColumnView(false);
this->SetAlternatingRowColors(false);
this->SetShowRootNode(false);
this->CurrentSelectionMTime = 0;
this->ColorArrayNameInternal = 0;
double defCol[3] = {0.827,0.827,0.827};
this->ApplyColors->SetDefaultPointColor(defCol);
this->ApplyColors->SetUseCurrentAnnotationColor(true);
this->LastInputMTime = 0;
// Drag/Drop parameters - defaults to off
this->TreeView->setDragEnabled(false);
this->TreeView->setDragDropMode(QAbstractItemView::DragOnly);
this->TreeView->setDragDropOverwriteMode(false);
this->TreeView->setAcceptDrops(false);
this->TreeView->setDropIndicatorShown(false);
this->ColumnView->setDragEnabled(false);
this->ColumnView->setDragDropMode(QAbstractItemView::DragOnly);
this->ColumnView->setDragDropOverwriteMode(false);
this->ColumnView->setAcceptDrops(false);
this->ColumnView->setDropIndicatorShown(false);
QObject::connect(this->TreeView,
SIGNAL(expanded(const QModelIndex&)),
this, SIGNAL(expanded(const QModelIndex&)));
QObject::connect(this->TreeView,
SIGNAL(collapsed(const QModelIndex&)),
this, SIGNAL(collapsed(const QModelIndex&)));
QObject::connect(this->SelectionModel,
SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
this,
SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
QObject::connect(this->ColumnView,
SIGNAL(updatePreviewWidget(const QModelIndex&)),
this, SIGNAL(updatePreviewWidget(const QModelIndex&)));
}
//----------------------------------------------------------------------------
vtkQtTreeView::~vtkQtTreeView()
{
delete this->TreeView;
delete this->ColumnView;
delete this->Layout;
delete this->Widget;
delete this->SelectionModel;
delete this->TreeAdapter;
delete this->TreeFilter;
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetUseColumnView(int state)
{
if (state)
{
this->ColumnView->show();
this->TreeView->hide();
this->View = qobject_cast<QAbstractItemView *>(this->ColumnView);
}
else
{
this->ColumnView->hide();
this->TreeView->show();
this->View = qobject_cast<QAbstractItemView *>(this->TreeView);
}
// Probably a good idea to make sure the container widget is refreshed
this->Widget->update();
}
//----------------------------------------------------------------------------
QWidget* vtkQtTreeView::GetWidget()
{
return this->Widget;
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetShowHeaders(bool state)
{
if (state)
{
this->TreeView->header()->show();
}
else
{
this->TreeView->header()->hide();
}
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetAlternatingRowColors(bool state)
{
this->TreeView->setAlternatingRowColors(state);
this->ColumnView->setAlternatingRowColors(state);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetEnableDragDrop(bool state)
{
this->TreeView->setDragEnabled(state);
this->ColumnView->setDragEnabled(state);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetShowRootNode(bool state)
{
if (!state)
{
this->TreeView->setRootIndex(this->TreeView->model()->index(0,0));
this->ColumnView->setRootIndex(this->TreeView->model()->index(0,0));
}
else
{
this->TreeView->setRootIndex(QModelIndex());
this->ColumnView->setRootIndex(QModelIndex());
}
}
//----------------------------------------------------------------------------
void vtkQtTreeView::HideColumn(int i)
{
this->TreeView->hideColumn(i);
this->HiddenColumns << i;
}
//----------------------------------------------------------------------------
void vtkQtTreeView::ShowColumn(int i)
{
this->TreeView->showColumn(i);
this->HiddenColumns.removeAll(i);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::HideAllButFirstColumn()
{
this->HiddenColumns.clear();
this->TreeView->showColumn(0);
for(int j=1; j<this->TreeAdapter->columnCount(); ++j)
{
this->TreeView->hideColumn(j);
this->HiddenColumns << j;
}
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetFilterColumn(int i)
{
this->TreeFilter->setFilterKeyColumn(i);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetFilterRegExp(const QRegExp& pattern)
{
this->TreeFilter->setFilterRegExp(pattern);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetFilterTreeLevel(int level)
{
this->TreeFilter->setFilterTreeLevel(level);
}
void vtkQtTreeView::AddRepresentationInternal(vtkDataRepresentation* rep)
{
vtkAlgorithmOutput *annConn, *conn;
conn = rep->GetInputConnection();
annConn = rep->GetInternalAnnotationOutputPort();
this->ApplyColors->SetInputConnection(0, conn);
if(annConn)
{
this->ApplyColors->SetInputConnection(1, annConn);
}
}
void vtkQtTreeView::RemoveRepresentationInternal(vtkDataRepresentation* rep)
{
vtkAlgorithmOutput *annConn, *conn;
conn = rep->GetInputConnection();
annConn = rep->GetInternalAnnotationOutputPort();
this->ApplyColors->RemoveInputConnection(0, conn);
this->ApplyColors->RemoveInputConnection(1, annConn);
this->TreeAdapter->SetVTKDataObject(0);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetItemDelegate(QAbstractItemDelegate* delegate)
{
this->TreeView->setItemDelegate(delegate);
this->ColumnView->setItemDelegate(delegate);
}
void vtkQtTreeView::SetColorByArray(bool b)
{
this->ApplyColors->SetUsePointLookupTable(b);
}
bool vtkQtTreeView::GetColorByArray()
{
return this->ApplyColors->GetUsePointLookupTable();
}
void vtkQtTreeView::SetColorArrayName(const char* name)
{
this->SetColorArrayNameInternal(name);
this->ApplyColors->SetInputArrayToProcess(0, 0, 0,
vtkDataObject::FIELD_ASSOCIATION_VERTICES, name);
}
const char* vtkQtTreeView::GetColorArrayName()
{
return this->GetColorArrayNameInternal();
}
//----------------------------------------------------------------------------
void vtkQtTreeView::slotQtSelectionChanged(const QItemSelection& vtkNotUsed(s1), const QItemSelection& vtkNotUsed(s2))
{
// Convert from a QModelIndexList to an index based vtkSelection
const QModelIndexList qmil = this->View->selectionModel()->selectedRows();
QModelIndexList origRows;
for(int i=0; i<qmil.size(); ++i)
{
origRows.push_back(this->TreeFilter->mapToSource(qmil[i]));
}
// If in column view mode, don't propagate a selection of a non-leaf node
// since such a selection is used to expand the next column.
if(this->ColumnView->isVisible())
{
bool leafNodeSelected = false;
for(int i=0; i<origRows.size(); ++i)
{
if(!this->TreeAdapter->hasChildren(origRows[i]))
{
leafNodeSelected = true;
break;
}
}
if(!leafNodeSelected)
{
return;
}
}
vtkSelection *VTKIndexSelectList = this->TreeAdapter->QModelIndexListToVTKIndexSelection(origRows);
// Convert to the correct type of selection
vtkDataRepresentation* rep = this->GetRepresentation();
vtkDataObject* data = this->TreeAdapter->GetVTKDataObject();
vtkSmartPointer<vtkSelection> converted;
converted.TakeReference(vtkConvertSelection::ToSelectionType(
VTKIndexSelectList, data, rep->GetSelectionType(), rep->GetSelectionArrayNames()));
// Call select on the representation (all 'linked' views will receive this selection)
rep->Select(this, converted);
// Delete the selection list
VTKIndexSelectList->Delete();
// Store the selection mtime
this->CurrentSelectionMTime = rep->GetAnnotationLink()->GetCurrentSelection()->GetMTime();
}
//----------------------------------------------------------------------------
void vtkQtTreeView::SetVTKSelection()
{
// Check to see we actually have data
vtkDataObject *d = this->TreeAdapter->GetVTKDataObject();
if (!d)
{
return;
}
// See if the selection has changed in any way
vtkDataRepresentation* rep = this->GetRepresentation();
vtkAlgorithmOutput *annConn = rep->GetInternalAnnotationOutputPort();
vtkAnnotationLayers* a = vtkAnnotationLayers::SafeDownCast(annConn->GetProducer()->GetOutputDataObject(0));
vtkSelection* s = a->GetCurrentAnnotation()->GetSelection();
vtkSmartPointer<vtkSelection> selection;
selection.TakeReference(vtkConvertSelection::ToSelectionType(
s, d, vtkSelectionNode::INDICES, 0, vtkSelectionNode::VERTEX));
QItemSelection qisList = this->TreeAdapter->
VTKIndexSelectionToQItemSelection(selection);
QItemSelection filteredSel = this->TreeFilter->mapSelectionFromSource(qisList);
// Here we want the qt model to have it's selection changed
// but we don't want to emit the selection.
QObject::disconnect(this->View->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
this, SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
this->View->selectionModel()->select(filteredSel,
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
QObject::connect(this->View->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
this, SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
// Make sure selected items are visible
// FIXME: Should really recurse up all levels of the tree, this just does one.
for(int i=0; i<filteredSel.size(); ++i)
{
this->TreeView->setExpanded(filteredSel[i].parent(), true);
}
}
//----------------------------------------------------------------------------
void vtkQtTreeView::Update()
{
vtkDataRepresentation* rep = this->GetRepresentation();
if (!rep)
{
// Remove VTK data from the adapter
this->TreeAdapter->SetVTKDataObject(0);
this->View->update();
return;
}
rep->Update();
// Make the data current
vtkAlgorithm* alg = rep->GetInputConnection()->GetProducer();
alg->Update();
vtkDataObject *d = alg->GetOutputDataObject(0);
vtkTree *tree = vtkTree::SafeDownCast(d);
// Special-case: if our input is missing or not-a-tree, or empty then quietly exit.
if(!tree || !tree->GetNumberOfVertices())
{
return;
}
vtkAlgorithmOutput *annConn = rep->GetInternalAnnotationOutputPort();
if(annConn)
{
annConn->GetProducer()->Update();
}
this->ApplyColors->Update();
if (tree->GetMTime() > this->LastInputMTime)
{
// Reset the model
this->TreeAdapter->SetVTKDataObject(0);
this->TreeAdapter->SetVTKDataObject(this->ApplyColors->GetOutput());
if (this->GetColorByArray())
{
this->TreeAdapter->SetColorColumnName("vtkApplyColors color");
}
else
{
this->TreeAdapter->SetColorColumnName("");
}
this->TreeView->resizeColumnToContents(0);
this->TreeView->collapseAll();
// Reset show root node if it was false.
if (this->TreeView->rootIndex() != QModelIndex())
{
this->SetShowRootNode(false);
}
this->LastInputMTime = tree->GetMTime();
}
unsigned long atime = rep->GetAnnotationLink()->GetMTime();
if (atime > this->CurrentSelectionMTime)
{
this->SetVTKSelection();
this->CurrentSelectionMTime = atime;
}
// Re-hide the hidden columns
int col;
foreach (col, this->HiddenColumns)
{
this->TreeView->hideColumn(col);
}
for (int j=0; j<this->TreeAdapter->columnCount(); ++j)
{
QString colName = this->TreeAdapter->headerData(j, Qt::Horizontal).toString();
if(colName == "vtkApplyColors color")
{
this->TreeView->hideColumn(j);
}
}
// Redraw the view
this->TreeView->update();
this->ColumnView->update();
}
//----------------------------------------------------------------------------
void vtkQtTreeView::ApplyViewTheme(vtkViewTheme* theme)
{
this->Superclass::ApplyViewTheme(theme);
this->ApplyColors->SetPointLookupTable(theme->GetPointLookupTable());
this->ApplyColors->SetDefaultPointColor(theme->GetPointColor());
this->ApplyColors->SetDefaultPointOpacity(theme->GetPointOpacity());
this->ApplyColors->SetDefaultCellColor(theme->GetCellColor());
this->ApplyColors->SetDefaultCellOpacity(theme->GetCellOpacity());
this->ApplyColors->SetSelectedPointColor(theme->GetSelectedPointColor());
this->ApplyColors->SetSelectedPointOpacity(theme->GetSelectedPointOpacity());
this->ApplyColors->SetSelectedCellColor(theme->GetSelectedCellColor());
this->ApplyColors->SetSelectedCellOpacity(theme->GetSelectedCellOpacity());
this->ApplyColors->SetScalePointLookupTable(theme->GetScalePointLookupTable());
this->ApplyColors->SetScaleCellLookupTable(theme->GetScaleCellLookupTable());
}
//----------------------------------------------------------------------------
void vtkQtTreeView::Collapse( const QModelIndex & index )
{
this->TreeView->collapse(index);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::CollapseAll()
{
this->TreeView->collapseAll();
}
//----------------------------------------------------------------------------
void vtkQtTreeView::Expand ( const QModelIndex & index )
{
this->TreeView->expand(index);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::ExpandAll ()
{
this->TreeView->expandAll();
}
//----------------------------------------------------------------------------
void vtkQtTreeView::ExpandToDepth ( int depth )
{
this->TreeView->expandToDepth(depth);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::ResizeColumnToContents ( int column )
{
this->TreeView->resizeColumnToContents(column);
}
//----------------------------------------------------------------------------
void vtkQtTreeView::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}