Skip to content

Commit

Permalink
[optimize]加速播放提示音
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Nov 29, 2022
1 parent 2157b67 commit 37b2ba7
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 39 deletions.
18 changes: 5 additions & 13 deletions SmartScreenSnapper/SmartScreenSnapper.pro.user
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 8.0.2, 2022-10-30T14:33:11. -->
<!-- Written by QtCreator 8.0.2, 2022-11-29T21:48:52. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{94c2d154-65b9-4d95-af4f-31f397cd8fb5}</value>
<value type="QByteArray">{3fe38946-4407-40d9-a72b-c3edd3f2d295}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
Expand Down Expand Up @@ -70,11 +70,6 @@
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
<value type="QString">-fno-delayed-template-parsing</value>
</valuelist>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">false</value>
<value type="QString" key="ClangCodeModel.WarningConfigId">{ffb2ff00-740b-4965-afd4-eb474f10ba05}</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
Expand All @@ -85,9 +80,6 @@
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
<valuemap type="QVariantMap" key="CppEditor.QuickFix">
<value type="bool" key="UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data>
Expand All @@ -97,7 +89,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 6.4.0 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 6.4.0 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt6.640.win64_mingw_kit</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
Expand Down Expand Up @@ -188,7 +180,7 @@
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">true</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<valuelist type="QVariantList" key="QtProjectManager.QMakeBuildStep.SelectedAbis"/>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
Expand Down Expand Up @@ -250,7 +242,7 @@
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">E:/C++/SmartScreenSnapper/build-Qt_6_4_0_MinGW-Debug</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">E:/C++/SmartScreenSnapper/build-Qt_6_4_0_MinGW-Release</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
2 changes: 1 addition & 1 deletion SmartScreenSnapper/src/freesnap/freesnaprect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ FreeSnapRect::FreeSnapRect(QWidget* parent, Qt::WindowFlags) : QFrame(parent)

void FreeSnapRect::mouseMoveEvent(QMouseEvent *event)
{
emit mouseMove(event->pos());
QWidget::mouseMoveEvent(event);
emit mouseMove(event->pos());
}
51 changes: 35 additions & 16 deletions SmartScreenSnapper/src/graphicsview.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "graphicsview.h"
#include "src/publicdata.h"
#include <QScrollBar>
#include <QApplication>
#include <QDrag>
#include <QDir>
#include <QBuffer>
#include <QTemporaryFile>

GraphicsView::GraphicsView(QWidget *parent):
QGraphicsView(parent),
mousePressed(false),
lastMousePoint(-1, -1),
mimeData(nullptr)
fileName("")
{

}
Expand Down Expand Up @@ -43,23 +46,18 @@ void GraphicsView::mousePressEvent(QMouseEvent *event)
mousePressed = true;
lastMousePoint.setX(event->position().x());
lastMousePoint.setY(event->position().y());

if (event->buttons() & Qt::RightButton) { // 准备右键拖出时的图片
QPixmap pixmap(scene()->width(), scene()->height());
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
scene()->render(&painter);
mimeData = new QMimeData();
mimeData->clear();
mimeData->setImageData(pixmap.toImage());
}
}

void GraphicsView::mouseReleaseEvent(QMouseEvent *)
{
mousePressed = false;
}

void GraphicsView::setFileName(const QString &newFileName)
{
fileName = newFileName;
}

void GraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
Expand All @@ -70,13 +68,34 @@ void GraphicsView::mouseMoveEvent(QMouseEvent *event)
event->position().y() + lastMousePoint.y());
}
}
if (event->buttons() & Qt::RightButton) {
QDrag* drag = new QDrag(this); //The QDrag must be constructed on the heap with a parent QObject
if (event->buttons() & Qt::LeftButton && event->modifiers() == Qt::ControlModifier) {
QPixmap pixmap(scene()->width(), scene()->height());
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
scene()->render(&painter);

QPixmap dragPixmap(scene()->width(), scene()->height());
dragPixmap.fill(Qt::transparent);
QPainter dragPixmapPainter(&dragPixmap);
dragPixmapPainter.setOpacity(0.5f);
dragPixmapPainter.drawPixmap(0, 0, pixmap);

QTemporaryFile tempFile = QTemporaryFile();
tempFile.open();
if (!fileName.isEmpty()) {
tempFile.rename(QDir::tempPath() + "/" + fileName + PublicData::imageExtName[0].first);
}
pixmap.save(&tempFile, nullptr, PublicData::saveImageQuality);

QMimeData *mimeData = new QMimeData();
mimeData->clear();
mimeData->setImageData(pixmap.toImage());
mimeData->setUrls(QList<QUrl>() << QUrl::fromLocalFile(tempFile.fileName()));
QDrag *drag = new QDrag(this); // The QDrag must be constructed on the heap with a parent QObject
drag->setPixmap(dragPixmap);
drag->setMimeData(mimeData);
drag->setHotSpot(event->pos());
drag->setHotSpot(mapToScene(event->pos()).toPoint());
drag->exec(Qt::CopyAction, Qt::CopyAction);
} else {
delete mimeData;
}
lastMousePoint.setX(event->position().x());
lastMousePoint.setY(event->position().y());
Expand Down
4 changes: 3 additions & 1 deletion SmartScreenSnapper/src/graphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class GraphicsView : public QGraphicsView

void mouseReleaseEvent(QMouseEvent *event) override;

void setFileName(const QString &newFileName);

private:
bool mousePressed;

QPoint lastMousePoint;

QMimeData *mimeData;
QString fileName;
};

#endif // GRAPHICSVIEW_H
28 changes: 20 additions & 8 deletions SmartScreenSnapper/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QNetworkReply>
#include <QSoundEffect>
#include <QMimeData>
#include <QSessionManager>
#include "publicdata.h"
#include "mdiwindow.h"
#include "aboutdialog.h"
Expand All @@ -40,9 +41,21 @@ bool MainWindow::closeAllNotSave = false;

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow) {
ui(new Ui::MainWindow),
soundEffect(this)
{
ui->setupUi(this);

// connect(qApp, &QGuiApplication::commitDataRequest, this, [=](QSessionManager &manager){
// qDebug() << manager.allowsInteraction();
// if (manager.allowsInteraction()) {
// manager.cancel();
// close();
// } else {
// // we did not get permission to interact, then
// // do something reasonable instead
// }
// });
initSystemTray();
initStatusBar(); // 这句里面有控件初始化,在设置值之前进行

Expand Down Expand Up @@ -390,20 +403,19 @@ void MainWindow::snapSuccessCallback(ScreenShotHelper::ShotType shotType, QPixma
activeWindow = createMDIWindow();
activeWindow->setName(name);
activeWindow->setPixmap(pixmap);
activeWindow->setShotType(shotType);

if (PublicData::copyToClipBoardAfterSnap) {
QApplication::clipboard()->setPixmap(pixmap);
}
if (PublicData::isPlaySound) {
QSoundEffect *startSound = new QSoundEffect(this);
startSound->setSource(QUrl::fromLocalFile(":/sound/typewriter.wav"));
connect(startSound, &QSoundEffect::playingChanged, this, [=](){
if (!startSound->isPlaying()) {
disconnect(this, nullptr, startSound, nullptr);
delete startSound;
soundEffect.setSource(QUrl::fromLocalFile(":/sound/typewriter.wav"));
connect(&soundEffect, &QSoundEffect::playingChanged, this, [=](){
if (!soundEffect.isPlaying()) {
disconnect(this, nullptr, &soundEffect, nullptr);
}
});
startSound->play();
soundEffect.play();
}

ShotTypeItem snapTypeItem = PublicData::snapTypeItems[shotType];
Expand Down
3 changes: 3 additions & 0 deletions SmartScreenSnapper/src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <windows.h>
#include <Mmsystem.h>
#include <QSpinBox>
#include <QSoundEffect>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
Expand Down Expand Up @@ -71,6 +72,8 @@ class MainWindow : public QMainWindow

QNetworkAccessManager *networkAccessManager;

QSoundEffect soundEffect;

protected:

void closeEvent(QCloseEvent *event);
Expand Down
11 changes: 11 additions & 0 deletions SmartScreenSnapper/src/mdiwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ QString MdiWindow::getName() {

void MdiWindow::setName(QString name) {
this->name = name;
ui->graphicsView->setFileName(name);
if (saved) {
setWindowTitle(name);
setListItemName(name);
Expand All @@ -80,6 +81,16 @@ void MdiWindow::setSaved(bool saved) {
}
}

ScreenShotHelper::ShotType MdiWindow::getShotType() const
{
return shotType;
}

void MdiWindow::setShotType(ScreenShotHelper::ShotType newShotType)
{
shotType = newShotType;
}

void MdiWindow::closeEvent(QCloseEvent *event) {
if (!saved && !MainWindow::closeAllNotSave && !MainWindow::noToAllClicked) {
QMessageBox::StandardButton messageBoxResult = QMessageBox::StandardButton(QMessageBox::Cancel);
Expand Down
7 changes: 7 additions & 0 deletions SmartScreenSnapper/src/mdiwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QMdiSubWindow>
#include <QListWidgetItem>
#include <QCloseEvent>
#include "screenshothelper.h"

namespace Ui {
class MdiWindowWidget;
Expand Down Expand Up @@ -47,6 +48,10 @@ class MdiWindow : public QMdiSubWindow

bool saveByPath(QString filePath);

ScreenShotHelper::ShotType getShotType() const;

void setShotType(ScreenShotHelper::ShotType newShotType);

private:

void setSaved(bool saved);
Expand All @@ -63,6 +68,8 @@ class MdiWindow : public QMdiSubWindow

double imageScale;

ScreenShotHelper::ShotType shotType;

protected:
void closeEvent(QCloseEvent *event) override;
};
Expand Down

0 comments on commit 37b2ba7

Please sign in to comment.