2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-22 12:24:00 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2016-01-22 12:24:00 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2015-02-13 11:15:33 +00:00
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-07 20:57:49 +00:00
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "screenshot.h"
|
|
|
|
|
|
|
|
//! [0]
|
|
|
|
Screenshot::Screenshot()
|
2015-08-28 08:56:58 +00:00
|
|
|
: screenshotLabel(new QLabel(this))
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-11-23 13:27:50 +00:00
|
|
|
screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
2011-04-27 10:05:43 +00:00
|
|
|
screenshotLabel->setAlignment(Qt::AlignCenter);
|
|
|
|
|
2015-08-28 08:56:58 +00:00
|
|
|
const QRect screenGeometry = QApplication::desktop()->screenGeometry(this);
|
|
|
|
screenshotLabel->setMinimumSize(screenGeometry.width() / 8, screenGeometry.height() / 8);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-08-28 08:56:58 +00:00
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addWidget(screenshotLabel);
|
2015-08-28 08:56:58 +00:00
|
|
|
|
|
|
|
QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"), this);
|
|
|
|
delaySpinBox = new QSpinBox(optionsGroupBox);
|
|
|
|
delaySpinBox->setSuffix(tr(" s"));
|
|
|
|
delaySpinBox->setMaximum(60);
|
|
|
|
|
|
|
|
typedef void (QSpinBox::*QSpinBoxIntSignal)(int);
|
|
|
|
connect(delaySpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
|
|
|
|
this, &Screenshot::updateCheckBox);
|
|
|
|
|
|
|
|
hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"), optionsGroupBox);
|
|
|
|
|
|
|
|
QGridLayout *optionsGroupBoxLayout = new QGridLayout(optionsGroupBox);
|
|
|
|
optionsGroupBoxLayout->addWidget(new QLabel(tr("Screenshot Delay:"), this), 0, 0);
|
|
|
|
optionsGroupBoxLayout->addWidget(delaySpinBox, 0, 1);
|
|
|
|
optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 1, 0, 1, 2);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addWidget(optionsGroupBox);
|
2015-08-28 08:56:58 +00:00
|
|
|
|
|
|
|
QHBoxLayout *buttonsLayout = new QHBoxLayout;
|
|
|
|
newScreenshotButton = new QPushButton(tr("New Screenshot"), this);
|
|
|
|
connect(newScreenshotButton, &QPushButton::clicked, this, &Screenshot::newScreenshot);
|
|
|
|
buttonsLayout->addWidget(newScreenshotButton);
|
|
|
|
QPushButton *saveScreenshotButton = new QPushButton(tr("Save Screenshot"), this);
|
|
|
|
connect(saveScreenshotButton, &QPushButton::clicked, this, &Screenshot::saveScreenshot);
|
|
|
|
buttonsLayout->addWidget(saveScreenshotButton);
|
|
|
|
QPushButton *quitScreenshotButton = new QPushButton(tr("Quit"), this);
|
|
|
|
quitScreenshotButton->setShortcut(Qt::CTRL + Qt::Key_Q);
|
|
|
|
connect(quitScreenshotButton, &QPushButton::clicked, this, &QWidget::close);
|
|
|
|
buttonsLayout->addWidget(quitScreenshotButton);
|
|
|
|
buttonsLayout->addStretch();
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addLayout(buttonsLayout);
|
|
|
|
|
|
|
|
shootScreen();
|
|
|
|
delaySpinBox->setValue(5);
|
|
|
|
|
|
|
|
setWindowTitle(tr("Screenshot"));
|
|
|
|
resize(300, 200);
|
|
|
|
}
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [1]
|
|
|
|
void Screenshot::resizeEvent(QResizeEvent * /* event */)
|
|
|
|
{
|
|
|
|
QSize scaledSize = originalPixmap.size();
|
|
|
|
scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio);
|
2012-11-23 13:27:50 +00:00
|
|
|
if (!screenshotLabel->pixmap() || scaledSize != screenshotLabel->pixmap()->size())
|
2011-04-27 10:05:43 +00:00
|
|
|
updateScreenshotLabel();
|
|
|
|
}
|
|
|
|
//! [1]
|
|
|
|
|
|
|
|
//! [2]
|
|
|
|
void Screenshot::newScreenshot()
|
|
|
|
{
|
|
|
|
if (hideThisWindowCheckBox->isChecked())
|
|
|
|
hide();
|
|
|
|
newScreenshotButton->setDisabled(true);
|
|
|
|
|
2015-08-28 08:56:58 +00:00
|
|
|
QTimer::singleShot(delaySpinBox->value() * 1000, this, &Screenshot::shootScreen);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [2]
|
|
|
|
|
|
|
|
//! [3]
|
|
|
|
void Screenshot::saveScreenshot()
|
|
|
|
{
|
2015-08-28 08:56:58 +00:00
|
|
|
const QString format = "png";
|
|
|
|
QString initialPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
|
|
|
|
if (initialPath.isEmpty())
|
|
|
|
initialPath = QDir::currentPath();
|
|
|
|
initialPath += tr("/untitled.") + format;
|
|
|
|
|
|
|
|
QFileDialog fileDialog(this, tr("Save As"), initialPath);
|
|
|
|
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
|
|
|
|
fileDialog.setFileMode(QFileDialog::AnyFile);
|
|
|
|
fileDialog.setDirectory(initialPath);
|
|
|
|
QStringList mimeTypes;
|
|
|
|
foreach (const QByteArray &bf, QImageWriter::supportedMimeTypes())
|
|
|
|
mimeTypes.append(QLatin1String(bf));
|
|
|
|
fileDialog.setMimeTypeFilters(mimeTypes);
|
|
|
|
fileDialog.selectMimeTypeFilter("image/" + format);
|
|
|
|
fileDialog.setDefaultSuffix(format);
|
|
|
|
if (fileDialog.exec() != QDialog::Accepted)
|
|
|
|
return;
|
|
|
|
const QString fileName = fileDialog.selectedFiles().first();
|
|
|
|
if (!originalPixmap.save(fileName)) {
|
|
|
|
QMessageBox::warning(this, tr("Save Error"), tr("The image could not be saved to \"%1\".")
|
|
|
|
.arg(QDir::toNativeSeparators(fileName)));
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [3]
|
|
|
|
|
|
|
|
//! [4]
|
|
|
|
void Screenshot::shootScreen()
|
|
|
|
{
|
2012-08-17 12:15:36 +00:00
|
|
|
QScreen *screen = QGuiApplication::primaryScreen();
|
2015-08-28 08:56:58 +00:00
|
|
|
if (const QWindow *window = windowHandle())
|
|
|
|
screen = window->screen();
|
|
|
|
if (!screen)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (delaySpinBox->value() != 0)
|
|
|
|
QApplication::beep();
|
|
|
|
|
|
|
|
originalPixmap = screen->grabWindow(0);
|
2011-04-27 10:05:43 +00:00
|
|
|
updateScreenshotLabel();
|
|
|
|
|
|
|
|
newScreenshotButton->setDisabled(false);
|
|
|
|
if (hideThisWindowCheckBox->isChecked())
|
|
|
|
show();
|
|
|
|
}
|
2015-08-28 08:56:58 +00:00
|
|
|
//! [4]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
//! [6]
|
|
|
|
void Screenshot::updateCheckBox()
|
|
|
|
{
|
|
|
|
if (delaySpinBox->value() == 0) {
|
|
|
|
hideThisWindowCheckBox->setDisabled(true);
|
|
|
|
hideThisWindowCheckBox->setChecked(false);
|
2012-11-23 13:27:50 +00:00
|
|
|
} else {
|
2011-04-27 10:05:43 +00:00
|
|
|
hideThisWindowCheckBox->setDisabled(false);
|
2012-11-23 13:27:50 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [6]
|
|
|
|
|
|
|
|
|
|
|
|
//! [10]
|
|
|
|
void Screenshot::updateScreenshotLabel()
|
|
|
|
{
|
|
|
|
screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(),
|
|
|
|
Qt::KeepAspectRatio,
|
|
|
|
Qt::SmoothTransformation));
|
|
|
|
}
|
|
|
|
//! [10]
|