| 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 "kbuttonbox.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QBoxLayout> |
| 26 |
#include <QButtonGroup> |
| 27 |
#include <QColor> |
| 28 |
#include <QGraphicsDropShadowEffect> |
| 29 |
#include <QPainter> |
| 30 |
#include <QStyleOptionButton> |
| 31 |
#include <QStylePainter> |
| 32 |
#include "accessinfohelper.h" |
| 33 |
|
| 34 |
namespace kdk |
| 35 |
{ |
| 36 |
class KButtonBoxPrivate : public QObject, public ThemeController |
| 37 |
{ |
| 38 |
Q_OBJECT |
| 39 |
Q_DECLARE_PUBLIC(KButtonBox) |
| 40 |
public: |
| 41 |
KButtonBoxPrivate(KButtonBox *parent); |
| 42 |
void updateBorderRadius(); |
| 43 |
void updateButtonList(); |
| 44 |
|
| 45 |
protected: |
| 46 |
void changeTheme(); |
| 47 |
|
| 48 |
private: |
| 49 |
KButtonBox *q_ptr; |
| 50 |
QButtonGroup *m_buttonGroup; |
| 51 |
QBoxLayout *m_layout; |
| 52 |
int m_radius; |
| 53 |
bool m_isCheckable; |
| 54 |
bool m_shadowFlag; |
| 55 |
QList<KPushButton *> m_buttonList; |
| 56 |
QGraphicsDropShadowEffect *m_pShadow_effect; |
| 57 |
}; |
| 58 |
|
| 59 |
KButtonBoxPrivate::KButtonBoxPrivate(KButtonBox *parent) |
| 60 |
: q_ptr(parent) |
| 61 |
{ |
| 62 |
Q_Q(KButtonBox); |
| 63 |
m_buttonGroup = new QButtonGroup(q); |
| 64 |
q->connect(m_buttonGroup, SIGNAL(buttonClicked(QAbstractButton *)), q, SIGNAL(buttonClicked(QAbstractButton *))); |
| 65 |
q->connect(m_buttonGroup, SIGNAL(buttonPressed(QAbstractButton *)), q, SIGNAL(buttonPressed(QAbstractButton *))); |
| 66 |
q->connect(m_buttonGroup, SIGNAL(buttonReleased(QAbstractButton *)), q, SIGNAL(buttonReleased(QAbstractButton *))); |
| 67 |
q->connect(m_buttonGroup, SIGNAL(buttonToggled(QAbstractButton *, bool)), q, SIGNAL(buttonToggled(QAbstractButton *, bool))); |
| 68 |
q->connect(m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 69 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 70 |
{ |
| 71 |
changeTheme(); |
| 72 |
} |
| 73 |
}); |
| 74 |
|
| 75 |
m_layout = new QHBoxLayout(q); |
| 76 |
m_layout->setSizeConstraint(QLayout::SetFixedSize); |
| 77 |
m_layout->setContentsMargins(0, 0, 0, 0); |
| 78 |
m_layout->setSpacing(0); |
| 79 |
m_radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 80 |
if (m_radius == -1) |
| 81 |
m_radius = 6; |
| 82 |
m_isCheckable = false; |
| 83 |
m_shadowFlag = false; |
| 84 |
m_pShadow_effect = new QGraphicsDropShadowEffect(); |
| 85 |
|
| 86 |
KDK_OBJ_INFO_FORMAT(m_buttonGroup); |
| 87 |
KDK_OBJ_INFO_FORMAT(m_pShadow_effect); |
| 88 |
} |
| 89 |
|
| 90 |
void KButtonBoxPrivate::updateBorderRadius() |
| 91 |
{ |
| 92 |
Q_Q(KButtonBox); |
| 93 |
QList<KPushButton *> buttonList = q->buttonList(); |
| 94 |
switch (q->orientation()) { |
| 95 |
case Qt::Horizontal: { |
| 96 |
for (int i = 0; i < buttonList.count(); i++) { |
| 97 |
KPushButton *button = buttonList.at(i); |
| 98 |
if (i == 0) { |
| 99 |
Q_ASSERT(button); |
| 100 |
button->setBorderRadius(m_radius, m_radius, 0, 0); |
| 101 |
} else if (i == buttonList.count() - 1) { |
| 102 |
Q_ASSERT(button); |
| 103 |
button->setBorderRadius(0, 0, m_radius, m_radius); |
| 104 |
} else { |
| 105 |
Q_ASSERT(button); |
| 106 |
button->setBorderRadius(0); |
| 107 |
} |
| 108 |
} |
| 109 |
break; |
| 110 |
} |
| 111 |
case Qt::Vertical: { |
| 112 |
for (int i = 0; i < buttonList.count(); i++) { |
| 113 |
KPushButton *button = buttonList.at(i); |
| 114 |
if (i == 0) { |
| 115 |
Q_ASSERT(button); |
| 116 |
button->setBorderRadius(0, m_radius, m_radius, 0); |
| 117 |
} else if (i == buttonList.count() - 1) { |
| 118 |
Q_ASSERT(button); |
| 119 |
button->setBorderRadius(m_radius, 0, 0, m_radius); |
| 120 |
} else { |
| 121 |
Q_ASSERT(button); |
| 122 |
button->setBorderRadius(0); |
| 123 |
} |
| 124 |
} |
| 125 |
break; |
| 126 |
} |
| 127 |
default: |
| 128 |
break; |
| 129 |
} |
| 130 |
q->update(); |
| 131 |
} |
| 132 |
|
| 133 |
void KButtonBoxPrivate::updateButtonList() |
| 134 |
{ |
| 135 |
Q_Q(KButtonBox); |
| 136 |
|
| 137 |
QColor shadowColor; |
| 138 |
if (ThemeController::themeMode() == LightTheme) |
| 139 |
shadowColor = ThemeController::getCustomColorFromDT("kblack"); |
| 140 |
else |
| 141 |
shadowColor = ThemeController::getCustomColorFromDT("kwhite"); |
| 142 |
shadowColor.setAlphaF(0.1); |
| 143 |
for (QAbstractButton *button : m_buttonGroup->buttons()) { |
| 144 |
m_buttonGroup->removeButton(button); |
| 145 |
m_layout->removeWidget(button); |
| 146 |
} |
| 147 |
for (int i = 0; i < m_buttonList.count(); ++i) { |
| 148 |
KPushButton *button = m_buttonList.at(i); |
| 149 |
button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); |
| 150 |
m_layout->insertWidget(i, button); |
| 151 |
m_buttonGroup->addButton(button, i); |
| 152 |
button->setCheckable(m_isCheckable); |
| 153 |
if (m_shadowFlag) { |
| 154 |
m_pShadow_effect->setOffset(0, 2); |
| 155 |
m_pShadow_effect->setColor(shadowColor); |
| 156 |
m_pShadow_effect->setBlurRadius(4); |
| 157 |
q->setGraphicsEffect(m_pShadow_effect); |
| 158 |
} |
| 159 |
} |
| 160 |
updateBorderRadius(); |
| 161 |
} |
| 162 |
|
| 163 |
void KButtonBoxPrivate::changeTheme() |
| 164 |
{ |
| 165 |
Q_Q(KButtonBox); |
| 166 |
initThemeStyle(); |
| 167 |
|
| 168 |
if (ThemeController::themeMode() == LightTheme) { |
| 169 |
QColor shadowColor(ThemeController::getCustomColorFromDT("kblack")); |
| 170 |
shadowColor.setAlphaF(0.1); |
| 171 |
if (m_shadowFlag) { |
| 172 |
m_pShadow_effect->setOffset(0, 2); |
| 173 |
m_pShadow_effect->setColor(shadowColor); |
| 174 |
m_pShadow_effect->setBlurRadius(4); |
| 175 |
q->setGraphicsEffect(m_pShadow_effect); |
| 176 |
} |
| 177 |
} else { |
| 178 |
QColor shadowColor(255, 255, 255); |
| 179 |
shadowColor.setAlphaF(0.1); |
| 180 |
if (m_shadowFlag) { |
| 181 |
m_pShadow_effect->setOffset(0, 2); |
| 182 |
m_pShadow_effect->setColor(shadowColor); |
| 183 |
m_pShadow_effect->setBlurRadius(4); |
| 184 |
q->setGraphicsEffect(m_pShadow_effect); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
KButtonBox::KButtonBox(QWidget *parent) |
| 190 |
: QWidget(parent) |
| 191 |
, d_ptr(new KButtonBoxPrivate(this)) |
| 192 |
{ |
| 193 |
Q_D(KButtonBox); |
| 194 |
} |
| 195 |
|
| 196 |
KButtonBox::~KButtonBox() |
| 197 |
{ |
| 198 |
} |
| 199 |
|
| 200 |
Qt::Orientation KButtonBox::orientation() |
| 201 |
{ |
| 202 |
Q_D(KButtonBox); |
| 203 |
QBoxLayout::Direction layoutDirection = d->m_layout->direction(); |
| 204 |
if (layoutDirection == QBoxLayout::LeftToRight || layoutDirection == QBoxLayout::RightToLeft) { |
| 205 |
return Qt::Horizontal; |
| 206 |
} |
| 207 |
return Qt::Vertical; |
| 208 |
} |
| 209 |
|
| 210 |
void KButtonBox::setOrientation(Qt::Orientation orientation) |
| 211 |
{ |
| 212 |
Q_D(KButtonBox); |
| 213 |
if (orientation == Qt::Vertical) { |
| 214 |
d->m_layout->setDirection(QBoxLayout::TopToBottom); |
| 215 |
d->updateBorderRadius(); |
| 216 |
return; |
| 217 |
} |
| 218 |
d->m_layout->setDirection(QBoxLayout::LeftToRight); |
| 219 |
d->updateBorderRadius(); |
| 220 |
} |
| 221 |
|
| 222 |
void KButtonBox::addButton(KPushButton *button, int i) |
| 223 |
{ |
| 224 |
Q_D(KButtonBox); |
| 225 |
if (i < -1) |
| 226 |
return; |
| 227 |
|
| 228 |
if (i == -1 || i >= d->m_buttonList.count()) { |
| 229 |
d->m_buttonList.append(button); |
| 230 |
} else { |
| 231 |
d->m_buttonList.insert(i, button); |
| 232 |
} |
| 233 |
|
| 234 |
button->show(); |
| 235 |
|
| 236 |
setButtonList(d->m_buttonList); |
| 237 |
} |
| 238 |
|
| 239 |
void KButtonBox::removeButton(KPushButton *button) |
| 240 |
{ |
| 241 |
Q_D(KButtonBox); |
| 242 |
if (d->m_buttonList.contains(button)) { |
| 243 |
d->m_buttonList.removeAll(button); |
| 244 |
button->hide(); |
| 245 |
} |
| 246 |
setButtonList(d->m_buttonList); |
| 247 |
} |
| 248 |
|
| 249 |
void KButtonBox::removeButton(int i) |
| 250 |
{ |
| 251 |
Q_D(KButtonBox); |
| 252 |
if (i < 0 || i >= d->m_buttonList.count()) |
| 253 |
return; |
| 254 |
auto button = d->m_buttonList.at(i); |
| 255 |
if (button) |
| 256 |
button->hide(); |
| 257 |
d->m_buttonList.removeAt(i); |
| 258 |
setButtonList(d->m_buttonList); |
| 259 |
} |
| 260 |
|
| 261 |
void KButtonBox::setButtonList(const QList<KPushButton *> &list) |
| 262 |
{ |
| 263 |
Q_D(KButtonBox); |
| 264 |
d->m_buttonList = list; |
| 265 |
d->updateButtonList(); |
| 266 |
} |
| 267 |
|
| 268 |
QList<KPushButton *> KButtonBox::buttonList() |
| 269 |
{ |
| 270 |
Q_D(KButtonBox); |
| 271 |
return d->m_buttonList; |
| 272 |
} |
| 273 |
|
| 274 |
void KButtonBox::setBorderRadius(int radius) |
| 275 |
{ |
| 276 |
Q_D(KButtonBox); |
| 277 |
d->m_radius = radius; |
| 278 |
update(); |
| 279 |
} |
| 280 |
|
| 281 |
int KButtonBox::borderRadius() |
| 282 |
{ |
| 283 |
Q_D(KButtonBox); |
| 284 |
return d->m_radius; |
| 285 |
} |
| 286 |
|
| 287 |
void KButtonBox::setId(KPushButton *button, int id) |
| 288 |
{ |
| 289 |
Q_D(KButtonBox); |
| 290 |
d->m_buttonGroup->setId(button, id); |
| 291 |
} |
| 292 |
|
| 293 |
int KButtonBox::id(KPushButton *button) |
| 294 |
{ |
| 295 |
Q_D(KButtonBox); |
| 296 |
return d->m_buttonGroup->id(button); |
| 297 |
} |
| 298 |
|
| 299 |
KPushButton *KButtonBox::checkedButton() |
| 300 |
{ |
| 301 |
Q_D(KButtonBox); |
| 302 |
KPushButton *button = dynamic_cast<KPushButton *>(d->m_buttonGroup->checkedButton()); |
| 303 |
return button; |
| 304 |
} |
| 305 |
|
| 306 |
KPushButton *KButtonBox::button(int id) |
| 307 |
{ |
| 308 |
Q_D(KButtonBox); |
| 309 |
KPushButton *button = dynamic_cast<KPushButton *>(d->m_buttonGroup->button(id)); |
| 310 |
return button; |
| 311 |
} |
| 312 |
|
| 313 |
int KButtonBox::checkedId() |
| 314 |
{ |
| 315 |
Q_D(KButtonBox); |
| 316 |
return d->m_buttonGroup->checkedId(); |
| 317 |
} |
| 318 |
|
| 319 |
void KButtonBox::setExclusive(bool exclusive) |
| 320 |
{ |
| 321 |
Q_D(KButtonBox); |
| 322 |
d->m_buttonGroup->setExclusive(exclusive); |
| 323 |
} |
| 324 |
|
| 325 |
bool KButtonBox::exclusive() |
| 326 |
{ |
| 327 |
Q_D(KButtonBox); |
| 328 |
return d->m_buttonGroup->exclusive(); |
| 329 |
} |
| 330 |
|
| 331 |
void KButtonBox::setCheckable(bool flag) |
| 332 |
{ |
| 333 |
Q_D(KButtonBox); |
| 334 |
d->m_isCheckable = flag; |
| 335 |
QList<KPushButton *> list = buttonList(); |
| 336 |
for (int i = 0; i < list.count(); ++i) { |
| 337 |
KPushButton *button = list.at(i); |
| 338 |
button->setCheckable(d->m_isCheckable); |
| 339 |
} |
| 340 |
update(); |
| 341 |
} |
| 342 |
|
| 343 |
bool KButtonBox::isCheckable() |
| 344 |
{ |
| 345 |
Q_D(KButtonBox); |
| 346 |
return d->m_isCheckable; |
| 347 |
} |
| 348 |
|
| 349 |
void KButtonBox::setShadow(bool flag) |
| 350 |
{ |
| 351 |
Q_D(KButtonBox); |
| 352 |
QColor shadowColor(0, 0, 0); |
| 353 |
shadowColor.setAlphaF(0.1); |
| 354 |
d->m_shadowFlag = flag; |
| 355 |
if (flag) { |
| 356 |
d->updateButtonList(); |
| 357 |
} else { |
| 358 |
for (int i = 0; i < d->m_buttonList.count(); ++i) { |
| 359 |
KPushButton *button = d->m_buttonList.at(i); |
| 360 |
d->m_pShadow_effect->setOffset(0, 2); |
| 361 |
d->m_pShadow_effect->setColor(Qt::NoBrush); |
| 362 |
d->m_pShadow_effect->setBlurRadius(4); |
| 363 |
button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); |
| 364 |
setGraphicsEffect(d->m_pShadow_effect); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
bool KButtonBox::shadow() |
| 370 |
{ |
| 371 |
Q_D(KButtonBox); |
| 372 |
return d->m_shadowFlag; |
| 373 |
} |
| 374 |
|
| 375 |
} |
| 376 |
#include "kbuttonbox.moc" |
| 377 |
#include "moc_kbuttonbox.cpp" |