dde-dock/plugins/network/item/applet/devicecontrolwidget.cpp
FanPengCheng bf3063f150 fix: 修复蓝牙列表抖动的问题
只有一个蓝牙设备时,鼠标放上去滚动会抖动

Log:
Bug: https://pms.uniontech.com/zentao/bug-view-81649.html
Change-Id: I745bcca6b2b1b71c12c231db6af17172a551e009
2021-06-11 15:02:39 +08:00

132 lines
4.1 KiB
C++

/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: sbw <sbw@sbw.so>
*
* Maintainer: sbw <sbw@sbw.so>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "devicecontrolwidget.h"
#include "horizontalseperator.h"
#include "../widgets/tipswidget.h"
#include <DHiDPIHelper>
#include <DGuiApplicationHelper>
#include <QTimer>
#include <QHBoxLayout>
#include <QDebug>
#include <QEvent>
#include <QLabel>
DWIDGET_USE_NAMESPACE
DGUI_USE_NAMESPACE
extern const int ItemHeight = 30;
DeviceControlWidget::DeviceControlWidget(QWidget *parent)
: QWidget(parent)
, m_deviceName(new QLabel(this))
, m_switchBtn(new DSwitchButton(this))
, m_loadingIndicator(new DLoadingIndicator(this))
{
m_deviceName->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_deviceName->setForegroundRole(QPalette::BrightText);
const QPixmap pixmap = DHiDPIHelper::loadNxPixmap(":/wireless/resources/wireless/refresh.svg");
m_loadingIndicator->setLoading(false);
m_loadingIndicator->setSmooth(true);
m_loadingIndicator->setAniDuration(1000);
m_loadingIndicator->setAniEasingCurve(QEasingCurve::InOutCirc);
m_loadingIndicator->installEventFilter(this);
m_loadingIndicator->setFixedSize(pixmap.size() / devicePixelRatioF());
m_loadingIndicator->viewport()->setAutoFillBackground(false);
m_loadingIndicator->setFrameShape(QFrame::NoFrame);
refreshIcon();
QHBoxLayout *infoLayout = new QHBoxLayout;
infoLayout->setMargin(0);
infoLayout->setSpacing(0);
infoLayout->setContentsMargins(0, 0, 0, 0);
infoLayout->addWidget(m_deviceName);
infoLayout->addStretch();
infoLayout->addWidget(m_loadingIndicator);
infoLayout->addSpacing(4);
infoLayout->addWidget(m_switchBtn);
infoLayout->addSpacing(8);
QVBoxLayout *centralLayout = new QVBoxLayout;
centralLayout->addLayout(infoLayout);
centralLayout->setMargin(0);
centralLayout->setSpacing(0);
centralLayout->setContentsMargins(0, 0, 0, 0);
setLayout(centralLayout);
setFixedHeight(ItemHeight);
connect(m_switchBtn, &DSwitchButton::clicked, this, &DeviceControlWidget::enableButtonToggled);
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &DeviceControlWidget::refreshIcon);
}
void DeviceControlWidget::setDeviceName(const QString &name)
{
m_deviceName->setText(name);
}
void DeviceControlWidget::setDeviceEnabled(const bool enable)
{
m_switchBtn->blockSignals(true);
m_switchBtn->setChecked(enable);
m_loadingIndicator->setVisible(enable);
m_switchBtn->blockSignals(false);
}
bool DeviceControlWidget::eventFilter(QObject *watched, QEvent *event)
{
if (watched == m_loadingIndicator) {
if (event->type() == QEvent::MouseButtonPress) {
if (!m_loadingIndicator->loading()) {
refreshNetwork();
}
}
}
return QWidget::eventFilter(watched, event);
}
void DeviceControlWidget::refreshNetwork()
{
emit requestRefresh();
m_loadingIndicator->setLoading(true);
QTimer::singleShot(1000, this, [ = ] {
m_loadingIndicator->setLoading(false);
});
}
void DeviceControlWidget::refreshIcon()
{
QPixmap pixmap;
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
pixmap = DHiDPIHelper::loadNxPixmap(":/wireless/resources/wireless/refresh_dark.svg");
else
pixmap = DHiDPIHelper::loadNxPixmap(":/wireless/resources/wireless/refresh.svg");
m_loadingIndicator->setImageSource(pixmap);
}