SVN_SILENT made messages (after extraction)
[kdepim.git] / kalarm / pickfileradio.cpp
blobc72b4eedf708daa555b6a4b702203f4173df9806
1 /*
2 * pickfileradio.cpp - radio button with an associated file picker
3 * Program: kalarm
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.
21 #include "kalarm.h"
22 #include "pickfileradio.h"
24 #include "buttongroup.h"
25 #include "lineedit.h"
27 #include <QPushButton>
28 #include <QTimer>
29 #include "kalarm_debug.h"
32 PickFileRadio::PickFileRadio(QPushButton* button, LineEdit* edit, const QString& text, ButtonGroup* group, QWidget* parent)
33 : RadioButton(text, parent),
34 mGroup(group),
35 mEdit(Q_NULLPTR),
36 mLastButton(Q_NULLPTR),
37 mRevertButton(false)
39 Q_ASSERT(parent);
40 init(button, edit);
43 PickFileRadio::PickFileRadio(const QString& text, ButtonGroup* group, QWidget* parent)
44 : RadioButton(text, parent),
45 mGroup(group),
46 mEdit(Q_NULLPTR),
47 mButton(Q_NULLPTR),
48 mLastButton(Q_NULLPTR),
49 mRevertButton(false)
51 Q_ASSERT(parent);
54 void PickFileRadio::init(QPushButton* button, LineEdit* edit)
56 Q_ASSERT(button);
57 if (mEdit)
58 mEdit->disconnect(this);
59 mEdit = edit;
60 mButton = button;
61 mButton->setEnabled(false);
62 connect(mButton, &QPushButton::clicked, this, &PickFileRadio::slotPickFile);
63 if (mEdit)
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);
75 if (mButton)
77 if (mEdit)
78 mEdit->setReadOnly(ro);
79 if (ro)
80 mButton->hide();
81 else
82 mButton->show();
86 void PickFileRadio::setFile(const QString& file)
88 mFile = 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)
102 Q_ASSERT(mButton);
103 RadioButton::setEnabled(enable);
104 enable = enable && mGroup->checkedButton() == this;
105 if (enable)
107 if (!pickFileIfNone())
108 enable = false; // revert to previously selected type
110 mButton->setEnabled(enable);
111 if (mEdit)
112 mEdit->setEnabled(enable);
115 /******************************************************************************
116 * Called when the selected radio button changes.
118 void PickFileRadio::slotSelectionChanged(QAbstractButton* button)
120 if (button == mLastButton || mRevertButton)
121 return;
122 if (mLastButton == this)
124 mButton->setEnabled(false);
125 if (mEdit)
126 mEdit->setEnabled(false);
128 else if (button == this)
130 if (!pickFileIfNone())
131 return; // revert to previously selected type
132 mButton->setEnabled(true);
133 if (mEdit)
134 mEdit->setEnabled(true);
136 mLastButton = button;
139 /******************************************************************************
140 * Prompt for a file name if there is none currently entered.
142 bool PickFileRadio::pickFileIfNone()
144 if (mEdit)
145 mFile = mEdit->text();
146 if (!mFile.isEmpty())
147 return true;
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();
161 if (file.isNull())
162 return file; // 'this' is probably invalid now
163 if (!file.isEmpty())
165 mFile = file;
166 if (mEdit)
167 mEdit->setText(mFile);
169 if (mFile.isEmpty())
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);
176 return mFile;
179 /******************************************************************************
180 * Select the previously selected radio button in the group.
182 void PickFileRadio::setLastButton()
184 if (!mLastButton)
185 setChecked(false); // we don't know the previous selection, so just turn this button off
186 else
187 mLastButton->setChecked(true);
188 mRevertButton = false;
191 // vim: et sw=4: