2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
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);
|
|
|
|
|
2019-09-09 14:11:48 +00:00
|
|
|
const QRect screenGeometry = screen()->geometry();
|
2015-08-28 08:56:58 +00:00
|
|
|
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);
|
|
|
|
|
2020-02-07 08:36:54 +00:00
|
|
|
connect(delaySpinBox, &QSpinBox::valueChanged,
|
2015-08-28 08:56:58 +00:00
|
|
|
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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
quitScreenshotButton->setShortcut(Qt::CTRL | Qt::Key_Q);
|
2015-08-28 08:56:58 +00:00
|
|
|
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);
|
2023-02-20 09:06:46 +00:00
|
|
|
if (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;
|
2018-12-07 11:01:42 +00:00
|
|
|
const QList<QByteArray> baMimeTypes = QImageWriter::supportedMimeTypes();
|
|
|
|
for (const QByteArray &bf : baMimeTypes)
|
2015-08-28 08:56:58 +00:00
|
|
|
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]
|