File indexing completed on 2024-04-28 15:32:16

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2004 Felix Berger <felixberger@beldesign.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #ifndef KTOOLBARLABELACTION_H
0009 #define KTOOLBARLABELACTION_H
0010 
0011 #include <QWidgetAction>
0012 #include <memory>
0013 
0014 #include <kwidgetsaddons_export.h>
0015 
0016 /**
0017  * @class KToolBarLabelAction ktoolbarlabelaction.h KToolBarLabelAction
0018  *
0019  * @short Class to display a label in a toolbar.
0020  *
0021  * KToolBarLabelAction is a convenience class for displaying a label in a
0022  * toolbar.
0023  *
0024  * It provides easy access to the label's #setBuddy(QAction*) and #buddy()
0025  * methods and can be used as follows:
0026  *
0027  * \code
0028  *
0029  * KHistoryComboBox *findCombo = new KHistoryComboBox(true, this);
0030  *
0031  * KWidgetAction *action = new KWidgetAction(findCombo, i18n("Find Combo"),
0032  *                                            Qt::Key_F6, this, SLOT( slotFocus()),
0033  *                                            actionCollection(), "find_combo");
0034  *
0035  * QAction *action = new KToolBarLabelAction(action, i18n("Find "), "find_label");
0036  * action->setShortcut(Qt::Key_F6);
0037  * connect(action, &QAction::triggered, this, [this]() { slotFocus(); });
0038  *
0039  * \endcode
0040  *
0041  * @author Felix Berger <felixberger@beldesign.de>
0042  */
0043 class KWIDGETSADDONS_EXPORT KToolBarLabelAction : public QWidgetAction
0044 {
0045     Q_OBJECT
0046 
0047 public:
0048     /**
0049      * Creates a toolbar label.
0050      *
0051      * @param text The label's and the action's text.
0052      * @param parent This action's parent.
0053      */
0054     KToolBarLabelAction(const QString &text, QObject *parent);
0055 
0056     /**
0057      * Creates a toolbar label setting a buddy for the label.
0058      *
0059      * @param buddy The action whose widget which is focused when the label's accelerator is
0060      * typed.
0061      * @param text The label's and the action's text.
0062      * @param parent This action's parent.
0063      */
0064     KToolBarLabelAction(QAction *buddy, const QString &text, QObject *parent);
0065 
0066     /**
0067      * Destroys the toolbar label.
0068      */
0069     ~KToolBarLabelAction() override;
0070 
0071     /**
0072      * Sets the label's buddy to buddy.
0073      *
0074      * See QLabel#setBuddy() for details.
0075      */
0076     void setBuddy(QAction *buddy);
0077 
0078     /**
0079      * Returns the label's buddy or a null pointer if no buddy is currently set.
0080      *
0081      * See QLabel#buddy() and QLabel#setBuddy() for more information.
0082      */
0083     QAction *buddy() const;
0084 
0085     /**
0086      * Reimplemented from QWidgetAction.
0087      */
0088     QWidget *createWidget(QWidget *parent) override;
0089 
0090 Q_SIGNALS:
0091     /**
0092      * This signal is emitted whenever the text of this action
0093      * is changed.
0094      */
0095     void textChanged(const QString &newText);
0096 
0097 protected:
0098     bool event(QEvent *) override;
0099     bool eventFilter(QObject *watched, QEvent *event) override;
0100 
0101 private:
0102     std::unique_ptr<class KToolBarLabelActionPrivate> const d;
0103 };
0104 
0105 #endif