| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhenyu Wang <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kitemwidget.h" |
| 24 |
#include "klistwidget.h" |
| 25 |
#include <QDebug> |
| 26 |
#include <QListWidgetItem> |
| 27 |
#include <QPainter> |
| 28 |
|
| 29 |
namespace kdk |
| 30 |
{ |
| 31 |
|
| 32 |
class KItemWidgetPrivate : public QObject, public ThemeController |
| 33 |
{ |
| 34 |
Q_OBJECT |
| 35 |
Q_DECLARE_PUBLIC(KItemWidget) |
| 36 |
|
| 37 |
public: |
| 38 |
KItemWidgetPrivate(KItemWidget *parent); |
| 39 |
|
| 40 |
protected: |
| 41 |
void changeTheme(); |
| 42 |
|
| 43 |
private: |
| 44 |
KItemWidget *q_ptr; |
| 45 |
QIcon m_icon; |
| 46 |
QString m_maintext; |
| 47 |
QString m_sectext; |
| 48 |
QSize m_iconsize; |
| 49 |
QPixmap pixmap; |
| 50 |
int x; |
| 51 |
int y; |
| 52 |
KItemWidget *m_ltem; |
| 53 |
bool flag = false; |
| 54 |
}; |
| 55 |
|
| 56 |
KItemWidgetPrivate::KItemWidgetPrivate(KItemWidget *parent) |
| 57 |
: q_ptr(parent) |
| 58 |
{ |
| 59 |
Q_Q(KItemWidget); |
| 60 |
setParent(parent); |
| 61 |
m_iconsize = QSize(35, 35); |
| 62 |
x = m_iconsize.width(); |
| 63 |
y = m_iconsize.height(); |
| 64 |
changeTheme(); |
| 65 |
|
| 66 |
connect(m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 67 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 68 |
{ |
| 69 |
changeTheme(); |
| 70 |
} |
| 71 |
}); |
| 72 |
} |
| 73 |
|
| 74 |
void KItemWidgetPrivate::changeTheme() |
| 75 |
{ |
| 76 |
Q_Q(KItemWidget); |
| 77 |
initThemeStyle(); |
| 78 |
} |
| 79 |
|
| 80 |
KItemWidget::KItemWidget(const QIcon &Myicon, QString MmainText, QString MsecText, QWidget *parent) |
| 81 |
: QWidget(parent) |
| 82 |
, d_ptr(new KItemWidgetPrivate(this)) |
| 83 |
{ |
| 84 |
Q_D(KItemWidget); |
| 85 |
setMinimumSize(800, 800); |
| 86 |
|
| 87 |
d->pixmap = Myicon.pixmap(QSize(d->m_iconsize)); |
| 88 |
|
| 89 |
d->m_maintext = MmainText; |
| 90 |
|
| 91 |
d->m_sectext = MsecText; |
| 92 |
} |
| 93 |
|
| 94 |
void KItemWidget::SetInverse() |
| 95 |
{ |
| 96 |
Q_D(KItemWidget); |
| 97 |
d->flag = true; |
| 98 |
update(); |
| 99 |
} |
| 100 |
|
| 101 |
void KItemWidget::CancelInverse() |
| 102 |
{ |
| 103 |
Q_D(KItemWidget); |
| 104 |
d->flag = false; |
| 105 |
update(); |
| 106 |
} |
| 107 |
|
| 108 |
void KItemWidget::SetIconSize(QSize size) |
| 109 |
{ |
| 110 |
Q_D(KItemWidget); |
| 111 |
d->m_iconsize = size; |
| 112 |
d->x = d->m_iconsize.width(); |
| 113 |
d->y = d->m_iconsize.height(); |
| 114 |
update(); |
| 115 |
} |
| 116 |
|
| 117 |
void KItemWidget::paintEvent(QPaintEvent *event) |
| 118 |
{ |
| 119 |
Q_D(KItemWidget); |
| 120 |
|
| 121 |
QPainter painter(this); |
| 122 |
|
| 123 |
painter.drawPixmap(8, 10, d->x, d->y, d->pixmap); // 画图片 |
| 124 |
|
| 125 |
QFont font = painter.font(); |
| 126 |
font.setPointSize(10); // 设置字体 |
| 127 |
painter.setFont(font); |
| 128 |
if (ThemeController::themeMode() == LightTheme) // 判断主题 |
| 129 |
{ |
| 130 |
if (d->flag) { |
| 131 |
painter.setPen(ThemeController::getCustomColorFromDT("kwhite")); // 白色 |
| 132 |
painter.drawText(QRect(52, 10, 1000, 16), d->m_maintext); // 画文本 |
| 133 |
} else { |
| 134 |
painter.setPen(QColor(54, 54, 54)); // 黑色 |
| 135 |
painter.drawText(QRect(52, 10, 1000, 16), d->m_maintext); // 画文本 |
| 136 |
} |
| 137 |
painter.setPen(QColor(150, 150, 150)); // 褐色 |
| 138 |
painter.drawText(QRect(52, 28, 1000, 16), d->m_sectext); |
| 139 |
} else if (ThemeController::themeMode() == DarkTheme) { |
| 140 |
painter.setPen(ThemeController::getCustomColorFromDT("kwhite")); // 白色 |
| 141 |
painter.drawText(QRect(52, 10, 1000, 16), d->m_maintext); // 画文本 |
| 142 |
|
| 143 |
painter.setPen(QColor(150, 150, 150)); // 褐色 |
| 144 |
painter.drawText(QRect(52, 28, 1000, 16), d->m_sectext); |
| 145 |
} |
| 146 |
|
| 147 |
painter.setRenderHint(QPainter::Antialiasing); |
| 148 |
/* 尽可能消除文本锯齿边缘 */ |
| 149 |
painter.setRenderHint(QPainter::TextAntialiasing); |
| 150 |
|
| 151 |
this->setContentsMargins(8, 10, 10, 5); // 边距 |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
#include "kitemwidget.moc" |
| 156 |
#include "moc_kitemwidget.cpp" |