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
|
/*=========================================================================
Program: ParaView
Module: vtkSMNewWidgetRepresentationProxy.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 "vtkSMNewWidgetRepresentationProxy.h"
#include "vtk3DWidgetRepresentation.h"
#include "vtkAbstractWidget.h"
#include "vtkClientServerStream.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkProcessModule.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkSMDoubleVectorProperty.h"
#include "vtkSMIntVectorProperty.h"
#include "vtkSMPropertyGroup.h"
#include "vtkSMPropertyIterator.h"
#include "vtkSMPropertyLink.h"
#include "vtkSMProxyProperty.h"
#include "vtkSMRenderViewProxy.h"
#include "vtkSMSession.h"
#include "vtkSMUncheckedPropertyHelper.h"
#include "vtkSMViewProxy.h"
#include "vtkSMWidgetRepresentationProxy.h"
#include "vtkWeakPointer.h"
#include "vtkWidgetRepresentation.h"
#include <list>
#include <cassert>
vtkStandardNewMacro(vtkSMNewWidgetRepresentationProxy);
class vtkSMNewWidgetRepresentationObserver : public vtkCommand
{
public:
static vtkSMNewWidgetRepresentationObserver *New()
{ return new vtkSMNewWidgetRepresentationObserver; }
virtual void Execute(vtkObject* caller, unsigned long event, void*)
{
if (this->Proxy)
{
if (vtkAbstractWidget::SafeDownCast(caller))
{
this->Proxy->ExecuteEvent(event);
}
else if (vtkSMProperty* prop = vtkSMProperty::SafeDownCast(caller))
{
this->Proxy->ProcessLinkedPropertyEvent(prop, event);
}
}
}
vtkSMNewWidgetRepresentationObserver():Proxy(0) {}
vtkSMNewWidgetRepresentationProxy* Proxy;
};
struct vtkSMNewWidgetRepresentationInternals
{
typedef std::list<vtkSmartPointer<vtkSMLink> > LinksType;
LinksType Links;
vtkWeakPointer<vtkSMRenderViewProxy> ViewProxy;
// Data about "controlled proxies".
vtkWeakPointer<vtkSMProxy> ControlledProxy;
vtkWeakPointer<vtkSMPropertyGroup> ControlledPropertyGroup;
};
//----------------------------------------------------------------------------
vtkSMNewWidgetRepresentationProxy::vtkSMNewWidgetRepresentationProxy()
{
this->SetLocation(vtkPVSession::CLIENT_AND_SERVERS);
this->RepresentationProxy = 0;
this->WidgetProxy = 0;
this->Widget = 0;
this->Observer = vtkSMNewWidgetRepresentationObserver::New();
this->Observer->Proxy = this;
this->Internal = new vtkSMNewWidgetRepresentationInternals;
}
//----------------------------------------------------------------------------
vtkSMNewWidgetRepresentationProxy::~vtkSMNewWidgetRepresentationProxy()
{
this->RepresentationProxy = 0;
this->WidgetProxy = 0;
this->Widget = 0;
this->Observer->Proxy = 0;
this->Observer->Delete();
if (this->Internal)
{
delete this->Internal;
}
}
//-----------------------------------------------------------------------------
void vtkSMNewWidgetRepresentationProxy::CreateVTKObjects()
{
if (this->ObjectsCreated)
{
return;
}
this->RepresentationProxy = this->GetSubProxy("Prop");
if (!this->RepresentationProxy)
{
this->RepresentationProxy = this->GetSubProxy("Prop2D");
}
if (!this->RepresentationProxy)
{
vtkErrorMacro(
"A representation proxy must be defined as a Prop (or Prop2D) sub-proxy");
return;
}
this->RepresentationProxy->SetLocation(
vtkPVSession::RENDER_SERVER | vtkPVSession::CLIENT);
this->WidgetProxy = this->GetSubProxy("Widget");
if (this->WidgetProxy)
{
this->WidgetProxy->SetLocation(vtkPVSession::CLIENT);
}
this->Superclass::CreateVTKObjects();
// Bind the Widget and the representations on the server side
vtkClientServerStream stream;
stream << vtkClientServerStream::Invoke
<< VTKOBJECT(this)
<< "SetRepresentation"
<< VTKOBJECT(this->RepresentationProxy)
<< vtkClientServerStream::End;
this->ExecuteStream(stream, false, vtkPVSession::RENDER_SERVER |
vtkPVSession::CLIENT);
// Location 0 is for prototype objects !!! No need to send to the server something.
if (!this->WidgetProxy || this->Location == 0)
{
return;
}
vtkSMProxyProperty* pp = vtkSMProxyProperty::SafeDownCast(
this->WidgetProxy->GetProperty("Representation"));
if (pp)
{
pp->AddProxy(this->RepresentationProxy);
}
this->WidgetProxy->UpdateVTKObjects();
// Get the local VTK object for that widget
this->Widget = vtkAbstractWidget::SafeDownCast(
this->WidgetProxy->GetClientSideObject());
if (this->Widget)
{
this->Widget->AddObserver(
vtkCommand::StartInteractionEvent, this->Observer);
this->Widget->AddObserver(
vtkCommand::EndInteractionEvent, this->Observer);
this->Widget->AddObserver(
vtkCommand::InteractionEvent, this->Observer);
}
vtk3DWidgetRepresentation* clientObject =
vtk3DWidgetRepresentation::SafeDownCast(this->GetClientSideObject());
clientObject->SetWidget(this->Widget);
// Since links copy values from input to output,
// we need to make sure that input properties i.e. the info
// properties are not empty.
this->UpdatePropertyInformation();
vtkSMPropertyIterator* piter = this->NewPropertyIterator();
for(piter->Begin(); !piter->IsAtEnd(); piter->Next())
{
vtkSMProperty* prop = piter->GetProperty();
vtkSMProperty* info = prop->GetInformationProperty();
if (info)
{
// This ensures that the property value from the loaded state is
// preserved, and not overwritten by the default value from
// the property information
info->Copy(prop);
vtkSMPropertyLink* link = vtkSMPropertyLink::New();
// NOTE: vtkSMPropertyLink no longer affect proxy reference. We're now
// only using vtkWeakPointer in vtkSMPropertyLink.
link->AddLinkedProperty(this,
piter->GetKey(),
vtkSMLink::OUTPUT);
link->AddLinkedProperty(this,
this->GetPropertyName(info),
vtkSMLink::INPUT);
this->Internal->Links.push_back(link);
link->Delete();
}
}
piter->Delete();
}
//-----------------------------------------------------------------------------
void vtkSMNewWidgetRepresentationProxy::ExecuteEvent(unsigned long event)
{
this->InvokeEvent(event);
vtkSMWidgetRepresentationProxy* widgetRepresentation =
vtkSMWidgetRepresentationProxy::SafeDownCast(this->RepresentationProxy);
if (event == vtkCommand::StartInteractionEvent)
{
if (vtkRenderWindowInteractor* iren = this->Widget->GetInteractor())
{
iren->InvokeEvent(event);
}
if(widgetRepresentation)
{
widgetRepresentation->OnStartInteraction();
}
}
else if (event == vtkCommand::InteractionEvent)
{
this->RepresentationProxy->UpdatePropertyInformation();
this->UpdateVTKObjects();
if (vtkRenderWindowInteractor* iren = this->Widget->GetInteractor())
{
iren->InvokeEvent(event);
}
if(widgetRepresentation)
{
widgetRepresentation->OnInteraction();
}
}
else if (event == vtkCommand::EndInteractionEvent)
{
vtkSMProperty* sizeHandles =
this->RepresentationProxy->GetProperty("SizeHandles");
if (sizeHandles)
{
sizeHandles->Modified();
this->RepresentationProxy->UpdateProperty("SizeHandles");
}
if(widgetRepresentation)
{
widgetRepresentation->OnEndInteraction();
}
if (vtkRenderWindowInteractor* iren = this->Widget->GetInteractor())
{
iren->InvokeEvent(event);
}
}
}
//----------------------------------------------------------------------------
bool vtkSMNewWidgetRepresentationProxy::LinkProperties(
vtkSMProxy* controlledProxy, vtkSMPropertyGroup* controlledPropertyGroup)
{
if (this->Internal->ControlledProxy != NULL)
{
vtkErrorMacro("Cannot `LinkProperties` with muliple proxies.");
return false;
}
this->Internal->ControlledProxy = controlledProxy;
this->Internal->ControlledPropertyGroup = controlledPropertyGroup;
for (unsigned int cc=0, max=controlledPropertyGroup->GetNumberOfProperties(); cc < max; ++cc)
{
vtkSMProperty* prop = controlledPropertyGroup->GetProperty(cc);
const char* function = controlledPropertyGroup->GetFunction(prop);
if (vtkSMProperty* widgetProperty = this->GetProperty(function))
{
prop->AddObserver(
vtkCommand::UncheckedPropertyModifiedEvent, this->Observer);
widgetProperty->AddObserver(
vtkCommand::ModifiedEvent, this->Observer);
vtkSMUncheckedPropertyHelper helper(prop);
vtkSMPropertyHelper(widgetProperty).Copy(helper);
}
}
this->UpdateVTKObjects();
return true;
}
//----------------------------------------------------------------------------
bool vtkSMNewWidgetRepresentationProxy::UnlinkProperties(vtkSMProxy* controlledProxy)
{
if (this->Internal->ControlledProxy != controlledProxy)
{
vtkErrorMacro("Cannot 'UnlinkProperties' from a non-linked proxy.");
return false;
}
vtkSMPropertyGroup* controlledPropertyGroup = this->Internal->ControlledPropertyGroup;
for (unsigned int cc=0, max=controlledPropertyGroup->GetNumberOfProperties(); cc < max; ++cc)
{
vtkSMProperty* prop = controlledPropertyGroup->GetProperty(cc);
prop->RemoveObserver(this->Observer);
}
return true;
}
//----------------------------------------------------------------------------
void vtkSMNewWidgetRepresentationProxy::ProcessLinkedPropertyEvent(
vtkSMProperty* caller, unsigned long event)
{
assert(this->Internal->ControlledPropertyGroup);
vtkSMPropertyGroup* controlledPropertyGroup = this->Internal->ControlledPropertyGroup;
if (event == vtkCommand::UncheckedPropertyModifiedEvent)
{
// Whenever a controlled property's unchecked value changes, we copy that
// value
vtkSMProperty* controlledProperty = caller;
const char* function = controlledPropertyGroup->GetFunction(controlledProperty);
if (vtkSMProperty* widgetProperty = this->GetProperty(function))
{
// Copy unchecked values from controlledProperty to the checked values of
// this widget's property.
vtkSMUncheckedPropertyHelper chelper(controlledProperty);
vtkSMPropertyHelper(widgetProperty).Copy(chelper);
this->UpdateVTKObjects();
}
}
else if (event == vtkCommand::ModifiedEvent)
{
// Whenever a property on the widget is modified, we change the unchecked
// property on the linked controlled proxy.
vtkSMProperty* widgetProperty = caller;
const char* function = this->GetPropertyName(widgetProperty);
if (vtkSMProperty* controlledProperty = controlledPropertyGroup->GetProperty(function))
{
vtkSMPropertyHelper whelper(widgetProperty);
vtkSMUncheckedPropertyHelper(controlledProperty).Copy(whelper);
}
}
}
//----------------------------------------------------------------------------
void vtkSMNewWidgetRepresentationProxy::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
|