| 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 "kcomboboxwidget.h" |
| 24 |
#include "klabel.h" |
| 25 |
#include "ktoolbutton.h" |
| 26 |
#include "themeController.h" |
| 27 |
#include <QApplication> |
| 28 |
#include <QDebug> |
| 29 |
#include <QEvent> |
| 30 |
#include <QHBoxLayout> |
| 31 |
#include <QList> |
| 32 |
#include <QMenu> |
| 33 |
#include <QMouseEvent> |
| 34 |
#include <QPainter> |
| 35 |
#include <QPainterPath> |
| 36 |
#include <QProxyStyle> |
| 37 |
#include <QStyleOption> |
| 38 |
#include <QToolButton> |
| 39 |
#include <QWidgetAction> |
| 40 |
|
| 41 |
namespace kdk |
| 42 |
{ |
| 43 |
|
| 44 |
class Q_DECL_HIDDEN ProxyStyle : public QProxyStyle |
| 45 |
{ |
| 46 |
Q_OBJECT |
| 47 |
public: |
| 48 |
ProxyStyle(QWidget *parent = nullptr) |
| 49 |
{ |
| 50 |
} |
| 51 |
void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = nullptr) const override; |
| 52 |
}; |
| 53 |
|
| 54 |
class Q_DECL_HIDDEN MenuItem : public QToolButton, public ThemeController |
| 55 |
{ |
| 56 |
Q_OBJECT |
| 57 |
public: |
| 58 |
MenuItem(QString week, int id, QWidget *parent = nullptr); |
| 59 |
|
| 60 |
void setIconVisible(bool status); |
| 61 |
|
| 62 |
protected: |
| 63 |
void mouseReleaseEvent(QMouseEvent *event); |
| 64 |
void enterEvent(QEvent *e); |
| 65 |
void leaveEvent(QEvent *e); |
| 66 |
|
| 67 |
Q_SIGNALS: |
| 68 |
void showLabelIcon(int id); |
| 69 |
void hideLabelIcon(int id); |
| 70 |
|
| 71 |
private: |
| 72 |
QLabel *m_pTextLabel; |
| 73 |
QLabel *m_pMenuIconLabel; |
| 74 |
QHBoxLayout *m_pHLayout; |
| 75 |
int m_id = 0; |
| 76 |
ProxyStyle *m_proxyStyle = nullptr; |
| 77 |
}; |
| 78 |
|
| 79 |
class Q_DECL_HIDDEN KComboBoxWidgetPrivate : public QObject, public ThemeController |
| 80 |
{ |
| 81 |
Q_OBJECT |
| 82 |
Q_DECLARE_PUBLIC(KComboBoxWidget) |
| 83 |
public: |
| 84 |
KComboBoxWidgetPrivate(KComboBoxWidget *parent); |
| 85 |
void initLayout(); |
| 86 |
void setChosenString(); |
| 87 |
QStringList reorderListBasedOnSelection(const QStringList stringList, const QStringList chosenList); |
| 88 |
|
| 89 |
private: |
| 90 |
KComboBoxWidget *q_ptr; |
| 91 |
QLabel *m_pMainLabel; |
| 92 |
KLabel *m_pSubLabel; |
| 93 |
QLabel *m_pIconLabel; |
| 94 |
QHBoxLayout *m_pHLayout; |
| 95 |
bool m_mouseEnter = false; |
| 96 |
QStringList m_stringList; |
| 97 |
QStringList m_chosenList; |
| 98 |
QMenu *m_pMenu; |
| 99 |
}; |
| 100 |
|
| 101 |
KComboBoxWidget::KComboBoxWidget(QWidget *parent) |
| 102 |
: QWidget(parent) |
| 103 |
, d_ptr(new KComboBoxWidgetPrivate(this)) |
| 104 |
{ |
| 105 |
} |
| 106 |
|
| 107 |
void KComboBoxWidget::setStringList(QStringList list) |
| 108 |
{ |
| 109 |
Q_D(KComboBoxWidget); |
| 110 |
d->m_stringList = list; |
| 111 |
for (int i = 0; i < list.count(); i++) { |
| 112 |
MenuItem *item = new MenuItem(list[i], i, this); |
| 113 |
connect(item, &MenuItem::showLabelIcon, this, [=](int index) { |
| 114 |
if (!d->m_chosenList.contains(d->m_stringList.at(index))) |
| 115 |
d->m_chosenList.append(d->m_stringList.at(index)); |
| 116 |
d->setChosenString(); |
| 117 |
}); |
| 118 |
connect(item, &MenuItem::hideLabelIcon, this, [=](int index) { |
| 119 |
if (d->m_chosenList.contains(d->m_stringList.at(index))) |
| 120 |
d->m_chosenList.removeOne(d->m_stringList.at(index)); |
| 121 |
d->setChosenString(); |
| 122 |
}); |
| 123 |
QWidgetAction *action = new QWidgetAction(this); |
| 124 |
action->setDefaultWidget(item); |
| 125 |
d->m_pMenu->addAction(action); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
QStringList KComboBoxWidget::getChosenList() |
| 130 |
{ |
| 131 |
Q_D(KComboBoxWidget); |
| 132 |
return d->m_chosenList; |
| 133 |
} |
| 134 |
|
| 135 |
void KComboBoxWidget::setMainText(QString text) |
| 136 |
{ |
| 137 |
Q_D(KComboBoxWidget); |
| 138 |
d->m_pMainLabel->setText(text); |
| 139 |
} |
| 140 |
|
| 141 |
QMenu *KComboBoxWidget::menu() |
| 142 |
{ |
| 143 |
Q_D(KComboBoxWidget); |
| 144 |
return d->m_pMenu; |
| 145 |
} |
| 146 |
|
| 147 |
void KComboBoxWidget::mouseReleaseEvent(QMouseEvent *event) |
| 148 |
{ |
| 149 |
Q_D(KComboBoxWidget); |
| 150 |
emit comBoxWidgetClicked(); |
| 151 |
QWidget::mouseReleaseEvent(event); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
void KComboBoxWidget::mousePressEvent(QMouseEvent *event) |
| 156 |
{ |
| 157 |
Q_D(KComboBoxWidget); |
| 158 |
} |
| 159 |
|
| 160 |
void KComboBoxWidget::leaveEvent(QEvent *e) |
| 161 |
{ |
| 162 |
Q_D(KComboBoxWidget); |
| 163 |
d->m_mouseEnter = false; |
| 164 |
repaint(); |
| 165 |
} |
| 166 |
|
| 167 |
void KComboBoxWidget::enterEvent(QEvent *e) |
| 168 |
{ |
| 169 |
Q_D(KComboBoxWidget); |
| 170 |
d->m_mouseEnter = true; |
| 171 |
repaint(); |
| 172 |
} |
| 173 |
|
| 174 |
void KComboBoxWidget::paintEvent(QPaintEvent *event) |
| 175 |
{ |
| 176 |
Q_D(KComboBoxWidget); |
| 177 |
QStyleOption opt; |
| 178 |
opt.initFrom(this); |
| 179 |
QPainter p(this); |
| 180 |
QPainterPath path; |
| 181 |
opt.rect.adjust(0, 0, 0, 0); |
| 182 |
|
| 183 |
if (!opt.state.testFlag(QStyle::State_Enabled)) { |
| 184 |
p.setBrush(ThemeController::getCustomColorFromDT("button-disable")); |
| 185 |
} else { |
| 186 |
|
| 187 |
if (d->m_mouseEnter) { |
| 188 |
p.setBrush(ThemeController::getCustomColorFromDT("midlight-active")); |
| 189 |
} else { |
| 190 |
p.setBrush(ThemeController::getCustomColorFromDT("button-active")); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
p.setOpacity(1); |
| 195 |
p.setPen(Qt::NoPen); |
| 196 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 197 |
if (radius == -1) |
| 198 |
radius = 6; |
| 199 |
p.drawRoundedRect(opt.rect, radius, radius); |
| 200 |
p.setRenderHint(QPainter::Antialiasing); // 反锯齿 |
| 201 |
setProperty("blurRegion", QRegion(path.toFillPolygon().toPolygon())); |
| 202 |
} |
| 203 |
|
| 204 |
KComboBoxWidgetPrivate::KComboBoxWidgetPrivate(KComboBoxWidget *parent) |
| 205 |
: q_ptr(parent) |
| 206 |
{ |
| 207 |
Q_Q(KComboBoxWidget); |
| 208 |
initLayout(); |
| 209 |
} |
| 210 |
|
| 211 |
void KComboBoxWidgetPrivate::initLayout() |
| 212 |
{ |
| 213 |
Q_Q(KComboBoxWidget); |
| 214 |
m_pMenu = new QMenu; |
| 215 |
m_pMainLabel = new QLabel(); |
| 216 |
m_pMainLabel->setAlignment(Qt::AlignVCenter); |
| 217 |
|
| 218 |
m_pSubLabel = new KLabel(); |
| 219 |
m_pSubLabel->setAlignment(Qt::AlignRight); |
| 220 |
|
| 221 |
m_pSubLabel->setFixedWidth(165); |
| 222 |
m_pIconLabel = new QLabel(); |
| 223 |
QIcon labelIcon = QIcon::fromTheme("ukui-down.symbolic"); |
| 224 |
m_pIconLabel->setPixmap(labelIcon.pixmap(QSize(16, 16))); |
| 225 |
m_pIconLabel->setProperty("useIconHighlightEffect", 0x1); |
| 226 |
m_pHLayout = new QHBoxLayout(); |
| 227 |
m_pHLayout->setSpacing(0); |
| 228 |
m_pHLayout->setContentsMargins(0, 0, 0, 0); |
| 229 |
q->setContentsMargins(0, 0, 0, 0); |
| 230 |
|
| 231 |
m_pHLayout->addItem(new QSpacerItem(16, 1, QSizePolicy::Fixed)); |
| 232 |
m_pHLayout->addWidget(m_pMainLabel, Qt::AlignVCenter); |
| 233 |
m_pMainLabel->setAlignment(Qt::AlignVCenter); |
| 234 |
m_pHLayout->addItem(new QSpacerItem(12, 20, QSizePolicy::Expanding)); |
| 235 |
m_pHLayout->addWidget(m_pSubLabel, Qt::AlignVCenter); |
| 236 |
m_pSubLabel->setAlignment(Qt::AlignVCenter); |
| 237 |
m_pHLayout->addItem(new QSpacerItem(8, 30, QSizePolicy::Minimum)); |
| 238 |
m_pHLayout->addWidget(m_pIconLabel, Qt::AlignVCenter); |
| 239 |
m_pHLayout->addItem(new QSpacerItem(8, 5, QSizePolicy::Fixed)); |
| 240 |
|
| 241 |
q->setLayout(m_pHLayout); |
| 242 |
|
| 243 |
connect(q, &KComboBoxWidget::comBoxWidgetClicked, q, [=] { |
| 244 |
if (!m_pMenu->isVisible()) { |
| 245 |
m_pMenu->exec(q->mapToGlobal(QPoint(0, q->rect().height()))); |
| 246 |
} else { |
| 247 |
m_pMenu->hide(); |
| 248 |
} |
| 249 |
}); |
| 250 |
} |
| 251 |
|
| 252 |
void KComboBoxWidgetPrivate::setChosenString() |
| 253 |
{ |
| 254 |
Q_Q(KComboBoxWidget); |
| 255 |
QStringList list = reorderListBasedOnSelection(m_stringList, m_chosenList); |
| 256 |
QString text; |
| 257 |
for (int i = 0; i < list.count(); i++) { |
| 258 |
text.append(list.at(i)); |
| 259 |
if (i < list.count() - 1) |
| 260 |
text.append(" "); |
| 261 |
} |
| 262 |
|
| 263 |
m_pSubLabel->setText(text); |
| 264 |
} |
| 265 |
|
| 266 |
QStringList KComboBoxWidgetPrivate::reorderListBasedOnSelection(const QStringList stringList, const QStringList chosenList) |
| 267 |
{ |
| 268 |
QStringList result; |
| 269 |
QVector<bool> isSelected(chosenList.size(), false); |
| 270 |
|
| 271 |
for (const QString &selected : stringList) { |
| 272 |
int index = chosenList.indexOf(selected); |
| 273 |
if (index != -1) { |
| 274 |
result.append(selected); |
| 275 |
isSelected[index] = true; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
for (int i = 0; i < chosenList.size(); ++i) { |
| 280 |
if (!isSelected[i]) { |
| 281 |
result.append(chosenList.at(i)); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return result; |
| 286 |
} |
| 287 |
|
| 288 |
void ProxyStyle::drawComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const |
| 289 |
{ |
| 290 |
switch (control) { |
| 291 |
case CC_ToolButton: { |
| 292 |
if (const QStyleOptionComplex *f = qstyleoption_cast<const QStyleOptionComplex *>(option)) { |
| 293 |
const bool hover = f->state & State_MouseOver; |
| 294 |
if (hover) { |
| 295 |
painter->save(); |
| 296 |
painter->setRenderHint(QPainter::Antialiasing, true); |
| 297 |
painter->setPen(Qt::NoPen); |
| 298 |
QStyleOption opt; |
| 299 |
QColor color = ThemeController::getCustomColorFromDT("highlight-active"); |
| 300 |
painter->setBrush(color); |
| 301 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 302 |
if (radius == -1) |
| 303 |
radius = 6; |
| 304 |
painter->drawRoundedRect(option->rect, radius, radius); |
| 305 |
painter->restore(); |
| 306 |
return; |
| 307 |
} |
| 308 |
} |
| 309 |
break; |
| 310 |
} |
| 311 |
default: |
| 312 |
break; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
MenuItem::MenuItem(QString week, int id, QWidget *parent) |
| 317 |
: QToolButton(parent) |
| 318 |
, m_id(id) |
| 319 |
{ |
| 320 |
m_proxyStyle = new ProxyStyle(this); |
| 321 |
this->setStyle(m_proxyStyle); |
| 322 |
m_pTextLabel = new QLabel(); |
| 323 |
m_pTextLabel->setText(week); |
| 324 |
this->setAutoRaise(true); // 设置默认状态背景透明 |
| 325 |
|
| 326 |
m_pMenuIconLabel = new QLabel(); |
| 327 |
|
| 328 |
m_pMenuIconLabel->setFixedSize(16, 16); |
| 329 |
QIcon labelIcon = QIcon::fromTheme("object-select-symbolic"); |
| 330 |
m_pMenuIconLabel->setPixmap(labelIcon.pixmap(QSize(16, 16))); |
| 331 |
m_pMenuIconLabel->setProperty("useIconHighlightEffect", 0x8); |
| 332 |
m_pMenuIconLabel->setFixedSize(16, 16); |
| 333 |
|
| 334 |
m_pHLayout = new QHBoxLayout(); |
| 335 |
m_pHLayout->setContentsMargins(17, 0, 0, 0); |
| 336 |
this->setLayout(m_pHLayout); |
| 337 |
m_pHLayout->addWidget(m_pMenuIconLabel); |
| 338 |
m_pHLayout->addWidget(m_pTextLabel); |
| 339 |
|
| 340 |
QSizePolicy policy = m_pMenuIconLabel->sizePolicy(); |
| 341 |
policy.setRetainSizeWhenHidden(true); |
| 342 |
m_pMenuIconLabel->setSizePolicy(policy); |
| 343 |
m_pMenuIconLabel->setVisible(false); |
| 344 |
|
| 345 |
connect(m_gsetting, &QGSettings::changed, this, [=](const QString &key) { |
| 346 |
if(key == "styleName" ) |
| 347 |
{ |
| 348 |
if (ThemeController::themeMode() == DarkTheme) { |
| 349 |
QPalette palette; |
| 350 |
palette.setColor(QPalette::Active, QPalette::ButtonText, Qt::white); |
| 351 |
m_pTextLabel->setPalette(palette); |
| 352 |
} else { |
| 353 |
QPalette palette; |
| 354 |
palette.setColor(QPalette::Active, QPalette::ButtonText, Qt::black); |
| 355 |
m_pTextLabel->setPalette(palette); |
| 356 |
} |
| 357 |
} |
| 358 |
}); |
| 359 |
} |
| 360 |
|
| 361 |
void MenuItem::setIconVisible(bool status) |
| 362 |
{ |
| 363 |
m_pMenuIconLabel->setVisible(status); |
| 364 |
} |
| 365 |
|
| 366 |
void MenuItem::mouseReleaseEvent(QMouseEvent *event) |
| 367 |
{ |
| 368 |
if (event->button() == Qt::LeftButton) { |
| 369 |
if (!this->m_pMenuIconLabel->isVisible()) { |
| 370 |
m_pMenuIconLabel->setVisible(true); |
| 371 |
emit showLabelIcon(m_id); |
| 372 |
} else { |
| 373 |
m_pMenuIconLabel->setVisible(false); |
| 374 |
emit hideLabelIcon(m_id); |
| 375 |
} |
| 376 |
} |
| 377 |
QToolButton::mouseReleaseEvent(event); |
| 378 |
} |
| 379 |
|
| 380 |
void MenuItem::enterEvent(QEvent *e) |
| 381 |
{ |
| 382 |
QApplication::postEvent(m_pMenuIconLabel, new QEvent(QEvent::Enter)); |
| 383 |
|
| 384 |
QPalette palette; |
| 385 |
palette.setColor(QPalette::Active, QPalette::ButtonText, Qt::white); |
| 386 |
m_pTextLabel->setPalette(palette); |
| 387 |
} |
| 388 |
|
| 389 |
void MenuItem::leaveEvent(QEvent *e) |
| 390 |
{ |
| 391 |
if (ThemeController::themeMode() == LightTheme) { |
| 392 |
QPalette palette; |
| 393 |
palette.setColor(QPalette::Active, QPalette::ButtonText, Qt::black); |
| 394 |
m_pTextLabel->setPalette(palette); |
| 395 |
} |
| 396 |
QApplication::postEvent(m_pMenuIconLabel, new QEvent(QEvent::Leave)); |
| 397 |
} |
| 398 |
|
| 399 |
} |
| 400 |
|
| 401 |
#include "kcomboboxwidget.moc" |
| 402 |
#include "moc_kcomboboxwidget.cpp" |