Kaynağa Gözat

First implementation of XML progress report parsing and watching.

Sascha Zelzer 13 yıl önce
ebeveyn
işleme
48a295827a

+ 5 - 0
Libs/CommandLineModules/Core/CMakeLists.txt

@@ -25,6 +25,7 @@ set(KIT_SRCS
   ctkCmdLineModuleParameterGroupPrivate.h
   ctkCmdLineModuleParameterParsers_p.h
   ctkCmdLineModuleProcessTask.cpp
+  ctkCmdLineModuleXmlProgressWatcher.cpp
   ctkCmdLineModuleReference.cpp
   ctkCmdLineModuleReferencePrivate.cpp
   ctkCmdLineModuleRunException.cpp
@@ -43,6 +44,10 @@ set(KIT_MOC_SRCS
   ctkCmdLineModuleProcessTask.h
 )
 
+qt4_wrap_cpp(_dummy ctkCmdLineModuleXmlProgressWatcher.h)
+set_source_files_properties(ctkCmdLineModuleXmlProgressWatcher.cpp
+  PROPERTIES OBJECT_DEPENDS ${_dummy})
+
 # UI files
 set(KIT_UI_FORMS
 )

+ 1 - 0
Libs/CommandLineModules/Core/Testing/Cpp/CMakeLists.txt

@@ -2,6 +2,7 @@ set(KIT ${PROJECT_NAME})
 
 create_test_sourcelist(Tests ${KIT}CppTests.cpp
   ctkCmdLineModuleFutureTest.cpp
+  ctkCmdLineModuleXmlProgressWatcherTest.cpp
   )
 
 set(TestsToRun ${Tests})

+ 62 - 0
Libs/CommandLineModules/Core/Testing/Cpp/ctkCmdLineModuleXmlProgressWatcherTest.cpp

@@ -0,0 +1,62 @@
+/*=============================================================================
+  
+  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 <ctkCmdLineModuleXmlProgressWatcher.h>
+
+#include "ctkCmdLineModuleSignalTester.h"
+
+#include <QCoreApplication>
+#include <QBuffer>
+#include <QDataStream>
+
+#include <cstdlib>
+
+
+int ctkCmdLineModuleXmlProgressWatcherTest(int argc, char* argv[])
+{
+  QCoreApplication app(argc, argv);
+
+  QByteArray input;
+  QBuffer buffer(&input);
+  buffer.open(QIODevice::ReadWrite);
+
+  QByteArray ba = "<filter-start><filter-name>My Filter</filter-name><filter-comment>"
+      "Awesome filter</filter-comment></filter-start>"
+      "<filter-progress>0.3</filter-progress>"
+      "<filter-progress>0.6</filter-progress>"
+      "<filter-progress>0.9</filter-progress>"
+      "<filter-end><filter-name>My Filter</filter-name><filter-time>23</filter-time></filter-end>";
+
+  ctkCmdLineModuleXmlProgressWatcher progressWatcher(&buffer);
+
+  ctkCmdLineModuleSignalTester signalTester;
+  signalTester.connect(&progressWatcher, SIGNAL(filterStarted(QString,QString)), &signalTester, SLOT(filterStarted(QString,QString)));
+  signalTester.connect(&progressWatcher, SIGNAL(filterProgress(float)), &signalTester, SLOT(filterProgress(float)));
+  signalTester.connect(&progressWatcher, SIGNAL(filterFinished(QString)), &signalTester, SLOT(filterFinished(QString)));
+
+  QDataStream xmlOut(&buffer);
+  xmlOut << ba;
+
+  QCoreApplication::processEvents();
+
+  return EXIT_SUCCESS;
+}

+ 165 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleXmlProgressWatcher.cpp

@@ -0,0 +1,165 @@
+/*===================================================================
+  
+BlueBerry Platform
+
+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 "ctkCmdLineModuleXmlProgressWatcher.h"
+
+#include <QIODevice>
+#include <QXmlStreamReader>
+
+#include <QDebug>
+
+namespace {
+
+static QString FILTER_START = "filter-start";
+static QString FILTER_NAME = "filter-name";
+static QString FILTER_COMMENT = "filter-comment";
+static QString FILTER_PROGRESS = "filter-progress";
+static QString FILTER_END = "filter-end";
+
+}
+
+class ctkCmdLineModuleXmlProgressWatcherPrivate
+{
+public:
+
+  ctkCmdLineModuleXmlProgressWatcherPrivate(QIODevice* input, ctkCmdLineModuleXmlProgressWatcher* qq)
+    : input(input), q(qq), error(false)
+  {}
+
+  void _q_readyRead()
+  {
+    QByteArray ba = input->readAll();
+    qDebug() << input->pos() << " [" << input->bytesAvailable() << "]:" << ba;
+    //reader.addData(ba);
+    //parseProgressXml();
+  }
+
+  void parseProgressXml()
+  {
+    QXmlStreamReader::TokenType type = reader.readNext();
+    while(type != QXmlStreamReader::Invalid)
+    {
+      switch(type)
+      {
+      case QXmlStreamReader::NoToken: break;
+      case QXmlStreamReader::StartElement:
+      {
+        QStringRef name = reader.name();
+        if (name.compare(FILTER_START, Qt::CaseInsensitive) == 0)
+        {
+          stack.push_back(FILTER_START);
+          currentName.clear();
+          currentComment.clear();
+        }
+        else if (name.compare(FILTER_NAME, Qt::CaseInsensitive) == 0)
+        {
+          if (stack.back() == FILTER_START || stack.back() == FILTER_END)
+          {
+            currentName = reader.name().toString().trimmed();
+          }
+        }
+        else if (name.compare(FILTER_COMMENT, Qt::CaseInsensitive) == 0)
+        {
+          if (stack.back() == FILTER_START)
+          {
+            currentComment = reader.name().toString().trimmed();
+          }
+        }
+        else if (name.compare(FILTER_PROGRESS, Qt::CaseInsensitive) == 0)
+        {
+          if (!stack.empty())
+          {
+            if (!error)
+            {
+              emit q->filterXmlError(QString("\"%1\" must be a top-level element, found at line %2.")
+                                     .arg(FILTER_PROGRESS).arg(reader.lineNumber()));
+            }
+            continue;
+          }
+          emit q->filterProgress(reader.text().toString().toFloat());
+        }
+        type = reader.readNext();
+        break;
+      }
+      case QXmlStreamReader::EndElement:
+      {
+        QStringRef name = reader.name();
+        if (name.compare(FILTER_START, Qt::CaseInsensitive) == 0)
+        {
+          if (stack.back() != FILTER_START)
+          {
+            if (!error)
+            {
+              emit q->filterXmlError(QString("Unexpected end tag \"%1\" found at line %2.")
+                                     .arg(FILTER_END).arg(reader.lineNumber()));
+            }
+            continue;
+          }
+          stack.pop_back();
+          emit q->filterStarted(currentName, currentComment);
+        }
+        else if (name.compare(FILTER_END, Qt::CaseInsensitive) == 0)
+        {
+          if (!stack.empty())
+          {
+            if (!error)
+            {
+              emit q->filterXmlError(QString("\"%1\" must be a top-level element, found at line %2.")
+                                     .arg(FILTER_PROGRESS).arg(reader.lineNumber()));
+            }
+            continue;
+          }
+          stack.pop_back();
+          emit q->filterFinished(currentName);
+        }
+        type = reader.readNext();
+        break;
+      }
+      default:
+        type = reader.readNext();
+      }
+    }
+  }
+
+  QIODevice* input;
+  ctkCmdLineModuleXmlProgressWatcher* q;
+  bool error;
+  QXmlStreamReader reader;
+  QList<QString> stack;
+  QString currentName;
+  QString currentComment;
+};
+
+ctkCmdLineModuleXmlProgressWatcher::ctkCmdLineModuleXmlProgressWatcher(QIODevice* input)
+  : d(new ctkCmdLineModuleXmlProgressWatcherPrivate(input, this))
+{
+  if (d->input == NULL) return;
+
+  if (!(d->input->openMode() & QIODevice::ReadOnly))
+  {
+    input->open(QIODevice::ReadOnly);
+  }
+  connect(d->input, SIGNAL(readyRead()), SLOT(_q_readyRead()));
+
+  // start parsing
+  d->_q_readyRead();
+}
+
+ctkCmdLineModuleXmlProgressWatcher::~ctkCmdLineModuleXmlProgressWatcher()
+{
+}
+
+#include "moc_ctkCmdLineModuleXmlProgressWatcher.cxx"

+ 53 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleXmlProgressWatcher.h

@@ -0,0 +1,53 @@
+/*===================================================================
+  
+BlueBerry Platform
+
+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 CTKCMDLINEMODULEXMLPROGRESSWATCHER_H
+#define CTKCMDLINEMODULEXMLPROGRESSWATCHER_H
+
+#include "ctkCommandLineModulesCoreExport.h"
+
+#include <QObject>
+
+class ctkCmdLineModuleXmlProgressWatcherPrivate;
+
+class QIODevice;
+
+class CTK_CMDLINEMODULECORE_EXPORT ctkCmdLineModuleXmlProgressWatcher : public QObject
+{
+  Q_OBJECT
+
+public:
+
+  ctkCmdLineModuleXmlProgressWatcher(QIODevice* input);
+  ~ctkCmdLineModuleXmlProgressWatcher();
+
+Q_SIGNALS:
+
+  void filterStarted(const QString& name, const QString& comment);
+  void filterProgress(float progress);
+  void filterFinished(const QString& name);
+  void filterXmlError(const QString& error);
+
+private:
+
+  friend class ctkCmdLineModuleXmlProgressWatcherPrivate;
+
+  Q_PRIVATE_SLOT(d, void _q_readyRead())
+
+  QScopedPointer<ctkCmdLineModuleXmlProgressWatcherPrivate> d;
+};
+
+#endif // CTKCMDLINEMODULEXMLPROGRESSWATCHER_H