1 /*=========================================================================
2
3 Library: CTK
4
5 Copyright (c) Kitware Inc.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0.txt
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19 =========================================================================*/
20
21 // QT includes
22 #include <QApplication>
23 #include <QCoreApplication>
24 #include <QDebug>
25 #include <QLayout>
26 #include <QMainWindow>
27 #include <QVariant>
28
29 // CTKQtTesting includes
30 #include "ctkQtTestingUtility.h"
31 #include "ctkXMLEventObserver.h"
32
33 //-----------------------------------------------------------------------------
34 // ctkXMLEventObserver methods
35
36 //-----------------------------------------------------------------------------
ctkXMLEventObserver(QObject * p)37 ctkXMLEventObserver::ctkXMLEventObserver(QObject* p)
38 : pqEventObserver(p)
39 {
40 this->XMLStream = NULL;
41 this->TestUtility = qobject_cast<pqTestUtility*>(p);
42 Q_ASSERT(this->TestUtility);
43 }
44
45 //-----------------------------------------------------------------------------
~ctkXMLEventObserver()46 ctkXMLEventObserver::~ctkXMLEventObserver()
47 {
48 delete this->XMLStream;
49 this->TestUtility = 0;
50 }
51
52 //-----------------------------------------------------------------------------
recordApplicationSettings()53 void ctkXMLEventObserver::recordApplicationSettings()
54 {
55 Q_ASSERT(this->TestUtility);
56 if (!this->XMLStream)
57 {
58 return;
59 }
60 this->XMLStream->writeStartElement("settings");
61
62 // Informations about the application
63 this->recordApplicationSetting("name","qApp", "applicationName",
64 QCoreApplication::applicationName());
65 this->recordApplicationSetting("version" , "qApp", "applicationVersion",
66 QCoreApplication::applicationVersion());
67
68 // save Geometry and State of the application
69 QMainWindow* window = NULL;
70 foreach(QWidget * widget, QApplication::topLevelWidgets())
71 {
72 window = qobject_cast<QMainWindow*>(widget);
73 if (window)
74 {
75 this->recordApplicationSetting("geometry" , "MainWindow", "mainWindowGeometry",
76 QString(window->saveGeometry().toHex()));
77
78 this->recordApplicationSetting("state" , "MainWindow", "mainWindowState",
79 QString(window->saveState().toHex()));
80 break;
81 }
82 }
83
84 // Save extra properties from the application
85 QMap<QObject*, QStringList> states = this->TestUtility->objectStateProperty();
86 QMap<QObject*, QStringList>::iterator iter;
87 for(iter = states.begin() ; iter!=states.end() ; ++iter)
88 {
89 foreach(QString property, iter.value())
90 {
91 this->recordApplicationSetting(
92 QString("appsetting"),
93 iter.key()->metaObject()->className(),
94 property,
95 iter.key()->property(property.toLatin1()).toString()
96 );
97 }
98 }
99
100 this->XMLStream->writeEndElement();
101 }
102
103 //-----------------------------------------------------------------------------
recordApplicationSetting(const QString & startElement,const QString & attribute1,const QString & attribute2,const QString & attribute3)104 void ctkXMLEventObserver::recordApplicationSetting(const QString &startElement,
105 const QString &attribute1,
106 const QString &attribute2,
107 const QString &attribute3)
108 {
109 this->XMLStream->writeStartElement(startElement);
110 this->XMLStream->writeAttribute("widget", attribute1);
111 this->XMLStream->writeAttribute("command", attribute2);
112 this->XMLStream->writeAttribute("arguments", attribute3);
113 this->XMLStream->writeEndElement();
114 }
115
116 //-----------------------------------------------------------------------------
setStream(QTextStream * stream)117 void ctkXMLEventObserver::setStream(QTextStream* stream)
118 {
119 if (this->XMLStream)
120 {
121 this->XMLStream->writeEndElement();
122 this->XMLStream->writeEndElement();
123 this->XMLStream->writeEndDocument();
124 delete this->XMLStream;
125 this->XMLStream = NULL;
126 }
127 if (this->Stream)
128 {
129 *this->Stream << this->XMLString;
130 }
131 this->XMLString = QString();
132 pqEventObserver::setStream(stream);
133 if (this->Stream)
134 {
135 this->XMLStream = new QXmlStreamWriter(&this->XMLString);
136 this->XMLStream->setAutoFormatting(true);
137 this->XMLStream->writeStartDocument();
138 this->XMLStream->writeStartElement("QtTesting");
139 this->recordApplicationSettings();
140 this->XMLStream->writeStartElement("events");
141 }
142 }
143
144 //-----------------------------------------------------------------------------
onRecordEvent(const QString & widget,const QString & command,const QString & arguments,const int & eventType)145 void ctkXMLEventObserver::onRecordEvent(const QString& widget,
146 const QString& command,
147 const QString& arguments,
148 const int& eventType)
149 {
150 if(this->XMLStream)
151 {
152 this->XMLStream->writeStartElement("event");
153 this->XMLStream->writeAttribute("widget", widget);
154 this->XMLStream->writeAttribute("command", command);
155 this->XMLStream->writeAttribute("arguments", arguments);
156 this->XMLStream->writeAttribute("type", ctkQtTestingUtility::eventTypeToString(eventType));
157 this->XMLStream->writeEndElement();
158 if (this->Stream)
159 {
160 *this->Stream << this->XMLString;
161 }
162 this->XMLString = QString();
163 emit this->eventRecorded(widget, command, arguments, eventType);
164 }
165 }
166