diff --git a/plugins/bluetooth/bluetoothapplet.cpp b/plugins/bluetooth/bluetoothapplet.cpp index 8543234d2..a6cf834b2 100644 --- a/plugins/bluetooth/bluetoothapplet.cpp +++ b/plugins/bluetooth/bluetoothapplet.cpp @@ -27,14 +27,11 @@ #include "componments/switchitem.h" #include "componments/adaptersmanager.h" #include "componments/adapteritem.h" +#include "componments/bluetoothconstants.h" #include DGUI_USE_NAMESPACE -extern int ControlHeight; -extern int ItemHeight; -const int Width = 200; - extern void initFontColor(QWidget *widget) { if (!widget) @@ -74,7 +71,7 @@ BluetoothApplet::BluetoothApplet(QWidget *parent) auto appletNameLayout = new QHBoxLayout; appletNameLayout->setMargin(0); appletNameLayout->setSpacing(0); - appletNameLayout->addSpacing(12); + appletNameLayout->addSpacing(MARGIN); appletNameLayout->addWidget(m_appletName); appletNameLayout->addStretch(); @@ -83,10 +80,10 @@ BluetoothApplet::BluetoothApplet(QWidget *parent) m_centrealLayout->addLayout(appletNameLayout); m_centrealLayout->addWidget(m_line); m_centralWidget->setLayout(m_centrealLayout); - m_centralWidget->setFixedWidth(Width); + m_centralWidget->setFixedWidth(POPUPWIDTH); m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); - setFixedWidth(Width); + setFixedWidth(POPUPWIDTH); setWidget(m_centralWidget); setFrameShape(QFrame::NoFrame); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -116,23 +113,6 @@ bool BluetoothApplet::hasAadapter() return m_adaptersManager->adaptersCount(); } -Device::State BluetoothApplet::initDeviceState() -{ - m_initDeviceState = Device::StateUnavailable; - for (auto adapterItem : m_adapterItems) { - if (adapterItem) - if (Device::StateAvailable == adapterItem->initDeviceState()) { - m_initDeviceState = Device::StateAvailable; - continue; - } - if (Device::StateConnected == adapterItem->initDeviceState()) { - m_initDeviceState = Device::StateConnected; - break; - } - } - return m_initDeviceState; -} - void BluetoothApplet::onPowerChanged(bool state) { Q_UNUSED(state) @@ -146,10 +126,8 @@ void BluetoothApplet::onPowerChanged(bool state) emit powerChanged(powerState); } -void BluetoothApplet::onDeviceStateChanged(const Device::State state) +void BluetoothApplet::onDeviceStateChanged() { - Q_UNUSED(state) - Device::State deviceState = Device::StateUnavailable; for (auto adapterItem : m_adapterItems) { if (Device::StateAvailable == adapterItem->currentDeviceState()) { @@ -178,6 +156,7 @@ void BluetoothApplet::addAdapter(Adapter *adapter) auto adatpterItem = new AdapterItem(m_adaptersManager, adapter, this); m_adapterItems[adapterId] = adatpterItem; m_centrealLayout->addWidget(adatpterItem); + getDevieInitState(adatpterItem); connect(adatpterItem, &AdapterItem::deviceStateChanged, this, &BluetoothApplet::onDeviceStateChanged); connect(adatpterItem, &AdapterItem::powerChanged, this, &BluetoothApplet::onPowerChanged); @@ -226,14 +205,50 @@ void BluetoothApplet::updateView() contentHeight += m_appletName->height(); if (itemCount <= 10) { - contentHeight += itemCount * ItemHeight; + contentHeight += itemCount * ITEMHEIGHT; m_centralWidget->setFixedHeight(contentHeight); setFixedHeight(contentHeight); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); } else { - contentHeight += itemCount * ItemHeight; + contentHeight += itemCount * ITEMHEIGHT; m_centralWidget->setFixedHeight(contentHeight); - setFixedHeight(10 * ItemHeight); + setFixedHeight(10 * ITEMHEIGHT); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); } } + +void BluetoothApplet::getDevieInitState(AdapterItem *item) +{ + if (!item) + return; + + Device::State deviceState = item->initDeviceState(); + Device::State otherDeviceState = Device::StateUnavailable; + for (auto 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; + } +} diff --git a/plugins/bluetooth/bluetoothapplet.h b/plugins/bluetooth/bluetoothapplet.h index a92aba612..657e4a575 100644 --- a/plugins/bluetooth/bluetoothapplet.h +++ b/plugins/bluetooth/bluetoothapplet.h @@ -41,7 +41,6 @@ public: void setAdapterPowered(bool powered); bool poweredInitState(); bool hasAadapter(); - Device::State initDeviceState(); public slots : void addAdapter(Adapter *constadapter); @@ -55,10 +54,11 @@ signals: private slots: void onPowerChanged(bool state); - void onDeviceStateChanged(const Device::State state); + void onDeviceStateChanged(); private: void updateView(); + void getDevieInitState(AdapterItem *item); private: HorizontalSeparator *m_line; diff --git a/plugins/bluetooth/bluetoothitem.cpp b/plugins/bluetooth/bluetoothitem.cpp index 7c27c15a7..ae7fe3e98 100644 --- a/plugins/bluetooth/bluetoothitem.cpp +++ b/plugins/bluetooth/bluetoothitem.cpp @@ -47,8 +47,6 @@ BluetoothItem::BluetoothItem(QWidget *parent) m_applet->setVisible(false); m_adapterPowered = m_applet->poweredInitState(); - m_devState = m_applet->initDeviceState(); - connect(m_timer, &QTimer::timeout, this, &BluetoothItem::refreshIcon); connect(m_applet, &BluetoothApplet::powerChanged, [&](bool powered) { m_adapterPowered = powered; diff --git a/plugins/bluetooth/componments/adapteritem.cpp b/plugins/bluetooth/componments/adapteritem.cpp index 8a21738b1..4ea6fa75a 100644 --- a/plugins/bluetooth/componments/adapteritem.cpp +++ b/plugins/bluetooth/componments/adapteritem.cpp @@ -25,12 +25,10 @@ #include "deviceitem.h" #include "adapter.h" #include "adaptersmanager.h" +#include "bluetoothconstants.h" #include -extern const int ItemHeight; -extern const int ControlHeight; -const int Width = 200; extern void initFontColor(QWidget *widget); AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWidget *parent) @@ -43,13 +41,13 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid , m_adapter(adapter) , m_switchItem(new SwitchItem(this)) { - m_centralWidget->setFixedWidth(Width); + m_centralWidget->setFixedWidth(POPUPWIDTH); m_line->setVisible(true); m_deviceLayout->setMargin(0); m_deviceLayout->setSpacing(0); m_openControlCenter->setText(tr("Bluetooth settings")); initFontColor(m_openControlCenter); - m_openControlCenter->setFixedHeight(ItemHeight); + m_openControlCenter->setFixedHeight(ITEMHEIGHT); m_openControlCenter->setVisible(false); m_switchItem->setTitle(adapter->name()); m_switchItem->setChecked(adapter->powered()); @@ -57,11 +55,11 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid m_deviceLayout->addWidget(m_switchItem); m_deviceLayout->addWidget(m_line); m_deviceLayout->addWidget(m_openControlCenter); - m_centralWidget->setFixedWidth(Width); + m_centralWidget->setFixedWidth(POPUPWIDTH); m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); m_centralWidget->setLayout(m_deviceLayout); - setFixedWidth(Width); + setFixedWidth(POPUPWIDTH); setWidget(m_centralWidget); setFrameShape(QFrame::NoFrame); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -126,7 +124,7 @@ bool AdapterItem::isPowered() int AdapterItem::viewHeight() { - return m_openControlCenter->isVisible() ? ControlHeight + ItemHeight : ControlHeight; + return m_openControlCenter->isVisible() ? CONTROLHEIGHT + ITEMHEIGHT : CONTROLHEIGHT; } void AdapterItem::deviceItemPaired(const bool paired) @@ -223,10 +221,9 @@ void AdapterItem::deviceChangeState(const Device::State state) break; } } + m_currentDeviceState = state; + emit deviceStateChanged(state); } - - m_currentDeviceState = state; - emit deviceStateChanged(state); } void AdapterItem::moveDeviceItem(Device::State state, DeviceItem *item) @@ -275,7 +272,7 @@ void AdapterItem::createDeviceItem(Device *device) void AdapterItem::updateView() { auto contentHeight = m_switchItem->height(); - contentHeight += (m_deviceLayout->count() - 3) * ItemHeight; + contentHeight += (m_deviceLayout->count() - 3) * ITEMHEIGHT; m_centralWidget->setFixedHeight(contentHeight); setFixedHeight(contentHeight); emit sizeChange(); diff --git a/plugins/bluetooth/componments/bluetoothconstants.h b/plugins/bluetooth/componments/bluetoothconstants.h new file mode 100644 index 000000000..f0f7515d1 --- /dev/null +++ b/plugins/bluetooth/componments/bluetoothconstants.h @@ -0,0 +1,11 @@ +#ifndef BLUETOOTHCONSTANTS_H +#define BLUETOOTHCONSTANTS_H + +#define POPUPWIDTH (200) +#define ITEMHEIGHT (30) +#define CONTROLHEIGHT (35) +#define LIGHTSUFFIX ("_dark.svg") +#define DARKSUFFIX (".svg") +#define MARGIN (12) + +#endif // BLUETOOTHCONSTANTS_H diff --git a/plugins/bluetooth/componments/deviceitem.cpp b/plugins/bluetooth/componments/deviceitem.cpp index 4ccf8e274..2d6f39ebd 100644 --- a/plugins/bluetooth/componments/deviceitem.cpp +++ b/plugins/bluetooth/componments/deviceitem.cpp @@ -21,6 +21,8 @@ */ #include "deviceitem.h" +#include "constants.h" +#include "bluetoothconstants.h" #include #include @@ -30,9 +32,6 @@ DGUI_USE_NAMESPACE -extern const int ItemHeight = 30; -const QString LightSuffix = "_dark.svg"; -const QString DarkSuffix = ".svg"; extern void initFontColor(QWidget *widget); DeviceItem::DeviceItem(const QString &title, QWidget *parent) @@ -42,25 +41,19 @@ DeviceItem::DeviceItem(const QString &title, QWidget *parent) , m_loadingStat(new DSpinner) , m_line(new HorizontalSeparator(this)) { - setFixedHeight(ItemHeight); + setFixedHeight(ITEMHEIGHT); auto themeChanged = [&](DApplicationHelper::ColorType themeType){ switch (themeType) { case DApplicationHelper::UnknownType: - case DApplicationHelper::LightType: m_statSuffix = LightSuffix; break; - case DApplicationHelper::DarkType: m_statSuffix = DarkSuffix; break; + case DApplicationHelper::LightType: m_statSuffix = LIGHTSUFFIX; break; + case DApplicationHelper::DarkType: m_statSuffix = DARKSUFFIX; break; } m_state->setPixmap(QPixmap(":/select" + m_statSuffix)); }; themeChanged(DApplicationHelper::instance()->themeType()); - auto strTitle = title; - m_title->setText(strTitle); - QFontMetrics fontMetrics(m_title->font()); - if(fontMetrics.width(strTitle) > m_title->width()) - { - strTitle = QFontMetrics(m_title->font()).elidedText(strTitle, Qt::ElideRight, m_title->width()); - } + QString strTitle = QFontMetrics(m_title->font()).elidedText(title, Qt::ElideRight, POPUPWIDTH - MARGIN * 2 - PLUGIN_ICON_MIN_SIZE - 3); m_title->setText(strTitle); initFontColor(m_title); @@ -76,12 +69,12 @@ DeviceItem::DeviceItem(const QString &title, QWidget *parent) auto itemLayout = new QHBoxLayout(this); itemLayout->setMargin(0); itemLayout->setSpacing(0); - itemLayout->addSpacing(12); + itemLayout->addSpacing(MARGIN); itemLayout->addWidget(m_title); itemLayout->addStretch(); itemLayout->addWidget(m_state); itemLayout->addWidget(m_loadingStat); - itemLayout->addSpacing(12); + itemLayout->addSpacing(MARGIN); deviceLayout->addLayout(itemLayout); setLayout(deviceLayout); diff --git a/plugins/bluetooth/componments/switchitem.cpp b/plugins/bluetooth/componments/switchitem.cpp index fa2261b4d..5af739365 100644 --- a/plugins/bluetooth/componments/switchitem.cpp +++ b/plugins/bluetooth/componments/switchitem.cpp @@ -21,10 +21,11 @@ */ #include "switchitem.h" +#include "bluetoothconstants.h" +#include "bluetoothconstants.h" #include "QHBoxLayout" -extern const int ControlHeight = 35; extern void initFontColor(QWidget *widget); SwitchItem::SwitchItem(QWidget *parent) @@ -35,15 +36,15 @@ SwitchItem::SwitchItem(QWidget *parent) { initFontColor(m_title); - setFixedHeight(ControlHeight); + setFixedHeight(CONTROLHEIGHT); auto switchLayout = new QHBoxLayout(this); switchLayout->setSpacing(0); switchLayout->setMargin(0); - switchLayout->addSpacing(12); + switchLayout->addSpacing(MARGIN); switchLayout->addWidget(m_title); switchLayout->addStretch(); switchLayout->addWidget(m_switchBtn); - switchLayout->addSpacing(12); + switchLayout->addSpacing(MARGIN); setLayout(switchLayout); connect(m_switchBtn, &DSwitchButton::toggled, [&](bool change) {