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