From 1b6db122f385789b5b693dc931503d7b06d3d3c3 Mon Sep 17 00:00:00 2001 From: zhaolong Date: Wed, 6 May 2020 18:44:11 +0800 Subject: [PATCH] fix(bluetooth):popup and tooltips display are abnormal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bug24279 【TR4】【桌面专业版】【SP1】【华为】【Kunpeng920】【uos-20-pangu-daliy-20200505-build48】【任务栏-蓝牙】任务栏蓝牙tooltips显示异常,功能无法使用 (cherry picked from commit 882ad1ee7704dfc824019a391a3d35efd49e0e23) # Conflicts: # translations/dde-dock.ts # widgets/tipswidget.cpp # widgets/tipswidget.h --- plugins/bluetooth/bluetoothapplet.cpp | 13 +++++- plugins/bluetooth/bluetoothapplet.h | 1 + plugins/bluetooth/bluetoothitem.cpp | 42 +++++++++++++++++-- plugins/bluetooth/bluetoothitem.h | 5 ++- plugins/bluetooth/bluetoothplugin.cpp | 14 +++---- plugins/bluetooth/bluetoothplugin.h | 2 +- plugins/bluetooth/componments/adapteritem.cpp | 18 +++++++- plugins/bluetooth/componments/adapteritem.h | 1 + plugins/bluetooth/componments/deviceitem.h | 1 + plugins/bluetooth/componments/switchitem.cpp | 7 ++-- plugins/bluetooth/componments/switchitem.h | 1 + translations/dde-dock.ts | 14 ++++++- widgets/tipswidget.cpp | 34 ++++++++++++--- widgets/tipswidget.h | 5 ++- 14 files changed, 132 insertions(+), 26 deletions(-) diff --git a/plugins/bluetooth/bluetoothapplet.cpp b/plugins/bluetooth/bluetoothapplet.cpp index dd9a8af53..2b8a11d8c 100644 --- a/plugins/bluetooth/bluetoothapplet.cpp +++ b/plugins/bluetooth/bluetoothapplet.cpp @@ -117,6 +117,17 @@ 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; @@ -210,7 +221,7 @@ void BluetoothApplet::updateView() contentHeight += itemCount * ITEMHEIGHT; m_centralWidget->setFixedHeight(contentHeight); - setFixedHeight(qMin(10,itemCount) * ITEMHEIGHT); + setFixedHeight(itemCount <= 10 ? contentHeight : 10 * ITEMHEIGHT); setVerticalScrollBarPolicy(itemCount <= 10 ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAlwaysOn); } diff --git a/plugins/bluetooth/bluetoothapplet.h b/plugins/bluetooth/bluetoothapplet.h index 065bc58c0..67897881b 100644 --- a/plugins/bluetooth/bluetoothapplet.h +++ b/plugins/bluetooth/bluetoothapplet.h @@ -41,6 +41,7 @@ public: void setAdapterPowered(bool powered); bool poweredInitState(); bool hasAadapter(); + QStringList connectedDevsName(); public slots : void addAdapter(Adapter *adapter); diff --git a/plugins/bluetooth/bluetoothitem.cpp b/plugins/bluetooth/bluetoothitem.cpp index 8d4b01e54..47b1e79fc 100644 --- a/plugins/bluetooth/bluetoothitem.cpp +++ b/plugins/bluetooth/bluetoothitem.cpp @@ -41,6 +41,7 @@ DGUI_USE_NAMESPACE BluetoothItem::BluetoothItem(QWidget *parent) : QWidget(parent) + , m_tipsLabel(new TipsWidget(this)) , m_applet(new BluetoothApplet(this)) , m_timer(new QTimer(this)) { @@ -61,9 +62,11 @@ BluetoothItem::BluetoothItem(QWidget *parent) connect(m_applet, SIGNAL(justHasAdapter()), this, SIGNAL(justHasAdapter())); } -//QWidget *BluetoothItem::tipsWidget() -//{ -//} +QWidget *BluetoothItem::tipsWidget() +{ + refreshTips(); + return m_tipsLabel; +} QWidget *BluetoothItem::popupApplet() { @@ -165,6 +168,39 @@ void BluetoothItem::refreshIcon() update(); } +void BluetoothItem::refreshTips() +{ + if (!m_applet) + return; + + QString tipsText; + + if (m_adapterPowered) { + switch (m_devState) { + case Device::StateConnected: { + QStringList textList; + for (QString devName : m_applet->connectedDevsName()) { + textList << tr("%1 connected").arg(devName); + } + m_tipsLabel->setTextList(textList); + return; + } + case Device::StateAvailable: { + tipsText = tr("Connecting..."); + return ; + } + case Device::StateUnavailable: { + tipsText = tr("Bluetooth"); + } break; + } + } else { + tipsText = tr("Bluetooth"); + } + + m_tipsLabel->setText(tipsText); +} + + bool BluetoothItem::hasAdapter() { return m_applet->hasAadapter(); diff --git a/plugins/bluetooth/bluetoothitem.h b/plugins/bluetooth/bluetoothitem.h index 9ac31d022..a82745afd 100644 --- a/plugins/bluetooth/bluetoothitem.h +++ b/plugins/bluetooth/bluetoothitem.h @@ -38,13 +38,14 @@ class BluetoothItem : public QWidget public: explicit BluetoothItem(QWidget *parent = nullptr); -// QWidget *tipsWidget(); + QWidget *tipsWidget(); QWidget *popupApplet(); const QString contextMenu() const; void invokeMenuItem(const QString menuId, const bool checked); void refreshIcon(); + void refreshTips(); bool hasAdapter(); @@ -58,7 +59,7 @@ signals: void justHasAdapter(); private: -// TipsWidget *m_tipsLabel; + TipsWidget *m_tipsLabel; BluetoothApplet *m_applet; QPixmap m_iconPixmap; diff --git a/plugins/bluetooth/bluetoothplugin.cpp b/plugins/bluetooth/bluetoothplugin.cpp index 0ab3fd2b9..61d9410ce 100644 --- a/plugins/bluetooth/bluetoothplugin.cpp +++ b/plugins/bluetooth/bluetoothplugin.cpp @@ -85,14 +85,14 @@ QWidget *BluetoothPlugin::itemWidget(const QString &itemKey) return nullptr; } -//QWidget *BluetoothPlugin::itemTipsWidget(const QString &itemKey) -//{ -// if (itemKey == BLUETOOTH_KEY) { -// return m_bluetoothItem->tipsWidget(); -// } +QWidget *BluetoothPlugin::itemTipsWidget(const QString &itemKey) +{ + if (itemKey == BLUETOOTH_KEY) { + return m_bluetoothItem->tipsWidget(); + } -// return nullptr; -//} + return nullptr; +} QWidget *BluetoothPlugin::itemPopupApplet(const QString &itemKey) { diff --git a/plugins/bluetooth/bluetoothplugin.h b/plugins/bluetooth/bluetoothplugin.h index 7f0109dc8..ac3eaa13e 100644 --- a/plugins/bluetooth/bluetoothplugin.h +++ b/plugins/bluetooth/bluetoothplugin.h @@ -42,7 +42,7 @@ public: bool pluginIsAllowDisable() Q_DECL_OVERRIDE { return true; } bool pluginIsDisable() Q_DECL_OVERRIDE; QWidget *itemWidget(const QString &itemKey) Q_DECL_OVERRIDE; -// QWidget *itemTipsWidget(const QString &itemKey) Q_DECL_OVERRIDE; + QWidget *itemTipsWidget(const QString &itemKey) Q_DECL_OVERRIDE; QWidget *itemPopupApplet(const QString &itemKey) Q_DECL_OVERRIDE; const QString itemContextMenu(const QString &itemKey) Q_DECL_OVERRIDE; void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) Q_DECL_OVERRIDE; diff --git a/plugins/bluetooth/componments/adapteritem.cpp b/plugins/bluetooth/componments/adapteritem.cpp index 5781dab2a..5f7c5250d 100644 --- a/plugins/bluetooth/componments/adapteritem.cpp +++ b/plugins/bluetooth/componments/adapteritem.cpp @@ -93,11 +93,15 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid } connect(m_switchItem, &SwitchItem::checkedChanged, this, &AdapterItem::showAndConnect); + 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); + m_switchItem->setChecked(powered, false); }); connect(m_openControlCenter, &MenueItem::clicked, []{ DDBusSender() @@ -132,6 +136,18 @@ int AdapterItem::viewHeight() return m_openControlCenter->isVisible() ? CONTROLHEIGHT + ITEMHEIGHT : CONTROLHEIGHT; } +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(sender()); diff --git a/plugins/bluetooth/componments/adapteritem.h b/plugins/bluetooth/componments/adapteritem.h index d01ef3c40..2163b021a 100644 --- a/plugins/bluetooth/componments/adapteritem.h +++ b/plugins/bluetooth/componments/adapteritem.h @@ -46,6 +46,7 @@ public: int viewHeight(); inline Device::State initDeviceState() { return m_initDeviceState; } inline Device::State currentDeviceState() { return m_currentDeviceState; } + QStringList connectedDevsName(); signals: void deviceStateChanged(const Device::State state); diff --git a/plugins/bluetooth/componments/deviceitem.h b/plugins/bluetooth/componments/deviceitem.h index 7e5d595cb..650c29adc 100644 --- a/plugins/bluetooth/componments/deviceitem.h +++ b/plugins/bluetooth/componments/deviceitem.h @@ -40,6 +40,7 @@ public: bool operator <(const DeviceItem &item); inline void setTitle(const QString &name) { m_title->setText(name); } + inline QString title() { return m_title->text(); } void setDevice(Device *device); inline Device *device() { return m_device; } diff --git a/plugins/bluetooth/componments/switchitem.cpp b/plugins/bluetooth/componments/switchitem.cpp index 93be987a6..e7d1e9e89 100644 --- a/plugins/bluetooth/componments/switchitem.cpp +++ b/plugins/bluetooth/componments/switchitem.cpp @@ -59,17 +59,16 @@ SwitchItem::SwitchItem(QWidget *parent) void SwitchItem::setChecked(const bool checked,bool notify) { - if(!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); } - - m_checkState = checked; } void SwitchItem::setTitle(const QString &title) diff --git a/plugins/bluetooth/componments/switchitem.h b/plugins/bluetooth/componments/switchitem.h index 82eec4a2b..54d4d004b 100644 --- a/plugins/bluetooth/componments/switchitem.h +++ b/plugins/bluetooth/componments/switchitem.h @@ -42,6 +42,7 @@ public: signals: void checkedChanged(bool checked); + void justUpdateView(bool checked); private: QLabel *m_title; diff --git a/translations/dde-dock.ts b/translations/dde-dock.ts index ef173dab2..784bec1e7 100644 --- a/translations/dde-dock.ts +++ b/translations/dde-dock.ts @@ -11,7 +11,7 @@ AdapterItem - My Device + Bluetooth settings @@ -36,6 +36,18 @@ Bluetooth settings + + %1 connected + + + + Connecting... + + + + Bluetooth + + BluetoothPlugin diff --git a/widgets/tipswidget.cpp b/widgets/tipswidget.cpp index 87ad27503..ca5eb117d 100644 --- a/widgets/tipswidget.cpp +++ b/widgets/tipswidget.cpp @@ -16,21 +16,45 @@ void TipsWidget::setText(const QString &text) update(); } -void TipsWidget::refreshFont() +void TipsWidget::setTextList(const QStringList &textList) { - setFixedSize(fontMetrics().width(m_text) + 6, fontMetrics().height()); + m_textList = textList; + + int maxLength = 0; + int k = fontMetrics().height() * m_textList.size(); + setFixedHeight(k); + for (QString text : m_textList) { + int fontLength = fontMetrics().width(text) + 6; + maxLength = maxLength > fontLength ? maxLength : fontLength; + } + m_width = maxLength; + setFixedWidth(maxLength); + update(); } void TipsWidget::paintEvent(QPaintEvent *event) { QFrame::paintEvent(event); - refreshFont(); QPainter painter(this); painter.setPen(QPen(palette().brightText(), 1)); - QTextOption option; + int fontHeight = fontMetrics().height(); option.setAlignment(Qt::AlignCenter); - painter.drawText(rect(), m_text, option); + + if (!m_text.isEmpty() && m_textList.isEmpty()) { + painter.drawText(rect(), m_text, option); + } + + int y = 0; + if (m_text.isEmpty() && !m_textList.isEmpty()) { + if (m_textList.size() != 1) + option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter); + for (QString text : m_textList) { + painter.drawText(QRect(0, y, m_width, fontHeight), text, option); + y += fontHeight; + } + } + } diff --git a/widgets/tipswidget.h b/widgets/tipswidget.h index e2c951261..e4b4c0d27 100644 --- a/widgets/tipswidget.h +++ b/widgets/tipswidget.h @@ -10,14 +10,17 @@ public: explicit TipsWidget(QWidget *parent = nullptr); const QString& text(){return m_text;} + const QStringList &textList() { return m_textList; } void setText(const QString &text); - void refreshFont(); + void setTextList(const QStringList &textList); protected: void paintEvent(QPaintEvent *event) override; private: QString m_text; + QStringList m_textList; + int m_width; }; #endif // TIPSWIDGET_H