1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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 examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "screenshot.h"
|
---|
44 |
|
---|
45 | //! [0]
|
---|
46 | Screenshot::Screenshot()
|
---|
47 | {
|
---|
48 | screenshotLabel = new QLabel;
|
---|
49 | screenshotLabel->setSizePolicy(QSizePolicy::Expanding,
|
---|
50 | QSizePolicy::Expanding);
|
---|
51 | screenshotLabel->setAlignment(Qt::AlignCenter);
|
---|
52 | screenshotLabel->setMinimumSize(240, 160);
|
---|
53 |
|
---|
54 | createOptionsGroupBox();
|
---|
55 | createButtonsLayout();
|
---|
56 |
|
---|
57 | mainLayout = new QVBoxLayout;
|
---|
58 | mainLayout->addWidget(screenshotLabel);
|
---|
59 | mainLayout->addWidget(optionsGroupBox);
|
---|
60 | mainLayout->addLayout(buttonsLayout);
|
---|
61 | setLayout(mainLayout);
|
---|
62 |
|
---|
63 | shootScreen();
|
---|
64 | delaySpinBox->setValue(5);
|
---|
65 |
|
---|
66 | setWindowTitle(tr("Screenshot"));
|
---|
67 | resize(300, 200);
|
---|
68 | }
|
---|
69 | //! [0]
|
---|
70 |
|
---|
71 | //! [1]
|
---|
72 | void Screenshot::resizeEvent(QResizeEvent * /* event */)
|
---|
73 | {
|
---|
74 | QSize scaledSize = originalPixmap.size();
|
---|
75 | scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio);
|
---|
76 | if (!screenshotLabel->pixmap()
|
---|
77 | || scaledSize != screenshotLabel->pixmap()->size())
|
---|
78 | updateScreenshotLabel();
|
---|
79 | }
|
---|
80 | //! [1]
|
---|
81 |
|
---|
82 | //! [2]
|
---|
83 | void Screenshot::newScreenshot()
|
---|
84 | {
|
---|
85 | if (hideThisWindowCheckBox->isChecked())
|
---|
86 | hide();
|
---|
87 | newScreenshotButton->setDisabled(true);
|
---|
88 |
|
---|
89 | QTimer::singleShot(delaySpinBox->value() * 1000, this, SLOT(shootScreen()));
|
---|
90 | }
|
---|
91 | //! [2]
|
---|
92 |
|
---|
93 | //! [3]
|
---|
94 | void Screenshot::saveScreenshot()
|
---|
95 | {
|
---|
96 | QString format = "png";
|
---|
97 | QString initialPath = QDir::currentPath() + tr("/untitled.") + format;
|
---|
98 |
|
---|
99 | QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
|
---|
100 | initialPath,
|
---|
101 | tr("%1 Files (*.%2);;All Files (*)")
|
---|
102 | .arg(format.toUpper())
|
---|
103 | .arg(format));
|
---|
104 | if (!fileName.isEmpty())
|
---|
105 | originalPixmap.save(fileName, format.toAscii());
|
---|
106 | }
|
---|
107 | //! [3]
|
---|
108 |
|
---|
109 | //! [4]
|
---|
110 | void Screenshot::shootScreen()
|
---|
111 | {
|
---|
112 | if (delaySpinBox->value() != 0)
|
---|
113 | qApp->beep();
|
---|
114 | //! [4]
|
---|
115 | originalPixmap = QPixmap(); // clear image for low memory situations
|
---|
116 | // on embedded devices.
|
---|
117 | //! [5]
|
---|
118 | originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
|
---|
119 | updateScreenshotLabel();
|
---|
120 |
|
---|
121 | newScreenshotButton->setDisabled(false);
|
---|
122 | if (hideThisWindowCheckBox->isChecked())
|
---|
123 | show();
|
---|
124 | }
|
---|
125 | //! [5]
|
---|
126 |
|
---|
127 | //! [6]
|
---|
128 | void Screenshot::updateCheckBox()
|
---|
129 | {
|
---|
130 | if (delaySpinBox->value() == 0) {
|
---|
131 | hideThisWindowCheckBox->setDisabled(true);
|
---|
132 | hideThisWindowCheckBox->setChecked(false);
|
---|
133 | }
|
---|
134 | else
|
---|
135 | hideThisWindowCheckBox->setDisabled(false);
|
---|
136 | }
|
---|
137 | //! [6]
|
---|
138 |
|
---|
139 | //! [7]
|
---|
140 | void Screenshot::createOptionsGroupBox()
|
---|
141 | {
|
---|
142 | optionsGroupBox = new QGroupBox(tr("Options"));
|
---|
143 |
|
---|
144 | delaySpinBox = new QSpinBox;
|
---|
145 | delaySpinBox->setSuffix(tr(" s"));
|
---|
146 | delaySpinBox->setMaximum(60);
|
---|
147 | connect(delaySpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateCheckBox()));
|
---|
148 |
|
---|
149 | delaySpinBoxLabel = new QLabel(tr("Screenshot Delay:"));
|
---|
150 |
|
---|
151 | hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"));
|
---|
152 |
|
---|
153 | optionsGroupBoxLayout = new QGridLayout;
|
---|
154 | optionsGroupBoxLayout->addWidget(delaySpinBoxLabel, 0, 0);
|
---|
155 | optionsGroupBoxLayout->addWidget(delaySpinBox, 0, 1);
|
---|
156 | optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 1, 0, 1, 2);
|
---|
157 | optionsGroupBox->setLayout(optionsGroupBoxLayout);
|
---|
158 | }
|
---|
159 | //! [7]
|
---|
160 |
|
---|
161 | //! [8]
|
---|
162 | void Screenshot::createButtonsLayout()
|
---|
163 | {
|
---|
164 | newScreenshotButton = createButton(tr("New Screenshot"),
|
---|
165 | this, SLOT(newScreenshot()));
|
---|
166 |
|
---|
167 | saveScreenshotButton = createButton(tr("Save Screenshot"),
|
---|
168 | this, SLOT(saveScreenshot()));
|
---|
169 |
|
---|
170 | quitScreenshotButton = createButton(tr("Quit"), this, SLOT(close()));
|
---|
171 |
|
---|
172 | buttonsLayout = new QHBoxLayout;
|
---|
173 | buttonsLayout->addStretch();
|
---|
174 | buttonsLayout->addWidget(newScreenshotButton);
|
---|
175 | buttonsLayout->addWidget(saveScreenshotButton);
|
---|
176 | buttonsLayout->addWidget(quitScreenshotButton);
|
---|
177 | }
|
---|
178 | //! [8]
|
---|
179 |
|
---|
180 | //! [9]
|
---|
181 | QPushButton *Screenshot::createButton(const QString &text, QWidget *receiver,
|
---|
182 | const char *member)
|
---|
183 | {
|
---|
184 | QPushButton *button = new QPushButton(text);
|
---|
185 | button->connect(button, SIGNAL(clicked()), receiver, member);
|
---|
186 | return button;
|
---|
187 | }
|
---|
188 | //! [9]
|
---|
189 |
|
---|
190 | //! [10]
|
---|
191 | void Screenshot::updateScreenshotLabel()
|
---|
192 | {
|
---|
193 | screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(),
|
---|
194 | Qt::KeepAspectRatio,
|
---|
195 | Qt::SmoothTransformation));
|
---|
196 | }
|
---|
197 | //! [10]
|
---|