mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-04 09:23:03 +00:00

1、修复拖动过程中组合图标尺寸显示异常的问题 2、修复从控制中心设置插件显示隐藏引起的异常问题 3、修复高缩放率下的组合图标显示异常问题 4、修复蓝牙面板在不同的主题下颜色的显示的问题 5、社区版键盘布局默认在任务栏显示 Log: 修复图标显示的问题 Influence: 高缩放率下,观察组合图标显示是否正常 Bug: https://pms.uniontech.com/bug-view-181723.html Change-Id: Id351063095a2290731876ce896c2cd05a32d4ec0
210 lines
7.3 KiB
C++
210 lines
7.3 KiB
C++
/*
|
|
* Copyright (C) 2022 ~ 2022 Deepin Technology Co., Ltd.
|
|
*
|
|
* Author: donghualin <donghualin@uniontech.com>
|
|
*
|
|
* Maintainer: donghualin <donghualin@uniontech.com>
|
|
*
|
|
* 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 "bluetoothmainwidget.h"
|
|
#include "bluetoothitem.h"
|
|
#include "adaptersmanager.h"
|
|
#include "adapter.h"
|
|
|
|
#include <DGuiApplicationHelper>
|
|
#include <DFontSizeManager>
|
|
|
|
#include <QLabel>
|
|
#include <QHBoxLayout>
|
|
#include <QPainter>
|
|
#include <QPainterPath>
|
|
#include <DStyle>
|
|
|
|
DGUI_USE_NAMESPACE
|
|
DWIDGET_USE_NAMESPACE
|
|
|
|
BluetoothMainWidget::BluetoothMainWidget(AdaptersManager *adapterManager, QWidget *parent)
|
|
: QWidget(parent)
|
|
, m_adapterManager(adapterManager)
|
|
, m_iconWidget(new QWidget(this))
|
|
, m_nameLabel(new QLabel(this))
|
|
, m_stateLabel(new QLabel(this))
|
|
, m_expandLabel(new QLabel(this))
|
|
, m_mouseEnter(false)
|
|
{
|
|
initUi();
|
|
initConnection();
|
|
}
|
|
|
|
BluetoothMainWidget::~BluetoothMainWidget()
|
|
{
|
|
}
|
|
|
|
bool BluetoothMainWidget::eventFilter(QObject *watcher, QEvent *event)
|
|
{
|
|
if (watcher == m_iconWidget) {
|
|
switch (event->type()) {
|
|
case QEvent::Paint: {
|
|
QPainter painter(m_iconWidget);
|
|
// 在区域最中间绘制
|
|
QRect iconRect = m_iconWidget->rect();
|
|
int size = qMin(iconRect.height(), iconRect.width());
|
|
QPoint ptCenter(iconRect.center());
|
|
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
|
|
// 填充原型路径
|
|
QPainterPath path;
|
|
path.addEllipse(ptCenter, size / 2 - 1, size / 2 - 1);
|
|
// 设置黑色背景色
|
|
QColor backColor = (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::ColorType::LightType ? Qt::black : Qt::white);
|
|
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::ColorType::LightType)
|
|
backColor.setAlphaF(m_mouseEnter ? 0.2 : 0.1);
|
|
else
|
|
backColor.setAlphaF(m_mouseEnter ? 0.1 : 0.2);
|
|
painter.setBrush(backColor);
|
|
painter.fillPath(path, backColor);
|
|
// 添加图标
|
|
bool blueStatus = isOpen();
|
|
QPixmap pixmap(bluetoothIcon(blueStatus));
|
|
if (blueStatus) {
|
|
QPainter pa(&pixmap);
|
|
pa.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
|
pa.fillRect(pixmap.rect(), qApp->palette().highlight());
|
|
}
|
|
painter.drawPixmap(QPoint(ptCenter.x() - pixmap.size().width() / 2, ptCenter.y() - pixmap.size().height() / 2), pixmap);
|
|
return true;
|
|
}
|
|
case QEvent::Enter: {
|
|
m_mouseEnter = true;
|
|
m_iconWidget->update();
|
|
break;
|
|
}
|
|
case QEvent::Leave: {
|
|
m_mouseEnter = false;
|
|
m_iconWidget->update();
|
|
break;
|
|
}
|
|
case QEvent::MouseButtonRelease: {
|
|
bool status = !(isOpen());
|
|
for (const Adapter *adapter : m_adapterManager->adapters())
|
|
m_adapterManager->setAdapterPowered(adapter, status);
|
|
|
|
return true;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
if (watcher == m_expandLabel && event->type() == QEvent::MouseButtonRelease) {
|
|
Q_EMIT requestExpand();
|
|
return true;
|
|
}
|
|
if (watcher == m_nameLabel && event->type() == QEvent::Resize) {
|
|
m_nameLabel->setText(QFontMetrics(m_nameLabel->font()).elidedText(tr("Bluetooth"), Qt::TextElideMode::ElideRight, m_nameLabel->width()));
|
|
}
|
|
return QWidget::eventFilter(watcher, event);
|
|
}
|
|
|
|
void BluetoothMainWidget::initUi()
|
|
{
|
|
QHBoxLayout *mainLayout = new QHBoxLayout(this);
|
|
// 添加左侧的图标
|
|
m_iconWidget->setFixedWidth(36);
|
|
// 添加中间的文本
|
|
QWidget *textWidget = new QWidget(this);
|
|
QVBoxLayout *textLayout = new QVBoxLayout(textWidget);
|
|
textLayout->setContentsMargins(0, 10, 0, 10);
|
|
textLayout->setSpacing(0);
|
|
QFont nameFont = DFontSizeManager::instance()->t6();
|
|
nameFont.setBold(true);
|
|
|
|
m_nameLabel->setParent(textWidget);
|
|
m_nameLabel->setFont(nameFont);
|
|
|
|
m_stateLabel->setParent(textWidget);
|
|
m_stateLabel->setFont(DFontSizeManager::instance()->t10());
|
|
textLayout->addWidget(m_nameLabel);
|
|
textLayout->addWidget(m_stateLabel);
|
|
|
|
// 添加右侧的展开按钮
|
|
QWidget *expandWidget = new QWidget(this);
|
|
QVBoxLayout *expandLayout = new QVBoxLayout(expandWidget);
|
|
expandLayout->setContentsMargins(0, 0, 0, 0);
|
|
expandLayout->setSpacing(0);
|
|
expandLayout->addWidget(m_expandLabel);
|
|
|
|
// 设置图标和文本
|
|
m_nameLabel->setText(tr("Bluetooth"));
|
|
updateExpandIcon();
|
|
|
|
// 将所有的窗体都添加到主布局中
|
|
mainLayout->setContentsMargins(10, 0, 10, 0);
|
|
mainLayout->setSpacing(0);
|
|
mainLayout->addWidget(m_iconWidget);
|
|
mainLayout->addSpacing(10);
|
|
mainLayout->addWidget(textWidget);
|
|
mainLayout->addStretch();
|
|
mainLayout->addWidget(expandWidget);
|
|
|
|
m_iconWidget->installEventFilter(this);
|
|
m_expandLabel->installEventFilter(this);
|
|
m_nameLabel->installEventFilter(this);
|
|
}
|
|
|
|
void BluetoothMainWidget::initConnection()
|
|
{
|
|
connect(m_adapterManager, &AdaptersManager::adapterIncreased, this, &BluetoothMainWidget::onAdapterChanged);
|
|
connect(m_adapterManager, &AdaptersManager::adapterDecreased, this, &BluetoothMainWidget::onAdapterChanged);
|
|
connect(m_adapterManager, &AdaptersManager::adapterIncreased, this, [ = ](Adapter *adapter) {
|
|
connect(adapter, &Adapter::poweredChanged, this, &BluetoothMainWidget::onAdapterChanged);
|
|
});
|
|
|
|
for (const Adapter *adapter : m_adapterManager->adapters())
|
|
connect(adapter, &Adapter::poweredChanged, this, &BluetoothMainWidget::onAdapterChanged);
|
|
|
|
onAdapterChanged();
|
|
}
|
|
|
|
void BluetoothMainWidget::updateExpandIcon()
|
|
{
|
|
QPixmap enterPixmap = DStyle::standardIcon(style(), DStyle::SP_ArrowEnter).pixmap(QSize(16, 16));
|
|
m_expandLabel->setPixmap(enterPixmap);
|
|
}
|
|
|
|
bool BluetoothMainWidget::isOpen() const
|
|
{
|
|
QList<const Adapter *> adapters = m_adapterManager->adapters();
|
|
for (const Adapter *adapter : adapters) {
|
|
if (adapter->powered())
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
QString BluetoothMainWidget::bluetoothIcon(bool isOpen) const
|
|
{
|
|
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::ColorType::LightType)
|
|
return isOpen ? ":/bluetooth-active-symbolic-dark.svg" : ":/bluetooth-disable-symbolic-dark.svg";
|
|
|
|
return isOpen ? ":/bluetooth-active-symbolic.svg" : ":/bluetooth-disable-symbolic.svg";
|
|
}
|
|
|
|
void BluetoothMainWidget::onAdapterChanged()
|
|
{
|
|
bool bluetoothIsOpen = isOpen();
|
|
m_stateLabel->setText(bluetoothIsOpen ? tr("Turn on") : tr("Turn off"));
|
|
m_iconWidget->update();
|
|
}
|