fix(bluetooth):popups display truncated

task20793 【TR4】【桌面专业版】【SP1】【华为】【Kunpeng920】【uos-20-pangu-daliy-20200424-build46】【任务栏】任务栏蓝牙显示截断

(cherry picked from commit 619ba29a258a215868a05e83f543f8a030f08d1d)
This commit is contained in:
zhaolong 2020-05-07 16:28:16 +08:00 committed by fpc_diesel
parent 1b6db122f3
commit 4303f90d75
7 changed files with 64 additions and 37 deletions

View File

@ -30,6 +30,7 @@
#include "componments/bluetoothconstants.h"
#include <DApplicationHelper>
#include <DDBusSender>
#include <QLabel>
#include <QVBoxLayout>
@ -60,6 +61,7 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
, m_appletName(new QLabel(this))
, m_centralWidget(new QWidget)
, m_centrealLayout(new QVBoxLayout)
, m_openControlCenter(new MenueItem(this))
, m_adaptersManager(new AdaptersManager(this))
{
m_line->setVisible(false);
@ -72,6 +74,11 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
initFontColor(m_appletName);
m_appletName->setVisible(false);
m_openControlCenter->setText(tr("Bluetooth settings"));
initFontColor(m_openControlCenter);
m_openControlCenter->setFixedHeight(ITEMHEIGHT);
m_openControlCenter->setVisible(false);
auto appletNameLayout = new QHBoxLayout;
appletNameLayout->setMargin(0);
appletNameLayout->setSpacing(0);
@ -79,10 +86,19 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
appletNameLayout->addWidget(m_appletName);
appletNameLayout->addStretch();
m_menueLayout = new QHBoxLayout;
m_menueLayout->setMargin(0);
m_menueLayout->setSpacing(0);
m_menueLayout->addSpacing(MARGIN);
m_adapterLayout = new QVBoxLayout;
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_menueLayout);
m_centralWidget->setLayout(m_centrealLayout);
m_centralWidget->setFixedWidth(POPUPWIDTH);
m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
@ -97,6 +113,15 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
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)
@ -155,6 +180,7 @@ void BluetoothApplet::onDeviceStateChanged()
}
emit deviceStateChanged(deviceState);
updateView();
}
void BluetoothApplet::addAdapter(Adapter *adapter)
@ -169,7 +195,7 @@ void BluetoothApplet::addAdapter(Adapter *adapter)
QString adapterId = adapter->id();
auto adatpterItem = new AdapterItem(m_adaptersManager, adapter, this);
m_adapterItems[adapterId] = adatpterItem;
m_centrealLayout->addWidget(adatpterItem);
m_adapterLayout->addWidget(adatpterItem);
getDevieInitState(adatpterItem);
connect(adatpterItem, &AdapterItem::deviceStateChanged, this, &BluetoothApplet::onDeviceStateChanged);
@ -185,7 +211,7 @@ void BluetoothApplet::removeAdapter(Adapter *adapter)
QString adapterId = adapter->id();
AdapterItem *adapterItem = m_adapterItems.value(adapterId);
if (adapterItem) {
m_centrealLayout->removeWidget(adapterItem);
m_adapterLayout->removeWidget(adapterItem);
delete adapterItem;
m_adapterItems.remove(adapterId);
updateView();
@ -199,14 +225,24 @@ void BluetoothApplet::updateView()
{
int contentHeight = 0;
int itemCount = 0;
bool isAdapterConnected = true;
for (AdapterItem *adapterItem : m_adapterItems) {
if (adapterItem) {
contentHeight += adapterItem->viewHeight();
contentHeight += CONTROLHEIGHT;
if (adapterItem->isPowered())
itemCount += adapterItem->deviceCount();
if (adapterItem->connectedDevsName().size())
isAdapterConnected = false;
}
}
m_openControlCenter->setVisible(isAdapterConnected);
if (isAdapterConnected) {
m_menueLayout->addWidget(m_openControlCenter);
contentHeight += ITEMHEIGHT;
} else
m_menueLayout->removeWidget(m_openControlCenter);
int adaptersCnt = m_adapterItems.size();
if (adaptersCnt > 1) {
m_line->setVisible(true);

View File

@ -29,9 +29,11 @@
class QLabel;
class QVBoxLayout;
class QHBoxLayout;
class Adapter;
class AdapterItem;
class HorizontalSeparator;
class MenueItem;
class AdaptersManager;
class BluetoothApplet : public QScrollArea
{
@ -66,6 +68,9 @@ private:
QLabel *m_appletName;
QWidget *m_centralWidget;
QVBoxLayout *m_centrealLayout;
QVBoxLayout *m_adapterLayout;
QHBoxLayout *m_menueLayout;
MenueItem *m_openControlCenter;
AdaptersManager *m_adaptersManager;

View File

@ -187,11 +187,12 @@ void BluetoothItem::refreshTips()
}
case Device::StateAvailable: {
tipsText = tr("Connecting...");
return ;
}
break ;
case Device::StateUnavailable: {
tipsText = tr("Bluetooth");
} break;
}
break;
}
} else {
tipsText = tr("Bluetooth");

View File

@ -27,8 +27,6 @@
#include "adaptersmanager.h"
#include "bluetoothconstants.h"
#include <DDBusSender>
#include <QLabel>
#include <QVBoxLayout>
@ -39,7 +37,6 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid
, m_centralWidget(new QWidget(this))
, m_line(new HorizontalSeparator(this))
, m_deviceLayout(new QVBoxLayout)
, m_openControlCenter(new MenueItem(this))
, m_adaptersManager(adapterManager)
, m_adapter(adapter)
, m_switchItem(new SwitchItem(this))
@ -48,16 +45,12 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid
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->setVisible(false);
m_switchItem->setTitle(adapter->name());
m_switchItem->setChecked(adapter->powered(),false);
m_deviceLayout->addWidget(m_switchItem);
m_deviceLayout->addWidget(m_line);
m_deviceLayout->addWidget(m_openControlCenter);
m_centralWidget->setFixedWidth(POPUPWIDTH);
m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
m_centralWidget->setLayout(m_deviceLayout);
@ -103,15 +96,6 @@ AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWid
connect(adapter, &Adapter::poweredChanged, m_switchItem, [=](const bool powered){
m_switchItem->setChecked(powered, false);
});
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();
});
showDevices(adapter->powered());
}
@ -131,11 +115,6 @@ bool AdapterItem::isPowered()
return m_switchItem->checkState();
}
int AdapterItem::viewHeight()
{
return m_openControlCenter->isVisible() ? CONTROLHEIGHT + ITEMHEIGHT : CONTROLHEIGHT;
}
QStringList AdapterItem::connectedDevsName()
{
QStringList devsName;
@ -293,7 +272,7 @@ void AdapterItem::createDeviceItem(Device *device)
void AdapterItem::updateView()
{
int contentHeight = m_switchItem->height();
contentHeight += (m_deviceLayout->count() - 3) * ITEMHEIGHT;
contentHeight += (m_deviceLayout->count() - 2) * ITEMHEIGHT;
m_centralWidget->setFixedHeight(contentHeight);
setFixedHeight(contentHeight);
emit sizeChange();
@ -317,8 +296,6 @@ void AdapterItem::showDevices(bool powered)
deviceItem->setVisible(powered);
}
int itemCount = m_deviceItems.size();
m_line->setVisible(powered);
m_openControlCenter->setVisible(!itemCount);
updateView();
}

View File

@ -43,7 +43,6 @@ public:
int deviceCount();
void setPowered(bool powered);
bool isPowered();
int viewHeight();
inline Device::State initDeviceState() { return m_initDeviceState; }
inline Device::State currentDeviceState() { return m_currentDeviceState; }
QStringList connectedDevsName();
@ -71,7 +70,6 @@ private:
QWidget *m_centralWidget;
HorizontalSeparator *m_line;
QVBoxLayout *m_deviceLayout;
MenueItem *m_openControlCenter;
AdaptersManager *m_adaptersManager;

View File

@ -9,6 +9,7 @@ TipsWidget::TipsWidget(QWidget *parent) : QFrame(parent)
void TipsWidget::setText(const QString &text)
{
m_type = TipsWidget::SingleLine;
m_text = text;
setFixedSize(fontMetrics().width(text) + 6, fontMetrics().height());
@ -18,6 +19,7 @@ void TipsWidget::setText(const QString &text)
void TipsWidget::setTextList(const QStringList &textList)
{
m_type = TipsWidget::MultiLine;
m_textList = textList;
int maxLength = 0;
@ -43,12 +45,13 @@ void TipsWidget::paintEvent(QPaintEvent *event)
int fontHeight = fontMetrics().height();
option.setAlignment(Qt::AlignCenter);
if (!m_text.isEmpty() && m_textList.isEmpty()) {
switch (m_type) {
case SingleLine: {
painter.drawText(rect(), m_text, option);
}
int y = 0;
if (m_text.isEmpty() && !m_textList.isEmpty()) {
break;
case MultiLine: {
int y = 0;
if (m_textList.size() != 1)
option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
for (QString text : m_textList) {
@ -56,5 +59,6 @@ void TipsWidget::paintEvent(QPaintEvent *event)
y += fontHeight;
}
}
break;
}
}

View File

@ -6,6 +6,11 @@
class TipsWidget : public QFrame
{
Q_OBJECT
enum ShowType
{
SingleLine,
MultiLine
};
public:
explicit TipsWidget(QWidget *parent = nullptr);
@ -21,6 +26,7 @@ private:
QString m_text;
QStringList m_textList;
int m_width;
ShowType m_type;
};
#endif // TIPSWIDGET_H