Sfoglia il codice sorgente

Add ctkCallback

ctkCallback allows to easily associated a Qt signal with a
static function.
Jean-Christophe Fillion-Robin 14 anni fa
parent
commit
1414ea5530

+ 3 - 0
Libs/Core/CMakeLists.txt

@@ -26,6 +26,8 @@ SET(KIT_SRCS
   ctkAbstractQObjectFactory.tpp
   ctkAbstractQObjectFactory.tpp
   ctkAbstractLibraryFactory.h
   ctkAbstractLibraryFactory.h
   ctkAbstractLibraryFactory.tpp
   ctkAbstractLibraryFactory.tpp
+  ctkCallback.cpp
+  ctkCallback.h
   ctkCommandLineParser.cpp
   ctkCommandLineParser.cpp
   ctkCommandLineParser.h
   ctkCommandLineParser.h
   ctkDependencyGraph.cpp
   ctkDependencyGraph.cpp
@@ -82,6 +84,7 @@ ENDIF()
 
 
 # Headers that should run through moc
 # Headers that should run through moc
 SET(KIT_MOC_SRCS
 SET(KIT_MOC_SRCS
+  ctkCallback.h
   ctkCommandLineParser.h
   ctkCommandLineParser.h
   ctkErrorLogFDMessageHandler_p.h
   ctkErrorLogFDMessageHandler_p.h
   ctkErrorLogModel.h
   ctkErrorLogModel.h

+ 2 - 0
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -25,6 +25,7 @@ SET(KITTests_SRCS
   ctkAbstractObjectFactoryTest1.cpp
   ctkAbstractObjectFactoryTest1.cpp
   ctkAbstractPluginFactoryTest1.cpp
   ctkAbstractPluginFactoryTest1.cpp
   ctkAbstractQObjectFactoryTest1.cpp
   ctkAbstractQObjectFactoryTest1.cpp
+  ctkCallbackTest1.cpp
   ctkCommandLineParserTest1.cpp
   ctkCommandLineParserTest1.cpp
   ctkErrorLogModelTest1.cpp
   ctkErrorLogModelTest1.cpp
   ctkErrorLogModelTest2.cpp
   ctkErrorLogModelTest2.cpp
@@ -110,6 +111,7 @@ IF(HAVE_BFD)
   ADD_TEST(NAME ctkBinaryFileDescriptorTest1 COMMAND ${KIT_TESTS} ctkBinaryFileDescriptorTest1 $<TARGET_FILE:ctkBinaryFileDescriptorTestHelper>)
   ADD_TEST(NAME ctkBinaryFileDescriptorTest1 COMMAND ${KIT_TESTS} ctkBinaryFileDescriptorTest1 $<TARGET_FILE:ctkBinaryFileDescriptorTestHelper>)
   SET_PROPERTY(TEST ctkBinaryFileDescriptorTest1 PROPERTY LABELS ${PROJECT_NAME})
   SET_PROPERTY(TEST ctkBinaryFileDescriptorTest1 PROPERTY LABELS ${PROJECT_NAME})
 ENDIF()
 ENDIF()
+SIMPLE_TEST( ctkCallbackTest1 )
 SIMPLE_TEST( ctkCommandLineParserTest1 )
 SIMPLE_TEST( ctkCommandLineParserTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest2 )
 SIMPLE_TEST( ctkDependencyGraphTest2 )

+ 83 - 0
Libs/Core/Testing/Cpp/ctkCallbackTest1.cpp

@@ -0,0 +1,83 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) 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 <QCoreApplication>
+#include <QTimer>
+
+// CTK includes
+#include "ctkCallback.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+namespace
+{
+bool Done1;
+void doSomething1()
+{
+  Done1 = true;
+}
+
+bool Done2;
+void doSomething2()
+{
+  Done2 = true;
+}
+
+} // end of anomymous namespace
+
+//-----------------------------------------------------------------------------
+int ctkCallbackTest1(int argc, char * argv [] )
+{
+  QCoreApplication app(argc, argv);
+
+  Done1 = false;
+  Done2 = false;
+
+  ctkCallback callback(doSomething1);
+  QTimer::singleShot(0, &callback, SLOT(invoke()));
+  
+  ctkCallback callback2;
+  callback2.setCallback(doSomething2);
+  QTimer::singleShot(0, &callback2, SLOT(invoke()));
+
+  QTimer::singleShot(0, &app, SLOT(quit()));
+
+  int status = app.exec();
+  if (status == EXIT_FAILURE)
+    {
+    return EXIT_FAILURE;
+    }
+
+  if (!Done1)
+    {
+    std::cerr << "Line " << __LINE__ << " - Probem with ctkCallback" << std::endl;
+    return EXIT_FAILURE;
+    }
+    
+  if (!Done2)
+    {
+    std::cerr << "Line " << __LINE__ << " - Probem with ctkCallback::setCallback" << std::endl;
+    return EXIT_FAILURE;
+    }
+  return EXIT_SUCCESS;
+}

+ 54 - 0
Libs/Core/ctkCallback.cpp

@@ -0,0 +1,54 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) 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.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkCallback.h"
+
+// --------------------------------------------------------------------------
+// ctkCallback methods
+
+// --------------------------------------------------------------------------
+ctkCallback::ctkCallback(void (*callback)(), QObject * parentObject) : QObject(parentObject)
+{
+  this->setCallback(callback);
+}
+
+// --------------------------------------------------------------------------
+ctkCallback::~ctkCallback()
+{
+}
+
+// --------------------------------------------------------------------------
+void ctkCallback::setCallback(void (*callback)())
+{
+  this->Callback = callback;
+}
+
+// --------------------------------------------------------------------------
+void ctkCallback::invoke()
+{
+  if (!this->Callback)
+    {
+    return;
+    }
+  (*this->Callback)();
+}
+
+

+ 49 - 0
Libs/Core/ctkCallback.h

@@ -0,0 +1,49 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) 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.
+
+=========================================================================*/
+
+#ifndef __ctkCallback_h
+#define __ctkCallback_h
+
+// Qt includes
+#include <QObject>
+
+// CTK includes
+#include "ctkCoreExport.h"
+
+//------------------------------------------------------------------------------
+class CTK_CORE_EXPORT ctkCallback : public QObject
+{
+  Q_OBJECT
+public:
+
+  ctkCallback(void (*callback)() = 0, QObject * parentObject = 0);
+  virtual ~ctkCallback();
+  
+  void setCallback(void (*callback)());
+  
+public slots:
+  void invoke();
+  
+private:
+  void (*Callback)();
+};
+
+#endif
+