vtkQtTableRepresentation.cxx 8.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkQtTableRepresentation.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 (c) Sandia Corporation
 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/

#include "vtkQtTableRepresentation.h"
#include "vtkQtTableModelAdapter.h"

23 24 25 26 27 28 29 30
#include "vtkAlgorithm.h"
#include "vtkAlgorithmOutput.h"
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkLookupTable.h"
#include "vtkObjectFactory.h"
#include "vtkTable.h"
31 32 33 34

#include <QModelIndex>
#include <QColor>

35
#include <cassert>
36

37
// ----------------------------------------------------------------------
38 39

// ----------------------------------------------------------------------
40
vtkCxxSetObjectMacro(vtkQtTableRepresentation, ColorTable, vtkLookupTable);
41

42
// ----------------------------------------------------------------------
43 44
vtkQtTableRepresentation::vtkQtTableRepresentation()
{
45
  this->ModelAdapter = new vtkQtTableModelAdapter;
46 47 48 49 50 51 52 53 54 55 56 57 58

  this->ColorTable = vtkLookupTable::New();
  this->ColorTable->Register(this);
  this->ColorTable->Delete();

  this->ColorTable->SetHueRange(0.0, 1.0);
  this->ColorTable->SetRange(0.0, 1.0);
  this->ColorTable->Build();

  this->SeriesColors = vtkDoubleArray::New();
  this->SeriesColors->SetNumberOfComponents(4);
  this->SeriesColors->Register(this);
  this->SeriesColors->Delete();
59

60 61 62
  this->KeyColumnInternal = nullptr;
  this->FirstDataColumn = nullptr;
  this->LastDataColumn = nullptr;
63 64 65 66 67 68
}

// ----------------------------------------------------------------------

vtkQtTableRepresentation::~vtkQtTableRepresentation()
{
69
  delete this->ModelAdapter;
70 71
  this->ColorTable->UnRegister(this);
  this->SeriesColors->UnRegister(this);
72 73 74
  this->SetKeyColumnInternal(nullptr);
  this->SetFirstDataColumn(nullptr);
  this->SetLastDataColumn(nullptr);
75 76 77 78
}

// ----------------------------------------------------------------------

79 80 81 82
int
vtkQtTableRepresentation::RequestData(vtkInformation*,
                                      vtkInformationVector**,
                                      vtkInformationVector*)
83
{
84
  this->UpdateTable();
85
  return 1;
86 87 88 89 90 91 92
}

// ----------------------------------------------------------------------

void
vtkQtTableRepresentation::SetKeyColumn(const char *col)
{
93
  if((!col && !this->KeyColumnInternal) ||
94
      (this->KeyColumnInternal && col && strcmp(this->KeyColumnInternal, col) == 0))
95
  {
96
    return;
97
  }
98 99 100

  this->SetKeyColumnInternal(col);
  this->ModelAdapter->SetKeyColumn(-1);
101
  this->Modified();
102 103 104 105
  // We don't call Update(), representations should not call Update() on
  // themselves when their ivars are changed. It's almost like a vtkAlgorithm
  // calling Update() on itself when an ivar change which is not recommended.
  //this->Update();
106 107
}

108 109 110 111 112 113 114
// ----------------------------------------------------------------------

char* vtkQtTableRepresentation::GetKeyColumn()
{
  return this->GetKeyColumnInternal();
}

115
// ----------------------------------------------------------------------
116
void vtkQtTableRepresentation::UpdateTable()
117 118 119
{
  this->ResetModel();

120
  if (!this->GetInput())
121
  {
122
    return;
123
  }
124

125
  vtkTable *table = vtkTable::SafeDownCast(this->GetInput());
126
  if (!table)
127
  {
128
    vtkErrorMacro(<<"vtkQtTableRepresentation: I need a vtkTable as input.  You supplied a " << this->GetInput()->GetClassName() << ".");
129
    return;
130
  }
131

132 133
  // Set first/last data column names if they
  // have not already been set.
134 135 136
  const char* firstDataColumn = this->FirstDataColumn;
  const char* lastDataColumn = this->LastDataColumn;
  if (!firstDataColumn)
137
  {
138
    firstDataColumn = table->GetColumnName(0);
139
  }
140
  if (!lastDataColumn)
141
  {
142
    lastDataColumn = table->GetColumnName(table->GetNumberOfColumns()-1);
143
  }
144

145 146 147 148
  // Now that we're sure of having data, put it into a Qt model
  // adapter that we can push into the QListView.  Before we hand that
  // off, though, we'll need to come up with colors for
  // each series.
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
149
  //int keyColumnIndex = -1;
150 151
  int firstDataColumnIndex = -1;
  int lastDataColumnIndex = -1;
152
  //if (this->KeyColumnInternal != nullptr)
153 154 155 156 157 158 159 160 161 162 163
  //  {
  //  table->GetRowData()->GetAbstractArray(this->KeyColumnInternal, keyColumnIndex);
  //  if (keyColumnIndex >= 0)
  //    {
  //    this->ModelAdapter->SetKeyColumn(keyColumnIndex);
  //    }
  //  else
  //    {
  //    // Either the user didn't specify a key column or else it wasn't
  //    // found.  We'll do the best we can.
  //    vtkWarningMacro(<<"vtkQtTableRepresentation: Key column "
164
  //                    << (this->KeyColumnInternal ? this->KeyColumnInternal : "(nullptr)")
165 166 167 168
  //                    << " not found.  Defaulting to column 0.");
  //    this->ModelAdapter->SetKeyColumn(0);
  //    }
  //  }
169
  if (firstDataColumn != nullptr)
170
  {
171 172
    table->GetRowData()->GetAbstractArray(firstDataColumn,
                                          firstDataColumnIndex);
173
  }
174
  if (lastDataColumn != nullptr)
175
  {
176 177
    table->GetRowData()->GetAbstractArray(lastDataColumn,
                                          lastDataColumnIndex);
178
  }
179 180 181 182 183 184 185
  this->ModelAdapter->SetDataColumnRange(firstDataColumnIndex, lastDataColumnIndex);

  // The view will try to do this when we add the representation, but
  // we need the model to be populated before that so we'll just do it
  // here.

  this->ModelAdapter->SetVTKDataObject(table);
186
  if (this->KeyColumnInternal != nullptr)
187
  {
188
    this->ModelAdapter->SetKeyColumnName(this->KeyColumnInternal);
189
  }
190 191 192 193 194 195 196 197 198 199
  this->CreateSeriesColors();
}

// ----------------------------------------------------------------------

void
vtkQtTableRepresentation::ResetModel()
{
  this->SetModelType();
  if (this->ModelAdapter)
200
  {
201 202 203 204
    // FIXME
    // Need to alert the model of potential changes to the vtkTable
    // in different way than disconnecting/reconnecting the vtkTable from
    // the model adapter
205
    //this->ModelAdapter->SetVTKDataObject(nullptr);
206
  }
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  this->SeriesColors->Reset();
  this->SeriesColors->SetNumberOfComponents(4);
}

// ----------------------------------------------------------------------

void
vtkQtTableRepresentation::CreateSeriesColors()
{
  this->SeriesColors->Reset();
  this->SeriesColors->SetNumberOfComponents(4);

  int size = this->ModelAdapter->rowCount(QModelIndex());

  this->SeriesColors->SetNumberOfTuples(size);

  for (int i = 0; i < size; ++i)
224
  {
225 226
    double seriesValue = 1;
    if (size > 1)
227
    {
228
      seriesValue = static_cast<double>(i) / (size-1);
229
    }
230 231
    QColor c;
    if (this->ColorTable)
232
    {
233 234 235 236 237
      double rgb[3];
      double opacity;
      this->ColorTable->GetColor(seriesValue, rgb);
      opacity = this->ColorTable->GetOpacity(seriesValue);
      c.setRgbF(rgb[0], rgb[1], rgb[2], opacity);
238
    }
239
    else
240
    {
241
      c.setHsvF(seriesValue, 1, 0.7);
242
    }
243 244 245 246 247

    this->SeriesColors->SetComponent(i, 0, c.redF());
    this->SeriesColors->SetComponent(i, 1, c.greenF());
    this->SeriesColors->SetComponent(i, 2, c.blueF());
    this->SeriesColors->SetComponent(i, 3, c.alphaF());
248
  }
249 250 251 252 253 254 255 256 257 258
}


// ----------------------------------------------------------------------

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

259
  os << indent << "First data column: "
260
     << (this->FirstDataColumn ? this->FirstDataColumn : "(nullptr)")
261
     << "\n";
262 263

  os << indent << "Last data column: "
264
     << (this->LastDataColumn ? this->LastDataColumn : "(nullptr)")
265 266
     << "\n";

267
  os << indent << "Key column: "
268
     << (this->KeyColumnInternal ? this->KeyColumnInternal : "(nullptr)")
269 270 271 272 273 274 275 276 277 278 279
     << "\n";

  os << indent << "Model adapter: Qt object " << this->ModelAdapter
     << "\n";

  os << indent << "Color creation table: ";
  this->ColorTable->PrintSelf(os, indent.GetNextIndent());

  os << indent << "Series color table: ";
  this->SeriesColors->PrintSelf(os, indent.GetNextIndent());
}