ctkXMLEventSource.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // QT includes
  15. #include <QApplication>
  16. #include <QCoreApplication>
  17. #include <QDebug>
  18. #include <QFile>
  19. #include <QMap>
  20. #include <QMessageBox>
  21. #include <QVariant>
  22. // CTKQtTesting includes
  23. #include "ctkXMLEventSource.h"
  24. //-----------------------------------------------------------------------------
  25. // ctkXMLEventSource methods
  26. //-----------------------------------------------------------------------------
  27. ctkXMLEventSource::ctkXMLEventSource(QObject* p)
  28. : Superclass(p)
  29. {
  30. this->Automatic = false;
  31. this->XMLStream = NULL;
  32. this->TestUtility = qobject_cast<pqTestUtility*>(p);
  33. }
  34. //-----------------------------------------------------------------------------
  35. ctkXMLEventSource::~ctkXMLEventSource()
  36. {
  37. delete this->XMLStream;
  38. this->TestUtility = 0;
  39. }
  40. //-----------------------------------------------------------------------------
  41. void ctkXMLEventSource::setRestoreSettingsAuto(bool value)
  42. {
  43. this->Automatic = value;
  44. }
  45. //-----------------------------------------------------------------------------
  46. bool ctkXMLEventSource::restoreSettingsAuto() const
  47. {
  48. return this->Automatic;
  49. }
  50. //-----------------------------------------------------------------------------
  51. bool ctkXMLEventSource::setContent(const QString& xmlfilename)
  52. {
  53. delete this->XMLStream;
  54. this->XMLStream = NULL;
  55. QFile xml(xmlfilename);
  56. if (!xml.open(QIODevice::ReadOnly))
  57. {
  58. qDebug() << "Failed to load " << xmlfilename;
  59. return false;
  60. }
  61. QByteArray data = xml.readAll();
  62. this->XMLStream = new QXmlStreamReader(data);
  63. /* This checked for valid event objects, but also caused the first event
  64. * to get dropped. Commenting this out in the example. If you wish to report
  65. * empty XML test files a flag indicating whether valid events were found is
  66. * probably the best way to go.
  67. while (!this->XMLStream->atEnd())
  68. {
  69. QXmlStreamReader::TokenType token = this->XMLStream->readNext();
  70. if (token == QXmlStreamReader::StartElement)
  71. {
  72. if (this->XMLStream->name() == "event")
  73. {
  74. break;
  75. }
  76. }
  77. } */
  78. if (this->XMLStream->atEnd())
  79. {
  80. qDebug() << "Invalid xml" << endl;
  81. return false;
  82. }
  83. if(this->settingsRecorded())
  84. {
  85. qDebug() << "settings recorded";
  86. this->OldSettings = this->recoverSettingsFromXML();
  87. if(!this->settingsUpToData())
  88. {
  89. qDebug() << "restoring ...";
  90. this->restoreApplicationSettings();
  91. }
  92. }
  93. return true;
  94. }
  95. //-----------------------------------------------------------------------------
  96. int ctkXMLEventSource::getNextEvent(QString& widget, QString& command, QString&arguments)
  97. {
  98. if (this->XMLStream->atEnd())
  99. {
  100. return DONE;
  101. }
  102. while (!this->XMLStream->atEnd())
  103. {
  104. QXmlStreamReader::TokenType token = this->XMLStream->readNext();
  105. if (token == QXmlStreamReader::StartElement)
  106. {
  107. if (this->XMLStream->name() == "event")
  108. {
  109. break;
  110. }
  111. }
  112. }
  113. if (this->XMLStream->atEnd())
  114. {
  115. return DONE;
  116. }
  117. widget = this->XMLStream->attributes().value("widget").toString();
  118. command = this->XMLStream->attributes().value("command").toString();
  119. arguments = this->XMLStream->attributes().value("arguments").toString();
  120. return SUCCESS;
  121. }
  122. //-----------------------------------------------------------------------------
  123. bool ctkXMLEventSource::settingsRecorded()
  124. {
  125. while(this->XMLStream->name() != "settings" && this->XMLStream->name() != "events")
  126. {
  127. this->XMLStream->readNext();
  128. }
  129. return (this->XMLStream->name() == "settings") ? true : false;
  130. }
  131. //-----------------------------------------------------------------------------
  132. bool ctkXMLEventSource::settingsUpToData()
  133. {
  134. QMainWindow* window = this->mainWindow();
  135. bool result = true;
  136. QMap<QObject*, QString> states = this->TestUtility->objectStateProperty();
  137. result &= (this->OldSettings.value("geometry") == QString(window->saveGeometry().toHex()));
  138. result &= (this->OldSettings.value("state") == QString(window->saveState().toHex()));
  139. QMap<QObject*, QString>::iterator iter;
  140. for(iter = states.begin() ; iter!=states.end() ; ++iter)
  141. {
  142. result &= (this->OldSettings.value(iter.value()) ==
  143. iter.key()->property(iter.value().toLatin1()).toString());
  144. }
  145. return result;
  146. }
  147. //-----------------------------------------------------------------------------
  148. bool ctkXMLEventSource::restoreApplicationSettings()
  149. {
  150. QMainWindow* window = this->mainWindow();
  151. bool result = false;
  152. QMap<QObject*, QString> states = this->TestUtility->objectStateProperty();
  153. if (!this->Automatic)
  154. {
  155. if (QMessageBox::No == QMessageBox::warning(0, tr("Playback ..."),
  156. tr("The settings are differents from the record Settings.\n"
  157. "Do you want to restore the settings?"),
  158. QMessageBox::Yes | QMessageBox::No,
  159. QMessageBox::Yes))
  160. {
  161. return false;
  162. }
  163. result = window->restoreState(
  164. QByteArray::fromHex(QByteArray(this->OldSettings.value("state").toLocal8Bit().constData())));
  165. result = window->restoreGeometry(
  166. QByteArray::fromHex(QByteArray(this->OldSettings.value("geometry").toLocal8Bit().constData())));
  167. QMap<QObject*, QString>::iterator iter;
  168. for(iter = states.begin() ; iter!=states.end() ; ++iter)
  169. {
  170. iter.key()->setProperty(iter.value().toLatin1(),
  171. QVariant(this->OldSettings.value(iter.value())));
  172. }
  173. }
  174. return result;
  175. }
  176. //-----------------------------------------------------------------------------
  177. QMap<QString, QString> ctkXMLEventSource::recoverSettingsFromXML()
  178. {
  179. // Recover the settings
  180. QMap<QString, QString> settings;
  181. while (this->XMLStream->tokenType() != QXmlStreamReader::EndElement ||
  182. this->XMLStream->name() != "settings")
  183. {
  184. this->XMLStream->readNext();
  185. if (!this->XMLStream->name().isEmpty() &&
  186. this->XMLStream->tokenType() == QXmlStreamReader::StartElement)
  187. {
  188. settings.insert(this->XMLStream->name().toString(),
  189. this->XMLStream->attributes().value("arguments").toString());
  190. }
  191. }
  192. return settings;
  193. }
  194. //-----------------------------------------------------------------------------
  195. QMainWindow* ctkXMLEventSource::mainWindow()
  196. {
  197. QMainWindow* window = NULL;
  198. foreach(QWidget * widget, QApplication::topLevelWidgets())
  199. {
  200. window = qobject_cast<QMainWindow*>(widget);
  201. if (window)
  202. {
  203. return window;
  204. }
  205. }
  206. return 0;
  207. }