infobutton adapt theme

This commit is contained in:
jzxc95 2021-10-28 13:39:46 +08:00
parent 87c822b7ad
commit 12bd4bcc8b
7 changed files with 153 additions and 2 deletions

View File

@ -0,0 +1,105 @@
#include "infobutton.h"
#include <QEvent>
#include <QPainter>
#include <QApplication>
#include <QDebug>
#define BUTTON_SIZE 36,36
#define ICON_SIZE 16,16
#define BACKGROUND_COLOR QColor(0,0,0,0)
#define FOREGROUND_COLOR_NORMAL qApp->palette().text().color()
#define FOREGROUND_COLOR_HOVER QColor(55,144,250,255)
#define FOREGROUND_COLOR_PRESS QColor(36,109,212,255)
#define OUTER_PATH 8,8,16,16
#define INNER_PATH 9,9,14,14
#define TEXT_POS 14,5,16,16,0
#define BUTTON_SIZE 36,36
#define THEME_SCHAME "org.ukui.style"
#define COLOR_THEME "styleName"
InfoButton::InfoButton(QWidget *parent) : QPushButton(parent)
{
this->setFixedSize(BUTTON_SIZE);
initUI();
const QByteArray style_id(THEME_SCHAME);
if (QGSettings::isSchemaInstalled(style_id)) {
m_styleGsettings = new QGSettings(style_id);
connect(m_styleGsettings, &QGSettings::changed, this, &InfoButton::onGSettingChaned);
} else {
qDebug() << "Gsettings interface \"org.ukui.style\" is not exist!";
}
}
void InfoButton::initUI()
{
this->setFixedSize(BUTTON_SIZE);
m_backgroundColor = BACKGROUND_COLOR;
m_foregroundColor = FOREGROUND_COLOR_NORMAL;
}
void InfoButton::onGSettingChaned(const QString &key)
{
if (key == COLOR_THEME) {
m_foregroundColor = FOREGROUND_COLOR_NORMAL;
this->repaint();
}
}
void InfoButton::paintEvent(QPaintEvent *event)
{
QPalette pal = this->palette();
pal.setColor(QPalette::Base, m_backgroundColor);
pal.setColor(QPalette::Text, m_foregroundColor);
QPainterPath cPath;
cPath.addRect(0, 0, ICON_SIZE);
cPath.addEllipse(0, 0, ICON_SIZE);
QPainterPath outerPath;
outerPath.addEllipse(OUTER_PATH);
QPainterPath innerPath;
innerPath.addEllipse(INNER_PATH);
outerPath -= innerPath;
QPainter painter(this);
painter.setRenderHint(QPainter:: Antialiasing, true); //设置渲染,启动反锯齿
painter.setPen(Qt::NoPen);
painter.setBrush(pal.color(QPalette::Base));
painter.drawPath(cPath);
painter.fillPath(outerPath, pal.color(QPalette::Text));
painter.setPen(m_foregroundColor);
QFont font("Noto Sans CJK SC", 11, QFont::Normal, false);
painter.setFont(font);
painter.drawText(TEXT_POS, "i");
}
void InfoButton::enterEvent(QEvent *event)
{
m_foregroundColor = FOREGROUND_COLOR_HOVER;
this->repaint();
}
void InfoButton::leaveEvent(QEvent *event)
{
m_foregroundColor = FOREGROUND_COLOR_NORMAL;
this->repaint();
}
void InfoButton::mousePressEvent(QMouseEvent *event)
{
m_foregroundColor = FOREGROUND_COLOR_PRESS;
this->repaint();
return QPushButton::mousePressEvent(event);
}
void InfoButton::mouseReleaseEvent(QMouseEvent *event)
{
m_foregroundColor = FOREGROUND_COLOR_HOVER;
this->repaint();
return QPushButton::mouseReleaseEvent(event);
}

View File

@ -0,0 +1,35 @@
#ifndef INFOBUTTON_H
#define INFOBUTTON_H
#include <QPushButton>
#include <QIcon>
#include <QGSettings>
class InfoButton : public QPushButton
{
Q_OBJECT
public:
explicit InfoButton(QWidget * parent = nullptr);
~InfoButton() = default;
protected:
void paintEvent(QPaintEvent *event);
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
void initUI();
private:
QColor m_backgroundColor;
QColor m_foregroundColor;
//监听主题的Gsettings
QGSettings * m_styleGsettings = nullptr;
private slots:
void onGSettingChaned(const QString &key);
};
#endif // INFOBUTTON_H

View File

@ -0,0 +1,9 @@
#LIBINTERFACE_NAME = $$qtLibraryTarget(infobutton)
SOURCES += \
$$PWD/InfoButton/infobutton.cpp \
HEADERS += \
$$PWD/InfoButton/infobutton.h \

View File

@ -10,7 +10,7 @@
#include <QDebug>
#include <QImage>
#include "fixlabel.h"
#include "infobutton.h"
#include "../component/InfoButton/infobutton.h"
class LanItem : public QPushButton
{

View File

@ -3,6 +3,7 @@ TEMPLATE = lib
CONFIG += plugin
include(../component/switchbutton.pri)
include(../component/infobutton.pri)
TARGET = $$qtLibraryTarget(netconnect)
DESTDIR = ../..

View File

@ -3,6 +3,7 @@ TEMPLATE = lib
CONFIG += plugin
include(../component/switchbutton.pri)
include(../component/infobutton.pri)
TARGET = $$qtLibraryTarget(wlanconnect)
DESTDIR = ../..

View File

@ -10,7 +10,7 @@
#include <QGSettings>
#include <QImage>
#include "fixlabel.h"
#include "infobutton.h"
#include "../component/InfoButton/infobutton.h"
class WlanItem : public QPushButton
{