dde-dock/plugins/sound/sounditem.cpp
曹威 b342f6f884 feat: 增加集中管控相关功能,所有功能可通过全局一个配置启用或禁用。
1. 模块可配置显示隐藏,能打开控制中心的右键菜单全屏蔽。
2. gsettings最终生效的设置在各个用户之间是互相隔离的,而且root用户读取不到普通用户的gsettings配置。
3. 集中管控版本右下解关机按钮右菜菜单的锁定功能改为调用SwitchTTYAndShow

Log: 引用头文件分类规范化
Task: https://pms.uniontech.com/zentao/task-view-30817.html
Change-Id: I5ae833f61864ba1874c8ceb75351d569614ab235
Reviewed-on: http://gerrit.uniontech.com/c/dde-dock/+/2377
Reviewed-by: <mailman@uniontech.com>
Reviewed-by: niecheng <niecheng@uniontech.com>
Reviewed-by: wangwei <wangwei@uniontech.com>
Tested-by: <mailman@uniontech.com>
2020-08-28 14:20:32 +08:00

244 lines
6.6 KiB
C++

/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: sbw <sbw@sbw.so>
*
* Maintainer: sbw <sbw@sbw.so>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QPainter>
#include <QIcon>
#include <QMouseEvent>
#include <QGSettings>
#include <QApplication>
#include <DApplication>
#include <DDBusSender>
#include <DGuiApplicationHelper>
DWIDGET_USE_NAMESPACE
DGUI_USE_NAMESPACE
#include "sounditem.h"
#include "constants.h"
#include "../widgets/tipswidget.h"
#include "../frame/util/imageutil.h"
#include "../frame/util/utils.h"
// menu actions
#define MUTE "mute"
#define SETTINGS "settings"
using namespace Dock;
SoundItem::SoundItem(QWidget *parent)
: QWidget(parent)
, m_tipsLabel(new TipsWidget(this))
, m_applet(new SoundApplet(this))
, m_sinkInter(nullptr)
{
m_tipsLabel->setObjectName("sound");
m_tipsLabel->setAccessibleName("soundtips");
m_tipsLabel->setVisible(false);
m_applet->setVisible(false);
connect(m_applet, static_cast<void (SoundApplet::*)(DBusSink *) const>(&SoundApplet::defaultSinkChanged), this, &SoundItem::sinkChanged);
connect(m_applet, &SoundApplet::volumeChanged, this, &SoundItem::refreshTips, Qt::QueuedConnection);
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [ = ] {
refreshIcon();
});
}
QWidget *SoundItem::tipsWidget()
{
refreshTips(true);
m_tipsLabel->resize(m_tipsLabel->sizeHint().width() + 10,
m_tipsLabel->sizeHint().height());
return m_tipsLabel;
}
QWidget *SoundItem::popupApplet()
{
return m_applet;
}
const QString SoundItem::contextMenu() const
{
QList<QVariant> items;
items.reserve(2);
QMap<QString, QVariant> open;
open["itemId"] = MUTE;
if (m_sinkInter->mute())
open["itemText"] = tr("Unmute");
else
open["itemText"] = tr("Mute");
open["isActive"] = true;
items.push_back(open);
if (!QFile::exists(ICBC_CONF_FILE)) {
QMap<QString, QVariant> settings;
settings["itemId"] = SETTINGS;
settings["itemText"] = tr("Sound settings");
settings["isActive"] = true;
items.push_back(settings);
#ifdef QT_DEBUG
qInfo() << "----------icbc sound setting.";
#endif
}
QMap<QString, QVariant> menu;
menu["items"] = items;
menu["checkableMenu"] = false;
menu["singleCheck"] = false;
return QJsonDocument::fromVariant(menu).toJson();
}
void SoundItem::invokeMenuItem(const QString menuId, const bool checked)
{
Q_UNUSED(checked);
if (menuId == MUTE)
m_sinkInter->SetMuteQueued(!m_sinkInter->mute());
else if (menuId == SETTINGS)
DDBusSender()
.service("com.deepin.dde.ControlCenter")
.interface("com.deepin.dde.ControlCenter")
.path("/com/deepin/dde/ControlCenter")
.method(QString("ShowModule"))
.arg(QString("sound"))
.call();
}
void SoundItem::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
// 保持横纵比
if (position == Dock::Bottom || position == Dock::Top) {
setMaximumWidth(height());
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMaximumHeight(width());
setMaximumWidth(QWIDGETSIZE_MAX);
}
refreshIcon();
}
void SoundItem::wheelEvent(QWheelEvent *e)
{
QWheelEvent *event = new QWheelEvent(e->pos(), e->delta(), e->buttons(), e->modifiers());
qApp->postEvent(m_applet->mainSlider(), event);
e->accept();
}
void SoundItem::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
QPainter painter(this);
const QRectF &rf = QRectF(rect());
const QRectF &rfp = QRectF(m_iconPixmap.rect());
painter.drawPixmap(rf.center() - rfp.center() / m_iconPixmap.devicePixelRatioF(), m_iconPixmap);
}
void SoundItem::refreshIcon()
{
if (!m_sinkInter)
return;
const double volmue = m_applet->volumeValue();
const double maxVolmue = m_applet->maxVolumeValue();
const bool mute = m_sinkInter->mute();
const Dock::DisplayMode displayMode = Dock::DisplayMode::Efficient;
QString iconString;
if (displayMode == Dock::Fashion) {
QString volumeString;
if (volmue >= 1000)
volumeString = "100";
else
volumeString = QString("0") + ('0' + int(volmue / 100)) + "0";
iconString = "audio-volume-" + volumeString;
if (mute)
iconString += "-muted";
} else {
QString volumeString;
if (mute)
volumeString = "muted";
else if (int(volmue) == 0)
volumeString = "off";
else if (volmue / maxVolmue >= double(2) / 3)
volumeString = "high";
else if (volmue / maxVolmue >= double(1) / 3)
volumeString = "medium";
else
volumeString = "low";
iconString = QString("audio-volume-%1-symbolic").arg(volumeString);
}
const auto ratio = devicePixelRatioF();
int iconSize = PLUGIN_ICON_MAX_SIZE;
if (height() <= PLUGIN_BACKGROUND_MIN_SIZE && DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
iconString.append(PLUGIN_MIN_ICON_NAME);
m_iconPixmap = ImageUtil::loadSvg(iconString, ":/", iconSize, ratio);
update();
}
void SoundItem::refreshTips(const bool force)
{
if (!force && !m_tipsLabel->isVisible())
return;
if (!m_sinkInter)
return;
QString value;
if (m_sinkInter->mute()) {
value = QString("0") + '%';
} else {
if (m_sinkInter->volume() * 1000 < m_applet->volumeValue())
value = QString::number(m_applet->volumeValue() / 10) + '%';
else
value = QString::number(m_sinkInter->volume() * 100) + '%';
}
m_tipsLabel->setText(QString(tr("Volume %1").arg(value)));
}
void SoundItem::sinkChanged(DBusSink *sink)
{
m_sinkInter = sink;
connect(m_sinkInter, &DBusSink::MuteChanged, this, &SoundItem::refreshIcon);
connect(m_sinkInter, &DBusSink::VolumeChanged, this, &SoundItem::refreshIcon);
refreshIcon();
}