From 528a1d28bdcbd511f2c0e5f5bc33496470ecb1f2 Mon Sep 17 00:00:00 2001 From: SkyD666 Date: Mon, 29 Jan 2024 16:06:44 +0800 Subject: [PATCH] [feature] Support automatic screenshot after releasing the mouse under the free snap function --- .../SmartScreenSnapper.pro.user | 2 +- SmartScreenSnapper/src/freesnapdialog.cpp | 30 ++++--- SmartScreenSnapper/src/freesnapdialog.h | 2 + SmartScreenSnapper/src/publicdata.cpp | 3 + SmartScreenSnapper/src/publicdata.h | 2 + SmartScreenSnapper/src/settingdialog.cpp | 89 ++++++++++--------- SmartScreenSnapper/ui/settingdialog.ui | 35 +++++--- 7 files changed, 100 insertions(+), 63 deletions(-) diff --git a/SmartScreenSnapper/SmartScreenSnapper.pro.user b/SmartScreenSnapper/SmartScreenSnapper.pro.user index 7ba2f67..922f3e0 100644 --- a/SmartScreenSnapper/SmartScreenSnapper.pro.user +++ b/SmartScreenSnapper/SmartScreenSnapper.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/SmartScreenSnapper/src/freesnapdialog.cpp b/SmartScreenSnapper/src/freesnapdialog.cpp index e5afae1..4bfe1e4 100644 --- a/SmartScreenSnapper/src/freesnapdialog.cpp +++ b/SmartScreenSnapper/src/freesnapdialog.cpp @@ -1,4 +1,5 @@ #include "freesnapdialog.h" +#include "publicdata.h" #include "ui_freesnapdialog.h" #include #include @@ -146,6 +147,10 @@ void FreeSnapDialog::mouseReleaseEvent(QMouseEvent* event) pressedInBottomArea = false; deltaHeight = deltaWidth = 0; + + if (PublicData::freeSnapReleaseMouseCapture) { + captureAndClose(); + } } void FreeSnapDialog::mouseMoveEvent(QMouseEvent* event) @@ -463,20 +468,25 @@ void FreeSnapDialog::refreshPreviewArea(QPoint mousePos) ui->labelPreview->setPixmap(processedPixmap.scaled(ui->labelPreview->width(), ui->labelPreview->height(), Qt::KeepAspectRatio)); } +void FreeSnapDialog::captureAndClose() +{ + auto geometry = ui->frameRect->geometry(); + auto devicePixelRatio = ui->graphicsView->devicePixelRatio(); + geometry.setRect(geometry.x() * devicePixelRatio, + geometry.y() * devicePixelRatio, + geometry.width() * devicePixelRatio, + geometry.height() * devicePixelRatio); + *this->resultPixmap = fullScreenPixmap.copy(geometry); + this->resultPixmap->setDevicePixelRatio(1); + captured = true; + close(); +} + // 键盘事件 void FreeSnapDialog::keyPressEvent(QKeyEvent* event) { if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { - auto geometry = ui->frameRect->geometry(); - auto devicePixelRatio = ui->graphicsView->devicePixelRatio(); - geometry.setRect(geometry.x() * devicePixelRatio, - geometry.y() * devicePixelRatio, - geometry.width() * devicePixelRatio, - geometry.height() * devicePixelRatio); - *this->resultPixmap = fullScreenPixmap.copy(geometry); - this->resultPixmap->setDevicePixelRatio(1); - captured = true; - close(); + captureAndClose(); return; } else if (event->key() == Qt::Key_Escape) { captured = false; diff --git a/SmartScreenSnapper/src/freesnapdialog.h b/SmartScreenSnapper/src/freesnapdialog.h index 62068db..981c81e 100644 --- a/SmartScreenSnapper/src/freesnapdialog.h +++ b/SmartScreenSnapper/src/freesnapdialog.h @@ -75,6 +75,8 @@ class FreeSnapDialog : public BaseFullScreenSnapDialog { void refreshGrayArea(); // 更新灰色区域 void refreshPreviewArea(QPoint mousePos); // 更新预览区域 + + void captureAndClose(); // 截图并关闭窗体 }; #endif // FREESNAPDIALOG_H diff --git a/SmartScreenSnapper/src/publicdata.cpp b/SmartScreenSnapper/src/publicdata.cpp index 2daa4f6..9328729 100644 --- a/SmartScreenSnapper/src/publicdata.cpp +++ b/SmartScreenSnapper/src/publicdata.cpp @@ -21,6 +21,7 @@ bool PublicData::hotKeyNoWait = true; bool PublicData::includeCursor = false; bool PublicData::noBorder = false; bool PublicData::copyToClipBoardAfterSnap = false; +bool PublicData::freeSnapReleaseMouseCapture = false; bool PublicData::editMode = false; QString PublicData::gifSavePath = ""; QString PublicData::styleName = ""; @@ -101,6 +102,7 @@ void PublicData::readSettings() saveImageQuality = qSettings.value("Config/SaveImageQuality", -1).toInt(); snapMethod = qSettings.value("Config/SnapMethod", SnapMethod2).toInt(); copyToClipBoardAfterSnap = qSettings.value("Config/CopyToClipBoardAfterSnap", false).toBool(); + freeSnapReleaseMouseCapture = qSettings.value("Config/FreeSnapReleaseMouseCapture", false).toBool(); gifSavePath = qSettings.value("Tool/GIFSavePath", "").toString(); styleName = qSettings.value("Config/StyleName", "").toString(); fileNameTemplate = qSettings.value("Config/FileNameTemplate", "").toString(); @@ -136,6 +138,7 @@ void PublicData::writeSettings() qSettings.setValue("Config/SaveImageQuality", saveImageQuality); qSettings.setValue("Config/SnapMethod", snapMethod); qSettings.setValue("Config/CopyToClipBoardAfterSnap", copyToClipBoardAfterSnap); + qSettings.setValue("Config/FreeSnapReleaseMouseCapture", freeSnapReleaseMouseCapture); qSettings.setValue("Tool/GIFSavePath", gifSavePath); qSettings.setValue("Config/StyleName", styleName); qSettings.setValue("Config/FileNameTemplate", fileNameTemplate); diff --git a/SmartScreenSnapper/src/publicdata.h b/SmartScreenSnapper/src/publicdata.h index b29b676..1517ebd 100644 --- a/SmartScreenSnapper/src/publicdata.h +++ b/SmartScreenSnapper/src/publicdata.h @@ -64,6 +64,8 @@ class PublicData { static bool copyToClipBoardAfterSnap; + static bool freeSnapReleaseMouseCapture; + static ShotTypeItem snapTypeItems[ScreenShotHelper::ShotType::Count]; static QPair imageExtName[6]; diff --git a/SmartScreenSnapper/src/settingdialog.cpp b/SmartScreenSnapper/src/settingdialog.cpp index c3581b6..a631c36 100644 --- a/SmartScreenSnapper/src/settingdialog.cpp +++ b/SmartScreenSnapper/src/settingdialog.cpp @@ -2,16 +2,16 @@ #include "publicdata.h" #include "ui_settingdialog.h" #include -#include #include -#include -#include #include +#include +#include #include +#include -SettingDialog::SettingDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::SettingDialog) +SettingDialog::SettingDialog(QWidget* parent) + : QDialog(parent) + , ui(new Ui::SettingDialog) { ui->setupUi(this); @@ -21,9 +21,9 @@ SettingDialog::SettingDialog(QWidget *parent) : ui->listWidgetSetting->addItem(new QListWidgetItem(tabBar->tabIcon(i), tabBar->tabText(i), ui->listWidgetSetting)); } - readAndInitSettings(); // 进行初始设置 + readAndInitSettings(); // 进行初始设置 - initConnect(); // 在控件状态(选中、值等)发生改变时改变PublicData里的值 + initConnect(); // 在控件状态(选中、值等)发生改变时改变PublicData里的值 } SettingDialog::~SettingDialog() @@ -38,67 +38,67 @@ SettingDialog::~SettingDialog() void SettingDialog::initConnect() { - connect(ui->listWidgetSetting, &QListWidget::currentRowChanged, this, [=](int currentRow){ + connect(ui->listWidgetSetting, &QListWidget::currentRowChanged, this, [=](int currentRow) { ui->tabWidgetSetting->setCurrentIndex(currentRow); }); - connect(ui->comboBoxSnapMethod, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index){ + connect(ui->comboBoxSnapMethod, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index) { if (index < SNAPMETHOD) { PublicData::snapMethod = index; } }); - connect(ui->keySequenceEditHotKey, &QKeySequenceEdit::keySequenceChanged, this, [=](const QKeySequence &keySequence){ + connect(ui->keySequenceEditHotKey, &QKeySequenceEdit::keySequenceChanged, this, [=](const QKeySequence& keySequence) { PublicData::snapTypeItems[ui->comboBoxSnapType->currentIndex()].hotKey = keySequence.toString(); }); - connect(ui->checkBoxNoBorder, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->checkBoxNoBorder, &QCheckBox::stateChanged, this, [=](int state) { PublicData::noBorder = state; }); - connect(ui->horizontalSliderWaitTime, &QAbstractSlider::valueChanged, this, [=](int value){ + connect(ui->horizontalSliderWaitTime, &QAbstractSlider::valueChanged, this, [=](int value) { ui->labelWaitTime->setText(tr("截图前等待时间: ") + QString::number(value) + tr("s")); if (ui->comboBoxSnapType->currentIndex() <= ScreenShotHelper::ShotType::Count) { PublicData::snapTypeItems[ui->comboBoxSnapType->currentIndex()].waitTime = value; } }); - connect(ui->cbManualSaveAfterShot, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->cbManualSaveAfterShot, &QCheckBox::stateChanged, this, [=](int state) { int index = ui->comboBoxSnapType->currentIndex(); if (index <= (int)(sizeof(PublicData::snapTypeItems) / sizeof(ShotTypeItem))) { PublicData::snapTypeItems[index].isManualSave = state; } }); - connect(ui->toolButtonDeleteHotKey, &QAbstractButton::clicked, this, [=](){ + connect(ui->toolButtonDeleteHotKey, &QAbstractButton::clicked, this, [=]() { ui->keySequenceEditHotKey->clear(); }); - connect(ui->checkBoxRunWithWindows, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->checkBoxRunWithWindows, &QCheckBox::stateChanged, this, [=](int state) { runWithWindows(state); }); - connect(ui->toolButtonAutoSavePath, &QAbstractButton::clicked, this, [=](){ + connect(ui->toolButtonAutoSavePath, &QAbstractButton::clicked, this, [=]() { QString dirPath = QFileDialog::getExistingDirectory(this, tr("选择目录"), - PublicData::snapTypeItems[ui->comboBoxSnapType->currentIndex()].autoSavePath, QFileDialog::ShowDirsOnly); + PublicData::snapTypeItems[ui->comboBoxSnapType->currentIndex()].autoSavePath, QFileDialog::ShowDirsOnly); if (!dirPath.isEmpty()) { ui->lineEditAutoSavePath->setText(dirPath); } }); - connect(ui->toolButtonQssPath, &QAbstractButton::clicked, this, [=](){ + connect(ui->toolButtonQssPath, &QAbstractButton::clicked, this, [=]() { QString dirPath = QFileDialog::getOpenFileName(this, tr("选择QSS文件"), - PublicData::qssPath, tr("QSS文件(*.qss);;CSS文件(*.css);;所有文件(*.*)")); + PublicData::qssPath, tr("QSS文件(*.qss);;CSS文件(*.css);;所有文件(*.*)")); if (!dirPath.isEmpty()) { ui->lineEditQssPath->setText(dirPath); } }); - connect(ui->pushButtonOpenConfigFile, &QAbstractButton::clicked, this, [=](){ + connect(ui->pushButtonOpenConfigFile, &QAbstractButton::clicked, this, [=]() { QDesktopServices::openUrl(QUrl::fromLocalFile(PublicData::getConfigFilePath())); }); - connect(ui->cbAutoSaveAfterShot, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->cbAutoSaveAfterShot, &QCheckBox::stateChanged, this, [=](int state) { int index = ui->comboBoxSnapType->currentIndex(); if (index <= (int)(sizeof(PublicData::snapTypeItems) / sizeof(ShotTypeItem))) { PublicData::snapTypeItems[index].isAutoSave = state; @@ -108,71 +108,75 @@ void SettingDialog::initConnect() ui->comboBoxAutoSaveExtName->setEnabled(state); }); - connect(ui->lineEditAutoSavePath, &QLineEdit::editingFinished, this, [=](){ + connect(ui->lineEditAutoSavePath, &QLineEdit::editingFinished, this, [=]() { int index = ui->comboBoxSnapType->currentIndex(); if (index <= (int)(sizeof(PublicData::snapTypeItems) / sizeof(ShotTypeItem))) { PublicData::snapTypeItems[index].autoSavePath = ui->lineEditAutoSavePath->text(); } }); - connect(ui->lineEditAutoSavePath, &QLineEdit::textChanged, this, [=](const QString &text){ + connect(ui->lineEditAutoSavePath, &QLineEdit::textChanged, this, [=](const QString& text) { int index = ui->comboBoxSnapType->currentIndex(); if (index <= (int)(sizeof(PublicData::snapTypeItems) / sizeof(ShotTypeItem))) { PublicData::snapTypeItems[index].autoSavePath = text; } }); - connect(ui->lineEditQssPath, &QLineEdit::editingFinished, this, [=](){ + connect(ui->lineEditQssPath, &QLineEdit::editingFinished, this, [=]() { PublicData::qssPath = ui->lineEditQssPath->text(); }); - connect(ui->lineEditQssPath, &QLineEdit::textChanged, this, [=](const QString &text){ + connect(ui->lineEditQssPath, &QLineEdit::textChanged, this, [=](const QString& text) { PublicData::qssPath = text; }); - connect(ui->lineEditFileNameTemplate, &QLineEdit::editingFinished, this, [=](){ + connect(ui->lineEditFileNameTemplate, &QLineEdit::editingFinished, this, [=]() { PublicData::fileNameTemplate = ui->lineEditFileNameTemplate->text(); }); - connect(ui->lineEditFileNameTemplate, &QLineEdit::textChanged, this, [=](const QString &text){ + connect(ui->lineEditFileNameTemplate, &QLineEdit::textChanged, this, [=](const QString& text) { PublicData::fileNameTemplate = text; ui->lineEditFileNamePreview->setText( - ScreenShotHelper::getPictureName(ScreenShotHelper::ScreenShot)); + ScreenShotHelper::getPictureName(ScreenShotHelper::ScreenShot)); }); // 自动保存格式,有信号重载 - connect(ui->comboBoxAutoSaveExtName, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index){ + connect(ui->comboBoxAutoSaveExtName, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index) { int i = ui->comboBoxSnapType->currentIndex(); if (i <= ScreenShotHelper::ShotType::Count) { PublicData::snapTypeItems[i].autoSaveExtName = ui->comboBoxAutoSaveExtName->itemData(index).value(); } }); - connect(ui->checkBoxClickCloseToTray, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->checkBoxClickCloseToTray, &QCheckBox::stateChanged, this, [=](int state) { PublicData::clickCloseToTray = state; }); - connect(ui->checkBoxPlaySound, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->checkBoxPlaySound, &QCheckBox::stateChanged, this, [=](int state) { PublicData::isPlaySound = state; }); - connect(ui->comboBoxMdiWindowInitState, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index){ + connect(ui->comboBoxMdiWindowInitState, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index) { PublicData::mdiWindowInitState = ui->comboBoxMdiWindowInitState->itemData(index).value(); }); - connect(ui->checkBoxHotKeyNoWait, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->checkBoxHotKeyNoWait, &QCheckBox::stateChanged, this, [=](int state) { PublicData::hotKeyNoWait = state; }); - connect(ui->checkBoxIncludeCursor, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->checkBoxIncludeCursor, &QCheckBox::stateChanged, this, [=](int state) { PublicData::includeCursor = state; }); - connect(ui->checkBoxCopyToClipBoardAfterSnap, &QCheckBox::stateChanged, this, [=](int state){ + connect(ui->checkBoxCopyToClipBoardAfterSnap, &QCheckBox::stateChanged, this, [=](int state) { PublicData::copyToClipBoardAfterSnap = state; }); - connect(ui->spinBoxImageQuality, &QSpinBox::valueChanged, this, [=](int i){ + connect(ui->checkBoxFreeSnapReleaseMouseCapture, &QCheckBox::stateChanged, this, [=](int state) { + PublicData::freeSnapReleaseMouseCapture = state; + }); + + connect(ui->spinBoxImageQuality, &QSpinBox::valueChanged, this, [=](int i) { PublicData::saveImageQuality = i; }); } @@ -183,7 +187,7 @@ void SettingDialog::readAndInitSettings() ui->comboBoxAutoSaveExtName->addItem(item.second, item.first); } - connect(ui->comboBoxSnapType, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index){ + connect(ui->comboBoxSnapType, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index) { if (index <= ScreenShotHelper::ShotType::Count) { ui->labelWaitTime->setText(tr("截图前等待时间: ") + QString::number(PublicData::snapTypeItems[index].waitTime) + tr("s")); ui->horizontalSliderWaitTime->setValue(PublicData::snapTypeItems[index].waitTime); @@ -215,7 +219,7 @@ void SettingDialog::readAndInitSettings() ui->comboBoxSnapMethod->addItem(tr("方式1\n(Aero开启时部分区域会透明;截取例如QQ等部分窗体为黑色)")); ui->comboBoxSnapMethod->addItem(tr("方式2")); - for (auto key : PublicData::mdiWindowInitStates.keys()) { + foreach (const auto& key, PublicData::mdiWindowInitStates.keys()) { ui->comboBoxMdiWindowInitState->addItem(PublicData::mdiWindowInitStates[key], key); } ui->comboBoxMdiWindowInitState->setCurrentText(PublicData::mdiWindowInitStates[PublicData::mdiWindowInitState]); @@ -227,6 +231,7 @@ void SettingDialog::readAndInitSettings() ui->spinBoxImageQuality->setValue(PublicData::saveImageQuality); ui->checkBoxNoBorder->setChecked(PublicData::noBorder); ui->checkBoxCopyToClipBoardAfterSnap->setChecked(PublicData::copyToClipBoardAfterSnap); + ui->checkBoxFreeSnapReleaseMouseCapture->setChecked(PublicData::freeSnapReleaseMouseCapture); ui->comboBoxSnapMethod->setCurrentIndex(PublicData::snapMethod); ui->lineEditQssPath->setText(PublicData::qssPath); ui->lineEditFileNameTemplate->setText(PublicData::fileNameTemplate); @@ -236,16 +241,16 @@ void SettingDialog::readAndInitSettings() QSettings qSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); QString value = qSettings.value(QApplication::applicationName()).toString(); QString appPath = QApplication::applicationFilePath(); - appPath = appPath.replace("/","\\"); + appPath = appPath.replace("/", "\\"); ui->checkBoxRunWithWindows->setChecked(value == "\"" + appPath + "\"" + " -autorun"); } void SettingDialog::runWithWindows(bool enable) { - QSettings qSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat); + QSettings qSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); QString appPath = QApplication::applicationFilePath(); QString appName = QApplication::applicationName(); - appPath = appPath.replace("/","\\"); + appPath = appPath.replace("/", "\\"); if (enable) { qSettings.setValue(appName, "\"" + appPath + "\"" + " -autorun"); } else { diff --git a/SmartScreenSnapper/ui/settingdialog.ui b/SmartScreenSnapper/ui/settingdialog.ui index 41d4c87..5a4a8b5 100644 --- a/SmartScreenSnapper/ui/settingdialog.ui +++ b/SmartScreenSnapper/ui/settingdialog.ui @@ -32,12 +32,18 @@ 16777215 + + + 32 + 32 + + - 3 + 2 false @@ -73,8 +79,8 @@ 0 0 - 429 - 475 + 431 + 474 @@ -518,8 +524,8 @@ 0 0 - 429 - 475 + 431 + 474 @@ -552,6 +558,13 @@ + + + + 自由截图时松开鼠标自动捕捉 + + + @@ -651,8 +664,8 @@ 0 0 - 429 - 475 + 431 + 474 @@ -729,6 +742,8 @@ <html><head><meta name="qrichtext" content="1" /><meta charset="utf-8" /><style type="text/css"> p, li { white-space: pre-wrap; } hr { height: 1px; border-width: 0; } +li.unchecked::marker { content: "\2610"; } +li.checked::marker { content: "\2612"; } </style></head><body style=" font-family:'Microsoft YaHei UI'; font-size:9pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'SimSun';">默认:&lt;Capture&gt; - &lt;yyyy&gt;-&lt;MM&gt;-&lt;dd&gt;_&lt;HH&gt;&lt;mm&gt;&lt;ss&gt;_&lt;Rand&gt;</span></p> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'SimSun';">注意:输入的文件名模板非法时,使用默认文件名模板</span></p> @@ -789,8 +804,8 @@ hr { height: 1px; border-width: 0; } 0 0 - 429 - 475 + 431 + 474 @@ -812,7 +827,7 @@ hr { height: 1px; border-width: 0; } - 存储图像质量(-1表示使用默认配置) + 存储图像质量(-1 表示使用默认配置)