Skip to content

Commit 528a1d2

Browse files
committed
[feature] Support automatic screenshot after releasing the mouse under the free snap function
1 parent 13cad09 commit 528a1d2

File tree

7 files changed

+100
-63
lines changed

7 files changed

+100
-63
lines changed

SmartScreenSnapper/SmartScreenSnapper.pro.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 12.0.1, 2024-01-28T00:57:24. -->
3+
<!-- Written by QtCreator 12.0.1, 2024-01-28T19:57:54. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

SmartScreenSnapper/src/freesnapdialog.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "freesnapdialog.h"
2+
#include "publicdata.h"
23
#include "ui_freesnapdialog.h"
34
#include <QApplication>
45
#include <QClipboard>
@@ -146,6 +147,10 @@ void FreeSnapDialog::mouseReleaseEvent(QMouseEvent* event)
146147
pressedInBottomArea = false;
147148

148149
deltaHeight = deltaWidth = 0;
150+
151+
if (PublicData::freeSnapReleaseMouseCapture) {
152+
captureAndClose();
153+
}
149154
}
150155

151156
void FreeSnapDialog::mouseMoveEvent(QMouseEvent* event)
@@ -463,20 +468,25 @@ void FreeSnapDialog::refreshPreviewArea(QPoint mousePos)
463468
ui->labelPreview->setPixmap(processedPixmap.scaled(ui->labelPreview->width(), ui->labelPreview->height(), Qt::KeepAspectRatio));
464469
}
465470

471+
void FreeSnapDialog::captureAndClose()
472+
{
473+
auto geometry = ui->frameRect->geometry();
474+
auto devicePixelRatio = ui->graphicsView->devicePixelRatio();
475+
geometry.setRect(geometry.x() * devicePixelRatio,
476+
geometry.y() * devicePixelRatio,
477+
geometry.width() * devicePixelRatio,
478+
geometry.height() * devicePixelRatio);
479+
*this->resultPixmap = fullScreenPixmap.copy(geometry);
480+
this->resultPixmap->setDevicePixelRatio(1);
481+
captured = true;
482+
close();
483+
}
484+
466485
// 键盘事件
467486
void FreeSnapDialog::keyPressEvent(QKeyEvent* event)
468487
{
469488
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
470-
auto geometry = ui->frameRect->geometry();
471-
auto devicePixelRatio = ui->graphicsView->devicePixelRatio();
472-
geometry.setRect(geometry.x() * devicePixelRatio,
473-
geometry.y() * devicePixelRatio,
474-
geometry.width() * devicePixelRatio,
475-
geometry.height() * devicePixelRatio);
476-
*this->resultPixmap = fullScreenPixmap.copy(geometry);
477-
this->resultPixmap->setDevicePixelRatio(1);
478-
captured = true;
479-
close();
489+
captureAndClose();
480490
return;
481491
} else if (event->key() == Qt::Key_Escape) {
482492
captured = false;

SmartScreenSnapper/src/freesnapdialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class FreeSnapDialog : public BaseFullScreenSnapDialog {
7575

7676
void refreshGrayArea(); // 更新灰色区域
7777
void refreshPreviewArea(QPoint mousePos); // 更新预览区域
78+
79+
void captureAndClose(); // 截图并关闭窗体
7880
};
7981

8082
#endif // FREESNAPDIALOG_H

SmartScreenSnapper/src/publicdata.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bool PublicData::hotKeyNoWait = true;
2121
bool PublicData::includeCursor = false;
2222
bool PublicData::noBorder = false;
2323
bool PublicData::copyToClipBoardAfterSnap = false;
24+
bool PublicData::freeSnapReleaseMouseCapture = false;
2425
bool PublicData::editMode = false;
2526
QString PublicData::gifSavePath = "";
2627
QString PublicData::styleName = "";
@@ -101,6 +102,7 @@ void PublicData::readSettings()
101102
saveImageQuality = qSettings.value("Config/SaveImageQuality", -1).toInt();
102103
snapMethod = qSettings.value("Config/SnapMethod", SnapMethod2).toInt();
103104
copyToClipBoardAfterSnap = qSettings.value("Config/CopyToClipBoardAfterSnap", false).toBool();
105+
freeSnapReleaseMouseCapture = qSettings.value("Config/FreeSnapReleaseMouseCapture", false).toBool();
104106
gifSavePath = qSettings.value("Tool/GIFSavePath", "").toString();
105107
styleName = qSettings.value("Config/StyleName", "").toString();
106108
fileNameTemplate = qSettings.value("Config/FileNameTemplate", "").toString();
@@ -136,6 +138,7 @@ void PublicData::writeSettings()
136138
qSettings.setValue("Config/SaveImageQuality", saveImageQuality);
137139
qSettings.setValue("Config/SnapMethod", snapMethod);
138140
qSettings.setValue("Config/CopyToClipBoardAfterSnap", copyToClipBoardAfterSnap);
141+
qSettings.setValue("Config/FreeSnapReleaseMouseCapture", freeSnapReleaseMouseCapture);
139142
qSettings.setValue("Tool/GIFSavePath", gifSavePath);
140143
qSettings.setValue("Config/StyleName", styleName);
141144
qSettings.setValue("Config/FileNameTemplate", fileNameTemplate);

SmartScreenSnapper/src/publicdata.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class PublicData {
6464

6565
static bool copyToClipBoardAfterSnap;
6666

67+
static bool freeSnapReleaseMouseCapture;
68+
6769
static ShotTypeItem snapTypeItems[ScreenShotHelper::ShotType::Count];
6870

6971
static QPair<QString, QString> imageExtName[6];

SmartScreenSnapper/src/settingdialog.cpp

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
#include "publicdata.h"
33
#include "ui_settingdialog.h"
44
#include <QCheckBox>
5-
#include <QFileDialog>
65
#include <QDebug>
7-
#include <QSettings>
8-
#include <QMessageBox>
96
#include <QDesktopServices>
7+
#include <QFileDialog>
8+
#include <QMessageBox>
109
#include <QRegularExpression>
10+
#include <QSettings>
1111

12-
SettingDialog::SettingDialog(QWidget *parent) :
13-
QDialog(parent),
14-
ui(new Ui::SettingDialog)
12+
SettingDialog::SettingDialog(QWidget* parent)
13+
: QDialog(parent)
14+
, ui(new Ui::SettingDialog)
1515
{
1616
ui->setupUi(this);
1717

@@ -21,9 +21,9 @@ SettingDialog::SettingDialog(QWidget *parent) :
2121
ui->listWidgetSetting->addItem(new QListWidgetItem(tabBar->tabIcon(i), tabBar->tabText(i), ui->listWidgetSetting));
2222
}
2323

24-
readAndInitSettings(); // 进行初始设置
24+
readAndInitSettings(); // 进行初始设置
2525

26-
initConnect(); // 在控件状态(选中、值等)发生改变时改变PublicData里的值
26+
initConnect(); // 在控件状态(选中、值等)发生改变时改变PublicData里的值
2727
}
2828

2929
SettingDialog::~SettingDialog()
@@ -38,67 +38,67 @@ SettingDialog::~SettingDialog()
3838

3939
void SettingDialog::initConnect()
4040
{
41-
connect(ui->listWidgetSetting, &QListWidget::currentRowChanged, this, [=](int currentRow){
41+
connect(ui->listWidgetSetting, &QListWidget::currentRowChanged, this, [=](int currentRow) {
4242
ui->tabWidgetSetting->setCurrentIndex(currentRow);
4343
});
4444

45-
connect(ui->comboBoxSnapMethod, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
45+
connect(ui->comboBoxSnapMethod, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
4646
if (index < SNAPMETHOD) {
4747
PublicData::snapMethod = index;
4848
}
4949
});
5050

51-
connect(ui->keySequenceEditHotKey, &QKeySequenceEdit::keySequenceChanged, this, [=](const QKeySequence &keySequence){
51+
connect(ui->keySequenceEditHotKey, &QKeySequenceEdit::keySequenceChanged, this, [=](const QKeySequence& keySequence) {
5252
PublicData::snapTypeItems[ui->comboBoxSnapType->currentIndex()].hotKey = keySequence.toString();
5353
});
5454

55-
connect(ui->checkBoxNoBorder, &QCheckBox::stateChanged, this, [=](int state){
55+
connect(ui->checkBoxNoBorder, &QCheckBox::stateChanged, this, [=](int state) {
5656
PublicData::noBorder = state;
5757
});
5858

59-
connect(ui->horizontalSliderWaitTime, &QAbstractSlider::valueChanged, this, [=](int value){
59+
connect(ui->horizontalSliderWaitTime, &QAbstractSlider::valueChanged, this, [=](int value) {
6060
ui->labelWaitTime->setText(tr("截图前等待时间: ") + QString::number(value) + tr("s"));
6161
if (ui->comboBoxSnapType->currentIndex() <= ScreenShotHelper::ShotType::Count) {
6262
PublicData::snapTypeItems[ui->comboBoxSnapType->currentIndex()].waitTime = value;
6363
}
6464
});
6565

66-
connect(ui->cbManualSaveAfterShot, &QCheckBox::stateChanged, this, [=](int state){
66+
connect(ui->cbManualSaveAfterShot, &QCheckBox::stateChanged, this, [=](int state) {
6767
int index = ui->comboBoxSnapType->currentIndex();
6868
if (index <= (int)(sizeof(PublicData::snapTypeItems) / sizeof(ShotTypeItem))) {
6969
PublicData::snapTypeItems[index].isManualSave = state;
7070
}
7171
});
7272

73-
connect(ui->toolButtonDeleteHotKey, &QAbstractButton::clicked, this, [=](){
73+
connect(ui->toolButtonDeleteHotKey, &QAbstractButton::clicked, this, [=]() {
7474
ui->keySequenceEditHotKey->clear();
7575
});
7676

77-
connect(ui->checkBoxRunWithWindows, &QCheckBox::stateChanged, this, [=](int state){
77+
connect(ui->checkBoxRunWithWindows, &QCheckBox::stateChanged, this, [=](int state) {
7878
runWithWindows(state);
7979
});
8080

81-
connect(ui->toolButtonAutoSavePath, &QAbstractButton::clicked, this, [=](){
81+
connect(ui->toolButtonAutoSavePath, &QAbstractButton::clicked, this, [=]() {
8282
QString dirPath = QFileDialog::getExistingDirectory(this, tr("选择目录"),
83-
PublicData::snapTypeItems[ui->comboBoxSnapType->currentIndex()].autoSavePath, QFileDialog::ShowDirsOnly);
83+
PublicData::snapTypeItems[ui->comboBoxSnapType->currentIndex()].autoSavePath, QFileDialog::ShowDirsOnly);
8484
if (!dirPath.isEmpty()) {
8585
ui->lineEditAutoSavePath->setText(dirPath);
8686
}
8787
});
8888

89-
connect(ui->toolButtonQssPath, &QAbstractButton::clicked, this, [=](){
89+
connect(ui->toolButtonQssPath, &QAbstractButton::clicked, this, [=]() {
9090
QString dirPath = QFileDialog::getOpenFileName(this, tr("选择QSS文件"),
91-
PublicData::qssPath, tr("QSS文件(*.qss);;CSS文件(*.css);;所有文件(*.*)"));
91+
PublicData::qssPath, tr("QSS文件(*.qss);;CSS文件(*.css);;所有文件(*.*)"));
9292
if (!dirPath.isEmpty()) {
9393
ui->lineEditQssPath->setText(dirPath);
9494
}
9595
});
9696

97-
connect(ui->pushButtonOpenConfigFile, &QAbstractButton::clicked, this, [=](){
97+
connect(ui->pushButtonOpenConfigFile, &QAbstractButton::clicked, this, [=]() {
9898
QDesktopServices::openUrl(QUrl::fromLocalFile(PublicData::getConfigFilePath()));
9999
});
100100

101-
connect(ui->cbAutoSaveAfterShot, &QCheckBox::stateChanged, this, [=](int state){
101+
connect(ui->cbAutoSaveAfterShot, &QCheckBox::stateChanged, this, [=](int state) {
102102
int index = ui->comboBoxSnapType->currentIndex();
103103
if (index <= (int)(sizeof(PublicData::snapTypeItems) / sizeof(ShotTypeItem))) {
104104
PublicData::snapTypeItems[index].isAutoSave = state;
@@ -108,71 +108,75 @@ void SettingDialog::initConnect()
108108
ui->comboBoxAutoSaveExtName->setEnabled(state);
109109
});
110110

111-
connect(ui->lineEditAutoSavePath, &QLineEdit::editingFinished, this, [=](){
111+
connect(ui->lineEditAutoSavePath, &QLineEdit::editingFinished, this, [=]() {
112112
int index = ui->comboBoxSnapType->currentIndex();
113113
if (index <= (int)(sizeof(PublicData::snapTypeItems) / sizeof(ShotTypeItem))) {
114114
PublicData::snapTypeItems[index].autoSavePath = ui->lineEditAutoSavePath->text();
115115
}
116116
});
117117

118-
connect(ui->lineEditAutoSavePath, &QLineEdit::textChanged, this, [=](const QString &text){
118+
connect(ui->lineEditAutoSavePath, &QLineEdit::textChanged, this, [=](const QString& text) {
119119
int index = ui->comboBoxSnapType->currentIndex();
120120
if (index <= (int)(sizeof(PublicData::snapTypeItems) / sizeof(ShotTypeItem))) {
121121
PublicData::snapTypeItems[index].autoSavePath = text;
122122
}
123123
});
124124

125-
connect(ui->lineEditQssPath, &QLineEdit::editingFinished, this, [=](){
125+
connect(ui->lineEditQssPath, &QLineEdit::editingFinished, this, [=]() {
126126
PublicData::qssPath = ui->lineEditQssPath->text();
127127
});
128128

129-
connect(ui->lineEditQssPath, &QLineEdit::textChanged, this, [=](const QString &text){
129+
connect(ui->lineEditQssPath, &QLineEdit::textChanged, this, [=](const QString& text) {
130130
PublicData::qssPath = text;
131131
});
132132

133-
connect(ui->lineEditFileNameTemplate, &QLineEdit::editingFinished, this, [=](){
133+
connect(ui->lineEditFileNameTemplate, &QLineEdit::editingFinished, this, [=]() {
134134
PublicData::fileNameTemplate = ui->lineEditFileNameTemplate->text();
135135
});
136136

137-
connect(ui->lineEditFileNameTemplate, &QLineEdit::textChanged, this, [=](const QString &text){
137+
connect(ui->lineEditFileNameTemplate, &QLineEdit::textChanged, this, [=](const QString& text) {
138138
PublicData::fileNameTemplate = text;
139139
ui->lineEditFileNamePreview->setText(
140-
ScreenShotHelper::getPictureName(ScreenShotHelper::ScreenShot));
140+
ScreenShotHelper::getPictureName(ScreenShotHelper::ScreenShot));
141141
});
142142

143143
// 自动保存格式,有信号重载
144-
connect(ui->comboBoxAutoSaveExtName, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
144+
connect(ui->comboBoxAutoSaveExtName, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
145145
int i = ui->comboBoxSnapType->currentIndex();
146146
if (i <= ScreenShotHelper::ShotType::Count) {
147147
PublicData::snapTypeItems[i].autoSaveExtName = ui->comboBoxAutoSaveExtName->itemData(index).value<QString>();
148148
}
149149
});
150150

151-
connect(ui->checkBoxClickCloseToTray, &QCheckBox::stateChanged, this, [=](int state){
151+
connect(ui->checkBoxClickCloseToTray, &QCheckBox::stateChanged, this, [=](int state) {
152152
PublicData::clickCloseToTray = state;
153153
});
154154

155-
connect(ui->checkBoxPlaySound, &QCheckBox::stateChanged, this, [=](int state){
155+
connect(ui->checkBoxPlaySound, &QCheckBox::stateChanged, this, [=](int state) {
156156
PublicData::isPlaySound = state;
157157
});
158158

159-
connect(ui->comboBoxMdiWindowInitState, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
159+
connect(ui->comboBoxMdiWindowInitState, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
160160
PublicData::mdiWindowInitState = ui->comboBoxMdiWindowInitState->itemData(index).value<Qt::WindowState>();
161161
});
162162

163-
connect(ui->checkBoxHotKeyNoWait, &QCheckBox::stateChanged, this, [=](int state){
163+
connect(ui->checkBoxHotKeyNoWait, &QCheckBox::stateChanged, this, [=](int state) {
164164
PublicData::hotKeyNoWait = state;
165165
});
166166

167-
connect(ui->checkBoxIncludeCursor, &QCheckBox::stateChanged, this, [=](int state){
167+
connect(ui->checkBoxIncludeCursor, &QCheckBox::stateChanged, this, [=](int state) {
168168
PublicData::includeCursor = state;
169169
});
170170

171-
connect(ui->checkBoxCopyToClipBoardAfterSnap, &QCheckBox::stateChanged, this, [=](int state){
171+
connect(ui->checkBoxCopyToClipBoardAfterSnap, &QCheckBox::stateChanged, this, [=](int state) {
172172
PublicData::copyToClipBoardAfterSnap = state;
173173
});
174174

175-
connect(ui->spinBoxImageQuality, &QSpinBox::valueChanged, this, [=](int i){
175+
connect(ui->checkBoxFreeSnapReleaseMouseCapture, &QCheckBox::stateChanged, this, [=](int state) {
176+
PublicData::freeSnapReleaseMouseCapture = state;
177+
});
178+
179+
connect(ui->spinBoxImageQuality, &QSpinBox::valueChanged, this, [=](int i) {
176180
PublicData::saveImageQuality = i;
177181
});
178182
}
@@ -183,7 +187,7 @@ void SettingDialog::readAndInitSettings()
183187
ui->comboBoxAutoSaveExtName->addItem(item.second, item.first);
184188
}
185189

186-
connect(ui->comboBoxSnapType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
190+
connect(ui->comboBoxSnapType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
187191
if (index <= ScreenShotHelper::ShotType::Count) {
188192
ui->labelWaitTime->setText(tr("截图前等待时间: ") + QString::number(PublicData::snapTypeItems[index].waitTime) + tr("s"));
189193
ui->horizontalSliderWaitTime->setValue(PublicData::snapTypeItems[index].waitTime);
@@ -215,7 +219,7 @@ void SettingDialog::readAndInitSettings()
215219
ui->comboBoxSnapMethod->addItem(tr("方式1\n(Aero开启时部分区域会透明;截取例如QQ等部分窗体为黑色)"));
216220
ui->comboBoxSnapMethod->addItem(tr("方式2"));
217221

218-
for (auto key : PublicData::mdiWindowInitStates.keys()) {
222+
foreach (const auto& key, PublicData::mdiWindowInitStates.keys()) {
219223
ui->comboBoxMdiWindowInitState->addItem(PublicData::mdiWindowInitStates[key], key);
220224
}
221225
ui->comboBoxMdiWindowInitState->setCurrentText(PublicData::mdiWindowInitStates[PublicData::mdiWindowInitState]);
@@ -227,6 +231,7 @@ void SettingDialog::readAndInitSettings()
227231
ui->spinBoxImageQuality->setValue(PublicData::saveImageQuality);
228232
ui->checkBoxNoBorder->setChecked(PublicData::noBorder);
229233
ui->checkBoxCopyToClipBoardAfterSnap->setChecked(PublicData::copyToClipBoardAfterSnap);
234+
ui->checkBoxFreeSnapReleaseMouseCapture->setChecked(PublicData::freeSnapReleaseMouseCapture);
230235
ui->comboBoxSnapMethod->setCurrentIndex(PublicData::snapMethod);
231236
ui->lineEditQssPath->setText(PublicData::qssPath);
232237
ui->lineEditFileNameTemplate->setText(PublicData::fileNameTemplate);
@@ -236,16 +241,16 @@ void SettingDialog::readAndInitSettings()
236241
QSettings qSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
237242
QString value = qSettings.value(QApplication::applicationName()).toString();
238243
QString appPath = QApplication::applicationFilePath();
239-
appPath = appPath.replace("/","\\");
244+
appPath = appPath.replace("/", "\\");
240245
ui->checkBoxRunWithWindows->setChecked(value == "\"" + appPath + "\"" + " -autorun");
241246
}
242247

243248
void SettingDialog::runWithWindows(bool enable)
244249
{
245-
QSettings qSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);
250+
QSettings qSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
246251
QString appPath = QApplication::applicationFilePath();
247252
QString appName = QApplication::applicationName();
248-
appPath = appPath.replace("/","\\");
253+
appPath = appPath.replace("/", "\\");
249254
if (enable) {
250255
qSettings.setValue(appName, "\"" + appPath + "\"" + " -autorun");
251256
} else {

0 commit comments

Comments
 (0)