Skip to content

Commit

Permalink
[feature] Support automatic screenshot after releasing the mouse unde…
Browse files Browse the repository at this point in the history
…r the free snap function
  • Loading branch information
SkyD666 committed Jan 29, 2024
1 parent 13cad09 commit 528a1d2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 63 deletions.
2 changes: 1 addition & 1 deletion SmartScreenSnapper/SmartScreenSnapper.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 12.0.1, 2024-01-28T00:57:24. -->
<!-- Written by QtCreator 12.0.1, 2024-01-28T19:57:54. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
30 changes: 20 additions & 10 deletions SmartScreenSnapper/src/freesnapdialog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "freesnapdialog.h"
#include "publicdata.h"
#include "ui_freesnapdialog.h"
#include <QApplication>
#include <QClipboard>
Expand Down Expand Up @@ -146,6 +147,10 @@ void FreeSnapDialog::mouseReleaseEvent(QMouseEvent* event)
pressedInBottomArea = false;

deltaHeight = deltaWidth = 0;

if (PublicData::freeSnapReleaseMouseCapture) {
captureAndClose();
}
}

void FreeSnapDialog::mouseMoveEvent(QMouseEvent* event)
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions SmartScreenSnapper/src/freesnapdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class FreeSnapDialog : public BaseFullScreenSnapDialog {

void refreshGrayArea(); // 更新灰色区域
void refreshPreviewArea(QPoint mousePos); // 更新预览区域

void captureAndClose(); // 截图并关闭窗体
};

#endif // FREESNAPDIALOG_H
3 changes: 3 additions & 0 deletions SmartScreenSnapper/src/publicdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions SmartScreenSnapper/src/publicdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class PublicData {

static bool copyToClipBoardAfterSnap;

static bool freeSnapReleaseMouseCapture;

static ShotTypeItem snapTypeItems[ScreenShotHelper::ShotType::Count];

static QPair<QString, QString> imageExtName[6];
Expand Down
89 changes: 47 additions & 42 deletions SmartScreenSnapper/src/settingdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#include "publicdata.h"
#include "ui_settingdialog.h"
#include <QCheckBox>
#include <QFileDialog>
#include <QDebug>
#include <QSettings>
#include <QMessageBox>
#include <QDesktopServices>
#include <QFileDialog>
#include <QMessageBox>
#include <QRegularExpression>
#include <QSettings>

SettingDialog::SettingDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingDialog)
SettingDialog::SettingDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::SettingDialog)
{
ui->setupUi(this);

Expand All @@ -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()
Expand All @@ -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<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
connect(ui->comboBoxSnapMethod, QOverload<int>::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;
Expand All @@ -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<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
connect(ui->comboBoxAutoSaveExtName, QOverload<int>::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<QString>();
}
});

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<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
connect(ui->comboBoxMdiWindowInitState, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
PublicData::mdiWindowInitState = ui->comboBoxMdiWindowInitState->itemData(index).value<Qt::WindowState>();
});

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;
});
}
Expand All @@ -183,7 +187,7 @@ void SettingDialog::readAndInitSettings()
ui->comboBoxAutoSaveExtName->addItem(item.second, item.first);
}

connect(ui->comboBoxSnapType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
connect(ui->comboBoxSnapType, QOverload<int>::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);
Expand Down Expand Up @@ -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]);
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 528a1d2

Please sign in to comment.