2023-02-16 13:51:55 +08:00
|
|
|
// Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
|
|
|
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
2022-09-06 11:36:55 +08:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2016-08-02 10:34:44 +08:00
|
|
|
#include "soundplugin.h"
|
2020-05-27 14:24:18 +08:00
|
|
|
#include "soundaccessible.h"
|
2022-11-01 08:13:27 +00:00
|
|
|
#include "soundwidget.h"
|
|
|
|
#include "sounddeviceswidget.h"
|
2020-05-27 14:24:18 +08:00
|
|
|
|
2022-12-02 18:30:35 +08:00
|
|
|
#include <DDBusSender>
|
2020-05-27 14:24:18 +08:00
|
|
|
|
2018-03-06 15:18:53 +08:00
|
|
|
#include <QDebug>
|
2020-05-27 14:24:18 +08:00
|
|
|
#include <QAccessible>
|
2016-08-02 10:34:44 +08:00
|
|
|
|
2017-10-23 13:15:57 +08:00
|
|
|
#define STATE_KEY "enable"
|
2023-05-05 11:32:14 +08:00
|
|
|
#define SOUND_KEY "sound-item-key"
|
2017-10-23 13:15:57 +08:00
|
|
|
|
2016-08-02 10:34:44 +08:00
|
|
|
SoundPlugin::SoundPlugin(QObject *parent)
|
2022-11-01 08:13:27 +00:00
|
|
|
: QObject(parent)
|
|
|
|
, m_soundWidget(nullptr)
|
2023-05-05 11:32:14 +08:00
|
|
|
, m_soundDeviceWidget(nullptr)
|
2016-08-02 10:34:44 +08:00
|
|
|
{
|
2020-05-27 14:24:18 +08:00
|
|
|
QAccessible::installFactory(soundAccessibleFactory);
|
2016-08-02 10:34:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString SoundPlugin::pluginName() const
|
|
|
|
{
|
|
|
|
return "sound";
|
|
|
|
}
|
|
|
|
|
2017-10-23 13:15:57 +08:00
|
|
|
const QString SoundPlugin::pluginDisplayName() const
|
|
|
|
{
|
|
|
|
return tr("Sound");
|
|
|
|
}
|
|
|
|
|
2016-08-02 10:34:44 +08:00
|
|
|
void SoundPlugin::init(PluginProxyInterface *proxyInter)
|
|
|
|
{
|
|
|
|
m_proxyInter = proxyInter;
|
|
|
|
|
2023-05-05 11:32:14 +08:00
|
|
|
if (m_soundWidget) return;
|
2021-12-10 13:53:56 +08:00
|
|
|
|
2022-11-01 08:13:27 +00:00
|
|
|
m_soundWidget.reset(new SoundWidget);
|
|
|
|
m_soundWidget->setFixedHeight(60);
|
2016-09-21 10:05:15 +08:00
|
|
|
|
2022-11-01 08:13:27 +00:00
|
|
|
m_soundDeviceWidget.reset(new SoundDevicesWidget);
|
|
|
|
|
|
|
|
if (!pluginIsDisable()) {
|
2018-11-13 20:07:52 +08:00
|
|
|
m_proxyInter->itemAdded(this, SOUND_KEY);
|
2022-11-01 08:13:27 +00:00
|
|
|
connect(m_soundWidget.data(), &SoundWidget::rightIconClick, this, [ this, proxyInter ] {
|
2022-11-04 04:51:48 +00:00
|
|
|
proxyInter->requestSetAppletVisible(this, QUICK_ITEM_KEY, true);
|
2022-11-01 08:13:27 +00:00
|
|
|
});
|
|
|
|
}
|
2022-11-28 14:01:16 +08:00
|
|
|
|
|
|
|
connect(m_soundDeviceWidget.data(), &SoundDevicesWidget::enableChanged, m_soundWidget.data(), &SoundWidget::setEnabled);
|
2023-01-14 19:45:12 +08:00
|
|
|
connect(m_soundDeviceWidget.data(), &SoundDevicesWidget::requestHide, this, [ this ] {
|
|
|
|
m_proxyInter->requestSetAppletVisible(this, QUICK_ITEM_KEY, false);
|
|
|
|
});
|
2023-03-07 11:37:49 +08:00
|
|
|
|
2023-05-05 11:32:14 +08:00
|
|
|
connect(m_soundDeviceWidget.data(), &SoundDevicesWidget::iconChanged, this, [=] {
|
2023-03-07 11:37:49 +08:00
|
|
|
m_proxyInter->updateDockInfo(this, DockPart::QuickPanel);
|
|
|
|
m_proxyInter->updateDockInfo(this, DockPart::QuickShow);
|
|
|
|
m_proxyInter->itemUpdate(this, SOUND_KEY);
|
|
|
|
});
|
2017-10-23 13:15:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundPlugin::pluginStateSwitched()
|
|
|
|
{
|
2018-11-21 18:22:22 +08:00
|
|
|
m_proxyInter->saveValue(this, STATE_KEY, pluginIsDisable());
|
2017-10-23 13:15:57 +08:00
|
|
|
|
2019-01-30 18:00:46 +08:00
|
|
|
refreshPluginItemsVisible();
|
2017-10-23 13:15:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SoundPlugin::pluginIsDisable()
|
|
|
|
{
|
2018-11-21 18:22:22 +08:00
|
|
|
return !m_proxyInter->getValue(this, STATE_KEY, true).toBool();
|
2016-08-02 10:34:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *SoundPlugin::itemWidget(const QString &itemKey)
|
|
|
|
{
|
2022-11-04 04:51:48 +00:00
|
|
|
if (itemKey == QUICK_ITEM_KEY)
|
2022-11-01 08:13:27 +00:00
|
|
|
return m_soundWidget.data();
|
2016-08-02 10:34:44 +08:00
|
|
|
|
2018-12-06 18:05:30 +08:00
|
|
|
return nullptr;
|
2016-08-02 10:34:44 +08:00
|
|
|
}
|
2016-08-02 11:32:24 +08:00
|
|
|
|
2016-08-26 10:07:30 +08:00
|
|
|
QWidget *SoundPlugin::itemTipsWidget(const QString &itemKey)
|
|
|
|
{
|
2022-11-01 08:13:27 +00:00
|
|
|
if (itemKey == SOUND_KEY)
|
2023-05-05 11:32:14 +08:00
|
|
|
return m_soundDeviceWidget->tipsWidget();
|
2016-08-26 10:07:30 +08:00
|
|
|
|
2018-12-06 18:05:30 +08:00
|
|
|
return nullptr;
|
2016-08-26 10:07:30 +08:00
|
|
|
}
|
|
|
|
|
2016-08-02 11:32:24 +08:00
|
|
|
QWidget *SoundPlugin::itemPopupApplet(const QString &itemKey)
|
|
|
|
{
|
2022-11-04 04:51:48 +00:00
|
|
|
if (itemKey == QUICK_ITEM_KEY)
|
2022-11-01 08:13:27 +00:00
|
|
|
return m_soundDeviceWidget.data();
|
2016-09-21 10:05:15 +08:00
|
|
|
|
2018-12-06 18:05:30 +08:00
|
|
|
return nullptr;
|
2016-09-21 10:05:15 +08:00
|
|
|
}
|
|
|
|
|
2018-03-06 15:18:53 +08:00
|
|
|
int SoundPlugin::itemSortKey(const QString &itemKey)
|
|
|
|
{
|
2020-01-11 10:35:29 +08:00
|
|
|
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
|
2018-09-06 10:42:27 +08:00
|
|
|
|
2020-08-03 11:28:48 +08:00
|
|
|
return m_proxyInter->getValue(this, key, 2).toInt();
|
2018-03-06 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundPlugin::setSortKey(const QString &itemKey, const int order)
|
|
|
|
{
|
2020-01-11 10:35:29 +08:00
|
|
|
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
|
2018-03-06 15:18:53 +08:00
|
|
|
|
2018-11-21 18:22:22 +08:00
|
|
|
m_proxyInter->saveValue(this, key, order);
|
2018-03-06 15:18:53 +08:00
|
|
|
}
|
2018-12-06 18:05:30 +08:00
|
|
|
|
2019-01-30 18:00:46 +08:00
|
|
|
void SoundPlugin::pluginSettingsChanged()
|
|
|
|
{
|
|
|
|
refreshPluginItemsVisible();
|
|
|
|
}
|
|
|
|
|
2022-12-02 15:41:34 +08:00
|
|
|
QIcon SoundPlugin::icon(const DockPart &dockPart, DGuiApplicationHelper::ColorType themeType)
|
2022-11-23 05:27:16 +00:00
|
|
|
{
|
2023-01-04 22:14:33 +08:00
|
|
|
switch (dockPart) {
|
|
|
|
case DockPart::QuickShow:
|
2023-05-05 11:32:14 +08:00
|
|
|
return m_soundDeviceWidget->pixmap(themeType, 18, 16);
|
2023-01-04 22:14:33 +08:00
|
|
|
case DockPart::DCCSetting:
|
2023-05-05 11:32:14 +08:00
|
|
|
return m_soundDeviceWidget->pixmap(themeType, 18, 18);
|
2023-01-04 22:14:33 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QIcon();
|
2022-11-23 05:27:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 15:41:34 +08:00
|
|
|
PluginsItemInterface::PluginMode SoundPlugin::status() const
|
2022-11-01 08:13:27 +00:00
|
|
|
{
|
|
|
|
return SoundPlugin::Active;
|
|
|
|
}
|
|
|
|
|
2022-11-28 14:37:54 +08:00
|
|
|
PluginFlags SoundPlugin::flags() const
|
|
|
|
{
|
|
|
|
return PluginFlag::Type_Common
|
|
|
|
| PluginFlag::Quick_Full
|
|
|
|
| PluginFlag::Attribute_CanDrag
|
|
|
|
| PluginFlag::Attribute_CanInsert
|
|
|
|
| PluginFlag::Attribute_CanSetting;
|
|
|
|
}
|
|
|
|
|
2022-12-02 18:30:35 +08:00
|
|
|
bool SoundPlugin::eventHandler(QEvent *event)
|
|
|
|
{
|
|
|
|
// 当前只处理鼠标滚轮事件
|
|
|
|
if (event->type() != QEvent::Wheel)
|
|
|
|
return PluginsItemInterface::eventHandler(event);
|
|
|
|
|
|
|
|
// 获取当前默认的声音设备
|
2022-12-07 05:39:06 +00:00
|
|
|
QDBusPendingCall defaultSinkCall = DDBusSender().service("org.deepin.dde.Audio1")
|
|
|
|
.path("/org/deepin/dde/Audio1")
|
|
|
|
.interface("org.deepin.dde.Audio1")
|
2022-12-02 18:30:35 +08:00
|
|
|
.property("DefaultSink").get();
|
|
|
|
defaultSinkCall.waitForFinished();
|
|
|
|
QDBusReply<QVariant> path = defaultSinkCall.reply();
|
|
|
|
const QString defaultSinkPath = path.value().value<QDBusObjectPath>().path();
|
|
|
|
if (defaultSinkPath.isNull())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// 获取当前默认声音设备的音量
|
2022-12-07 05:39:06 +00:00
|
|
|
DDBusSender sinkDBus = DDBusSender().service("org.deepin.dde.Audio1")
|
|
|
|
.path(defaultSinkPath).interface("org.deepin.dde.Audio1.Sink");
|
2022-12-02 18:30:35 +08:00
|
|
|
QDBusPendingCall volumeCall = sinkDBus.property("Volume").get();
|
|
|
|
volumeCall.waitForFinished();
|
|
|
|
QDBusReply<QVariant> volumePath = volumeCall.reply();
|
|
|
|
double volume = volumePath.value().value<double>();
|
|
|
|
|
2022-12-23 10:03:32 +08:00
|
|
|
// 获取当前默认声音设备的最大音量
|
|
|
|
DDBusSender audioDBus = DDBusSender().service("org.deepin.dde.Audio1")
|
|
|
|
.path("/org/deepin/dde/Audio1").interface("org.deepin.dde.Audio1");
|
|
|
|
QDBusPendingCall call = audioDBus.property("MaxUIVolume").get();
|
|
|
|
call.waitForFinished();
|
|
|
|
QDBusReply<QVariant> maxVolumeReply = call.reply();
|
|
|
|
double maxVolume = maxVolumeReply.value().value<double>();
|
|
|
|
|
2022-12-02 18:30:35 +08:00
|
|
|
// 根据滚轮的动作来增加音量或者减小音量
|
|
|
|
QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
|
|
|
|
if (wheelEvent->angleDelta().y() > 0) {
|
|
|
|
// 向上滚动,增大音量
|
2022-12-23 10:03:32 +08:00
|
|
|
if (volume < maxVolume)
|
|
|
|
sinkDBus.method("SetVolume").arg(qMin(volume + 0.02, maxVolume)).arg(true).call();
|
2022-12-02 18:30:35 +08:00
|
|
|
} else {
|
|
|
|
// 向下滚动,调小音量
|
|
|
|
if (volume > 0)
|
2022-12-23 10:03:32 +08:00
|
|
|
sinkDBus.method("SetVolume").arg(qMax(volume - 0.02, 0.0)).arg(true).call();
|
2022-12-02 18:30:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-30 18:00:46 +08:00
|
|
|
void SoundPlugin::refreshPluginItemsVisible()
|
|
|
|
{
|
|
|
|
if (pluginIsDisable())
|
|
|
|
m_proxyInter->itemRemoved(this, SOUND_KEY);
|
|
|
|
else
|
|
|
|
m_proxyInter->itemAdded(this, SOUND_KEY);
|
|
|
|
}
|