]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/actionwithwidget.h
Apply 2 suggestion(s) to 1 file(s)
[dolphin.git] / src / selectionmode / actionwithwidget.h
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #ifndef ACTIONWITHWIDGET_H
9 #define ACTIONWITHWIDGET_H
10
11 #include <QAction>
12 #include <QPointer>
13 #include <QWidget>
14
15 class QAbstractButton;
16
17 namespace SelectionMode
18 {
19
20 /**
21 * @brief Small wrapper/helper class that contains an action and its widget.
22 *
23 * This class takes neither the responsibility for deleting its action() nor its widget().
24 *
25 * This class is only used from BottomBarContentsContainer currently.
26 * @see BottomBarContentsContainer
27 */
28 class ActionWithWidget
29 {
30 public:
31 ActionWithWidget(QAction *action);
32
33 /**
34 * Connect @p action and @p button using copyActionDataToButton() and
35 * wraps the two together in a ActionWithWidget object.
36 * ActionWithWidget doesn't take any ownership over the parameters.
37 *
38 * @see copyActionDataToButton()
39 *
40 * @param button the button to be styled and used to fit the @p action.
41 */
42 ActionWithWidget(QAction *action, QAbstractButton *button);
43
44 /** @returns the action of this object. */
45 inline QAction *action()
46 {
47 Q_CHECK_PTR(m_action);
48 return m_action;
49 };
50
51 /** @returns the widget of this object. */
52 inline QWidget *widget()
53 {
54 return m_widget;
55 }
56
57 /**
58 * @returns a widget with parent @p parent for the action() of this object.
59 *
60 * For most actions some sort of button will be returned. For separators a vertical line will be returned.
61 * If this ActionWithWidget already has a widget(), this method will crash.
62 */
63 QWidget *newWidget(QWidget *parent);
64
65 /** returns true if the widget exists and is visible. false otherwise. */
66 inline bool isWidgetVisible() const
67 {
68 return m_widget && m_widget->isVisible();
69 };
70
71 private:
72 QPointer<QAction> m_action;
73 QPointer<QWidget> m_widget;
74 };
75
76 /**
77 * A small helper method.
78 * @return a button with the correct styling for the general mode of the BottomBarContentsContainer which can be added to its layout.
79 */
80 QAbstractButton *newButtonForAction(QAction *action, QWidget *parent);
81
82 /**
83 * Normally, if one wants a button that represents a QAction one would use a QToolButton
84 * and simply call QToolButton::setDefaultAction(action). However if one does this, all
85 * control over the style, text, etc. of the button is forfeited. One can't for example
86 * have text on the button then, if the action has a low QAction::priority().
87 *
88 * This method styles the @p button based on the @p action without using QToolButton::setDefaultAction().
89 *
90 * Another reason why this is necessary is because the actions have application-wide scope while
91 * these buttons belong to one ViewContainer.
92 */
93 void copyActionDataToButton(QAbstractButton *button, QAction *action);
94
95 }
96
97 #endif // ACTIONWITHWIDGET_H