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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
* 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 "leftwidgetitem.h"
#include <QPainter>
#include <QStyleOption>
#include <QDebug>
#include <QSvgRenderer>
#include <QApplication>
#include <QGSettings>
LeftWidgetItem::LeftWidgetItem(QWidget *parent) :
QWidget(parent)
{
widget = new QWidget(this);
widget->setFixedHeight(40);
iconLabel = new QLabel(widget);
textLabel = new QLabel(widget);
QSizePolicy policy1 = textLabel->sizePolicy();
policy1.setHorizontalPolicy(QSizePolicy::Fixed);
policy1.setVerticalPolicy(QSizePolicy::Fixed);
textLabel->setSizePolicy(policy1);
textLabel->setScaledContents(true);
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);
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();
}
});
}
LeftWidgetItem::~LeftWidgetItem()
{
}
void LeftWidgetItem::setLabelPixmap(QString filename, QString icoName, QString color) {
this->icoName = icoName;
QPixmap pix = loadSvg(filename, color);
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);
}
void LeftWidgetItem::setLabelText(QString text) {
mStr = text;
changedLabelSlot();
}
void LeftWidgetItem::setLabelTextIsWhite(bool selected) {
if(selected) {
textLabel->setStyleSheet("color: palette(highlighted-text);");
} else {
textLabel->setStyleSheet("color: palette(windowText);");
}
}
void LeftWidgetItem::setSelected(bool selected){
if (selected) {
widget->setStyleSheet("QWidget{background: palette(Highlight); border-radius: 4px;}");
} else {
widget->setStyleSheet("QListWidget::Item:hover{background:#FF3D6BE5;border-radius: 4px;}");
}
}
QString LeftWidgetItem::text(){
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);
}
void LeftWidgetItem::resizeEvent(QResizeEvent *event) {
this->textLabel->setFixedWidth(this->width() - 40);
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("");
}
}
|