diff --git a/Modules/OpenIGTLink/mitkIGTLMessageFactory.cpp b/Modules/OpenIGTLink/mitkIGTLMessageFactory.cpp index 14b80dbc68..53c24b653d 100644 --- a/Modules/OpenIGTLink/mitkIGTLMessageFactory.cpp +++ b/Modules/OpenIGTLink/mitkIGTLMessageFactory.cpp @@ -1,279 +1,280 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkIGTLMessageFactory.h" // IGT message types #include "igtlImageMessage.h" #include "igtlTransformMessage.h" #include "igtlPositionMessage.h" #include "igtlStatusMessage.h" #include "igtlImageMetaMessage.h" #include "igtlPointMessage.h" #include "igtlTrajectoryMessage.h" #include "igtlStringMessage.h" #include "igtlSensorMessage.h" #include "igtlBindMessage.h" #include "igtlPolyDataMessage.h" #include "igtlQuaternionTrackingDataMessage.h" #include "igtlCapabilityMessage.h" #include "igtlNDArrayMessage.h" #include "igtlTrackingDataMessage.h" #include "igtlColorTableMessage.h" #include "igtlLabelMetaMessage.h" //own types #include "mitkIGTLDummyMessage.h" #include "mitkIGTLMessageCommon.h" #include "itksys/SystemTools.hxx" //------------------------------------------------------------ // Define message clone classes // igtlMessageHandlerClassMacro() defines a child class of // igtl::MessageHandler to handle OpenIGTLink messages for // the message type specified as the first argument. The // second argument will be used for the name of this // message handler class, while the third argument specifies // a type of data that will be shared with the message functions // of this handler class. mitkIGTMessageCloneClassMacro(igtl::TransformMessage, TransformMsgCloneHandler); /** * \brief Clones the original message interpreted as transform message * \param original_ The original message that will be interpreted as transform * message * \return The clone of the input message */ igtl::MessageBase::Pointer TransformMsgCloneHandler::Clone(igtl::MessageBase* original_) { bool copySuccess = false; igtl::TransformMessage::Pointer clone_ = igtl::TransformMessage::New(); //initialize the clone // clone = igtl::MessageBase::New(); igtl::TransformMessage* original = (igtl::TransformMessage*)original_; //copy all meta data copySuccess = clone_->Copy(original); if ( !copySuccess ) return NULL; //copy all data that is important for this class //copy the matrix igtl::Matrix4x4 mat; original->GetMatrix(mat); clone_->SetMatrix(mat); //copy the normals float normals[3][3]; original->GetNormals(normals); clone_->SetNormals(normals); //copy the position float position[3]; original->GetPosition(position); clone_->SetPosition(position); return igtl::MessageBase::Pointer(clone_.GetPointer()); } mitk::IGTLMessageFactory::IGTLMessageFactory() { //create clone handlers // mitk::IGTLMessageCloneHandler::Pointer tmch = ; this->AddMessageNewMethod("NONE", NULL); //OpenIGTLink Types V1 this->AddMessageNewMethod("IMAGE", (PointerToMessageBaseNew)&igtl::ImageMessage::New); this->AddMessageNewMethod("TRANSFORM", (PointerToMessageBaseNew)&igtl::TransformMessage::New); this->AddMessageNewMethod("POSITION", (PointerToMessageBaseNew)&igtl::PositionMessage::New); this->AddMessageNewMethod("STATUS", (PointerToMessageBaseNew)&igtl::StatusMessage::New); this->AddMessageNewMethod("CAPABILITY", (PointerToMessageBaseNew)&igtl::StatusMessage::New); this->AddMessageNewMethod("GET_IMAGE", (PointerToMessageBaseNew)&igtl::GetImageMessage::New); this->AddMessageNewMethod("GET_TRANS", (PointerToMessageBaseNew)&igtl::GetTransformMessage::New); // this->AddMessageNewMethod("GET_POS", (PointerToMessageBaseNew)&igtl::GetPositionMessage::New); //not available??? this->AddMessageNewMethod("GET_STATUS", (PointerToMessageBaseNew)&igtl::GetStatusMessage::New); this->AddMessageNewMethod("GET_CAPABIL", (PointerToMessageBaseNew)&igtl::GetCapabilityMessage::New); // //OpenIGTLink Types V2 this->AddMessageNewMethod("IMGMETA", (PointerToMessageBaseNew)&igtl::ImageMetaMessage::New); this->AddMessageNewMethod("LBMETA", (PointerToMessageBaseNew)&igtl::LabelMetaMessage::New); this->AddMessageNewMethod("COLORT", (PointerToMessageBaseNew)&igtl::ColorTableMessage::New); this->AddMessageNewMethod("POINT", (PointerToMessageBaseNew)&igtl::PointMessage::New); this->AddMessageNewMethod("TRAJ", (PointerToMessageBaseNew)&igtl::TrajectoryMessage::New); this->AddMessageNewMethod("TDATA", (PointerToMessageBaseNew)&igtl::TrackingDataMessage::New); this->AddMessageNewMethod("QTDATA", (PointerToMessageBaseNew)&igtl::QuaternionTrackingDataMessage::New); this->AddMessageNewMethod("SENSOR", (PointerToMessageBaseNew)&igtl::SensorMessage::New); this->AddMessageNewMethod("STRING", (PointerToMessageBaseNew)&igtl::StringMessage::New); this->AddMessageNewMethod("NDARRAY", (PointerToMessageBaseNew)&igtl::NDArrayMessage::New); this->AddMessageNewMethod("BIND", (PointerToMessageBaseNew)&igtl::BindMessage::New); this->AddMessageNewMethod("POLYDATA", (PointerToMessageBaseNew)&igtl::PolyDataMessage::New); this->AddMessageNewMethod("GET_IMGMETA", (PointerToMessageBaseNew)&igtl::GetImageMetaMessage::New); this->AddMessageNewMethod("GET_LBMETA", (PointerToMessageBaseNew)&igtl::GetLabelMetaMessage::New); this->AddMessageNewMethod("GET_COLORT", (PointerToMessageBaseNew)&igtl::GetColorTableMessage::New); this->AddMessageNewMethod("GET_POINT", (PointerToMessageBaseNew)&igtl::GetPointMessage::New); this->AddMessageNewMethod("GET_TRAJ", (PointerToMessageBaseNew)&igtl::GetTrajectoryMessage::New); // this->AddMessageNewMethod("GET_TDATA", (PointerToMessageBaseNew)&igtl::GetTrackingDataMessage::New); //not available??? // this->AddMessageNewMethod("GET_QTDATA", (PointerToMessageBaseNew)&igtl::GetQuaternionTrackingDataMessage::New); // this->AddMessageNewMethod("GET_SENSOR", (PointerToMessageBaseNew)&igtl::GetSensorMessage::New); // this->AddMessageNewMethod("GET_STRING", (PointerToMessageBaseNew)&igtl::GetStringMessage::New); // this->AddMessageNewMethod("GET_NDARRAY", (PointerToMessageBaseNew)&igtl::GetNDArrayMessage::New); this->AddMessageNewMethod("GET_BIND", (PointerToMessageBaseNew)&igtl::GetBindMessage::New); this->AddMessageNewMethod("GET_POLYDATA", (PointerToMessageBaseNew)&igtl::GetPolyDataMessage::New); this->AddMessageNewMethod("RTS_BIND", (PointerToMessageBaseNew)&igtl::RTSBindMessage::New); this->AddMessageNewMethod("RTS_QTDATA", (PointerToMessageBaseNew)&igtl::RTSQuaternionTrackingDataMessage::New); this->AddMessageNewMethod("RTS_TDATA", (PointerToMessageBaseNew)&igtl::RTSTrackingDataMessage::New); //todo: check if there are more RTS messages this->AddMessageNewMethod("STT_BIND", (PointerToMessageBaseNew)&igtl::StartBindMessage::New); this->AddMessageNewMethod("STT_TDATA", (PointerToMessageBaseNew)&igtl::StartTrackingDataMessage::New); this->AddMessageNewMethod("STT_QTDATA", (PointerToMessageBaseNew)&igtl::StartQuaternionTrackingDataMessage::New); //todo: check if there are more STT messages this->AddMessageNewMethod("STP_BIND", (PointerToMessageBaseNew)&igtl::StopBindMessage::New); this->AddMessageNewMethod("STP_TDATA", (PointerToMessageBaseNew)&igtl::StopTrackingDataMessage::New); this->AddMessageNewMethod("STP_QTDATA", (PointerToMessageBaseNew)&igtl::StopQuaternionTrackingDataMessage::New); //todo: check if there are more STP messages //Own Types this->AddMessageNewMethod("DUMMY", (PointerToMessageBaseNew)&mitk::IGTLDummyMessage::New); } mitk::IGTLMessageFactory::~IGTLMessageFactory() { } void mitk::IGTLMessageFactory::AddMessageType(std::string messageTypeName, IGTLMessageFactory::PointerToMessageBaseNew messageTypeNewPointer, mitk::IGTLMessageCloneHandler::Pointer cloneHandler) { this->AddMessageNewMethod(messageTypeName, messageTypeNewPointer); this->AddMessageCloneHandler(messageTypeName, cloneHandler); } void mitk::IGTLMessageFactory::AddMessageNewMethod(std::string messageTypeName, IGTLMessageFactory::PointerToMessageBaseNew messageTypeNewPointer ) { this->m_NewMethods[messageTypeName] = messageTypeNewPointer; } void mitk::IGTLMessageFactory::AddMessageCloneHandler(std::string msgTypeName, mitk::IGTLMessageCloneHandler::Pointer cloneHandler ) { this->m_CloneHandlers[msgTypeName] = cloneHandler; } mitk::IGTLMessageCloneHandler::Pointer mitk::IGTLMessageFactory::GetCloneHandler(std::string messageTypeName) { if ( this->m_CloneHandlers.find(messageTypeName) != this->m_CloneHandlers.end() ) { return m_CloneHandlers[messageTypeName]; } MITK_ERROR("IGTLMessageFactory") << messageTypeName << " message type is not registered to factory!"; mitkThrow() << messageTypeName << " message type is not registered to factory!"; return NULL; } igtl::MessageBase::Pointer mitk::IGTLMessageFactory::Clone(igtl::MessageBase::Pointer msg) { return this->GetCloneHandler(msg->GetDeviceType())->Clone(msg); } mitk::IGTLMessageFactory::PointerToMessageBaseNew mitk::IGTLMessageFactory::GetMessageTypeNewPointer(std::string messageTypeName) { if ( this->m_NewMethods.find(messageTypeName) != this->m_NewMethods.end() ) { return m_NewMethods[messageTypeName]; } MITK_ERROR("IGTLMessageFactory") << messageTypeName << " message type is not registered to factory!"; return NULL; } std::list mitk::IGTLMessageFactory::GetAvailableMessageRequestTypes() { std::list allGetMessages; for ( std::map::const_iterator it = this->m_NewMethods.begin(); it != this->m_NewMethods.end(); ++it) { - if ( it->first.find("GET_") != std::string::npos ) + if ( it->first.find("GET_") != std::string::npos || + it->first.find("STT_") != std::string::npos) { allGetMessages.push_back(it->first); } } return allGetMessages; } igtl::MessageBase::Pointer mitk::IGTLMessageFactory::CreateInstance(igtl::MessageHeader::Pointer msgHeader) { std::string messageType; //check the header if ( msgHeader.IsNull() ) { messageType = "NONE"; } else { messageType = msgHeader->GetDeviceType(); } //make message type uppercase messageType = itksys::SystemTools::UpperCase(messageType); //find the according new method if ( this->m_NewMethods.find( messageType ) != this->m_NewMethods.end() ) { if ( this->m_NewMethods[messageType] != NULL) { // Call tracker New() function if tracker not NULL return (*this->m_NewMethods[messageType])(); } else return NULL; } else { MITK_ERROR("IGTLMessageFactory") << "Unknown IGT message type: " << messageType; return NULL; } } diff --git a/Modules/OpenIGTLinkUI/Qmitk/QmitkIGTLDeviceSourceManagementWidget.cpp b/Modules/OpenIGTLinkUI/Qmitk/QmitkIGTLDeviceSourceManagementWidget.cpp index 9d2c6d95fc..d6888def1b 100644 --- a/Modules/OpenIGTLinkUI/Qmitk/QmitkIGTLDeviceSourceManagementWidget.cpp +++ b/Modules/OpenIGTLinkUI/Qmitk/QmitkIGTLDeviceSourceManagementWidget.cpp @@ -1,547 +1,556 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkIGTLDeviceSourceManagementWidget.h" //mitk headers #include #include #include #include //qt headers #include #include #include +#include //igtl #include //poco headers #include const std::string QmitkIGTLDeviceSourceManagementWidget::VIEW_ID = "org.mitk.views.igtldevicesourcemanagementwidget"; QmitkIGTLDeviceSourceManagementWidget::QmitkIGTLDeviceSourceManagementWidget( QWidget* parent, Qt::WindowFlags f) - : QWidget(parent, f) + : QWidget(parent, f), m_OutputChanged(false) { m_Controls = NULL; m_OutputMutex = itk::FastMutexLock::New(); CreateQtPartControl(this); } QmitkIGTLDeviceSourceManagementWidget::~QmitkIGTLDeviceSourceManagementWidget() { } void QmitkIGTLDeviceSourceManagementWidget::CreateQtPartControl(QWidget *parent) { if (!m_Controls) { // create GUI widgets m_Controls = new Ui::QmitkIGTLDeviceSourceManagementWidgetControls; // setup GUI widgets m_Controls->setupUi(parent); } //every 100ms the logging window has to be updated m_UpdateLoggingWindowTimer.setInterval(100); // set the validator for the ip edit box (values must be between 0 and 255 and // there are four of them, seperated with a point QRegExpValidator *v = new QRegExpValidator(this); QRegExp rx("((1{0,1}[0-9]{0,2}|2[0-4]{1,1}[0-9]{1,1}|25[0-5]{1,1})\\.){3,3}(1{0,1}[0-9]{0,2}|2[0-4]{1,1}[0-9]{1,1}|25[0-5]{1,1})"); v->setRegExp(rx); m_Controls->editIP->setValidator(v); // set the validator for the port edit box (values must be between 1 and 65535) m_Controls->editPort->setValidator(new QIntValidator(1, 65535, this)); //connect slots with signals CreateConnections(); } void QmitkIGTLDeviceSourceManagementWidget::CreateConnections() { if (m_Controls) { // connect the widget items with the methods connect( m_Controls->butConnectWithServer, SIGNAL(clicked()), this, SLOT(OnConnect())); connect( m_Controls->editPort, SIGNAL(editingFinished()), this, SLOT(OnPortChanged()) ); connect( m_Controls->editIP, SIGNAL(editingFinished()), this, SLOT(OnHostnameChanged()) ); connect( m_Controls->butSend, SIGNAL(clicked()), this, SLOT(OnSendMessage())); connect( m_Controls->butSendCommand, SIGNAL(clicked()), this, SLOT(OnSendCommand())); connect( m_Controls->commandsComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(OnCommandChanged(const QString &))); connect( &m_UpdateLoggingWindowTimer, SIGNAL(timeout()), this, SLOT(OnUpdateLoggingWindow())); } // //main widget page: // connect( (QObject*)(m_Controls->m_AddTool), SIGNAL(clicked()), this, SLOT(OnAddTool()) ); // connect( (QObject*)(m_Controls->m_DeleteTool), SIGNAL(clicked()), this, SLOT(OnDeleteTool()) ); // connect( (QObject*)(m_Controls->m_EditTool), SIGNAL(clicked()), this, SLOT(OnEditTool()) ); // connect( (QObject*)(m_Controls->m_LoadStorage), SIGNAL(clicked()), this, SLOT(OnLoadStorage()) ); // connect( (QObject*)(m_Controls->m_SaveStorage), SIGNAL(clicked()), this, SLOT(OnSaveStorage()) ); // connect( (QObject*)(m_Controls->m_LoadTool), SIGNAL(clicked()), this, SLOT(OnLoadTool()) ); // connect( (QObject*)(m_Controls->m_SaveTool), SIGNAL(clicked()), this, SLOT(OnSaveTool()) ); // connect( (QObject*)(m_Controls->m_CreateNewStorage), SIGNAL(clicked()), this, SLOT(OnCreateStorage()) ); // //widget page "add tool": // connect( (QObject*)(m_Controls->m_ToolCreationWidget), SIGNAL(Canceled()), this, SLOT(OnAddToolCancel()) ); // connect( (QObject*)(m_Controls->m_ToolCreationWidget), SIGNAL(NavigationToolFinished()), this, SLOT(OnAddToolSave()) ); } //void QmitkIGTLDeviceSourceManagementWidget::OnLoadTool() //{ // if(m_IGTLDeviceSource->isLocked()) // { // MessageBox("Storage is locked, cannot modify it. Maybe the tracking device which uses this storage is connected. If you want to modify the storage please disconnect the device first."); // return; // } // mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New(); // std::string filename = QFileDialog::getOpenFileName(NULL,tr("Add Navigation Tool"), "/", "*.IGTTool").toAscii().data(); // if (filename == "") return; // mitk::NavigationTool::Pointer readTool = myReader->DoRead(filename); // if (readTool.IsNull()) MessageBox("Error: " + myReader->GetErrorMessage()); // else // { // if (!m_IGTLDeviceSource->AddTool(readTool)) // { // MessageBox("Error: Can't add tool!"); // m_DataStorage->Remove(readTool->GetDataNode()); // } // UpdateToolTable(); // } //} //void QmitkIGTLDeviceSourceManagementWidget::OnSaveTool() //{ // //if no item is selected, show error message: // if (m_Controls->m_ToolList->currentItem() == NULL) {MessageBox("Error: Please select tool first!");return;} // mitk::NavigationToolWriter::Pointer myWriter = mitk::NavigationToolWriter::New(); // std::string filename = QFileDialog::getSaveFileName(NULL,tr("Save Navigation Tool"), "/", "*.IGTTool").toAscii().data(); // if (filename == "") return; // if (!myWriter->DoWrite(filename,m_IGTLDeviceSource->GetTool(m_Controls->m_ToolList->currentIndex().row()))) // MessageBox("Error: "+ myWriter->GetErrorMessage()); //} void QmitkIGTLDeviceSourceManagementWidget::Initialize( mitk::DataStorage* /*dataStorage*/) { // m_DataStorage = dataStorage; // m_Controls->m_ToolCreationWidget->Initialize(m_DataStorage,"Tool0"); } void QmitkIGTLDeviceSourceManagementWidget::LoadSource( mitk::IGTLDeviceSource::Pointer sourceToLoad) { if(sourceToLoad.IsNotNull()) { this->m_IGTLDeviceSource = sourceToLoad; this->m_IGTLClient = (mitk::IGTLClient*)this->m_IGTLDeviceSource->GetIGTLDevice(); m_Controls->selectedSourceLabel->setText(m_IGTLDeviceSource->GetName().c_str()); //add observer for new message receiving // typedef itk::MemberCommand< QmitkIGTLDeviceSourceManagementWidget > CurCommandType; // CurCommandType::Pointer messageReceivedCommand = CurCommandType::New(); // messageReceivedCommand->SetCallbackFunction( // this, &QmitkIGTLDeviceSourceManagementWidget::OnMessageReceived ); // this->m_IGTLClient->AddObserver(mitk::MessageReceivedEvent(), messageReceivedCommand); typedef itk::MemberCommand< QmitkIGTLDeviceSourceManagementWidget > CurCommandType; m_MessageReceivedCommand = CurCommandType::New(); m_MessageReceivedCommand->SetCallbackFunction( this, &QmitkIGTLDeviceSourceManagementWidget::OnMessageReceived ); this->m_IGTLClient->AddObserver(mitk::MessageReceivedEvent(), m_MessageReceivedCommand); //Fill the commands combo box with all available commands FillCommandsComboBox(); //enable the controls of this widget EnableSourceControls(); //start to update the logging window this->m_UpdateLoggingWindowTimer.start(); } else { m_IGTLDeviceSource = NULL; DisableSourceControls(); } //reset the loggin text edit ResetOutput(); } ////################################################################################## ////############################## slots: main widget ################################ ////################################################################################## //void QmitkIGTLDeviceSourceManagementWidget::OnAddTool() // { // if(m_IGTLDeviceSource->isLocked()) // { // MessageBox("Storage is locked, cannot modify it. Maybe the tracking device which uses this storage is connected. If you want to modify the storage please disconnect the device first."); // return; // } // QString defaultIdentifier = "NavigationTool#"+QString::number(m_IGTLDeviceSource->GetToolCount()); // m_Controls->m_ToolCreationWidget->Initialize(m_DataStorage,defaultIdentifier.toStdString()); // m_edit = false; // m_Controls->m_MainWidgets->setCurrentIndex(1); // } //void QmitkIGTLDeviceSourceManagementWidget::OnDeleteTool() // { // //first: some checks // if(m_IGTLDeviceSource->isLocked()) // { // MessageBox("Storage is locked, cannot modify it. Maybe the tracking device which uses this storage is connected. If you want to modify the storage please disconnect the device first."); // return; // } // else if (m_Controls->m_ToolList->currentItem() == NULL) //if no item is selected, show error message: // { // MessageBox("Error: Please select tool first!"); // return; // } // m_DataStorage->Remove(m_IGTLDeviceSource->GetTool(m_Controls->m_ToolList->currentIndex().row())->GetDataNode()); // m_IGTLDeviceSource->DeleteTool(m_Controls->m_ToolList->currentIndex().row()); // UpdateToolTable(); // } //void QmitkIGTLDeviceSourceManagementWidget::OnEditTool() // { // if(m_IGTLDeviceSource->isLocked()) // { // MessageBox("Storage is locked, cannot modify it. Maybe the tracking device which uses this storage is connected. If you want to modify the storage please disconnect the device first."); // return; // } // else if (m_Controls->m_ToolList->currentItem() == NULL) //if no item is selected, show error message: // { // MessageBox("Error: Please select tool first!"); // return; // } // mitk::NavigationTool::Pointer selectedTool = m_IGTLDeviceSource->GetTool(m_Controls->m_ToolList->currentIndex().row()); // m_Controls->m_ToolCreationWidget->SetDefaultData(selectedTool); // m_edit = true; // m_Controls->m_MainWidgets->setCurrentIndex(1); // } //void QmitkIGTLDeviceSourceManagementWidget::OnCreateStorage() // { // QString storageName = QInputDialog::getText(NULL,"Storage Name","Name of the new tool storage:"); // if (storageName.isNull()) return; // m_IGTLDeviceSource = mitk::NavigationToolStorage::New(this->m_DataStorage); // m_IGTLDeviceSource->SetName(storageName.toStdString()); // m_Controls->m_StorageName->setText(m_IGTLDeviceSource->GetName().c_str()); // EnableStorageControls(); // emit NewStorageAdded(m_IGTLDeviceSource, storageName.toStdString()); // } //void QmitkIGTLDeviceSourceManagementWidget::OnLoadStorage() // { // mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(m_DataStorage); // std::string filename = QFileDialog::getOpenFileName(NULL, tr("Open Navigation Tool Storage"), "/", tr("IGT Tool Storage (*.IGTToolStorage)")).toStdString(); // if (filename == "") return; // try // { // mitk::NavigationToolStorage::Pointer tempStorage = myDeserializer->Deserialize(filename); // if (tempStorage.IsNull()) MessageBox("Error" + myDeserializer->GetErrorMessage()); // else // { // Poco::Path myPath = Poco::Path(filename.c_str()); // tempStorage->SetName(myPath.getFileName()); //set the filename as name for the storage, so the user can identify it // this->LoadStorage(tempStorage); // emit NewStorageAdded(m_IGTLDeviceSource,myPath.getFileName()); // } // } // catch (const mitk::Exception& exception) // { // MessageBox(exception.GetDescription()); // } // } //void QmitkIGTLDeviceSourceManagementWidget::OnSaveStorage() // { // //read in filename // QString filename = QFileDialog::getSaveFileName(NULL, tr("Save Navigation Tool Storage"), "/", tr("IGT Tool Storage (*.IGTToolStorage)")); // if (filename.isEmpty()) return; //canceled by the user // // add file extension if it wasn't added by the file dialog // if ( filename.right(15) != ".IGTToolStorage" ) { filename += ".IGTToolStorage"; } // //serialize tool storage // mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); // if (!mySerializer->Serialize(filename.toStdString(),m_IGTLDeviceSource)) // { // MessageBox("Error: " + mySerializer->GetErrorMessage()); // return; // } // Poco::Path myPath = Poco::Path(filename.toStdString()); // m_Controls->m_StorageName->setText(QString::fromStdString(myPath.getFileName())); // } ////################################################################################## ////############################## slots: add tool widget ############################ ////################################################################################## //void QmitkIGTLDeviceSourceManagementWidget::OnAddToolSave() // { // mitk::NavigationTool::Pointer newTool = m_Controls->m_ToolCreationWidget->GetCreatedTool(); // if (m_edit) //here we edit a existing tool // { // mitk::NavigationTool::Pointer editedTool = m_IGTLDeviceSource->GetTool(m_Controls->m_ToolList->currentIndex().row()); // editedTool->Graft(newTool); // } // else //here we create a new tool // { // m_IGTLDeviceSource->AddTool(newTool); // } // UpdateToolTable(); // m_Controls->m_MainWidgets->setCurrentIndex(0); // } //void QmitkIGTLDeviceSourceManagementWidget::OnAddToolCancel() // { // m_Controls->m_MainWidgets->setCurrentIndex(0); // } void QmitkIGTLDeviceSourceManagementWidget::MessageBox(std::string s) { QMessageBox msgBox; msgBox.setText(s.c_str()); msgBox.exec(); } void QmitkIGTLDeviceSourceManagementWidget::DisableSourceControls() { m_Controls->selectedSourceLabel->setText(""); m_Controls->editIP->setEnabled(false); m_Controls->editPort->setEnabled(false); m_Controls->editSend->setEnabled(false); // m_Controls->m_AddTool->setEnabled(false); // m_Controls->m_LoadTool->setEnabled(false); // m_Controls->m_selectedLabel->setEnabled(false); // m_Controls->m_DeleteTool->setEnabled(false); // m_Controls->m_EditTool->setEnabled(false); // m_Controls->m_SaveTool->setEnabled(false); // m_Controls->m_ToolList->setEnabled(false); // m_Controls->m_SaveStorage->setEnabled(false); // m_Controls->m_ToolLabel->setEnabled(false); } void QmitkIGTLDeviceSourceManagementWidget::EnableSourceControls() { m_Controls->editIP->setEnabled(true); m_Controls->editPort->setEnabled(true); m_Controls->butConnectWithServer->setEnabled(true); // m_Controls->editSend->setEnabled(false); // m_Controls->m_AddTool->setEnabled(true); // m_Controls->m_LoadTool->setEnabled(true); // m_Controls->m_selectedLabel->setEnabled(true); // m_Controls->m_DeleteTool->setEnabled(true); // m_Controls->m_EditTool->setEnabled(true); // m_Controls->m_SaveTool->setEnabled(true); // m_Controls->m_ToolList->setEnabled(true); // m_Controls->m_SaveStorage->setEnabled(true); // m_Controls->m_ToolLabel->setEnabled(true); } void QmitkIGTLDeviceSourceManagementWidget::OnConnect() { if(m_Controls->butConnectWithServer->text() == "Connect") { QString port = m_Controls->editPort->text(); m_IGTLClient->SetPortNumber(port.toInt()); std::string hostname = m_Controls->editIP->text().toStdString(); m_IGTLClient->SetHostname(hostname); if ( m_IGTLClient->OpenConnection() ) { if ( m_IGTLClient->StartCommunication() ) { m_Controls->editIP->setEnabled(false); m_Controls->editPort->setEnabled(false); m_Controls->editSend->setEnabled(true); m_Controls->butSend->setEnabled(true); m_Controls->commandsComboBox->setEnabled(true); m_Controls->butConnectWithServer->setText("Disconnect"); std::stringstream s; s << "
Successfully connected to " << hostname << " on port " << port.toStdString(); this->AddOutput(s.str()); } else { MITK_ERROR("QmitkIGTLDeviceSourceManagementWidget") << "Could not start a communication with the" "server because the client is in the wrong state"; this->AddOutput("
Connection is not working."); } } else { MITK_ERROR("QmitkIGTLDeviceSourceManagementWidget") << "Could not connect to the server. " "Please check the hostname and port."; this->AddOutput("
Connection is not working. Please Check Host and Port."); } } else { m_Controls->editIP->setEnabled(true); m_Controls->editPort->setEnabled(true); m_Controls->editSend->setEnabled(false); m_Controls->butSend->setEnabled(false); m_Controls->commandsComboBox->setEnabled(false); m_Controls->butConnectWithServer->setText("Connect"); m_IGTLClient->CloseConnection(); this->AddOutput("
Closed connection."); } } void QmitkIGTLDeviceSourceManagementWidget::OnPortChanged() { } void QmitkIGTLDeviceSourceManagementWidget::OnHostnameChanged() { } void QmitkIGTLDeviceSourceManagementWidget::OnSendCommand() { m_IGTLClient->SendMessage(m_CurrentCommand.GetPointer()); std::stringstream s; s << "
Sent command with DeviceType: " << m_CurrentCommand->GetDeviceType(); this->AddOutput(s.str()); } void QmitkIGTLDeviceSourceManagementWidget::OnCommandChanged( const QString & curCommand) { mitk::IGTLMessageFactory::Pointer msgFactory = this->m_IGTLClient->GetMessageFactory(); //create a new message that fits to the selected get message type command this->m_CurrentCommand = msgFactory->GetMessageTypeNewPointer( curCommand.toStdString())(); //enable the send command button this->m_Controls->butSendCommand->setEnabled(true); } void QmitkIGTLDeviceSourceManagementWidget::OnSendMessage() { std::string toBeSend = m_Controls->editSend->text().toStdString(); igtl::StringMessage::Pointer msg = igtl::StringMessage::New().GetPointer(); msg->SetString(toBeSend); m_IGTLClient->SendMessage(msg.GetPointer()); std::stringstream s; s << "
Sent message with DeviceType: " << msg->GetDeviceType(); this->AddOutput(s.str()); // if ( m_IGTLClient->SendMessage(msg.GetPointer()) ) // { // MITK_INFO("OpenIGTLinkExample") << "Successfully sent the message."; // } // else // { // MITK_ERROR("OpenIGTLinkExample") << "Could not send the message."; // } } void QmitkIGTLDeviceSourceManagementWidget::OnMessageReceived(itk::Object* caller, const itk::EventObject&/*event*/) { //get the IGTL device that invoked this event mitk::IGTLDevice* dev = (mitk::IGTLDevice*)caller; std::stringstream s; s << "
Received a message:
" << dev->GetOldestMessageInformation(); this->AddOutput(s.str()); } void QmitkIGTLDeviceSourceManagementWidget::OnUpdateLoggingWindow() { m_OutputMutex->Lock(); - m_Controls->m_Logging->setHtml(QString(m_Output.str().c_str())); - m_Controls->m_Logging->update(); +// m_Controls->m_Logging->setHtml(QString(m_Output.str().c_str())); +// m_Controls->m_Logging->update(); + if ( m_OutputChanged ) + { + m_Controls->m_Logging->setText(QString(m_Output.str().c_str())); + QScrollBar* sb = m_Controls->m_Logging->verticalScrollBar(); + sb->setValue(sb->maximum()); + m_OutputChanged = false; + } m_OutputMutex->Unlock(); } void QmitkIGTLDeviceSourceManagementWidget::FillCommandsComboBox() { //load the msg factory from the client (maybe this will be moved later on) mitk::IGTLMessageFactory::Pointer msgFactory = this->m_IGTLClient->GetMessageFactory(); //get the available commands as std::list std::list commandsList_ = msgFactory->GetAvailableMessageRequestTypes(); //create a string list to convert the std::list QStringList commandsList; while ( commandsList_.size() ) { commandsList.append(QString::fromStdString(commandsList_.front())); commandsList_.pop_front(); } //fill the combo box with life this->m_Controls->commandsComboBox->addItems(commandsList); } void QmitkIGTLDeviceSourceManagementWidget::ResetOutput() { m_OutputMutex->Lock(); m_Output.str(""); m_Output <<"output:"; m_Controls->m_Logging->setHtml(QString(m_Output.str().c_str())); m_OutputMutex->Unlock(); } void QmitkIGTLDeviceSourceManagementWidget::AddOutput(std::string s) { //print output m_OutputMutex->Lock(); m_Output << s; + m_OutputChanged = true; m_OutputMutex->Unlock(); } diff --git a/Modules/OpenIGTLinkUI/Qmitk/QmitkIGTLDeviceSourceManagementWidget.h b/Modules/OpenIGTLinkUI/Qmitk/QmitkIGTLDeviceSourceManagementWidget.h index dffbada44c..6c1e3dd7a1 100644 --- a/Modules/OpenIGTLinkUI/Qmitk/QmitkIGTLDeviceSourceManagementWidget.h +++ b/Modules/OpenIGTLinkUI/Qmitk/QmitkIGTLDeviceSourceManagementWidget.h @@ -1,150 +1,152 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef QMITKIGTLDeviceSourceMANAGEMENTWIDGET_H #define QMITKIGTLDeviceSourceMANAGEMENTWIDGET_H //QT headers #include #include //mitk headers #include "MitkOpenIGTLinkUIExports.h" #include "mitkIGTLDeviceSource.h" #include "mitkIGTLClient.h" #include "mitkDataStorage.h" //itk #include //ui header #include "ui_QmitkIGTLDeviceSourceManagementWidgetControls.h" /** Documentation: * \brief An object of this class offers an UI to manage OpenIGTLink Device * Sources and OpenIGTLink Devices. * * Be sure to call the Initialize-methode before you start the widget * otherwise some errors might occure. * * \ingroup OpenIGTLinkUI */ class MITK_OPENIGTLINKUI_EXPORT QmitkIGTLDeviceSourceManagementWidget : public QWidget { Q_OBJECT public: static const std::string VIEW_ID; /** Initializes the widget. Has to be called before any action, * otherwise errors might occur. */ void Initialize(mitk::DataStorage*); /** Loads a source to the widget. The old source is dropped, so be careful, * if the source is not saved somewhere else it might be lost. You might * want to ask the user if he wants to save the changes before calling this * method. * @param sourceToLoad This source will be loaded and might be modified * by the user. */ void LoadSource(mitk::IGTLDeviceSource::Pointer sourceToLoad); QmitkIGTLDeviceSourceManagementWidget(QWidget* parent = 0, Qt::WindowFlags f = 0); ~QmitkIGTLDeviceSourceManagementWidget(); /// \brief Is called when the current device received a message void OnMessageReceived(itk::Object* caller, const itk::EventObject&); signals: /** This signal is emmited if a new source was added by the widget itself, * e.g. because a source was loaded. * @param newSource Holds the new source which was added. * @param sourceName Name of the new source */ void NewSourceAdded(mitk::IGTLDeviceSource::Pointer newSource, std::string sourceName); protected slots: void OnConnect(); void OnPortChanged(); void OnHostnameChanged(); void OnCommandChanged(const QString& curCommand); void OnSendMessage(); void OnSendCommand(); void ResetOutput(); void AddOutput(std::string s); void OnUpdateLoggingWindow(); // //main widget page: // void OnAddTool(); // void OnDeleteTool(); // void OnEditTool(); // void OnLoadTool(); // void OnSaveTool(); // void OnLoadSource(); // void OnSaveSource(); // void OnCreateSource(); // //widget page "add tool": // void OnAddToolCancel(); // void OnAddToolSave(); protected: /// \brief Fills the commands combo box with available commands void FillCommandsComboBox(); /// \brief Creation of the connections virtual void CreateConnections(); virtual void CreateQtPartControl(QWidget *parent); Ui::QmitkIGTLDeviceSourceManagementWidgetControls* m_Controls; /** @brief holds the OpenIGTLink device */ mitk::IGTLClient* m_IGTLClient; /** @brief holds the IGTLDeviceSource we are working with. */ mitk::IGTLDeviceSource::Pointer m_IGTLDeviceSource; /** @brief shows if we are in edit mode, if not we create new source. */ bool m_edit; igtl::MessageBase::Pointer m_CurrentCommand; /** mutex to control access to m_State */ itk::FastMutexLock::Pointer m_OutputMutex; /** @brief a string stream used for logging */ std::stringstream m_Output; + /** @brief flag to indicate if the output has to be updated */ + bool m_OutputChanged; /** @brief a string stream used for logging */ QTimer m_UpdateLoggingWindowTimer; itk::MemberCommand< QmitkIGTLDeviceSourceManagementWidget >::Pointer m_MessageReceivedCommand; //############## private help methods ####################### void MessageBox(std::string s); // void UpdateToolTable(); void DisableSourceControls(); void EnableSourceControls(); }; #endif