]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/actionwithwidget.h
14db6df2224196d51f4fcaae2af49b47498a44e9
[dolphin.git] / src / selectionmode / actionwithwidget.h
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
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 Q_CHECK_PTR(m_action);
47 return m_action;
48 };
49
50 /** @returns the widget of this object. */
51 inline QWidget *widget() {
52 return m_widget;
53 }
54
55 /**
56 * @returns a widget with parent @p parent for the action() of this object.
57 *
58 * For most actions some sort of button will be returned. For separators a vertical line will be returned.
59 * If this ActionWithWidget already has a widget(), this method will crash.
60 */
61 QWidget *newWidget(QWidget *parent);
62
63 /** returns true if the widget exists and is visible. false otherwise. */
64 inline bool isWidgetVisible() const {
65 return m_widget && m_widget->isVisible();
66 };
67
68 private:
69 QPointer<QAction> m_action;
70 QPointer<QWidget> m_widget;
71 };
72
73 /**
74 * A small helper method.
75 * @return a button with the correct styling for the general mode of the BottomBarContentsContainer which can be added to its layout.
76 */
77 QAbstractButton *newButtonForAction(QAction *action, QWidget *parent);
78
79 /**
80 * Normally, if one wants a button that represents a QAction one would use a QToolButton
81 * and simply call QToolButton::setDefaultAction(action). However if one does this, all
82 * control over the style, text, etc. of the button is forfeited. One can't for example
83 * have text on the button then, if the action has a low QAction::priority().
84 *
85 * This method styles the @p button based on the @p action without using QToolButton::setDefaultAction().
86 *
87 * Another reason why this is necessary is because the actions have application-wide scope while
88 * these buttons belong to one ViewContainer.
89 */
90 void copyActionDataToButton(QAbstractButton *button, QAction *action);
91
92 }
93
94 #endif // ACTIONWITHWIDGET_H