fix(bluetooth):the device name is only half displayed

task20946 任务栏蓝牙插件,蓝牙名称只能显示为界面宽度的一半长度

(cherry picked from commit 1757dd92947a9805876b0bd4a168444e85fc23c0)
This commit is contained in:
zhaolong 2020-04-29 14:15:37 +08:00 committed by fpc_diesel
parent 3549da8f6e
commit a4113538be
7 changed files with 80 additions and 65 deletions

View File

@ -27,14 +27,11 @@
#include "componments/switchitem.h" #include "componments/switchitem.h"
#include "componments/adaptersmanager.h" #include "componments/adaptersmanager.h"
#include "componments/adapteritem.h" #include "componments/adapteritem.h"
#include "componments/bluetoothconstants.h"
#include <DApplicationHelper> #include <DApplicationHelper>
DGUI_USE_NAMESPACE DGUI_USE_NAMESPACE
extern int ControlHeight;
extern int ItemHeight;
const int Width = 200;
extern void initFontColor(QWidget *widget) extern void initFontColor(QWidget *widget)
{ {
if (!widget) if (!widget)
@ -74,7 +71,7 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
auto appletNameLayout = new QHBoxLayout; auto appletNameLayout = new QHBoxLayout;
appletNameLayout->setMargin(0); appletNameLayout->setMargin(0);
appletNameLayout->setSpacing(0); appletNameLayout->setSpacing(0);
appletNameLayout->addSpacing(12); appletNameLayout->addSpacing(MARGIN);
appletNameLayout->addWidget(m_appletName); appletNameLayout->addWidget(m_appletName);
appletNameLayout->addStretch(); appletNameLayout->addStretch();
@ -83,10 +80,10 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
m_centrealLayout->addLayout(appletNameLayout); m_centrealLayout->addLayout(appletNameLayout);
m_centrealLayout->addWidget(m_line); m_centrealLayout->addWidget(m_line);
m_centralWidget->setLayout(m_centrealLayout); m_centralWidget->setLayout(m_centrealLayout);
m_centralWidget->setFixedWidth(Width); m_centralWidget->setFixedWidth(POPUPWIDTH);
m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
setFixedWidth(Width); setFixedWidth(POPUPWIDTH);
setWidget(m_centralWidget); setWidget(m_centralWidget);
setFrameShape(QFrame::NoFrame); setFrameShape(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -116,23 +113,6 @@ bool BluetoothApplet::hasAadapter()
return m_adaptersManager->adaptersCount(); 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) void BluetoothApplet::onPowerChanged(bool state)
{ {
Q_UNUSED(state) Q_UNUSED(state)
@ -146,10 +126,8 @@ void BluetoothApplet::onPowerChanged(bool state)
emit powerChanged(powerState); emit powerChanged(powerState);
} }
void BluetoothApplet::onDeviceStateChanged(const Device::State state) void BluetoothApplet::onDeviceStateChanged()
{ {
Q_UNUSED(state)
Device::State deviceState = Device::StateUnavailable; Device::State deviceState = Device::StateUnavailable;
for (auto adapterItem : m_adapterItems) { for (auto adapterItem : m_adapterItems) {
if (Device::StateAvailable == adapterItem->currentDeviceState()) { if (Device::StateAvailable == adapterItem->currentDeviceState()) {
@ -178,6 +156,7 @@ void BluetoothApplet::addAdapter(Adapter *adapter)
auto adatpterItem = new AdapterItem(m_adaptersManager, adapter, this); auto adatpterItem = new AdapterItem(m_adaptersManager, adapter, this);
m_adapterItems[adapterId] = adatpterItem; m_adapterItems[adapterId] = adatpterItem;
m_centrealLayout->addWidget(adatpterItem); m_centrealLayout->addWidget(adatpterItem);
getDevieInitState(adatpterItem);
connect(adatpterItem, &AdapterItem::deviceStateChanged, this, &BluetoothApplet::onDeviceStateChanged); connect(adatpterItem, &AdapterItem::deviceStateChanged, this, &BluetoothApplet::onDeviceStateChanged);
connect(adatpterItem, &AdapterItem::powerChanged, this, &BluetoothApplet::onPowerChanged); connect(adatpterItem, &AdapterItem::powerChanged, this, &BluetoothApplet::onPowerChanged);
@ -226,14 +205,50 @@ void BluetoothApplet::updateView()
contentHeight += m_appletName->height(); contentHeight += m_appletName->height();
if (itemCount <= 10) { if (itemCount <= 10) {
contentHeight += itemCount * ItemHeight; contentHeight += itemCount * ITEMHEIGHT;
m_centralWidget->setFixedHeight(contentHeight); m_centralWidget->setFixedHeight(contentHeight);
setFixedHeight(contentHeight); setFixedHeight(contentHeight);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
} else { } else {
contentHeight += itemCount * ItemHeight; contentHeight += itemCount * ITEMHEIGHT;
m_centralWidget->setFixedHeight(contentHeight); m_centralWidget->setFixedHeight(contentHeight);
setFixedHeight(10 * ItemHeight); setFixedHeight(10 * ITEMHEIGHT);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 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;
}
}

View File

@ -41,7 +41,6 @@ public:
void setAdapterPowered(bool powered); void setAdapterPowered(bool powered);
bool poweredInitState(); bool poweredInitState();
bool hasAadapter(); bool hasAadapter();
Device::State initDeviceState();
public slots : public slots :
void addAdapter(Adapter *constadapter); void addAdapter(Adapter *constadapter);
@ -55,10 +54,11 @@ signals:
private slots: private slots:
void onPowerChanged(bool state); void onPowerChanged(bool state);
void onDeviceStateChanged(const Device::State state); void onDeviceStateChanged();
private: private:
void updateView(); void updateView();
void getDevieInitState(AdapterItem *item);
private: private:
HorizontalSeparator *m_line; HorizontalSeparator *m_line;

View File

@ -47,8 +47,6 @@ BluetoothItem::BluetoothItem(QWidget *parent)
m_applet->setVisible(false); m_applet->setVisible(false);
m_adapterPowered = m_applet->poweredInitState(); m_adapterPowered = m_applet->poweredInitState();
m_devState = m_applet->initDeviceState();
connect(m_timer, &QTimer::timeout, this, &BluetoothItem::refreshIcon); connect(m_timer, &QTimer::timeout, this, &BluetoothItem::refreshIcon);
connect(m_applet, &BluetoothApplet::powerChanged, [&](bool powered) { connect(m_applet, &BluetoothApplet::powerChanged, [&](bool powered) {
m_adapterPowered = powered; m_adapterPowered = powered;

View File

@ -25,12 +25,10 @@
#include "deviceitem.h" #include "deviceitem.h"
#include "adapter.h" #include "adapter.h"
#include "adaptersmanager.h" #include "adaptersmanager.h"
#include "bluetoothconstants.h"
#include <DDBusSender> #include <DDBusSender>
extern const int ItemHeight;
extern const int ControlHeight;
const int Width = 200;
extern void initFontColor(QWidget *widget); extern void initFontColor(QWidget *widget);
AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWidget *parent) AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWidget *parent)
@ -43,13 +41,13 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid
, m_adapter(adapter) , m_adapter(adapter)
, m_switchItem(new SwitchItem(this)) , m_switchItem(new SwitchItem(this))
{ {
m_centralWidget->setFixedWidth(Width); m_centralWidget->setFixedWidth(POPUPWIDTH);
m_line->setVisible(true); m_line->setVisible(true);
m_deviceLayout->setMargin(0); m_deviceLayout->setMargin(0);
m_deviceLayout->setSpacing(0); m_deviceLayout->setSpacing(0);
m_openControlCenter->setText(tr("Bluetooth settings")); m_openControlCenter->setText(tr("Bluetooth settings"));
initFontColor(m_openControlCenter); initFontColor(m_openControlCenter);
m_openControlCenter->setFixedHeight(ItemHeight); m_openControlCenter->setFixedHeight(ITEMHEIGHT);
m_openControlCenter->setVisible(false); m_openControlCenter->setVisible(false);
m_switchItem->setTitle(adapter->name()); m_switchItem->setTitle(adapter->name());
m_switchItem->setChecked(adapter->powered()); 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_switchItem);
m_deviceLayout->addWidget(m_line); m_deviceLayout->addWidget(m_line);
m_deviceLayout->addWidget(m_openControlCenter); m_deviceLayout->addWidget(m_openControlCenter);
m_centralWidget->setFixedWidth(Width); m_centralWidget->setFixedWidth(POPUPWIDTH);
m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
m_centralWidget->setLayout(m_deviceLayout); m_centralWidget->setLayout(m_deviceLayout);
setFixedWidth(Width); setFixedWidth(POPUPWIDTH);
setWidget(m_centralWidget); setWidget(m_centralWidget);
setFrameShape(QFrame::NoFrame); setFrameShape(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -126,7 +124,7 @@ bool AdapterItem::isPowered()
int AdapterItem::viewHeight() int AdapterItem::viewHeight()
{ {
return m_openControlCenter->isVisible() ? ControlHeight + ItemHeight : ControlHeight; return m_openControlCenter->isVisible() ? CONTROLHEIGHT + ITEMHEIGHT : CONTROLHEIGHT;
} }
void AdapterItem::deviceItemPaired(const bool paired) void AdapterItem::deviceItemPaired(const bool paired)
@ -223,10 +221,9 @@ void AdapterItem::deviceChangeState(const Device::State state)
break; break;
} }
} }
m_currentDeviceState = state;
emit deviceStateChanged(state);
} }
m_currentDeviceState = state;
emit deviceStateChanged(state);
} }
void AdapterItem::moveDeviceItem(Device::State state, DeviceItem *item) void AdapterItem::moveDeviceItem(Device::State state, DeviceItem *item)
@ -275,7 +272,7 @@ void AdapterItem::createDeviceItem(Device *device)
void AdapterItem::updateView() void AdapterItem::updateView()
{ {
auto contentHeight = m_switchItem->height(); auto contentHeight = m_switchItem->height();
contentHeight += (m_deviceLayout->count() - 3) * ItemHeight; contentHeight += (m_deviceLayout->count() - 3) * ITEMHEIGHT;
m_centralWidget->setFixedHeight(contentHeight); m_centralWidget->setFixedHeight(contentHeight);
setFixedHeight(contentHeight); setFixedHeight(contentHeight);
emit sizeChange(); emit sizeChange();

View File

@ -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

View File

@ -21,6 +21,8 @@
*/ */
#include "deviceitem.h" #include "deviceitem.h"
#include "constants.h"
#include "bluetoothconstants.h"
#include <DApplicationHelper> #include <DApplicationHelper>
#include <DStyle> #include <DStyle>
@ -30,9 +32,6 @@
DGUI_USE_NAMESPACE DGUI_USE_NAMESPACE
extern const int ItemHeight = 30;
const QString LightSuffix = "_dark.svg";
const QString DarkSuffix = ".svg";
extern void initFontColor(QWidget *widget); extern void initFontColor(QWidget *widget);
DeviceItem::DeviceItem(const QString &title, QWidget *parent) DeviceItem::DeviceItem(const QString &title, QWidget *parent)
@ -42,25 +41,19 @@ DeviceItem::DeviceItem(const QString &title, QWidget *parent)
, m_loadingStat(new DSpinner) , m_loadingStat(new DSpinner)
, m_line(new HorizontalSeparator(this)) , m_line(new HorizontalSeparator(this))
{ {
setFixedHeight(ItemHeight); setFixedHeight(ITEMHEIGHT);
auto themeChanged = [&](DApplicationHelper::ColorType themeType){ auto themeChanged = [&](DApplicationHelper::ColorType themeType){
switch (themeType) { switch (themeType) {
case DApplicationHelper::UnknownType: case DApplicationHelper::UnknownType:
case DApplicationHelper::LightType: m_statSuffix = LightSuffix; break; case DApplicationHelper::LightType: m_statSuffix = LIGHTSUFFIX; break;
case DApplicationHelper::DarkType: m_statSuffix = DarkSuffix; break; case DApplicationHelper::DarkType: m_statSuffix = DARKSUFFIX; break;
} }
m_state->setPixmap(QPixmap(":/select" + m_statSuffix)); m_state->setPixmap(QPixmap(":/select" + m_statSuffix));
}; };
themeChanged(DApplicationHelper::instance()->themeType()); themeChanged(DApplicationHelper::instance()->themeType());
auto strTitle = title; QString strTitle = QFontMetrics(m_title->font()).elidedText(title, Qt::ElideRight, POPUPWIDTH - MARGIN * 2 - PLUGIN_ICON_MIN_SIZE - 3);
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());
}
m_title->setText(strTitle); m_title->setText(strTitle);
initFontColor(m_title); initFontColor(m_title);
@ -76,12 +69,12 @@ DeviceItem::DeviceItem(const QString &title, QWidget *parent)
auto itemLayout = new QHBoxLayout(this); auto itemLayout = new QHBoxLayout(this);
itemLayout->setMargin(0); itemLayout->setMargin(0);
itemLayout->setSpacing(0); itemLayout->setSpacing(0);
itemLayout->addSpacing(12); itemLayout->addSpacing(MARGIN);
itemLayout->addWidget(m_title); itemLayout->addWidget(m_title);
itemLayout->addStretch(); itemLayout->addStretch();
itemLayout->addWidget(m_state); itemLayout->addWidget(m_state);
itemLayout->addWidget(m_loadingStat); itemLayout->addWidget(m_loadingStat);
itemLayout->addSpacing(12); itemLayout->addSpacing(MARGIN);
deviceLayout->addLayout(itemLayout); deviceLayout->addLayout(itemLayout);
setLayout(deviceLayout); setLayout(deviceLayout);

View File

@ -21,10 +21,11 @@
*/ */
#include "switchitem.h" #include "switchitem.h"
#include "bluetoothconstants.h"
#include "bluetoothconstants.h"
#include "QHBoxLayout" #include "QHBoxLayout"
extern const int ControlHeight = 35;
extern void initFontColor(QWidget *widget); extern void initFontColor(QWidget *widget);
SwitchItem::SwitchItem(QWidget *parent) SwitchItem::SwitchItem(QWidget *parent)
@ -35,15 +36,15 @@ SwitchItem::SwitchItem(QWidget *parent)
{ {
initFontColor(m_title); initFontColor(m_title);
setFixedHeight(ControlHeight); setFixedHeight(CONTROLHEIGHT);
auto switchLayout = new QHBoxLayout(this); auto switchLayout = new QHBoxLayout(this);
switchLayout->setSpacing(0); switchLayout->setSpacing(0);
switchLayout->setMargin(0); switchLayout->setMargin(0);
switchLayout->addSpacing(12); switchLayout->addSpacing(MARGIN);
switchLayout->addWidget(m_title); switchLayout->addWidget(m_title);
switchLayout->addStretch(); switchLayout->addStretch();
switchLayout->addWidget(m_switchBtn); switchLayout->addWidget(m_switchBtn);
switchLayout->addSpacing(12); switchLayout->addSpacing(MARGIN);
setLayout(switchLayout); setLayout(switchLayout);
connect(m_switchBtn, &DSwitchButton::toggled, [&](bool change) { connect(m_switchBtn, &DSwitchButton::toggled, [&](bool change) {