| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org> |
| 4 | SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org> |
| 5 | SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org> |
| 6 | SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org> |
| 7 | SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org> |
| 8 | SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org> |
| 9 | SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org> |
| 10 | SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org> |
| 11 | SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org> |
| 12 | SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org> |
| 13 | SPDX-FileCopyrightText: 2006 Albert Astals Cid <aacid@kde.org> |
| 14 | SPDX-FileCopyrightText: 2006 Clarence Dang <dang@kde.org> |
| 15 | SPDX-FileCopyrightText: 2006 Michel Hermier <michel.hermier@gmail.com> |
| 16 | SPDX-FileCopyrightText: 2007 Nick Shaforostoff <shafff@ukr.net> |
| 17 | |
| 18 | SPDX-License-Identifier: LGPL-2.0-only |
| 19 | */ |
| 20 | |
| 21 | #include "kselectaction.h" |
| 22 | #include "kselectaction_p.h" |
| 23 | |
| 24 | #include "loggingcategory.h" |
| 25 | |
| 26 | #include <QActionEvent> |
| 27 | #include <QEvent> |
| 28 | #include <QMenu> |
| 29 | #include <QStandardItem> |
| 30 | #include <QToolBar> |
| 31 | |
| 32 | // QAction::setText("Hi") and then KPopupAccelManager exec'ing, causes |
| 33 | // QAction::text() to return "&Hi" :( Comboboxes don't have accels and |
| 34 | // display ampersands literally. |
| 35 | static QString DropAmpersands(const QString &text) |
| 36 | { |
| 37 | QString label = text; |
| 38 | |
| 39 | int p = label.indexOf(ch: QLatin1Char('&')); |
| 40 | while (p >= 0 && p < label.length() - 1) { |
| 41 | if (label[p + 1].isLetterOrNumber() // Valid accelerator. |
| 42 | || label[p + 1] == QLatin1Char('&')) { // Escaped accelerator marker. |
| 43 | label.remove(i: p, len: 1); |
| 44 | } |
| 45 | |
| 46 | p = label.indexOf(ch: QLatin1Char('&'), from: p + 1); |
| 47 | } |
| 48 | |
| 49 | return label; |
| 50 | } |
| 51 | |
| 52 | KSelectAction::KSelectAction(QObject *parent) |
| 53 | : KSelectAction(*new KSelectActionPrivate(this), parent) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | KSelectAction::KSelectAction(const QString &text, QObject *parent) |
| 58 | : KSelectAction(*new KSelectActionPrivate(this), parent) |
| 59 | { |
| 60 | setText(text); |
| 61 | } |
| 62 | |
| 63 | KSelectAction::KSelectAction(const QIcon &icon, const QString &text, QObject *parent) |
| 64 | : KSelectAction(*new KSelectActionPrivate(this), parent) |
| 65 | { |
| 66 | setIcon(icon); |
| 67 | setText(text); |
| 68 | } |
| 69 | |
| 70 | KSelectAction::KSelectAction(KSelectActionPrivate &dd, QObject *parent) |
| 71 | : QWidgetAction(parent) |
| 72 | , d_ptr(&dd) |
| 73 | { |
| 74 | Q_D(KSelectAction); |
| 75 | d->init(); |
| 76 | } |
| 77 | |
| 78 | KSelectAction::~KSelectAction() |
| 79 | { |
| 80 | menu()->deleteLater(); |
| 81 | } |
| 82 | |
| 83 | void KSelectActionPrivate::init() |
| 84 | { |
| 85 | QObject::connect(sender: q_ptr->selectableActionGroup(), signal: &QActionGroup::triggered, context: q_ptr, slot: &KSelectAction::slotActionTriggered); |
| 86 | QObject::connect(sender: q_ptr, signal: &QAction::toggled, context: q_ptr, slot: &KSelectAction::slotToggled); |
| 87 | q_ptr->setMenu(new QMenu()); |
| 88 | q_ptr->setEnabled(false); |
| 89 | } |
| 90 | |
| 91 | QActionGroup *KSelectAction::selectableActionGroup() const |
| 92 | { |
| 93 | Q_D(const KSelectAction); |
| 94 | return d->m_actionGroup; |
| 95 | } |
| 96 | |
| 97 | QList<QAction *> KSelectAction::actions() const |
| 98 | { |
| 99 | return selectableActionGroup()->actions(); |
| 100 | } |
| 101 | |
| 102 | QAction *KSelectAction::currentAction() const |
| 103 | { |
| 104 | return selectableActionGroup()->checkedAction(); |
| 105 | } |
| 106 | |
| 107 | int KSelectAction::currentItem() const |
| 108 | { |
| 109 | return selectableActionGroup()->actions().indexOf(t: currentAction()); |
| 110 | } |
| 111 | |
| 112 | QString KSelectAction::currentText() const |
| 113 | { |
| 114 | if (QAction *a = currentAction()) { |
| 115 | return ::DropAmpersands(text: a->text()); |
| 116 | } |
| 117 | |
| 118 | return QString(); |
| 119 | } |
| 120 | |
| 121 | bool KSelectAction::setCurrentAction(QAction *action) |
| 122 | { |
| 123 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::setCurrentAction(" << action << ")"; |
| 124 | if (action) { |
| 125 | if (actions().contains(t: action)) { |
| 126 | if (action->isVisible() && action->isEnabled() && action->isCheckable()) { |
| 127 | action->setChecked(true); |
| 128 | if (isCheckable()) { |
| 129 | setChecked(true); |
| 130 | } |
| 131 | return true; |
| 132 | } else { |
| 133 | qCWarning(KWidgetsAddonsLog) << "Action does not have the correct properties to be current:" << action->text(); |
| 134 | } |
| 135 | } else { |
| 136 | qCWarning(KWidgetsAddonsLog) << "Action does not belong to group:" << action->text(); |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if (currentAction()) { |
| 142 | currentAction()->setChecked(false); |
| 143 | } |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool KSelectAction::setCurrentItem(int index) |
| 149 | { |
| 150 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::setCurrentIndex(" << index << ")"; |
| 151 | return setCurrentAction(action(index)); |
| 152 | } |
| 153 | |
| 154 | QAction *KSelectAction::action(int index) const |
| 155 | { |
| 156 | if (index >= 0 && index < selectableActionGroup()->actions().count()) { |
| 157 | return selectableActionGroup()->actions().at(i: index); |
| 158 | } |
| 159 | |
| 160 | return nullptr; |
| 161 | } |
| 162 | |
| 163 | QAction *KSelectAction::action(const QString &text, Qt::CaseSensitivity cs) const |
| 164 | { |
| 165 | QString compare; |
| 166 | if (cs == Qt::CaseSensitive) { |
| 167 | compare = text; |
| 168 | } else { |
| 169 | compare = text.toLower(); |
| 170 | } |
| 171 | |
| 172 | const auto selectableActions = selectableActionGroup()->actions(); |
| 173 | for (QAction *action : selectableActions) { |
| 174 | const QString text = ::DropAmpersands(text: action->text()); |
| 175 | if (cs == Qt::CaseSensitive) { |
| 176 | if (text == compare) { |
| 177 | return action; |
| 178 | } |
| 179 | |
| 180 | } else if (cs == Qt::CaseInsensitive) { |
| 181 | if (text.toLower() == compare) { |
| 182 | return action; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
| 190 | bool KSelectAction::setCurrentAction(const QString &text, Qt::CaseSensitivity cs) |
| 191 | { |
| 192 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::setCurrentAction(" << text << ",cs=" << cs << ")"; |
| 193 | return setCurrentAction(action(text, cs)); |
| 194 | } |
| 195 | |
| 196 | void KSelectAction::setComboWidth(int width) |
| 197 | { |
| 198 | Q_D(KSelectAction); |
| 199 | if (width < 0) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | d->m_comboWidth = width; |
| 204 | |
| 205 | for (QComboBox *box : std::as_const(t&: d->m_comboBoxes)) { |
| 206 | box->setMaximumWidth(d->m_comboWidth); |
| 207 | } |
| 208 | |
| 209 | Q_EMIT changed(); |
| 210 | } |
| 211 | |
| 212 | void KSelectAction::setMaxComboViewCount(int n) |
| 213 | { |
| 214 | Q_D(KSelectAction); |
| 215 | d->m_maxComboViewCount = n; |
| 216 | |
| 217 | for (QComboBox *box : std::as_const(t&: d->m_comboBoxes)) { |
| 218 | if (d->m_maxComboViewCount != -1) { |
| 219 | box->setMaxVisibleItems(d->m_maxComboViewCount); |
| 220 | } else |
| 221 | // hardcoded qt default |
| 222 | { |
| 223 | box->setMaxVisibleItems(10); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | Q_EMIT changed(); |
| 228 | } |
| 229 | |
| 230 | void KSelectAction::addAction(QAction *action) |
| 231 | { |
| 232 | insertAction(before: nullptr, action); |
| 233 | } |
| 234 | |
| 235 | QAction *KSelectAction::addAction(const QString &text) |
| 236 | { |
| 237 | Q_D(KSelectAction); |
| 238 | QAction *newAction = new QAction(parent()); |
| 239 | newAction->setText(text); |
| 240 | newAction->setCheckable(true); |
| 241 | newAction->setProperty(name: "isShortcutConfigurable" , value: false); |
| 242 | |
| 243 | if (!d->m_menuAccelsEnabled) { |
| 244 | newAction->setText(text); |
| 245 | newAction->setShortcut(QKeySequence()); |
| 246 | } |
| 247 | |
| 248 | addAction(action: newAction); |
| 249 | return newAction; |
| 250 | } |
| 251 | |
| 252 | QAction *KSelectAction::addAction(const QIcon &icon, const QString &text) |
| 253 | { |
| 254 | QAction *newAction = addAction(text); |
| 255 | newAction->setIcon(icon); |
| 256 | return newAction; |
| 257 | } |
| 258 | |
| 259 | QAction *KSelectAction::removeAction(QAction *action) |
| 260 | { |
| 261 | Q_D(KSelectAction); |
| 262 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::removeAction(" << action << ")"; |
| 263 | // int index = selectableActionGroup()->actions().indexOf(action); |
| 264 | // qCDebug(KWidgetsAddonsLog) << "\tindex=" << index; |
| 265 | |
| 266 | // Removes the action from the group and sets its parent to null. |
| 267 | d->m_actionGroup->removeAction(a: action); |
| 268 | |
| 269 | // Disable when no action is in the group |
| 270 | bool hasActions = selectableActionGroup()->actions().isEmpty(); |
| 271 | setEnabled(!hasActions); |
| 272 | |
| 273 | for (QToolButton *button : std::as_const(t&: d->m_buttons)) { |
| 274 | button->setEnabled(!hasActions); |
| 275 | button->removeAction(action); |
| 276 | } |
| 277 | |
| 278 | for (QComboBox *comboBox : std::as_const(t&: d->m_comboBoxes)) { |
| 279 | comboBox->setEnabled(!hasActions); |
| 280 | comboBox->removeAction(action); |
| 281 | } |
| 282 | |
| 283 | menu()->removeAction(action); |
| 284 | |
| 285 | return action; |
| 286 | } |
| 287 | |
| 288 | void KSelectAction::insertAction(QAction *before, QAction *action) |
| 289 | { |
| 290 | Q_D(KSelectAction); |
| 291 | action->setActionGroup(selectableActionGroup()); |
| 292 | |
| 293 | // Re-Enable when an action is added |
| 294 | setEnabled(true); |
| 295 | |
| 296 | // Keep in sync with createToolBarWidget() |
| 297 | for (QToolButton *button : std::as_const(t&: d->m_buttons)) { |
| 298 | button->setEnabled(true); |
| 299 | button->insertAction(before, action); |
| 300 | } |
| 301 | |
| 302 | for (QComboBox *comboBox : std::as_const(t&: d->m_comboBoxes)) { |
| 303 | comboBox->setEnabled(true); |
| 304 | comboBox->insertAction(before, action); |
| 305 | } |
| 306 | |
| 307 | menu()->insertAction(before, action); |
| 308 | } |
| 309 | |
| 310 | void KSelectAction::slotActionTriggered(QAction *action) |
| 311 | { |
| 312 | // cache values so we don't need access to members in the action |
| 313 | // after we've done an emit() |
| 314 | const QString text = ::DropAmpersands(text: action->text()); |
| 315 | const int index = selectableActionGroup()->actions().indexOf(t: action); |
| 316 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::slotActionTriggered(" << action << ") text=" << text |
| 317 | // << " index=" << index << " emitting triggered()" << endl; |
| 318 | |
| 319 | if (isCheckable()) { // if this is subsidiary of other KSelectAction-derived class |
| 320 | trigger(); // then imitate usual QAction behaviour so that other submenus (and their items) become unchecked |
| 321 | } |
| 322 | |
| 323 | Q_EMIT actionTriggered(action); |
| 324 | Q_EMIT indexTriggered(index); |
| 325 | Q_EMIT textTriggered(text); |
| 326 | } |
| 327 | |
| 328 | QStringList KSelectAction::items() const |
| 329 | { |
| 330 | Q_D(const KSelectAction); |
| 331 | QStringList ret; |
| 332 | |
| 333 | const auto actions = d->m_actionGroup->actions(); |
| 334 | ret.reserve(asize: actions.size()); |
| 335 | for (QAction *action : actions) { |
| 336 | ret << ::DropAmpersands(text: action->text()); |
| 337 | } |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | void KSelectAction::changeItem(int index, const QString &text) |
| 343 | { |
| 344 | Q_D(KSelectAction); |
| 345 | if (index < 0 || index >= actions().count()) { |
| 346 | qCWarning(KWidgetsAddonsLog) << "KSelectAction::changeItem Index out of scope" ; |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | actions()[index]->setText(d->makeMenuText(text: text)); |
| 351 | } |
| 352 | |
| 353 | void KSelectAction::setItems(const QStringList &lst) |
| 354 | { |
| 355 | Q_D(KSelectAction); |
| 356 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::setItems(" << lst << ")"; |
| 357 | |
| 358 | clear(); |
| 359 | |
| 360 | for (const QString &string : lst) { |
| 361 | if (!string.isEmpty()) { |
| 362 | addAction(text: string); |
| 363 | } else { |
| 364 | QAction *action = new QAction(this); |
| 365 | action->setSeparator(true); |
| 366 | addAction(action); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Disable if empty and not editable |
| 371 | setEnabled(lst.count() > 0 || d->m_edit); |
| 372 | } |
| 373 | |
| 374 | int KSelectAction::comboWidth() const |
| 375 | { |
| 376 | Q_D(const KSelectAction); |
| 377 | return d->m_comboWidth; |
| 378 | } |
| 379 | |
| 380 | void KSelectAction::clear() |
| 381 | { |
| 382 | Q_D(KSelectAction); |
| 383 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::clear()"; |
| 384 | |
| 385 | // we need to delete the actions later since we may get a call to clear() |
| 386 | // from a method called due to a triggered(...) signal |
| 387 | const QList<QAction *> actions = d->m_actionGroup->actions(); |
| 388 | for (int i = 0; i < actions.count(); ++i) { |
| 389 | // deleteLater() only removes us from the actions() list (among |
| 390 | // other things) on the next entry into the event loop. Until then, |
| 391 | // e.g. action() and setCurrentItem() will be working on items |
| 392 | // that are supposed to have been deleted. So detach the action to |
| 393 | // prevent this from happening. |
| 394 | removeAction(action: actions[i]); |
| 395 | |
| 396 | actions[i]->deleteLater(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void KSelectAction::removeAllActions() |
| 401 | { |
| 402 | Q_D(KSelectAction); |
| 403 | while (d->m_actionGroup->actions().count()) { |
| 404 | removeAction(action: d->m_actionGroup->actions().first()); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void KSelectAction::setEditable(bool edit) |
| 409 | { |
| 410 | Q_D(KSelectAction); |
| 411 | d->m_edit = edit; |
| 412 | |
| 413 | for (QComboBox *comboBox : std::as_const(t&: d->m_comboBoxes)) { |
| 414 | comboBox->setEditable(edit); |
| 415 | } |
| 416 | |
| 417 | Q_EMIT changed(); |
| 418 | } |
| 419 | |
| 420 | bool KSelectAction::isEditable() const |
| 421 | { |
| 422 | Q_D(const KSelectAction); |
| 423 | return d->m_edit; |
| 424 | } |
| 425 | |
| 426 | void KSelectAction::slotToggled(bool checked) |
| 427 | { |
| 428 | // if (checked && selectableActionGroup()->checkedAction()) |
| 429 | if (!checked && currentAction()) { // other's submenu item has been selected |
| 430 | currentAction()->setChecked(false); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | KSelectAction::ToolBarMode KSelectAction::toolBarMode() const |
| 435 | { |
| 436 | Q_D(const KSelectAction); |
| 437 | return d->m_toolBarMode; |
| 438 | } |
| 439 | |
| 440 | void KSelectAction::setToolBarMode(ToolBarMode mode) |
| 441 | { |
| 442 | Q_D(KSelectAction); |
| 443 | d->m_toolBarMode = mode; |
| 444 | } |
| 445 | |
| 446 | QToolButton::ToolButtonPopupMode KSelectAction::() const |
| 447 | { |
| 448 | Q_D(const KSelectAction); |
| 449 | return d->m_toolButtonPopupMode; |
| 450 | } |
| 451 | |
| 452 | void KSelectAction::(QToolButton::ToolButtonPopupMode mode) |
| 453 | { |
| 454 | Q_D(KSelectAction); |
| 455 | d->m_toolButtonPopupMode = mode; |
| 456 | } |
| 457 | |
| 458 | void KSelectActionPrivate::comboBoxDeleted(QComboBox *combo) |
| 459 | { |
| 460 | m_comboBoxes.removeAll(t: combo); |
| 461 | } |
| 462 | |
| 463 | void KSelectActionPrivate::comboBoxCurrentIndexChanged(int index) |
| 464 | { |
| 465 | Q_Q(KSelectAction); |
| 466 | // qCDebug(KWidgetsAddonsLog) << "KSelectActionPrivate::comboBoxCurrentIndexChanged(" << index << ")"; |
| 467 | |
| 468 | QComboBox *triggeringCombo = qobject_cast<QComboBox *>(object: q->sender()); |
| 469 | |
| 470 | QAction *a = q->action(index); |
| 471 | // qCDebug(KWidgetsAddonsLog) << "\ta=" << a; |
| 472 | if (a) { |
| 473 | // qCDebug(KWidgetsAddonsLog) << "\t\tsetting as current action"; |
| 474 | a->trigger(); |
| 475 | |
| 476 | } else if (q->isEditable() && triggeringCombo && triggeringCombo->count() > 0 && index == triggeringCombo->count() - 1) { |
| 477 | // User must have added a new item by typing and pressing enter. |
| 478 | const QString newItemText = triggeringCombo->currentText(); |
| 479 | // qCDebug(KWidgetsAddonsLog) << "\t\tuser typed new item '" << newItemText << "'"; |
| 480 | |
| 481 | // Only 1 combobox contains this and it's not a proper action. |
| 482 | bool blocked = triggeringCombo->blockSignals(b: true); |
| 483 | triggeringCombo->removeItem(index); |
| 484 | triggeringCombo->blockSignals(b: blocked); |
| 485 | |
| 486 | QAction *newAction = q->addAction(text: newItemText); |
| 487 | |
| 488 | newAction->trigger(); |
| 489 | } else { |
| 490 | if (q->selectableActionGroup()->checkedAction()) { |
| 491 | q->selectableActionGroup()->checkedAction()->setChecked(false); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // TODO: DropAmpersands() certainly makes sure this doesn't work. But I don't |
| 497 | // think it did anyway esp. in the presence KCheckAccelerator - Clarence. |
| 498 | void KSelectAction::(bool b) |
| 499 | { |
| 500 | Q_D(KSelectAction); |
| 501 | d->m_menuAccelsEnabled = b; |
| 502 | } |
| 503 | |
| 504 | bool KSelectAction::() const |
| 505 | { |
| 506 | Q_D(const KSelectAction); |
| 507 | return d->m_menuAccelsEnabled; |
| 508 | } |
| 509 | |
| 510 | QWidget *KSelectAction::createWidget(QWidget *parent) |
| 511 | { |
| 512 | Q_D(KSelectAction); |
| 513 | QMenu * = qobject_cast<QMenu *>(object: parent); |
| 514 | if (menu) { // If used in a menu want to return 0 and use only the text, not a widget |
| 515 | return nullptr; |
| 516 | } |
| 517 | ToolBarMode mode = toolBarMode(); |
| 518 | QToolBar *toolBar = qobject_cast<QToolBar *>(object: parent); |
| 519 | if (!toolBar && mode != ComboBoxMode) { // we can return a combobox just fine. |
| 520 | return nullptr; |
| 521 | } |
| 522 | switch (mode) { |
| 523 | case MenuMode: { |
| 524 | QToolButton *button = new QToolButton(toolBar); |
| 525 | button->setToolTip(toolTip()); |
| 526 | button->setWhatsThis(whatsThis()); |
| 527 | button->setStatusTip(statusTip()); |
| 528 | button->setAutoRaise(true); |
| 529 | button->setFocusPolicy(Qt::NoFocus); |
| 530 | button->setIconSize(toolBar->iconSize()); |
| 531 | button->setToolButtonStyle(toolBar->toolButtonStyle()); |
| 532 | QObject::connect(sender: toolBar, signal: &QToolBar::iconSizeChanged, context: button, slot: &QAbstractButton::setIconSize); |
| 533 | QObject::connect(sender: toolBar, signal: &QToolBar::toolButtonStyleChanged, context: button, slot: &QToolButton::setToolButtonStyle); |
| 534 | button->setDefaultAction(this); |
| 535 | QObject::connect(sender: button, signal: &QToolButton::triggered, context: toolBar, slot: &QToolBar::actionTriggered); |
| 536 | |
| 537 | button->setPopupMode(toolButtonPopupMode()); |
| 538 | |
| 539 | button->addActions(actions: selectableActionGroup()->actions()); |
| 540 | |
| 541 | d->m_buttons.append(t: button); |
| 542 | return button; |
| 543 | } |
| 544 | |
| 545 | case ComboBoxMode: { |
| 546 | QComboBox *comboBox = new QComboBox(parent); |
| 547 | comboBox->installEventFilter(filterObj: this); |
| 548 | |
| 549 | if (d->m_maxComboViewCount != -1) { |
| 550 | comboBox->setMaxVisibleItems(d->m_maxComboViewCount); |
| 551 | } |
| 552 | |
| 553 | if (d->m_comboWidth > 0) { |
| 554 | comboBox->setMaximumWidth(d->m_comboWidth); |
| 555 | } |
| 556 | |
| 557 | comboBox->setEditable(isEditable()); |
| 558 | comboBox->setToolTip(toolTip()); |
| 559 | comboBox->setWhatsThis(whatsThis()); |
| 560 | comboBox->setStatusTip(statusTip()); |
| 561 | comboBox->setPlaceholderText(text()); |
| 562 | |
| 563 | const auto selectableActions = selectableActionGroup()->actions(); |
| 564 | for (QAction *action : selectableActions) { |
| 565 | comboBox->addAction(action); |
| 566 | } |
| 567 | |
| 568 | if (selectableActions.isEmpty()) { |
| 569 | comboBox->setEnabled(false); |
| 570 | } |
| 571 | |
| 572 | connect(sender: comboBox, signal: &QComboBox::destroyed, context: this, slot: [d, comboBox]() { |
| 573 | d->comboBoxDeleted(combo: comboBox); |
| 574 | }); |
| 575 | |
| 576 | connect(sender: comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: [d](int value) { |
| 577 | d->comboBoxCurrentIndexChanged(index: value); |
| 578 | }); |
| 579 | |
| 580 | d->m_comboBoxes.append(t: comboBox); |
| 581 | |
| 582 | return comboBox; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return nullptr; |
| 587 | } |
| 588 | |
| 589 | void KSelectAction::deleteWidget(QWidget *widget) |
| 590 | { |
| 591 | Q_D(KSelectAction); |
| 592 | if (QToolButton *toolButton = qobject_cast<QToolButton *>(object: widget)) { |
| 593 | d->m_buttons.removeAll(t: toolButton); |
| 594 | } else if (QComboBox *comboBox = qobject_cast<QComboBox *>(object: widget)) { |
| 595 | d->m_comboBoxes.removeAll(t: comboBox); |
| 596 | } |
| 597 | QWidgetAction::deleteWidget(widget); |
| 598 | } |
| 599 | |
| 600 | bool KSelectAction::event(QEvent *event) |
| 601 | { |
| 602 | Q_D(KSelectAction); |
| 603 | if (event->type() == QEvent::ActionChanged) { |
| 604 | for (QComboBox *comboBox : std::as_const(t&: d->m_comboBoxes)) { |
| 605 | comboBox->setToolTip(toolTip()); |
| 606 | comboBox->setWhatsThis(whatsThis()); |
| 607 | comboBox->setStatusTip(statusTip()); |
| 608 | } |
| 609 | for (QToolButton *toolButton : std::as_const(t&: d->m_buttons)) { |
| 610 | toolButton->setToolTip(toolTip()); |
| 611 | toolButton->setWhatsThis(whatsThis()); |
| 612 | toolButton->setStatusTip(statusTip()); |
| 613 | } |
| 614 | } |
| 615 | return QWidgetAction::event(event); |
| 616 | } |
| 617 | |
| 618 | // KSelectAction::eventFilter() is called before action->setChecked() |
| 619 | // invokes the signal to update QActionGroup so KSelectAction::currentItem() |
| 620 | // returns an old value. There are 3 possibilities, where n actions will |
| 621 | // report QAction::isChecked() where n is: |
| 622 | // |
| 623 | // 0: the checked action was unchecked |
| 624 | // 1: the checked action did not change |
| 625 | // 2: another action was checked but QActionGroup has not been invoked yet |
| 626 | // to uncheck the one that was checked before |
| 627 | // |
| 628 | // TODO: we might want to cache this since QEvent::ActionChanged is fired |
| 629 | // often. |
| 630 | static int TrueCurrentItem(KSelectAction *sa) |
| 631 | { |
| 632 | QAction *curAction = sa->currentAction(); |
| 633 | // qCDebug(KWidgetsAddonsLog) << "\tTrueCurrentItem(" << sa << ") curAction=" << curAction; |
| 634 | |
| 635 | const auto actions = sa->actions(); |
| 636 | int i = 0; |
| 637 | for (QAction *action : actions) { |
| 638 | if (action->isChecked()) { |
| 639 | // qCDebug(KWidgetsAddonsLog) << "\t\taction " << action << " (text=" << action->text () << ") isChecked"; |
| 640 | |
| 641 | // 2 actions checked case? |
| 642 | if (action != curAction) { |
| 643 | // qCDebug(KWidgetsAddonsLog) << "\t\t\tmust be newly selected one"; |
| 644 | return i; |
| 645 | } |
| 646 | } |
| 647 | ++i; |
| 648 | } |
| 649 | |
| 650 | // qCDebug(KWidgetsAddonsLog) << "\t\tcurrent action still selected? " << (curAction && curAction->isChecked ()); |
| 651 | // 1 or 0 actions checked case (in that order)? |
| 652 | return (curAction && curAction->isChecked()) ? sa->actions().indexOf(t: curAction) : -1; |
| 653 | } |
| 654 | |
| 655 | bool KSelectAction::eventFilter(QObject *watched, QEvent *event) |
| 656 | { |
| 657 | QComboBox *comboBox = qobject_cast<QComboBox *>(object: watched); |
| 658 | if (!comboBox) { |
| 659 | return false /*propagate event*/; |
| 660 | } |
| 661 | |
| 662 | // If focus is lost, replace any edited text with the currently selected |
| 663 | // item. |
| 664 | if (event->type() == QEvent::FocusOut) { |
| 665 | QFocusEvent *const e = static_cast<QFocusEvent *>(event); |
| 666 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::eventFilter(FocusOut)" |
| 667 | // << " comboBox: ptr=" << comboBox |
| 668 | // << " reason=" << e->reason () |
| 669 | // << endl; |
| 670 | |
| 671 | if (e->reason() != Qt::ActiveWindowFocusReason // switch window |
| 672 | && e->reason() != Qt::PopupFocusReason // menu |
| 673 | && e->reason() != Qt::OtherFocusReason // inconsistently reproduceable actions... |
| 674 | ) { |
| 675 | // qCDebug(KWidgetsAddonsLog) << "\tkilling text"; |
| 676 | comboBox->setEditText(comboBox->itemText(index: comboBox->currentIndex())); |
| 677 | } |
| 678 | |
| 679 | return false /*propagate event*/; |
| 680 | } |
| 681 | |
| 682 | bool blocked = comboBox->blockSignals(b: true); |
| 683 | |
| 684 | if (event->type() == QEvent::ActionAdded) { |
| 685 | QActionEvent *const e = static_cast<QActionEvent *>(event); |
| 686 | |
| 687 | const int index = e->before() ? comboBox->findData(data: QVariant::fromValue(value: e->before())) : comboBox->count(); |
| 688 | const int newItem = ::TrueCurrentItem(sa: this); |
| 689 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::eventFilter(ActionAdded)" |
| 690 | // << " comboBox: ptr=" << comboBox |
| 691 | // << " currentItem=" << comboBox->currentIndex () |
| 692 | // << " add index=" << index |
| 693 | // << " action new: e->before=" << e->before () |
| 694 | // << " ptr=" << e->action () |
| 695 | // << " icon=" << e->action ()->icon () |
| 696 | // << " text=" << e->action ()->text () |
| 697 | // << " currentItem=" << newItem |
| 698 | // << endl; |
| 699 | comboBox->insertItem(index, icon: e->action()->icon(), text: ::DropAmpersands(text: e->action()->text()), userData: QVariant::fromValue(value: e->action())); |
| 700 | if (QStandardItemModel *model = qobject_cast<QStandardItemModel *>(object: comboBox->model())) { |
| 701 | QStandardItem *item = model->item(row: index); |
| 702 | item->setEnabled(e->action()->isEnabled()); |
| 703 | } |
| 704 | |
| 705 | // Inserting an item into a combobox can change the current item so |
| 706 | // make sure the item corresponding to the checked action is selected. |
| 707 | comboBox->setCurrentIndex(newItem); |
| 708 | } else if (event->type() == QEvent::ActionChanged) { |
| 709 | QActionEvent *const e = static_cast<QActionEvent *>(event); |
| 710 | |
| 711 | const int index = comboBox->findData(data: QVariant::fromValue(value: e->action())); |
| 712 | const int newItem = ::TrueCurrentItem(sa: this); |
| 713 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::eventFilter(ActionChanged)" |
| 714 | // << " comboBox: ptr=" << comboBox |
| 715 | // << " currentItem=" << comboBox->currentIndex () |
| 716 | // << " changed action's index=" << index |
| 717 | // << " action new: ptr=" << e->action () |
| 718 | // << " icon=" << e->action ()->icon () |
| 719 | // << " text=" << e->action ()->text () |
| 720 | // << " currentItem=" << newItem |
| 721 | // << endl; |
| 722 | comboBox->setItemIcon(index, icon: e->action()->icon()); |
| 723 | comboBox->setItemText(index, text: ::DropAmpersands(text: e->action()->text())); |
| 724 | if (QStandardItemModel *model = qobject_cast<QStandardItemModel *>(object: comboBox->model())) { |
| 725 | QStandardItem *item = model->item(row: index); |
| 726 | item->setEnabled(e->action()->isEnabled()); |
| 727 | } |
| 728 | |
| 729 | // The checked action may have become unchecked so |
| 730 | // make sure the item corresponding to the checked action is selected. |
| 731 | comboBox->setCurrentIndex(newItem); |
| 732 | } else if (event->type() == QEvent::ActionRemoved) { |
| 733 | QActionEvent *const e = static_cast<QActionEvent *>(event); |
| 734 | |
| 735 | const int index = comboBox->findData(data: QVariant::fromValue(value: e->action())); |
| 736 | const int newItem = ::TrueCurrentItem(sa: this); |
| 737 | // qCDebug(KWidgetsAddonsLog) << "KSelectAction::eventFilter(ActionRemoved)" |
| 738 | // << " comboBox: ptr=" << comboBox |
| 739 | // << " currentItem=" << comboBox->currentIndex () |
| 740 | // << " delete action index=" << index |
| 741 | // << " new: currentItem=" << newItem |
| 742 | // << endl; |
| 743 | comboBox->removeItem(index); |
| 744 | |
| 745 | // Removing an item from a combobox can change the current item so |
| 746 | // make sure the item corresponding to the checked action is selected. |
| 747 | comboBox->setCurrentIndex(newItem); |
| 748 | } |
| 749 | |
| 750 | comboBox->blockSignals(b: blocked); |
| 751 | |
| 752 | return false /*propagate event*/; |
| 753 | } |
| 754 | |
| 755 | // END |
| 756 | |
| 757 | #include "moc_kselectaction.cpp" |
| 758 | |