0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
/*
|
|
42 |
* This programm takes a *.ui file and an output dir as argument in order to
|
|
43 |
* create a screenshot of the widget defined in the ui file.
|
|
44 |
*
|
|
45 |
* The screenshot is saved in the output dir (default current dir), ".png" is
|
|
46 |
* appended to the ui file name.
|
|
47 |
*/
|
|
48 |
|
|
49 |
#include <QApplication>
|
|
50 |
#include <QWidget>
|
|
51 |
#include <QFile>
|
|
52 |
#include <QDebug>
|
|
53 |
|
|
54 |
#include <QDir>
|
|
55 |
|
|
56 |
#include <iostream>
|
|
57 |
|
|
58 |
using namespace std;
|
|
59 |
|
|
60 |
|
|
61 |
#ifdef Q_WS_QWS
|
|
62 |
// we don't compile designer on embedded...
|
|
63 |
|
|
64 |
int main(int argc, char **argv)
|
|
65 |
{
|
|
66 |
return 0;
|
|
67 |
}
|
|
68 |
|
|
69 |
|
|
70 |
#else
|
|
71 |
|
|
72 |
#include <QUiLoader>
|
|
73 |
|
|
74 |
/*
|
|
75 |
* Take the path of an ui file and return appropriate QWidget.
|
|
76 |
*/
|
|
77 |
|
|
78 |
QWidget* getWidgetFromUiFile(const QString& fileNameUiFile)
|
|
79 |
{
|
|
80 |
qDebug() << "\t\t\t...loading ui file" << fileNameUiFile;
|
|
81 |
|
|
82 |
QUiLoader loader;
|
|
83 |
QFile uiFile(fileNameUiFile);
|
|
84 |
if (!uiFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
85 |
qDebug("\t\tError: QFile.open() failed.");
|
|
86 |
exit(EXIT_FAILURE);
|
|
87 |
}
|
|
88 |
|
|
89 |
QFileInfo fileInfo(fileNameUiFile);
|
|
90 |
QDir::setCurrent(fileInfo.absolutePath()); //for the stylesheet to find their images
|
|
91 |
|
|
92 |
QWidget *ui = loader.load(&uiFile);
|
|
93 |
if (!ui) {
|
|
94 |
qDebug("\t\tError: Quilodader.load() returned NULL pointer.");
|
|
95 |
exit(EXIT_FAILURE);
|
|
96 |
}
|
|
97 |
uiFile.close();
|
|
98 |
|
|
99 |
return ui;
|
|
100 |
}
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
/*
|
|
105 |
* Takes the actual screenshot.
|
|
106 |
*
|
|
107 |
* Hint: provide filename without extension, ".png" will be added
|
|
108 |
*/
|
|
109 |
|
|
110 |
void makeScreenshot(QWidget* widget, const QString& fileName, const QString& pathOutputDir)
|
|
111 |
{
|
|
112 |
QFileInfo fileInfo(fileName);
|
|
113 |
QString realFileName = fileInfo.completeBaseName() + "." + fileInfo.suffix() + ".png";
|
|
114 |
QString realPath = pathOutputDir + "/" + realFileName;
|
|
115 |
|
|
116 |
|
|
117 |
//QString realFileName = fileName + ".png";
|
|
118 |
qDebug() << "\t\t\t...Taking screenshot" << fileInfo.absoluteFilePath();
|
|
119 |
|
|
120 |
//widget->show();
|
|
121 |
qApp->processEvents();
|
|
122 |
QImage originalPixmap(widget->size(),QImage::Format_ARGB32);
|
|
123 |
widget->render(&originalPixmap);
|
|
124 |
if ( originalPixmap.isNull() ) {
|
|
125 |
qDebug("\t\tError: QPixmap::grabWidget() returned a NULL QPixmap.");
|
|
126 |
exit(EXIT_FAILURE);
|
|
127 |
}
|
|
128 |
//QString fileName = QDir::currentPath() + "/secondwidget." + format;
|
|
129 |
if ( !originalPixmap.save(realPath, "PNG") ) {
|
|
130 |
qDebug("\t\tError: QPixmap.save() failed.");
|
|
131 |
exit(EXIT_FAILURE);
|
|
132 |
}
|
|
133 |
qDebug() << "\t\t\t...Screenshot saved in" << realPath;
|
|
134 |
|
|
135 |
widget->close();
|
|
136 |
}
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
/*
|
|
141 |
* Call this if you just want to pass the ui file name and the output dir.
|
|
142 |
*/
|
|
143 |
|
|
144 |
void createScreenshotFromUiFile(const QString& fileNameUiFile, const QString pathOutputDir)
|
|
145 |
{
|
|
146 |
qDebug() << "\t\tCreating screenshot from widget defined in" << fileNameUiFile;
|
|
147 |
|
|
148 |
QWidget* w = getWidgetFromUiFile(fileNameUiFile);
|
|
149 |
makeScreenshot(w, fileNameUiFile, pathOutputDir);
|
|
150 |
}
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
/*
|
|
155 |
* Start here.
|
|
156 |
*/
|
|
157 |
|
|
158 |
int main(int argc, char **argv)
|
|
159 |
{
|
|
160 |
QApplication app(argc, argv);
|
|
161 |
|
|
162 |
// check for necessary arguments
|
|
163 |
if (argc == 1) {
|
|
164 |
cout << "Syntax: " << argv[0] << " <path to *.ui file> [output directory]" << endl;
|
|
165 |
cout << "" << endl;
|
|
166 |
cout << "Takes a *.ui file and an output dir as argument in order to" << endl;
|
|
167 |
cout << "create a screenshot of the widget defined in the ui file." << endl;
|
|
168 |
cout << "" << endl;
|
|
169 |
cout << "The screenshot is saved in the output dir (default current dir)," << endl;
|
|
170 |
cout << "'.png' is appended to the ui file name." << endl;
|
|
171 |
exit(EXIT_FAILURE);
|
|
172 |
}
|
|
173 |
|
|
174 |
|
|
175 |
// check for *.ui
|
|
176 |
QString fileName = app.arguments().value(1);
|
|
177 |
if ( !fileName.endsWith(".ui") ) {
|
|
178 |
qDebug() << fileName + " is not a *.ui file.";
|
|
179 |
exit(EXIT_FAILURE);
|
|
180 |
}
|
|
181 |
|
|
182 |
// does the file exist?
|
|
183 |
QFile uiFile(fileName);
|
|
184 |
if ( !uiFile.exists() ) {
|
|
185 |
qDebug() << fileName + " does not exist.";
|
|
186 |
exit(EXIT_FAILURE);
|
|
187 |
}
|
|
188 |
|
|
189 |
// check output directory
|
|
190 |
QString pathOutputDir = QDir::currentPath();
|
|
191 |
|
|
192 |
if (argc >= 3 ) {
|
|
193 |
QDir outputDir = app.arguments().value(2);
|
|
194 |
if ( outputDir.exists() ) {
|
|
195 |
pathOutputDir = outputDir.absolutePath();
|
|
196 |
} else {
|
|
197 |
qDebug() << outputDir.absolutePath() + " does not exist or is not a directory.";
|
|
198 |
exit(EXIT_FAILURE);
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
// take the screenshot
|
|
203 |
createScreenshotFromUiFile(fileName, pathOutputDir);
|
|
204 |
|
|
205 |
app.quit();
|
|
206 |
return 0;
|
|
207 |
}
|
|
208 |
|
|
209 |
#endif
|