Prechádzať zdrojové kódy

Use ctkCmdLineModuleFutureWatcher output reporting in the explorer app.

Sascha Zelzer 12 rokov pred
rodič
commit
b09af0bf81

+ 2 - 0
Applications/ctkCommandLineModuleExplorer/CMakeLists.txt

@@ -11,6 +11,7 @@ set(KIT_SRCS
   ctkCmdLineModuleExplorerMainWindow.h
   ctkCmdLineModuleExplorerMainWindow.cpp
   ctkCmdLineModuleExplorerModulesSettings.cpp
+  ctkCmdLineModuleExplorerOutputText.cpp
   ctkCmdLineModuleExplorerProgressWidget.cpp
   ctkCmdLineModuleExplorerTabList.cpp
   ctkCmdLineModuleExplorerTreeWidget.cpp
@@ -20,6 +21,7 @@ set(KIT_SRCS
 set(KIT_MOC_SRCS
   ctkCmdLineModuleExplorerMainWindow.h
   ctkCmdLineModuleExplorerModulesSettings.h
+  ctkCmdLineModuleExplorerOutputText.h
   ctkCmdLineModuleExplorerProgressWidget.h
   ctkCmdLineModuleExplorerTabList.h
   ctkCmdLineModuleExplorerTreeWidget.h

+ 3 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.cpp

@@ -92,6 +92,7 @@ ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
   connect(ui->modulesTreeWidget, SIGNAL(moduleFrontendCreated(ctkCmdLineModuleFrontend*)), tabList.data(), SLOT(addTab(ctkCmdLineModuleFrontend*)));
   // React to tab-changes
   connect(tabList.data(), SIGNAL(tabActivated(ctkCmdLineModuleFrontend*)), SLOT(moduleTabActivated(ctkCmdLineModuleFrontend*)));
+  connect(tabList.data(), SIGNAL(tabClosed(ctkCmdLineModuleFrontend*)), ui->outputText, SLOT(frontendRemoved(ctkCmdLineModuleFrontend*)));
 
   // Listen to future events for the currently active tab
 
@@ -290,6 +291,7 @@ void ctkCLModuleExplorerMainWindow::moduleTabActivated(ctkCmdLineModuleFrontend
     ui->actionPause->setEnabled(false);
     ui->actionCancel->setEnabled(false);
     ui->actionReset->setEnabled(false);
+    ui->outputText->setActiveFrontend(NULL);
     currentFutureWatcher.setFuture(ctkCmdLineModuleFuture());
   }
   else
@@ -299,6 +301,7 @@ void ctkCLModuleExplorerMainWindow::moduleTabActivated(ctkCmdLineModuleFrontend
     ui->actionPause->setChecked(module->isPaused());
     ui->actionCancel->setEnabled(module->future().canCancel() && module->isRunning());
     ui->actionReset->setEnabled(true);
+    ui->outputText->setActiveFrontend(module);
     currentFutureWatcher.setFuture(module->future());
   }
 }

+ 1 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.h

@@ -90,6 +90,7 @@ private:
 
   QTimer pollPauseTimer;
   QFutureWatcher<ctkCmdLineModuleResult> currentFutureWatcher;
+  QHash<ctkCmdLineModuleFrontend*, QByteArray> frontendToOutputMap;
 
   ctkCmdLineModuleDirectoryWatcher directoryWatcher;
 

+ 33 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.ui

@@ -159,6 +159,34 @@
     </layout>
    </widget>
   </widget>
+  <widget class="QDockWidget" name="dockWidget_3">
+   <property name="features">
+    <set>QDockWidget::DockWidgetFloatable|QDockWidget::DockWidgetMovable</set>
+   </property>
+   <property name="allowedAreas">
+    <set>Qt::BottomDockWidgetArea|Qt::RightDockWidgetArea|Qt::TopDockWidgetArea</set>
+   </property>
+   <property name="windowTitle">
+    <string>Output</string>
+   </property>
+   <attribute name="dockWidgetArea">
+    <number>8</number>
+   </attribute>
+   <widget class="QWidget" name="dockWidgetContents_3">
+    <layout class="QVBoxLayout" name="verticalLayout_3">
+     <property name="margin">
+      <number>0</number>
+     </property>
+     <item>
+      <widget class="ctkCmdLineModuleExplorerOutputText" name="outputText">
+       <property name="readOnly">
+        <bool>true</bool>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </widget>
+  </widget>
   <action name="actionRun">
    <property name="icon">
     <iconset resource="resources/ctkCmdLineModuleExplorer.qrc">
@@ -243,6 +271,11 @@
    <extends>QTreeWidget</extends>
    <header>ctkCmdLineModuleExplorerTreeWidget.h</header>
   </customwidget>
+  <customwidget>
+   <class>ctkCmdLineModuleExplorerOutputText</class>
+   <extends>QTextEdit</extends>
+   <header>ctkCmdLineModuleExplorerOutputText.h</header>
+  </customwidget>
  </customwidgets>
  <resources>
   <include location="resources/ctkCmdLineModuleExplorer.qrc"/>

+ 129 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerOutputText.cpp

@@ -0,0 +1,129 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  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.apache.org/licenses/LICENSE-2.0
+
+  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.
+
+=============================================================================*/
+
+#include "ctkCmdLineModuleExplorerOutputText.h"
+
+#include "ctkCmdLineModuleFrontend.h"
+#include "ctkCmdLineModuleFuture.h"
+#include "ctkCmdLineModuleFutureWatcher.h"
+
+ctkCmdLineModuleExplorerOutputText::ctkCmdLineModuleExplorerOutputText(QWidget* parent)
+  : QTextEdit(parent)
+  , CurrentWatcher(NULL)
+  , CurrentFrontend(NULL)
+{
+}
+
+ctkCmdLineModuleExplorerOutputText::~ctkCmdLineModuleExplorerOutputText()
+{
+  qDeleteAll(this->FrontendToWatcherMap);
+}
+
+void ctkCmdLineModuleExplorerOutputText::setActiveFrontend(ctkCmdLineModuleFrontend* moduleFrontend)
+{
+  if (this->CurrentFrontend == moduleFrontend) return;
+
+  if (this->CurrentFrontend)
+  {
+    if (this->CurrentWatcher)
+    {
+      this->CurrentWatcher->disconnect();
+    }
+    this->CurrentFrontend->disconnect();
+
+    // save the current output text
+    this->FrontendToOutputMap[this->CurrentFrontend] = this->toHtml();
+    this->clear();
+  }
+
+  this->CurrentFrontend = moduleFrontend;
+  if (moduleFrontend)
+  {
+    // restore previous content
+    this->setHtml(this->FrontendToOutputMap[moduleFrontend]);
+    QTextCursor endCursor = this->textCursor();
+    endCursor.movePosition(QTextCursor::End);
+    this->setTextCursor(endCursor);
+
+    this->CurrentWatcher = FrontendToWatcherMap[moduleFrontend];
+    if (this->CurrentWatcher == NULL)
+    {
+      this->CurrentWatcher = new ctkCmdLineModuleFutureWatcher;
+      this->FrontendToWatcherMap[moduleFrontend] = this->CurrentWatcher;
+    }
+
+    connect(this->CurrentFrontend, SIGNAL(started()), SLOT(frontendStarted()));
+
+    connect(this->CurrentWatcher, SIGNAL(outputDataReady()), SLOT(outputDataReady()));
+    connect(this->CurrentWatcher, SIGNAL(errorDataReady()), SLOT(errorDataReady()));
+
+    this->CurrentWatcher->setFuture(moduleFrontend->future());
+
+    // if the frontend is already finished get any output we have not yet fetched
+    if (moduleFrontend->future().isFinished())
+    {
+      this->outputDataReady();
+      this->errorDataReady();
+    }
+  }
+  else
+  {
+    if (this->CurrentWatcher)
+    {
+      this->CurrentWatcher->disconnect();
+      this->CurrentWatcher = NULL;
+    }
+    if (this->CurrentFrontend)
+    {
+      this->CurrentFrontend->disconnect();
+      this->CurrentFrontend = NULL;
+    }
+    this->clear();
+  }
+}
+
+void ctkCmdLineModuleExplorerOutputText::frontendRemoved(ctkCmdLineModuleFrontend *frontend)
+{
+  delete this->FrontendToWatcherMap[frontend];
+  this->FrontendToWatcherMap.remove(frontend);
+  this->FrontendToOutputMap.remove(frontend);
+}
+
+void ctkCmdLineModuleExplorerOutputText::frontendStarted()
+{
+  this->clear();
+  this->FrontendToOutputMap[this->CurrentFrontend].clear();
+  this->CurrentWatcher->setFuture(this->CurrentFrontend->future());
+}
+
+void ctkCmdLineModuleExplorerOutputText::outputDataReady()
+{
+  QByteArray newOutput = this->CurrentWatcher->readPendingOutputData();
+  this->setTextColor(QColor(Qt::black));
+  this->insertPlainText(newOutput.data());
+}
+
+void ctkCmdLineModuleExplorerOutputText::errorDataReady()
+{
+  QByteArray newOutput = this->CurrentWatcher->readPendingErrorData();
+  this->setTextColor(QColor(Qt::darkRed));
+  this->insertPlainText(newOutput.data());
+}

+ 61 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerOutputText.h

@@ -0,0 +1,61 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  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.apache.org/licenses/LICENSE-2.0
+
+  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 CTKCMDLINEMODULEEXPLOREROUTPUTTEXT_H
+#define CTKCMDLINEMODULEEXPLOREROUTPUTTEXT_H
+
+
+#include <QTextEdit>
+
+class ctkCmdLineModuleFrontend;
+class ctkCmdLineModuleFutureWatcher;
+
+class ctkCmdLineModuleExplorerOutputText : public QTextEdit
+{
+  Q_OBJECT
+
+public:
+
+  ctkCmdLineModuleExplorerOutputText(QWidget* parent = 0);
+  ~ctkCmdLineModuleExplorerOutputText();
+
+public Q_SLOTS:
+
+  void setActiveFrontend(ctkCmdLineModuleFrontend* frontend);
+
+  void frontendRemoved(ctkCmdLineModuleFrontend* frontend);
+
+private Q_SLOTS:
+
+  void frontendStarted();
+
+  void outputDataReady();
+  void errorDataReady();
+
+private:
+
+  ctkCmdLineModuleFutureWatcher* CurrentWatcher;
+  ctkCmdLineModuleFrontend* CurrentFrontend;
+  QHash<ctkCmdLineModuleFrontend*,ctkCmdLineModuleFutureWatcher*> FrontendToWatcherMap;
+  QHash<ctkCmdLineModuleFrontend*,QString> FrontendToOutputMap;
+};
+
+#endif // CTKCMDLINEMODULEEXPLOREROUTPUTTEXT_H

+ 1 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerTabList.cpp

@@ -116,6 +116,7 @@ void ctkCmdLineModuleExplorerTabList::tabCloseRequested(int index)
   {
     this->TabIndexToFrontend.removeAt(index);
     this->TabWidget->removeTab(index);
+    emit this->tabClosed(frontend);
     delete frontend;
   }
 }

+ 1 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerTabList.h

@@ -51,6 +51,7 @@ public:
   Q_SLOT void addTab(ctkCmdLineModuleFrontend* frontend);
 
   Q_SIGNAL void tabActivated(ctkCmdLineModuleFrontend* module);
+  Q_SIGNAL void tabClosed(ctkCmdLineModuleFrontend* module);
 
 private: