diff --git a/Modules/QtWidgetsExt/QmitkGnuplotWidget.cpp b/Modules/QtWidgetsExt/QmitkGnuplotWidget.cpp new file mode 100644 index 0000000000..b12ae3e7eb --- /dev/null +++ b/Modules/QtWidgetsExt/QmitkGnuplotWidget.cpp @@ -0,0 +1,221 @@ +/*=================================================================== + +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 +#include +#include +#include +#include "QmitkGnuplotWidget.h" + +QmitkGnuplotWidget::QmitkGnuplotWidget(QWidget* parent) + : QWidget(parent), + m_Ui(new Ui::QmitkGnuplotWidget), + m_ContextMenu(NULL), + m_CopyPlotAction(NULL), + m_CopyScriptAction(NULL), + m_Process(new QProcess(this)) +{ + m_Ui->setupUi(this); + + connect(m_Process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(OnProcessStateChanged(QProcess::ProcessState))); + connect(m_Process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(OnProcessError(QProcess::ProcessError))); + connect(m_Process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(OnProcessFinished(int, QProcess::ExitStatus))); + + this->CreateContextMenu(); +} + +QmitkGnuplotWidget::~QmitkGnuplotWidget() +{ +} + +void QmitkGnuplotWidget::CreateContextMenu() +{ + m_CopyPlotAction = new QAction("Copy &Plot", this); + connect(m_CopyPlotAction, SIGNAL(triggered()), this, SLOT(OnCopyPlot())); + + m_CopyScriptAction = new QAction("Copy &Script", this); + connect(m_CopyScriptAction, SIGNAL(triggered()), this, SLOT(OnCopyScript())); + + m_ContextMenu = new QMenu(this); + + m_ContextMenu->addActions(QList() + << m_CopyPlotAction + << m_CopyScriptAction); +} + +void QmitkGnuplotWidget::contextMenuEvent(QContextMenuEvent* event) +{ + const QPixmap* plot = m_Ui->label->pixmap(); + + m_CopyPlotAction->setEnabled(plot != NULL && !plot->isNull()); + m_CopyScriptAction->setEnabled(!m_Commands.empty()); + + m_ContextMenu->popup(event->globalPos()); + + event->accept(); +} + +void QmitkGnuplotWidget::OnCopyPlot() +{ + const QPixmap* plot = m_Ui->label->pixmap(); + + if (plot != NULL && !plot->isNull()) + QApplication::clipboard()->setPixmap(*plot); +} + +void QmitkGnuplotWidget::OnCopyScript() +{ + if (m_Commands.empty()) + return; + + QString script = this->CreateSetTermCommand(); + + Q_FOREACH(const QString& command, m_Commands) + { + script += command + "\n"; + } + + QApplication::clipboard()->setText(script); +} + +void QmitkGnuplotWidget::resizeEvent(QResizeEvent*) +{ + m_ModifiedTime.Modified(); + + if (m_Process->isOpen() || m_Commands.isEmpty() || m_GnuplotPath.isEmpty()) + return; + + this->Update(); +} + +QString QmitkGnuplotWidget::GetGnuplotPath() const +{ + return m_GnuplotPath; +} + +void QmitkGnuplotWidget::SetGnuplotPath(const QString &path) +{ + m_GnuplotPath = path; + m_ModifiedTime.Modified(); +} + +QStringList QmitkGnuplotWidget::GetCommands() const +{ + return m_Commands; +} + +void QmitkGnuplotWidget::SetCommands(const QStringList& commands) +{ + m_Commands = commands; + m_ModifiedTime.Modified(); +} + +void QmitkGnuplotWidget::Update() +{ + if (m_UpdateTime < m_ModifiedTime) + m_Process->start(m_GnuplotPath, QStringList() << "-"); +} + +QSize QmitkGnuplotWidget::sizeHint() const +{ + return QSize(400, 300); +} + +QString QmitkGnuplotWidget::CreateSetTermCommand() const +{ + return QString("set term pngcairo size %1,%2 enhanced font '%3,%4'\n") + .arg(this->width()) + .arg(this->height()) + .arg(this->font().family()) + .arg(this->font().pointSize()); +} + +void QmitkGnuplotWidget::OnProcessStateChanged(QProcess::ProcessState state) +{ + if (state == QProcess::Running) + { + m_UpdateTime = m_ModifiedTime; + + m_Process->write(this->CreateSetTermCommand().toAscii()); + + Q_FOREACH(const QString& command, m_Commands) + { + m_Process->write(QString("%1\n").arg(command).toAscii()); + } + + m_Process->write("exit\n"); + m_Process->closeWriteChannel(); + } +} + +void QmitkGnuplotWidget::OnProcessError(QProcess::ProcessError error) +{ + switch (error) + { + case QProcess::FailedToStart: + m_Ui->label->setText("Gnuplot failed to start!"); + break; + + case QProcess::Crashed: + m_Ui->label->setText("Gnuplot crashed!"); + break; + + case QProcess::Timedout: + m_Ui->label->setText("Gnuplot timed out!"); + break; + + case QProcess::WriteError: + m_Ui->label->setText("Could not write to gnuplot!"); + break; + + case QProcess::ReadError: + m_Ui->label->setText("Could not read from gnuplot!"); + break; + + default: + m_Ui->label->setText("An unknown error occurred!"); + break; + } +} + +void QmitkGnuplotWidget::OnProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) +{ + bool needUpdate = false; + + if (exitStatus != QProcess::CrashExit) + { + if (exitCode == 0) + { + if (m_UpdateTime < m_ModifiedTime) + { + needUpdate = true; + } + else + { + m_Ui->label->setPixmap(QPixmap::fromImage(QImage::fromData(m_Process->readAllStandardOutput(), "PNG"))); + } + } + else + { + m_Ui->label->setText(QString("Gnuplot exit code: %1!").arg(exitCode)); + } + } + + m_Process->close(); + + if (needUpdate) + this->Update(); +} diff --git a/Modules/QtWidgetsExt/QmitkGnuplotWidget.h b/Modules/QtWidgetsExt/QmitkGnuplotWidget.h new file mode 100644 index 0000000000..e1277293f4 --- /dev/null +++ b/Modules/QtWidgetsExt/QmitkGnuplotWidget.h @@ -0,0 +1,78 @@ +/*=================================================================== + +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 QmitkGnuplotWidget_h +#define QmitkGnuplotWidget_h + +#include +#include +#include +#include +#include + +class QAction; +class QMenu; + +namespace Ui +{ + class QmitkGnuplotWidget; +} + +class MitkQtWidgetsExt_EXPORT QmitkGnuplotWidget : public QWidget +{ + Q_OBJECT + +public: + explicit QmitkGnuplotWidget(QWidget* parent = NULL); + ~QmitkGnuplotWidget(); + + QString GetGnuplotPath() const; + void SetGnuplotPath(const QString& path); + + QStringList GetCommands() const; + void SetCommands(const QStringList& commands); + + void Update(); + + QSize sizeHint() const; + +protected: + void contextMenuEvent(QContextMenuEvent* event); + void resizeEvent(QResizeEvent* event); + +private slots: + void OnProcessStateChanged(QProcess::ProcessState state); + void OnProcessError(QProcess::ProcessError error); + void OnProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); + void OnCopyPlot(); + void OnCopyScript(); + +private: + void CreateContextMenu(); + QString CreateSetTermCommand() const; + + QScopedPointer m_Ui; + QMenu* m_ContextMenu; + QAction* m_CopyPlotAction; + QAction* m_CopyScriptAction; + QProcess* m_Process; + QString m_GnuplotPath; + QStringList m_Commands; + itk::TimeStamp m_ModifiedTime; + itk::TimeStamp m_UpdateTime; +}; + +#endif diff --git a/Modules/QtWidgetsExt/QmitkGnuplotWidget.ui b/Modules/QtWidgetsExt/QmitkGnuplotWidget.ui new file mode 100644 index 0000000000..c36919fa6c --- /dev/null +++ b/Modules/QtWidgetsExt/QmitkGnuplotWidget.ui @@ -0,0 +1,56 @@ + + + QmitkGnuplotWidget + + + + 0 + 0 + 400 + 300 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 1 + 1 + + + + background-color: rgb(255, 255, 255); +color: rgb(255, 0, 0); + + + true + + + Qt::AlignCenter + + + true + + + Qt::NoTextInteraction + + + + + + + + diff --git a/Modules/QtWidgetsExt/files.cmake b/Modules/QtWidgetsExt/files.cmake index 6ed25fb208..7a30a88c11 100644 --- a/Modules/QtWidgetsExt/files.cmake +++ b/Modules/QtWidgetsExt/files.cmake @@ -1,131 +1,134 @@ set(CPP_FILES QmitkApplicationBase/QmitkIOUtil.cpp QmitkAboutDialog/QmitkAboutDialog.cpp QmitkPropertyObservers/QmitkBasePropertyView.cpp QmitkPropertyObservers/QmitkBoolPropertyWidget.cpp QmitkPropertyObservers/QmitkColorPropertyEditor.cpp QmitkPropertyObservers/QmitkColorPropertyView.cpp QmitkPropertyObservers/QmitkEnumerationPropertyWidget.cpp QmitkPropertyObservers/QmitkNumberPropertyEditor.cpp QmitkPropertyObservers/QmitkNumberPropertyView.cpp QmitkPropertyObservers/QmitkPropertyViewFactory.cpp QmitkPropertyObservers/QmitkStringPropertyEditor.cpp QmitkPropertyObservers/QmitkStringPropertyOnDemandEdit.cpp QmitkPropertyObservers/QmitkStringPropertyView.cpp QmitkPropertyObservers/QmitkNumberPropertySlider.cpp QmitkPropertyObservers/QmitkUGCombinedRepresentationPropertyWidget.cpp qclickablelabel.cpp QmitkCallbackFromGUIThread.cpp QmitkEditPointDialog.cpp QmitkExtRegisterClasses.cpp QmitkFileChooser.cpp QmitkFloatingPointSpanSlider.cpp QmitkColorTransferFunctionCanvas.cpp QmitkStandardViews.cpp QmitkStepperAdapter.cpp QmitkPiecewiseFunctionCanvas.cpp QmitkSliderNavigatorWidget.cpp QmitkTransferFunctionCanvas.cpp QmitkCrossWidget.cpp QmitkSliceWidget.cpp QmitkTransferFunctionWidget.cpp QmitkTransferFunctionGeneratorWidget.cpp QmitkSelectableGLWidget.cpp QmitkPrimitiveMovieNavigatorWidget.cpp QmitkHistogram.cpp QmitkHistogramWidget.cpp QmitkPlotWidget.cpp QmitkPlotDialog.cpp QmitkPointListModel.cpp QmitkPointListView.cpp QmitkPointListWidget.cpp QmitkPointListViewWidget.cpp QmitkCorrespondingPointSetsView.cpp QmitkCorrespondingPointSetsModel.cpp QmitkCorrespondingPointSetsWidget.cpp QmitkVideoBackground.cpp QmitkHotkeyLineEdit.cpp QmitkBoundingObjectWidget.cpp QmitkModuleTableModel.cpp QmitkModulesDialog.cpp QmitkHistogramJSWidget.cpp QmitkWebPage.cpp QmitkLineEdit.cpp + QmitkGnuplotWidget.cpp ) set(MOC_H_FILES QmitkPropertyObservers/QmitkBasePropertyView.h QmitkPropertyObservers/QmitkBoolPropertyWidget.h QmitkPropertyObservers/QmitkColorPropertyEditor.h QmitkPropertyObservers/QmitkColorPropertyView.h QmitkPropertyObservers/QmitkEnumerationPropertyWidget.h QmitkPropertyObservers/QmitkNumberPropertyEditor.h QmitkPropertyObservers/QmitkNumberPropertyView.h QmitkPropertyObservers/QmitkStringPropertyEditor.h QmitkPropertyObservers/QmitkStringPropertyOnDemandEdit.h QmitkPropertyObservers/QmitkStringPropertyView.h QmitkPropertyObservers/QmitkNumberPropertySlider.h QmitkPropertyObservers/QmitkUGCombinedRepresentationPropertyWidget.h qclickablelabel.h QmitkCallbackFromGUIThread.h QmitkEditPointDialog.h QmitkStandardViews.h QmitkStepperAdapter.h QmitkSliderNavigatorWidget.h QmitkSliceWidget.h QmitkColorTransferFunctionCanvas.h QmitkPiecewiseFunctionCanvas.h QmitkTransferFunctionCanvas.h QmitkFloatingPointSpanSlider.h QmitkCrossWidget.h QmitkTransferFunctionWidget.h QmitkTransferFunctionGeneratorWidget.h QmitkPrimitiveMovieNavigatorWidget.h QmitkPlotWidget.h QmitkPointListModel.h QmitkPointListView.h QmitkPointListWidget.h QmitkPointListViewWidget.h QmitkCorrespondingPointSetsView.h QmitkCorrespondingPointSetsModel.h QmitkCorrespondingPointSetsWidget.h QmitkHistogramWidget.h QmitkVideoBackground.h QmitkFileChooser.h QmitkHotkeyLineEdit.h QmitkAboutDialog/QmitkAboutDialog.h QmitkBoundingObjectWidget.h QmitkPlotWidget.h QmitkHistogramJSWidget.h QmitkWebPage.h QmitkLineEdit.h + QmitkGnuplotWidget.h ) set(UI_FILES QmitkSliderNavigator.ui QmitkSliceWidget.ui QmitkTransferFunctionWidget.ui QmitkTransferFunctionGeneratorWidget.ui QmitkSelectableGLWidget.ui QmitkPrimitiveMovieNavigatorWidget.ui QmitkAboutDialog/QmitkAboutDialogGUI.ui + QmitkGnuplotWidget.ui ) set(QRC_FILES QmitkExt.qrc resources/QmitkResources.qrc ) diff --git a/Plugins/org.mitk.gui.qt.ext/files.cmake b/Plugins/org.mitk.gui.qt.ext/files.cmake index 956c346aa1..ba1181ca41 100644 --- a/Plugins/org.mitk.gui.qt.ext/files.cmake +++ b/Plugins/org.mitk.gui.qt.ext/files.cmake @@ -1,55 +1,58 @@ set(SRC_CPP_FILES QmitkExtActionBarAdvisor.cpp QmitkExtWorkbenchWindowAdvisor.cpp QmitkExtFileSaveProjectAction.cpp QmitkOpenDicomEditorAction.cpp QmitkOpenXnatEditorAction.cpp ) set(INTERNAL_CPP_FILES QmitkAppInstancesPreferencePage.cpp + QmitkExternalProgramsPreferencePage.cpp QmitkCommonExtPlugin.cpp QmitkInputDevicesPrefPage.cpp QmitkModuleView.cpp ) set(UI_FILES src/internal/QmitkAppInstancesPreferencePage.ui + src/internal/QmitkExternalProgramsPreferencePage.ui ) set(MOC_H_FILES src/QmitkExtFileSaveProjectAction.h src/QmitkExtWorkbenchWindowAdvisor.h src/internal/QmitkAppInstancesPreferencePage.h + src/internal/QmitkExternalProgramsPreferencePage.h src/internal/QmitkCommonExtPlugin.h src/internal/QmitkExtWorkbenchWindowAdvisorHack.h src/internal/QmitkInputDevicesPrefPage.h src/internal/QmitkModuleView.h src/QmitkOpenDicomEditorAction.h src/QmitkOpenXnatEditorAction.h ) set(CACHED_RESOURCE_FILES # list of resource files which can be used by the plug-in # system without loading the plug-ins shared library, # for example the icon used in the menu and tabs for the # plug-in views in the workbench plugin.xml resources/ModuleView.png ) set(QRC_FILES # uncomment the following line if you want to use Qt resources resources/org_mitk_gui_qt_ext.qrc ) set(CPP_FILES ) foreach(file ${SRC_CPP_FILES}) set(CPP_FILES ${CPP_FILES} src/${file}) endforeach(file ${SRC_CPP_FILES}) foreach(file ${INTERNAL_CPP_FILES}) set(CPP_FILES ${CPP_FILES} src/internal/${file}) endforeach(file ${INTERNAL_CPP_FILES}) diff --git a/Plugins/org.mitk.gui.qt.ext/plugin.xml b/Plugins/org.mitk.gui.qt.ext/plugin.xml index e0da503720..b9e718f264 100644 --- a/Plugins/org.mitk.gui.qt.ext/plugin.xml +++ b/Plugins/org.mitk.gui.qt.ext/plugin.xml @@ -1,31 +1,34 @@ + + diff --git a/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkCommonExtPlugin.cpp b/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkCommonExtPlugin.cpp index ce57e1e976..d4e116e005 100644 --- a/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkCommonExtPlugin.cpp +++ b/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkCommonExtPlugin.cpp @@ -1,255 +1,257 @@ /*=================================================================== 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 "QmitkCommonExtPlugin.h" #include #include "QmitkAppInstancesPreferencePage.h" +#include "QmitkExternalProgramsPreferencePage.h" #include "QmitkInputDevicesPrefPage.h" #include "QmitkModuleView.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "berryPlatform.h" #include ctkPluginContext* QmitkCommonExtPlugin::_context = 0; void QmitkCommonExtPlugin::start(ctkPluginContext* context) { this->_context = context; QmitkExtRegisterClasses(); BERRY_REGISTER_EXTENSION_CLASS(QmitkAppInstancesPreferencePage, context) + BERRY_REGISTER_EXTENSION_CLASS(QmitkExternalProgramsPreferencePage, context) BERRY_REGISTER_EXTENSION_CLASS(QmitkInputDevicesPrefPage, context) BERRY_REGISTER_EXTENSION_CLASS(QmitkModuleView, context) if (qApp->metaObject()->indexOfSignal("messageReceived(QByteArray)") > -1) { connect(qApp, SIGNAL(messageReceived(QByteArray)), this, SLOT(handleIPCMessage(QByteArray))); } std::vector args = berry::Platform::GetApplicationArgs(); QStringList qargs; for (std::vector::const_iterator it = args.begin(); it != args.end(); ++it) { qargs << QString::fromStdString(*it); } // This is a potentially long running operation. loadDataFromDisk(qargs, true); } void QmitkCommonExtPlugin::stop(ctkPluginContext* context) { Q_UNUSED(context) this->_context = 0; } ctkPluginContext* QmitkCommonExtPlugin::getContext() { return _context; } void QmitkCommonExtPlugin::loadDataFromDisk(const QStringList &arguments, bool globalReinit) { if (!arguments.empty()) { ctkServiceReference serviceRef = _context->getServiceReference(); if (serviceRef) { mitk::IDataStorageService* dataStorageService = _context->getService(serviceRef); mitk::DataStorage::Pointer dataStorage = dataStorageService->GetDefaultDataStorage()->GetDataStorage(); int argumentsAdded = 0; for (int i = 0; i < arguments.size(); ++i) { if (arguments[i].right(5) == ".mitk") { mitk::SceneIO::Pointer sceneIO = mitk::SceneIO::New(); bool clearDataStorageFirst(false); mitk::ProgressBar::GetInstance()->AddStepsToDo(2); dataStorage = sceneIO->LoadScene( arguments[i].toLocal8Bit().constData(), dataStorage, clearDataStorageFirst ); mitk::ProgressBar::GetInstance()->Progress(2); argumentsAdded++; } else { mitk::DataNodeFactory::Pointer nodeReader = mitk::DataNodeFactory::New(); try { nodeReader->SetFileName(arguments[i].toStdString()); nodeReader->Update(); for (unsigned int j = 0 ; j < nodeReader->GetNumberOfOutputs( ); ++j) { mitk::DataNode::Pointer node = nodeReader->GetOutput(j); if (node->GetData() != 0) { dataStorage->Add(node); argumentsAdded++; } } } catch(...) { MITK_WARN << "Failed to load command line argument: " << arguments[i].toStdString(); } } } // end for each command line argument if (argumentsAdded > 0 && globalReinit) { // calculate bounding geometry mitk::RenderingManager::GetInstance()->InitializeViews(dataStorage->ComputeBoundingGeometry3D()); } } else { MITK_ERROR << "A service reference for mitk::IDataStorageService does not exist"; } } } void QmitkCommonExtPlugin::startNewInstance(const QStringList &args, const QStringList& files) { QStringList newArgs(args); #ifdef Q_OS_UNIX newArgs << QString("--") + QString::fromStdString(berry::Platform::ARG_NEWINSTANCE); #else newArgs << QString("/") + QString::fromStdString(berry::Platform::ARG_NEWINSTANCE); #endif newArgs << files; QProcess::startDetached(qApp->applicationFilePath(), newArgs); } void QmitkCommonExtPlugin::handleIPCMessage(const QByteArray& msg) { QDataStream ds(msg); QString msgType; ds >> msgType; // we only handle messages containing command line arguments if (msgType != "$cmdLineArgs") return; // activate the current workbench window berry::IWorkbenchWindow::Pointer window = berry::PlatformUI::GetWorkbench()->GetActiveWorkbenchWindow(); QMainWindow* mainWindow = static_cast (window->GetShell()->GetControl()); mainWindow->setWindowState(mainWindow->windowState() & ~Qt::WindowMinimized); mainWindow->raise(); mainWindow->activateWindow(); // Get the preferences for the instantiation behavior berry::IPreferencesService::Pointer prefService = berry::Platform::GetServiceRegistry() .GetServiceById(berry::IPreferencesService::ID); berry::IPreferences::Pointer prefs = prefService->GetSystemPreferences()->Node("/General"); bool newInstanceAlways = prefs->GetBool("newInstance.always", false); bool newInstanceScene = prefs->GetBool("newInstance.scene", true); QStringList args; ds >> args; QStringList fileArgs; QStringList sceneArgs; Poco::Util::OptionSet os; berry::Platform::GetOptionSet(os); Poco::Util::OptionProcessor processor(os); #if !defined(POCO_OS_FAMILY_UNIX) processor.setUnixStyle(false); #endif args.pop_front(); QStringList::Iterator it = args.begin(); while (it != args.end()) { std::string name; std::string value; if (processor.process(it->toStdString(), name, value)) { ++it; } else { if (it->endsWith(".mitk")) { sceneArgs << *it; } else { fileArgs << *it; } it = args.erase(it); } } if (newInstanceAlways) { if (newInstanceScene) { startNewInstance(args, fileArgs); foreach(QString sceneFile, sceneArgs) { startNewInstance(args, QStringList(sceneFile)); } } else { fileArgs.append(sceneArgs); startNewInstance(args, fileArgs); } } else { loadDataFromDisk(fileArgs, false); if (newInstanceScene) { foreach(QString sceneFile, sceneArgs) { startNewInstance(args, QStringList(sceneFile)); } } else { loadDataFromDisk(sceneArgs, false); } } } Q_EXPORT_PLUGIN2(org_mitk_gui_qt_ext, QmitkCommonExtPlugin) diff --git a/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.cpp b/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.cpp new file mode 100644 index 0000000000..af8e485885 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.cpp @@ -0,0 +1,136 @@ +/*=================================================================== + +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 +#include +#include +#include +#include +#include +#include +#include +#include "QmitkExternalProgramsPreferencePage.h" + +static berry::IPreferences::Pointer GetPreferences() +{ + berry::IPreferencesService::Pointer preferencesService = + berry::Platform::GetServiceRegistry().GetServiceById(berry::IPreferencesService::ID); + + if (preferencesService.IsNotNull()) + { + berry::IPreferences::Pointer systemPreferences = preferencesService->GetSystemPreferences(); + + if (systemPreferences.IsNotNull()) + return systemPreferences->Node("/org.mitk.gui.qt.ext.externalprograms"); + } + + mitkThrow(); +} + +QmitkExternalProgramsPreferencePage::QmitkExternalProgramsPreferencePage() + : m_Preferences(GetPreferences()), + m_Ui(new Ui::QmitkExternalProgramsPreferencePage), + m_Control(NULL), + m_GnuplotProcess(NULL) +{ +} + +QmitkExternalProgramsPreferencePage::~QmitkExternalProgramsPreferencePage() +{ +} + +void QmitkExternalProgramsPreferencePage::CreateQtControl(QWidget* parent) +{ + m_Control = new QWidget(parent); + m_GnuplotProcess = new QProcess(m_Control); + + m_Ui->setupUi(m_Control); + + connect(m_GnuplotProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(OnGnuplotProcessError(QProcess::ProcessError))); + connect(m_GnuplotProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(OnGnuplotProcessFinished(int, QProcess::ExitStatus))); + connect(m_Ui->gnuplotButton, SIGNAL(clicked()), this, SLOT(OnGnuplotButtonClicked())); + + this->Update(); +} + +void QmitkExternalProgramsPreferencePage::OnGnuplotButtonClicked() +{ + QString filter = "Gnuplot executable "; + +#if defined(WIN32) + filter += "(gnuplot.exe)"; +#else + filter += "(gnuplot)"; +#endif + + QString gnuplotPath = QFileDialog::getOpenFileName(m_Control, "Gnuplot", "", filter); + + if (!gnuplotPath.isEmpty()) + { + m_GnuplotPath = gnuplotPath; + m_GnuplotProcess->start(gnuplotPath, QStringList() << "--version", QProcess::ReadOnly); + } +} + +void QmitkExternalProgramsPreferencePage::OnGnuplotProcessError(QProcess::ProcessError) +{ + m_GnuplotPath.clear(); + m_Ui->gnuplotLineEdit->clear(); +} + +void QmitkExternalProgramsPreferencePage::OnGnuplotProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) +{ + if (exitStatus == QProcess::NormalExit && exitCode == 0) + { + QString version = QTextCodec::codecForName("UTF-8")->toUnicode(m_GnuplotProcess->readAllStandardOutput()).trimmed(); + + if (version.startsWith("gnuplot")) + { + m_Ui->gnuplotLineEdit->setText(QString("%1 (%2)").arg(m_GnuplotPath).arg(version.trimmed())); + return; + } + } + + m_GnuplotPath.clear(); + m_Ui->gnuplotLineEdit->clear(); +} + +QWidget* QmitkExternalProgramsPreferencePage::GetQtControl() const +{ + return m_Control; +} + +void QmitkExternalProgramsPreferencePage::Init(berry::IWorkbench::Pointer) +{ +} + +void QmitkExternalProgramsPreferencePage::PerformCancel() +{ +} + +bool QmitkExternalProgramsPreferencePage::PerformOk() +{ + m_Preferences->Put("gnuplot", m_GnuplotPath.toStdString()); + return true; +} + +void QmitkExternalProgramsPreferencePage::Update() +{ + m_GnuplotPath = QString::fromStdString(m_Preferences->Get("gnuplot", "")); + + if (!m_GnuplotPath.isEmpty()) + m_GnuplotProcess->start(m_GnuplotPath, QStringList() << "--version", QProcess::ReadOnly); +} diff --git a/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.h b/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.h new file mode 100644 index 0000000000..e766b3b400 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.h @@ -0,0 +1,59 @@ +/*=================================================================== + +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 QmitkExternalProgramsPreferencePage_h +#define QmitkExternalProgramsPreferencePage_h + +#include +#include +#include +#include + +namespace Ui +{ + class QmitkExternalProgramsPreferencePage; +} + +class QmitkExternalProgramsPreferencePage : public QObject, public berry::IQtPreferencePage +{ + Q_OBJECT + Q_INTERFACES(berry::IPreferencePage) + +public: + QmitkExternalProgramsPreferencePage(); + ~QmitkExternalProgramsPreferencePage(); + + void CreateQtControl(QWidget* parent); + QWidget* GetQtControl() const; + void Init(berry::IWorkbench::Pointer); + void PerformCancel(); + bool PerformOk(); + void Update(); + +private slots: + void OnGnuplotButtonClicked(); + void OnGnuplotProcessError(QProcess::ProcessError error); + void OnGnuplotProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); + +private: + berry::IPreferences::Pointer m_Preferences; + QScopedPointer m_Ui; + QWidget* m_Control; + QProcess* m_GnuplotProcess; + QString m_GnuplotPath; +}; + +#endif diff --git a/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.ui b/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.ui new file mode 100644 index 0000000000..ace9d303c8 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.ext/src/internal/QmitkExternalProgramsPreferencePage.ui @@ -0,0 +1,65 @@ + + + QmitkExternalProgramsPreferencePage + + + + 0 + 0 + 400 + 300 + + + + External Programs + + + + + + + + Gnuplot &executable: + + + Qt::PlainText + + + gnuplotButton + + + + + + + true + + + + + + + ... + + + + + + + + + Qt::Vertical + + + + 20 + 248 + + + + + + + + +