1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* 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, or (at your option)
* 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 "tristatelabel.h"
static inline qreal mixQreal(qreal a, qreal b, qreal bias)
{
return a + (b - a) * bias;
}
QColor mixColor(const QColor &c1, const QColor &c2, qreal bias)
{
if (bias <= 0.0) {
return c1;
}
if (bias >= 1.0) {
return c2;
}
if (qIsNaN(bias)) {
return c1;
}
qreal r = mixQreal(c1.redF(), c2.redF(), bias);
qreal g = mixQreal(c1.greenF(), c2.greenF(), bias);
qreal b = mixQreal(c1.blueF(), c2.blueF(), bias);
qreal a = mixQreal(c1.alphaF(), c2.alphaF(), bias);
return QColor::fromRgbF(r, g, b, a);
}
TristateLabel::TristateLabel(const QString &text, QWidget *parent)
{
setText(abridge(text));
adjustSize();
QPalette pal;
QBrush brush = pal.placeholderText();
QColor textColor = brush.color();
QString stringColor = QString("color: rgba(%1,%2,%3,%4)")
.arg(textColor.red())
.arg(textColor.green())
.arg(textColor.blue())
.arg(textColor.alphaF());
this->setStyleSheet(stringColor);
const QByteArray idd(THEME_QT_SCHEMA);
QGSettings *qtSettings = new QGSettings(idd, QByteArray(), this);
connect(qtSettings, &QGSettings::changed, this, [=](const QString &key) {
if ("styleName" == key) {
QPalette pal;
QBrush brush = pal.placeholderText();
QColor textColor = brush.color();
QString stringColor = QString("color: rgba(%1,%2,%3,%4)")
.arg(textColor.red())
.arg(textColor.green())
.arg(textColor.blue())
.arg(textColor.alphaF());
this->setStyleSheet(stringColor);
}
});
}
TristateLabel::~TristateLabel()
{
}
QString TristateLabel::abridge(QString text)
{
/* 设计要求,部分首页显示插件名和导航显示名不一致*/
if (text == "时间和日期") {
text = "时间日期";
} else if (text == "区域语言") {
text = "语言";
}
return text;
}
void TristateLabel::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
QPalette pal;
QBrush brush = pal.placeholderText();
QColor textColor = brush.color();
QPushButton *button = new QPushButton;
QColor highlight = button->palette().color(QPalette::Active, QPalette::Highlight);
QColor mix = button->palette().color(QPalette::Active, QPalette::BrightText);
textColor = mixColor(highlight, mix, 0.2);
QString stringColor = QString("color: rgba(%1,%2,%3,%4)")
.arg(textColor.red())
.arg(textColor.green())
.arg(textColor.blue())
.arg(textColor.alphaF());
this->setStyleSheet(stringColor);
mClicked = true;
}
}
void TristateLabel::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
mClicked = false;
QPalette pal;
QBrush brush = pal.placeholderText();
QColor textColor = brush.color();
QString stringColor = QString("color: rgba(%1,%2,%3,%4)")
.arg(textColor.red())
.arg(textColor.green())
.arg(textColor.blue())
.arg(textColor.alphaF());
this->setStyleSheet(stringColor);
emit clicked();
}
}
void TristateLabel::enterEvent(QEvent *e)
{
Q_UNUSED(e)
QPalette pal;
QBrush brush = pal.placeholderText();
QColor textColor = brush.color();
QPushButton *button = new QPushButton;
QColor highlight = button->palette().color(QPalette::Active, QPalette::Highlight);
QColor mix = button->palette().color(QPalette::Active, QPalette::BrightText);
textColor = mixColor(highlight, mix, 0.05);
QString stringColor = QString("color: rgba(%1,%2,%3,%4)")
.arg(textColor.red())
.arg(textColor.green())
.arg(textColor.blue())
.arg(textColor.alphaF());
this->setStyleSheet(stringColor);
mMoved = true;
}
void TristateLabel::leaveEvent(QEvent *e)
{
Q_UNUSED(e)
mMoved = false;
QPalette pal;
QBrush brush = pal.placeholderText();
QColor textColor = brush.color();
QString stringColor = QString("color: rgba(%1,%2,%3,%4)")
.arg(textColor.red())
.arg(textColor.green())
.arg(textColor.blue())
.arg(textColor.alphaF());
this->setStyleSheet(stringColor);
}
|