2 * pickfileradio.cpp - radio button with an associated file picker
4 * Copyright © 2005,2009 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "pickfileradio.h"
24 #include "buttongroup.h"
27 #include <QPushButton>
29 #include "kalarm_debug.h"
32 PickFileRadio::PickFileRadio(QPushButton
* button
, LineEdit
* edit
, const QString
& text
, ButtonGroup
* group
, QWidget
* parent
)
33 : RadioButton(text
, parent
),
36 mLastButton(Q_NULLPTR
),
43 PickFileRadio::PickFileRadio(const QString
& text
, ButtonGroup
* group
, QWidget
* parent
)
44 : RadioButton(text
, parent
),
48 mLastButton(Q_NULLPTR
),
54 void PickFileRadio::init(QPushButton
* button
, LineEdit
* edit
)
58 mEdit
->disconnect(this);
61 mButton
->setEnabled(false);
62 connect(mButton
, &QPushButton::clicked
, this, &PickFileRadio::slotPickFile
);
65 mEdit
->setEnabled(false);
66 connect(mEdit
, &LineEdit::textChanged
, this, &PickFileRadio::fileChanged
);
68 connect(mGroup
, &ButtonGroup::buttonSet
, this, &PickFileRadio::slotSelectionChanged
);
69 setReadOnly(RadioButton::isReadOnly());
72 void PickFileRadio::setReadOnly(bool ro
)
74 RadioButton::setReadOnly(ro
);
78 mEdit
->setReadOnly(ro
);
86 void PickFileRadio::setFile(const QString
& file
)
91 QString
PickFileRadio::file() const
93 return mEdit
? mEdit
->text() : mFile
;
96 /******************************************************************************
97 * Set the radio button enabled or disabled.
98 * Adjusts the enabled/disabled state of other controls appropriately.
100 void PickFileRadio::setEnabled(bool enable
)
103 RadioButton::setEnabled(enable
);
104 enable
= enable
&& mGroup
->checkedButton() == this;
107 if (!pickFileIfNone())
108 enable
= false; // revert to previously selected type
110 mButton
->setEnabled(enable
);
112 mEdit
->setEnabled(enable
);
115 /******************************************************************************
116 * Called when the selected radio button changes.
118 void PickFileRadio::slotSelectionChanged(QAbstractButton
* button
)
120 if (button
== mLastButton
|| mRevertButton
)
122 if (mLastButton
== this)
124 mButton
->setEnabled(false);
126 mEdit
->setEnabled(false);
128 else if (button
== this)
130 if (!pickFileIfNone())
131 return; // revert to previously selected type
132 mButton
->setEnabled(true);
134 mEdit
->setEnabled(true);
136 mLastButton
= button
;
139 /******************************************************************************
140 * Prompt for a file name if there is none currently entered.
142 bool PickFileRadio::pickFileIfNone()
145 mFile
= mEdit
->text();
146 if (!mFile
.isEmpty())
148 return !slotPickFile().isEmpty();
151 /******************************************************************************
152 * Called when the file picker button is clicked.
153 * Reply = mFile, or null string if dialogue, and 'this', was deleted.
155 QString
PickFileRadio::slotPickFile()
157 // To avoid crashes on application quit, we need to check whether the
158 // dialogue, and hence this PickFileRadio, was deleted while active,
159 // before accessing class members.
160 QString file
= pickFile();
162 return file
; // 'this' is probably invalid now
167 mEdit
->setText(mFile
);
171 // No file is selected, so revert to the previous radio button selection.
172 // But wait a moment before setting the radio button, or it won't work.
173 mRevertButton
= true; // prevent picker dialog popping up twice
174 QTimer::singleShot(0, this, &PickFileRadio::setLastButton
);
179 /******************************************************************************
180 * Select the previously selected radio button in the group.
182 void PickFileRadio::setLastButton()
185 setChecked(false); // we don't know the previous selection, so just turn this button off
187 mLastButton
->setChecked(true);
188 mRevertButton
= false;