| 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 "kintrowidget.h" |
| 24 |
#include "klabel.h" |
| 25 |
#include "themeController.h" |
| 26 |
#include <QBoxLayout> |
| 27 |
#include <QDebug> |
| 28 |
#include <QEvent> |
| 29 |
#include <QLabel> |
| 30 |
#include <QMouseEvent> |
| 31 |
#include <QPainter> |
| 32 |
#include <QPainterPath> |
| 33 |
|
| 34 |
namespace kdk |
| 35 |
{ |
| 36 |
|
| 37 |
class Q_DECL_HIDDEN KIntroWidgetPrivate : public QObject, public ThemeController |
| 38 |
{ |
| 39 |
Q_OBJECT |
| 40 |
Q_DECLARE_PUBLIC(KIntroWidget) |
| 41 |
public: |
| 42 |
KIntroWidgetPrivate(KIntroWidget *parent); |
| 43 |
|
| 44 |
protected: |
| 45 |
void changeTheme(); |
| 46 |
|
| 47 |
private: |
| 48 |
KIntroWidget *const q_ptr; |
| 49 |
ViewType m_viewType; |
| 50 |
int m_bottomRightRadius; |
| 51 |
int m_topLeftRadius; |
| 52 |
int m_bottomLeftRadius; |
| 53 |
int m_topRightRadius; |
| 54 |
KLabel *m_pMainLabel; |
| 55 |
KLabel *m_pSubLabel; |
| 56 |
QColor m_bkgColor; |
| 57 |
QColor m_mixColor; |
| 58 |
QColor m_borderColor; |
| 59 |
QColor m_titleColor; |
| 60 |
}; |
| 61 |
|
| 62 |
KIntroWidgetPrivate::KIntroWidgetPrivate(KIntroWidget *parent) |
| 63 |
: q_ptr(parent) |
| 64 |
, m_viewType(ListView) |
| 65 |
{ |
| 66 |
Q_Q(KIntroWidget); |
| 67 |
m_pMainLabel = new KLabel(); |
| 68 |
QFont font = q->font(); |
| 69 |
font.setBold(true); |
| 70 |
m_pMainLabel->setFont(font); |
| 71 |
m_pSubLabel = new KLabel(); |
| 72 |
QPalette palette; |
| 73 |
palette.setColor(QPalette::WindowText, QColor(140, 140, 140)); |
| 74 |
m_pSubLabel->setPalette(palette); |
| 75 |
m_pMainLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true); |
| 76 |
m_pSubLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true); |
| 77 |
m_titleColor = ThemeController::getCustomColorFromDT("highlight-active"); |
| 78 |
|
| 79 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 80 |
if (radius == -1) { |
| 81 |
m_bottomLeftRadius = 6; |
| 82 |
m_bottomRightRadius = 6; |
| 83 |
m_topLeftRadius = 6; |
| 84 |
m_topRightRadius = 6; |
| 85 |
} else { |
| 86 |
m_bottomLeftRadius = radius; |
| 87 |
m_bottomRightRadius = radius; |
| 88 |
m_topLeftRadius = radius; |
| 89 |
m_topRightRadius = radius; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void KIntroWidgetPrivate::changeTheme() |
| 94 |
{ |
| 95 |
Q_Q(KIntroWidget); |
| 96 |
initThemeStyle(); |
| 97 |
m_bkgColor = ThemeController::getCustomColorFromDT("tooltipbase-active"); |
| 98 |
m_borderColor = ThemeController::getCustomColorFromDT("tooltipbase-active"); |
| 99 |
m_borderColor.setAlphaF(0.08); |
| 100 |
if (ThemeController::themeMode() == LightTheme) |
| 101 |
m_mixColor = Qt::black; |
| 102 |
else |
| 103 |
m_mixColor = Qt::white; |
| 104 |
q->update(); |
| 105 |
} |
| 106 |
|
| 107 |
KIntroWidget::KIntroWidget(QWidget *parent) |
| 108 |
: QWidget(parent) |
| 109 |
, d_ptr(new KIntroWidgetPrivate(this)) |
| 110 |
{ |
| 111 |
Q_D(KIntroWidget); |
| 112 |
if (d->m_viewType == ListView) |
| 113 |
setContentsMargins(22, 4, 16, 4); |
| 114 |
else |
| 115 |
setContentsMargins(22, d->m_topLeftRadius + 4, 16, 4); |
| 116 |
QVBoxLayout *vlayout = new QVBoxLayout(this); |
| 117 |
vlayout->setSpacing(0); |
| 118 |
vlayout->addWidget(d->m_pMainLabel); |
| 119 |
// vlayout->addSpacing(4); |
| 120 |
vlayout->addWidget(d->m_pSubLabel); |
| 121 |
installEventFilter(this); |
| 122 |
// setFixedSize(rect().width(),74); |
| 123 |
d->m_pMainLabel->setAlignment(Qt::AlignLeft); |
| 124 |
d->m_pSubLabel->setAlignment(Qt::AlignLeft); |
| 125 |
d->changeTheme(); |
| 126 |
connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 127 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 128 |
{ |
| 129 |
d->changeTheme(); |
| 130 |
} |
| 131 |
}); |
| 132 |
} |
| 133 |
|
| 134 |
void KIntroWidget::setViewType(ViewType type) |
| 135 |
{ |
| 136 |
Q_D(KIntroWidget); |
| 137 |
d->m_viewType = type; |
| 138 |
if (d->m_viewType == ListView) |
| 139 |
setContentsMargins(22, 4, 16, 4); |
| 140 |
else |
| 141 |
setContentsMargins(22, d->m_topLeftRadius + 4, 16, 4); |
| 142 |
} |
| 143 |
|
| 144 |
void KIntroWidget::setMainLabel(QString str) |
| 145 |
{ |
| 146 |
Q_D(KIntroWidget); |
| 147 |
d->m_pMainLabel->setText(str); |
| 148 |
} |
| 149 |
|
| 150 |
void KIntroWidget::setSubLabel(QString str) |
| 151 |
{ |
| 152 |
Q_D(KIntroWidget); |
| 153 |
d->m_pSubLabel->setText(str); |
| 154 |
} |
| 155 |
|
| 156 |
void KIntroWidget::setBorderRadius(int radius) |
| 157 |
{ |
| 158 |
Q_D(KIntroWidget); |
| 159 |
d->m_bottomRightRadius = radius; |
| 160 |
d->m_topLeftRadius = radius; |
| 161 |
d->m_bottomLeftRadius = radius; |
| 162 |
d->m_topRightRadius = radius; |
| 163 |
if (d->m_viewType == ListView) |
| 164 |
setContentsMargins(22, 4, 16, 4); |
| 165 |
else |
| 166 |
setContentsMargins(22, d->m_topLeftRadius + 4, 16, 4); |
| 167 |
} |
| 168 |
|
| 169 |
void KIntroWidget::setBorderRadius(int bottomLeft, int topLeft, int topRight, int bottomRight) |
| 170 |
{ |
| 171 |
Q_D(KIntroWidget); |
| 172 |
d->m_bottomRightRadius = bottomRight; |
| 173 |
d->m_topLeftRadius = topLeft; |
| 174 |
d->m_bottomLeftRadius = bottomLeft; |
| 175 |
d->m_topRightRadius = topRight; |
| 176 |
if (d->m_viewType == ListView) |
| 177 |
setContentsMargins(22, 4, 16, 4); |
| 178 |
else |
| 179 |
setContentsMargins(22, d->m_topLeftRadius + 4, 16, 4); |
| 180 |
} |
| 181 |
|
| 182 |
void KIntroWidget::setTitleColor(QColor color) |
| 183 |
{ |
| 184 |
Q_D(KIntroWidget); |
| 185 |
d->m_titleColor = color; |
| 186 |
} |
| 187 |
|
| 188 |
void KIntroWidget::paintEvent(QPaintEvent *event) |
| 189 |
{ |
| 190 |
Q_D(KIntroWidget); |
| 191 |
QPainter painter(this); |
| 192 |
painter.setRenderHint(QPainter::Antialiasing); |
| 193 |
painter.setRenderHint(QPainter::SmoothPixmapTransform); |
| 194 |
painter.setPen(Qt::NoPen); |
| 195 |
QPainterPath path; |
| 196 |
QPainterPath path1; |
| 197 |
QPainterPath path2; |
| 198 |
QRect drawRect = this->rect(); |
| 199 |
path.moveTo(drawRect.topLeft() + QPoint(d->m_topLeftRadius, 0)); |
| 200 |
path.lineTo(drawRect.topRight() - QPoint(d->m_topRightRadius, 0)); |
| 201 |
path.quadTo(drawRect.topRight(), drawRect.topRight() + QPoint(0, d->m_topRightRadius)); |
| 202 |
path.lineTo(drawRect.bottomRight() - QPoint(0, d->m_bottomRightRadius)); |
| 203 |
path.quadTo(drawRect.bottomRight(), drawRect.bottomRight() - QPoint(d->m_bottomRightRadius, 0)); |
| 204 |
path.lineTo(drawRect.bottomLeft() + QPoint(d->m_bottomLeftRadius, 0)); |
| 205 |
path.quadTo(drawRect.bottomLeft(), drawRect.bottomLeft() - QPoint(0, d->m_bottomLeftRadius)); |
| 206 |
path.lineTo(drawRect.topLeft() + QPoint(0, d->m_topLeftRadius)); |
| 207 |
path.quadTo(drawRect.topLeft(), drawRect.topLeft() + QPoint(d->m_topLeftRadius, 0)); |
| 208 |
|
| 209 |
switch (d->m_viewType) { |
| 210 |
case ViewType::IconView: { |
| 211 |
// d->m_pMainLabel->setAlignment(Qt::AlignCenter); |
| 212 |
// d->m_pSubLabel->setAlignment(Qt::AlignCenter); |
| 213 |
d->m_pMainLabel->setWordWrap(true); |
| 214 |
d->m_pSubLabel->setWordWrap(true); |
| 215 |
path1.moveTo(drawRect.topLeft() + QPoint(d->m_topLeftRadius, 0)); |
| 216 |
path1.lineTo(drawRect.topRight() - QPoint(d->m_topRightRadius, 0)); |
| 217 |
path1.quadTo(drawRect.topRight(), drawRect.topRight() + QPoint(0, d->m_topRightRadius)); |
| 218 |
path1.lineTo(drawRect.topLeft() + QPoint(0, d->m_topLeftRadius)); |
| 219 |
path1.quadTo(drawRect.topLeft(), drawRect.topLeft() + QPoint(d->m_topLeftRadius, 0)); |
| 220 |
|
| 221 |
path2.moveTo(drawRect.topLeft() + QPoint(0, d->m_topLeftRadius)); |
| 222 |
path2.lineTo(drawRect.bottomLeft() - QPoint(0, d->m_bottomLeftRadius)); |
| 223 |
path2.quadTo(drawRect.bottomLeft(), drawRect.bottomLeft() + QPoint(d->m_bottomLeftRadius, 0)); |
| 224 |
path2.lineTo(drawRect.bottomRight() - QPoint(d->m_bottomRightRadius, 0)); |
| 225 |
path2.quadTo(drawRect.bottomRight(), drawRect.bottomRight() - QPoint(0, d->m_bottomRightRadius)); |
| 226 |
path2.lineTo(drawRect.topRight() + QPoint(0, d->m_topRightRadius)); |
| 227 |
path2.lineTo(drawRect.topLeft() + QPoint(0, d->m_topLeftRadius)); |
| 228 |
} break; |
| 229 |
case ViewType::ListView: { |
| 230 |
|
| 231 |
d->m_pMainLabel->setWordWrap(false); |
| 232 |
d->m_pSubLabel->setWordWrap(false); |
| 233 |
path1.moveTo(drawRect.topLeft() + QPoint(d->m_topLeftRadius, 0)); |
| 234 |
path1.lineTo(drawRect.bottomLeft() + QPoint(d->m_bottomLeftRadius, 0)); |
| 235 |
path1.quadTo(drawRect.bottomLeft(), drawRect.bottomLeft() - QPoint(0, d->m_bottomLeftRadius)); |
| 236 |
path1.lineTo(drawRect.topLeft() + QPoint(0, d->m_topLeftRadius)); |
| 237 |
path1.quadTo(drawRect.topLeft(), drawRect.topLeft() + QPoint(d->m_topLeftRadius, 0)); |
| 238 |
|
| 239 |
path2.moveTo(drawRect.topLeft() + QPoint(d->m_topLeftRadius, 0)); |
| 240 |
path2.lineTo(drawRect.topRight() - QPoint(d->m_topLeftRadius, 0)); |
| 241 |
path2.quadTo(drawRect.topRight(), drawRect.topRight() + QPoint(0, d->m_topRightRadius)); |
| 242 |
path2.lineTo(drawRect.bottomRight() - QPoint(0, d->m_bottomRightRadius)); |
| 243 |
path2.quadTo(drawRect.bottomRight(), drawRect.bottomRight() - QPoint(d->m_bottomRightRadius, 0)); |
| 244 |
path2.lineTo(drawRect.bottomLeft() + QPoint(d->m_bottomLeftRadius, 0)); |
| 245 |
path2.lineTo(drawRect.topLeft() + QPoint(d->m_topLeftRadius, 0)); |
| 246 |
|
| 247 |
} break; |
| 248 |
default: |
| 249 |
break; |
| 250 |
} |
| 251 |
painter.setPen(d->m_borderColor); |
| 252 |
painter.drawPath(path); |
| 253 |
|
| 254 |
painter.setPen(Qt::NoPen); |
| 255 |
painter.setBrush(d->m_titleColor); |
| 256 |
painter.drawPath(path1); |
| 257 |
painter.setBrush(d->m_bkgColor); |
| 258 |
painter.drawPath(path2); |
| 259 |
} |
| 260 |
|
| 261 |
bool KIntroWidget::eventFilter(QObject *watched, QEvent *event) |
| 262 |
{ |
| 263 |
Q_D(KIntroWidget); |
| 264 |
if (watched == this) { |
| 265 |
|
| 266 |
switch (event->type()) { |
| 267 |
case QEvent::Enter: |
| 268 |
d->m_bkgColor = ThemeController::mixColor(ThemeController::getCustomColorFromDT("tooltipbase-active"), d->m_mixColor, 0.05); |
| 269 |
update(); |
| 270 |
break; |
| 271 |
case QEvent::Leave: |
| 272 |
d->m_bkgColor = ThemeController::getCustomColorFromDT("tooltipbase-active"); |
| 273 |
update(); |
| 274 |
break; |
| 275 |
case QEvent::MouseButtonPress: |
| 276 |
d->m_bkgColor = ThemeController::mixColor(ThemeController::getCustomColorFromDT("tooltipbase-active"), d->m_mixColor, 0.1); |
| 277 |
update(); |
| 278 |
break; |
| 279 |
case QEvent::MouseButtonRelease: |
| 280 |
d->m_bkgColor = ThemeController::mixColor(ThemeController::getCustomColorFromDT("tooltipbase-active"), d->m_mixColor, 0.05); |
| 281 |
update(); |
| 282 |
break; |
| 283 |
default: |
| 284 |
break; |
| 285 |
} |
| 286 |
} |
| 287 |
return QWidget::eventFilter(watched, event); |
| 288 |
} |
| 289 |
|
| 290 |
QSize KIntroWidget::sizeHint() const |
| 291 |
{ |
| 292 |
Q_D(const KIntroWidget); |
| 293 |
return QWidget::sizeHint(); |
| 294 |
} |
| 295 |
|
| 296 |
void KIntroWidget::mousePressEvent(QMouseEvent *event) |
| 297 |
{ |
| 298 |
Q_D(KIntroWidget); |
| 299 |
if (event->button() == Qt::MouseButton::RightButton) { |
| 300 |
emit rightClick(event->globalPos()); |
| 301 |
} |
| 302 |
return QWidget::mousePressEvent(event); |
| 303 |
} |
| 304 |
|
| 305 |
void KIntroWidget::mouseReleaseEvent(QMouseEvent *event) |
| 306 |
{ |
| 307 |
Q_D(KIntroWidget); |
| 308 |
return QWidget::mouseReleaseEvent(event); |
| 309 |
} |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
#include "kintrowidget.moc" |
| 314 |
#include "moc_kintrowidget.cpp" |