File: vtkSMProxySelectionModel.cxx

package info (click to toggle)
paraview 5.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 221,108 kB
  • ctags: 236,092
  • sloc: cpp: 2,416,026; ansic: 190,891; python: 99,856; xml: 81,001; tcl: 46,915; yacc: 5,039; java: 4,413; perl: 3,108; sh: 1,974; lex: 1,926; f90: 748; asm: 471; pascal: 228; makefile: 198; objc: 83; fortran: 31
file content (472 lines) | stat: -rw-r--r-- 15,004 bytes parent folder | download | duplicates (2)
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*=========================================================================

  Program:   ParaView
  Module:    vtkSMProxySelectionModel.cxx

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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.

=========================================================================*/
#include "vtkSMProxySelectionModel.h"

#include "vtkBoundingBox.h"
#include "vtkCollection.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkPVDataInformation.h"
#include "vtkPVSession.h"
#include "vtkSmartPointer.h"
#include "vtkSMCollaborationManager.h"
#include "vtkSMMessage.h"
#include "vtkSMOutputPort.h"
#include "vtkSMProxy.h"
#include "vtkSMProxyLocator.h"
#include "vtkSMSession.h"
#include "vtkSMSourceProxy.h"
#include "vtkSMTrace.h"

#include <vector>
#include <set>
#include <vtkNew.h>

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkSMProxySelectionModel);

//-----------------------------------------------------------------------------
class vtkSMProxySelectionModel::vtkInternal
{
public:
  unsigned int CollaborationManagerObserverID;
  vtkSMProxySelectionModel* Owner;
  bool FollowinMaster;
  bool Initilized;
  bool DisableSessionStatePush;
  std::map<int, vtkSMMessage> ClientsCachedState;

  vtkInternal(vtkSMProxySelectionModel* owner)
    {
    this->DisableSessionStatePush = false;
    this->Owner = owner;
    this->CollaborationManagerObserverID = 0;
    this->FollowinMaster = true;
    this->Initilized = false;
    }

  ~vtkInternal()
    {
    if(this->Owner->Session && this->CollaborationManagerObserverID)
      {
      this->Owner->Session->GetCollaborationManager()->RemoveObserver(
          this->CollaborationManagerObserverID);
      }
    this->CollaborationManagerObserverID = 0;
    }

  void MasterChangedCallBack( vtkObject* vtkNotUsed(src),
                              unsigned long vtkNotUsed(event),
                              void* vtkNotUsed(data))
    {
    if(this->FollowinMaster && this->GetMasterId() != -1 &&
       this->ClientsCachedState.find(
           this->GetMasterId()) != this->ClientsCachedState.end())
      {
      this->Owner->LoadState( &this->ClientsCachedState[this->GetMasterId()],
                              this->Owner->Session->GetProxyLocator());
      this->Owner->PushStateToSession();
      }
    }

  void ExportSelection(vtkCollection* src, vtkCollection* dst)
    {
    dst->RemoveAllItems();
    src->InitTraversal();
    while (vtkObject* obj = src->GetNextItemAsObject())
      {
      dst->AddItem(obj);
      }
    }

  int GetMasterId()
    {
    if(!this->Owner->Session || !this->Owner->Session->GetCollaborationManager())
      {
      return -1;
      }
    return this->Owner->Session->GetCollaborationManager()->GetMasterId();
    }
};

//-----------------------------------------------------------------------------
vtkSMProxySelectionModel::vtkSMProxySelectionModel()
{
  this->Internal = new vtkSMProxySelectionModel::vtkInternal(this);

  this->State = new vtkSMMessage();
  this->SetLocation(vtkPVSession::CLIENT);
  this->State->SetExtension(DefinitionHeader::server_class, "vtkSIObject"); // Dummy SIObject
}

//-----------------------------------------------------------------------------
vtkSMProxySelectionModel::~vtkSMProxySelectionModel()
{
  delete this->Internal;
  delete this->State;
}

//-----------------------------------------------------------------------------
vtkSMProxy* vtkSMProxySelectionModel::GetCurrentProxy()
{
  return this->Current;
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::SetCurrentProxy(vtkSMProxy*  proxy,  int  command)
{ 
  if (this->Current != proxy)
    {
    SM_SCOPED_TRACE(SetCurrentProxy)
      .arg("selmodel", this)
      .arg("proxy", proxy)
      .arg("command", command);
    this->Internal->DisableSessionStatePush = true;
    this->Current = proxy;
    this->Select(proxy, command);
    this->Internal->DisableSessionStatePush = false;
    this->InvokeCurrentChanged(proxy);
    }
}

//-----------------------------------------------------------------------------
bool vtkSMProxySelectionModel::IsSelected(vtkSMProxy*  proxy)
{
  return this->Selection.find(proxy) != this->Selection.end();
}

//-----------------------------------------------------------------------------
unsigned int vtkSMProxySelectionModel::GetNumberOfSelectedProxies()
{
  return static_cast<unsigned int>(this->Selection.size());
}

//-----------------------------------------------------------------------------
vtkSMProxy* vtkSMProxySelectionModel::GetSelectedProxy(unsigned int idx)
{
  if (idx < this->GetNumberOfSelectedProxies())
    {
    SelectionType::iterator iter = this->Selection.begin();
    for (unsigned int cc=0; cc < idx; ++cc, ++iter)
      {
      }
    return vtkSMProxy::SafeDownCast(iter->GetPointer());
    }

  return NULL;
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::Select(vtkSMProxy* proxy, int command)
{
  SelectionType selection;
  if (proxy)
    {
    selection.insert(proxy);
    }
  this->Select(selection, command);
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::Select(
  const vtkSMProxySelectionModel::SelectionType& proxies, int command)
{ 
  if (command == vtkSMProxySelectionModel::NO_UPDATE)
    {
    return;
    }

  SelectionType new_selection;

  if (command & vtkSMProxySelectionModel::CLEAR)
    {
    // everything from old-selection needs to be removed. 
    }
  else
    {
    // start with existing selection.
    new_selection = this->Selection;
    }

  for (SelectionType::const_iterator iter = proxies.begin();
    iter != proxies.end(); ++iter)
    {
    vtkSMProxy* proxy = iter->GetPointer();
    if (proxy && (command & vtkSMProxySelectionModel::SELECT) !=0)
      {
      new_selection.insert(proxy);
      }
    if (proxy && (command & vtkSMProxySelectionModel::DESELECT) !=0)
      {
      new_selection.erase(proxy);
      }
    }

  bool changed = (this->Selection != new_selection);
  if (changed)
    {
    this->Selection = new_selection;
    this->InvokeSelectionChanged();
    }
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::InvokeCurrentChanged(vtkSMProxy*  proxy)
{
  this->InvokeEvent(vtkCommand::CurrentChangedEvent, proxy);
  this->PushStateToSession();
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::InvokeSelectionChanged()
{
  this->InvokeEvent(vtkCommand::SelectionChangedEvent);
  this->PushStateToSession();
}

//-----------------------------------------------------------------------------
bool vtkSMProxySelectionModel::GetSelectionDataBounds(double bounds[6])
{
  vtkBoundingBox bbox;
  for (SelectionType::iterator iter = this->Selection.begin();
    iter != this->Selection.end(); ++iter)
    {
    vtkSMProxy* proxy = iter->GetPointer();
    vtkSMSourceProxy* source = vtkSMSourceProxy::SafeDownCast(proxy);
    vtkSMOutputPort* opPort = vtkSMOutputPort::SafeDownCast(proxy);
    if (source)
      {
      for (unsigned int kk=0; kk <  source->GetNumberOfOutputPorts(); kk++)
        {
        bbox.AddBounds(source->GetDataInformation(kk)->GetBounds());
        }
      }
    else if (opPort)
      {
      bbox.AddBounds(opPort->GetDataInformation()->GetBounds());
      }
    }
  if (bbox.IsValid())
    {
    bbox.GetBounds(bounds);
    return true;
    }

  return false;
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::PrintSelf(ostream&  os,  vtkIndent  indent)
{
  this->Superclass::PrintSelf(os, indent);
os << indent << "Current Proxy: "
   << (this->Current ? this->Current->GetGlobalIDAsString() : "NULL") << endl;
  os << indent << "Selected Proxies: ";
  for( SelectionType::iterator iter = this->Selection.begin();
       iter != this->Selection.end(); iter++)
    {
    os << iter->GetPointer()->GetGlobalIDAsString() << " ";
    }
  os << endl;
}

//-----------------------------------------------------------------------------
const vtkSMMessage* vtkSMProxySelectionModel::GetFullState()
{
  return this->State;
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::LoadState( const vtkSMMessage* msg, vtkSMProxyLocator* locator)
{
  // Store in cache the state
  this->Internal->ClientsCachedState[msg->client_id()] = *msg;

  if(!this->HasGlobalID())
    {
    this->SetGlobalID(msg->global_id());
    }

  // Make sure we are loading the master state and we want to follow it.
  // Otherwise don't try to load that state
  // If we did not get initialized yet, we don't filter
  if( this->Internal->GetMasterId() != -1 &&
      !( !this->Internal->Initilized ||
         (this->IsFollowingMaster() &&
          this->Internal->GetMasterId() == static_cast<int>(msg->client_id()))))
    {
    return;
    }

  // Has we are going to load a state, we can consider to be initialized
  this->Internal->Initilized = true;

  // Load current proxy
  vtkSMProxy* currentProxy = NULL;
  if( msg->GetExtension(ProxySelectionModelState::current_proxy) != 0)
    {
    vtkTypeUInt32 currentProxyId =
        msg->GetExtension(ProxySelectionModelState::current_proxy);
    currentProxy = locator->LocateProxy(currentProxyId);
    if(currentProxy)
      {
      if(msg->GetExtension(ProxySelectionModelState::current_port) != -1)
        {
        // We have to select an output port
        vtkSMSourceProxy* source = vtkSMSourceProxy::SafeDownCast(currentProxy);
        assert("Try to select an output port of a non source proxy" && source);

        currentProxy =
            source->GetOutputPort(
              msg->GetExtension(ProxySelectionModelState::current_port));
        }
      }
    else
      {
      // Switch to warning as the selected proxy could be simply removed by
      // master when we finally decided to select it.
      vtkWarningMacro(
            "Did not find the CURRENT proxy for selection Model with ID: "
            << currentProxyId);
      }
    }

  SelectionType new_selection;

  // Load the proxy in the state
  for(int i=0; i < msg->ExtensionSize(ProxySelectionModelState::proxy); i++)
    {
    vtkSMProxy* proxy =
        locator->LocateProxy(msg->GetExtension(ProxySelectionModelState::proxy, i));
    if(proxy)
      {
      if(msg->GetExtension(ProxySelectionModelState::port, i) != -1)
        {
        // We have to select an output port
        vtkSMSourceProxy* source = vtkSMSourceProxy::SafeDownCast(proxy);
        assert("Try to select an output port of a non source proxy" && source);

        proxy = source->GetOutputPort(msg->GetExtension(ProxySelectionModelState::port, i));
        }

      // Just add the proxy in the set
      new_selection.insert(proxy);
      }
    }

  // Apply the state
  bool tmp = this->IsLocalPushOnly();
  this->EnableLocalPushOnly();
  this->Select(new_selection, CLEAR_AND_SELECT);
  this->SetCurrentProxy(currentProxy, new_selection.size() == 0 ? SELECT : NO_UPDATE);
  if(!tmp) this->DisableLocalPushOnly();
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::PushStateToSession()
{
  if(this->Internal->DisableSessionStatePush)
    {
    return;
    }

  // Update the local state and push to the session
  this->State->ClearExtension(ProxySelectionModelState::current_proxy);
  this->State->ClearExtension(ProxySelectionModelState::current_port);
  this->State->ClearExtension(ProxySelectionModelState::proxy);
  this->State->ClearExtension(ProxySelectionModelState::port);

  // - Selection
  for ( SelectionType::iterator iter = this->Selection.begin();
        iter != this->Selection.end(); ++iter)
    {
    vtkSMProxy* obj = iter->GetPointer();
    if (vtkSMOutputPort* port = vtkSMOutputPort::SafeDownCast(obj))
      {
      this->State->AddExtension( ProxySelectionModelState::proxy,
                                 port->GetSourceProxy()->GetGlobalID());
      this->State->AddExtension( ProxySelectionModelState::port,
                                 port->GetPortIndex());
      }
    else
      {
      this->State->AddExtension(ProxySelectionModelState::proxy, obj->GetGlobalID());
      this->State->AddExtension(ProxySelectionModelState::port, -1); // Not an outputport
      }
    }
  // - Current
  if ( this->Current )
    {
    if (vtkSMOutputPort* port = vtkSMOutputPort::SafeDownCast(this->Current.GetPointer()))
      {
      this->State->SetExtension( ProxySelectionModelState::current_proxy,
                                 port->GetSourceProxy()->GetGlobalID());
      this->State->SetExtension( ProxySelectionModelState::current_port,
                                 port->GetPortIndex());
      }
    else
      {
      this->State->SetExtension( ProxySelectionModelState::current_proxy,
                                 this->Current->GetGlobalID());
      this->State->SetExtension( ProxySelectionModelState::current_port,
                                 -1); // Not an outputport
      }
    }

  // If we push our state we are correctly initialized
  this->Internal->Initilized = true;

  if(!this->IsLocalPushOnly() && this->GetSession())
    {
    this->PushState(this->State);
    }
}
//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::SetSession(vtkSMSession* session)
{
  // Unregister observer if meaningful
  if(this->Session && this->Internal->CollaborationManagerObserverID)
    {
    this->Session->GetCollaborationManager()->RemoveObserver(
        this->Internal->CollaborationManagerObserverID);
    this->Internal->CollaborationManagerObserverID = 0;
    }

  this->Superclass::SetSession(session);

  if(this->Session && this->Session->GetCollaborationManager())
    {
    this->Internal->CollaborationManagerObserverID =
        this->Session->GetCollaborationManager()->AddObserver(
            vtkSMCollaborationManager::UpdateMasterUser, this->Internal,
            &vtkSMProxySelectionModel::vtkInternal::MasterChangedCallBack);
    }
}

//-----------------------------------------------------------------------------
void vtkSMProxySelectionModel::SetFollowingMaster(bool following)
{
  this->Internal->FollowinMaster = following;
  if(following)
    {
    this->Internal->MasterChangedCallBack(0,0,0);
    }
}

//-----------------------------------------------------------------------------
bool vtkSMProxySelectionModel::IsFollowingMaster()
{
  return this->Internal->FollowinMaster;
}