| 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 examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** 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 have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtGui> | 
|---|
| 43 |  | 
|---|
| 44 | #include "screenshot.h" | 
|---|
| 45 |  | 
|---|
| 46 | //! [0] | 
|---|
| 47 | Screenshot::Screenshot() | 
|---|
| 48 | { | 
|---|
| 49 | screenshotLabel = new QLabel; | 
|---|
| 50 | screenshotLabel->setSizePolicy(QSizePolicy::Expanding, | 
|---|
| 51 | QSizePolicy::Expanding); | 
|---|
| 52 | screenshotLabel->setAlignment(Qt::AlignCenter); | 
|---|
| 53 | screenshotLabel->setMinimumSize(240, 160); | 
|---|
| 54 |  | 
|---|
| 55 | createOptionsGroupBox(); | 
|---|
| 56 | createButtonsLayout(); | 
|---|
| 57 |  | 
|---|
| 58 | mainLayout = new QVBoxLayout; | 
|---|
| 59 | mainLayout->addWidget(screenshotLabel); | 
|---|
| 60 | mainLayout->addWidget(optionsGroupBox); | 
|---|
| 61 | mainLayout->addLayout(buttonsLayout); | 
|---|
| 62 | setLayout(mainLayout); | 
|---|
| 63 |  | 
|---|
| 64 | shootScreen(); | 
|---|
| 65 | delaySpinBox->setValue(5); | 
|---|
| 66 |  | 
|---|
| 67 | setWindowTitle(tr("Screenshot")); | 
|---|
| 68 | resize(300, 200); | 
|---|
| 69 | } | 
|---|
| 70 | //! [0] | 
|---|
| 71 |  | 
|---|
| 72 | //! [1] | 
|---|
| 73 | void Screenshot::resizeEvent(QResizeEvent * /* event */) | 
|---|
| 74 | { | 
|---|
| 75 | QSize scaledSize = originalPixmap.size(); | 
|---|
| 76 | scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio); | 
|---|
| 77 | if (!screenshotLabel->pixmap() | 
|---|
| 78 | || scaledSize != screenshotLabel->pixmap()->size()) | 
|---|
| 79 | updateScreenshotLabel(); | 
|---|
| 80 | } | 
|---|
| 81 | //! [1] | 
|---|
| 82 |  | 
|---|
| 83 | //! [2] | 
|---|
| 84 | void Screenshot::newScreenshot() | 
|---|
| 85 | { | 
|---|
| 86 | if (hideThisWindowCheckBox->isChecked()) | 
|---|
| 87 | hide(); | 
|---|
| 88 | newScreenshotButton->setDisabled(true); | 
|---|
| 89 |  | 
|---|
| 90 | QTimer::singleShot(delaySpinBox->value() * 1000, this, SLOT(shootScreen())); | 
|---|
| 91 | } | 
|---|
| 92 | //! [2] | 
|---|
| 93 |  | 
|---|
| 94 | //! [3] | 
|---|
| 95 | void Screenshot::saveScreenshot() | 
|---|
| 96 | { | 
|---|
| 97 | QString format = "png"; | 
|---|
| 98 | QString initialPath = QDir::currentPath() + tr("/untitled.") + format; | 
|---|
| 99 |  | 
|---|
| 100 | QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), | 
|---|
| 101 | initialPath, | 
|---|
| 102 | tr("%1 Files (*.%2);;All Files (*)") | 
|---|
| 103 | .arg(format.toUpper()) | 
|---|
| 104 | .arg(format)); | 
|---|
| 105 | if (!fileName.isEmpty()) | 
|---|
| 106 | originalPixmap.save(fileName, format.toAscii()); | 
|---|
| 107 | } | 
|---|
| 108 | //! [3] | 
|---|
| 109 |  | 
|---|
| 110 | //! [4] | 
|---|
| 111 | void Screenshot::shootScreen() | 
|---|
| 112 | { | 
|---|
| 113 | if (delaySpinBox->value() != 0) | 
|---|
| 114 | qApp->beep(); | 
|---|
| 115 | //! [4] | 
|---|
| 116 | originalPixmap = QPixmap(); // clear image for low memory situations | 
|---|
| 117 | // on embedded devices. | 
|---|
| 118 | //! [5] | 
|---|
| 119 | originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); | 
|---|
| 120 | updateScreenshotLabel(); | 
|---|
| 121 |  | 
|---|
| 122 | newScreenshotButton->setDisabled(false); | 
|---|
| 123 | if (hideThisWindowCheckBox->isChecked()) | 
|---|
| 124 | show(); | 
|---|
| 125 | } | 
|---|
| 126 | //! [5] | 
|---|
| 127 |  | 
|---|
| 128 | //! [6] | 
|---|
| 129 | void Screenshot::updateCheckBox() | 
|---|
| 130 | { | 
|---|
| 131 | if (delaySpinBox->value() == 0) { | 
|---|
| 132 | hideThisWindowCheckBox->setDisabled(true); | 
|---|
| 133 | hideThisWindowCheckBox->setChecked(false); | 
|---|
| 134 | } | 
|---|
| 135 | else | 
|---|
| 136 | hideThisWindowCheckBox->setDisabled(false); | 
|---|
| 137 | } | 
|---|
| 138 | //! [6] | 
|---|
| 139 |  | 
|---|
| 140 | //! [7] | 
|---|
| 141 | void Screenshot::createOptionsGroupBox() | 
|---|
| 142 | { | 
|---|
| 143 | optionsGroupBox = new QGroupBox(tr("Options")); | 
|---|
| 144 |  | 
|---|
| 145 | delaySpinBox = new QSpinBox; | 
|---|
| 146 | delaySpinBox->setSuffix(tr(" s")); | 
|---|
| 147 | delaySpinBox->setMaximum(60); | 
|---|
| 148 | connect(delaySpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateCheckBox())); | 
|---|
| 149 |  | 
|---|
| 150 | delaySpinBoxLabel = new QLabel(tr("Screenshot Delay:")); | 
|---|
| 151 |  | 
|---|
| 152 | hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window")); | 
|---|
| 153 |  | 
|---|
| 154 | optionsGroupBoxLayout = new QGridLayout; | 
|---|
| 155 | optionsGroupBoxLayout->addWidget(delaySpinBoxLabel, 0, 0); | 
|---|
| 156 | optionsGroupBoxLayout->addWidget(delaySpinBox, 0, 1); | 
|---|
| 157 | optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 1, 0, 1, 2); | 
|---|
| 158 | optionsGroupBox->setLayout(optionsGroupBoxLayout); | 
|---|
| 159 | } | 
|---|
| 160 | //! [7] | 
|---|
| 161 |  | 
|---|
| 162 | //! [8] | 
|---|
| 163 | void Screenshot::createButtonsLayout() | 
|---|
| 164 | { | 
|---|
| 165 | newScreenshotButton = createButton(tr("New Screenshot"), | 
|---|
| 166 | this, SLOT(newScreenshot())); | 
|---|
| 167 |  | 
|---|
| 168 | saveScreenshotButton = createButton(tr("Save Screenshot"), | 
|---|
| 169 | this, SLOT(saveScreenshot())); | 
|---|
| 170 |  | 
|---|
| 171 | quitScreenshotButton = createButton(tr("Quit"), this, SLOT(close())); | 
|---|
| 172 |  | 
|---|
| 173 | buttonsLayout = new QHBoxLayout; | 
|---|
| 174 | buttonsLayout->addStretch(); | 
|---|
| 175 | buttonsLayout->addWidget(newScreenshotButton); | 
|---|
| 176 | buttonsLayout->addWidget(saveScreenshotButton); | 
|---|
| 177 | buttonsLayout->addWidget(quitScreenshotButton); | 
|---|
| 178 | } | 
|---|
| 179 | //! [8] | 
|---|
| 180 |  | 
|---|
| 181 | //! [9] | 
|---|
| 182 | QPushButton *Screenshot::createButton(const QString &text, QWidget *receiver, | 
|---|
| 183 | const char *member) | 
|---|
| 184 | { | 
|---|
| 185 | QPushButton *button = new QPushButton(text); | 
|---|
| 186 | button->connect(button, SIGNAL(clicked()), receiver, member); | 
|---|
| 187 | return button; | 
|---|
| 188 | } | 
|---|
| 189 | //! [9] | 
|---|
| 190 |  | 
|---|
| 191 | //! [10] | 
|---|
| 192 | void Screenshot::updateScreenshotLabel() | 
|---|
| 193 | { | 
|---|
| 194 | screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(), | 
|---|
| 195 | Qt::KeepAspectRatio, | 
|---|
| 196 | Qt::SmoothTransformation)); | 
|---|
| 197 | } | 
|---|
| 198 | //! [10] | 
|---|