File indexing completed on 2024-05-12 05:14:53
0001 /* 0002 * pickfileradio.h - radio button with an associated file picker 0003 * Program: kalarm 0004 * SPDX-FileCopyrightText: 2005-2019 David Jarvie <djarvie@kde.org> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #pragma once 0010 0011 /** @file pickfileradio.h - radio button with an associated file picker */ 0012 0013 #include "lib/radiobutton.h" 0014 0015 class QPushButton; 0016 class ButtonGroup; 0017 class LineEdit; 0018 0019 /** 0020 * @short Radio button with associated file picker controls. 0021 * 0022 * The PickFileRadio class is a radio button with an associated button to choose 0023 * a file, and an optional file name edit box. Its purpose is to ensure that while 0024 * the radio button is selected, the chosen file name is never blank. 0025 * 0026 * To achieve this, whenever the button is newly selected and the 0027 * file name is currently blank, the file picker dialog is displayed to choose a 0028 * file. If the dialog exits without a file being chosen, the radio button selection 0029 * is reverted to the previously selected button in the parent button group. 0030 * 0031 * The class handles the activation of the file picker dialog (via a virtual method 0032 * which must be supplied by deriving a class from this one). It also handles all 0033 * enabling and disabling of the browse button and edit box when the enable state of 0034 * the radio button is changed, and when the radio button selection changes. 0035 * 0036 * @author David Jarvie <djarvie@kde.org> 0037 */ 0038 class PickFileRadio : public RadioButton 0039 { 0040 Q_OBJECT 0041 public: 0042 /** Constructor. 0043 * @param button Push button to invoke the file picker dialog 0044 * @param edit File name edit widget, or null if there is none 0045 * @param text Radio button's text 0046 * @param group The button group which the radio button will be part of 0047 * @param parent Button group which is to be the parent object for the radio button 0048 */ 0049 PickFileRadio(QPushButton* button, LineEdit* edit, const QString& text, ButtonGroup* group, QWidget* parent); 0050 0051 /** Constructor. 0052 * The init() method must be called before the widget can be used. 0053 * @param text Radio button's text 0054 * @param group The button group which the radio button will be part of 0055 * @param parent Button group which is to be the parent object for the radio button 0056 */ 0057 PickFileRadio(const QString& text, ButtonGroup* group, QWidget* parent); 0058 0059 /** Initialises the widget. 0060 * @param button Push button to invoke the file picker dialog 0061 * @param edit File name edit widget, or null if there is none 0062 */ 0063 void init(QPushButton* button, LineEdit* edit = nullptr); 0064 0065 /** Sets whether the radio button and associated widgets are read-only for the user. 0066 * If read-only, their states cannot be changed by the user. 0067 * @param readOnly true to set the widgets read-only, false to set them read-write 0068 */ 0069 void setReadOnly(bool readOnly) override; 0070 0071 /** Chooses a file, for example by displaying a file selection dialog. 0072 * This method is called when the push button is clicked - the client 0073 * should not activate a file selection dialog directly. 0074 * @param file Updated with the selected file name, or empty if no 0075 * selection was made. 0076 * @return true if @p file value can be used, 0077 * false if the dialog was deleted while visible. 0078 */ 0079 virtual bool pickFile(QString& file) = 0; 0080 0081 /** Notifies the widget of the currently selected file name. 0082 * This should only be used when no file name edit box is used. 0083 * It should be called to initialise the widget's data, and also any time the file 0084 * name is changed without using the push button. 0085 */ 0086 void setFile(const QString& file); 0087 0088 /** Returns the currently selected file name. */ 0089 QString file() const; 0090 0091 /** Returns the associated file name edit widget, or null if none. */ 0092 LineEdit* fileEdit() const { return mEdit; } 0093 0094 /** Returns the associated file browse push button. */ 0095 QPushButton* pushButton() const { return mButton; } 0096 0097 public Q_SLOTS: 0098 /** Enables or disables the radio button, and adjusts the enabled state of the 0099 * associated browse button and file name edit box. 0100 */ 0101 virtual void setEnabled(bool); 0102 0103 Q_SIGNALS: 0104 void fileChanged(); // emitted whenever the selected file changes 0105 0106 private Q_SLOTS: 0107 void slotSelectionChanged(QAbstractButton*); 0108 void slotPickFile(); 0109 void setLastButton(); 0110 0111 private: 0112 bool doPickFile(QString& file); 0113 bool pickFileIfNone(); 0114 0115 ButtonGroup* mGroup; // button group which radio button is in 0116 LineEdit* mEdit {nullptr}; // file name edit box, or null if none 0117 QPushButton* mButton; // push button to pick a file 0118 QString mFile; // saved file name (if mEdit is null) 0119 QAbstractButton* mLastButton {nullptr}; // previous radio button selected 0120 bool mRevertButton {false}; // true to revert to the previous radio button selection 0121 }; 0122 0123 // vim: et sw=4: