• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

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

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal