| 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 "klineedit.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QBoxLayout> |
| 26 |
#include <QDebug> |
| 27 |
#include <QEvent> |
| 28 |
#include <QMouseEvent> |
| 29 |
#include <QPainter> |
| 30 |
#include <QStyleOptionButton> |
| 31 |
|
| 32 |
namespace kdk |
| 33 |
{ |
| 34 |
|
| 35 |
class Q_DECL_HIDDEN PushButton : public QPushButton, public ThemeController |
| 36 |
{ |
| 37 |
Q_OBJECT |
| 38 |
public: |
| 39 |
PushButton(QWidget *parent = nullptr); |
| 40 |
|
| 41 |
void setFocusStyle(bool flag); |
| 42 |
|
| 43 |
protected: |
| 44 |
void paintEvent(QPaintEvent *event); |
| 45 |
void mouseReleaseEvent(QMouseEvent *event); |
| 46 |
void mousePressEvent(QMouseEvent *event); |
| 47 |
void leaveEvent(QEvent *e); |
| 48 |
void enterEvent(QEnterEvent *e); |
| 49 |
|
| 50 |
private: |
| 51 |
bool m_pressEnter; |
| 52 |
bool m_mouseEnter; |
| 53 |
bool m_foucusStyle; |
| 54 |
QLinearGradient m_linearGradient ; |
| 55 |
}; |
| 56 |
|
| 57 |
class Q_DECL_HIDDEN KLineEditPrivate : public QObject, public ThemeController |
| 58 |
{ |
| 59 |
Q_OBJECT |
| 60 |
Q_DECLARE_PUBLIC(KLineEdit) |
| 61 |
public: |
| 62 |
KLineEditPrivate(KLineEdit *parent); |
| 63 |
void adjustLayout(); |
| 64 |
|
| 65 |
protected: |
| 66 |
void changeTheme() override; |
| 67 |
|
| 68 |
private: |
| 69 |
KLineEdit *q_ptr; |
| 70 |
QWidget *m_pWidget; |
| 71 |
PushButton *m_pPushButton; |
| 72 |
}; |
| 73 |
|
| 74 |
KLineEdit::KLineEdit(QWidget *parent) |
| 75 |
: QLineEdit(parent) |
| 76 |
, d_ptr(new KLineEditPrivate(this)) |
| 77 |
{ |
| 78 |
installEventFilter(this); |
| 79 |
setFocusPolicy(Qt::StrongFocus); |
| 80 |
clearFocus(); |
| 81 |
connect(this, &KLineEdit::returnPressed, this, &KLineEdit::clearFocus); |
| 82 |
} |
| 83 |
|
| 84 |
QPushButton *KLineEdit::pushbutton() |
| 85 |
{ |
| 86 |
Q_D(KLineEdit); |
| 87 |
return d->m_pPushButton; |
| 88 |
} |
| 89 |
|
| 90 |
void KLineEdit::resizeEvent(QResizeEvent *event) |
| 91 |
{ |
| 92 |
Q_D(KLineEdit); |
| 93 |
QLineEdit::resizeEvent(event); |
| 94 |
d->adjustLayout(); |
| 95 |
} |
| 96 |
|
| 97 |
bool KLineEdit::eventFilter(QObject *watched, QEvent *event) |
| 98 |
{ |
| 99 |
Q_D(KLineEdit); |
| 100 |
if (watched == this) { |
| 101 |
switch (event->type()) { |
| 102 |
case QEvent::FocusIn: |
| 103 |
d->m_pPushButton->setFocusStyle(true); |
| 104 |
break; |
| 105 |
case QEvent::FocusOut: |
| 106 |
d->m_pPushButton->setFocusStyle(false); |
| 107 |
break; |
| 108 |
default: |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
return QLineEdit::eventFilter(watched, event); |
| 113 |
} |
| 114 |
|
| 115 |
void PushButton::mouseReleaseEvent(QMouseEvent *event) |
| 116 |
{ |
| 117 |
m_pressEnter = false; |
| 118 |
m_mouseEnter = true; |
| 119 |
QPushButton::mouseReleaseEvent(event); |
| 120 |
} |
| 121 |
|
| 122 |
void PushButton::mousePressEvent(QMouseEvent *event) |
| 123 |
{ |
| 124 |
m_pressEnter = true; |
| 125 |
m_mouseEnter = false; |
| 126 |
QPushButton::mousePressEvent(event); |
| 127 |
} |
| 128 |
|
| 129 |
void PushButton::leaveEvent(QEvent *e) |
| 130 |
{ |
| 131 |
m_mouseEnter = false; |
| 132 |
QPushButton::leaveEvent(e); |
| 133 |
} |
| 134 |
|
| 135 |
void PushButton::enterEvent(QEnterEvent *e) |
| 136 |
{ |
| 137 |
m_mouseEnter = true; |
| 138 |
QPushButton::enterEvent(e); |
| 139 |
} |
| 140 |
|
| 141 |
KLineEditPrivate::KLineEditPrivate(KLineEdit *parent) |
| 142 |
: q_ptr(parent) |
| 143 |
{ |
| 144 |
Q_Q(KLineEdit); |
| 145 |
m_pPushButton = new PushButton(); |
| 146 |
m_pPushButton->setFixedSize(24, 24); |
| 147 |
m_pWidget = new QWidget(q); |
| 148 |
QHBoxLayout *hlayout = new QHBoxLayout(m_pWidget); |
| 149 |
hlayout->setContentsMargins(0, 0, 0, 0); |
| 150 |
hlayout->addWidget(m_pPushButton); |
| 151 |
connect(m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 152 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 153 |
{ |
| 154 |
changeTheme(); |
| 155 |
} |
| 156 |
}); |
| 157 |
} |
| 158 |
|
| 159 |
void KLineEditPrivate::adjustLayout() |
| 160 |
{ |
| 161 |
Q_Q(KLineEdit); |
| 162 |
int spacing = 5; |
| 163 |
int width = spacing; |
| 164 |
if (!m_pPushButton->isHidden()) |
| 165 |
width += (spacing + m_pPushButton->width()); |
| 166 |
m_pWidget->setFixedSize(width, q->height()); |
| 167 |
m_pWidget->move(q->width() - m_pWidget->width(), (q->rect().height() - m_pWidget->height()) / 2); |
| 168 |
q->setTextMargins(0, 0, m_pWidget->width(), 0); |
| 169 |
} |
| 170 |
|
| 171 |
void KLineEditPrivate::changeTheme() |
| 172 |
{ |
| 173 |
initThemeStyle(); |
| 174 |
} |
| 175 |
|
| 176 |
PushButton::PushButton(QWidget *parent) |
| 177 |
{ |
| 178 |
// connect(m_ge) |
| 179 |
m_pressEnter = false; |
| 180 |
m_mouseEnter = false; |
| 181 |
m_foucusStyle = false; |
| 182 |
} |
| 183 |
|
| 184 |
void PushButton::setFocusStyle(bool flag) |
| 185 |
{ |
| 186 |
m_foucusStyle = flag; |
| 187 |
if (flag) |
| 188 |
setFocusPolicy(Qt::NoFocus); |
| 189 |
else |
| 190 |
setFocusPolicy(Qt::StrongFocus); |
| 191 |
} |
| 192 |
|
| 193 |
void PushButton::paintEvent(QPaintEvent *event) |
| 194 |
{ |
| 195 |
QStyleOptionButton option; |
| 196 |
initStyleOption(&option); |
| 197 |
QPainter p(this); |
| 198 |
p.setRenderHint(QPainter::Antialiasing); |
| 199 |
p.setRenderHint(QPainter::TextAntialiasing); |
| 200 |
p.setRenderHint(QPainter::SmoothPixmapTransform); |
| 201 |
p.setPen(Qt::NoPen); |
| 202 |
QPixmap pixmap = icon().pixmap(iconSize()); |
| 203 |
|
| 204 |
m_linearGradient = QLinearGradient(rect().left() + rect().width()/2, rect().top(),rect().left() + rect().width()/2, rect().bottom()); |
| 205 |
QColor startColor; |
| 206 |
QColor endColor; |
| 207 |
if (m_foucusStyle) { |
| 208 |
if (ThemeController::themeMode() == DarkTheme) |
| 209 |
pixmap = ThemeController::drawSymbolicColoredPixmap(pixmap); |
| 210 |
if (m_mouseEnter || m_pressEnter) { |
| 211 |
pixmap = ThemeController::drawSymbolicColoredPixmap(pixmap); |
| 212 |
m_linearGradient.setColorAt(0, ThemeController::getCustomColorFromDT("highlight-active")); |
| 213 |
m_linearGradient.setColorAt(1, ThemeController::getCustomColorFromDT("highlight-active")); |
| 214 |
p.setBrush(m_linearGradient); |
| 215 |
} else |
| 216 |
{ |
| 217 |
p.setBrush(Qt::NoBrush); |
| 218 |
} |
| 219 |
} else { |
| 220 |
if (ThemeController::themeMode() == DarkTheme) |
| 221 |
pixmap = ThemeController::drawSymbolicColoredPixmap(pixmap); |
| 222 |
if (m_mouseEnter) { |
| 223 |
ThemeController::getGradientFromDT("kcomponent-hover",startColor,endColor); |
| 224 |
m_linearGradient.setColorAt(0,startColor); |
| 225 |
m_linearGradient.setColorAt(1,endColor); |
| 226 |
p.setBrush(m_linearGradient); |
| 227 |
} |
| 228 |
else if (m_pressEnter) |
| 229 |
{ |
| 230 |
ThemeController::getGradientFromDT("kcomponent-click",startColor,endColor); |
| 231 |
m_linearGradient.setColorAt(0,startColor); |
| 232 |
m_linearGradient.setColorAt(1,endColor); |
| 233 |
p.setBrush(m_linearGradient); |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
p.setBrush(Qt::NoBrush); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 242 |
if (radius == -1) |
| 243 |
radius = 6; |
| 244 |
p.drawRoundedRect(rect(), radius, radius); |
| 245 |
|
| 246 |
QRect iconRect((rect().width() - iconSize().width()) / 2, (rect().height() - iconSize().height()) / 2, iconSize().width(), iconSize().height()); |
| 247 |
p.drawPixmap(iconRect, pixmap); |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
#include "klineedit.moc" |
| 253 |
#include "moc_klineedit.cpp" |