ctkXMLEventSource.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <QtXmlPatterns/QXmlSchema>
  22. #include <QtXmlPatterns/QXmlSchemaValidator>
  23. #include <QVariant>
  24. // CTKQtTesting includes
  25. #include "ctkQtTestingUtility.h"
  26. #include "ctkXMLEventSource.h"
  27. //-----------------------------------------------------------------------------
  28. // ctkXMLEventSource methods
  29. //-----------------------------------------------------------------------------
  30. ctkXMLEventSource::ctkXMLEventSource(QObject* testUtility)
  31. : Superclass(testUtility)
  32. {
  33. this->Automatic = false;
  34. this->XMLStream = NULL;
  35. this->TestUtility = qobject_cast<pqTestUtility*>(testUtility);
  36. Q_ASSERT(this->TestUtility);
  37. }
  38. //-----------------------------------------------------------------------------
  39. ctkXMLEventSource::~ctkXMLEventSource()
  40. {
  41. delete this->XMLStream;
  42. this->TestUtility = 0;
  43. }
  44. //-----------------------------------------------------------------------------
  45. void ctkXMLEventSource::setRestoreSettingsAuto(bool value)
  46. {
  47. this->Automatic = value;
  48. }
  49. //-----------------------------------------------------------------------------
  50. bool ctkXMLEventSource::restoreSettingsAuto() const
  51. {
  52. return this->Automatic;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void ctkXMLEventSource::setContent(const QString& xmlfilename)
  56. {
  57. delete this->XMLStream;
  58. this->XMLStream = NULL;
  59. QFile xml(xmlfilename);
  60. if (!xml.open(QIODevice::ReadOnly))
  61. {
  62. qCritical() << "Failed to load " << xmlfilename;
  63. return;
  64. }
  65. // Check if the xml file is valid
  66. QXmlSchema xmlSchema;
  67. if (!xmlSchema.load(QUrl::fromLocalFile(":/XML/XMLDescription.xsd")) ||
  68. !xmlSchema.isValid())
  69. {
  70. qCritical() << "Xml cannot be check.";
  71. return;
  72. }
  73. QXmlSchemaValidator validator(xmlSchema);
  74. if(!validator.validate(&xml, QUrl::fromLocalFile(xml.fileName())))
  75. {
  76. qCritical() << xmlfilename << "invalid xml file for qtTesting !";
  77. return;
  78. }
  79. xml.reset();
  80. QByteArray data = xml.readAll();
  81. this->XMLStream = new QXmlStreamReader(data);
  82. if(this->settingsRecorded())
  83. {
  84. this->OldSettings = this->recoverSettingsFromXML();
  85. if(!this->settingsUpToData())
  86. {
  87. this->restoreApplicationSettings();
  88. }
  89. }
  90. return;
  91. }
  92. //-----------------------------------------------------------------------------
  93. int ctkXMLEventSource::getNextEvent(QString& widget, QString& command,
  94. QString& arguments, int& eventType)
  95. {
  96. if (!this->XMLStream)
  97. {
  98. return EXIT_FAILURE;
  99. }
  100. if (this->XMLStream->atEnd())
  101. {
  102. return DONE;
  103. }
  104. while (!this->XMLStream->atEnd())
  105. {
  106. QXmlStreamReader::TokenType token = this->XMLStream->readNext();
  107. if (token == QXmlStreamReader::StartElement)
  108. {
  109. if (this->XMLStream->name() == "event")
  110. {
  111. break;
  112. }
  113. }
  114. }
  115. if (this->XMLStream->atEnd())
  116. {
  117. return DONE;
  118. }
  119. const QXmlStreamAttributes attributes = this->XMLStream->attributes();
  120. widget = attributes.value("widget").toString();
  121. command = attributes.value("command").toString();
  122. arguments = attributes.value("arguments").toString();
  123. eventType = attributes.hasAttribute("type") ?
  124. ctkQtTestingUtility::eventTypeFromString(this->XMLStream->attributes().value("type").toString()) :
  125. pqEventTypes::ACTION_EVENT;
  126. return SUCCESS;
  127. }
  128. //-----------------------------------------------------------------------------
  129. bool ctkXMLEventSource::settingsRecorded()
  130. {
  131. while(this->XMLStream->name() != "settings" && this->XMLStream->name() != "events")
  132. {
  133. this->XMLStream->readNext();
  134. }
  135. return (this->XMLStream->name() == "settings") ? true : false;
  136. }
  137. //-----------------------------------------------------------------------------
  138. bool ctkXMLEventSource::settingsUpToData()
  139. {
  140. QMainWindow* window = this->mainWindow();
  141. bool result = true;
  142. QMap<QObject*, QStringList> states = this->TestUtility->objectStateProperty();
  143. result &= (this->OldSettings.value("geometry") == QString(window->saveGeometry().toHex()));
  144. result &= (this->OldSettings.value("state") == QString(window->saveState().toHex()));
  145. QMap<QObject*, QStringList>::iterator iter;
  146. for(iter = states.begin() ; iter!=states.end() ; ++iter)
  147. {
  148. foreach(QString property, iter.value())
  149. {
  150. result &= (this->OldSettings.value(QString("appsetting_%1").arg(property)) ==
  151. iter.key()->property(property.toLatin1()).toString());
  152. }
  153. }
  154. return result;
  155. }
  156. //-----------------------------------------------------------------------------
  157. bool ctkXMLEventSource::restoreApplicationSettings()
  158. {
  159. QMainWindow* window = this->mainWindow();
  160. bool result = false;
  161. QMap<QObject*, QStringList> states = this->TestUtility->objectStateProperty();
  162. qDebug() << "restoreApplicationSetting" << states;
  163. if (!this->Automatic)
  164. {
  165. if (QMessageBox::No == QMessageBox::warning(0, tr("Playback ..."),
  166. tr("The settings are differents from the record Settings.\n"
  167. "Do you want to restore the settings?"),
  168. QMessageBox::Yes | QMessageBox::No,
  169. QMessageBox::Yes))
  170. {
  171. return false;
  172. }
  173. }
  174. result = window->restoreState(
  175. QByteArray::fromHex(QByteArray(this->OldSettings.value("state").toLocal8Bit().constData())));
  176. result = window->restoreGeometry(
  177. QByteArray::fromHex(QByteArray(this->OldSettings.value("geometry").toLocal8Bit().constData())));
  178. QMap<QObject*, QStringList>::iterator iter;
  179. for(iter = states.begin() ; iter!=states.end() ; ++iter)
  180. {
  181. foreach(QString property, iter.value())
  182. {
  183. iter.key()->setProperty(property.toLatin1(),
  184. QVariant(this->OldSettings.value(
  185. QString("appsetting_%1").arg(property))));
  186. }
  187. }
  188. return result;
  189. }
  190. //-----------------------------------------------------------------------------
  191. QMap<QString, QString> ctkXMLEventSource::recoverSettingsFromXML()
  192. {
  193. // Recover the settings
  194. QMap<QString, QString> settings;
  195. while (this->XMLStream->tokenType() != QXmlStreamReader::EndElement ||
  196. this->XMLStream->name() != "settings")
  197. {
  198. this->XMLStream->readNext();
  199. if (!this->XMLStream->name().isEmpty() &&
  200. this->XMLStream->tokenType() == QXmlStreamReader::StartElement)
  201. {
  202. QString key = this->XMLStream->name().toString();
  203. // There might be multiple appsetting elements, ensure key is unique
  204. if (this->XMLStream->name().toString() == "appsetting")
  205. {
  206. key += "_" + this->XMLStream->attributes().value("command").toString();
  207. }
  208. settings[key].append(
  209. this->XMLStream->attributes().value("arguments").toString());
  210. }
  211. }
  212. return settings;
  213. }
  214. //-----------------------------------------------------------------------------
  215. QMainWindow* ctkXMLEventSource::mainWindow()
  216. {
  217. QMainWindow* window = NULL;
  218. foreach(QWidget * widget, QApplication::topLevelWidgets())
  219. {
  220. window = qobject_cast<QMainWindow*>(widget);
  221. if (window)
  222. {
  223. return window;
  224. }
  225. }
  226. return 0;
  227. }