forked from Kitware/VTK
/
vtkQtTableModelAdapter.cxx
879 lines (787 loc) · 24.7 KB
/
vtkQtTableModelAdapter.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*=========================================================================
Program: Visualization Toolkit
Module: vtkQtTableModelAdapter.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 "vtkQtTableModelAdapter.h"
#include "vtkConvertSelection.h"
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkTable.h"
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkIntArray.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkStdString.h"
#include "vtkVariant.h"
#include "vtkDoubleArray.h"
#include "vtkUnsignedCharArray.h"
#include <QColor>
#include <QIcon>
#include <QPainter>
#include <QPixmap>
#include <QHash>
#include <QMap>
#include <QMimeData>
#include <QPair>
#include <QPixmap>
#include <QImage>
#include <set>
#include <sstream>
//----------------------------------------------------------------------------
class vtkQtTableModelAdapter::vtkInternal {
public:
vtkInternal()
{
}
~vtkInternal()
{
}
QHash<QModelIndex, QVariant> IndexToDecoration;
QHash<int, QPair<vtkIdType, int> > ModelColumnToTableColumn;
QHash<int, QString> ModelColumnNames;
QHash<vtkIdType, vtkSmartPointer<vtkDoubleArray> > MagnitudeColumns;
};
//----------------------------------------------------------------------------
vtkQtTableModelAdapter::vtkQtTableModelAdapter(QObject* p)
: vtkQtAbstractModelAdapter(p)
{
this->Internal = new vtkInternal;
this->Table = NULL;
this->SplitMultiComponentColumns = false;
this->DecorationLocation = vtkQtTableModelAdapter::HEADER;
this->DecorationStrategy = vtkQtTableModelAdapter::NONE;
this->ColorColumn = -1;
this->IconIndexColumn = -1;
this->IconSheetSize[0] = this->IconSheetSize[1] = 0;
this->IconSize[0] = this->IconSize[1] = 0;
}
//----------------------------------------------------------------------------
vtkQtTableModelAdapter::vtkQtTableModelAdapter(vtkTable* t, QObject* p)
: vtkQtAbstractModelAdapter(p), Table(t)
{
this->Internal = new vtkInternal;
this->SplitMultiComponentColumns = false;
this->DecorationLocation = vtkQtTableModelAdapter::HEADER;
this->DecorationStrategy = vtkQtTableModelAdapter::NONE;
this->ColorColumn = -1;
this->IconIndexColumn = -1;
this->IconSheetSize[0] = this->IconSheetSize[1] = 0;
this->IconSize[0] = this->IconSize[1] = 0;
if (this->Table != NULL)
{
this->Table->Register(0);
}
}
//----------------------------------------------------------------------------
vtkQtTableModelAdapter::~vtkQtTableModelAdapter()
{
if (this->Table != NULL)
{
this->Table->Delete();
}
delete this->Internal;
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetColorColumnName(const char* name)
{
int color_column = this->ColorColumn;
if (name == 0 || !this->Table)
{
this->ColorColumn = -1;
}
else if (this->SplitMultiComponentColumns)
{
this->ColorColumn = -1;
int color_index=0;
foreach(QString columnname, this->Internal->ModelColumnNames)
{
if (columnname == name)
{
this->ColorColumn = color_index;
break;
}
color_index++;
}
}
else
{
this->ColorColumn = -1;
for (int i = 0; i < static_cast<int>(this->Table->GetNumberOfColumns()); i++)
{
if (!strcmp(name, this->Table->GetColumn(i)->GetName()))
{
this->ColorColumn = i;
break;
}
}
}
if (this->ColorColumn != color_column)
{
emit this->reset();
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetIconIndexColumnName(const char* name)
{
int color_column = this->IconIndexColumn;
if (name == 0 || !this->Table)
{
this->IconIndexColumn = -1;
}
else if (this->SplitMultiComponentColumns)
{
this->IconIndexColumn = -1;
int color_index=0;
foreach(QString columnname, this->Internal->ModelColumnNames)
{
if (columnname == name)
{
this->IconIndexColumn = color_index;
break;
}
color_index++;
}
}
else
{
this->IconIndexColumn = -1;
for (int i = 0; i < static_cast<int>(this->Table->GetNumberOfColumns()); i++)
{
if (!strcmp(name, this->Table->GetColumn(i)->GetName()))
{
this->IconIndexColumn = i;
break;
}
}
}
if (this->IconIndexColumn != color_column)
{
emit this->reset();
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetKeyColumnName(const char* name)
{
int key_column = this->KeyColumn;
if (name == 0 || !this->Table)
{
this->KeyColumn = -1;
}
else if (this->SplitMultiComponentColumns)
{
this->KeyColumn = -1;
int key_index=0;
foreach(QString columnname, this->Internal->ModelColumnNames)
{
if (columnname == name)
{
this->KeyColumn = key_index;
break;
}
key_index++;
}
}
else
{
this->KeyColumn = -1;
for (int i = 0; i < static_cast<int>(this->Table->GetNumberOfColumns()); i++)
{
if (!strcmp(name, this->Table->GetColumn(i)->GetName()))
{
this->KeyColumn = i;
break;
}
}
}
if (this->KeyColumn != key_column)
{
emit this->reset();
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetVTKDataObject(vtkDataObject *obj)
{
vtkTable *t = vtkTable::SafeDownCast(obj);
if (obj && !t)
{
qWarning("vtkQtTableModelAdapter needs a vtkTable for SetVTKDataObject");
return;
}
// Okay it's a table so set it :)
this->setTable(t);
}
//----------------------------------------------------------------------------
vtkDataObject* vtkQtTableModelAdapter::GetVTKDataObject() const
{
return this->Table;
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::updateModelColumnHashTables()
{
// Clear the hash tables
this->Internal->ModelColumnToTableColumn.clear();
this->Internal->ModelColumnNames.clear();
this->Internal->MagnitudeColumns.clear();
// Do not continue if SplitMultiComponentColumns is false
// or our table is null.
if (!this->SplitMultiComponentColumns || !this->Table)
{
return;
}
// Get the start and end columns.
int startColumn = 0;
int endColumn = this->Table->GetNumberOfColumns() - 1;
if (this->GetViewType() == DATA_VIEW)
{
startColumn = this->DataStartColumn;
endColumn = this->DataEndColumn;
}
// Double check to make sure startColumn and endColumn are within bounds
int maxColumn = this->Table->GetNumberOfColumns()-1;
if ((startColumn < 0 || startColumn > maxColumn) ||
(endColumn < 0 || endColumn > maxColumn))
{
return;
}
// For each column in the vtkTable, iterate over the column's number of
// components to construct a mapping from qt model columns to
// vtkTable columns-component pairs. Also generate qt model column names.
int modelColumn = 0;
for (int tableColumn = startColumn; tableColumn <= endColumn; ++tableColumn)
{
const int nComponents = this->Table->GetColumn(tableColumn)->GetNumberOfComponents();
for (int c = 0; c < nComponents; ++c)
{
QString columnName = this->Table->GetColumnName(tableColumn);
if (nComponents != 1)
{
columnName = QString("%1 (%2)").arg(columnName).arg(c);
}
this->Internal->ModelColumnNames[modelColumn] = columnName;
this->Internal->ModelColumnToTableColumn[modelColumn++] =
QPair<vtkIdType, int>(tableColumn, c);
}
// If number of components is greater than 1, create a new column for Magnitude
vtkDataArray* dataArray = vtkArrayDownCast<vtkDataArray>(this->Table->GetColumn(tableColumn));
if (nComponents > 1 && dataArray)
{
vtkSmartPointer<vtkDoubleArray> magArray = vtkSmartPointer<vtkDoubleArray>::New();
magArray->SetNumberOfComponents(1);
const vtkIdType nTuples = dataArray->GetNumberOfTuples();
for (vtkIdType i = 0; i < nTuples; ++i)
{
double mag = 0;
for (int j = 0; j < nComponents; ++j)
{
double tmp = dataArray->GetComponent(i,j);
mag += tmp*tmp;
}
mag = sqrt(mag);
magArray->InsertNextValue(mag);
}
// Create a name for this column and add it to the ModelColumnNames map
QString columnName = this->Table->GetColumnName(tableColumn);
columnName = QString("%1 (Magnitude)").arg(columnName);
this->Internal->ModelColumnNames[modelColumn] = columnName;
// Store the magnitude column mapped to its corresponding column in the vtkTable
this->Internal->MagnitudeColumns[tableColumn] = magArray;
// Add a pair that has the vtkTable column and component, but use a component value
// that is out of range to signal that this column represents magnitude
this->Internal->ModelColumnToTableColumn[modelColumn++] =
QPair<vtkIdType, int>(tableColumn, nComponents);
}
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::setTable(vtkTable* t)
{
if (this->Table != NULL)
{
this->Table->Delete();
}
this->Table = t;
if (this->Table != NULL)
{
this->Table->Register(0);
// When setting a table, update the QHash tables for column mapping.
// If SplitMultiComponentColumns is disabled, this call will just clear
// the tables and return.
this->updateModelColumnHashTables();
// We will assume the table is totally
// new and any views should update completely
emit this->reset();
}
}
//----------------------------------------------------------------------------
bool vtkQtTableModelAdapter::noTableCheck() const
{
if (this->Table == NULL)
{
// It's not necessarily an error to have a null pointer for the
// table. It just means that the model is empty.
return true;
}
if (this->Table->GetNumberOfRows() == 0)
{
return true;
}
return false;
}
//----------------------------------------------------------------------------
// Description:
// Selection conversion from VTK land to Qt land
vtkSelection* vtkQtTableModelAdapter::QModelIndexListToVTKIndexSelection(
const QModelIndexList qmil) const
{
// Create vtk index selection
vtkSelection* IndexSelection = vtkSelection::New(); // Caller needs to delete
vtkSmartPointer<vtkSelectionNode> node =
vtkSmartPointer<vtkSelectionNode>::New();
node->SetContentType(vtkSelectionNode::INDICES);
node->SetFieldType(vtkSelectionNode::ROW);
vtkSmartPointer<vtkIdTypeArray> index_arr =
vtkSmartPointer<vtkIdTypeArray>::New();
node->SetSelectionList(index_arr);
IndexSelection->AddNode(node);
// Run through the QModelIndexList pulling out vtk indexes
std::set<int> unique_ids;
for (int i = 0; i < qmil.size(); i++)
{
unique_ids.insert(qmil.at(i).internalId());
}
std::set<int>::iterator iter;
for (iter = unique_ids.begin(); iter != unique_ids.end(); ++iter)
{
index_arr->InsertNextValue(*iter);
}
return IndexSelection;
}
//----------------------------------------------------------------------------
QItemSelection vtkQtTableModelAdapter::VTKIndexSelectionToQItemSelection(
vtkSelection *vtksel) const
{
QItemSelection qis_list;
vtkSelectionNode* node = vtksel->GetNode(0);
if (node)
{
vtkIdTypeArray* arr = vtkArrayDownCast<vtkIdTypeArray>(node->GetSelectionList());
if (arr)
{
for (vtkIdType i = 0; i < arr->GetNumberOfTuples(); i++)
{
vtkIdType vtk_index = arr->GetValue(i);
QModelIndex qmodel_index =
this->createIndex(vtk_index, 0, static_cast<int>(vtk_index));
qis_list.select(qmodel_index, qmodel_index);
}
}
}
return qis_list;
}
//----------------------------------------------------------------------------
bool vtkQtTableModelAdapter::GetSplitMultiComponentColumns() const
{
return this->SplitMultiComponentColumns;
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetSplitMultiComponentColumns(bool value)
{
if (value != this->SplitMultiComponentColumns)
{
this->SplitMultiComponentColumns = value;
this->updateModelColumnHashTables();
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetDecorationStrategy(int s)
{
if (s != this->DecorationStrategy)
{
this->DecorationStrategy = s;
}
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::SetDecorationLocation(int s)
{
if (s != this->DecorationLocation)
{
this->DecorationLocation = s;
}
}
//----------------------------------------------------------------------------
QVariant vtkQtTableModelAdapter::data(const QModelIndex &idx, int role) const
{
if (this->noTableCheck())
{
return QVariant();
}
if (!idx.isValid())
{
return QVariant();
}
// Map the qt model column to a column in the vtk table and
// get the value from the table as a vtkVariant
vtkVariant v;
this->getValue(idx.row(), idx.column(), v);
// Return a string if they ask for a display role
if (role == Qt::DisplayRole)
{
bool ok;
double value = v.ToDouble(&ok);
if (ok)
{
return QVariant(value);
}
else
{
return QString::fromUtf8(v.ToUnicodeString().utf8_str()).trimmed();
}
}
// Return a byte array if they ask for a decorate role
if (role == Qt::DecorationRole)
{
if(this->DecorationStrategy == vtkQtTableModelAdapter::COLORS &&
this->DecorationLocation == vtkQtTableModelAdapter::ITEM && this->ColorColumn >= 0)
{
return this->getColorIcon(idx.row());
}
else if(this->DecorationStrategy == vtkQtTableModelAdapter::ICONS &&
this->DecorationLocation == vtkQtTableModelAdapter::ITEM && this->IconIndexColumn >= 0)
{
return this->getIcon(idx.row());
}
return this->Internal->IndexToDecoration[idx];
}
// Return a raw value if they ask for a user role
if (role == Qt::UserRole)
{
if (v.IsNumeric())
{
return QVariant(v.ToDouble());
}
return QVariant(v.ToString().c_str());
}
// Hmmm... if the role isn't decorate, user or display
// then just punt and return a empty qvariant
return QVariant();
}
//----------------------------------------------------------------------------
bool vtkQtTableModelAdapter::setData(const QModelIndex &idx, const QVariant &value, int role)
{
if (role == Qt::DecorationRole)
{
this->Internal->IndexToDecoration[idx] = value;
emit this->dataChanged(idx, idx);
return true;
}
return false;
}
//----------------------------------------------------------------------------
Qt::ItemFlags vtkQtTableModelAdapter::flags(const QModelIndex &idx) const
{
if (!idx.isValid())
{
return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
}
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled;
}
//----------------------------------------------------------------------------
QVariant vtkQtTableModelAdapter::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (this->noTableCheck())
{
return QVariant();
}
// For horizontal headers, try to convert the column names to double.
// If it doesn't work, return a string.
if (orientation == Qt::Horizontal &&
(role == Qt::DisplayRole || role == Qt::UserRole))
{
QString columnName;
if (this->GetSplitMultiComponentColumns())
{
columnName = this->Internal->ModelColumnNames[section];
}
else
{
int column = this->ModelColumnToFieldDataColumn(section);
columnName = this->Table->GetColumnName(column);
}
QVariant svar(columnName);
bool ok;
double value = svar.toDouble(&ok);
if (ok)
{
return QVariant(value);
}
return svar;
}
// For vertical headers, return values in the first column if
// KeyColumn is valid.
if (orientation == Qt::Vertical)
{
if(role == Qt::DisplayRole || role == Qt::UserRole)
{
if(this->KeyColumn >= 0)
{
vtkVariant v;
this->getValue(section, this->KeyColumn, v);
if (v.IsNumeric())
{
return QVariant(v.ToDouble());
}
return QVariant(v.ToString().c_str());
}
}
else if(role == Qt::DecorationRole &&
this->DecorationStrategy == vtkQtTableModelAdapter::ICONS &&
this->DecorationLocation == vtkQtTableModelAdapter::ITEM && this->IconIndexColumn >= 0)
{
return this->getIcon(section);
}
}
return QVariant();
}
//----------------------------------------------------------------------------
void vtkQtTableModelAdapter::getValue(int row, int in_column, vtkVariant& v) const
{
// Map the qt model column to a column in the vtk table
int column;
if (this->GetSplitMultiComponentColumns())
{
column = this->Internal->ModelColumnToTableColumn[in_column].first;
}
else
{
column = this->ModelColumnToFieldDataColumn(in_column);
}
// Get the value from the table as a vtkVariant
// We don't use vtkTable::GetValue() since for multi-component arrays, it can
// be slow due to the use of vtkDataArray in the vtkVariant.
vtkAbstractArray* array = this->Table->GetColumn(column);
if (!array)
{
return;
}
const int nComponents = array->GetNumberOfComponents();
// Special case- if the variant is an array it means the column data
// has multiple components
if (nComponents == 1)
{
v = array->GetVariantValue(row);
}
else if (nComponents > 1)
{
if (this->GetSplitMultiComponentColumns())
{
// Map the qt model column to the corresponding component in the vtkTable column
const int component = this->Internal->ModelColumnToTableColumn[in_column].second;
if (component < nComponents)
{
// If component is in range, then fetch the component's value
v = array->GetVariantValue(nComponents*row + component);
}
else
{
// If component is out of range this signals that we should return the
// value from the magnitude column
v = this->Internal->MagnitudeColumns[column]->GetValue(row);
}
}
else // don't split columns.
{
QString strValue;
for (int i = 0; i < nComponents; ++i)
{
strValue.append(QString("%1, ").arg(
array->GetVariantValue(row*nComponents + i).ToString().c_str()));
}
strValue = strValue.remove(strValue.size()-2, 2); // remove the last comma
// Reconstruct the variant using this string value
v = vtkVariant(strValue.toLatin1().data());
}
}
}
//----------------------------------------------------------------------------
QModelIndex vtkQtTableModelAdapter::index(int row, int column,
const QModelIndex & vtkNotUsed(parentIdx)) const
{
return createIndex(row, column, row);
}
//----------------------------------------------------------------------------
QModelIndex vtkQtTableModelAdapter::parent(const QModelIndex & vtkNotUsed(idx)) const
{
return QModelIndex();
}
//----------------------------------------------------------------------------
int vtkQtTableModelAdapter::rowCount(const QModelIndex & mIndex) const
{
if (this->noTableCheck())
{
return 0;
}
if (mIndex == QModelIndex())
{
return this->Table->GetNumberOfRows();
}
return 0;
}
//----------------------------------------------------------------------------
int vtkQtTableModelAdapter::columnCount(const QModelIndex &) const
{
if (this->noTableCheck())
{
return 0;
}
// If we are splitting multi-component columns, then just return the
// number of entries in the QHash map that stores column names.
if (this->GetSplitMultiComponentColumns())
{
return this->Internal->ModelColumnNames.size();
}
// The number of columns in the qt model depends on the current ViewType
switch (this->ViewType)
{
case FULL_VIEW:
return this->Table->GetNumberOfColumns();;
case DATA_VIEW:
return this->DataEndColumn - this->DataStartColumn + 1;;
default:
vtkGenericWarningMacro("vtkQtTreeModelAdapter: Bad view type.");
};
return 0;
}
bool vtkQtTableModelAdapter::dropMimeData(const QMimeData *d,
Qt::DropAction action, int vtkNotUsed(row), int vtkNotUsed(column), const QModelIndex& vtkNotUsed(parent))
{
if (action == Qt::IgnoreAction)
return true;
if (!d->hasFormat("vtk/selection"))
return false;
void* temp = 0;
std::istringstream buffer(d->data("vtk/selection").data());
buffer >> temp;
vtkSelection* s = reinterpret_cast<vtkSelection*>(temp);
emit this->selectionDropped(s);
return true;
}
QStringList vtkQtTableModelAdapter::mimeTypes() const
{
QStringList types;
types << "vtk/selection";
return types;
}
Qt::DropActions vtkQtTableModelAdapter::supportedDropActions() const
{
return Qt::CopyAction;
}
QMimeData *vtkQtTableModelAdapter::mimeData(const QModelIndexList &indexes) const
{
// Only supports dragging single item right now ...
if(indexes.size() == 0)
{
return 0;
}
vtkSmartPointer<vtkSelection> indexSelection = vtkSmartPointer<vtkSelection>::Take(QModelIndexListToVTKIndexSelection(indexes));
//vtkSmartPointer<vtkSelection> pedigreeIdSelection = vtkSmartPointer<vtkSelection>::Take(vtkConvertSelection::ToSelectionType(indexSelection, this->Table, vtkSelectionNode::PEDIGREEIDS));
// This is a memory-leak, we need to serialize its contents as a string, instead of serializing a pointer to the object
vtkSelection* pedigreeIdSelection = vtkConvertSelection::ToSelectionType(indexSelection, this->Table, vtkSelectionNode::PEDIGREEIDS);
if(pedigreeIdSelection->GetNode(0) == 0 || pedigreeIdSelection->GetNode(0)->GetSelectionList()->GetNumberOfTuples() == 0)
{
return 0;
}
std::ostringstream buffer;
buffer << pedigreeIdSelection;
QMimeData *mime_data = new QMimeData();
mime_data->setData("vtk/selection", buffer.str().c_str());
return mime_data;
}
QVariant vtkQtTableModelAdapter::getColorIcon(int row) const
{
int column;
if (this->GetSplitMultiComponentColumns())
{
column = this->Internal->ModelColumnToTableColumn[this->ColorColumn].first;
}
else
{
column = this->ModelColumnToFieldDataColumn(this->ColorColumn);
}
vtkUnsignedCharArray* colors = vtkArrayDownCast<vtkUnsignedCharArray>(this->Table->GetColumn(column));
if (!colors)
{
return QVariant();
}
const int nComponents = colors->GetNumberOfComponents();
if(nComponents >= 3)
{
unsigned char rgba[4];
colors->GetTypedTuple(row, rgba);
int rgb[3];
rgb[0] = static_cast<int>(0x0ff & rgba[0]);
rgb[1] = static_cast<int>(0x0ff & rgba[1]);
rgb[2] = static_cast<int>(0x0ff & rgba[2]);
QPixmap pixmap(16, 16);
pixmap.fill(QColor(0, 0, 0, 0));
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(QColor(rgb[0], rgb[1], rgb[2])));
painter.drawEllipse(4, 4, 7, 7);
return QVariant(pixmap);
}
return QVariant();
}
QVariant vtkQtTableModelAdapter::getIcon(int row) const
{
int column;
if (this->GetSplitMultiComponentColumns())
{
column = this->Internal->ModelColumnToTableColumn[this->IconIndexColumn].first;
}
else
{
column = this->ModelColumnToFieldDataColumn(this->IconIndexColumn);
}
vtkIntArray* icon_indices = vtkArrayDownCast<vtkIntArray>(this->Table->GetColumn(column));
if (!icon_indices)
{
return QVariant();
}
int icon_idx = icon_indices->GetValue(row);
int x, y;
int dimX = this->IconSheetSize[0]/this->IconSize[0];
x = (icon_idx >= dimX) ? icon_idx % dimX : icon_idx;
x *= this->IconSize[0];
y = (icon_idx >= dimX) ? static_cast<int>(icon_idx/dimX) : 0;
y *= this->IconSize[1];
return this->IconSheet.copy(x, y, this->IconSize[0], this->IconSize[1]);
}
void vtkQtTableModelAdapter::SetIconSheet(QImage sheet)
{
this->IconSheet = sheet;
}
void vtkQtTableModelAdapter::SetIconSheetSize(int w, int h)
{
this->IconSheetSize[0] = w;
this->IconSheetSize[1] = h;
}
void vtkQtTableModelAdapter::SetIconSize(int w, int h)
{
this->IconSize[0] = w;
this->IconSize[1] = h;
}