Files

190 lines
5.8 KiB
C++
Raw Permalink Normal View History

2020-02-02 20:13:31 +08:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2019 Tianjin KYLIN Information Technology 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 2 of the License, 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
2020-01-17 10:20:22 +08:00
#include "leftwidgetitem.h"
2020-02-01 13:06:16 +08:00
#include <QPainter>
#include <QStyleOption>
#include <QDebug>
#include <QSvgRenderer>
#include <QApplication>
2021-01-30 16:47:29 +08:00
#include <QGSettings>
2020-02-01 13:06:16 +08:00
2020-01-17 10:20:22 +08:00
LeftWidgetItem::LeftWidgetItem(QWidget *parent) :
QWidget(parent)
{
2020-02-01 13:06:16 +08:00
widget = new QWidget(this);
2020-02-23 12:45:22 +08:00
widget->setFixedHeight(40);
2020-02-01 13:06:16 +08:00
iconLabel = new QLabel(widget);
2020-02-01 13:06:16 +08:00
textLabel = new QLabel(widget);
2021-10-08 10:45:08 +08:00
textLabel->setObjectName("textlabel");
QSizePolicy policy1 = textLabel->sizePolicy();
policy1.setHorizontalPolicy(QSizePolicy::Fixed);
policy1.setVerticalPolicy(QSizePolicy::Fixed);
textLabel->setSizePolicy(policy1);
textLabel->setScaledContents(true);
2020-02-01 13:06:16 +08:00
QHBoxLayout * mainlayout = new QHBoxLayout(widget);
mainlayout->setContentsMargins(8, 0, 0, 0);
mainlayout->addWidget(iconLabel, Qt::AlignVCenter);
mainlayout->addWidget(textLabel, Qt::AlignVCenter);
mainlayout->addStretch();
widget->setLayout(mainlayout);
QVBoxLayout * baseVerLayout = new QVBoxLayout(this);
baseVerLayout->setSpacing(0);
baseVerLayout->setMargin(0);
baseVerLayout->addWidget(widget);
baseVerLayout->addStretch();
setLayout(baseVerLayout);
2021-01-30 16:47:29 +08:00
const QByteArray id("org.ukui.style");
QGSettings * fontSetting = new QGSettings(id, QByteArray(), this);
connect(fontSetting, &QGSettings::changed,[=](QString key) {
if ("systemFont" == key || "systemFontSize" ==key) {
changedLabelSlot();
}
});
}
2020-01-17 10:20:22 +08:00
LeftWidgetItem::~LeftWidgetItem()
{
}
void LeftWidgetItem::setLabelPixmap(QString filename, QString icoName, QString color) {
this->icoName = icoName;
QPixmap pix = loadSvg(filename, color);
2021-05-12 09:25:56 +08:00
iconLabel->setFixedSize(24,24);//使用pix.size()时,多倍显示(200%)会有问题
iconLabel->setPixmap(pix);
}
void LeftWidgetItem::isSetLabelPixmapWhite(bool selected) {
QString fileName;
if(selected) {
fileName = "://img/secondaryleftmenu/"+this->icoName+"White.svg";
} else {
fileName = "://img/secondaryleftmenu/"+this->icoName+".svg";
}
QPixmap pix = loadSvg(fileName, "blue");
iconLabel->setPixmap(pix);
}
2021-01-30 16:47:29 +08:00
void LeftWidgetItem::setLabelText(QString text) {
2021-01-30 16:47:29 +08:00
mStr = text;
changedLabelSlot();
}
void LeftWidgetItem::setLabelTextIsWhite(bool selected) {
if(selected) {
2021-10-08 10:45:08 +08:00
textLabel->setStyleSheet("QLabel#textlabel{color: palette(highlighted-text);}");
} else {
2021-10-08 10:45:08 +08:00
textLabel->setStyleSheet("QLabel#textlabel{color: palette(windowText);}");
}
}
2020-02-01 13:06:16 +08:00
void LeftWidgetItem::setSelected(bool selected){
2020-04-17 14:00:44 +08:00
if (selected) {
2021-03-18 16:29:17 +08:00
widget->setStyleSheet("QWidget{background: palette(Highlight); border-radius: 4px;}");
2020-04-17 14:00:44 +08:00
} else {
widget->setStyleSheet("QListWidget::Item:hover{background:#FF3D6BE5;border-radius: 4px;}");
2020-04-17 14:00:44 +08:00
}
2020-02-01 13:06:16 +08:00
}
2020-01-17 10:20:22 +08:00
QString LeftWidgetItem::text(){
2021-01-30 16:47:29 +08:00
return mStr;
}
const QPixmap LeftWidgetItem::loadSvg(const QString &fileName, QString color)
{
int size = 24;
const auto ratio = qApp->devicePixelRatio();
if ( 2 == ratio) {
size = 48;
} else if (3 == ratio) {
size = 96;
}
QPixmap pixmap(size, size);
QSvgRenderer renderer(fileName);
pixmap.fill(Qt::transparent);
QPainter painter;
painter.begin(&pixmap);
renderer.render(&painter);
painter.end();
pixmap.setDevicePixelRatio(ratio);
return drawSymbolicColoredPixmap(pixmap, color);
}
QPixmap LeftWidgetItem::drawSymbolicColoredPixmap(const QPixmap &source, QString cgColor)
{
QImage img = source.toImage();
for (int x = 0; x < img.width(); x++) {
for (int y = 0; y < img.height(); y++) {
auto color = img.pixelColor(x, y);
if (color.alpha() > 0) {
if ("white" == cgColor) {
color.setRed(255);
color.setGreen(255);
color.setBlue(255);
img.setPixelColor(x, y, color);
} else if ("black" == cgColor) {
color.setRed(0);
color.setGreen(0);
color.setBlue(0);
img.setPixelColor(x, y, color);
} else if ("gray" == cgColor) {
color.setRed(152);
color.setGreen(163);
color.setBlue(164);
img.setPixelColor(x, y, color);
} else {
return source;
}
}
}
}
return QPixmap::fromImage(img);
}
2021-01-30 16:47:29 +08:00
void LeftWidgetItem::resizeEvent(QResizeEvent *event) {
2021-05-12 09:25:56 +08:00
this->textLabel->setFixedWidth(this->width() - 40);
2021-01-30 16:47:29 +08:00
changedLabelSlot();
QWidget::resizeEvent(event);
}
void LeftWidgetItem::changedLabelSlot() {
QFontMetrics fontMetrics(textLabel->font());
int fontSize = fontMetrics.width(mStr);
if (fontSize > textLabel->width()) {
textLabel->setText(fontMetrics.elidedText(mStr, Qt::ElideRight, textLabel->width()));
textLabel->setToolTip(mStr);
} else {
textLabel->setText(mStr);
textLabel->setToolTip("");
}
}