| 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: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kcommentpanel.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QBoxLayout> |
| 26 |
#include <QIcon> |
| 27 |
#include <QLabel> |
| 28 |
#include <QPainter> |
| 29 |
#include "accessinfohelper.h" |
| 30 |
|
| 31 |
namespace kdk |
| 32 |
{ |
| 33 |
class StarWidget : public QWidget |
| 34 |
{ |
| 35 |
public: |
| 36 |
StarWidget(QWidget *parent); |
| 37 |
void setGrade(StarLevel level); |
| 38 |
|
| 39 |
private: |
| 40 |
void doFresh(); |
| 41 |
StarLevel m_starLevel; |
| 42 |
QLabel *m_plbl1; |
| 43 |
QLabel *m_plbl2; |
| 44 |
QLabel *m_plbl3; |
| 45 |
QLabel *m_plbl4; |
| 46 |
QLabel *m_plbl5; |
| 47 |
}; |
| 48 |
|
| 49 |
StarWidget::StarWidget(QWidget *parent) |
| 50 |
: QWidget(parent) |
| 51 |
, m_starLevel(LevelZero) |
| 52 |
{ |
| 53 |
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 54 |
QHBoxLayout *hLayout = new QHBoxLayout(this); |
| 55 |
hLayout->setContentsMargins(0, 0, 0, 0); |
| 56 |
hLayout->setSpacing(2); |
| 57 |
m_plbl1 = new QLabel(this); |
| 58 |
m_plbl2 = new QLabel(this); |
| 59 |
m_plbl3 = new QLabel(this); |
| 60 |
m_plbl4 = new QLabel(this); |
| 61 |
m_plbl5 = new QLabel(this); |
| 62 |
m_plbl1->setAlignment(Qt::AlignTop); |
| 63 |
m_plbl2->setAlignment(Qt::AlignTop); |
| 64 |
m_plbl3->setAlignment(Qt::AlignTop); |
| 65 |
m_plbl4->setAlignment(Qt::AlignTop); |
| 66 |
m_plbl5->setAlignment(Qt::AlignTop); |
| 67 |
m_plbl1->setFixedSize(14, 14); |
| 68 |
m_plbl2->setFixedSize(14, 14); |
| 69 |
m_plbl3->setFixedSize(14, 14); |
| 70 |
m_plbl4->setFixedSize(14, 14); |
| 71 |
m_plbl5->setFixedSize(14, 14); |
| 72 |
hLayout->addWidget(m_plbl1); |
| 73 |
hLayout->addWidget(m_plbl2); |
| 74 |
hLayout->addWidget(m_plbl3); |
| 75 |
hLayout->addWidget(m_plbl4); |
| 76 |
hLayout->addWidget(m_plbl5); |
| 77 |
|
| 78 |
KDK_ALL_INFO_FORMAT(m_plbl1,""); |
| 79 |
KDK_ALL_INFO_FORMAT(m_plbl2,""); |
| 80 |
KDK_ALL_INFO_FORMAT(m_plbl3,""); |
| 81 |
KDK_ALL_INFO_FORMAT(m_plbl4,""); |
| 82 |
KDK_ALL_INFO_FORMAT(m_plbl5,""); |
| 83 |
} |
| 84 |
|
| 85 |
void StarWidget::setGrade(StarLevel level) |
| 86 |
{ |
| 87 |
if (level != m_starLevel) { |
| 88 |
m_starLevel = level; |
| 89 |
doFresh(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void StarWidget::doFresh() |
| 94 |
{ |
| 95 |
switch (m_starLevel) { |
| 96 |
case LevelZero: |
| 97 |
break; |
| 98 |
case LevelOne: |
| 99 |
m_plbl1->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 100 |
m_plbl2->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 101 |
m_plbl3->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 102 |
m_plbl4->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 103 |
m_plbl5->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 104 |
break; |
| 105 |
case LevelTwo: |
| 106 |
m_plbl1->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 107 |
m_plbl2->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 108 |
m_plbl3->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 109 |
m_plbl4->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 110 |
m_plbl5->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 111 |
break; |
| 112 |
case LevelThree: |
| 113 |
m_plbl1->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 114 |
m_plbl2->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 115 |
m_plbl3->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 116 |
m_plbl4->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 117 |
m_plbl5->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 118 |
break; |
| 119 |
case LevelFour: |
| 120 |
m_plbl1->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 121 |
m_plbl2->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 122 |
m_plbl3->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 123 |
m_plbl4->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 124 |
m_plbl5->setPixmap(QIcon::fromTheme("ukui-starred-symbolic").pixmap(12, 12)); |
| 125 |
break; |
| 126 |
case LevelFive: |
| 127 |
m_plbl1->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 128 |
m_plbl2->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 129 |
m_plbl3->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 130 |
m_plbl4->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 131 |
m_plbl5->setPixmap(QIcon::fromTheme("ukui-starred-on-symbolic").pixmap(12, 12)); |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
class KCommentPanelPrivate : public QObject, public ThemeController |
| 137 |
{ |
| 138 |
Q_DECLARE_PUBLIC(KCommentPanel) |
| 139 |
Q_OBJECT |
| 140 |
|
| 141 |
public: |
| 142 |
KCommentPanelPrivate(KCommentPanel *parent); |
| 143 |
void changeTheme(); |
| 144 |
|
| 145 |
private: |
| 146 |
QLabel *m_pContentLabel; |
| 147 |
QLabel *m_pPicLabel; |
| 148 |
QLabel *m_pTimeLabel; |
| 149 |
QLabel *m_pNameLabel; |
| 150 |
StarWidget *m_pStarWidget; |
| 151 |
QColor m_color; |
| 152 |
KCommentPanel *q_ptr; |
| 153 |
}; |
| 154 |
|
| 155 |
KCommentPanel::KCommentPanel(QWidget *parent) |
| 156 |
: QWidget(parent) |
| 157 |
, d_ptr(new KCommentPanelPrivate(this)) |
| 158 |
{ |
| 159 |
} |
| 160 |
|
| 161 |
void KCommentPanel::setIcon(const QIcon &icon) |
| 162 |
{ |
| 163 |
Q_D(KCommentPanel); |
| 164 |
d->m_pPicLabel->setPixmap(icon.pixmap(50, 50)); |
| 165 |
} |
| 166 |
|
| 167 |
void KCommentPanel::setTime(const QString &str) |
| 168 |
{ |
| 169 |
Q_D(KCommentPanel); |
| 170 |
d->m_pTimeLabel->setText(str); |
| 171 |
} |
| 172 |
|
| 173 |
void KCommentPanel::setName(const QString &str) |
| 174 |
{ |
| 175 |
Q_D(KCommentPanel); |
| 176 |
d->m_pNameLabel->setText(str); |
| 177 |
} |
| 178 |
|
| 179 |
void KCommentPanel::setText(const QString &str) |
| 180 |
{ |
| 181 |
Q_D(KCommentPanel); |
| 182 |
d->m_pContentLabel->setText(str); |
| 183 |
} |
| 184 |
|
| 185 |
void KCommentPanel::setGrade(StarLevel level) |
| 186 |
{ |
| 187 |
Q_D(KCommentPanel); |
| 188 |
d->m_pStarWidget->setGrade(level); |
| 189 |
} |
| 190 |
|
| 191 |
void KCommentPanel::paintEvent(QPaintEvent *event) |
| 192 |
{ |
| 193 |
Q_D(KCommentPanel); |
| 194 |
|
| 195 |
QPainter painter(this); |
| 196 |
painter.setRenderHint(QPainter::Antialiasing); // 反锯齿; |
| 197 |
painter.setPen(Qt::NoPen); |
| 198 |
painter.setBrush(d->m_color); |
| 199 |
|
| 200 |
QRect rect = this->rect(); |
| 201 |
rect.setWidth(rect.width()); |
| 202 |
rect.setHeight(rect.height()); |
| 203 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 204 |
if (radius == -1) |
| 205 |
radius = 6; |
| 206 |
painter.drawRoundedRect(rect, radius, radius); |
| 207 |
} |
| 208 |
|
| 209 |
KCommentPanelPrivate::KCommentPanelPrivate(KCommentPanel *parent) |
| 210 |
: q_ptr(parent) |
| 211 |
{ |
| 212 |
Q_Q(KCommentPanel); |
| 213 |
QVBoxLayout *mainLayout = new QVBoxLayout(q); |
| 214 |
mainLayout->setContentsMargins(16, 16, 16, 16); |
| 215 |
QHBoxLayout *controlLayout = new QHBoxLayout; |
| 216 |
m_pPicLabel = new QLabel(q); |
| 217 |
m_pPicLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 218 |
m_pPicLabel->setFixedSize(50, 50); |
| 219 |
controlLayout->addWidget(m_pPicLabel); |
| 220 |
|
| 221 |
QVBoxLayout *vLayout = new QVBoxLayout; |
| 222 |
vLayout->setSpacing(1); |
| 223 |
vLayout->setContentsMargins(0, 0, 0, 0); |
| 224 |
controlLayout->addLayout(vLayout); |
| 225 |
|
| 226 |
QHBoxLayout *hLayout = new QHBoxLayout; |
| 227 |
hLayout->setContentsMargins(0, 0, 0, 0); |
| 228 |
m_pNameLabel = new QLabel(q); |
| 229 |
m_pNameLabel->setAlignment(Qt::AlignBottom); |
| 230 |
m_pNameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 231 |
m_pTimeLabel = new QLabel(q); |
| 232 |
m_pTimeLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 233 |
hLayout->addWidget(m_pNameLabel); |
| 234 |
hLayout->addStretch(); |
| 235 |
hLayout->addWidget(m_pTimeLabel); |
| 236 |
vLayout->addLayout(hLayout); |
| 237 |
hLayout = new QHBoxLayout; |
| 238 |
hLayout->setContentsMargins(0, 0, 0, 0); |
| 239 |
m_pStarWidget = new StarWidget(q); |
| 240 |
hLayout->addWidget(m_pStarWidget); |
| 241 |
hLayout->addStretch(); |
| 242 |
vLayout->addLayout(hLayout); |
| 243 |
m_pContentLabel = new QLabel(q); |
| 244 |
m_pContentLabel->setWordWrap(true); |
| 245 |
m_pContentLabel->setAlignment(Qt::AlignTop); |
| 246 |
m_pContentLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 247 |
mainLayout->addLayout(controlLayout); |
| 248 |
mainLayout->addWidget(m_pContentLabel); |
| 249 |
mainLayout->addStretch(); |
| 250 |
changeTheme(); |
| 251 |
|
| 252 |
connect(m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 253 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 254 |
{ |
| 255 |
changeTheme(); |
| 256 |
} |
| 257 |
}); |
| 258 |
} |
| 259 |
|
| 260 |
void KCommentPanelPrivate::changeTheme() |
| 261 |
{ |
| 262 |
Q_Q(KCommentPanel); |
| 263 |
initThemeStyle(); |
| 264 |
if (ThemeController::themeMode() == LightTheme) { |
| 265 |
m_pTimeLabel->setStyleSheet("font-size:14px;color:#8C8C8C;"); |
| 266 |
m_pNameLabel->setStyleSheet("font-size:16px;font-weight:500;color:#262626;"); |
| 267 |
m_color = ThemeController::getCustomColorFromDT("kgray-2"); |
| 268 |
m_pContentLabel->setStyleSheet("color:#595959"); |
| 269 |
} else { |
| 270 |
m_pTimeLabel->setStyleSheet("font-size:14px;color:#737373;"); |
| 271 |
m_pNameLabel->setStyleSheet("font-size:16px;font-weight:500;color:#FFFFFF;"); |
| 272 |
m_color = ThemeController::getCustomColorFromDT("kgray-17"); |
| 273 |
m_pContentLabel->setStyleSheet("color:#D9D9D9"); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
#include "kcommentpanel.moc" |
| 279 |
#include "moc_kcommentpanel.cpp" |