| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2024, 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 "ktoolbar.h" |
| 24 |
#include "klineframe.h" |
| 25 |
#include "themeController.h" |
| 26 |
#include <QBoxLayout> |
| 27 |
#include <QDebug> |
| 28 |
#include <QPainter> |
| 29 |
|
| 30 |
namespace kdk |
| 31 |
{ |
| 32 |
|
| 33 |
class KToolBarPrivate : public QObject, public ThemeController |
| 34 |
{ |
| 35 |
Q_DECLARE_PUBLIC(KToolBar) |
| 36 |
Q_OBJECT |
| 37 |
public: |
| 38 |
KToolBarPrivate(KToolBar *parent); |
| 39 |
|
| 40 |
void init(); |
| 41 |
void updateButtonLayout(); |
| 42 |
void clearButtonLayout(); |
| 43 |
void getSelectList(KToolButton *button, bool checked); |
| 44 |
|
| 45 |
private: |
| 46 |
KToolBar *q_ptr; |
| 47 |
QBoxLayout *m_pLayout; |
| 48 |
QList<KToolButton *> m_pButtonList; |
| 49 |
QList<KToolButton *> m_pCheckList; |
| 50 |
int m_radius; |
| 51 |
bool m_exclusive; |
| 52 |
bool m_isCheckable; |
| 53 |
bool m_isLineVisible; |
| 54 |
bool m_isBkgColorEnable; |
| 55 |
}; |
| 56 |
|
| 57 |
KToolBar::KToolBar(QWidget *parent) |
| 58 |
: QWidget(parent) |
| 59 |
, d_ptr(new KToolBarPrivate(this)) |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
Qt::Orientation KToolBar::orientation() |
| 64 |
{ |
| 65 |
Q_D(KToolBar); |
| 66 |
QBoxLayout::Direction layoutDirection = d->m_pLayout->direction(); |
| 67 |
if (layoutDirection == QBoxLayout::LeftToRight || layoutDirection == QBoxLayout::RightToLeft) { |
| 68 |
return Qt::Horizontal; |
| 69 |
} |
| 70 |
return Qt::Vertical; |
| 71 |
} |
| 72 |
|
| 73 |
void KToolBar::setOrientation(Qt::Orientation orientation) |
| 74 |
{ |
| 75 |
Q_D(KToolBar); |
| 76 |
if (orientation == Qt::Vertical) |
| 77 |
d->m_pLayout->setDirection(QBoxLayout::TopToBottom); |
| 78 |
else |
| 79 |
d->m_pLayout->setDirection(QBoxLayout::LeftToRight); |
| 80 |
d->updateButtonLayout(); |
| 81 |
} |
| 82 |
|
| 83 |
KToolButton *KToolBar::addButton(QIcon icon) |
| 84 |
{ |
| 85 |
Q_D(KToolBar); |
| 86 |
KToolButton *button = new KToolButton(this); |
| 87 |
button->setType(KToolButtonType::Frameless); |
| 88 |
button->setIcon(icon); |
| 89 |
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
| 90 |
d->m_pButtonList.append(button); |
| 91 |
d->updateButtonLayout(); |
| 92 |
return button; |
| 93 |
} |
| 94 |
|
| 95 |
KToolButton *KToolBar::addButton(QString string) |
| 96 |
{ |
| 97 |
Q_D(KToolBar); |
| 98 |
KToolButton *button = new KToolButton(this); |
| 99 |
button->setType(KToolButtonType::Frameless); |
| 100 |
button->setText(string); |
| 101 |
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
| 102 |
d->m_pButtonList.append(button); |
| 103 |
d->updateButtonLayout(); |
| 104 |
return button; |
| 105 |
} |
| 106 |
|
| 107 |
QList<KToolButton *> KToolBar::addButton(QList<QIcon> list) |
| 108 |
{ |
| 109 |
Q_D(KToolBar); |
| 110 |
QList<KToolButton *> btnList; |
| 111 |
for (int i = 0; i < list.count(); i++) { |
| 112 |
KToolButton *button = new KToolButton(this); |
| 113 |
button->setType(KToolButtonType::Frameless); |
| 114 |
button->setIcon(list.at(i)); |
| 115 |
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
| 116 |
d->m_pButtonList.append(button); |
| 117 |
btnList.append(button); |
| 118 |
} |
| 119 |
d->updateButtonLayout(); |
| 120 |
return btnList; |
| 121 |
} |
| 122 |
|
| 123 |
QList<KToolButton *> KToolBar::addButton(QList<QString> list) |
| 124 |
{ |
| 125 |
Q_D(KToolBar); |
| 126 |
QList<KToolButton *> btnList; |
| 127 |
for (int i = 0; i < list.count(); i++) { |
| 128 |
KToolButton *button = new KToolButton(this); |
| 129 |
button->setType(KToolButtonType::Frameless); |
| 130 |
button->setText(list.at(i)); |
| 131 |
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
| 132 |
d->m_pButtonList.append(button); |
| 133 |
btnList.append(button); |
| 134 |
} |
| 135 |
d->updateButtonLayout(); |
| 136 |
return btnList; |
| 137 |
} |
| 138 |
|
| 139 |
// void KToolBar::addButton(KToolButton *button) |
| 140 |
//{ |
| 141 |
// Q_D(KToolBar); |
| 142 |
// if(!d->m_pButtonList.contains(button)) |
| 143 |
// { |
| 144 |
// d->m_pButtonList.append(button); |
| 145 |
// d->updateButtonLayout(); |
| 146 |
// } |
| 147 |
// } |
| 148 |
|
| 149 |
// void KToolBar::addButton(QList<KToolButton *> list) |
| 150 |
//{ |
| 151 |
// Q_D(KToolBar); |
| 152 |
// for(int i = 0 ; i < list.count() ; i++) |
| 153 |
// { |
| 154 |
// if(!d->m_pButtonList.contains(list.at(i))) |
| 155 |
// { |
| 156 |
// d->m_pButtonList.append(list.at(i)); |
| 157 |
// } |
| 158 |
// } |
| 159 |
// d->updateButtonLayout(); |
| 160 |
// } |
| 161 |
|
| 162 |
QList<KToolButton *> KToolBar::buttonList() |
| 163 |
{ |
| 164 |
Q_D(KToolBar); |
| 165 |
return d->m_pButtonList; |
| 166 |
} |
| 167 |
|
| 168 |
void KToolBar::removeButton(KToolButton *button) |
| 169 |
{ |
| 170 |
Q_D(KToolBar); |
| 171 |
if (d->m_pButtonList.contains(button)) { |
| 172 |
d->m_pButtonList.removeAll(button); |
| 173 |
} |
| 174 |
d->updateButtonLayout(); |
| 175 |
} |
| 176 |
|
| 177 |
void KToolBar::removeButton(int id) |
| 178 |
{ |
| 179 |
Q_D(KToolBar); |
| 180 |
if (id < 0 || id >= d->m_pButtonList.count()) |
| 181 |
return; |
| 182 |
d->m_pButtonList.removeAt(id); |
| 183 |
d->updateButtonLayout(); |
| 184 |
} |
| 185 |
|
| 186 |
void KToolBar::removeButton(QList<KToolButton *> list) |
| 187 |
{ |
| 188 |
Q_D(KToolBar); |
| 189 |
for (int i = 0; i < list.count(); i++) { |
| 190 |
if (d->m_pButtonList.contains(list.at(i))) { |
| 191 |
d->m_pButtonList.removeAll(list.at(i)); |
| 192 |
} |
| 193 |
} |
| 194 |
d->updateButtonLayout(); |
| 195 |
} |
| 196 |
|
| 197 |
void KToolBar::setBorderRadius(int radius) |
| 198 |
{ |
| 199 |
Q_D(KToolBar); |
| 200 |
d->m_radius = radius; |
| 201 |
} |
| 202 |
|
| 203 |
int KToolBar::borderRadius() |
| 204 |
{ |
| 205 |
Q_D(KToolBar); |
| 206 |
return d->m_radius; |
| 207 |
} |
| 208 |
|
| 209 |
void KToolBar::setExclusive(bool exclusive) |
| 210 |
{ |
| 211 |
Q_D(KToolBar); |
| 212 |
d->m_exclusive = exclusive; |
| 213 |
} |
| 214 |
|
| 215 |
bool KToolBar::exclusive() |
| 216 |
{ |
| 217 |
Q_D(KToolBar); |
| 218 |
return d->m_exclusive; |
| 219 |
} |
| 220 |
|
| 221 |
void KToolBar::setCheckable(bool flag) |
| 222 |
{ |
| 223 |
Q_D(KToolBar); |
| 224 |
d->m_isCheckable = flag; |
| 225 |
QList<KToolButton *> list = buttonList(); |
| 226 |
for (int i = 0; i < list.count(); ++i) { |
| 227 |
KToolButton *button = list.at(i); |
| 228 |
button->setCheckable(d->m_isCheckable); |
| 229 |
} |
| 230 |
update(); |
| 231 |
} |
| 232 |
|
| 233 |
bool KToolBar::isCheckable() |
| 234 |
{ |
| 235 |
Q_D(KToolBar); |
| 236 |
return d->m_isCheckable; |
| 237 |
} |
| 238 |
|
| 239 |
QList<KToolButton *> KToolBar::checkedButton() |
| 240 |
{ |
| 241 |
Q_D(KToolBar); |
| 242 |
return d->m_pCheckList; |
| 243 |
} |
| 244 |
|
| 245 |
void KToolBar::setLineVisible(bool flag) |
| 246 |
{ |
| 247 |
Q_D(KToolBar); |
| 248 |
d->m_isLineVisible = flag; |
| 249 |
d->updateButtonLayout(); |
| 250 |
} |
| 251 |
|
| 252 |
bool KToolBar::isLineVisible() |
| 253 |
{ |
| 254 |
Q_D(KToolBar); |
| 255 |
return d->m_isLineVisible; |
| 256 |
} |
| 257 |
|
| 258 |
void KToolBar::setBackgroundColorEnabled(bool flag) |
| 259 |
{ |
| 260 |
Q_D(KToolBar); |
| 261 |
d->m_isBkgColorEnable = flag; |
| 262 |
d->updateButtonLayout(); |
| 263 |
} |
| 264 |
|
| 265 |
bool KToolBar::backgroundColorEnabled() |
| 266 |
{ |
| 267 |
Q_D(KToolBar); |
| 268 |
return d->m_isBkgColorEnable; |
| 269 |
} |
| 270 |
|
| 271 |
void KToolBar::paintEvent(QPaintEvent *event) |
| 272 |
{ |
| 273 |
Q_D(KToolBar); |
| 274 |
QPainter painter(this); |
| 275 |
painter.setRenderHint(QPainter::Antialiasing); |
| 276 |
if (d->m_isBkgColorEnable) |
| 277 |
painter.setBrush(ThemeController::getCustomColorFromDT("button-active")); |
| 278 |
else |
| 279 |
painter.setBrush(Qt::transparent); |
| 280 |
painter.setPen(Qt::NoPen); |
| 281 |
painter.drawRoundedRect(this->rect(), d->m_radius, d->m_radius); |
| 282 |
QWidget::paintEvent(event); |
| 283 |
} |
| 284 |
|
| 285 |
KToolBarPrivate::KToolBarPrivate(KToolBar *parent) |
| 286 |
: q_ptr(parent) |
| 287 |
{ |
| 288 |
Q_Q(KToolBar); |
| 289 |
init(); |
| 290 |
} |
| 291 |
|
| 292 |
void KToolBarPrivate::init() |
| 293 |
{ |
| 294 |
Q_Q(KToolBar); |
| 295 |
m_pLayout = new QHBoxLayout(q); |
| 296 |
m_pLayout->setContentsMargins(0, 0, 0, 0); |
| 297 |
m_pLayout->setSpacing(0); |
| 298 |
m_pLayout->setDirection(QBoxLayout::LeftToRight); |
| 299 |
m_radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 300 |
if (m_radius == -1) |
| 301 |
m_radius = 6; |
| 302 |
m_exclusive = false; |
| 303 |
m_isCheckable = false; |
| 304 |
m_isLineVisible = false; |
| 305 |
m_isBkgColorEnable = false; |
| 306 |
} |
| 307 |
|
| 308 |
void KToolBarPrivate::updateButtonLayout() |
| 309 |
{ |
| 310 |
Q_Q(KToolBar); |
| 311 |
clearButtonLayout(); |
| 312 |
for (int i = 0; i < m_pButtonList.count(); i++) { |
| 313 |
if (m_isBkgColorEnable) |
| 314 |
m_pButtonList.at(i)->setBackgroundColorEnabled(true); |
| 315 |
else |
| 316 |
m_pButtonList.at(i)->setBackgroundColorEnabled(false); |
| 317 |
|
| 318 |
m_pButtonList.at(i)->setBorderRadius(m_radius, m_radius, m_radius, m_radius); |
| 319 |
|
| 320 |
m_pLayout->addWidget(m_pButtonList.at(i)); |
| 321 |
q->setCheckable(q->isCheckable()); |
| 322 |
|
| 323 |
if (m_pButtonList.at(i)->menu()) |
| 324 |
m_pButtonList.at(i)->setArrow(true); |
| 325 |
|
| 326 |
connect(m_pButtonList.at(i), &KToolButton::checkedChanged, this, [=](bool checked) { |
| 327 |
getSelectList(m_pButtonList.at(i), checked); |
| 328 |
}); |
| 329 |
|
| 330 |
if (i != m_pButtonList.count() - 1 && m_isLineVisible) { |
| 331 |
if (q->orientation() == Qt::Vertical) { |
| 332 |
KHLineFrame *lineFrame = new KHLineFrame(q); |
| 333 |
lineFrame->setFollowPalette(false); |
| 334 |
QPalette pale; |
| 335 |
QColor color(0, 0, 0); |
| 336 |
color.setAlphaF(0.1); |
| 337 |
pale.setColor(QPalette::Window, color); |
| 338 |
lineFrame->setPalette(pale); |
| 339 |
m_pLayout->addWidget(lineFrame); |
| 340 |
} else { |
| 341 |
KVLineFrame *lineFrame = new KVLineFrame(q); |
| 342 |
lineFrame->setFollowPalette(false); |
| 343 |
QPalette pale; |
| 344 |
QColor color(0, 0, 0); |
| 345 |
color.setAlphaF(0.1); |
| 346 |
pale.setColor(QPalette::Window, color); |
| 347 |
lineFrame->setPalette(pale); |
| 348 |
m_pLayout->addWidget(lineFrame); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
void KToolBarPrivate::clearButtonLayout() |
| 355 |
{ |
| 356 |
for (int i = 0; i < m_pButtonList.count(); i++) { |
| 357 |
disconnect(m_pButtonList.at(i), SIGNAL(checkedChanged(bool)), 0, 0); |
| 358 |
} |
| 359 |
|
| 360 |
QLayoutItem *child; |
| 361 |
while ((child = m_pLayout->takeAt(0)) != 0) { |
| 362 |
if (child->spacerItem()) { |
| 363 |
m_pLayout->removeItem(child); |
| 364 |
continue; |
| 365 |
} |
| 366 |
m_pLayout->removeWidget(child->widget()); |
| 367 |
child->widget()->setParent(nullptr); |
| 368 |
delete child; |
| 369 |
child = nullptr; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
void KToolBarPrivate::getSelectList(KToolButton *button, bool checked) |
| 374 |
{ |
| 375 |
m_pCheckList.clear(); |
| 376 |
if (m_exclusive) { |
| 377 |
if (checked) { |
| 378 |
for (int i = 0; i < m_pButtonList.count(); i++) { |
| 379 |
|
| 380 |
if (button == m_pButtonList.at(i)) { |
| 381 |
button->setChecked(checked); |
| 382 |
} else |
| 383 |
m_pButtonList.at(i)->setChecked(false); |
| 384 |
} |
| 385 |
|
| 386 |
} else { |
| 387 |
button->setChecked(checked); |
| 388 |
} |
| 389 |
} else { |
| 390 |
for (int i = 0; i < m_pButtonList.count(); i++) { |
| 391 |
if (button == m_pButtonList.at(i)) { |
| 392 |
button->setChecked(checked); |
| 393 |
} else |
| 394 |
m_pButtonList.at(i)->setChecked(m_pButtonList.at(i)->isChecked()); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
for (int i = 0; i < m_pButtonList.count(); i++) { |
| 399 |
if (m_pButtonList.at(i)->isChecked()) |
| 400 |
m_pCheckList.append(m_pButtonList.at(i)); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
#include "ktoolbar.moc" |
| 407 |
#include "moc_ktoolbar.cpp" |