annotate examples/desktop/screenshot/screenshot.d @ 374:681af90e1d53

Removed debug writelns. Fixed build script.
author Max Samukha <maxter@spambox.com>
date Thu, 08 Jul 2010 22:21:56 +0300
parents 256ab6cb8e85
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
120
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
1 /****************************************************************************
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
2 **
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
4 ** Contact: Qt Software Information (qt-info@nokia.com)
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
5 **
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
6 ** This file is part of the examples of the Qt Toolkit.
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
7 **
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
8 ** $QT_BEGIN_LICENSE:LGPL$
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
9 ** Commercial Usage
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
10 ** Licensees holding valid Qt Commercial licenses may use this file in
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
11 ** accordance with the Qt Commercial License Agreement provided with the
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
12 ** Software or, alternatively, in accordance with the terms contained in
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
13 ** a written agreement between you and Nokia.
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
14 **
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
15 ** GNU Lesser General Public License Usage
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
17 ** General Public License version 2.1 as published by the Free Software
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
19 ** packaging of this file. Please review the following information to
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
22 **
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
23 ** In addition, as a special exception, Nokia gives you certain
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
24 ** additional rights. These rights are described in the Nokia Qt LGPL
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
26 ** package.
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
27 **
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
28 ** GNU General Public License Usage
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
29 ** Alternatively, this file may be used under the terms of the GNU
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
30 ** General Public License version 3.0 as published by the Free Software
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
31 ** Foundation and appearing in the file LICENSE.GPL included in the
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
32 ** packaging of this file. Please review the following information to
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
33 ** ensure the GNU General Public License version 3.0 requirements will be
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
34 ** met: http://www.gnu.org/copyleft/gpl.html.
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
35 **
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
36 ** If you are unsure which license is appropriate for your use, please
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
37 ** contact the sales department at qt-sales@nokia.com.
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
38 ** $QT_END_LICENSE$
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
39 **
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
40 ****************************************************************************/
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
41 module screenshot;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
42
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
43
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
44 import qt.gui.QPixmap;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
45 import qt.gui.QWidget;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
46 import qt.gui.QCheckBox;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
47 import qt.gui.QGridLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
48 import qt.gui.QGroupBox;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
49 import qt.gui.QHBoxLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
50 import qt.gui.QLabel;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
51 import qt.gui.QPushButton;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
52 import qt.gui.QSpinBox;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
53 import qt.gui.QVBoxLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
54 import qt.gui.QFileDialog;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
55 import qt.core.QDir;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
56 import qt.core.QTimer;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
57
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
58 import tango.text.convert.Format;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
59 import tango.text.Ascii;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
60
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
61
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
62 class Screenshot : public QWidget
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
63 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
64 public:
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
65
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
66 this()
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
67 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
68 screenshotLabel = new QLabel;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
69 screenshotLabel.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
70 screenshotLabel.setAlignment(Qt.AlignCenter);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
71 screenshotLabel.setMinimumSize(240, 160);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
72
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
73 createOptionsGroupBox();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
74 createButtonsLayout();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
75
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
76 mainLayout = new QVBoxLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
77 mainLayout.addWidget(screenshotLabel);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
78 mainLayout.addWidget(optionsGroupBox);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
79 mainLayout.addLayout(buttonsLayout);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
80 setLayout(mainLayout);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
81
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
82 shootScreen();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
83 delaySpinBox.setValue(5);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
84
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
85 setWindowTitle(tr("Screenshot"));
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
86 resize(300, 200);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
87 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
88
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
89 protected:
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
90
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
91 void resizeEvent(QResizeEvent /* event */)
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
92 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
93 QSize scaledSize = originalPixmap.size();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
94 scaledSize.scale(screenshotLabel.size(), Qt.KeepAspectRatio);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
95 if (!screenshotLabel.pixmap() || scaledSize != screenshotLabel.pixmap().size())
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
96 updateScreenshotLabel();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
97 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
98
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
99 private:
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
100
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
101 void newScreenshot()
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
102 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
103 if (hideThisWindowCheckBox.isChecked())
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
104 hide();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
105 newScreenshotButton.setDisabled(true);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
106
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
107 QTimer.singleShot(delaySpinBox.value() * 1000, this, SLOT(shootScreen()));
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
108 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
109
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
110 void saveScreenshot()
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
111 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
112 string format = "png";
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
113 string initialPath = QDir.currentPath() + tr("/untitled.") + format;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
114
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
115 string fileName = QFileDialog.getSaveFileName(this, tr("Save As"), initialPath,
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
116 Format(tr("{} Files (*.{});;All Files (*)"), toUpper(format), format));
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
117 if (fileName.length)
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
118 originalPixmap.save(fileName, format);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
119 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
120
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
121 void shootScreen()
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
122 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
123 if (delaySpinBox.value() != 0)
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
124 QApplication.beep();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
125
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
126 originalPixmap = new QPixmap(); // clear image for low memory situations
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
127
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
128 // on embedded devices.
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
129 originalPixmap = QPixmap.grabWindow(QApplication.desktop().winId());
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
130 updateScreenshotLabel();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
131
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
132 newScreenshotButton.setDisabled(false);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
133 if (hideThisWindowCheckBox.isChecked())
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
134 show();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
135 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
136
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
137 void updateCheckBox()
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
138 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
139 if (delaySpinBox.value() == 0) {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
140 hideThisWindowCheckBox.setDisabled(true);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
141 hideThisWindowCheckBox.setChecked(false);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
142 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
143 else
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
144 hideThisWindowCheckBox.setDisabled(false);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
145 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
146
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
147 private:
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
148
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
149 void createOptionsGroupBox()
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
150 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
151 optionsGroupBox = new QGroupBox(tr("Options"));
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
152
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
153 delaySpinBox = new QSpinBox;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
154 delaySpinBox.setSuffix(tr(" s"));
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
155 delaySpinBox.setMaximum(60);
282
256ab6cb8e85 Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
eldar
parents: 120
diff changeset
156 connect!("valueChanged")(delaySpinBox, &this.updateCheckBox);
120
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
157
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
158 delaySpinBoxLabel = new QLabel(tr("Screenshot Delay:"));
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
159
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
160 hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"));
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
161
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
162 optionsGroupBoxLayout = new QGridLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
163 optionsGroupBoxLayout.addWidget(delaySpinBoxLabel, 0, 0);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
164 optionsGroupBoxLayout.addWidget(delaySpinBox, 0, 1);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
165 optionsGroupBoxLayout.addWidget(hideThisWindowCheckBox, 1, 0, 1, 2);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
166 optionsGroupBox.setLayout(optionsGroupBoxLayout);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
167 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
168
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
169 void createButtonsLayout()
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
170 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
171 newScreenshotButton = createButton(tr("New Screenshot"), &this.newScreenshot);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
172
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
173 saveScreenshotButton = createButton(tr("Save Screenshot"), &this.saveScreenshot);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
174
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
175 quitScreenshotButton = createButton(tr("Quit"), &this.close);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
176
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
177 buttonsLayout = new QHBoxLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
178 buttonsLayout.addStretch();
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
179 buttonsLayout.addWidget(newScreenshotButton);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
180 buttonsLayout.addWidget(saveScreenshotButton);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
181 buttonsLayout.addWidget(quitScreenshotButton);
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
182 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
183
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
184 QPushButton createButton(string text, void delegate() slot)
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
185 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
186 QPushButton button = new QPushButton(text);
282
256ab6cb8e85 Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
eldar
parents: 120
diff changeset
187 connect!("clicked")(button, slot);
120
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
188 return button;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
189 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
190
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
191 void updateScreenshotLabel()
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
192 {
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
193 screenshotLabel.setPixmap(originalPixmap.scaled(screenshotLabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation));
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
194 }
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
195
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
196 QPixmap originalPixmap;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
197
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
198 QLabel screenshotLabel;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
199 QGroupBox optionsGroupBox;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
200 QSpinBox delaySpinBox;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
201 QLabel delaySpinBoxLabel;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
202 QCheckBox hideThisWindowCheckBox;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
203 QPushButton newScreenshotButton;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
204 QPushButton saveScreenshotButton;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
205 QPushButton quitScreenshotButton;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
206
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
207 QVBoxLayout mainLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
208 QGridLayout optionsGroupBoxLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
209 QHBoxLayout buttonsLayout;
2c9ef955a171 add desktop examples
mandel
parents:
diff changeset
210 }