vtkQtListView.cxx 6.92 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 25
#include <QListView>

26 27 28 29 30 31
#include "vtkAlgorithm.h"
#include "vtkAlgorithmOutput.h"
#include "vtkConvertSelection.h"
#include "vtkDataRepresentation.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
32
#include "vtkObjectFactory.h"
33 34 35 36 37 38
#include "vtkQtTableModelAdapter.h"
#include "vtkSelection.h"
#include "vtkSelectionLink.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkTable.h"
39

Jeff Baumes's avatar
Jeff Baumes committed
40
vtkCxxRevisionMacro(vtkQtListView, "1.5");
41 42
vtkStandardNewMacro(vtkQtListView);

43

44 45 46
//----------------------------------------------------------------------------
vtkQtListView::vtkQtListView()
{
47 48 49
  this->ListView = new QListView();
  this->ListAdapter = new vtkQtTableModelAdapter();
  this->ListView->setModel(this->ListAdapter);
Jeff Baumes's avatar
Jeff Baumes committed
50 51
  this->ListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
  this->ListView->setSelectionBehavior(QAbstractItemView::SelectRows);
52 53 54 55 56
  this->Selecting = false;

  QObject::connect(this->ListView->selectionModel(), 
      SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
      this, 
Jeff Baumes's avatar
Jeff Baumes committed
57
      SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
58 59 60 61 62
}

//----------------------------------------------------------------------------
vtkQtListView::~vtkQtListView()
{
63
  if(this->ListView)
64
    {
65
    delete this->ListView;
66
    }
67 68 69 70 71 72 73 74 75 76
  delete this->ListAdapter;
}

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

//----------------------------------------------------------------------------
Jeff Baumes's avatar
Jeff Baumes committed
77
void vtkQtListView::SetAlternatingRowColors(bool state)
78
{
Jeff Baumes's avatar
Jeff Baumes committed
79
  this->ListView->setAlternatingRowColors(state);
80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

//----------------------------------------------------------------------------
void vtkQtListView::AddInputConnection( int vtkNotUsed(port), int vtkNotUsed(index),
  vtkAlgorithmOutput* conn, vtkAlgorithmOutput* vtkNotUsed(selectionConn))
{
  // Get a handle to the input data object. Note: For now
  // we are enforcing that the input data is a List.
  conn->GetProducer()->Update();
  vtkDataObject *d = conn->GetProducer()->GetOutputDataObject(0);
  vtkTable *table = vtkTable::SafeDownCast(d);

  // Enforce input
  if (!table)
94
    {
95 96
    vtkErrorMacro("vtkQtERMView requires a vtkList as input (for now)");
    return;
97
    }
98 99 100 101 102 103 104

  // Give the data object to the Qt List Adapters
  this->ListAdapter->SetVTKDataObject(table);

  // Now set the Qt Adapters (qt models) on the views
  this->ListView->update();

105 106 107
}

//----------------------------------------------------------------------------
108 109
void vtkQtListView::RemoveInputConnection(int vtkNotUsed(port), int vtkNotUsed(index),
  vtkAlgorithmOutput* conn, vtkAlgorithmOutput* vtkNotUsed(selectionConn))
110
{
111 112 113 114
  // Remove VTK data from the adapter
  conn->GetProducer()->Update();
  vtkDataObject *d = conn->GetProducer()->GetOutputDataObject(0);
  if (this->ListAdapter->GetVTKDataObject() == d)
115
    {
116 117
    this->ListAdapter->SetVTKDataObject(0);
    this->ListView->update();
118
    }
119
}
120

121
//----------------------------------------------------------------------------
Jeff Baumes's avatar
Jeff Baumes committed
122
void vtkQtListView::slotQtSelectionChanged(const QItemSelection& vtkNotUsed(s1), const QItemSelection& vtkNotUsed(s2))
123 124
{  
  this->Selecting = true;
Jeff Baumes's avatar
Jeff Baumes committed
125 126 127 128
 
  // Convert from a QModelIndexList to an index based vtkSelection
  const QModelIndexList qmil = this->ListView->selectionModel()->selectedRows();
  vtkSelection *VTKIndexSelectList = this->ListAdapter->QModelIndexListToVTKIndexSelection(qmil);
129 130 131 132 133

  // Convert to the correct type of selection
  vtkDataObject* data = this->ListAdapter->GetVTKDataObject();
  vtkSmartPointer<vtkSelection> converted;
  converted.TakeReference(vtkConvertSelection::ToSelectionType(
Jeff Baumes's avatar
Jeff Baumes committed
134
    VTKIndexSelectList, data, this->SelectionType, this->SelectionArrayNames));
135 136 137
   
  // Call select on the representation
  this->GetRepresentation()->Select(this, converted);
138
  
139
  this->Selecting = false;
140 141
}

Jeff Baumes's avatar
Jeff Baumes committed
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
//----------------------------------------------------------------------------
void vtkQtListView::SetVTKSelection()
{
  // Make the selection current
  if (this->Selecting)
    {
    // If we initiated the selection, do nothing.
    return;
    }

  // See if the selection has changed in any way
  vtkDataRepresentation* rep = this->GetRepresentation();
  vtkDataObject *d = this->ListAdapter->GetVTKDataObject();
  vtkSelection* s = rep->GetSelectionLink()->GetSelection();
  if (s->GetMTime() != this->CurrentSelectionMTime)
    {
    this->CurrentSelectionMTime = s->GetMTime();
    
    vtkSmartPointer<vtkSelection> selection;
    selection.TakeReference(vtkConvertSelection::ToIndexSelection(s, d));
    
    QItemSelection qisList = this->ListAdapter->
      VTKIndexSelectionToQItemSelection(selection);
      
    // Here we want the qt model to have it's selection changed
    // but we don't want to emit the selection.
    QObject::disconnect(this->ListView->selectionModel(), 
      SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
      this, SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
      
    this->ListView->selectionModel()->select(qisList, 
      QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
      
    QObject::connect(this->ListView->selectionModel(), 
     SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),
     this, SLOT(slotQtSelectionChanged(const QItemSelection&,const QItemSelection&)));
    }
}

181
//----------------------------------------------------------------------------
182
void vtkQtListView::Update()
183
{
184 185 186 187 188 189 190 191 192 193 194 195
  vtkDataRepresentation* rep = this->GetRepresentation();
  if (!rep)
    {
    return;
    }

  // Make the data current
  vtkAlgorithm* alg = rep->GetInputConnection()->GetProducer();
  alg->Update();
  vtkDataObject *d = alg->GetOutputDataObject(0);
  this->ListAdapter->SetVTKDataObject(d);
  
Jeff Baumes's avatar
Jeff Baumes committed
196 197
  // Update the VTK selection
  this->SetVTKSelection();
198
  
199
  this->ListView->update();
200 201 202 203 204 205 206 207
}

//----------------------------------------------------------------------------
void vtkQtListView::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}