ctkCmdLineModuleXmlProgressWatcher.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*===================================================================
  2. BlueBerry Platform
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics.
  5. All rights reserved.
  6. This software is distributed WITHOUT ANY WARRANTY; without
  7. even the implied warranty of MERCHANTABILITY or FITNESS FOR
  8. A PARTICULAR PURPOSE.
  9. See LICENSE.txt or http://www.mitk.org for details.
  10. ===================================================================*/
  11. #include "ctkCmdLineModuleXmlProgressWatcher.h"
  12. #include <QIODevice>
  13. #include <QXmlStreamReader>
  14. #include <QDebug>
  15. namespace {
  16. static QString FILTER_START = "filter-start";
  17. static QString FILTER_NAME = "filter-name";
  18. static QString FILTER_COMMENT = "filter-comment";
  19. static QString FILTER_PROGRESS = "filter-progress";
  20. static QString FILTER_END = "filter-end";
  21. }
  22. class ctkCmdLineModuleXmlProgressWatcherPrivate
  23. {
  24. public:
  25. ctkCmdLineModuleXmlProgressWatcherPrivate(QIODevice* input, ctkCmdLineModuleXmlProgressWatcher* qq)
  26. : input(input), q(qq), error(false)
  27. {}
  28. void _q_readyRead()
  29. {
  30. QByteArray ba = input->readAll();
  31. qDebug() << input->pos() << " [" << input->bytesAvailable() << "]:" << ba;
  32. //reader.addData(ba);
  33. //parseProgressXml();
  34. }
  35. void parseProgressXml()
  36. {
  37. QXmlStreamReader::TokenType type = reader.readNext();
  38. while(type != QXmlStreamReader::Invalid)
  39. {
  40. switch(type)
  41. {
  42. case QXmlStreamReader::NoToken: break;
  43. case QXmlStreamReader::StartElement:
  44. {
  45. QStringRef name = reader.name();
  46. if (name.compare(FILTER_START, Qt::CaseInsensitive) == 0)
  47. {
  48. stack.push_back(FILTER_START);
  49. currentName.clear();
  50. currentComment.clear();
  51. }
  52. else if (name.compare(FILTER_NAME, Qt::CaseInsensitive) == 0)
  53. {
  54. if (stack.back() == FILTER_START || stack.back() == FILTER_END)
  55. {
  56. currentName = reader.name().toString().trimmed();
  57. }
  58. }
  59. else if (name.compare(FILTER_COMMENT, Qt::CaseInsensitive) == 0)
  60. {
  61. if (stack.back() == FILTER_START)
  62. {
  63. currentComment = reader.name().toString().trimmed();
  64. }
  65. }
  66. else if (name.compare(FILTER_PROGRESS, Qt::CaseInsensitive) == 0)
  67. {
  68. if (!stack.empty())
  69. {
  70. if (!error)
  71. {
  72. emit q->filterXmlError(QString("\"%1\" must be a top-level element, found at line %2.")
  73. .arg(FILTER_PROGRESS).arg(reader.lineNumber()));
  74. }
  75. continue;
  76. }
  77. emit q->filterProgress(reader.text().toString().toFloat());
  78. }
  79. type = reader.readNext();
  80. break;
  81. }
  82. case QXmlStreamReader::EndElement:
  83. {
  84. QStringRef name = reader.name();
  85. if (name.compare(FILTER_START, Qt::CaseInsensitive) == 0)
  86. {
  87. if (stack.back() != FILTER_START)
  88. {
  89. if (!error)
  90. {
  91. emit q->filterXmlError(QString("Unexpected end tag \"%1\" found at line %2.")
  92. .arg(FILTER_END).arg(reader.lineNumber()));
  93. }
  94. continue;
  95. }
  96. stack.pop_back();
  97. emit q->filterStarted(currentName, currentComment);
  98. }
  99. else if (name.compare(FILTER_END, Qt::CaseInsensitive) == 0)
  100. {
  101. if (!stack.empty())
  102. {
  103. if (!error)
  104. {
  105. emit q->filterXmlError(QString("\"%1\" must be a top-level element, found at line %2.")
  106. .arg(FILTER_PROGRESS).arg(reader.lineNumber()));
  107. }
  108. continue;
  109. }
  110. stack.pop_back();
  111. emit q->filterFinished(currentName);
  112. }
  113. type = reader.readNext();
  114. break;
  115. }
  116. default:
  117. type = reader.readNext();
  118. }
  119. }
  120. }
  121. QIODevice* input;
  122. ctkCmdLineModuleXmlProgressWatcher* q;
  123. bool error;
  124. QXmlStreamReader reader;
  125. QList<QString> stack;
  126. QString currentName;
  127. QString currentComment;
  128. };
  129. ctkCmdLineModuleXmlProgressWatcher::ctkCmdLineModuleXmlProgressWatcher(QIODevice* input)
  130. : d(new ctkCmdLineModuleXmlProgressWatcherPrivate(input, this))
  131. {
  132. if (d->input == NULL) return;
  133. if (!(d->input->openMode() & QIODevice::ReadOnly))
  134. {
  135. input->open(QIODevice::ReadOnly);
  136. }
  137. connect(d->input, SIGNAL(readyRead()), SLOT(_q_readyRead()));
  138. // start parsing
  139. d->_q_readyRead();
  140. }
  141. ctkCmdLineModuleXmlProgressWatcher::~ctkCmdLineModuleXmlProgressWatcher()
  142. {
  143. }
  144. #include "moc_ctkCmdLineModuleXmlProgressWatcher.cxx"