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