Debian Codegraph @

tests/manual/scenegraph_lancelot/scenegrabber/main.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include <QtCore/QTimer>
30 #include <QtCore/QDebug>
31 #include <QtCore/QFileInfo>
32 #include <QtCore/QHashFunctions>
33 #include <QtGui/QGuiApplication>
34 #include <QtGui/QImage>
35
36 #include <QtQuick/QQuickView>
37 #include <QtQuick/QQuickItem>
38
39 #ifdef Q_OS_WIN
40 # include <fcntl.h>
41 # include <io.h>
42 #endif // Q_OS_WIN
43
44 // Timeout values:
45
46 // A valid screen grab requires the scene to not change
47 // for SCENE_STABLE_TIME ms
48 #define SCENE_STABLE_TIME 200
49
50 // Give up after SCENE_TIMEOUT ms
51 #define SCENE_TIMEOUT 6000
52
53 //#define GRABBERDEBUG
54
55 class GrabbingView : public QQuickView
56 {
57 Q_OBJECT
58
59 public:
60 GrabbingView(const QString &outputFile)
61 : ofile(outputFile), grabNo(0), isGrabbing(false), initDone(false)
62 {
63 grabTimer = new QTimer(this);
64 grabTimer->setSingleShot(true);
65 grabTimer->setInterval(SCENE_STABLE_TIME);
66 connect(grabTimer, SIGNAL(timeout()), SLOT(grab()));
67
68 connect(this, SIGNAL(afterRendering()), SLOT(startGrabbing()));
69
70 QTimer::singleShot(SCENE_TIMEOUT, this, SLOT(timedOut()));
71 }
72
73 private slots:
74 void startGrabbing()
75 {
76 if (!initDone) {
77 initDone = true;
78 grabTimer->start();
79 }
80 }
81
82 void grab()
83 {
84 if (isGrabbing)
85 return;
86 isGrabbing = true;
87 grabNo++;
88 #ifdef GRABBERDEBUG
89 printf("grab no. %i\n", grabNo);
90 #endif
91 QImage img = grabWindow();
92 if (!img.isNull() && img == lastGrab) {
93 sceneStabilized();
94 } else {
95 lastGrab = img;
96 grabTimer->start();
97 }
98
99 isGrabbing = false;
100 }
101
102 void sceneStabilized()
103 {
104 #ifdef GRABBERDEBUG
105 printf("...sceneStabilized IN\n");
106 #endif
107 if (ofile == "-") { // Write to stdout
108 QFile of;
109 #ifdef Q_OS_WIN
110 // Make sure write to stdout doesn't do LF->CRLF
111 _setmode(_fileno(stdout), _O_BINARY);
112 #endif // Q_OS_WIN
113 if (!of.open(1, QIODevice::WriteOnly) || !lastGrab.save(&of, "ppm")) {
114 qWarning() << "Error: failed to write grabbed image to stdout.";
115 QGuiApplication::exit(2);
116 return;
117 }
118 } else {
119 if (!lastGrab.save(ofile)) {
120 qWarning() << "Error: failed to store grabbed image to" << ofile;
121 QGuiApplication::exit(2);
122 return;
123 }
124 }
125 QGuiApplication::exit(0);
126 #ifdef GRABBERDEBUG
127 printf("...sceneStabilized OUT\n");
128 #endif
129 }
130
131 void timedOut()
132 {
133 qWarning() << "Error: timed out waiting for scene to stabilize." << grabNo << "grab(s) done. Last grab was" << (lastGrab.isNull() ? "invalid." : "valid.");
134 QGuiApplication::exit(3);
135 }
136
137 private:
138 QImage lastGrab;
139 QTimer *grabTimer;
140 QString ofile;
141 int grabNo;
142 bool isGrabbing;
143 bool initDone;
144 };
145
146
147 int main(int argc, char *argv[])
148 {
149 qSetGlobalQHashSeed(0);
150
151 QGuiApplication a(argc, argv);
152
153 // Parse command line
154 QString ifile, ofile;
155 bool noText = false;
156 QStringList args = a.arguments();
157 int i = 0;
158 bool argError = false;
159 while (++i < args.size()) {
160 QString arg = args.at(i);
161 if ((arg == "-o") && (i < args.size()-1)) {
162 ofile = args.at(++i);
163 }
164 else if (arg == "-notext") {
165 noText = true;
166 }
167 else if (arg == "--cache-distance-fields") {
168 ;
169 }
170 else if (ifile.isEmpty()) {
171 ifile = arg;
172 }
173 else {
174 argError = true;
175 break;
176 }
177 }
178 if (argError || ifile.isEmpty() || ofile.isEmpty()) {
179 qWarning() << "Usage:" << args.at(0).toLatin1().constData() << "[-notext] <qml-infile> -o <outfile or - for ppm on stdout>";
180 return 1;
181 }
182
183 QFileInfo ifi(ifile);
184 if (!ifi.exists() || !ifi.isReadable() || !ifi.isFile()) {
185 qWarning() << args.at(0).toLatin1().constData() << " error: unreadable input file" << ifile;
186 return 1;
187 }
188 // End parsing
189
190 GrabbingView v(ofile);
191 v.setSource(QUrl::fromLocalFile(ifile));
192
193 if (noText) {
194 const QList<QQuickItem*> items = v.rootObject()->findChildren<QQuickItem*>();
195 for (QQuickItem *item : items) {
196 if (QByteArray(item->metaObject()->className()).contains("Text"))
197 item->setVisible(false);
198 }
199 }
200
201 v.show();
202
203 int retVal = a.exec();
204 #ifdef GRABBERDEBUG
205 printf("...retVal=%i\n", retVal);
206 #endif
207
208 return retVal;
209 }
210
211 #include "main.moc"