mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-03 00:15:21 +00:00

当开机时连接蓝牙设备,重启过程中关闭已连接的蓝牙设备,在特定机型上开机时,蓝牙的状态为 StateAvailable (这个状态的意思是识别的蓝牙设备可以连接,但未连接成功的状态, 可能出现的情况:蓝牙信号不好,意外断开连接或连接不上;蓝牙设备将配对信息删除,导致本机连不上蓝牙设备), 原来的代码中没有在 StateAvailable 状态更新图标(目的是因为当有多个蓝牙设备连接,其中一台因为上述原因断开连接时,不去更新蓝牙图标状态,只有在没有设备连接的情况下, 状态才会变为 StateUnavailable,更新图标为灰色)。如果不在一开始更新一下图标,就会导致开机时是 StateAvailable 状态,不刷新图标,显示默认的蓝色蓝牙图标。 故在初始化的时候先刷新一遍图标,如果后面状态有变化再触发更新蓝牙图标状态。 Log: 修复蓝牙图标未及时刷新的问题。 Bug: https://pms.uniontech.com/zentao/bug-view-58253.html Change-Id: I511e541b86b713fdc353793ff042d7abc5ec3414
230 lines
6.2 KiB
C++
230 lines
6.2 KiB
C++
/*
|
|
* Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd.
|
|
*
|
|
* Author: zhaolong <zhaolong@uniontech.com>
|
|
*
|
|
* Maintainer: zhaolong <zhaolong@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 "bluetoothitem.h"
|
|
#include "constants.h"
|
|
#include "../widgets/tipswidget.h"
|
|
#include "../frame/util/imageutil.h"
|
|
#include "bluetoothapplet.h"
|
|
|
|
#include <DApplication>
|
|
#include <DDBusSender>
|
|
#include <DGuiApplicationHelper>
|
|
|
|
#include <QPainter>
|
|
|
|
// menu actions
|
|
#define SHIFT "shift"
|
|
#define SETTINGS "settings"
|
|
|
|
DWIDGET_USE_NAMESPACE
|
|
DGUI_USE_NAMESPACE
|
|
|
|
using namespace Dock;
|
|
|
|
BluetoothItem::BluetoothItem(QWidget *parent)
|
|
: QWidget(parent)
|
|
, m_tipsLabel(new TipsWidget(this))
|
|
, m_applet(new BluetoothApplet(this))
|
|
, m_devState(Device::State::StateUnavailable)
|
|
, m_adapterPowered(m_applet->poweredInitState())
|
|
{
|
|
m_applet->setVisible(false);
|
|
m_tipsLabel->setVisible(false);
|
|
refreshIcon();
|
|
|
|
connect(m_applet, &BluetoothApplet::powerChanged, [&](bool powered) {
|
|
m_adapterPowered = powered;
|
|
refreshIcon();
|
|
});
|
|
connect(m_applet, &BluetoothApplet::deviceStateChanged, [&](const Device::State state) {
|
|
m_devState = state;
|
|
refreshIcon();
|
|
refreshTips();
|
|
});
|
|
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &BluetoothItem::refreshIcon);
|
|
connect(m_applet,&BluetoothApplet::noAdapter,this,&BluetoothItem::noAdapter);
|
|
connect(m_applet,&BluetoothApplet::justHasAdapter,this,&BluetoothItem::justHasAdapter);
|
|
}
|
|
|
|
QWidget *BluetoothItem::tipsWidget()
|
|
{
|
|
refreshTips();
|
|
return m_tipsLabel;
|
|
}
|
|
|
|
QWidget *BluetoothItem::popupApplet()
|
|
{
|
|
if (m_applet && m_applet->hasAadapter())
|
|
m_applet->setAdapterRefresh();
|
|
return m_applet->hasAadapter() ? m_applet : nullptr;
|
|
}
|
|
|
|
const QString BluetoothItem::contextMenu() const
|
|
{
|
|
QList<QVariant> items;
|
|
if (m_applet->hasAadapter()) {
|
|
items.reserve(2);
|
|
|
|
QMap<QString, QVariant> shift;
|
|
shift["itemId"] = SHIFT;
|
|
if (m_adapterPowered)
|
|
shift["itemText"] = tr("Turn off");
|
|
else
|
|
shift["itemText"] = tr("Turn on");
|
|
shift["isActive"] = true;
|
|
items.push_back(shift);
|
|
|
|
QMap<QString, QVariant> settings;
|
|
settings["itemId"] = SETTINGS;
|
|
settings["itemText"] = tr("Bluetooth settings");
|
|
settings["isActive"] = true;
|
|
items.push_back(settings);
|
|
|
|
QMap<QString, QVariant> menu;
|
|
menu["items"] = items;
|
|
menu["checkableMenu"] = false;
|
|
menu["singleCheck"] = false;
|
|
return QJsonDocument::fromVariant(menu).toJson();
|
|
}
|
|
return QByteArray();
|
|
}
|
|
|
|
void BluetoothItem::invokeMenuItem(const QString menuId, const bool checked)
|
|
{
|
|
Q_UNUSED(checked);
|
|
|
|
if (menuId == SHIFT) {
|
|
m_applet->setAdapterPowered(!m_adapterPowered);
|
|
}
|
|
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("bluetooth"))
|
|
.call();
|
|
}
|
|
|
|
void BluetoothItem::refreshIcon()
|
|
{
|
|
if (!m_applet)
|
|
return;
|
|
|
|
QString stateString;
|
|
QString iconString;
|
|
|
|
if (m_adapterPowered) {
|
|
switch (m_devState) {
|
|
case Device::StateConnected:
|
|
stateString = "active";
|
|
break;
|
|
case Device::StateAvailable: {
|
|
return ;
|
|
}
|
|
case Device::StateUnavailable: {
|
|
stateString = "disable";
|
|
} break;
|
|
}
|
|
} else {
|
|
stateString = "disable";
|
|
}
|
|
|
|
iconString = QString("bluetooth-%1-symbolic").arg(stateString);
|
|
|
|
const qreal 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 BluetoothItem::refreshTips()
|
|
{
|
|
if (!m_applet)
|
|
return;
|
|
|
|
QString tipsText;
|
|
|
|
if (m_adapterPowered) {
|
|
switch (m_devState) {
|
|
case Device::StateConnected: {
|
|
QStringList textList;
|
|
for (QString devName : m_applet->connectedDevsName()) {
|
|
textList << tr("%1 connected").arg(devName);
|
|
}
|
|
m_tipsLabel->setTextList(textList);
|
|
return;
|
|
}
|
|
case Device::StateAvailable: {
|
|
tipsText = tr("Connecting...");
|
|
}
|
|
break ;
|
|
case Device::StateUnavailable: {
|
|
tipsText = tr("Bluetooth");
|
|
}
|
|
break;
|
|
}
|
|
} else {
|
|
tipsText = tr("Turned off");
|
|
}
|
|
|
|
m_tipsLabel->setText(tipsText);
|
|
}
|
|
|
|
|
|
bool BluetoothItem::hasAdapter()
|
|
{
|
|
return m_applet->hasAadapter();
|
|
}
|
|
|
|
void BluetoothItem::resizeEvent(QResizeEvent *event)
|
|
{
|
|
QWidget::resizeEvent(event);
|
|
|
|
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 BluetoothItem::paintEvent(QPaintEvent *event)
|
|
{
|
|
QWidget::paintEvent(event);
|
|
|
|
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);
|
|
}
|