pqContextView.cxx 9.09 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
23
24
25
26
27
28
29
30
31
32
33
/*=========================================================================

   Program: ParaView
   Module:    pqContextView.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 "pqContextView.h"

34
#include "pqDataRepresentation.h"
35
36
37
38
#include "pqEventDispatcher.h"
#include "pqImageUtil.h"
#include "pqOutputPort.h"
#include "pqPipelineSource.h"
39
#include "pqQVTKWidget.h"
40
#include "pqSMAdaptor.h"
41
#include "pqServer.h"
42
#include "pqUndoStack.h"
43
44
45
46
#include "vtkAnnotationLink.h"
#include "vtkChartXY.h"
#include "vtkCommand.h"
#include "vtkContextView.h"
47
#include "vtkErrorCode.h"
48
49
#include "vtkEventQtSlotConnect.h"
#include "vtkIdTypeArray.h"
50
#include "vtkImageData.h"
51
#include "vtkNew.h"
52
53
#include "vtkPVDataInformation.h"
#include "vtkPVXMLElement.h"
54
#include "vtkProcessModule.h"
55
#include "vtkRenderWindow.h"
56
#include "vtkSMContextViewProxy.h"
57
#include "vtkSMPropertyHelper.h"
58
59
#include "vtkSMSelectionHelper.h"
#include "vtkSMSourceProxy.h"
60
#include "vtkSMTrace.h"
61
62
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
63
#include "vtkVariant.h"
64
#include <vtksys/SystemTools.hxx>
65

66
#include <QDebug>
67
#include <QList>
68
#include <QPointer>
69
#include <QVariant>
70

71
72
#include <cassert>

73
74
75
76
// Command implementation
class pqContextView::command : public vtkCommand
{
public:
Kitware Robot's avatar
Kitware Robot committed
77
  static command* New(pqContextView& view) { return new command(view); }
78

Kitware Robot's avatar
Kitware Robot committed
79
80
  command(pqContextView& view)
    : Target(view)
81
82
83
  {
  }

Ben Boeckel's avatar
Ben Boeckel committed
84
  void Execute(vtkObject*, unsigned long, void*) override { Target.selectionChanged(); }
Kitware Robot's avatar
Kitware Robot committed
85

86
  pqContextView& Target;
87
88
89

private:
  void operator=(const pqContextView::command&);
90
91
};

92
93
94
95
class pqContextView::pqInternal
{
public:
  bool InitializedAfterObjectsCreated;
96
  int SelectionAction;
97
98

  pqInternal()
Kitware Robot's avatar
Kitware Robot committed
99
100
  {
    this->InitializedAfterObjectsCreated = false;
101
    this->SelectionAction = vtkChart::SELECT_RECTANGLE;
Kitware Robot's avatar
Kitware Robot committed
102
  }
103
  ~pqInternal() = default;
104
105

  vtkNew<vtkEventQtSlotConnect> VTKConnect;
106
107
};

108
//-----------------------------------------------------------------------------
Kitware Robot's avatar
Kitware Robot committed
109
110
111
pqContextView::pqContextView(const QString& type, const QString& group, const QString& name,
  vtkSMViewProxy* viewProxy, pqServer* server, QObject* parentObject)
  : Superclass(type, group, name, viewProxy, server, parentObject)
112
{
113
  this->Internal = new pqContextView::pqInternal();
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
114
  viewProxy->UpdateVTKObjects(); // this results in calling CreateVTKObjects().
115
  this->Command = command::New(*this);
Kitware Robot's avatar
Kitware Robot committed
116
117
  vtkObject::SafeDownCast(viewProxy->GetClientSideObject())
    ->AddObserver(vtkCommand::SelectionChangedEvent, this->Command);
118
119
120
121
122

  this->Internal->VTKConnect->Connect(
    viewProxy, vtkCommand::StartInteractionEvent, this, SLOT(startInteraction()));
  this->Internal->VTKConnect->Connect(
    viewProxy, vtkCommand::EndInteractionEvent, this, SLOT(endInteraction()));
123
124
125
126
127
}

//-----------------------------------------------------------------------------
pqContextView::~pqContextView()
{
128
  this->Command->Delete();
129
130
131
132
133
134
  delete this->Internal;
}

//-----------------------------------------------------------------------------
QWidget* pqContextView::createWidget()
{
135
  vtkSMContextViewProxy* proxy = this->getContextViewProxy();
136
  assert(proxy);
137
138
139
140
141
142
143
144

  // Enable multisample for chart views when not running tests. Multisamples
  // is disabled for testing to avoid failures due to antialiasing
  // differences.
  bool use_multisampling = (!vtksys::SystemTools::HasEnv("DASHBOARD_TEST_FROM_CTEST"));
  auto renWin = proxy->GetRenderWindow();
  renWin->SetMultiSamples(use_multisampling ? 8 : 0);

145
146
  pqQVTKWidget* vtkwidget = new pqQVTKWidget();
  vtkwidget->setViewProxy(this->getProxy());
147
148
  vtkwidget->setContextMenuPolicy(Qt::NoContextMenu);
  vtkwidget->installEventFilter(this);
149

150
151
  vtkwidget->setRenderWindow(proxy->GetRenderWindow());
  proxy->SetupInteractor(vtkwidget->interactor());
152
  return vtkwidget;
153
154
155
}

//-----------------------------------------------------------------------------
156
/// Returns the internal vtkChartView that provides the implementation for
157
158
159
/// the chart rendering.
vtkContextView* pqContextView::getVTKContextView() const
{
Kyle Lutz's avatar
Kyle Lutz committed
160
  return vtkSMContextViewProxy::SafeDownCast(this->getProxy())->GetContextView();
161
162
}

163
164
165
166
167
168
//-----------------------------------------------------------------------------
vtkSMContextViewProxy* pqContextView::getContextViewProxy() const
{
  return vtkSMContextViewProxy::SafeDownCast(this->getProxy());
}

169
170
171
172
173
174
//-----------------------------------------------------------------------------
bool pqContextView::supportsSelection() const
{
  return true;
}

175
176
//-----------------------------------------------------------------------------
/// Resets the zoom level to 100%.
177
void pqContextView::resetDisplay(bool vtkNotUsed(closest))
178
{
Kitware Robot's avatar
Kitware Robot committed
179
  vtkSMContextViewProxy* proxy = this->getContextViewProxy();
180
  if (proxy)
Kitware Robot's avatar
Kitware Robot committed
181
  {
182
    proxy->ResetDisplay();
183
    this->render();
Kitware Robot's avatar
Kitware Robot committed
184
  }
185
186
}

187
//-----------------------------------------------------------------------------
188
void pqContextView::selectionChanged()
189
190
{
  // Fill the selection source with the selection from the view
191
192
  vtkSelection* sel = this->getContextViewProxy()->GetCurrentSelection();
  if (sel)
Kitware Robot's avatar
Kitware Robot committed
193
  {
194
    this->setSelection(sel);
Kitware Robot's avatar
Kitware Robot committed
195
  }
196
197
}

198
//-----------------------------------------------------------------------------
199
void pqContextView::setSelection(vtkSelection* sel)
200
{
201
  // Get the representation's source
202
  pqDataRepresentation* pqRepr = nullptr;
203
204

  for (int i = 0; i < this->getNumberOfRepresentations(); ++i)
Kitware Robot's avatar
Kitware Robot committed
205
  {
206
    if (this->getRepresentation(i)->isVisible())
Kitware Robot's avatar
Kitware Robot committed
207
    {
208
209
      pqRepr = qobject_cast<pqDataRepresentation*>(this->getRepresentation(i));
    }
Kitware Robot's avatar
Kitware Robot committed
210
  }
211
212

  if (!pqRepr)
Kitware Robot's avatar
Kitware Robot committed
213
  {
214
    return;
Kitware Robot's avatar
Kitware Robot committed
215
  }
216
217

  pqOutputPort* opPort = pqRepr->getOutputPortFromInput();
Kitware Robot's avatar
Kitware Robot committed
218
219
  vtkSMSourceProxy* repSource = vtkSMSourceProxy::SafeDownCast(opPort->getSource()->getProxy());

220
  repSource->CleanSelectionInputs(opPort->getPortNumber());
221

222
  vtkSMProxy* selectionSource =
Kitware Robot's avatar
Kitware Robot committed
223
    vtkSMSelectionHelper::NewSelectionSourceFromSelection(repSource->GetSession(), sel);
224

Kitware Robot's avatar
Kitware Robot committed
225
  // If not selection has been made,
226
227
  // the selection source can be null.
  if (selectionSource)
Kitware Robot's avatar
Kitware Robot committed
228
  {
229
    // Set the selection on the representation's source
Kitware Robot's avatar
Kitware Robot committed
230
231
    repSource->SetSelectionInput(
      opPort->getPortNumber(), vtkSMSourceProxy::SafeDownCast(selectionSource), 0);
232
    selectionSource->Delete();
233

234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    // Trace the selection
    if (strcmp(selectionSource->GetXMLName(), "ThresholdSelectionSource") == 0)
    {
      SM_SCOPED_TRACE(CallFunction)
        .arg("SelectThresholds")
        .arg("Thresholds", vtkSMPropertyHelper(selectionSource, "Thresholds").GetDoubleArray())
        .arg("ArrayName", vtkSMPropertyHelper(selectionSource, "ArrayName").GetAsString())
        .arg("FieldType", vtkSMPropertyHelper(selectionSource, "FieldType").GetAsInt());
    }
    else
    {
      // Map from selection source proxy name to trace function
      std::string functionName(selectionSource->GetXMLName());
      functionName.erase(functionName.size() - sizeof("SelectionSource") + 1);
      functionName.append("s");
      functionName.insert(0, "Select");

      SM_SCOPED_TRACE(CallFunction)
        .arg(functionName.c_str())
        .arg("IDs", vtkSMPropertyHelper(selectionSource, "IDs").GetIntArray())
        .arg("FieldType", vtkSMPropertyHelper(selectionSource, "FieldType").GetAsInt())
        .arg("ContainingCells", vtkSMPropertyHelper(selectionSource, "ContainingCells").GetAsInt());
    }
257
258
  }

259
  Q_EMIT this->selected(opPort);
260
}
261

262
//-----------------------------------------------------------------------------
263
264
void pqContextView::setSelectionAction(int selAction)
{
Kitware Robot's avatar
Kitware Robot committed
265
  if (this->Internal->SelectionAction == selAction || selAction < vtkChart::SELECT ||
266
    selAction > vtkChart::SELECT_POLYGON)
Kitware Robot's avatar
Kitware Robot committed
267
  {
268
    return;
Kitware Robot's avatar
Kitware Robot committed
269
  }
270
271
272
  this->Internal->SelectionAction = selAction;
}

273
//-----------------------------------------------------------------------------
274
275
276
277
int pqContextView::selectionAction()
{
  return this->Internal->SelectionAction;
}
278
279
280
281
282
283
284
285
286
287
288
289

//-----------------------------------------------------------------------------
void pqContextView::startInteraction()
{
  BEGIN_UNDO_SET("Interaction");
}

//-----------------------------------------------------------------------------
void pqContextView::endInteraction()
{
  END_UNDO_SET();
}