Files
SOM/plugins/ScreenShotPlugin/ScreenShotPlugin.cpp

164 lines
5.4 KiB
C++

#include "ScreenShotPlugin.h"
//#include <QVBoxLayout>
//#include <QGroupBox>
//#include <QPushButton>
//#include <QScreen>
#include <QtWidgets>
//#include <QSpinBox>
//#include <QCheckBox>
//#include <QPushButton>
//#include <QApplication>
//#include <QTimer>
QString ScreenShotPlugin::pname()
{
return "Screen Shot Plugin";
}
QString ScreenShotPlugin::pdesc()
{
return "Simple Screen Shot Plugin.";
}
QWidget* ScreenShotPlugin::pcontent()
{
test = new QWidget();
test->setWindowTitle(tr("Screen Shot Plugin"));
test->resize(400, 300);
QVBoxLayout *mainLayout = new QVBoxLayout(nullptr);
screenshotLabel = new QLabel;
screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
screenshotLabel->setAlignment(Qt::AlignCenter);
const QRect screenGeometry = test->screen()->geometry();
screenshotLabel->setMinimumSize(screenGeometry.width() / 8, screenGeometry.height() / 8);
mainLayout->addWidget(screenshotLabel);
QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"));
delaySpinBox = new QSpinBox(optionsGroupBox);
delaySpinBox->setSuffix(tr(" s"));
delaySpinBox->setMaximum(60);
connect(delaySpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
this, &ScreenShotPlugin::updateceheck);
hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"), optionsGroupBox);
QGridLayout *optionsGroupBoxLayout = new QGridLayout(optionsGroupBox);
optionsGroupBoxLayout->addWidget(new QLabel(tr("Screenshot Delay:"), nullptr), 0, 0);
optionsGroupBoxLayout->addWidget(delaySpinBox, 0, 1);
optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 1, 0, 1, 2);
mainLayout->addWidget(optionsGroupBox);
QHBoxLayout *buttonsLayout = new QHBoxLayout;
newScreenshotButton = new QPushButton(tr("New Screenshot"), test);
connect(newScreenshotButton, &QPushButton::clicked, this, &ScreenShotPlugin::newScreenshot);
buttonsLayout->addWidget(newScreenshotButton);
QPushButton *saveScreenshotButton = new QPushButton(tr("Save Screenshot"));
connect(saveScreenshotButton, &QPushButton::clicked, this, &ScreenShotPlugin::saveScreenshot);
buttonsLayout->addWidget(saveScreenshotButton);
QPushButton *quitScreenshotButton = new QPushButton(tr("Quit"));
quitScreenshotButton->setShortcut(Qt::CTRL + Qt::Key_Q);
connect(quitScreenshotButton, &QPushButton::clicked, test, &QWidget::close);
buttonsLayout->addWidget(quitScreenshotButton);
buttonsLayout->addStretch();
mainLayout->addLayout(buttonsLayout);
delaySpinBox->setValue(5);
test->setLayout(mainLayout);
QTimer::singleShot(250, this, &ScreenShotPlugin::shootScreen);
QIcon icon = QIcon(":/images/monkey.png");
test->setWindowIcon(icon);
return test;
}
void ScreenShotPlugin::updateScreenshotLabel()
{
screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(),
Qt::KeepAspectRatio,
Qt::SmoothTransformation));
}
void ScreenShotPlugin::shootScreen()
{
QScreen *screen = QGuiApplication::primaryScreen();
if (QWindow *window = test->windowHandle())
screen = window->screen();
if (!screen)
return;
if (delaySpinBox->value() != 0)
QApplication::beep();
originalPixmap = screen->grabWindow(0);
updateScreenshotLabel();
newScreenshotButton->setDisabled(false);
if (hideThisWindowCheckBox->isChecked())
test->show();
}
void ScreenShotPlugin::saveScreenshot()
{
QString d = QDate::currentDate().toString();
QString t = QTime::currentTime().toString();
const QString format = "png";
QString initialPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
if (initialPath.isEmpty())
initialPath = QDir::currentPath();
initialPath += tr("/ScreenShot_") + t + "-" + d + "." + format;
QFileDialog fileDialog(test, tr("Save As"), initialPath);
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
fileDialog.setFileMode(QFileDialog::AnyFile);
fileDialog.setDirectory(initialPath);
QStringList mimeTypes;
const QList<QByteArray> baMimeTypes = QImageWriter::supportedMimeTypes();
for (const QByteArray &bf : baMimeTypes)
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(test, tr("Save Error"), tr("The image could not be saved to \"%1\".")
.arg(QDir::toNativeSeparators(fileName)));
}
}
void ScreenShotPlugin::newScreenshot()
{
if (hideThisWindowCheckBox->isChecked())
test->hide();
newScreenshotButton->setDisabled(true);
//shootScreen();
QTimer::singleShot(delaySpinBox->value() * 1000, this, &ScreenShotPlugin::shootScreen);
}
void ScreenShotPlugin::updateceheck()
{
if (delaySpinBox->value() == 0) {
hideThisWindowCheckBox->setDisabled(true);
hideThisWindowCheckBox->setChecked(false);
} else {
hideThisWindowCheckBox->setDisabled(false);
}
}
void ScreenShotPlugin::connectToHost(QObject* host)
{
Q_UNUSED(host);
// Calculator plugin doesn't need host signals
}