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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/*=========================================================================
Program: ParaView
Module: vtkSMProxyProperty.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 "vtkSMProxyProperty.h"
#include "vtkSMProxyPropertyInternals.h"
#include "vtkClientServerStream.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkPVXMLElement.h"
#include "vtkSMDomainIterator.h"
#include "vtkSMMessage.h"
#include "vtkSMProxy.h"
#include "vtkSMProxyGroupDomain.h"
#include "vtkSMProxyListDomain.h"
#include "vtkSMProxyLocator.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSession.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMStateLocator.h"
#include "vtkSmartPointer.h"
#include "vtkStdString.h"
#include <assert.h>
namespace
{
template <class T>
void vtkGetProxyListValues(
T& tvalues, vtkSMProxyListDomain* tpld, const T& svalues, vtkSMProxyListDomain* spld)
{
for (typename T::const_iterator siter = svalues.begin(); siter != svalues.end(); ++siter)
{
vtkSMProxy* svalue = siter->GetPointer();
vtkSMProxy* tvalue = tpld->GetProxyWithName(spld->GetProxyName(svalue));
if (tvalue && svalue)
{
tvalue->Copy(svalue);
}
tvalues.push_back(tvalue);
}
}
}
bool vtkSMProxyProperty::CreateProxyAllowed = false; // static init
//***************************************************************************
vtkStandardNewMacro(vtkSMProxyProperty);
//---------------------------------------------------------------------------
vtkSMProxyProperty::vtkSMProxyProperty()
{
this->PPInternals = new vtkSMProxyProperty::vtkPPInternals(this);
this->SkipDependency = false;
}
//---------------------------------------------------------------------------
vtkSMProxyProperty::~vtkSMProxyProperty()
{
delete this->PPInternals;
this->PPInternals = NULL;
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::AddProxy(vtkSMProxy* proxy)
{
if (this->PPInternals->Add(proxy))
{
this->Modified();
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::RemoveProxy(vtkSMProxy* proxy)
{
if (this->PPInternals->Remove(proxy))
{
this->Modified();
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::RemoveAllProxies()
{
if (this->PPInternals->Clear())
{
this->Modified();
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::SetProxy(unsigned int index, vtkSMProxy* proxy)
{
if (this->PPInternals->Set(index, proxy))
{
this->Modified();
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::SetProxies(unsigned int numProxies, vtkSMProxy* proxies[])
{
if (this->PPInternals->Set(numProxies, proxies))
{
this->Modified();
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::SetNumberOfProxies(unsigned int count)
{
if (this->PPInternals->Resize(count))
{
this->Modified();
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::AddUncheckedProxy(vtkSMProxy* proxy)
{
if (this->PPInternals->AddUnchecked(proxy))
{
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::SetUncheckedProxy(unsigned int idx, vtkSMProxy* proxy)
{
if (this->PPInternals->SetUnchecked(idx, proxy))
{
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::RemoveAllUncheckedProxies()
{
if (this->PPInternals->ClearUnchecked())
{
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
bool vtkSMProxyProperty::IsProxyAdded(vtkSMProxy* proxy)
{
return this->PPInternals->IsAdded(proxy);
}
//---------------------------------------------------------------------------
unsigned int vtkSMProxyProperty::GetNumberOfProxies()
{
return static_cast<unsigned int>(this->PPInternals->GetProxies().size());
}
//---------------------------------------------------------------------------
unsigned int vtkSMProxyProperty::GetNumberOfUncheckedProxies()
{
if (this->PPInternals->GetUncheckedProxies().size() > 0)
{
return static_cast<unsigned int>(this->PPInternals->GetUncheckedProxies().size());
}
else
{
return this->GetNumberOfProxies();
}
}
//---------------------------------------------------------------------------
vtkSMProxy* vtkSMProxyProperty::GetProxy(unsigned int idx)
{
return this->PPInternals->Get(idx);
}
//---------------------------------------------------------------------------
vtkSMProxy* vtkSMProxyProperty::GetUncheckedProxy(unsigned int idx)
{
return this->PPInternals->GetUnchecked(idx);
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::SetNumberOfUncheckedProxies(unsigned int count)
{
if (this->PPInternals->ResizeUnchecked(count))
{
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::WriteTo(vtkSMMessage* message)
{
ProxyState_Property* prop = message->AddExtension(ProxyState::property);
prop->set_name(this->GetXMLName());
this->PPInternals->WriteTo(prop->mutable_value());
// Generally, domains don't need to be serialized in protobuf state. The only
// except in vtkSMProxyListDomain. That domain has a state that need to
// serialized and preserved esp. for undo-redo and collaboration to work
// correctly.
for (this->DomainIterator->Begin(); !this->DomainIterator->IsAtEnd();
this->DomainIterator->Next())
{
vtkSMProxyListDomain* pld =
vtkSMProxyListDomain::SafeDownCast(this->DomainIterator->GetDomain());
if (pld)
{
// for each vtkSMProxyListDomain, add all proxies in that domain to the
// message.
ProxyState_UserData* user_data = prop->add_user_data();
user_data->set_key("ProxyListDomain"); // fixme -- add domain name, maybe?
Variant* variant = user_data->add_variant();
variant->set_type(Variant::PROXY);
for (unsigned int cc = 0, max = pld->GetNumberOfProxies(); cc < max; cc++)
{
variant->add_proxy_global_id(pld->GetProxy(cc) ? pld->GetProxy(cc)->GetGlobalID() : 0);
}
}
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::ReadFrom(
const vtkSMMessage* message, int msg_offset, vtkSMProxyLocator* locator)
{
const ProxyState_Property* prop = &message->GetExtension(ProxyState::property, msg_offset);
if (strcmp(prop->name().c_str(), this->GetXMLName()) != 0)
{
vtkErrorMacro("Invalid offset for this property. "
"Typically indicates that state being loaded is invalid/corrupted.");
return;
}
if (this->PPInternals->ReadFrom(prop->value(), locator))
{
this->Modified();
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
// The EXCEPTION case: read proxy list domain state.
int pld_index = 0;
for (this->DomainIterator->Begin(); !this->DomainIterator->IsAtEnd();
this->DomainIterator->Next())
{
vtkSMProxyListDomain* pld =
vtkSMProxyListDomain::SafeDownCast(this->DomainIterator->GetDomain());
if (pld)
{
if (pld_index >= prop->user_data_size())
{
vtkWarningMacro("Missing matched in ProxyListDomain state.");
continue;
}
const ProxyState_UserData& user_data = prop->user_data(pld_index);
pld_index++;
std::vector<vtkSMProxy*> proxies;
assert(user_data.key() == "ProxyListDomain");
assert(user_data.variant_size() == 1);
for (int cc = 0, max = user_data.variant(0).proxy_global_id_size(); cc < max; cc++)
{
vtkTypeUInt32 gid = user_data.variant(0).proxy_global_id(cc);
// either ask the locator for the proxy, or find an existing one.
vtkSMProxy* proxy = NULL;
if (locator && vtkSMProxyProperty::CanCreateProxy())
{
proxy = locator->LocateProxy(gid);
}
else
{
proxy = vtkSMProxy::SafeDownCast(this->GetParent()->GetSession()->GetRemoteObject(gid));
}
if (proxy != NULL || gid == 0)
{
proxies.push_back(proxy);
}
}
proxies.push_back(NULL);
pld->SetProxies(&proxies[0], static_cast<unsigned int>(proxies.size() - 1));
}
}
}
//---------------------------------------------------------------------------
int vtkSMProxyProperty::ReadXMLAttributes(vtkSMProxy* parent, vtkPVXMLElement* element)
{
int skip_dependency;
if (element->GetScalarAttribute("skip_dependency", &skip_dependency))
{
this->SkipDependency = (skip_dependency == 1);
}
return this->Superclass::ReadXMLAttributes(parent, element);
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::Copy(vtkSMProperty* src)
{
this->Superclass::Copy(src);
vtkSMProxyProperty* psrc = vtkSMProxyProperty::SafeDownCast(src);
if (!psrc)
{
return;
}
vtkSMProxyListDomain* spld =
vtkSMProxyListDomain::SafeDownCast(src->FindDomain("vtkSMProxyListDomain"));
vtkSMProxyListDomain* tpld =
vtkSMProxyListDomain::SafeDownCast(this->FindDomain("vtkSMProxyListDomain"));
bool modified = false;
bool unchecked_modified = false;
if (spld && tpld && psrc->GetNumberOfProxies() > 0)
{
vtkPPInternals::SmartVectorOfProxies tvalues;
vtkGetProxyListValues(tvalues, tpld, psrc->PPInternals->GetProxies(), spld);
modified = this->PPInternals->SetProxies(tvalues, psrc->PPInternals->GetPorts());
vtkPPInternals::WeakVectorOfProxies tuvalues;
vtkGetProxyListValues(tuvalues, tpld, psrc->PPInternals->GetUncheckedProxies(), spld);
unchecked_modified =
this->PPInternals->SetUncheckedProxies(tuvalues, psrc->PPInternals->GetUncheckedPorts());
}
else
{
modified =
this->PPInternals->SetProxies(psrc->PPInternals->GetProxies(), psrc->PPInternals->GetPorts());
unchecked_modified = this->PPInternals->SetUncheckedProxies(
psrc->PPInternals->GetUncheckedProxies(), psrc->PPInternals->GetUncheckedPorts());
}
if (modified)
{
this->Modified();
}
if (modified || unchecked_modified)
{
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "SkipDependency: " << this->SkipDependency << endl;
// os << indent << "Values: ";
// for (unsigned int i=0; i<this->GetNumberOfProxies(); i++)
// {
// os << this->GetProxy(i) << " ";
// }
// os << endl;
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::SaveStateValues(vtkPVXMLElement* propertyElement)
{
unsigned int size = this->GetNumberOfProxies();
if (size > 0)
{
propertyElement->AddAttribute("number_of_elements", size);
}
for (unsigned int i = 0; i < size; i++)
{
this->AddProxyElementState(propertyElement, i);
}
}
//---------------------------------------------------------------------------
vtkPVXMLElement* vtkSMProxyProperty::AddProxyElementState(vtkPVXMLElement* prop, unsigned int idx)
{
vtkSMProxy* proxy = this->GetProxy(idx);
vtkPVXMLElement* proxyElement = 0;
if (proxy)
{
proxyElement = vtkPVXMLElement::New();
proxyElement->SetName("Proxy");
proxyElement->AddAttribute("value", static_cast<unsigned int>(proxy->GetGlobalID()));
prop->AddNestedElement(proxyElement);
proxyElement->FastDelete();
}
return proxyElement;
}
//---------------------------------------------------------------------------
// NOTE: This method is duplicated in some way in vtkSMInputProperty
// Therefore, care must be taken to keep the two in sync.
int vtkSMProxyProperty::LoadState(vtkPVXMLElement* element, vtkSMProxyLocator* loader)
{
if (!loader)
{
// no loader specified, state remains unchanged.
return 1;
}
int prevImUpdate = this->ImmediateUpdate;
// Wait until all values are set before update (if ImmediateUpdate)
this->ImmediateUpdate = 0;
this->Superclass::LoadState(element, loader);
// If "clear" is present and is 0, it implies that the proxy elements
// currently in the property should not be cleared before loading
// the new state.
int clear = 1;
element->GetScalarAttribute("clear", &clear);
if (clear)
{
this->PPInternals->Clear();
}
unsigned int numElems = element->GetNumberOfNestedElements();
for (unsigned int i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = element->GetNestedElement(i);
if (currentElement->GetName() && (strcmp(currentElement->GetName(), "Element") == 0 ||
strcmp(currentElement->GetName(), "Proxy") == 0))
{
int id;
if (currentElement->GetScalarAttribute("value", &id))
{
if (id)
{
int port = 0;
// if output_port is not present, port remains 0.
currentElement->GetScalarAttribute("output_port", &port);
vtkSMProxy* proxy = loader->LocateProxy(id);
if (proxy)
{
this->PPInternals->Add(proxy, port);
}
else
{
// It is not an error to have missing proxies in a proxy property.
// We simply ignore such proxies.
// vtkErrorMacro("Could not create proxy of id: " << id);
// return 0;
}
}
else
{
this->PPInternals->Add(NULL);
}
}
}
}
// Do not immediately update. Leave it to the loader.
this->Modified();
this->InvokeEvent(vtkCommand::UncheckedPropertyModifiedEvent);
this->ImmediateUpdate = prevImUpdate;
return 1;
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::UpdateAllInputs()
{
unsigned int numProxies = this->GetNumberOfProxies();
for (unsigned int idx = 0; idx < numProxies; idx++)
{
vtkSMProxy* proxy = this->GetProxy(idx);
if (proxy)
{
proxy->UpdateSelfAndAllInputs();
}
}
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::EnableProxyCreation()
{
vtkSMProxyProperty::CreateProxyAllowed = true;
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::DisableProxyCreation()
{
vtkSMProxyProperty::CreateProxyAllowed = false;
}
//---------------------------------------------------------------------------
bool vtkSMProxyProperty::CanCreateProxy()
{
return vtkSMProxyProperty::CreateProxyAllowed;
}
//---------------------------------------------------------------------------
bool vtkSMProxyProperty::IsValueDefault()
{
// proxy properties are default only if they contain no proxies
return this->GetNumberOfProxies() == 0;
}
//---------------------------------------------------------------------------
void vtkSMProxyProperty::ResetToXMLDefaults()
{
this->RemoveAllProxies();
}
|