File indexing completed on 2024-04-28 05:45:14
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #ifndef ACTIONWITHWIDGET_H 0009 #define ACTIONWITHWIDGET_H 0010 0011 #include <QAction> 0012 #include <QPointer> 0013 #include <QWidget> 0014 0015 class QAbstractButton; 0016 0017 namespace SelectionMode 0018 { 0019 0020 /** 0021 * @brief Small wrapper/helper class that contains an action and its widget. 0022 * 0023 * This class takes neither the responsibility for deleting its action() nor its widget(). 0024 * 0025 * This class is only used from BottomBarContentsContainer currently. 0026 * @see BottomBarContentsContainer 0027 */ 0028 class ActionWithWidget 0029 { 0030 public: 0031 ActionWithWidget(QAction *action); 0032 0033 /** 0034 * Connect @p action and @p button using copyActionDataToButton() and 0035 * wraps the two together in a ActionWithWidget object. 0036 * ActionWithWidget doesn't take any ownership over the parameters. 0037 * 0038 * @see copyActionDataToButton() 0039 * 0040 * @param button the button to be styled and used to fit the @p action. 0041 */ 0042 ActionWithWidget(QAction *action, QAbstractButton *button); 0043 0044 /** @returns the action of this object. */ 0045 inline QAction *action() 0046 { 0047 Q_CHECK_PTR(m_action); 0048 return m_action; 0049 }; 0050 0051 /** @returns the widget of this object. */ 0052 inline QWidget *widget() 0053 { 0054 return m_widget; 0055 } 0056 0057 /** 0058 * @returns a widget with parent @p parent for the action() of this object. 0059 * 0060 * For most actions some sort of button will be returned. For separators a vertical line will be returned. 0061 * If this ActionWithWidget already has a widget(), this method will crash. 0062 */ 0063 QWidget *newWidget(QWidget *parent); 0064 0065 /** returns true if the widget exists and is visible. false otherwise. */ 0066 inline bool isWidgetVisible() const 0067 { 0068 return m_widget && m_widget->isVisible(); 0069 }; 0070 0071 private: 0072 QPointer<QAction> m_action; 0073 QPointer<QWidget> m_widget; 0074 }; 0075 0076 /** 0077 * A small helper method. 0078 * @return a button with the correct styling for the general mode of the BottomBarContentsContainer which can be added to its layout. 0079 */ 0080 QAbstractButton *newButtonForAction(QAction *action, QWidget *parent); 0081 0082 /** 0083 * Normally, if one wants a button that represents a QAction one would use a QToolButton 0084 * and simply call QToolButton::setDefaultAction(action). However if one does this, all 0085 * control over the style, text, etc. of the button is forfeited. One can't for example 0086 * have text on the button then, if the action has a low QAction::priority(). 0087 * 0088 * This method styles the @p button based on the @p action without using QToolButton::setDefaultAction(). 0089 * 0090 * Another reason why this is necessary is because the actions have application-wide scope while 0091 * these buttons belong to one ViewContainer. 0092 */ 0093 void copyActionDataToButton(QAbstractButton *button, QAction *action); 0094 0095 } 0096 0097 #endif // ACTIONWITHWIDGET_H