From 13988c0b2af84965945f714518e10eeac8a5fee5 Mon Sep 17 00:00:00 2001 From: donghualin Date: Fri, 2 Dec 2022 18:30:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A3=B0=E9=9F=B3=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=BB=9A=E8=BD=AE=E8=B0=83=E8=8A=82=E9=9F=B3=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 适配插件接口的eventHandler事件,响应对滚轮的处理,让其来调节音量 Log: Influence: 将鼠标放在任务栏声音图标上,滚动滚轮,观察音量大小是否调节 Bug: https://pms.uniontech.com/bug-view-172417.html Change-Id: I82cf58f652568f4152083973ba8328a39f496656 --- frame/window/quickpluginwindow.cpp | 10 +++++++ frame/window/quickpluginwindow.h | 1 + plugins/sound/soundplugin.cpp | 42 ++++++++++++++++++++++++++++++ plugins/sound/soundplugin.h | 1 + 4 files changed, 54 insertions(+) diff --git a/frame/window/quickpluginwindow.cpp b/frame/window/quickpluginwindow.cpp index bf0b58212..3262feccd 100644 --- a/frame/window/quickpluginwindow.cpp +++ b/frame/window/quickpluginwindow.cpp @@ -711,6 +711,14 @@ void QuickDockItem::hideEvent(QHideEvent *event) } } +bool QuickDockItem::eventFilter(QObject *watched, QEvent *event) +{ + if (watched == this) + return m_pluginItem->eventHandler(event); + + return QWidget::eventFilter(watched, event); +} + QPixmap QuickDockItem::iconPixmap() const { int pixmapSize = static_cast(ICONHEIGHT * qApp->devicePixelRatio()); @@ -749,6 +757,8 @@ void QuickDockItem::initAttribute() Qt::WindowFlags flags = m_popupWindow->windowFlags() | Qt::FramelessWindowHint; m_popupWindow->setWindowFlags(flags); } + + this->installEventFilter(this); } void QuickDockItem::initConnection() diff --git a/frame/window/quickpluginwindow.h b/frame/window/quickpluginwindow.h index 7cf39a454..149db0be3 100644 --- a/frame/window/quickpluginwindow.h +++ b/frame/window/quickpluginwindow.h @@ -109,6 +109,7 @@ protected: void leaveEvent(QEvent *event) override; void showEvent(QShowEvent *event) override; void hideEvent(QHideEvent *event) override; + bool eventFilter(QObject *watched, QEvent *event) override; private: QPoint topleftPoint() const; diff --git a/plugins/sound/soundplugin.cpp b/plugins/sound/soundplugin.cpp index 62775c9ff..7b8e9f17b 100644 --- a/plugins/sound/soundplugin.cpp +++ b/plugins/sound/soundplugin.cpp @@ -24,6 +24,8 @@ #include "soundwidget.h" #include "sounddeviceswidget.h" +#include + #include #include @@ -164,6 +166,46 @@ PluginFlags SoundPlugin::flags() const | PluginFlag::Attribute_CanSetting; } +bool SoundPlugin::eventHandler(QEvent *event) +{ + // 当前只处理鼠标滚轮事件 + if (event->type() != QEvent::Wheel) + return PluginsItemInterface::eventHandler(event); + + // 获取当前默认的声音设备 + QDBusPendingCall defaultSinkCall = DDBusSender().service("org.deepin.daemon.Audio1") + .path("/org/deepin/daemon/Audio1") + .interface("org.deepin.daemon.Audio1") + .property("DefaultSink").get(); + defaultSinkCall.waitForFinished(); + QDBusReply path = defaultSinkCall.reply(); + const QString defaultSinkPath = path.value().value().path(); + if (defaultSinkPath.isNull()) + return false; + + // 获取当前默认声音设备的音量 + DDBusSender sinkDBus = DDBusSender().service("org.deepin.daemon.Audio1") + .path(defaultSinkPath).interface("org.deepin.daemon.Audio1.Sink"); + QDBusPendingCall volumeCall = sinkDBus.property("Volume").get(); + volumeCall.waitForFinished(); + QDBusReply volumePath = volumeCall.reply(); + double volume = volumePath.value().value(); + + // 根据滚轮的动作来增加音量或者减小音量 + QWheelEvent *wheelEvent = static_cast(event); + if (wheelEvent->angleDelta().y() > 0) { + // 向上滚动,增大音量 + if (volume < 1) + sinkDBus.method("SetVolume").arg(volume + 0.02).arg(true).call(); + } else { + // 向下滚动,调小音量 + if (volume > 0) + sinkDBus.method("SetVolume").arg(volume - 0.02).arg(true).call(); + } + + return true; +} + void SoundPlugin::refreshPluginItemsVisible() { if (pluginIsDisable()) diff --git a/plugins/sound/soundplugin.h b/plugins/sound/soundplugin.h index 7b9a2be42..ad638fbbb 100644 --- a/plugins/sound/soundplugin.h +++ b/plugins/sound/soundplugin.h @@ -54,6 +54,7 @@ public: QIcon icon(const DockPart &dockPart, DGuiApplicationHelper::ColorType themeType) override; PluginMode status() const override; PluginFlags flags() const override; + bool eventHandler(QEvent *event) override; private: void refreshPluginItemsVisible();