ctkVTKObjectEventsObserver.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QStringList>
00023 #include <QVariant>
00024 #include <QList>
00025 #include <QHash>
00026 #include <QDebug>
00027 
00028 // CTK includes
00029 #include "ctkVTKObjectEventsObserver.h"
00030 #include "ctkVTKConnection.h"
00031 
00032 // VTK includes
00033 #include <vtkObject.h>
00034 #include <vtkSmartPointer.h>
00035 
00036 //-----------------------------------------------------------------------------
00037 class ctkVTKObjectEventsObserverPrivate: public ctkPrivate<ctkVTKObjectEventsObserver>
00038 {
00039 public:
00040   ctkVTKObjectEventsObserverPrivate();
00041 
00044   bool containsConnection(vtkObject* vtk_obj, unsigned long vtk_event,
00045     const QObject* qt_obj, const char* qt_slot);
00046 
00049   ctkVTKConnection* findConnection(const QString& id);
00050 
00053   ctkVTKConnection* findConnection(vtkObject* vtk_obj, unsigned long vtk_event,
00054     const QObject* qt_obj, const char* qt_slot);
00055 
00058   QList<ctkVTKConnection*> findConnections(vtkObject* vtk_obj, unsigned long vtk_event,
00059     const QObject* qt_obj, const char* qt_slot);
00060   
00061   inline QList<ctkVTKConnection*> connections()const
00062   {
00063     return ctk_p()->findChildren<ctkVTKConnection*>();
00064   }
00065   
00066   bool                            AllEnabled;
00067   bool                            AllBlocked;
00068 };
00069 
00070 //-----------------------------------------------------------------------------
00071 // ctkVTKObjectEventsObserver methods
00072 
00073 //-----------------------------------------------------------------------------
00074 ctkVTKObjectEventsObserver::ctkVTKObjectEventsObserver(QObject* _parent):Superclass(_parent)
00075 {
00076   CTK_INIT_PRIVATE(ctkVTKObjectEventsObserver);
00077   this->setProperty("QVTK_OBJECT", true);
00078 }
00079 
00080 //-----------------------------------------------------------------------------
00081 void ctkVTKObjectEventsObserver::printAdditionalInfo()
00082 {
00083   this->Superclass::dumpObjectInfo();
00084   CTK_D(ctkVTKObjectEventsObserver);
00085   qDebug() << "ctkVTKObjectEventsObserver:" << this << endl
00086            << " AllEnabled:" << d->AllEnabled << endl
00087            << " AllBlocked:" << d->AllBlocked << endl
00088            << " Parent:" << (this->parent()?this->parent()->objectName():"NULL") << endl
00089            << " Connection count:" << d->connections().count();
00090 
00091   // Loop through all connection
00092   foreach (ctkVTKConnection* connection, d->connections())
00093     {
00094     connection->printAdditionalInfo();
00095     }
00096 }
00097 
00098 //-----------------------------------------------------------------------------
00099 bool ctkVTKObjectEventsObserver::allEnabled()const
00100 {
00101   return ctk_d()->AllEnabled;
00102 }
00103 
00104 //-----------------------------------------------------------------------------
00105 void ctkVTKObjectEventsObserver::setAllEnabled(bool enable)
00106 {
00107   CTK_D(ctkVTKObjectEventsObserver);
00108   // FIXME: maybe a particular module has been enabled/disabled
00109   if (d->AllEnabled == enable) 
00110     { 
00111     return; 
00112     }
00113   // Loop through VTKQtConnections to enable/disable
00114   foreach(ctkVTKConnection* connection, d->connections())
00115     {
00116     connection->setEnabled(enable);
00117     }
00118   d->AllEnabled = enable;
00119 }
00120 
00121 //-----------------------------------------------------------------------------
00122 QString ctkVTKObjectEventsObserver::addConnection(vtkObject* old_vtk_obj, vtkObject* vtk_obj,
00123   unsigned long vtk_event, const QObject* qt_obj, const char* qt_slot, float priority)
00124 {
00125   QString connectionId; 
00126   if (old_vtk_obj)
00127     {
00128     // Check that old_object and new_object are the same type
00129     if (vtk_obj && !vtk_obj->IsA(old_vtk_obj->GetClassName()))
00130       {
00131       qCritical() << "Old vtkObject (type:" << old_vtk_obj->GetClassName() << ") to disconnect and "
00132                   << "the new VtkObject (type:" << vtk_obj->GetClassName() << ") to connect"
00133                   << "should have the same type.";
00134       return connectionId;
00135       }
00136     // Disconnect old vtkObject
00137     this->removeConnection(old_vtk_obj, vtk_event, qt_obj, qt_slot);
00138     }
00139   if (vtk_obj)
00140     {
00141     connectionId = this->addConnection(vtk_obj, vtk_event, qt_obj, qt_slot, priority);
00142     }
00143   return connectionId; 
00144 }
00145 
00146 //-----------------------------------------------------------------------------
00147 QString ctkVTKObjectEventsObserver::reconnection(vtkObject* vtk_obj,
00148   unsigned long vtk_event, const QObject* qt_obj, 
00149   const char* qt_slot, float priority)
00150 {
00151   QString connectionId; 
00152   this->removeConnection(0, vtk_event, qt_obj, qt_slot);
00153   if (vtk_obj)
00154     {
00155     connectionId = this->addConnection(vtk_obj, vtk_event, qt_obj, qt_slot, priority);
00156     }
00157   return connectionId; 
00158 }
00159 
00160 //-----------------------------------------------------------------------------
00161 QString ctkVTKObjectEventsObserver::addConnection(vtkObject* vtk_obj, unsigned long vtk_event,
00162   const QObject* qt_obj, const char* qt_slot, float priority)
00163 {
00164   CTK_D(ctkVTKObjectEventsObserver);
00165   if (!ctkVTKConnection::ValidateParameters(vtk_obj, vtk_event, qt_obj, qt_slot))
00166     {
00167     qDebug() << "ctkVTKObjectEventsObserver::addConnection(...) - Invalid parameters - "
00168              << ctkVTKConnection::shortDescription(vtk_obj, vtk_event, qt_obj, qt_slot);
00169     return QString();
00170     }
00171 
00172   // Check if such event is already observed
00173   if (d->containsConnection(vtk_obj, vtk_event, qt_obj, qt_slot))
00174     {
00175     qWarning() << "ctkVTKObjectEventsObserver::addConnection - [vtkObject:"
00176                << vtk_obj->GetClassName()
00177                << ", event:" << vtk_event << "]"
00178                << " is already connected with [qObject:" << qt_obj->objectName()
00179                << ", slot:" << qt_slot << "]";
00180     return QString();
00181     }
00182 
00183   // Instantiate a new connection, set its parameters and add it to the list
00184   ctkVTKConnection * connection = new ctkVTKConnection(this);
00185   connection->SetParameters(vtk_obj, vtk_event, qt_obj, qt_slot, priority);
00186 
00187   // If required, establish connection
00188   connection->setEnabled(d->AllEnabled);
00189   connection->setBlocked(d->AllBlocked);
00190 
00191   return connection->id();
00192 }
00193 
00194 //-----------------------------------------------------------------------------
00195 void ctkVTKObjectEventsObserver::blockAllConnections(bool block)
00196 {
00197   CTK_D(ctkVTKObjectEventsObserver);
00198   this->printAdditionalInfo();
00199   if (d->AllBlocked == block) { return; }
00200 
00201   foreach (ctkVTKConnection* connection, d->connections())
00202     {
00203     connection->setBlocked(block);
00204     }
00205   d->AllBlocked = block;
00206 }
00207 
00208 //-----------------------------------------------------------------------------
00209 void ctkVTKObjectEventsObserver::blockConnection(const QString& id, bool blocked)
00210 {
00211   CTK_D(ctkVTKObjectEventsObserver);
00212   ctkVTKConnection* connection = d->findConnection(id);
00213   if (connection == 0)
00214     {
00215     return;
00216     }
00217   connection->setBlocked(blocked);
00218 }
00219 
00220 //-----------------------------------------------------------------------------
00221 int ctkVTKObjectEventsObserver::blockConnection(bool block, vtkObject* vtk_obj,
00222   unsigned long vtk_event, const QObject* qt_obj)
00223 {
00224   CTK_D(ctkVTKObjectEventsObserver);
00225   if (!vtk_obj)
00226     {
00227     qDebug() << "ctkVTKObjectEventsObserver::blockConnectionRecursive"
00228              << "- Failed to " << (block?"block":"unblock") <<" connection"
00229              << "- vtkObject is NULL";
00230     return 0;
00231     }
00232   QList<ctkVTKConnection*> connections =
00233     d->findConnections(vtk_obj, vtk_event, qt_obj, 0);
00234   foreach (ctkVTKConnection* connection, connections)
00235     {
00236     connection->setBlocked(block);
00237     }
00238   return connections.size();
00239 }
00240 
00241 //-----------------------------------------------------------------------------
00242 int ctkVTKObjectEventsObserver::removeConnection(vtkObject* vtk_obj, unsigned long vtk_event,
00243     const QObject* qt_obj, const char* qt_slot)
00244 {
00245   CTK_D(ctkVTKObjectEventsObserver);
00246 
00247   QList<ctkVTKConnection*> connections =
00248     d->findConnections(vtk_obj, vtk_event, qt_obj, qt_slot);
00249   
00250   foreach (ctkVTKConnection* connection, connections)
00251     {
00252     connection->deleteConnection();
00253     }
00254   return connections.count();
00255 }
00256 
00257 //-----------------------------------------------------------------------------
00258 // ctkVTKObjectEventsObserverPrivate methods
00259 
00260 //-----------------------------------------------------------------------------
00261 ctkVTKObjectEventsObserverPrivate::ctkVTKObjectEventsObserverPrivate()
00262 {
00263   this->AllEnabled = true;
00264   this->AllBlocked = false;
00265 }
00266 
00267 //-----------------------------------------------------------------------------
00268 bool ctkVTKObjectEventsObserverPrivate::containsConnection(vtkObject* vtk_obj, unsigned long vtk_event,
00269   const QObject* qt_obj, const char* qt_slot)
00270 {
00271   return (this->findConnection(vtk_obj, vtk_event, qt_obj, qt_slot) != 0);
00272 }
00273 
00274 //-----------------------------------------------------------------------------
00275 ctkVTKConnection*
00276 ctkVTKObjectEventsObserverPrivate::findConnection(const QString& id)
00277 {
00278   foreach(ctkVTKConnection* connection, this->connections())
00279     {
00280     if (connection->id() == id)
00281       {
00282       return connection;
00283       }
00284     }
00285   return 0;
00286 }
00287 
00288 //-----------------------------------------------------------------------------
00289 ctkVTKConnection*
00290 ctkVTKObjectEventsObserverPrivate::findConnection(vtkObject* vtk_obj, unsigned long vtk_event,
00291                                          const QObject* qt_obj, const char* qt_slot)
00292 {
00293   QList<ctkVTKConnection*> foundConnections =
00294     this->findConnections(vtk_obj, vtk_event, qt_obj, qt_slot);
00295 
00296   return foundConnections.size() ? foundConnections[0] : 0;
00297 }
00298 
00299 //-----------------------------------------------------------------------------
00300 QList<ctkVTKConnection*>
00301 ctkVTKObjectEventsObserverPrivate::findConnections(
00302   vtkObject* vtk_obj, unsigned long vtk_event,
00303   const QObject* qt_obj, const char* qt_slot)
00304 {
00305   bool all_info = true;
00306   if(qt_slot == NULL || qt_obj == NULL || vtk_event == vtkCommand::NoEvent)
00307     {
00308     all_info = false;
00309     }
00310 
00311   QList<ctkVTKConnection*> foundConnections;
00312   // Loop through all connection
00313   foreach (ctkVTKConnection* connection, this->connections())
00314     {
00315     if (connection->isEqual(vtk_obj, vtk_event, qt_obj, qt_slot)) 
00316       {
00317       foundConnections.append(connection); 
00318       if (all_info)
00319         {
00320         break;
00321         }
00322       }
00323     }
00324   return foundConnections;
00325 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1