00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date$ 00006 Version: $Revision$ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 #include "QmitkCallbackFromGUIThread.h" 00019 00020 #include <QEvent> 00021 #include <QApplication> 00022 00024 class QmitkCallbackEvent : public QEvent 00025 { 00026 00027 public: 00028 00029 QmitkCallbackEvent( itk::Command* cmd, itk::EventObject* e ) 00030 : QEvent(QEvent::User), 00031 m_Command(cmd), 00032 m_Event(e) 00033 { 00034 } 00035 00036 ~QmitkCallbackEvent() 00037 { 00038 delete m_Event; 00039 } 00040 00041 itk::Command* command() 00042 { 00043 return m_Command; 00044 } 00045 00046 itk::EventObject* itkevent() 00047 { 00048 return m_Event; 00049 } 00050 00051 protected: 00052 00053 itk::Command::Pointer m_Command; 00054 itk::EventObject* m_Event; 00055 }; 00056 00057 00058 QmitkCallbackFromGUIThread::QmitkCallbackFromGUIThread() 00059 { 00060 mitk::CallbackFromGUIThread::RegisterImplementation(this); 00061 } 00062 00063 QmitkCallbackFromGUIThread::~QmitkCallbackFromGUIThread() 00064 { 00065 } 00066 00067 void QmitkCallbackFromGUIThread::CallThisFromGUIThread(itk::Command* cmd, itk::EventObject* e) 00068 { 00069 QApplication::instance()->postEvent( this, new QmitkCallbackEvent(cmd, e) ); 00070 } 00071 00072 bool QmitkCallbackFromGUIThread::event( QEvent* e ) 00073 { 00074 QmitkCallbackEvent* event( dynamic_cast<QmitkCallbackEvent*>(e) ); 00075 00076 if (!event) return false; 00077 00078 itk::Command* cmd( event->command() ); 00079 00080 00081 00082 if (cmd) 00083 { 00084 if (event->itkevent()) 00085 { 00086 cmd->Execute( (const itk::Object*) NULL, // no itk::Object here 00087 *(event->itkevent()) ); 00088 } 00089 else 00090 { 00091 const itk::NoEvent dummyEvent; 00092 cmd->Execute( (const itk::Object*) NULL, // no itk::Object here 00093 dummyEvent ); 00094 } 00095 } 00096 00097 return true; 00098 } 00099