fix: 修复蓝牙列表高度错误的问题

高度计算错误导致

Log:
Change-Id: Ic168a905ef568560bfd9b065787a06b2c31ff700
This commit is contained in:
FanPengCheng 2021-06-10 18:11:12 +08:00
parent 365a7e63a2
commit 0db854c785
6 changed files with 31 additions and 19 deletions

View File

@ -36,6 +36,11 @@ HorizontalSeperator::HorizontalSeperator(QWidget *parent)
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
} }
QSize HorizontalSeperator::sizeHint() const
{
return QSize(QWidget::sizeHint().width(), 2);
}
void HorizontalSeperator::paintEvent(QPaintEvent *e) void HorizontalSeperator::paintEvent(QPaintEvent *e)
{ {
Q_UNUSED(e) Q_UNUSED(e)

View File

@ -31,8 +31,10 @@ class HorizontalSeperator : public QWidget
public: public:
explicit HorizontalSeperator(QWidget *parent = nullptr); explicit HorizontalSeperator(QWidget *parent = nullptr);
QSize sizeHint() const override;
protected: protected:
void paintEvent(QPaintEvent *e); void paintEvent(QPaintEvent *e) override;
}; };
#endif // HORIZONTALSEPERATOR_H #endif // HORIZONTALSEPERATOR_H

View File

@ -131,7 +131,6 @@ BluetoothAdapterItem::BluetoothAdapterItem(Adapter *adapter, QWidget *parent)
, m_bluetoothInter(new DBusBluetooth("com.deepin.daemon.Bluetooth", "/com/deepin/daemon/Bluetooth", QDBusConnection::sessionBus(), this)) , m_bluetoothInter(new DBusBluetooth("com.deepin.daemon.Bluetooth", "/com/deepin/daemon/Bluetooth", QDBusConnection::sessionBus(), this))
, m_showUnnamedDevices(false) , m_showUnnamedDevices(false)
, m_seperator(new HorizontalSeperator(this)) , m_seperator(new HorizontalSeperator(this))
, m_bottomSeperator(new HorizontalSeperator(this))
{ {
initData(); initData();
initUi(); initUi();
@ -181,6 +180,12 @@ void BluetoothAdapterItem::updateIconTheme(DGuiApplicationHelper::ColorType type
m_refreshBtn->setRotateIcon(":/wireless/resources/wireless/refresh.svg"); m_refreshBtn->setRotateIcon(":/wireless/resources/wireless/refresh.svg");
} }
QSize BluetoothAdapterItem::sizeHint() const
{
return QSize(ItemWidth, m_deviceListview->model()->rowCount() * DeviceItemHeight
+ (m_deviceListview->model()->rowCount() > 0 ? m_seperator->sizeHint().height() : 0));// 加上割线的高度
}
int BluetoothAdapterItem::currentDeviceCount() int BluetoothAdapterItem::currentDeviceCount()
{ {
return m_deviceItems.size(); return m_deviceItems.size();
@ -292,13 +297,10 @@ void BluetoothAdapterItem::initUi()
mainLayout->addWidget(m_adapterLabel); mainLayout->addWidget(m_adapterLabel);
mainLayout->addWidget(m_seperator); mainLayout->addWidget(m_seperator);
mainLayout->addWidget(m_deviceListview); mainLayout->addWidget(m_deviceListview);
mainLayout->addWidget(m_bottomSeperator);
m_seperator->setVisible(m_deviceListview->count() != 0); m_seperator->setVisible(m_deviceListview->count() != 0);
m_bottomSeperator->setVisible(m_deviceListview->count() != 0);
connect(m_deviceListview, &DListView::rowCountChanged, this, [ = ] { connect(m_deviceListview, &DListView::rowCountChanged, this, [ = ] {
m_seperator->setVisible(m_deviceListview->count() != 0); m_seperator->setVisible(m_deviceListview->count() != 0);
m_bottomSeperator->setVisible(m_deviceListview->count() != 0);
}); });
m_deviceListview->setItemDelegate(m_itemDelegate); m_deviceListview->setItemDelegate(m_itemDelegate);
@ -334,7 +336,6 @@ void BluetoothAdapterItem::initConnect()
m_refreshBtn->setVisible(state); m_refreshBtn->setVisible(state);
m_deviceListview->setVisible(state); m_deviceListview->setVisible(state);
m_seperator->setVisible(state); m_seperator->setVisible(state);
m_bottomSeperator->setVisible(state);
m_adapterStateBtn->setChecked(state); m_adapterStateBtn->setChecked(state);
m_adapterStateBtn->setEnabled(true); m_adapterStateBtn->setEnabled(true);
emit adapterPowerChanged(); emit adapterPowerChanged();
@ -345,7 +346,6 @@ void BluetoothAdapterItem::initConnect()
m_deviceModel->clear(); m_deviceModel->clear();
m_deviceListview->setVisible(false); m_deviceListview->setVisible(false);
m_seperator->setVisible(false); m_seperator->setVisible(false);
m_bottomSeperator->setVisible(false);
m_adapterStateBtn->setEnabled(false); m_adapterStateBtn->setEnabled(false);
m_refreshBtn->setVisible(state); m_refreshBtn->setVisible(state);
emit requestSetAdapterPower(m_adapter, state); emit requestSetAdapterPower(m_adapter, state);

View File

@ -113,6 +113,8 @@ public slots:
void onAdapterNameChanged(const QString name); void onAdapterNameChanged(const QString name);
void updateIconTheme(DGuiApplicationHelper::ColorType type); void updateIconTheme(DGuiApplicationHelper::ColorType type);
QSize sizeHint() const override;
signals: signals:
void adapterPowerChanged(); void adapterPowerChanged();
void requestSetAdapterPower(Adapter *adapter, bool state); void requestSetAdapterPower(Adapter *adapter, bool state);
@ -140,7 +142,6 @@ private:
QMap<QString, BluetoothDeviceItem *> m_deviceItems; QMap<QString, BluetoothDeviceItem *> m_deviceItems;
HorizontalSeperator *m_seperator; HorizontalSeperator *m_seperator;
HorizontalSeperator *m_bottomSeperator;
}; };
#endif // BLUETOOTHADAPTERITEM_H #endif // BLUETOOTHADAPTERITEM_H

View File

@ -26,6 +26,7 @@
#include "adaptersmanager.h" #include "adaptersmanager.h"
#include "adapter.h" #include "adapter.h"
#include "bluetoothadapteritem.h" #include "bluetoothadapteritem.h"
#include "util/horizontalseperator.h"
#include <DApplicationHelper> #include <DApplicationHelper>
#include <DDBusSender> #include <DDBusSender>
@ -98,6 +99,7 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
, m_settingLabel(new SettingLabel(tr("Bluetooth settings"), this)) , m_settingLabel(new SettingLabel(tr("Bluetooth settings"), this))
, m_mainLayout(new QVBoxLayout(this)) , m_mainLayout(new QVBoxLayout(this))
, m_contentLayout(new QVBoxLayout(m_contentWidget)) , m_contentLayout(new QVBoxLayout(m_contentWidget))
, m_seperator(new HorizontalSeperator(this))
{ {
initUi(); initUi();
initConnect(); initConnect();
@ -213,6 +215,7 @@ void BluetoothApplet::initUi()
m_contentWidget->setContentsMargins(0, 0, 0, 0); m_contentWidget->setContentsMargins(0, 0, 0, 0);
m_contentLayout->setMargin(0); m_contentLayout->setMargin(0);
m_contentLayout->setSpacing(0); m_contentLayout->setSpacing(0);
m_contentLayout->addWidget(m_seperator);
m_contentLayout->addWidget(m_settingLabel, 0, Qt::AlignBottom | Qt::AlignVCenter); m_contentLayout->addWidget(m_settingLabel, 0, Qt::AlignBottom | Qt::AlignVCenter);
m_scroarea = new QScrollArea(this); m_scroarea = new QScrollArea(this);
@ -271,19 +274,18 @@ void BluetoothApplet::updateIconTheme()
void BluetoothApplet::updateSize() void BluetoothApplet::updateSize()
{ {
int hetght = 0; int height = 0;
int count = 0; height += TitleSpace;
foreach (const auto item, m_adapterItems) { foreach (const auto item, m_adapterItems) {
hetght += TitleHeight + TitleSpace; height += TitleHeight;
if (item->adapter()->powered()) { height += item->sizeHint().height();
count += item->currentDeviceCount();
hetght += count * DeviceItemHeight;
}
} }
hetght += DeviceItemHeight; // 加上蓝牙设置选项的高度
int maxHeight = (TitleHeight + TitleSpace) + MaxDeviceCount * DeviceItemHeight; height += DeviceItemHeight;
hetght = hetght > maxHeight ? maxHeight : hetght;
setFixedSize(ItemWidth, hetght); static const int maxHeight = (TitleHeight + TitleSpace) + MaxDeviceCount * DeviceItemHeight;
setFixedSize(ItemWidth, qMin(maxHeight, height));
} }

View File

@ -36,6 +36,7 @@ class Device;
class Adapter; class Adapter;
class BluetoothAdapterItem; class BluetoothAdapterItem;
class AdaptersManager; class AdaptersManager;
class HorizontalSeperator;
DWIDGET_BEGIN_NAMESPACE DWIDGET_BEGIN_NAMESPACE
class DLabel; class DLabel;
@ -111,6 +112,7 @@ private:
SettingLabel *m_settingLabel; SettingLabel *m_settingLabel;
QVBoxLayout *m_mainLayout; QVBoxLayout *m_mainLayout;
QVBoxLayout *m_contentLayout; QVBoxLayout *m_contentLayout;
HorizontalSeperator *m_seperator;
QStringList m_connectDeviceName; QStringList m_connectDeviceName;
QMap<QString, BluetoothAdapterItem *> m_adapterItems; // 所有蓝牙适配器 QMap<QString, BluetoothAdapterItem *> m_adapterItems; // 所有蓝牙适配器