33 #include "kselectaction_p.h" 35 #include <QActionEvent> 37 #include <QToolButton> 39 #include <QStandardItem> 58 , d_ptr(new KSelectActionPrivate())
66 , d_ptr(new KSelectActionPrivate())
75 , d_ptr(new KSelectActionPrivate())
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 );
107 return d->m_actionGroup;
144 kWarning (129) <<
"Action does not have the correct properties to be current:" <<
action->text();
146 kWarning (129) <<
"Action does not belong to group:" <<
action->text();
173 if (cs == Qt::CaseSensitive)
176 compare = text.toLower();
180 if (cs == Qt::CaseSensitive) {
181 if (text == compare) {
185 }
else if (cs == Qt::CaseInsensitive) {
186 if (text.toLower() == compare) {
207 d->m_comboWidth = width;
209 foreach (
KComboBox* box, d->m_comboBoxes)
210 box->setMaximumWidth(d->m_comboWidth);
218 d->m_maxComboViewCount = n;
220 foreach (
KComboBox* box, d->m_comboBoxes)
221 if ( d->m_maxComboViewCount != -1 )
222 box->setMaxVisibleItems(d->m_maxComboViewCount);
225 box->setMaxVisibleItems(10);
242 button->setEnabled(
true);
243 button->addAction(
action);
246 foreach (
KComboBox* comboBox, d->m_comboBoxes) {
247 comboBox->setEnabled(
true);
248 comboBox->addAction(
action);
251 menu()->addAction(
action);
258 newAction->setText(text);
259 newAction->setCheckable(
true );
262 if (!d->m_menuAccelsEnabled) {
263 newAction->setText(text);
274 newAction->setIcon(icon);
286 d->m_actionGroup->removeAction(
action);
290 setEnabled( !hasActions );
293 button->setEnabled( !hasActions );
294 button->removeAction(
action);
297 foreach (
KComboBox* comboBox, d->m_comboBoxes)
299 comboBox->setEnabled( !hasActions );
300 comboBox->removeAction(
action);
303 menu()->removeAction(
action);
340 if ( index < 0 || index >=
actions().count() )
342 kWarning() <<
"KSelectAction::changeItem Index out of scope";
346 actions()[index]->setText( d->makeMenuText( text ) );
356 foreach (
const QString&
string, lst) {
357 if ( !
string.isEmpty() ) {
361 action->setSeparator(
true);
367 setEnabled( lst.count() > 0 || d->m_edit );
373 return d->m_comboWidth;
384 for (
int i = 0; i <
actions.count(); ++i)
400 while (d->m_actionGroup->actions().count())
409 foreach (
KComboBox* comboBox, d->m_comboBoxes)
431 return d->m_toolBarMode;
437 d->m_toolBarMode = mode;
443 return d->m_toolButtonPopupMode;
449 d->m_toolButtonPopupMode = mode;
452 void KSelectActionPrivate::_k_comboBoxDeleted(
QObject*
object)
454 foreach (
KComboBox* comboBox, m_comboBoxes)
455 if (
object == comboBox) {
456 m_comboBoxes.removeAll(static_cast<KComboBox*>(
object));
461 void KSelectActionPrivate::_k_comboBoxCurrentIndexChanged(
int index)
466 KComboBox *triggeringCombo = qobject_cast <KComboBox *> (q->sender ());
475 triggeringCombo && triggeringCombo->count () > 0 &&
476 index == triggeringCombo->count () - 1) {
479 const QString newItemText = triggeringCombo->currentText ();
483 bool blocked = triggeringCombo->blockSignals (
true);
484 triggeringCombo->removeItem (index);
485 triggeringCombo->blockSignals (blocked);
489 newAction->trigger();
501 d->m_menuAccelsEnabled = b;
507 return d->m_menuAccelsEnabled;
513 QMenu *menu = qobject_cast<QMenu *>(parent);
517 QToolBar *toolBar = qobject_cast<QToolBar *>(parent);
524 button->setToolTip(toolTip());
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);
542 d->m_buttons.append(button);
548 comboBox->installEventFilter (
this);
550 if ( d->m_maxComboViewCount != -1 )
551 comboBox->setMaxVisibleItems( d->m_maxComboViewCount );
553 if ( d->m_comboWidth > 0 )
554 comboBox->setMaximumWidth( d->m_comboWidth );
557 comboBox->setToolTip(toolTip());
559 comboBox->setStatusTip(statusTip());
562 comboBox->addAction(
action);
565 comboBox->setEnabled(
false);
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);
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);
591 if (
event->type() == QEvent::ActionChanged) {
592 Q_FOREACH(
KComboBox* comboBox, d->m_comboBoxes) {
593 comboBox->setToolTip(toolTip());
595 comboBox->setStatusTip(statusTip());
598 toolButton->setToolTip(toolTip());
600 toolButton->setStatusTip(statusTip());
625 if (action->isChecked ())
630 if (action != curAction)
633 return sa->
actions ().indexOf (action);
640 return (curAction && curAction->isChecked ()) ? sa->
actions ().indexOf (curAction) : -1;
648 KComboBox *comboBox = qobject_cast <KComboBox *> (watched);
655 if (event->type () == QEvent::FocusOut) {
656 QFocusEvent *
const e = static_cast <QFocusEvent *> (event);
662 if (e->reason () != Qt::ActiveWindowFocusReason &&
663 e->reason () != Qt::PopupFocusReason &&
664 e->reason () != Qt::OtherFocusReason) {
667 comboBox->setEditText (comboBox->itemText (comboBox->currentIndex ()));
673 bool blocked = comboBox->blockSignals (
true);
675 if (event->type () == QEvent::ActionAdded)
677 QActionEvent *
const e = static_cast <QActionEvent *> (event);
679 const int index = e->before () ?
680 comboBox->findData (QVariant::fromValue (e->before ())) :
693 comboBox->insertItem (index,
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());
704 comboBox->setCurrentIndex (
newItem);
706 else if (event->type () == QEvent::ActionChanged)
708 QActionEvent *
const e = static_cast <QActionEvent *> (event);
710 const int index = comboBox->findData (QVariant::fromValue (e->action ()));
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());
730 comboBox->setCurrentIndex (
newItem);
732 else if (event->type () == QEvent::ActionRemoved)
734 QActionEvent *
const e = static_cast <QActionEvent *> (event);
736 const int index = comboBox->findData (QVariant::fromValue (e->action ()));
744 comboBox->removeItem (index);
748 comboBox->setCurrentIndex (
newItem);
751 comboBox->blockSignals (blocked);
761 #include "kselectaction.moc"
virtual ~KSelectAction()
Destructor.
QString currentText() const
Returns the text of the currently selected item.
void setComboWidth(int width)
When this action is plugged into a toolbar, it creates a combobox.
void setEditable(bool editable)
"Re-implemented" so that setEditable(true) creates a KLineEdit instead of QLineEdit.
bool menuAccelsEnabled() const
Returns whether ampersands passed to methods using QStrings are interpreted as keyboard accelerator i...
void setToolBarMode(ToolBarMode mode)
Set the type of widget to be inserted in a toolbar to mode.
static int TrueCurrentItem(KSelectAction *sa)
Creates a button which pops up a menu when interacted with, as defined by toolButtonPopupMode().
QAction * action(int index) const
Returns the action at index, if one exists.
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...
int currentItem() const
Returns the index of the current item.
QToolButton::ToolButtonPopupMode toolButtonPopupMode() const
Returns the style for the list of actions, when this action is plugged into a KToolBar.
virtual void addAction(QAction *action)
Add action to the list of selectable actions.
virtual QAction * removeAction(QAction *action)
Remove the specified action from this action selector.
KSelectAction(QObject *parent)
Constructs a selection action with the specified parent.
void setToolButtonPopupMode(QToolButton::ToolButtonPopupMode mode)
Set how this list of actions should behave when in popup mode and plugged into a toolbar.
QAction * currentAction() const
Returns the current QAction.
KSelectActionPrivate * d_ptr
Action for selecting one of several items.
void setEditable(bool)
When this action is plugged into a toolbar, it creates a combobox.
void setShortcutConfigurable(bool configurable)
Indicate whether the user may configure the action's shortcut.
bool setCurrentItem(int index)
QActionGroup * selectableActionGroup() const
The action group used to create exclusivity between the actions associated with this action.
static QString DropAmpersands(const QString &text)
QList< QAction * > actions() const
Returns the list of selectable actions.
virtual QWidget * createWidget(QWidget *parent)
Reimplemented from.
void setItems(const QStringList &lst)
Convenience function to create the list of selectable items.
A wrapper around QIcon that provides KDE icon features.
virtual void deleteWidget(QWidget *widget)
Reimplemented from.
void setMenuAccelsEnabled(bool b)
Sets whether any occurrence of the ampersand character ( & ) in items should be interpreted as keyboa...
void setShortcut(const KShortcut &shortcut, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))
Set the shortcut for this action.
void slotToggled(bool)
For structured menu building.
KAction(QObject *parent)
Constructs an action.
int comboWidth() const
When this action is plugged into a toolbar, it creates a combobox.
Creates a combo box which contains the actions.
KAction * whatsThis(const QObject *recvr, const char *slot, QObject *parent)
Trigger the What's This cursor.
void changeItem(int index, const QString &text)
Changes the text of item.
QStringList items() const
Convenience function which returns the items that can be selected with this action.
Class to encapsulate user-driven action or event.
void clear()
Clears up all the items in this action.
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
virtual void actionTriggered(QAction *action)
This function is called whenever an action from the selections is triggered.
ToolBarMode toolBarMode() const
Returns which type of widget (combo box or button with drop-down menu) will be inserted in a toolbar.
QString newItem(const QString &type, const QString &name, const QString &key, const QString &defaultValue, const CfgConfig &cfg, const QString ¶m=QString())
void triggered(QAction *action)
This signal is emitted when an item is selected;.
bool setCurrentAction(QAction *action)
Sets the currently checked item.
virtual bool event(QEvent *event)
bool isEditable() const
When this action is plugged into a toolbar, it creates a combobox.
QString removeAcceleratorMarker(const QString &label) const