]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/actionwithwidget.h
722fdf2843c1352e41c6bd0c8fc0845269e6025f
[dolphin.git] / src / selectionmode / actionwithwidget.h
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <fe.a.ernst@gmail.com>
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 /**
18 * @brief Small wrapper/helper class that contains an action and its widget.
19 *
20 * This class takes neither the responsibility for deleting its action() nor its widget().
21 */
22 class ActionWithWidget
23 {
24 public:
25 ActionWithWidget(QAction *action);
26
27 /**
28 * Connect @p action and @p button using copyActionDataToButton() and the
29 * wraps the two together in the ActionWithWidget object.
30 * ActionWithWidget doesn't take any ownership.
31 *
32 * @see copyActionDataToButton()
33 *
34 * @param button the button to be styled and used to fit the @p action.
35 */
36 ActionWithWidget(QAction *action, QAbstractButton *button);
37
38 /** @returns the action of this object. Crashes if that action has been deleted elsewhere in the meantime. */
39 inline QAction *action() {
40 Q_CHECK_PTR(m_action);
41 return m_action;
42 };
43
44 /** @returns the widget of this object. */
45 inline QWidget *widget() {
46 return m_widget;
47 }
48
49 /**
50 * @returns a widget with parent @p parent for the action() of this object.
51 *
52 * For most actions some sort of button will be returned. For separators a vertical line will be returned.
53 * If this ActionWithWidget already has a widget(), this method will crash.
54 */
55 QWidget *newWidget(QWidget *parent);
56
57 /** returns true if the widget exists and is visible. false otherwise. */
58 inline bool isWidgetVisible() const {
59 return m_widget && m_widget->isVisible();
60 };
61
62 private:
63 QPointer<QAction> m_action;
64 QPointer<QWidget> m_widget;
65 };
66
67 /**
68 * A small helper method.
69 * @return a button with the correct styling for the general mode of the SelectionModeBottomBar which can be added to its layout.
70 */
71 QAbstractButton *newButtonForAction(QAction *action, QWidget *parent);
72
73 /**
74 * Normally, if one wants a button that represents a QAction one would use a QToolButton
75 * and simply call QToolButton::setDefaultAction(action). However if one does this, all
76 * control over the style, text, etc. of the button is forfeited. One can't for example
77 * have text on the button then, if the action has a low QAction::priority().
78 *
79 * This method styles the @p button based on the @p action without using QToolButton::setDefaultAction().
80 *
81 * Another reason why this is necessary is because the actions have application-wide scope while
82 * these buttons belong to one ViewContainer.
83 */
84 void copyActionDataToButton(QAbstractButton *button, QAction *action);
85
86 #endif // ACTIONWITHWIDGET_H