ctkXMLEventObserver.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <QLayout>
  19. #include <QMainWindow>
  20. #include <QVariant>
  21. // CTKQtTesting includes
  22. #include "ctkQtTestingUtility.h"
  23. #include "ctkXMLEventObserver.h"
  24. //-----------------------------------------------------------------------------
  25. // ctkXMLEventObserver methods
  26. //-----------------------------------------------------------------------------
  27. ctkXMLEventObserver::ctkXMLEventObserver(QObject* p)
  28. : pqEventObserver(p)
  29. {
  30. this->XMLStream = NULL;
  31. this->TestUtility = qobject_cast<pqTestUtility*>(p);
  32. Q_ASSERT(this->TestUtility);
  33. }
  34. //-----------------------------------------------------------------------------
  35. ctkXMLEventObserver::~ctkXMLEventObserver()
  36. {
  37. delete this->XMLStream;
  38. this->TestUtility = 0;
  39. }
  40. //-----------------------------------------------------------------------------
  41. void ctkXMLEventObserver::recordApplicationSettings()
  42. {
  43. Q_ASSERT(this->TestUtility);
  44. if (!this->XMLStream)
  45. {
  46. return;
  47. }
  48. this->XMLStream->writeStartElement("settings");
  49. // Informations about the application
  50. this->recordApplicationSetting("name","qApp", "applicationName",
  51. QCoreApplication::applicationName());
  52. this->recordApplicationSetting("version" , "qApp", "applicationVersion",
  53. QCoreApplication::applicationVersion());
  54. // save Geometry and State of the application
  55. QMainWindow* window = NULL;
  56. foreach(QWidget * widget, QApplication::topLevelWidgets())
  57. {
  58. window = qobject_cast<QMainWindow*>(widget);
  59. if (window)
  60. {
  61. this->recordApplicationSetting("geometry" , "MainWindow", "mainWindowGeometry",
  62. QString(window->saveGeometry().toHex()));
  63. this->recordApplicationSetting("state" , "MainWindow", "mainWindowState",
  64. QString(window->saveState().toHex()));
  65. break;
  66. }
  67. }
  68. // Save extra properties from the application
  69. QMap<QObject*, QStringList> states = this->TestUtility->objectStateProperty();
  70. QMap<QObject*, QStringList>::iterator iter;
  71. for(iter = states.begin() ; iter!=states.end() ; ++iter)
  72. {
  73. foreach(QString property, iter.value())
  74. {
  75. this->recordApplicationSetting(
  76. QString("appsetting"),
  77. iter.key()->metaObject()->className(),
  78. property,
  79. iter.key()->property(property.toLatin1()).toString()
  80. );
  81. }
  82. }
  83. this->XMLStream->writeEndElement();
  84. }
  85. //-----------------------------------------------------------------------------
  86. void ctkXMLEventObserver::recordApplicationSetting(const QString &startElement,
  87. const QString &attribute1,
  88. const QString &attribute2,
  89. const QString &attribute3)
  90. {
  91. this->XMLStream->writeStartElement(startElement);
  92. this->XMLStream->writeAttribute("widget", attribute1);
  93. this->XMLStream->writeAttribute("command", attribute2);
  94. this->XMLStream->writeAttribute("arguments", attribute3);
  95. this->XMLStream->writeEndElement();
  96. }
  97. //-----------------------------------------------------------------------------
  98. void ctkXMLEventObserver::setStream(QTextStream* stream)
  99. {
  100. if (this->XMLStream)
  101. {
  102. this->XMLStream->writeEndElement();
  103. this->XMLStream->writeEndElement();
  104. this->XMLStream->writeEndDocument();
  105. delete this->XMLStream;
  106. this->XMLStream = NULL;
  107. }
  108. if (this->Stream)
  109. {
  110. *this->Stream << this->XMLString;
  111. }
  112. this->XMLString = QString();
  113. pqEventObserver::setStream(stream);
  114. if (this->Stream)
  115. {
  116. this->XMLStream = new QXmlStreamWriter(&this->XMLString);
  117. this->XMLStream->setAutoFormatting(true);
  118. this->XMLStream->writeStartDocument();
  119. this->XMLStream->writeStartElement("QtTesting");
  120. this->recordApplicationSettings();
  121. this->XMLStream->writeStartElement("events");
  122. }
  123. }
  124. //-----------------------------------------------------------------------------
  125. void ctkXMLEventObserver::onRecordEvent(const QString& widget,
  126. const QString& command,
  127. const QString& arguments,
  128. const int& eventType)
  129. {
  130. if(this->XMLStream)
  131. {
  132. this->XMLStream->writeStartElement("event");
  133. this->XMLStream->writeAttribute("widget", widget);
  134. this->XMLStream->writeAttribute("command", command);
  135. this->XMLStream->writeAttribute("arguments", arguments);
  136. this->XMLStream->writeAttribute("type", ctkQtTestingUtility::eventTypeToString(eventType));
  137. this->XMLStream->writeEndElement();
  138. if (this->Stream)
  139. {
  140. *this->Stream << this->XMLString;
  141. }
  142. this->XMLString = QString();
  143. emit this->eventRecorded(widget, command, arguments, eventType);
  144. }
  145. }