feat: 优化插件代码

优化蓝牙跟网络插件代码

Log: 优化蓝牙跟网络插件代码,提取分割线类
Task: https://pms.uniontech.com/zentao/task-view-77196.html
Change-Id: Ic7213a7fd4be03f59193f6049658c2a44c5384b8
This commit is contained in:
yanghongwei 2021-05-21 17:11:12 +08:00
parent 68d97efca6
commit 1377e77448
17 changed files with 63 additions and 211 deletions

View File

@ -100,6 +100,7 @@ SET_LABEL_ACCESSIBLE(QLabel, m_w->objectName() == "notifications" ? m_w->objectN
SET_BUTTON_ACCESSIBLE(DIconButton, m_w->objectName().isEmpty() ? "imagebutton" : m_w->objectName())
SET_BUTTON_ACCESSIBLE(DSwitchButton, m_w->text().isEmpty() ? "switchbutton" : m_w->text())
SET_BUTTON_ACCESSIBLE(DesktopWidget, "desktopWidget");
SET_FORM_ACCESSIBLE(HorizontalSeperator, "HorizontalSeperator");
// 几个没什么用的标记,但为了提醒大家不要遗漏标记控件,还是不要去掉
SET_FORM_ACCESSIBLE(DBlurEffectWidget, "DBlurEffectWidget")
SET_FORM_ACCESSIBLE(DListView, "DListView")
@ -183,6 +184,7 @@ QAccessibleInterface *accessibleFactory(const QString &classname, QObject *objec
USE_ACCESSIBLE(classname, NetworkItem);
USE_ACCESSIBLE(classname, StateButton);
USE_ACCESSIBLE(classname, DeviceControlWidget);
USE_ACCESSIBLE(classname, HorizontalSeperator);
if (!interface && object->inherits("QWidget") && !ignoreLst.contains(classname)) {
QWidget *w = static_cast<QWidget *>(object);

View File

@ -21,30 +21,33 @@
#include "horizontalseperator.h"
#include <DApplicationHelper>
#include <QPainter>
/**
* @brief HorizontalSeperator::HorizontalSeperator 线,2
* @brief HorizontalSeperator::HorizontalSeperator 线,2
* @param parent
*/
HorizontalSeperator::HorizontalSeperator(QWidget *parent)
: QWidget(parent),
m_color(0, 0, 0, 0.1*255)
: QWidget(parent)
{
setFixedHeight(2);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
void HorizontalSeperator::setColor(const QColor color)
{
m_color = color;
update();
QPalette palette = this->palette();
palette.setColor(QPalette::Light, QColor(0, 0, 0, 0.1 * 255));
palette.setColor(QPalette::Dark, QColor(255, 255, 255, 0.1 * 255));
this->setPalette(palette);
}
void HorizontalSeperator::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
Q_UNUSED(e)
QPainter painter(this);
painter.fillRect(rect(), m_color);
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
painter.fillRect(rect(), palette().color(QPalette::Light));
else
painter.fillRect(rect(), palette().color(QPalette::Dark));
}

View File

@ -31,13 +31,8 @@ class HorizontalSeperator : public QWidget
public:
explicit HorizontalSeperator(QWidget *parent = nullptr);
void setColor(const QColor color);
protected:
void paintEvent(QPaintEvent *e);
private:
QColor m_color;
};
#endif // HORIZONTALSEPERATOR_H

View File

@ -6,7 +6,8 @@ project(${PLUGIN_NAME})
# Sources files
file(GLOB_RECURSE SRCS "*.h" "*.cpp" "../../widgets/*.h" "../../widgets/*.cpp"
"../../frame/util/imageutil.h" "../../frame/util/imageutil.cpp"
"../../frame/util/statebutton.h" "../../frame/util/statebutton.cpp")
"../../frame/util/statebutton.h" "../../frame/util/statebutton.cpp"
"../../frame/util/horizontalseperator.h" "../../frame/util/horizontalseperator.cpp")
find_package(PkgConfig REQUIRED)
find_package(Qt5Widgets REQUIRED)

View File

@ -24,9 +24,7 @@
#include "componments/adapter.h"
#include "bluetoothconstants.h"
#include "refreshbutton.h"
#include <QBoxLayout>
#include <QStandardItemModel>
#include "util/horizontalseperator.h"
#include <DFontSizeManager>
#include <DLabel>
@ -35,6 +33,9 @@
#include <DSpinner>
#include <DApplicationHelper>
#include <QBoxLayout>
#include <QStandardItemModel>
ItemDelegate::ItemDelegate(QAbstractItemView *parent)
: DStyledItemDelegate(parent)
{
@ -156,8 +157,9 @@ BluetoothAdapterItem::BluetoothAdapterItem(Adapter *adapter, QWidget *parent)
QDBusConnection::sessionBus(),
this))
, m_showUnnamedDevices(false)
, m_Separator(new HorizontalSeperator(this))
, m_seperator(new HorizontalSeperator(this))
, m_itemDelegate(new ItemDelegate(m_deviceListview))
, m_bottomSeperator(new HorizontalSeperator(this))
{
initData();
initUi();
@ -201,15 +203,11 @@ void BluetoothAdapterItem::onAdapterNameChanged(const QString name)
void BluetoothAdapterItem::updateIconTheme(DGuiApplicationHelper::ColorType type)
{
if (type == DGuiApplicationHelper::LightType) {
if (type == DGuiApplicationHelper::LightType)
m_refreshBtn->setRotateIcon(":/wireless/resources/wireless/refresh_dark.svg");
//浅色主题蓝牙界面控件分割线颜色
m_Separator->setColor(QColor(0, 0, 0, 0.1 * 255));
} else {
else
m_refreshBtn->setRotateIcon(":/wireless/resources/wireless/refresh.svg");
//深色主题蓝牙界面控件分割线颜色
m_Separator->setColor(QColor(255, 255, 255, 0.1 * 255));
}
setItemHoverColor();
}
@ -346,8 +344,9 @@ void BluetoothAdapterItem::initUi()
mainLayout->addWidget(m_adapterLabel);
mainLayout->addSpacing(2);
mainLayout->addWidget(m_Separator);
mainLayout->addWidget(m_seperator);
mainLayout->addWidget(m_deviceListview);
mainLayout->addWidget(m_bottomSeperator);
updateIconTheme(DGuiApplicationHelper::instance()->themeType());
if (m_adapter->discover()) {
@ -379,6 +378,7 @@ void BluetoothAdapterItem::initConnect()
initData();
m_refreshBtn->setVisible(state);
m_deviceListview->setVisible(state);
m_seperator->setVisible(state);
m_adapterStateBtn->setChecked(state);
m_adapterStateBtn->setEnabled(true);
emit adapterPowerChanged();
@ -388,6 +388,7 @@ void BluetoothAdapterItem::initConnect()
m_deviceItems.clear();
m_deviceModel->clear();
m_deviceListview->setVisible(false);
m_seperator->setVisible(false);
m_adapterStateBtn->setEnabled(false);
m_refreshBtn->setVisible(state);
emit requestSetAdapterPower(m_adapter, state);

View File

@ -49,6 +49,7 @@ class Adapter;
class SettingLabel;
class QStandardItemModel;
class RefreshButton;
class HorizontalSeperator;
const QString LightString = QString(":/light/buletooth_%1_light.svg");
const QString DarkString = QString(":/dark/buletooth_%1_dark.svg");
@ -145,7 +146,8 @@ private:
bool m_showUnnamedDevices;
QMap<QString, BluetoothDeviceItem *> m_deviceItems;
HorizontalSeperator *m_Separator;
HorizontalSeperator *m_seperator;
HorizontalSeperator *m_bottomSeperator;
};
#endif // BLUETOOTHADAPTERITEM_H

View File

@ -27,11 +27,6 @@
#include "adapter.h"
#include "bluetoothadapteritem.h"
#include <QString>
#include <QBoxLayout>
#include <QMouseEvent>
#include <QDebug>
#include <DApplicationHelper>
#include <DDBusSender>
#include <DLabel>
@ -39,35 +34,10 @@
#include <DScrollArea>
#include <DListView>
/**
* @brief HorizontalSeperator::HorizontalSeperator 线,2
* @param parent
*/
HorizontalSeperator::HorizontalSeperator(QWidget *parent)
: QWidget(parent),
m_color(0, 0, 0, 0.1*255)
{
setFixedHeight(2);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
/**
* @brief HorizontalSeperator::setColor 线
* @param color
*/
void HorizontalSeperator::setColor(const QColor color)
{
m_color = color;
update();
}
void HorizontalSeperator::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
QPainter painter(this);
painter.fillRect(rect(), m_color);
}
#include <QString>
#include <QBoxLayout>
#include <QMouseEvent>
#include <QDebug>
SettingLabel::SettingLabel(QString text, QWidget *parent)
: QWidget(parent)
@ -136,7 +106,6 @@ BluetoothApplet::BluetoothApplet(QWidget *parent)
, m_settingLabel(new SettingLabel(tr("Bluetooth settings"), this))
, m_mainLayout(new QVBoxLayout(this))
, m_contentLayout(new QVBoxLayout(m_contentWidget))
, m_Separator(new HorizontalSeperator(this))
{
initUi();
initConnect();
@ -205,7 +174,7 @@ void BluetoothApplet::onAdapterAdded(Adapter *adapter)
m_adapterItems.insert(adapter->id(), adapterItem);
//插入分割线
m_contentLayout->insertWidget(0, m_Separator, Qt::AlignTop | Qt::AlignVCenter);
// m_contentLayout->insertWidget(0, m_Separator, Qt::AlignTop | Qt::AlignVCenter);
m_contentLayout->insertWidget(0, adapterItem, Qt::AlignTop | Qt::AlignVCenter);
updateBluetoothPowerState();
updateSize();
@ -297,13 +266,11 @@ void BluetoothApplet::updateIconTheme()
{
QPalette widgetBackgroud;
QPalette scroareaBackgroud;
if(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType){
if(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
widgetBackgroud.setColor(QPalette::Background, QColor(255, 255, 255, 0.03 * 255));
m_Separator->setColor(QColor(0, 0, 0, 0.1 * 255));
} else {
else
widgetBackgroud.setColor(QPalette::Background, QColor(0, 0, 0, 0.03 * 255));
m_Separator->setColor(QColor(255, 255, 255, 0.1 * 255));
}
m_contentWidget->setAutoFillBackground(true);
m_contentWidget->setPalette(widgetBackgroud);
scroareaBackgroud.setColor(QPalette::Background, Qt::transparent);

View File

@ -45,22 +45,6 @@ DWIDGET_END_NAMESPACE
DWIDGET_USE_NAMESPACE
class HorizontalSeperator : public QWidget
{
Q_OBJECT
public:
explicit HorizontalSeperator(QWidget *parent = nullptr);
void setColor(const QColor color);
protected:
void paintEvent(QPaintEvent *e);
private:
QColor m_color;
};
class SettingLabel : public QWidget
{
Q_OBJECT
@ -130,8 +114,7 @@ private:
QVBoxLayout *m_contentLayout = nullptr;
QStringList m_connectDeviceName;
QMap<QString, BluetoothAdapterItem *> m_adapterItems;
HorizontalSeperator *m_Separator = nullptr;
QMap<QString, BluetoothAdapterItem *> m_adapterItems; // 所有蓝牙适配器
};
#endif // BLUETOOTHAPPLET_H

View File

@ -6,7 +6,8 @@ project(${PLUGIN_NAME})
# Sources files
file(GLOB_RECURSE SRCS "*.h" "*.cpp" "../../widgets/*.h" "../../widgets/*.cpp"
"../../frame/util/imageutil.h" "../../frame/util/imageutil.cpp"
"../../frame/util/statebutton.h" "../../frame/util/statebutton.cpp")
"../../frame/util/statebutton.h" "../../frame/util/statebutton.cpp"
"../../frame/util/horizontalseperator.h" "../../frame/util/horizontalseperator.cpp")
find_package(PkgConfig REQUIRED)
find_package(Qt5Widgets REQUIRED)

View File

@ -22,7 +22,7 @@
#include "constants.h"
#include "wireditem.h"
#include "applet/horizontalseperator.h"
#include "util/horizontalseperator.h"
#include "../widgets/tipswidget.h"
#include "util/utils.h"
#include "util/statebutton.h"

View File

@ -183,18 +183,13 @@ void NetworkItem::setControlBackground()
{
QPalette backgroud;
QColor separatorColor;
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) {
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
backgroud.setColor(QPalette::Background, QColor(255, 255, 255, 0.03 * 255));
separatorColor.setRgb(0, 0, 0, 0.1 * 255);
} else {
else
backgroud.setColor(QPalette::Background, QColor(0, 0, 0, 0.03 * 255));
separatorColor.setRgb(255, 255, 255, 0.1 * 255);
}
m_applet->setAutoFillBackground(true);
m_applet->setPalette(backgroud);
m_firstSeparator->setColor(separatorColor);
m_secondSeparator->setColor(separatorColor);
m_thirdSeparator->setColor(separatorColor);
}
QWidget *NetworkItem::itemApplet()

View File

@ -4,7 +4,8 @@ set(PLUGIN_NAME "sound")
project(${PLUGIN_NAME})
# Sources files
file(GLOB_RECURSE SRCS "*.h" "*.cpp" "../../widgets/*.h" "../../widgets/*.cpp" "../../frame/util/imageutil.h" "../../frame/util/imageutil.cpp")
file(GLOB_RECURSE SRCS "*.h" "*.cpp" "../../widgets/*.h" "../../widgets/*.cpp" "../../frame/util/imageutil.h" "../../frame/util/imageutil.cpp"
"../../frame/util/horizontalseperator.h" "../../frame/util/horizontalseperator.cpp")
find_package(PkgConfig REQUIRED)
find_package(Qt5Widgets REQUIRED)

View File

@ -1,49 +0,0 @@
/*
* 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 "horizontalseparator.h"
#include <QPainter>
/**
* @brief HorizontalSeparator::HorizontalSeparator 线,2
* @param parent
*/
HorizontalSeparator::HorizontalSeparator(QWidget *parent)
: QWidget(parent)
{
setFixedHeight(2);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
void HorizontalSeparator::setColor(const QColor color)
{
m_color = color;
update();
}
void HorizontalSeparator::paintEvent(QPaintEvent *e)
{
QWidget::paintEvent(e);
QPainter painter(this);
painter.fillRect(rect(), m_color);
}

View File

@ -1,43 +0,0 @@
/*
* 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/>.
*/
#ifndef HORIZONTALSEPARATOR_H
#define HORIZONTALSEPARATOR_H
#include <QWidget>
class HorizontalSeparator : public QWidget
{
Q_OBJECT
public:
explicit HorizontalSeparator(QWidget *parent = 0);
void setColor(const QColor color);
protected:
void paintEvent(QPaintEvent *e);
private:
QColor m_color;
};
#endif // HORIZONTALSEPARATOR_H

View File

@ -6,13 +6,11 @@
#include "soundapplet.h"
#include "sinkinputwidget.h"
#include "./componments/volumeslider.h"
#include "./componments/horizontalseparator.h"
SET_BUTTON_ACCESSIBLE(SoundItem, "plugin-sounditem")
SET_FORM_ACCESSIBLE(SoundApplet, "soundapplet")
SET_FORM_ACCESSIBLE(SinkInputWidget, "sinkinputwidget")
SET_SLIDER_ACCESSIBLE(VolumeSlider, "volumeslider")
SET_FORM_ACCESSIBLE(HorizontalSeparator, "horizontalseparator")
QAccessibleInterface *soundAccessibleFactory(const QString &classname, QObject *object)
{
@ -22,7 +20,6 @@ QAccessibleInterface *soundAccessibleFactory(const QString &classname, QObject *
USE_ACCESSIBLE(classname, SoundApplet);
USE_ACCESSIBLE(classname, SinkInputWidget);
USE_ACCESSIBLE(classname, VolumeSlider);
USE_ACCESSIBLE(classname, HorizontalSeparator);
return interface;
}

View File

@ -21,7 +21,7 @@
#include "soundapplet.h"
#include "sinkinputwidget.h"
#include "componments/horizontalseparator.h"
#include "util/horizontalseperator.h"
#include "../widgets/tipswidget.h"
#include "../frame/util/imageutil.h"
#include "util/utils.h"
@ -140,8 +140,8 @@ SoundApplet::SoundApplet(QWidget *parent)
, m_volumeIconMax(new QLabel)
, m_volumeSlider(new VolumeSlider)
, m_soundShow(new TipsWidget)
, m_separator(new HorizontalSeparator(this))
, m_secondSeparator(new HorizontalSeparator(this))
, m_seperator(new HorizontalSeperator(this))
, m_secondSeperator(new HorizontalSeperator(this))
, m_deviceLabel(nullptr)
, m_audioInter(new DBusAudio("com.deepin.daemon.Audio", "/com/deepin/daemon/Audio", QDBusConnection::sessionBus(), this))
, m_defSinkInter(nullptr)
@ -163,15 +163,11 @@ void SoundApplet::setControlBackground()
{
QPalette soundAppletBackgroud;
QPalette listViewBackgroud = m_listView->palette();
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) {
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
soundAppletBackgroud.setColor(QPalette::Background, QColor(255, 255, 255, 0.03 * 255));
m_separator->setColor(QColor(0, 0, 0, 0.1 * 255));
m_secondSeparator->setColor(QColor(0, 0, 0, 0.1 * 255));
} else {
else
soundAppletBackgroud.setColor(QPalette::Background, QColor(0, 0, 0, 0.03 * 255));
m_separator->setColor(QColor(255, 255, 255, 0.1 * 255));
m_secondSeparator->setColor(QColor(255, 255, 255, 0.1 * 255));
}
this->setAutoFillBackground(true);
this->setPalette(soundAppletBackgroud);
listViewBackgroud.setColor(QPalette::Base, Qt::transparent);
@ -232,7 +228,7 @@ void SoundApplet::initUi()
QVBoxLayout *deviceLineLayout = new QVBoxLayout;
deviceLineLayout->addLayout(deviceLayout);
deviceLineLayout->addWidget(m_separator);
deviceLineLayout->addWidget(m_seperator);
deviceLineLayout->setMargin(0);
deviceLineLayout->setSpacing(DEVICE_SPACING);
@ -250,7 +246,7 @@ void SoundApplet::initUi()
//音频界面添加第二个分割线
QVBoxLayout *volumeLineLayout = new QVBoxLayout;
volumeLineLayout->addLayout(volumeCtrlLayout);
volumeLineLayout->addWidget(m_secondSeparator);
volumeLineLayout->addWidget(m_secondSeperator);
volumeLineLayout->setMargin(0);
m_volumeBtn->setFixedSize(ICON_SIZE, ICON_SIZE);
@ -392,7 +388,7 @@ void SoundApplet::sinkInputsChanged()
}
for (auto input : m_audioInter->sinkInputs()) {
appLayout->addWidget(new HorizontalSeparator);
appLayout->addWidget(new HorizontalSeperator(this));
SinkInputWidget *si = new SinkInputWidget(input.path());
appLayout->addWidget(si);
@ -740,7 +736,7 @@ void SoundApplet::updateListHeight()
int viewHeight = visualHeight + m_listView->spacing() * count * 2 + listMargin;
// 设备信息高度 = 设备标签 + 分隔线 + 滚动条 + 间隔
int labelHeight = m_deviceLabel->height() > m_soundShow->height() ? m_deviceLabel->height() : m_soundShow->height();
int infoHeight = labelHeight + m_separator->height() + m_volumeSlider->height() + m_centralLayout->spacing() * 3 + DEVICE_SPACING;
int infoHeight = labelHeight + m_seperator->height() + m_volumeSlider->height() + m_centralLayout->spacing() * 3 + DEVICE_SPACING;
int margain = m_centralLayout->contentsMargins().top() + m_centralLayout->contentsMargins().bottom();
//整个界面高度 = 显示声音设备列表高度 + 设备信息高度 + 边距
int totalHeight = viewHeight + infoHeight + margain;

View File

@ -40,7 +40,7 @@ DWIDGET_USE_NAMESPACE
using DBusAudio = com::deepin::daemon::Audio;
using DBusSink = com::deepin::daemon::audio::Sink;
class HorizontalSeparator;
class HorizontalSeperator;
class QGSettings;
namespace Dock{
@ -156,8 +156,8 @@ private:
VolumeSlider *m_volumeSlider;
Dock::TipsWidget *m_soundShow;
QVBoxLayout *m_centralLayout;
HorizontalSeparator *m_separator;
HorizontalSeparator *m_secondSeparator;
HorizontalSeperator *m_seperator;
HorizontalSeperator *m_secondSeperator;
Dock::TipsWidget *m_deviceLabel;
DBusAudio *m_audioInter;