vtkQtListView.cxx 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkQtListView.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.

=========================================================================*/
15 16 17 18 19
/*-------------------------------------------------------------------------
  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.
-------------------------------------------------------------------------*/
20 21 22

#include "vtkQtListView.h"

23
#include <QItemSelection>
24
#include <QListView>
25
#include <QSortFilterProxyModel>
26

27 28
#include "vtkAbstractArray.h"
#include "vtkAddMembershipArray.h"
29 30
#include "vtkAlgorithm.h"
#include "vtkAlgorithmOutput.h"
31 32
#include "vtkAnnotation.h"
#include "vtkAnnotationLayers.h"
33
#include "vtkAnnotationLink.h"
34
#include "vtkApplyColors.h"
35
#include "vtkConvertSelection.h"
36
#include "vtkDataObjectToTable.h"
37
#include "vtkDataRepresentation.h"
38 39
#include "vtkDataSetAttributes.h"
#include "vtkGraph.h"
40 41
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
42
#include "vtkLookupTable.h"
43
#include "vtkObjectFactory.h"
44
#include "vtkOutEdgeIterator.h"
45 46 47 48 49
#include "vtkQtTableModelAdapter.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkTable.h"
50
#include "vtkViewTheme.h"
51 52 53

vtkStandardNewMacro(vtkQtListView);

54

55 56 57
//----------------------------------------------------------------------------
vtkQtListView::vtkQtListView()
{
58 59
  this->ApplyColors = vtkSmartPointer<vtkApplyColors>::New();
  this->DataObjectToTable = vtkSmartPointer<vtkDataObjectToTable>::New();
60
  this->ApplyColors->SetInputConnection(0, this->DataObjectToTable->GetOutputPort(0));
61 62 63 64

  this->DataObjectToTable->SetFieldType(vtkDataObjectToTable::VERTEX_DATA);
  this->FieldType = vtkQtListView::VERTEX_DATA;

65
  this->ListView = new QListView();
66
  this->TableAdapter = new vtkQtTableModelAdapter();
67
  this->TableAdapter->SetDecorationLocation(vtkQtTableModelAdapter::ITEM);
68 69 70 71 72 73 74
  this->TableSorter = new QSortFilterProxyModel();
  this->TableSorter->setFilterCaseSensitivity(Qt::CaseInsensitive);
  this->TableSorter->setFilterRole(Qt::DisplayRole);
  this->TableSorter->setSourceModel(this->TableAdapter);
  this->ListView->setModel(this->TableSorter);
  this->ListView->setModelColumn(0);
  this->TableSorter->setFilterKeyColumn(0);
75 76
  this->TableAdapter->SetColorColumnName("vtkApplyColors color");

77
  // Set up some default properties
Jeff Baumes's avatar
Jeff Baumes committed
78 79
  this->ListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
  this->ListView->setSelectionBehavior(QAbstractItemView::SelectRows);
80 81 82 83 84 85

  this->LastSelectionMTime = 0;
  this->LastInputMTime = 0;
  this->LastMTime = 0;
  this->ApplyRowColors = false;
  this->VisibleColumn = 0;
86
  this->TableAdapter->SetDecorationStrategy(vtkQtTableModelAdapter::NONE);
87

88 89
  this->ColorArrayNameInternal = nullptr;
  this->IconIndexArrayNameInternal = nullptr;
90 91 92
  double defCol[3] = {0.827,0.827,0.827};
  this->ApplyColors->SetDefaultPointColor(defCol);
  this->ApplyColors->SetUseCurrentAnnotationColor(true);
93

94
  QObject::connect(this->ListView->selectionModel(),
95
      SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
96
      this,
Jeff Baumes's avatar
Jeff Baumes committed
97
      SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
98 99 100 101 102
}

//----------------------------------------------------------------------------
vtkQtListView::~vtkQtListView()
{
103
  delete this->ListView;
104
  delete this->TableAdapter;
105
  delete this->TableSorter;
106 107 108 109 110 111 112 113 114
}

//----------------------------------------------------------------------------
QWidget* vtkQtListView::GetWidget()
{
  return this->ListView;
}

//----------------------------------------------------------------------------
Jeff Baumes's avatar
Jeff Baumes committed
115
void vtkQtListView::SetAlternatingRowColors(bool state)
116
{
Jeff Baumes's avatar
Jeff Baumes committed
117
  this->ListView->setAlternatingRowColors(state);
118 119 120
}

//----------------------------------------------------------------------------
121
void vtkQtListView::SetEnableDragDrop(bool state)
122
{
123 124
  this->ListView->setDragEnabled(state);
}
125

126 127 128 129 130
//----------------------------------------------------------------------------
void vtkQtListView::SetFieldType(int type)
{
  this->DataObjectToTable->SetFieldType(type);
  if(this->FieldType != type)
131
  {
132 133
    this->FieldType = type;
    this->Modified();
134
  }
135
}
136

137
void vtkQtListView::SetIconSheet(QImage sheet)
138
{
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  this->TableAdapter->SetIconSheet(sheet);
}

void vtkQtListView::SetIconSheetSize(int w, int h)
{
  this->TableAdapter->SetIconSheetSize(w,h);
}

void vtkQtListView::SetIconSize(int w, int h)
{
  this->TableAdapter->SetIconSize(w,h);
}

void vtkQtListView::SetIconArrayName(const char* name)
{
  this->SetIconIndexArrayNameInternal(name);
  this->TableAdapter->SetIconIndexColumnName(name);
}

void vtkQtListView::SetDecorationStrategy(int value)
{
  this->TableAdapter->SetDecorationStrategy(value);
  this->Modified();
162
}
163

164
//----------------------------------------------------------------------------
165
void vtkQtListView::SetFilterRegExp(const QRegExp& pattern)
166 167 168 169
{
  this->ListView->selectionModel()->clearSelection();
  this->TableSorter->setFilterRegExp(pattern);
}
170

171 172 173
void vtkQtListView::SetColorByArray(bool b)
{
  this->ApplyColors->SetUsePointLookupTable(b);
174 175
}

176
bool vtkQtListView::GetColorByArray()
177
{
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  return this->ApplyColors->GetUsePointLookupTable();
}

void vtkQtListView::SetColorArrayName(const char* name)
{
  this->SetColorArrayNameInternal(name);
  this->ApplyColors->SetInputArrayToProcess(0, 0, 0,
    vtkDataObject::FIELD_ASSOCIATION_ROWS, name);
}

const char* vtkQtListView::GetColorArrayName()
{
  return this->GetColorArrayNameInternal();
}

void vtkQtListView::SetVisibleColumn(int col)
{
  this->ListView->setModelColumn(col);
  this->TableSorter->setFilterKeyColumn(col);
  this->VisibleColumn = col;
}

void vtkQtListView::AddRepresentationInternal(vtkDataRepresentation* rep)
201
{
202
  vtkAlgorithmOutput *annConn, *conn;
203 204 205
  conn = rep->GetInputConnection();
  annConn = rep->GetInternalAnnotationOutputPort();

206
  this->DataObjectToTable->SetInputConnection(0, conn);
207 208

  if(annConn)
209
  {
210
    this->ApplyColors->SetInputConnection(1, annConn);
211
  }
212 213 214
}

void vtkQtListView::RemoveRepresentationInternal(vtkDataRepresentation* rep)
215
{
216
  vtkAlgorithmOutput *annConn, *conn;
217 218 219
  conn = rep->GetInputConnection();
  annConn = rep->GetInternalAnnotationOutputPort();

220
  this->DataObjectToTable->RemoveInputConnection(0, conn);
221
  this->ApplyColors->RemoveInputConnection(1, annConn);
222
  this->TableAdapter->SetVTKDataObject(nullptr);
223
}
224

225
//----------------------------------------------------------------------------
Jeff Baumes's avatar
Jeff Baumes committed
226
void vtkQtListView::slotQtSelectionChanged(const QItemSelection& vtkNotUsed(s1), const QItemSelection& vtkNotUsed(s2))
227
{
228 229 230 231 232 233 234 235 236
  // Convert to the correct type of selection
  vtkDataObject* data = this->TableAdapter->GetVTKDataObject();
  if(!data)
    return;

  // Map the selected rows through the sorter map before sending to model
  const QModelIndexList selectedRows = this->ListView->selectionModel()->selectedRows();
  QModelIndexList origRows;
  for(int i=0; i<selectedRows.size(); ++i)
237
  {
238
    origRows.push_back(this->TableSorter->mapToSource(selectedRows[i]));
239
  }
240

241
  vtkSelection *VTKIndexSelectList =
242
    this->TableAdapter->QModelIndexListToVTKIndexSelection(origRows);
243 244

  // Convert to the correct type of selection
245
  vtkDataRepresentation* rep = this->GetRepresentation();
246 247
  vtkSmartPointer<vtkSelection> converted;
  converted.TakeReference(vtkConvertSelection::ToSelectionType(
248
    VTKIndexSelectList, data, rep->GetSelectionType(), nullptr));
249

250
  // Call select on the representation
251
  rep->Select(this, converted);
252

253 254
  // Delete the selection list
  VTKIndexSelectList->Delete();
255

256
  this->LastSelectionMTime = rep->GetAnnotationLink()->GetMTime();
257 258
}

Jeff Baumes's avatar
Jeff Baumes committed
259 260 261
//----------------------------------------------------------------------------
void vtkQtListView::SetVTKSelection()
{
262 263 264 265 266 267 268 269
  vtkDataRepresentation* rep = this->GetRepresentation();
  vtkDataObject *d = this->TableAdapter->GetVTKDataObject();
  vtkAlgorithmOutput *annConn = rep->GetInternalAnnotationOutputPort();
  vtkAnnotationLayers* a = vtkAnnotationLayers::SafeDownCast(annConn->GetProducer()->GetOutputDataObject(0));
  vtkSelection* s = a->GetCurrentAnnotation()->GetSelection();

  vtkSmartPointer<vtkSelection> selection;
  selection.TakeReference(vtkConvertSelection::ToSelectionType(
270
    s, d, vtkSelectionNode::INDICES, nullptr, vtkSelectionNode::ROW));
271

272
  if(!selection || selection->GetNumberOfNodes() == 0)
273
  {
Jeff Baumes's avatar
Jeff Baumes committed
274
    return;
275
  }
Jeff Baumes's avatar
Jeff Baumes committed
276

277
  if(selection->GetNode(0)->GetSelectionList()->GetNumberOfTuples())
278
  {
279
    QItemSelection qisList = this->TableAdapter->
Jeff Baumes's avatar
Jeff Baumes committed
280
      VTKIndexSelectionToQItemSelection(selection);
281
    QItemSelection sortedSel = this->TableSorter->mapSelectionFromSource(qisList);
282

Jeff Baumes's avatar
Jeff Baumes committed
283 284
    // Here we want the qt model to have it's selection changed
    // but we don't want to emit the selection.
285
    QObject::disconnect(this->ListView->selectionModel(),
Jeff Baumes's avatar
Jeff Baumes committed
286 287
      SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
      this, SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
288 289

    this->ListView->selectionModel()->select(sortedSel,
Jeff Baumes's avatar
Jeff Baumes committed
290
      QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
291 292

    QObject::connect(this->ListView->selectionModel(),
Jeff Baumes's avatar
Jeff Baumes committed
293 294
     SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
     this, SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
295
  }
Jeff Baumes's avatar
Jeff Baumes committed
296 297
}

298
//----------------------------------------------------------------------------
299
void vtkQtListView::Update()
300
{
301 302
  vtkDataRepresentation* rep = this->GetRepresentation();
  if (!rep)
303
  {
304
    // Remove VTK data from the adapter
305
    this->TableAdapter->SetVTKDataObject(nullptr);
306
    this->ListView->update();
307
    return;
308
  }
309
  rep->Update();
310 311

  // Make the data current
312 313 314 315 316 317 318 319 320
  vtkAlgorithmOutput *selConn, *annConn, *conn;
  conn = rep->GetInputConnection();
  conn->GetProducer()->Update();
  annConn = rep->GetInternalAnnotationOutputPort();
  annConn->GetProducer()->Update();
  selConn = rep->GetInternalSelectionOutputPort();
  selConn->GetProducer()->Update();

  vtkDataObject *d = conn->GetProducer()->GetOutputDataObject(0);
321
  vtkMTimeType atime = rep->GetAnnotationLink()->GetMTime();
322 323 324
  if (d->GetMTime() > this->LastInputMTime ||
      this->GetMTime() > this->LastMTime  ||
      atime > this->LastSelectionMTime)
325
  {
326
    this->DataObjectToTable->Update();
327
    this->ApplyColors->Update();
328
    this->TableAdapter->SetVTKDataObject(nullptr);
329
    this->TableAdapter->SetVTKDataObject(this->ApplyColors->GetOutput());
330

331 332
    this->TableAdapter->SetColorColumnName("vtkApplyColors color");
    this->TableAdapter->SetIconIndexColumnName(this->IconIndexArrayNameInternal);
333 334

    if (atime > this->LastSelectionMTime)
335
    {
336
      this->SetVTKSelection();
337
    }
338 339 340 341 342 343

    this->ListView->setModelColumn(this->VisibleColumn);

    this->LastSelectionMTime = atime;
    this->LastInputMTime = d->GetMTime();
    this->LastMTime = this->GetMTime();
344
  }
345

346
  this->ListView->update();
347 348
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
void vtkQtListView::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());
}

365 366 367 368
//----------------------------------------------------------------------------
void vtkQtListView::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
369 370

  os << indent << "ApplyRowColors: " << (this->ApplyRowColors ? "true" : "false") << endl;
371 372
}