fix: 修改蓝牙插件界面相关bug

根据设计图更新任务栏插件主界面

Log: 根据设计图更新任务栏插件主界面
Bug: https://pms.uniontech.com/zentao/bug-view-59037.html
Bug: https://pms.uniontech.com/zentao/bug-view-57897.html

Change-Id: Idaa6009ca361b4a7ee515cb4e6d0bee9ecac1b54
This commit is contained in:
chenwei 2021-01-08 14:33:08 +08:00
parent acbf68cca3
commit dc40bf513b
19 changed files with 800 additions and 1391 deletions

View File

@ -38,7 +38,6 @@
//#include "../plugins/sound/componments/horizontalseparator.h"
#include "../plugins/show-desktop/showdesktopwidget.h"
#include "../plugins/bluetooth/componments/deviceitem.h"
#include "../plugins/network/networkitem.h"
#include "../plugins/network/item/applet/devicecontrolwidget.h"
#include "../plugins/datetime/datetimewidget.h"
@ -88,11 +87,6 @@ SET_FORM_ACCESSIBLE(AttentionContainer, "attentioncontainer")
SET_FORM_ACCESSIBLE(HoldContainer, "holdcontainer")
SET_FORM_ACCESSIBLE(NormalContainer, "normalcontainer")
SET_FORM_ACCESSIBLE(SpliterAnimated, "spliteranimated")
//SET_BUTTON_ACCESSIBLE(SoundItem, "plugin-sounditem")
//SET_FORM_ACCESSIBLE(SoundApplet, "soundapplet")
//SET_FORM_ACCESSIBLE(SinkInputWidget, "sinkinputwidget")
//SET_SLIDER_ACCESSIBLE(VolumeSlider, QAccessible::Slider, "volumeslider")
//SET_FORM_ACCESSIBLE(HorizontalSeparator, "horizontalseparator")
SET_FORM_ACCESSIBLE(DatetimeWidget, "plugin-datetime")
SET_FORM_ACCESSIBLE(OnboardItem, "plugin-onboard")
SET_FORM_ACCESSIBLE(TrashWidget, "plugin-trash")
@ -119,9 +113,7 @@ SET_FORM_ACCESSIBLE(QScrollArea, "QScrollArea")
SET_FORM_ACCESSIBLE(QFrame, "QFrame")
SET_FORM_ACCESSIBLE(QGraphicsView, "QGraphicsView")
SET_FORM_ACCESSIBLE(DragWidget, "DragWidget")
SET_FORM_ACCESSIBLE(MenueItem, "MenueItem")
SET_FORM_ACCESSIBLE(NetworkItem, "NetworkItem")
SET_FORM_ACCESSIBLE(DeviceItem, "DeviceItem")
SET_FORM_ACCESSIBLE(StateButton, "StateButton")
SET_FORM_ACCESSIBLE(DeviceControlWidget, "DeviceControlWidget")
@ -157,11 +149,6 @@ QAccessibleInterface *accessibleFactory(const QString &classname, QObject *objec
USE_ACCESSIBLE(classname, SpliterAnimated);
USE_ACCESSIBLE(classname, IndicatorTrayWidget);
USE_ACCESSIBLE(classname, XEmbedTrayWidget);
// USE_ACCESSIBLE(classname, SoundItem);
// USE_ACCESSIBLE(classname, SoundApplet);
// USE_ACCESSIBLE(classname, SinkInputWidget);
// USE_ACCESSIBLE(classname, VolumeSlider);
// USE_ACCESSIBLE(classname, HorizontalSeparator);
USE_ACCESSIBLE(classname, DesktopWidget);
USE_ACCESSIBLE(classname, DatetimeWidget);
USE_ACCESSIBLE(classname, OnboardItem);
@ -193,9 +180,7 @@ QAccessibleInterface *accessibleFactory(const QString &classname, QObject *objec
USE_ACCESSIBLE(classname, QFrame);
USE_ACCESSIBLE(classname, QGraphicsView);
USE_ACCESSIBLE(classname, DragWidget);
USE_ACCESSIBLE(classname, MenueItem);
USE_ACCESSIBLE(classname, NetworkItem);
USE_ACCESSIBLE(classname, DeviceItem);
USE_ACCESSIBLE(classname, StateButton);
USE_ACCESSIBLE(classname, DeviceControlWidget);

View File

@ -1,331 +0,0 @@
/*
* 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 "bluetoothapplet.h"
#include "componments/switchitem.h"
#include "componments/deviceitem.h"
#include "componments/adapter.h"
#include "componments/switchitem.h"
#include "componments/adaptersmanager.h"
#include "componments/adapteritem.h"
#include "componments/bluetoothconstants.h"
#include <DApplicationHelper>
#include <DDBusSender>
#include <QLabel>
#include <QVBoxLayout>
DGUI_USE_NAMESPACE
extern void initFontColor(QWidget *widget)
{
if (!widget)
return;
auto fontChange = [&](QWidget *widget){
QPalette defaultPalette = widget->palette();
defaultPalette.setBrush(QPalette::WindowText, defaultPalette.brightText());
widget->setPalette(defaultPalette);
};
fontChange(widget);
QObject::connect(DApplicationHelper::instance(), &DApplicationHelper::themeTypeChanged, widget, [=]{
fontChange(widget);
});
}
BluetoothApplet::BluetoothApplet(QWidget *parent)
: QScrollArea(parent)
, m_line(new HorizontalSeparator(this))
, m_appletName(new QLabel(this))
, m_centralWidget(new QWidget)
, m_centrealLayout(new QVBoxLayout)
, m_adapterLayout(new QVBoxLayout)
, m_menueLayout(new QHBoxLayout)
, m_openControlCenter(new MenueItem(this))
, m_textLayout(new QVBoxLayout)
, m_adaptersManager(new AdaptersManager(this))
{
m_line->setVisible(false);
m_adapterLayout->setContentsMargins(0, 0, 0, 0);
QFont defaultFont = font();
auto titlefont = QFont(defaultFont.family(), defaultFont.pointSize() + 2);
m_appletName->setText(tr("Bluetooth"));
m_appletName->setFont(titlefont);
initFontColor(m_appletName);
m_appletName->setVisible(false);
m_openControlCenter->setText(tr("Bluetooth settings"));
m_textLayout->addWidget(m_openControlCenter);
m_textLayout->setContentsMargins(10, 0, 0, 0);
initFontColor(m_openControlCenter);
m_openControlCenter->setFixedHeight(ITEMHEIGHT);
m_openControlCenter->setVisible(false);
QHBoxLayout *appletNameLayout = new QHBoxLayout;
appletNameLayout->setMargin(0);
appletNameLayout->setSpacing(0);
appletNameLayout->addSpacing(MARGIN);
appletNameLayout->addWidget(m_appletName);
appletNameLayout->addStretch();
m_menueLayout->setMargin(0);
m_menueLayout->setSpacing(0);
m_menueLayout->addSpacing(MARGIN);
m_centrealLayout->setMargin(0);
m_centrealLayout->setSpacing(0);
m_centrealLayout->addLayout(appletNameLayout);
m_centrealLayout->addWidget(m_line);
m_centrealLayout->addLayout(m_adapterLayout);
m_centrealLayout->addLayout(m_textLayout);
m_centralWidget->setLayout(m_centrealLayout);
m_centralWidget->setFixedWidth(POPUPWIDTH);
m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
setFixedWidth(POPUPWIDTH);
setWidget(m_centralWidget);
setContentsMargins(0, 0, 0, 0);
setFrameShape(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_centralWidget->setAutoFillBackground(false);
viewport()->setAutoFillBackground(false);
connect(m_adaptersManager, &AdaptersManager::adapterIncreased, this, &BluetoothApplet::addAdapter);
connect(m_adaptersManager, &AdaptersManager::adapterDecreased, this, &BluetoothApplet::removeAdapter);
connect(m_openControlCenter, &MenueItem::clicked, []{
DDBusSender()
.service("com.deepin.dde.ControlCenter")
.interface("com.deepin.dde.ControlCenter")
.path("/com/deepin/dde/ControlCenter")
.method(QString("ShowModule"))
.arg(QString("bluetooth"))
.call();
});
}
void BluetoothApplet::setAdapterPowered(bool powered)
{
for (AdapterItem *adapterItem : m_adapterItems) {
if (adapterItem)
adapterItem->setPowered(powered);
}
}
bool BluetoothApplet::poweredInitState()
{
return m_adaptersManager->defaultAdapterInitPowerState();
}
bool BluetoothApplet::hasAadapter()
{
return m_adaptersManager->adaptersCount();
}
QStringList BluetoothApplet::connectedDevsName()
{
QStringList devicesName;
for (AdapterItem *adapterItem : m_adapterItems) {
if (adapterItem) {
devicesName << adapterItem->connectedDevsName();
}
}
return devicesName;
}
void BluetoothApplet::onPowerChanged()
{
bool powerState = false;
for (AdapterItem *adapterItem : m_adapterItems) {
if (adapterItem->isPowered()) {
powerState = true;
break;
}
}
emit powerChanged(powerState);
m_openControlCenter->setVisible(powerState);
}
void BluetoothApplet::onDeviceStateChanged()
{
Device::State deviceState = Device::StateUnavailable;
for (AdapterItem *adapterItem : m_adapterItems) {
if (Device::StateAvailable == adapterItem->currentDeviceState()) {
deviceState = Device::StateAvailable;
continue;
}
if (Device::StateConnected == adapterItem->currentDeviceState()) {
deviceState = Device::StateConnected;
break;
}
}
emit deviceStateChanged(deviceState);
updateView();
}
void BluetoothApplet::addAdapter(Adapter *adapter)
{
if (!adapter)
return;
if (!m_adapterItems.size()) {
emit justHasAdapter();
}
QString adapterId = adapter->id();
//dde-session-daemon重启的时候同一个Id的蓝牙设备会再次添加一次因此需要先移除以前的
if (m_adapterItems.contains(adapterId)) {
AdapterItem *adapterItem = m_adapterItems.value(adapterId);
if (adapterItem) {
m_adapterLayout->removeWidget(adapterItem);
delete adapterItem;
m_adapterItems.remove(adapterId);
}
}
auto adatpterItem = new AdapterItem(m_adaptersManager, adapter, this);
m_adapterItems[adapterId] = adatpterItem;
m_adapterLayout->addWidget(adatpterItem);
getDevieInitStatus(adatpterItem);
connect(adatpterItem, &AdapterItem::deviceStateChanged, this, &BluetoothApplet::onDeviceStateChanged);
connect(adatpterItem, &AdapterItem::powerChanged, this, &BluetoothApplet::onPowerChanged);
connect(adatpterItem, &AdapterItem::sizeChange, this, &BluetoothApplet::updateView);
updateView();
}
void BluetoothApplet::removeAdapter(Adapter *adapter)
{
if (adapter) {
QString adapterId = adapter->id();
AdapterItem *adapterItem = m_adapterItems.value(adapterId);
if (adapterItem) {
m_adapterLayout->removeWidget(adapterItem);
delete adapterItem;
m_adapterItems.remove(adapterId);
updateView();
if (!m_adapterItems.size())
emit noAdapter();
}
}
}
void BluetoothApplet::updateView()
{
int contentHeight = 0;
int itemCount = 0;
bool isPowered = false;
for (AdapterItem *adapterItem : m_adapterItems) {
if (adapterItem) {
contentHeight += CONTROLHEIGHT;
if (adapterItem->isPowered()) {
isPowered = true;
itemCount += adapterItem->deviceCount();
}
}
}
m_openControlCenter->setVisible(isPowered);
if (isPowered) {
contentHeight += ITEMHEIGHT;
}
int adaptersCnt = m_adapterItems.size();
if (adaptersCnt > 1) {
m_line->setVisible(true);
m_appletName->setVisible(true);
} else {
m_line->setVisible(false);
m_appletName->setVisible(false);
}
if (adaptersCnt > 1)
contentHeight += m_appletName->height();
contentHeight += itemCount * ITEMHEIGHT;
m_centralWidget->setFixedHeight(contentHeight);
setFixedHeight(itemCount <= 10 ? contentHeight : 10 * ITEMHEIGHT);
setVerticalScrollBarPolicy(itemCount <= 10 ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAlwaysOn);
}
void BluetoothApplet::getDevieInitStatus(AdapterItem *item)
{
if (!item)
return;
bool powered = item->isPowered();
for (AdapterItem *adapterItem : m_adapterItems) {
if (adapterItem != item) {
if (adapterItem->isPowered()) {
powered = true;
break;
}
}
}
emit powerChanged(powered);
Device::State deviceState = item->initDeviceState();
Device::State otherDeviceState = Device::StateUnavailable;
for (AdapterItem *adapterItem : m_adapterItems) {
if (adapterItem != item) {
if (Device::StateAvailable == adapterItem->currentDeviceState()) {
otherDeviceState = Device::StateAvailable;
continue;
}
if (Device::StateConnected == adapterItem->currentDeviceState()) {
otherDeviceState = Device::StateConnected;
break;
}
}
}
switch (deviceState) {
case Device::StateConnected:
emit deviceStateChanged(deviceState);
break;
case Device::StateUnavailable:
emit deviceStateChanged(otherDeviceState);
break;
case Device::StateAvailable: {
if (otherDeviceState != Device::StateConnected)
emit deviceStateChanged(deviceState);
else
emit deviceStateChanged(otherDeviceState);
}
break;
}
}
void BluetoothApplet::setAdapterRefresh()
{
for (AdapterItem *adapterItem : m_adapterItems) {
if (adapterItem)
adapterItem->refresh();
}
}

View File

@ -1,83 +0,0 @@
/*
* 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/>.
*/
#ifndef BLUETOOTHAPPLET_H
#define BLUETOOTHAPPLET_H
#include "componments/device.h"
#include <QScrollArea>
class QLabel;
class QVBoxLayout;
class QHBoxLayout;
class Adapter;
class AdapterItem;
class HorizontalSeparator;
class MenueItem;
class AdaptersManager;
class BluetoothApplet : public QScrollArea
{
Q_OBJECT
public:
explicit BluetoothApplet(QWidget *parent = nullptr);
void setAdapterPowered(bool powered);
void setAdapterRefresh();
bool poweredInitState();
bool hasAadapter();
QStringList connectedDevsName();
public slots :
void addAdapter(Adapter *adapter);
void removeAdapter(Adapter *adapter);
signals:
void powerChanged(bool powered);
void deviceStateChanged(const Device::State state);
void noAdapter();
void justHasAdapter();
private slots:
void onPowerChanged();
void onDeviceStateChanged();
private:
void updateView();
void getDevieInitStatus(AdapterItem *item);
private:
HorizontalSeparator *m_line;
QLabel *m_appletName;
QWidget *m_centralWidget;
QVBoxLayout *m_centrealLayout;
QVBoxLayout *m_adapterLayout;
QHBoxLayout *m_menueLayout;
MenueItem *m_openControlCenter;
QVBoxLayout *m_textLayout;
AdaptersManager *m_adaptersManager;
QMap<QString, AdapterItem *> m_adapterItems;
Device::State m_initDeviceState;
};
#endif // BLUETOOTHAPPLET_H

View File

@ -24,7 +24,7 @@
#include "constants.h"
#include "../widgets/tipswidget.h"
#include "../frame/util/imageutil.h"
#include "bluetoothapplet.h"
#include "componments/bluetoothapplet.h"
#include <DApplication>
#include <DDBusSender>
@ -48,22 +48,23 @@ BluetoothItem::BluetoothItem(QWidget *parent)
, m_devState(Device::State::StateUnavailable)
, m_adapterPowered(m_applet->poweredInitState())
{
setAccessibleName("BluetoothPluginItem");
m_applet->setVisible(false);
m_tipsLabel->setVisible(false);
refreshIcon();
connect(m_applet, &BluetoothApplet::powerChanged, [&](bool powered) {
connect(m_applet, &BluetoothApplet::powerChanged, [ & ] (bool powered) {
m_adapterPowered = powered;
refreshIcon();
});
connect(m_applet, &BluetoothApplet::deviceStateChanged, [&](const Device::State state) {
m_devState = state;
connect(m_applet, &BluetoothApplet::deviceStateChanged, [ & ] (const Device* device) {
m_devState = device->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);
connect(m_applet, &BluetoothApplet::noAdapter, this, &BluetoothItem::noAdapter);
connect(m_applet, &BluetoothApplet::justHasAdapter, this, &BluetoothItem::justHasAdapter);
}
QWidget *BluetoothItem::tipsWidget()
@ -128,9 +129,6 @@ void BluetoothItem::invokeMenuItem(const QString menuId, const bool checked)
void BluetoothItem::refreshIcon()
{
if (!m_applet)
return;
QString stateString;
QString iconString;
@ -164,16 +162,13 @@ void BluetoothItem::refreshIcon()
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()) {
for (QString devName : m_applet->connectedDevicesName()) {
textList << tr("%1 connected").arg(devName);
}
m_tipsLabel->setTextList(textList);

View File

@ -30,6 +30,7 @@
#define BLUETOOTH_KEY "bluetooth-item-key"
class BluetoothApplet;
namespace Dock {
class TipsWidget;
}
@ -63,8 +64,8 @@ signals:
private:
Dock::TipsWidget *m_tipsLabel;
BluetoothApplet *m_applet;
QPixmap m_iconPixmap;
QPixmap m_iconPixmap;
Device::State m_devState;
bool m_adapterPowered;
};

View File

@ -1,344 +0,0 @@
/*
* 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 "adapteritem.h"
#include "switchitem.h"
#include "deviceitem.h"
#include "adapter.h"
#include "adaptersmanager.h"
#include "bluetoothconstants.h"
#include <QLabel>
#include <QVBoxLayout>
extern void initFontColor(QWidget *widget);
AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWidget *parent)
: QScrollArea(parent)
, m_centralWidget(new QWidget(this))
, m_line(new HorizontalSeparator(this))
, m_deviceLayout(new QVBoxLayout)
, m_adaptersManager(adapterManager)
, m_adapter(adapter)
, m_switchItem(new SwitchItem(this))
{
m_centralWidget->setFixedWidth(POPUPWIDTH);
m_line->setVisible(true);
m_deviceLayout->setMargin(0);
m_deviceLayout->setSpacing(0);
m_switchItem->setTitle(adapter->name());
m_switchItem->setChecked(adapter->powered(),false);
m_switchItem->setLoading(adapter->discover());
m_deviceLayout->addWidget(m_switchItem, Qt::AlignCenter);
m_deviceLayout->addWidget(m_line);
m_centralWidget->setFixedWidth(POPUPWIDTH);
m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
m_centralWidget->setLayout(m_deviceLayout);
setFixedWidth(POPUPWIDTH);
setWidget(m_centralWidget);
setFrameShape(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_centralWidget->setAutoFillBackground(false);
viewport()->setAutoFillBackground(false);
QMap<QString, const Device *> myDevices = adapter->devices();
for (const Device *constDevice : myDevices) {
auto device = const_cast<Device *>(constDevice);
if (device)
createDeviceItem(device);
}
m_initDeviceState = Device::StateUnavailable;
for (const Device *constDevice : myDevices) {
auto device = const_cast<Device *>(constDevice);
if (device) {
if (device->state() == Device::StateAvailable) {
m_initDeviceState = Device::StateAvailable;
continue;
}
if (device->state() == Device::StateConnected && device->connectState()) {
m_initDeviceState = Device::StateConnected;
break;
}
}
}
connect(m_switchItem, &SwitchItem::checkedChanged, this, &AdapterItem::showAndConnect);
connect(m_switchItem, &SwitchItem::refresh, this, &AdapterItem::refresh);
connect(m_switchItem, &SwitchItem::justUpdateView, [&](bool checked){
showDevices(checked);
emit powerChanged(checked);
});
connect(adapter, &Adapter::nameChanged, m_switchItem, &SwitchItem::setTitle);
connect(adapter, &Adapter::deviceAdded, this, &AdapterItem::addDeviceItem);
connect(adapter, &Adapter::deviceRemoved, this, &AdapterItem::removeDeviceItem);
connect(adapter, &Adapter::poweredChanged, m_switchItem, [=](const bool powered){
m_switchItem->setChecked(powered, false);
});
connect(adapter, &Adapter::discoveringChanged, m_switchItem, [=](const bool discovering){
m_switchItem->setLoading(discovering);
});
showDevices(adapter->powered());
}
int AdapterItem::deviceCount()
{
return m_deviceItems.size();
}
void AdapterItem::setPowered(bool powered)
{
m_switchItem->setChecked(powered,true);
}
bool AdapterItem::isPowered()
{
return m_switchItem->checkState();
}
QStringList AdapterItem::connectedDevsName()
{
QStringList devsName;
for (DeviceItem *devItem : m_sortConnected) {
if (devItem) {
devsName << devItem->title();
}
}
return devsName;
}
void AdapterItem::deviceItemPaired(const bool paired)
{
auto device = qobject_cast<Device *>(sender());
if (device) {
DeviceItem *deviceItem = m_deviceItems.value(device->id());
if (paired) {
int index = m_sortConnected.indexOf(deviceItem);
if (index < 0) {
m_sortUnConnect.removeOne(deviceItem);
m_sortConnected << deviceItem;
}
} else {
int index = m_sortUnConnect.indexOf(deviceItem);
if(index < 0){
m_sortConnected.removeOne(deviceItem);
m_sortUnConnect << deviceItem;
}
}
showDevices(m_adapter->powered());
}
}
void AdapterItem::removeDeviceItem(const Device *device)
{
if (!device)
return;
DeviceItem *deviceItem = m_deviceItems.value(device->id());
if (deviceItem) {
m_deviceItems.remove(device->id());
m_sortConnected.removeOne(deviceItem);
m_sortUnConnect.removeOne(deviceItem);
m_deviceLayout->removeWidget(deviceItem);
delete deviceItem;
}
showDevices(m_adapter->powered());
}
void AdapterItem::showAndConnect(bool change)
{
m_adaptersManager->setAdapterPowered(m_adapter, change);
showDevices(change);
emit powerChanged(change);
}
void AdapterItem::addDeviceItem(const Device *constDevice)
{
auto device = const_cast<Device *>(constDevice);
if (!device)
return;
createDeviceItem(device);
showDevices(m_adapter->powered());
}
void AdapterItem::deviceChangeState(const Device::State state)
{
auto device = qobject_cast<Device *>(sender());
auto setUnavailableItem = [this](const Device::State state, DeviceItem *deviceItem){
int index = m_sortUnConnect.indexOf(deviceItem);
if (index < 0) {
m_sortConnected.removeOne(deviceItem);
m_sortUnConnect << deviceItem;
moveDeviceItem(state, deviceItem);
}
};
if (device) {
DeviceItem *deviceItem = m_deviceItems.value(device->id());
if (deviceItem) {
switch (state) {
case Device::StateUnavailable: {
setUnavailableItem(state, deviceItem);
}
break;
case Device::StateAvailable: {
int index = m_sortUnConnect.indexOf(deviceItem);
if (index < 0)
m_sortConnected.removeOne(deviceItem);
}
break;
case Device::StateConnected: {
if (!device->connectState()) {
setUnavailableItem(state, deviceItem);
break;
}
int index = m_sortConnected.indexOf(deviceItem);
if (index < 0) {
m_sortUnConnect.removeOne(deviceItem);
m_sortConnected << deviceItem;
moveDeviceItem(state, deviceItem);
}
}
break;
}
}
if (m_sortConnected.size() > 0)
m_currentDeviceState = Device::StateConnected;
else if (state == Device::StateAvailable)
m_currentDeviceState = Device::StateAvailable;
else
m_currentDeviceState = Device::StateUnavailable;
emit deviceStateChanged();
}
}
void AdapterItem::moveDeviceItem(Device::State state, DeviceItem *item)
{
int size = m_sortConnected.size();
int index = 0;
switch (state) {
case Device::StateUnavailable:
case Device::StateAvailable: {
index = m_sortUnConnect.indexOf(item);
index += size;
}
break;
case Device::StateConnected: {
index = m_sortUnConnect.indexOf(item);
}
break;
}
index += 2;
m_deviceLayout->removeWidget(item);
m_deviceLayout->insertWidget(index, item);
}
void AdapterItem::createDeviceItem(Device *device)
{
if (!device)
return;
QString deviceId = device->id();
auto deviceItem = new DeviceItem(device, this);
m_deviceItems[deviceId] = deviceItem;
if (device->state() == Device::StateConnected)
m_sortConnected << deviceItem;
else
m_sortUnConnect << deviceItem;
connect(device, &Device::pairedChanged, this, &AdapterItem::deviceItemPaired);
connect(device, &Device::nameChanged, deviceItem, &DeviceItem::setTitle);
connect(device, &Device::stateChanged, deviceItem, &DeviceItem::changeState);
connect(device, &Device::stateChanged, this, &AdapterItem::deviceChangeState);
connect(deviceItem, &DeviceItem::clicked, m_adaptersManager, [this, deviceItem](Device *device) {
m_adaptersManager->connectDevice(device, m_adapter);
m_deviceLayout->removeWidget(deviceItem);
m_deviceLayout->insertWidget(1, deviceItem);
});
}
void AdapterItem::updateView()
{
int contentHeight = m_switchItem->height();
contentHeight += (m_deviceLayout->count() - 2) * ITEMHEIGHT;
m_centralWidget->setFixedHeight(contentHeight);
setFixedHeight(contentHeight);
emit sizeChange();
}
void AdapterItem::showDevices(bool powered)
{
QList<DeviceItem *> deviceItems;
for (DeviceItem *deviceItem : m_sortUnConnect) {
deviceItems.push_front(deviceItem); // 未连接设备倒序放进list里
}
for (DeviceItem *deviceItem : m_sortConnected) {
deviceItems.push_front(deviceItem); // 已连接设备倒序放进list里
}
// 在蓝牙关闭的时候会出现不在connected和Unconnect列表中的设备连接/关闭中的状态),关闭的时候使用总表参数
qDebug() << m_sortConnected.size() << m_sortUnConnect.size() << m_deviceItems.size();
if (powered) {
for (DeviceItem *deviceItem : deviceItems) {
if (deviceItem) {
m_deviceLayout->addWidget(deviceItem);
deviceItem->setVisible(powered);
}
}
} else {
for (DeviceItem *deviceItem : m_deviceItems) {
if (deviceItem) {
m_deviceLayout->removeWidget(deviceItem);
deviceItem->setVisible(powered);
}
}
for (DeviceItem *deviceItem : m_sortConnected) {
if (deviceItem) {
m_adaptersManager->disconnectDevice(deviceItem->device());
}
}
}
m_line->setVisible(powered);
updateView();
}
void AdapterItem::refresh()
{
if (m_adapter->discover())
return;
m_adaptersManager->adapterRefresh(m_adapter);
}

View File

@ -1,88 +0,0 @@
/*
* 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/>.
*/
#ifndef ADAPTERITEM_H
#define ADAPTERITEM_H
#include "device.h"
#include <QScrollArea>
class QLabel;
class QVBoxLayout;
class HorizontalSeparator;
class Adapter;
class SwitchItem;
class DeviceItem;
class AdaptersManager;
class MenueItem;
class AdapterItem : public QScrollArea
{
Q_OBJECT
public:
explicit AdapterItem(AdaptersManager *a, Adapter *adapter, QWidget *parent = nullptr);
int deviceCount();
void setPowered(bool powered);
bool isPowered();
inline Device::State initDeviceState() { return m_initDeviceState; }
inline Device::State currentDeviceState() { return m_currentDeviceState; }
QStringList connectedDevsName();
signals:
void deviceStateChanged();
void powerChanged(bool powered);
void sizeChange();
private slots:
void deviceItemPaired(const bool paired);
void removeDeviceItem(const Device *device);
void showAndConnect(bool change);
void addDeviceItem(const Device *constDevice);
void deviceChangeState(const Device::State state);
void moveDeviceItem(Device::State state, DeviceItem *item);
public slots:
void refresh();
private:
void createDeviceItem(Device *device);
void updateView();
void showDevices(bool powered);
private:
QWidget *m_centralWidget;
HorizontalSeparator *m_line;
QVBoxLayout *m_deviceLayout;
AdaptersManager *m_adaptersManager;
Adapter *m_adapter;
SwitchItem *m_switchItem;
QMap<QString, DeviceItem *> m_deviceItems;
Device::State m_initDeviceState;
Device::State m_currentDeviceState;
QList<DeviceItem *> m_sortConnected;
QList<DeviceItem *> m_sortUnConnect;
QMap<DeviceItem *, Device::State> m_devicesState;
};
#endif // ADAPTERITEM_H

View File

@ -37,11 +37,11 @@ AdaptersManager::AdaptersManager(QObject *parent)
this))
, m_defaultAdapterState(false)
{
connect(m_bluetoothInter, &DBusBluetooth::AdapterAdded, this, &AdaptersManager::addAdapter);
connect(m_bluetoothInter, &DBusBluetooth::AdapterRemoved, this, &AdaptersManager::removeAdapter);
connect(m_bluetoothInter, &DBusBluetooth::AdapterAdded, this, &AdaptersManager::onAddAdapter);
connect(m_bluetoothInter, &DBusBluetooth::AdapterRemoved, this, &AdaptersManager::onRemoveAdapter);
connect(m_bluetoothInter, &DBusBluetooth::AdapterPropertiesChanged, this, &AdaptersManager::onAdapterPropertiesChanged);
connect(m_bluetoothInter, &DBusBluetooth::DeviceAdded, this, &AdaptersManager::addDevice);
connect(m_bluetoothInter, &DBusBluetooth::DeviceRemoved, this, &AdaptersManager::removeDevice);
connect(m_bluetoothInter, &DBusBluetooth::DeviceAdded, this, &AdaptersManager::onAddDevice);
connect(m_bluetoothInter, &DBusBluetooth::DeviceRemoved, this, &AdaptersManager::onRemoveDevice);
connect(m_bluetoothInter, &DBusBluetooth::DevicePropertiesChanged, this, &AdaptersManager::onDevicePropertiesChanged);
#ifdef QT_DEBUG
@ -112,7 +112,7 @@ void AdaptersManager::setAdapterPowered(const Adapter *adapter, const bool &powe
}
}
void AdaptersManager::connectDevice(Device *device, Adapter *adapter)
void AdaptersManager::connectDevice(const Device *device, Adapter *adapter)
{
if (device) {
QDBusObjectPath path(device->id());
@ -171,14 +171,14 @@ void AdaptersManager::onDevicePropertiesChanged(const QString &json)
}
}
void AdaptersManager::addAdapter(const QString &json)
void AdaptersManager::onAddAdapter(const QString &json)
{
const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
auto adapter = new Adapter(this);
adapterAdd(adapter, doc.object());
}
void AdaptersManager::removeAdapter(const QString &json)
void AdaptersManager::onRemoveAdapter(const QString &json)
{
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
QJsonObject obj = doc.object();
@ -197,7 +197,7 @@ void AdaptersManager::removeAdapter(const QString &json)
}
}
void AdaptersManager::addDevice(const QString &json)
void AdaptersManager::onAddDevice(const QString &json)
{
const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
const QJsonObject obj = doc.object();
@ -219,7 +219,7 @@ void AdaptersManager::addDevice(const QString &json)
}
}
void AdaptersManager::removeDevice(const QString &json)
void AdaptersManager::onRemoveDevice(const QString &json)
{
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
QJsonObject obj = doc.object();

View File

@ -35,7 +35,7 @@ public:
explicit AdaptersManager(QObject *parent = nullptr);
void setAdapterPowered(const Adapter *adapter, const bool &powered);
void connectDevice(Device *device, Adapter *adapter);
void connectDevice(const Device *device, Adapter *adapter);
bool defaultAdapterInitPowerState();
int adaptersCount();
void adapterRefresh(const Adapter *adapter);
@ -49,11 +49,11 @@ private slots:
void onAdapterPropertiesChanged(const QString &json);
void onDevicePropertiesChanged(const QString &json);
void addAdapter(const QString &json);
void removeAdapter(const QString &json);
void onAddAdapter(const QString &json);
void onRemoveAdapter(const QString &json);
void addDevice(const QString &json);
void removeDevice(const QString &json);
void onAddDevice(const QString &json);
void onRemoveDevice(const QString &json);
private:
void adapterAdd(Adapter *adapter, const QJsonObject &adpterObj);
@ -62,7 +62,6 @@ private:
private:
DBusBluetooth *m_bluetoothInter;
QMap<QString, const Adapter *> m_adapters;
bool m_defaultAdapterState;
};

View File

@ -0,0 +1,276 @@
/*
* Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: chenwei <chenwei@uniontech.com>
*
* Maintainer: chenwei <chenwei@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 "bluetoothadapteritem.h"
#include "componments/adapter.h"
#include "bluetoothapplet.h"
#include "bluetoothconstants.h"
#include <QBoxLayout>
#include <QStandardItemModel>
#include <DFontSizeManager>
#include <DLabel>
#include <DSwitchButton>
#include <DListView>
#include <DSpinner>
#include <DApplicationHelper>
BluetoothDeviceItem::BluetoothDeviceItem(QStyle *style, const Device *device, DListView *parent)
: m_style(style)
, m_device(device)
, m_standarditem(new DStandardItem())
, m_loading(new DSpinner(parent))
{
initActionList();
initConnect();
}
BluetoothDeviceItem::~BluetoothDeviceItem()
{
if (m_loading != nullptr) {
delete m_loading;
m_loading = nullptr;
}
}
void BluetoothDeviceItem::initActionList()
{
m_labelAction = new DViewItemAction(Qt::AlignLeft | Qt::AlignVCenter, QSize(), QSize(), false);
m_stateAction = new DViewItemAction(Qt::AlignLeft | Qt::AlignVCenter, QSize(), QSize(), true);
m_loading->setFixedSize(QSize(24, 24));
m_stateAction->setWidget(m_loading);
m_standarditem->setAccessibleText(m_device->name());
m_standarditem->setActionList(Qt::RightEdge, {m_stateAction});
m_standarditem->setActionList(Qt::LeftEdge, {m_labelAction});
m_labelAction->setText(m_device->name());
updateDeviceState(m_device->state());
updateIconTheme(DGuiApplicationHelper::instance()->themeType());
}
void BluetoothDeviceItem::initConnect()
{
connect(DApplicationHelper::instance(), &DApplicationHelper::themeTypeChanged, this, &BluetoothDeviceItem::updateIconTheme);
connect(m_device, &Device::stateChanged, this, &BluetoothDeviceItem::updateDeviceState);
}
void BluetoothDeviceItem::updateIconTheme(DGuiApplicationHelper::ColorType type)
{
if (type == DGuiApplicationHelper::LightType) {
if (!m_device->deviceType().isEmpty()) {
m_deviceIcon = LightString.arg(m_device->deviceType());
} else {
m_deviceIcon = LightString.arg("other");
}
} else {
if (!m_device->deviceType().isEmpty()) {
m_deviceIcon = DarkString.arg(m_device->deviceType());
} else {
m_deviceIcon = DarkString.arg("other");
}
}
m_labelAction->setIcon(QIcon(m_deviceIcon));
}
void BluetoothDeviceItem::updateDeviceState(Device::State state)
{
m_labelAction->setText(m_device->name());
if (state == Device::StateAvailable) {
m_loading->start();
m_stateAction->setVisible(true);
m_standarditem->setCheckState(Qt::Unchecked);
} else if (state == Device::StateConnected){
m_loading->stop();
m_stateAction->setVisible(false);
m_standarditem->setCheckState(Qt::Checked);
emit requestTopDeviceItem(m_standarditem);
} else {
m_loading->stop();
m_stateAction->setVisible(false);
m_standarditem->setCheckState(Qt::Unchecked);
}
emit deviceStateChanged(m_device);
}
BluetoothAdapterItem::BluetoothAdapterItem(Adapter *adapter, QWidget *parent)
: QWidget(parent)
, m_adapter(adapter)
, m_adapterLabel(new SettingLabel(adapter->name(), this))
, m_adapterStateBtn(new DSwitchButton(this))
, m_deviceListview(new DListView(this))
, m_deviceModel(new QStandardItemModel(m_deviceListview))
{
initData();
initUi();
initConnect();
}
BluetoothAdapterItem::~BluetoothAdapterItem()
{
qDeleteAll(m_deviceItems);
}
void BluetoothAdapterItem::onConnectDevice(const QModelIndex &index)
{
const QStandardItemModel *deviceModel = dynamic_cast<const QStandardItemModel *>(index.model());
if (!deviceModel)
return;
DStandardItem *deviceitem = dynamic_cast<DStandardItem *>(deviceModel->item(index.row()));
foreach(const auto item, m_deviceItems) {
if (item->standardItem() == deviceitem) {
emit connectDevice(item->device(), m_adapter);
}
}
}
void BluetoothAdapterItem::onTopDeviceItem(DStandardItem *item)
{
if (!item || item->row() == -1 || item->row() == 0)
return;
int index1 = item->row();
QStandardItem *index = m_deviceModel->takeItem(index1, 0);
m_deviceModel->removeRow(index1);
m_deviceModel->insertRow(0, index);
}
void BluetoothAdapterItem::onAdapterNameChanged(const QString name)
{
m_adapterLabel->label()->setText(name);
}
int BluetoothAdapterItem::currentDeviceCount()
{
return m_deviceItems.size();
}
QStringList BluetoothAdapterItem::connectedDevicesName()
{
QStringList devsName;
for (BluetoothDeviceItem *devItem : m_deviceItems) {
if (devItem && devItem->device()->state() == Device::StateConnected) {
devsName << devItem->device()->name();
}
}
return devsName;
}
void BluetoothAdapterItem::initData()
{
if (!m_adapter->powered())
return;
foreach(const auto device, m_adapter->devices()) {
if (!m_deviceItems.contains(device->id()))
onDeviceAdded(device);
}
emit deviceCountChanged();
}
void BluetoothAdapterItem::onDeviceAdded(const Device *device)
{
int insertRow = 0;
foreach(const auto item, m_deviceItems) {
if (item->device()->connectState()) {
insertRow++;
}
}
BluetoothDeviceItem *item = new BluetoothDeviceItem(style(), device, m_deviceListview);
connect(item, &BluetoothDeviceItem::requestTopDeviceItem, this, &BluetoothAdapterItem::onTopDeviceItem);
connect(item, &BluetoothDeviceItem::deviceStateChanged, this, &BluetoothAdapterItem::deviceStateChanged);
m_deviceItems.insert(device->id(), item);
m_deviceModel->insertRow(insertRow, item->standardItem());
emit deviceCountChanged();
}
void BluetoothAdapterItem::onDeviceRemoved(const Device *device)
{
if(m_deviceItems.isEmpty())
return;
m_deviceModel->removeRow(m_deviceItems.value(device->id())->standardItem()->row());
m_deviceItems.value(device->id())->deleteLater();
m_deviceItems.remove(device->id());
emit deviceCountChanged();
}
void BluetoothAdapterItem::initUi()
{
setAccessibleName(m_adapter->name());
setContentsMargins(0, 0, 0, 0);
m_adapterLabel->setFixedSize(ItemWidth, TitleHeight);
m_adapterLabel->addSwichButton(m_adapterStateBtn);
DFontSizeManager::instance()->bind(m_adapterLabel->label(), DFontSizeManager::T4);
m_adapterStateBtn->setChecked(m_adapter->powered());
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
m_deviceListview->setAccessibleName("DeviceItemList");
m_deviceListview->setModel(m_deviceModel);
m_deviceListview->setItemSpacing(1);
m_deviceListview->setItemSize(QSize(ItemWidth, DeviceItemHeight));
m_deviceListview->setBackgroundType(DStyledItemDelegate::ClipCornerBackground);
m_deviceListview->setItemRadius(0);
m_deviceListview->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_deviceListview->setSelectionMode(QAbstractItemView::NoSelection);
m_deviceListview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_deviceListview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_deviceListview->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
m_deviceListview->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
mainLayout->addWidget(m_adapterLabel);
mainLayout->addSpacing(2);
mainLayout->addWidget(m_deviceListview);
}
void BluetoothAdapterItem::initConnect()
{
connect(m_adapter, &Adapter::deviceAdded, this, &BluetoothAdapterItem::onDeviceAdded);
connect(m_adapter, &Adapter::deviceRemoved, this, &BluetoothAdapterItem::onDeviceRemoved);
connect(m_adapter, &Adapter::nameChanged, this, &BluetoothAdapterItem::onAdapterNameChanged);
connect(m_deviceListview, &DListView::clicked, this, &BluetoothAdapterItem::onConnectDevice);
connect(m_adapter, &Adapter::poweredChanged, this, [ = ] (bool state) {
initData();
m_deviceListview->setVisible(state);
m_adapterStateBtn->setChecked(state);
m_adapterStateBtn->setEnabled(true);
emit adapterPowerChanged();
});
connect(m_adapterStateBtn, &DSwitchButton::clicked, this, [ = ] (bool state){
qDeleteAll(m_deviceItems);
m_deviceItems.clear();
m_deviceModel->clear();
m_deviceListview->setVisible(false);
m_adapterStateBtn->setEnabled(false);
emit requestSetAdapterPower(m_adapter, state);
});
}

View File

@ -0,0 +1,126 @@
/*
* Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: chenwei <chenwei@uniontech.com>
*
* Maintainer: chenwei <chenwei@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/>.
*/
#ifndef BLUETOOTHADAPTERITEM_H
#define BLUETOOTHADAPTERITEM_H
#include "componments/device.h"
#include <QWidget>
#include <DStyleHelper>
#include <DApplicationHelper>
DWIDGET_USE_NAMESPACE
DWIDGET_BEGIN_NAMESPACE
class DSwitchButton;
class DStandardItem;
class DListView;
class DSpinner;
DWIDGET_END_NAMESPACE
class Adapter;
class SettingLabel;
class QStandardItemModel;
const QString LightString = QString(":/light/buletooth_%1_light.svg");
const QString DarkString = QString(":/dark/buletooth_%1_dark.svg");
class BluetoothDeviceItem : public QObject
{
Q_OBJECT
public:
explicit BluetoothDeviceItem(QStyle *style = nullptr, const Device *device = nullptr, DListView *parent = nullptr);
virtual ~BluetoothDeviceItem();
DStandardItem *standardItem() { return m_standarditem; }
const Device *device() { return m_device; }
public slots:
// 系统主题发生改变时更新蓝牙图标
void updateIconTheme(DGuiApplicationHelper::ColorType type);
// 更新蓝牙设备的连接状态
void updateDeviceState(Device::State state);
signals:
void requestTopDeviceItem(DStandardItem *item);
void deviceStateChanged(const Device* device);
private:
void initActionList();
void initConnect();
DStyleHelper m_style;
QString m_deviceIcon;
const Device *m_device = nullptr;
DStandardItem *m_standarditem = nullptr;
DViewItemAction *m_labelAction = nullptr;
DViewItemAction *m_stateAction = nullptr;
DSpinner *m_loading = nullptr;
};
class BluetoothAdapterItem : public QWidget
{
Q_OBJECT
public:
explicit BluetoothAdapterItem(Adapter *adapter, QWidget *parent = nullptr);
~BluetoothAdapterItem();
Adapter *adapter() { return m_adapter; }
int currentDeviceCount();
QStringList connectedDevicesName();
public slots:
// 添加蓝牙设备
void onDeviceAdded(const Device *device);
// 移除蓝牙设备
void onDeviceRemoved(const Device *device);
// 连接蓝牙设备
void onConnectDevice(const QModelIndex &index);
// 将已连接的蓝牙设备放到列表第一个
void onTopDeviceItem(DStandardItem* item);
// 设置蓝牙适配器名称
void onAdapterNameChanged(const QString name);
signals:
void adapterPowerChanged();
void requestSetAdapterPower(Adapter *adapter, bool state);
void connectDevice(const Device *device, Adapter *adapter);
void deviceCountChanged();
void deviceStateChanged(const Device* device);
private:
void initData();
void initUi();
void initConnect();
Adapter *m_adapter = nullptr;
SettingLabel *m_adapterLabel = nullptr;
DSwitchButton *m_adapterStateBtn = nullptr;
DListView *m_deviceListview = nullptr;
QStandardItemModel *m_deviceModel = nullptr;
QMap<QString, BluetoothDeviceItem *> m_deviceItems;
};
#endif // BLUETOOTHADAPTERITEM_H

View File

@ -0,0 +1,257 @@
/*
* Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: chenwei <chenwei@uniontech.com>
*
* Maintainer: chenwei <chenwei@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 "bluetoothapplet.h"
#include "device.h"
#include "bluetoothconstants.h"
#include "adaptersmanager.h"
#include "adapter.h"
#include "bluetoothadapteritem.h"
#include <QString>
#include <QBoxLayout>
#include <QMouseEvent>
#include <QDebug>
#include <QScrollArea>
#include <DApplicationHelper>
#include <DDBusSender>
#include <DLabel>
#include <DSwitchButton>
#include <DScrollArea>
#include <DListView>
SettingLabel::SettingLabel(QString text, QWidget *parent)
: QWidget(parent)
, m_label(new DLabel(text, this))
, m_layout(new QHBoxLayout(this))
{
setAccessibleName("BluetoothSettingLabel");
setContentsMargins(0, 0, 0, 0);
m_layout->setMargin(0);
m_layout->addSpacing(20);
m_layout->addWidget(m_label, 0, Qt::AlignLeft | Qt::AlignHCenter);
}
void SettingLabel::addSwichButton(DSwitchButton *button)
{
m_layout->addWidget(button, 0, Qt::AlignRight | Qt::AlignHCenter);
m_layout->addSpacing(10);
}
void SettingLabel::mousePressEvent(QMouseEvent *ev)
{
if (ev->button() == Qt::LeftButton) {
Q_EMIT clicked();
return;
}
return QWidget::mousePressEvent(ev);
}
void SettingLabel::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(Qt::NoPen);
if (DApplicationHelper::instance()->themeType() == DApplicationHelper::LightType) {
painter.setBrush(QColor(0, 0, 0, 0.03 * 255));
} else {
painter.setBrush(QColor(255, 255, 255, 0.03 * 255));
}
painter.drawRoundedRect(rect(), 0, 0);
return QWidget::paintEvent(event);
}
BluetoothApplet::BluetoothApplet(QWidget *parent)
: QWidget(parent)
, m_contentWidget(new QWidget(this))
, m_adaptersManager(new AdaptersManager(this))
, m_settingLabel(new SettingLabel(tr("Bluetooth settings"), this))
, m_mainLayout(new QVBoxLayout(this))
, m_contentLayout(new QVBoxLayout(m_contentWidget))
{
initUi();
initConnect();
}
bool BluetoothApplet::poweredInitState()
{
foreach(const auto adapter, m_adapterItems) {
if (adapter->adapter()->powered()) {
return true;
}
}
return false;
}
bool BluetoothApplet::hasAadapter()
{
return m_adaptersManager->adaptersCount();
}
void BluetoothApplet::setAdapterRefresh()
{
for (BluetoothAdapterItem *adapterItem : m_adapterItems) {
if (adapterItem->adapter()->discover())
m_adaptersManager->adapterRefresh(adapterItem->adapter());
}
updateSize();
}
void BluetoothApplet::setAdapterPowered(bool state)
{
for (BluetoothAdapterItem *adapterItem : m_adapterItems) {
if (adapterItem)
m_adaptersManager->setAdapterPowered(adapterItem->adapter(), state);
}
}
QStringList BluetoothApplet::connectedDevicesName()
{
QStringList deviceList;
for (BluetoothAdapterItem *adapterItem : m_adapterItems) {
if (adapterItem)
deviceList << adapterItem->connectedDevicesName();
}
return deviceList;
}
void BluetoothApplet::onAdapterAdded(Adapter *adapter)
{
if (!m_adapterItems.size()) {
emit justHasAdapter();
}
if (m_adapterItems.contains(adapter->id())) {
onAdapterRemoved(m_adapterItems.value(adapter->id())->adapter());
}
BluetoothAdapterItem *adapterItem = new BluetoothAdapterItem(adapter, this);
connect(adapterItem, &BluetoothAdapterItem::requestSetAdapterPower, this, &BluetoothApplet::onSetAdapterPower);
connect(adapterItem, &BluetoothAdapterItem::connectDevice, m_adaptersManager, &AdaptersManager::connectDevice);
connect(adapterItem, &BluetoothAdapterItem::deviceCountChanged, this, &BluetoothApplet::updateSize);
connect(adapterItem, &BluetoothAdapterItem::adapterPowerChanged, this, &BluetoothApplet::updateBluetoothPowerState);
connect(adapterItem, &BluetoothAdapterItem::deviceStateChanged, this, &BluetoothApplet::deviceStateChanged);
m_adapterItems.insert(adapter->id(), adapterItem);
m_contentLayout->insertWidget(0, adapterItem, Qt::AlignTop | Qt::AlignVCenter);
updateBluetoothPowerState();
updateSize();
}
void BluetoothApplet::onAdapterRemoved(Adapter *adapter)
{
m_contentLayout->removeWidget(m_adapterItems.value(adapter->id()));
m_adapterItems.value(adapter->id())->deleteLater();
m_adapterItems.remove(adapter->id());
if (m_adapterItems.isEmpty()) {
emit noAdapter();
}
updateBluetoothPowerState();
updateSize();
}
void BluetoothApplet::onSetAdapterPower(Adapter *adapter, bool state)
{
m_adaptersManager->setAdapterPowered(adapter, state);
updateSize();
}
void BluetoothApplet::updateBluetoothPowerState()
{
foreach(const auto item, m_adapterItems) {
if (item->adapter()->powered()) {
emit powerChanged(true);
return;
}
}
emit powerChanged(false);
updateSize();
}
void BluetoothApplet::initUi()
{
setFixedWidth(ItemWidth);
setAccessibleName("BluetoothApplet");
setContentsMargins(0, 0, 0, 0);
m_settingLabel->setFixedHeight(DeviceItemHeight);
DFontSizeManager::instance()->bind(m_settingLabel->label(), DFontSizeManager::T7);
m_contentWidget->setContentsMargins(0, 0, 0, 0);
m_contentLayout->setMargin(0);
m_contentLayout->setSpacing(0);
m_contentLayout->addWidget(m_settingLabel, 0, Qt::AlignBottom | Qt::AlignVCenter);
QScrollArea *scroarea = new QScrollArea(this);
m_contentWidget->setAttribute(Qt::WA_TranslucentBackground);
scroarea->setWidgetResizable(true);
scroarea->setFrameStyle(QFrame::NoFrame);
scroarea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scroarea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scroarea->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
scroarea->setContentsMargins(0, 0, 0, 0);
scroarea->setWidget(m_contentWidget);
m_mainLayout->setMargin(0);
m_mainLayout->setSpacing(0);
m_mainLayout->addWidget(scroarea);
updateSize();
}
void BluetoothApplet::initConnect()
{
connect(m_adaptersManager, &AdaptersManager::adapterIncreased, this, &BluetoothApplet::onAdapterAdded);
connect(m_adaptersManager, &AdaptersManager::adapterDecreased, this, &BluetoothApplet::onAdapterRemoved);
connect(m_settingLabel, &SettingLabel::clicked, this, [ = ] {
DDBusSender()
.service("com.deepin.dde.ControlCenter")
.interface("com.deepin.dde.ControlCenter")
.path("/com/deepin/dde/ControlCenter")
.method(QString("ShowModule"))
.arg(QString("bluetooth"))
.call();
});
}
void BluetoothApplet::updateSize()
{
int hetght = 0;
int count = 0;
foreach(const auto item, m_adapterItems) {
hetght += TitleHeight + TitleSpace;
if (item->adapter()->powered()) {
count += item->currentDeviceCount();
hetght += count * DeviceItemHeight;
}
}
hetght += DeviceItemHeight;
int maxHeight = (TitleHeight + TitleSpace) + MaxDeviceCount * DeviceItemHeight;
hetght = hetght > maxHeight ? maxHeight : hetght;
setFixedSize(ItemWidth, hetght);
}

View File

@ -0,0 +1,112 @@
/*
* Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: chenwei <chenwei@uniontech.com>
*
* Maintainer: chenwei <chenwei@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/>.
*/
#ifndef BLUETOOTHAPPLET_H
#define BLUETOOTHAPPLET_H
#include <QWidget>
#include <QStandardItemModel>
#include <dtkwidget_global.h>
class QVBoxLayout;
class QHBoxLayout;
class Device;
class Adapter;
class BluetoothAdapterItem;
class AdaptersManager;
DWIDGET_BEGIN_NAMESPACE
class DLabel;
class DSwitchButton;
class DListView;
DWIDGET_END_NAMESPACE
DWIDGET_USE_NAMESPACE
class SettingLabel : public QWidget
{
Q_OBJECT
public:
explicit SettingLabel(QString text, QWidget *parent = nullptr);
void addSwichButton(DSwitchButton *button);
DLabel *label() { return m_label; }
signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent *ev) override;
void paintEvent(QPaintEvent *event) override;
private:
DLabel *m_label = nullptr;
QHBoxLayout *m_layout = nullptr;
};
class BluetoothApplet : public QWidget
{
Q_OBJECT
public:
explicit BluetoothApplet(QWidget *parent = nullptr);
bool poweredInitState();
// 当前是否有蓝牙适配器
bool hasAadapter();
// 刷新蓝牙适配器搜索到的设备列表
void setAdapterRefresh();
// 设置当前所有蓝牙适配器的电源状态
void setAdapterPowered(bool state);
// 已连接蓝牙设备名称列表用于获取鼠标悬浮在蓝牙插件上时tips显示内容
QStringList connectedDevicesName();
signals:
void noAdapter();
void justHasAdapter();
void powerChanged(bool state);
void deviceStateChanged(const Device* device);
public slots:
// 蓝牙适配器增加
void onAdapterAdded(Adapter *adapter);
// 蓝牙适配器移除
void onAdapterRemoved(Adapter *adapter);
// 设置蓝牙适配器电源是否开启
void onSetAdapterPower(Adapter *adapter, bool state);
// 更新蓝牙适配器电源状态,用于更新任务栏蓝牙插件图标的显示状态
void updateBluetoothPowerState();
private:
void initUi();
void initConnect();
// 更新蓝牙插件主界面大小
void updateSize();
QWidget *m_contentWidget = nullptr;
AdaptersManager *m_adaptersManager = nullptr;
SettingLabel *m_settingLabel = nullptr;
QVBoxLayout *m_mainLayout = nullptr;
QVBoxLayout *m_contentLayout = nullptr;
QStringList m_connectDeviceName;
QMap<QString, BluetoothAdapterItem *> m_adapterItems;
};
#endif // BLUETOOTHAPPLET_H

View File

@ -1,18 +1,10 @@
#ifndef BLUETOOTHCONSTANTS_H
#define BLUETOOTHCONSTANTS_H
#define POPUPWIDTH (200)
#define ITEMHEIGHT (30)
#define CONTROLHEIGHT (35)
#define CONTROLTITLEHEIGHT (45)
#define LIGHTSUFFIX ("_dark.svg")
#define DARKSUFFIX (".svg")
#define MARGIN (12)
#define SWITCHBUTTONWIDTH (50)
#define LIGHTICON "light/buletooth_"
#define DARKICON "dark/buletooth_"
#define LIGHTICONSUFFIX "_light.svg"
#define DARKICONSUFFIX "_dark.svg"
const int DeviceItemHeight = 36;
const int ItemWidth = 300;
const int TitleHeight = 46;
const int TitleSpace = 2;
const int MaxDeviceCount = 8;
#endif // BLUETOOTHCONSTANTS_H

View File

@ -57,7 +57,6 @@ Device::Device(QObject *parent)
Device::~Device()
{
emit stateChanged(StateUnavailable);
}
void Device::updateDeviceTime()

View File

@ -1,189 +0,0 @@
/*
* 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 "deviceitem.h"
#include "constants.h"
#include "bluetoothconstants.h"
#include "util/imageutil.h"
#include "util/statebutton.h"
#include <DApplicationHelper>
#include <DStyle>
#include <QHBoxLayout>
#include <QPainter>
DGUI_USE_NAMESPACE
extern void initFontColor(QWidget *widget);
DeviceItem::DeviceItem(Device *d, QWidget *parent)
: QWidget(parent)
, m_title(new QLabel(this))
, m_state(new StateButton(this))
, m_loadingStat(new DSpinner)
, m_line(new HorizontalSeparator(this))
, m_typeIcon(new QLabel(this))
{
m_device = d;
setFixedHeight(ITEMHEIGHT);
auto themeChanged = [&](DApplicationHelper::ColorType themeType){
QString iconPrefix;
QString iconSuffix;
switch (themeType) {
case DApplicationHelper::UnknownType:
case DApplicationHelper::LightType: {
iconPrefix = LIGHTICON;
iconSuffix = LIGHTICONSUFFIX;
}
break;
case DApplicationHelper::DarkType: {
iconPrefix = DARKICON;
iconSuffix = DARKICONSUFFIX;
}
}
QString iconString;
if (!m_device->deviceType().isEmpty())
iconString = iconPrefix + m_device->deviceType() + iconSuffix;
else
iconString = iconPrefix + QString("other") + iconSuffix;
m_typeIcon->setPixmap(ImageUtil::loadSvg(iconString, ":/", PLUGIN_ICON_MIN_SIZE, devicePixelRatioF()));
};
themeChanged(DApplicationHelper::instance()->themeType());
m_state->setType(StateButton::Check);
m_state->setFixedSize(PLUGIN_ICON_MAX_SIZE, PLUGIN_ICON_MAX_SIZE);
m_state->setVisible(false);
m_title->setText(nameDecorated(m_device->name()));
initFontColor(m_title);
m_line->setVisible(false);
m_loadingStat->setFixedSize(20, 20);
m_loadingStat->setVisible(false);
auto itemLayout = new QHBoxLayout;
itemLayout->setMargin(0);
itemLayout->setSpacing(0);
itemLayout->addSpacing(MARGIN);
itemLayout->addWidget(m_typeIcon);
itemLayout->addSpacing(5);
itemLayout->addWidget(m_title);
itemLayout->addStretch();
itemLayout->addWidget(m_state);
itemLayout->addWidget(m_loadingStat);
itemLayout->addSpacing(MARGIN);
setLayout(itemLayout);
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, themeChanged);
changeState(m_device->state());
}
void DeviceItem::setTitle(const QString &name)
{
m_title->setText(nameDecorated(name));
}
void DeviceItem::mousePressEvent(QMouseEvent *event)
{
m_device->updateDeviceTime();
emit clicked(m_device);
QWidget::mousePressEvent(event);
}
void DeviceItem::paintEvent(QPaintEvent *e)
{
if (LightType == DApplicationHelper::instance()->themeType()) {
QPainter painter(this);
QRect rc = rect();
rc.moveTop(1);
painter.fillRect(rc, QColor(255, 255, 255, 51));
QWidget::paintEvent(e);
}
}
void DeviceItem::changeState(const Device::State state)
{
switch (state) {
case Device::StateUnavailable: {
m_state->setVisible(false);
m_loadingStat->stop();
m_loadingStat->hide();
m_loadingStat->setVisible(false);
}
break;
case Device::StateAvailable: {
m_state->setVisible(false);
m_loadingStat->start();
m_loadingStat->show();
m_loadingStat->setVisible(true);
}
break;
case Device::StateConnected: {
m_loadingStat->stop();
m_loadingStat->hide();
m_loadingStat->setVisible(false);
m_state->setVisible(true);
}
break;
}
}
QString DeviceItem::nameDecorated(const QString &name)
{
return QFontMetrics(m_title->font()).elidedText(name, Qt::ElideRight, POPUPWIDTH - MARGIN * 2 - PLUGIN_ICON_MIN_SIZE - 30);
}
HorizontalSeparator::HorizontalSeparator(QWidget *parent)
: QWidget(parent)
{
setFixedHeight(2);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
void HorizontalSeparator::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
QPainter painter(this);
if (LightType == DApplicationHelper::instance()->themeType()) {
painter.fillRect(rect(), QColor(0, 0, 0, 10));
} else if (DarkType == DApplicationHelper::instance()->themeType()) {
painter.fillRect(rect(), QColor(255, 255, 255, 13));
}
}
MenueItem::MenueItem(QWidget *parent)
: QLabel(parent)
{
}
void MenueItem::mousePressEvent(QMouseEvent *event)
{
QLabel::mousePressEvent(event);
emit clicked();
}

View File

@ -1,97 +0,0 @@
/*
* 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/>.
*/
#ifndef DEVICEITEM_H
#define DEVICEITEM_H
#include "device.h"
#include <DSpinner>
#include <QLabel>
DWIDGET_USE_NAMESPACE
enum ColorType {
UnknownType,
LightType,
DarkType
};
class StateButton;
class HorizontalSeparator;
class DeviceItem : public QWidget
{
Q_OBJECT
public:
explicit DeviceItem(Device *d, QWidget *parent = nullptr);
void setTitle(const QString &name);
inline QString title() { return m_title->text(); }
inline Device *device() { return m_device; }
inline const Device *device() const { return m_device; }
protected:
void mousePressEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *e) override;
signals:
void clicked(Device *);
public slots:
void changeState(const Device::State state);
private:
QString nameDecorated(const QString &name);
private:
QLabel *m_title;
StateButton *m_state;
DSpinner *m_loadingStat;
Device *m_device;
HorizontalSeparator *m_line;
QLabel *m_typeIcon;
};
class HorizontalSeparator : public QWidget
{
Q_OBJECT
public:
explicit HorizontalSeparator(QWidget *parent = nullptr);
protected:
void paintEvent(QPaintEvent *e);
};
class MenueItem : public QLabel
{
Q_OBJECT
public:
explicit MenueItem(QWidget *parent = nullptr);
signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent *event) override;
};
#endif // DEVICEITEM_H

View File

@ -1,137 +0,0 @@
/*
* 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 "switchitem.h"
#include "bluetoothconstants.h"
#include <DHiDPIHelper>
#include <DApplicationHelper>
#include <DFontSizeManager>
#include <QHBoxLayout>
#include <QFontMetrics>
#include <QLabel>
#include <QEvent>
extern void initFontColor(QWidget *widget);
SwitchItem::SwitchItem(QWidget *parent)
: QWidget(parent)
, m_title(new QLabel(this))
, m_switchBtn(new DSwitchButton(this))
, m_default(false)
{
initFontColor(m_title);
DFontSizeManager::instance()->bind(m_title, DFontSizeManager::T4, QFont::Medium);
m_switchBtn->setFixedWidth(SWITCHBUTTONWIDTH);
const QPixmap pixmap = DHiDPIHelper::loadNxPixmap(":/wireless/resources/wireless/refresh_dark.svg");
m_loadingIndicator = new DLoadingIndicator;
m_loadingIndicator->setSmooth(true);
m_loadingIndicator->setAniDuration(500);
m_loadingIndicator->setAniEasingCurve(QEasingCurve::InOutCirc);
m_loadingIndicator->installEventFilter(this);
m_loadingIndicator->setFixedSize(pixmap.size() / devicePixelRatioF());
m_loadingIndicator->viewport()->setAutoFillBackground(false);
m_loadingIndicator->setFrameShape(QFrame::NoFrame);
m_loadingIndicator->installEventFilter(this);
auto themeChanged = [&](DApplicationHelper::ColorType themeType){
Q_UNUSED(themeType)
setLoadIndicatorIcon();
};
themeChanged(DApplicationHelper::instance()->themeType());
setFixedHeight(CONTROLTITLEHEIGHT);
auto switchLayout = new QHBoxLayout;
switchLayout->setSpacing(0);
switchLayout->setMargin(0);
switchLayout->addSpacing(MARGIN);
switchLayout->addWidget(m_title);
switchLayout->addStretch();
switchLayout->addWidget(m_loadingIndicator);
switchLayout->addSpacing(MARGIN);
switchLayout->addWidget(m_switchBtn);
switchLayout->addSpacing(5);
setLayout(switchLayout);
connect(m_switchBtn, &DSwitchButton::toggled, [&](bool change) {
m_checkState = change;
m_loadingIndicator->setVisible(change);
emit checkedChanged(change);
});
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, themeChanged);
}
void SwitchItem::setChecked(const bool checked,bool notify)
{
m_checkState = checked;
if(!notify) { // 防止收到蓝牙开启或关闭信号后再触发一次打开或关闭
m_switchBtn->blockSignals(true);
m_switchBtn->setChecked(checked);
m_switchBtn->blockSignals(false);
emit justUpdateView(checked);
}
else {
m_switchBtn->setChecked(checked);
}
}
void SwitchItem::setTitle(const QString &title)
{
int width = POPUPWIDTH - MARGIN * 2 - m_switchBtn->width() - 3;
QString strTitle = QFontMetrics(m_title->font()).elidedText(title, Qt::ElideRight, width);
m_title->setText(strTitle);
}
bool SwitchItem::eventFilter(QObject *obj, QEvent *event)
{
if (obj == m_loadingIndicator) {
if (event->type() == QEvent::MouseButtonPress) {
if(!m_loadingIndicator->loading())
Q_EMIT refresh();
}
}
return false;
}
void SwitchItem::setLoading(const bool bloading)
{
m_loadingIndicator->setLoading(bloading);
}
void SwitchItem::setLoadIndicatorIcon()
{
QString filePath = ":/wireless/resources/wireless/refresh.svg";
if(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
filePath = ":/wireless/resources/wireless/refresh_dark.svg";
const QPixmap pixmap = DHiDPIHelper::loadNxPixmap(filePath);
m_loadingIndicator->setImageSource(pixmap);
}
//void SwitchItem::mousePressEvent(QMouseEvent *event)
//{
// emit clicked(m_adapterId);
// QWidget::mousePressEvent(event);
//}

View File

@ -1,64 +0,0 @@
/*
* 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/>.
*/
#ifndef SWITCHITEM_H
#define SWITCHITEM_H
#include <DSwitchButton>
#include <dloadingindicator.h>
#include <DGuiApplicationHelper>
DGUI_USE_NAMESPACE
DWIDGET_USE_NAMESPACE
class QLabel;
class SwitchItem : public QWidget
{
Q_OBJECT
public:
explicit SwitchItem(QWidget *parent = nullptr);
void setChecked(const bool checked = true,bool notify = false);
void setTitle(const QString &title);
void setLoading(const bool bloading);
void setLoadIndicatorIcon();
inline bool checkState() { return m_checkState; }
inline bool isdefault() { return m_default; }
inline void setDefault(bool def) { m_default = def; }
signals:
void checkedChanged(bool checked);
void justUpdateView(bool checked);
void refresh();
protected:
bool eventFilter(QObject *obj,QEvent *event) override;
private:
QLabel *m_title;
DSwitchButton *m_switchBtn;
bool m_default;
bool m_checkState;
DLoadingIndicator *m_loadingIndicator;
};
#endif // SWITCHITEM_H