| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 | /*=========================================================================  Library:   CTK   Copyright (c) 2010  Kitware Inc.  Licensed under the Apache License, Version 2.0 (the "License");  you may not use this file except in compliance with the License.  You may obtain a copy of the License at      http://www.commontk.org/LICENSE  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and  limitations under the License. =========================================================================*/// Qt includes#include <QStringList>#include <QVariant>#include <QList>#include <QHash>#include <QDebug>// CTK includes#include "ctkVTKObjectEventsObserver.h"#include "ctkVTKConnection.h"// VTK includes#include <vtkObject.h>#include <vtkSmartPointer.h>//-----------------------------------------------------------------------------class ctkVTKObjectEventsObserverPrivate: public ctkPrivate<ctkVTKObjectEventsObserver>{public:  ctkVTKObjectEventsObserverPrivate();  ///   /// Check if a connection has already been added  bool containsConnection(vtkObject* vtk_obj, unsigned long vtk_event,    const QObject* qt_obj, const char* qt_slot);  ///   /// Return a reference toward the corresponding connection or 0 if doesn't exist  ctkVTKConnection* findConnection(const QString& id);  ///   /// Return a reference toward the corresponding connection or 0 if doesn't exist  ctkVTKConnection* findConnection(vtkObject* vtk_obj, unsigned long vtk_event,    const QObject* qt_obj, const char* qt_slot);  ///   /// Return all the references that match the given parameters  QList<ctkVTKConnection*> findConnections(vtkObject* vtk_obj, unsigned long vtk_event,    const QObject* qt_obj, const char* qt_slot);    inline QList<ctkVTKConnection*> connections()const  {    return ctk_p()->findChildren<ctkVTKConnection*>();  }    bool AllBlocked;  bool ObserveDeletion;};//-----------------------------------------------------------------------------// ctkVTKObjectEventsObserver methods//-----------------------------------------------------------------------------ctkVTKObjectEventsObserver::ctkVTKObjectEventsObserver(QObject* _parent):Superclass(_parent){  CTK_INIT_PRIVATE(ctkVTKObjectEventsObserver);  this->setProperty("QVTK_OBJECT", true);}//-----------------------------------------------------------------------------void ctkVTKObjectEventsObserver::printAdditionalInfo(){  this->Superclass::dumpObjectInfo();  CTK_D(ctkVTKObjectEventsObserver);  qDebug() << "ctkVTKObjectEventsObserver:" << this << endl           << " AllBlocked:" << d->AllBlocked << endl           << " Parent:" << (this->parent()?this->parent()->objectName():"NULL") << endl           << " Connection count:" << d->connections().count();  // Loop through all connection  foreach (const ctkVTKConnection* connection, d->connections())    {    qDebug() << *connection;    }}//-----------------------------------------------------------------------------QString ctkVTKObjectEventsObserver::addConnection(vtkObject* old_vtk_obj, vtkObject* vtk_obj,  unsigned long vtk_event, const QObject* qt_obj, const char* qt_slot, float priority){  QString connectionId;   if (old_vtk_obj)    {    // Check that old_object and new_object are the same type    if (vtk_obj && !vtk_obj->IsA(old_vtk_obj->GetClassName()))      {      qDebug() << "Previous vtkObject (type:" << old_vtk_obj->GetClassName() << ") to disconnect"               << "and new vtkObject (type:" << vtk_obj->GetClassName() << ") to connect"               << "have a different type.";      return connectionId;      }    // Disconnect old vtkObject    this->removeConnection(old_vtk_obj, vtk_event, qt_obj, qt_slot);    }  if (vtk_obj)    {    connectionId = this->addConnection(vtk_obj, vtk_event, qt_obj, qt_slot, priority);    }  return connectionId; }//-----------------------------------------------------------------------------QString ctkVTKObjectEventsObserver::reconnection(vtkObject* vtk_obj,  unsigned long vtk_event, const QObject* qt_obj,   const char* qt_slot, float priority){  QString connectionId;   this->removeConnection(0, vtk_event, qt_obj, qt_slot);  if (vtk_obj)    {    connectionId = this->addConnection(vtk_obj, vtk_event, qt_obj, qt_slot, priority);    }  return connectionId; }//-----------------------------------------------------------------------------QString ctkVTKObjectEventsObserver::addConnection(vtkObject* vtk_obj, unsigned long vtk_event,  const QObject* qt_obj, const char* qt_slot, float priority){  CTK_D(ctkVTKObjectEventsObserver);  if (!ctkVTKConnection::isValid(vtk_obj, vtk_event, qt_obj, qt_slot))    {    qDebug() << "ctkVTKObjectEventsObserver::addConnection(...) - Invalid parameters - "             << ctkVTKConnection::shortDescription(vtk_obj, vtk_event, qt_obj, qt_slot);    return QString();    }  // Check if such event is already observed  if (d->containsConnection(vtk_obj, vtk_event, qt_obj, qt_slot))    {    qWarning() << "ctkVTKObjectEventsObserver::addConnection - [vtkObject:"               << vtk_obj->GetClassName()               << ", event:" << vtk_event << "]"               << " is already connected with [qObject:" << qt_obj->objectName()               << ", slot:" << qt_slot << "]";    return QString();    }  // Instantiate a new connection, set its parameters and add it to the list  ctkVTKConnection * connection = new ctkVTKConnection(this);  connection->observeDeletion(d->ObserveDeletion);  connection->setup(vtk_obj, vtk_event, qt_obj, qt_slot, priority);  // If required, establish connection  connection->setBlocked(d->AllBlocked);  return connection->id();}//-----------------------------------------------------------------------------void ctkVTKObjectEventsObserver::blockAllConnections(bool block){  CTK_D(ctkVTKObjectEventsObserver);  this->printAdditionalInfo();  if (d->AllBlocked == block) { return; }  foreach (ctkVTKConnection* connection, d->connections())    {    connection->setBlocked(block);    }  d->AllBlocked = block;}//-----------------------------------------------------------------------------void ctkVTKObjectEventsObserver::blockConnection(const QString& id, bool blocked){  CTK_D(ctkVTKObjectEventsObserver);  ctkVTKConnection* connection = d->findConnection(id);  if (connection == 0)    {    return;    }  connection->setBlocked(blocked);}//-----------------------------------------------------------------------------int ctkVTKObjectEventsObserver::blockConnection(bool block, vtkObject* vtk_obj,  unsigned long vtk_event, const QObject* qt_obj){  CTK_D(ctkVTKObjectEventsObserver);  if (!vtk_obj)    {    qDebug() << "ctkVTKObjectEventsObserver::blockConnectionRecursive"             << "- Failed to " << (block?"block":"unblock") <<" connection"             << "- vtkObject is NULL";    return 0;    }  QList<ctkVTKConnection*> connections =    d->findConnections(vtk_obj, vtk_event, qt_obj, 0);  foreach (ctkVTKConnection* connection, connections)    {    connection->setBlocked(block);    }  return connections.size();}//-----------------------------------------------------------------------------int ctkVTKObjectEventsObserver::removeConnection(vtkObject* vtk_obj, unsigned long vtk_event,    const QObject* qt_obj, const char* qt_slot){  CTK_D(ctkVTKObjectEventsObserver);  QList<ctkVTKConnection*> connections =    d->findConnections(vtk_obj, vtk_event, qt_obj, qt_slot);    foreach (ctkVTKConnection* connection, connections)    {    delete connection;    }  return connections.count();}//-----------------------------------------------------------------------------// ctkVTKObjectEventsObserverPrivate methods//-----------------------------------------------------------------------------ctkVTKObjectEventsObserverPrivate::ctkVTKObjectEventsObserverPrivate(){  this->AllBlocked = false;  // ObserveDeletion == false  hasn't been that well tested...  this->ObserveDeletion = true;}//-----------------------------------------------------------------------------bool ctkVTKObjectEventsObserverPrivate::containsConnection(vtkObject* vtk_obj, unsigned long vtk_event,  const QObject* qt_obj, const char* qt_slot){  return (this->findConnection(vtk_obj, vtk_event, qt_obj, qt_slot) != 0);}//-----------------------------------------------------------------------------ctkVTKConnection*ctkVTKObjectEventsObserverPrivate::findConnection(const QString& id){  foreach(ctkVTKConnection* connection, this->connections())    {    if (connection->id() == id)      {      return connection;      }    }  return 0;}//-----------------------------------------------------------------------------ctkVTKConnection*ctkVTKObjectEventsObserverPrivate::findConnection(vtkObject* vtk_obj, unsigned long vtk_event,                                         const QObject* qt_obj, const char* qt_slot){  QList<ctkVTKConnection*> foundConnections =    this->findConnections(vtk_obj, vtk_event, qt_obj, qt_slot);  return foundConnections.size() ? foundConnections[0] : 0;}//-----------------------------------------------------------------------------QList<ctkVTKConnection*>ctkVTKObjectEventsObserverPrivate::findConnections(  vtkObject* vtk_obj, unsigned long vtk_event,  const QObject* qt_obj, const char* qt_slot){  bool all_info = true;  if(qt_slot == NULL || qt_obj == NULL || vtk_event == vtkCommand::NoEvent)    {    all_info = false;    }  QList<ctkVTKConnection*> foundConnections;  // Loop through all connection  foreach (ctkVTKConnection* connection, this->connections())    {    if (connection->isEqual(vtk_obj, vtk_event, qt_obj, qt_slot))       {      foundConnections.append(connection);       if (all_info)        {        break;        }      }    }  return foundConnections;}
 |