File: pqCinemaTrackSelection.cxx

package info (click to toggle)
paraview 5.4.1+dfsg4-3.1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 218,616 kB
  • sloc: cpp: 2,331,508; ansic: 322,365; python: 111,051; xml: 79,203; tcl: 47,013; yacc: 4,877; java: 4,438; perl: 3,238; sh: 2,920; lex: 1,908; f90: 748; makefile: 273; pascal: 228; objc: 83; fortran: 31
file content (443 lines) | stat: -rw-r--r-- 14,518 bytes parent folder | download
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
/*=========================================================================

   Program: ParaView
   Module:  pqCinemaTrackSelection.cxx

   Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
   under the terms of the ParaView license version 1.2.

   See License_v1.2.txt for the full ParaView license.
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

========================================================================*/
#include <QDebug>
#include <QHeaderView>

#include "vtkPVArrayInformation.h"
#include "vtkPVDataInformation.h"
#include "vtkPVDataSetAttributesInformation.h"
#include "vtkSMSourceProxy.h"

#include "pqAbstractItemSelectionModel.h"
#include "pqApplicationCore.h"
#include "pqCinemaTrack.h"
#include "pqCinemaTrackSelection.h"
#include "pqOutputPort.h"
#include "pqPipelineFilter.h"
#include "pqPipelineModel.h"
#include "pqPropertyWidget.h"
#include "pqRenderView.h"
#include "pqServerManagerModel.h"
#include "pqTreeWidget.h"
#include "pqTreeWidgetCheckHelper.h"
#include "pqTreeWidgetItem.h"
#include "ui_pqCinemaTrackSelection.h"

// ============================================================================
/// @brief Concrete implementation for seleciton of proxy arrays.
class pqArraySelectionModel : public pqAbstractItemSelectionModel
{
public:
  pqArraySelectionModel(QObject* parent_ = NULL)
    : pqAbstractItemSelectionModel(parent_)
  {
    this->initializeRootItem();
  };

  ~pqArraySelectionModel(){};

  void initializeRootItem()
  {
    pqAbstractItemSelectionModel::RootItem->setData(0, Qt::DisplayRole, "Name");
    pqAbstractItemSelectionModel::RootItem->setData(1, Qt::DisplayRole, "Data Type");
  };

  void populateModel(void* proxy)
  {
    vtkSMSourceProxy* sourceProxy = static_cast<vtkSMSourceProxy*>(proxy);
    vtkPVDataInformation* dataInfo = sourceProxy->GetDataInformation();
    if (!dataInfo)
    {
      return;
    }

    QList<QTreeWidgetItem*> newItems;
    for (int align = 0; align < 2; align++)
    {
      vtkPVDataSetAttributesInformation const* attribInfo;
      if (align == 0)
      {
        attribInfo = dataInfo->GetPointDataInformation();
      }
      else
      {
        attribInfo = dataInfo->GetCellDataInformation();
      }
      if (!attribInfo)
      {
        continue;
      }

      int numArrays = attribInfo->GetNumberOfArrays();
      for (int i = 0; i < numArrays; i++)
      {
        vtkPVArrayInformation* arrInfo = attribInfo->GetArrayInformation(i);
        if (!arrInfo)
        {
          continue;
        }

        QTreeWidgetItem* item = new QTreeWidgetItem();
        item->setData(0, Qt::DisplayRole, arrInfo->GetName());
        // item->setData(0, Qt::DecorationRole, pixmaps[align]);
        QString dataType = vtkImageScalarTypeNameMacro(arrInfo->GetDataType());
        item->setData(1, Qt::DisplayRole, dataType);
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);

        // Ignore Normals arrays by default
        Qt::CheckState check =
          QString(arrInfo->GetName()) == QString("Normals") ? Qt::Unchecked : Qt::Checked;
        item->setCheckState(0, check);

        newItems.append(item);
      }
    }
    RootItem->addChildren(newItems);
  };

  QStringList getCheckedItemNames()
  {
    QStringList itemNames;
    int const numItems = this->RootItem->childCount();
    for (int i = 0; i < numItems; i++)
    {
      QModelIndex index_ = pqAbstractItemSelectionModel::index(i, 0, QModelIndex());

      if (pqAbstractItemSelectionModel::data(index_, Qt::CheckStateRole) == Qt::Unchecked)
        continue;

      QString displayedName =
        pqAbstractItemSelectionModel::data(index_, Qt::DisplayRole).toString();
      itemNames.append(displayedName);
    }

    return itemNames;
  };
};

// ============================================================================
pqCinemaTrackSelection::pqCinemaTrackSelection(QWidget* parent_)
  : QWidget(parent_)
  , Ui(new Ui::CinemaTrackSelection())
{
  this->Ui->setupUi(this);
  this->Ui->viewArrayPicker->setRootIsDecorated(false);
}

// ----------------------------------------------------------------------------
pqCinemaTrackSelection::~pqCinemaTrackSelection()
{
  delete Ui;
}

// ----------------------------------------------------------------------------
void pqCinemaTrackSelection::initializePipelineBrowser()
{
  pqServerManagerModel* smModel = pqApplicationCore::instance()->getServerManagerModel();
  pqPipelineBrowserWidget* plBrowser = this->Ui->wPipelineBrowser;
  pqPipelineModel* model = new pqPipelineModel(*smModel, plBrowser);
  plBrowser->setModel(model);
  plBrowser->expandAll();

  QItemSelectionModel* selModel = plBrowser->getSelectionModel();
  connect(selModel, SIGNAL(currentChanged(QModelIndex const&, QModelIndex const&)), this,
    SLOT(onPipelineItemChanged(QModelIndex const&, QModelIndex const&)));

  // As before, the track selection will be applied to all of the views.  If necessary,
  // the models/tracks can be stored per view to keep selections separate.
  QList<pqRenderViewBase*> views = smModel->findItems<pqRenderViewBase*>();
  if (views.isEmpty() == false)
  {
    plBrowser->setActiveView(views[0]);
  }
  this->initializePipelineItemValues(smModel->findItems<pqPipelineSource*>());
}

// ----------------------------------------------------------------------------
void pqCinemaTrackSelection::initializePipelineItemValues(QList<pqPipelineSource*> const& items)
{
  foreach (pqPipelineSource* plItem, items)
  {
    pqOutputPort const* port = plItem ? plItem->getOutputPort(0) : NULL;

    if (!port)
    {
      PV_DEBUG_PANELS() << "Failed to query outputPort from index(";
      continue;
    }

    vtkSMSourceProxy* proxy = port->getSourceProxy();
    if (!proxy)
    {
      PV_DEBUG_PANELS() << "Failed to query proxy!";
      continue;
    }

    ItemValues& values = this->PipelineItemValues[plItem];
    values.first = new pqArraySelectionModel(this);
    values.first->populateModel(proxy);
    values.second = NULL;

    // add only cinema-supported filters
    QWidget* parent_ = this->Ui->wTabValues;
    Qt::WindowFlags parentFlags = parent_->windowFlags();
    const char* className = proxy->GetVTKClassName();
    if (className &&
      (!strcmp(className, "vtkPVContourFilter") || !strcmp(className, "vtkPVMetaSliceDataSet") ||
          !strcmp(className, "vtkPVMetaClipDataSet")))
    {
      pqPipelineFilter* plFilter = static_cast<pqPipelineFilter*>(plItem);
      values.second = new pqCinemaTrack(parent_, parentFlags, plFilter);
      parent_->layout()->addWidget(values.second);
      values.second->hide();
    }
  }
}

// ----------------------------------------------------------------------------
void pqCinemaTrackSelection::onPipelineItemChanged(
  QModelIndex const& current, QModelIndex const& previous)
{
  pqPipelineSource* source = this->getPipelineSource(current);
  pqOutputPort const* port =
    source ? source->getOutputPort(0) : NULL; // qobject_cast<pqOutputPort const*>(smModelItem);

  if (!port)
  {
    PV_DEBUG_PANELS() << "Failed to query outputPort from index(" << current.row() << ", "
                      << current.column() << ")";

    // TODO create a functions which sets a null model and disables the widgets
    this->Ui->viewArrayPicker->setModel(NULL);
    this->Ui->tabProxyProperties->setEnabled(false);
    return;
  }

  // set current item's pipeline model
  ItemValuesMap::iterator valuesIt = this->PipelineItemValues.find(source);
  if (valuesIt == this->PipelineItemValues.end())
  {
    return;
  }

  this->Ui->viewArrayPicker->setModel(valuesIt->second.first);
  this->Ui->tabProxyProperties->setEnabled(true);
  QHeaderView* header = this->Ui->viewArrayPicker->header();
  header->resizeSections(QHeaderView::ResizeToContents);
  header->setStretchLastSection(true);

  // set current item's value input widget if available
  // show the current
  pqCinemaTrack* track = valuesIt->second.second;
  if (track)
  {
    track->show();
    this->Ui->tabProxyProperties->setTabEnabled(1, true);

    // array selection is disabled, so set jump to the 'controls' tab
    if (!this->Ui->tabProxyProperties->isTabEnabled(0))
    {
      this->Ui->tabProxyProperties->setCurrentIndex(1);
    }
  }
  else
  {
    this->Ui->tabProxyProperties->setCurrentIndex(0);
    this->Ui->tabProxyProperties->setTabEnabled(1, false);
  }
  // hide the previous
  pqPipelineSource* prevItem = this->getPipelineSource(previous);
  if (!prevItem)
  {
    return;
  }

  ItemValuesMap::iterator prevValuesIt = this->PipelineItemValues.find(prevItem);
  if (prevValuesIt != this->PipelineItemValues.end())
  {
    pqCinemaTrack* prevTrack = prevValuesIt->second.second;
    if (prevTrack)
    {
      prevTrack->hide();
    }
  }
}

// ----------------------------------------------------------------------------
void pqCinemaTrackSelection::enableArraySelection(bool enable)
{
  this->Ui->tabProxyProperties->setTabEnabled(0, enable);

  // force item selection changed to update the active tab
  pqPipelineBrowserWidget* plBrowser = this->Ui->wPipelineBrowser;
  QItemSelectionModel* selModel = plBrowser->getSelectionModel();
  this->onPipelineItemChanged(selModel->currentIndex(), QModelIndex());
}

// ----------------------------------------------------------------------------
pqPipelineSource* pqCinemaTrackSelection::getPipelineSource(QModelIndex const& index) const
{
  if (!index.isValid())
  {
    return NULL;
  }

  // transform the FilterModel QMIndex to its PipelineModel equivalent
  pqPipelineModel const* model = this->Ui->wPipelineBrowser->getPipelineModel(index);
  if (!model)
  {
    return NULL;
  }

  QModelIndex const plModelIndex = this->Ui->wPipelineBrowser->pipelineModelIndex(index);
  // query an item from the PipelineModel and get its vtkSMSourceProxy
  pqServerManagerModelItem* smModelItem = model->getItemFor(plModelIndex);
  pqPipelineSource* source = qobject_cast<pqPipelineSource*>(smModelItem);

  return source;
}

//-----------------------------------------------------------------------------
QList<pqCinemaTrack*> pqCinemaTrackSelection::getTracks()
{
  QList<pqCinemaTrack*> tracks;

  ItemValuesMap const& valuesMap = this->PipelineItemValues;
  for (ItemValuesMap::const_iterator it = valuesMap.begin(); it != valuesMap.end(); it++)
  {
    if (pqCinemaTrack* track = qobject_cast<pqCinemaTrack*>((*it).second.second))
    {
      // Update names as they might be out of sync (e.g. if the user renamed a filter).
      // TODO: Update the name right away when it changes with an ss connection.
      track->setFilterName(QString((*it).first->getSMName()));
      tracks.append(track);
    }
  }

  return tracks;
}

//------------------------------------------------------------------------------
QString pqCinemaTrackSelection::getTrackSelectionAsString(QString const& format)
{
  QString cinema_tracks;
  QList<pqCinemaTrack*> tracks = this->getTracks();

  for (int index = 0; index < tracks.count(); index++)
  {
    pqCinemaTrack* const& p = tracks.at(index);
    if (!p->explore())
    {
      continue;
    }

    QString name = p->filterName();
    QString values = "[";
    QVariantList vals = p->scalars();
    for (int j = 0; j < vals.count(); j++)
    {
      bool ok = true;
      QString valueStr = QString::number(vals.value(j).toDouble(&ok));
      if (!ok)
      {
        continue;
      }

      values += valueStr;
      values += ",";
    }
    values.chop(1);
    values += "]";

    // Following the smtrace.py convention, the first letter of the UI name is changed
    // to lower-case to facilitate passing these values to a CoProcessing pipeline.
    // When exporting through menu->export the actual UI name is resolved in pv_introspect.
    name = name.left(1).toLower() + name.mid(1);

    QString info = format.arg(name).arg(values);
    cinema_tracks += info;

    // append a comma except to the last element
    if (index < tracks.count() - 1)
      cinema_tracks += ", ";
  }

  return cinema_tracks;
}

// -----------------------------------------------------------------------------
QString pqCinemaTrackSelection::getArraySelectionAsString(QString const& format)
{
  bool allArraysUnchecked = true;
  QString array_selection("");
  ItemValuesMap const& valuesMap = this->PipelineItemValues;
  for (ItemValuesMap::const_iterator it = valuesMap.begin(); it != valuesMap.end(); it++)
  {
    pqArraySelectionModel* model = (*it).second.first;
    if (model)
    {
      QString itemName = QString((*it).first->getSMName());
      QStringList arrayNames = model->getCheckedItemNames();

      // append to the selection string
      if (arrayNames.count() > 0)
      {
        QString values = "[";
        for (int j = 0; j < arrayNames.count(); j++)
        {
          values += QString("'") + arrayNames.at(j) + QString("'");
          if (j < arrayNames.count() - 1)
            values += ", ";
        }
        values += "]";

        // Following the smtrace.py convention, the first letter of the UI name is changed
        // to lower-case to facilitate passing these values to a CoProcessing pipeline.
        // When exporting through menu->export the actual UI name is resolved in pv_introspect.
        itemName = itemName.left(1).toLower() + itemName.mid(1);

        QString info = format.arg(itemName).arg(values);
        array_selection += info;
        array_selection += ", ";
        allArraysUnchecked = false;
      }
    }
  }

  if (!allArraysUnchecked)
  {
    // chop off the last ", "
    array_selection.chop(2);
  }

  return array_selection;
}