kalarm

pickfileradio.cpp
1 /*
2  * pickfileradio.cpp - radio button with an associated file picker
3  * Program: kalarm
4  * Copyright (C) 2005 by David Jarvie <software@astrojar.org.uk>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "kalarm.h"
22 
23 #include <tqbuttongroup.h>
24 #include <tqpushbutton.h>
25 #include <tqtimer.h>
26 
27 #include <kdebug.h>
28 
29 #include "lineedit.h"
30 #include "pickfileradio.moc"
31 
32 
33 PickFileRadio::PickFileRadio(TQPushButton* button, LineEdit* edit, const TQString& text, TQButtonGroup* parent, const char* name)
34  : RadioButton(text, parent, name),
35  mGroup(parent),
36  mEdit(edit),
37  mButton(button),
38  mLastId(-1), // set to an invalid value
39  mRevertId(false)
40 {
41  Q_ASSERT(parent);
42  Q_ASSERT(button);
43  mButton->setEnabled(false);
44  connect(mButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotPickFile()));
45  if (mEdit)
46  mEdit->setEnabled(false);
47  connect(mGroup, TQ_SIGNAL(buttonSet(int)), TQ_SLOT(slotSelectionChanged(int)));
48 }
49 
50 PickFileRadio::PickFileRadio(const TQString& text, TQButtonGroup* parent, const char* name)
51  : RadioButton(text, parent, name),
52  mGroup(parent),
53  mEdit(0),
54  mButton(0),
55  mLastId(-1), // set to an invalid value
56  mRevertId(false)
57 {
58  Q_ASSERT(parent);
59 }
60 
61 void PickFileRadio::init(TQPushButton* button, LineEdit* edit)
62 {
63  Q_ASSERT(button);
64  mEdit = edit;
65  mButton = button;
66  mButton->setEnabled(false);
67  connect(mButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotPickFile()));
68  if (mEdit)
69  mEdit->setEnabled(false);
70  connect(mGroup, TQ_SIGNAL(buttonSet(int)), TQ_SLOT(slotSelectionChanged(int)));
71  setReadOnly(RadioButton::isReadOnly());
72 }
73 
75 {
76  RadioButton::setReadOnly(ro);
77  if (mButton)
78  {
79  if (mEdit)
80  mEdit->setReadOnly(ro);
81  if (ro)
82  mButton->hide();
83  else
84  mButton->show();
85  }
86 }
87 
88 void PickFileRadio::setFile(const TQString& file)
89 {
90  mFile = file;
91 }
92 
93 TQString PickFileRadio::file() const
94 {
95  return mEdit ? mEdit->text() : mFile;
96 }
97 
98 /******************************************************************************
99 * Set the radio button enabled or disabled.
100 * Adjusts the enabled/disabled state of other controls appropriately.
101 */
102 void PickFileRadio::setEnabled(bool enable)
103 {
104  Q_ASSERT(mButton);
105  RadioButton::setEnabled(enable);
106  enable = enable && mGroup->selected() == this;
107  if (enable)
108  {
109  if (!pickFileIfNone())
110  enable = false; // revert to previously selected type
111  }
112  mButton->setEnabled(enable);
113  if (mEdit)
114  mEdit->setEnabled(enable);
115 }
116 
117 /******************************************************************************
118 * Called when the selected radio button changes.
119 */
120 void PickFileRadio::slotSelectionChanged(int id)
121 {
122  if (id == mLastId || mRevertId)
123  return;
124  int radioId = mGroup->id(this);
125  if (mLastId == radioId)
126  {
127  mButton->setEnabled(false);
128  if (mEdit)
129  mEdit->setEnabled(false);
130  }
131  else if (id == radioId)
132  {
133  if (!pickFileIfNone())
134  return; // revert to previously selected type
135  mButton->setEnabled(true);
136  if (mEdit)
137  mEdit->setEnabled(true);
138  }
139  mLastId = id;
140 }
141 
142 /******************************************************************************
143 * Prompt for a file name if there is none currently entered.
144 */
145 bool PickFileRadio::pickFileIfNone()
146 {
147  if (mEdit)
148  mFile = mEdit->text();
149  if (!mFile.isEmpty())
150  return true;
151  slotPickFile();
152  return !mFile.isEmpty();
153 }
154 
155 /******************************************************************************
156 * Called when the file picker button is clicked.
157 */
158 void PickFileRadio::slotPickFile()
159 {
160  mFile = pickFile();
161  if (mEdit)
162  mEdit->setText(mFile);
163  if (mFile.isEmpty())
164  {
165  // No file is selected, so revert to the previous radio button selection.
166  // But wait a moment before setting the radio button, or it won't work.
167  mRevertId = true; // prevent picker dialogue popping up twice
168  TQTimer::singleShot(0, this, TQ_SLOT(setLastId()));
169  }
170 }
171 
172 /******************************************************************************
173 * Select the previously selected radio button in the group.
174 */
175 void PickFileRadio::setLastId()
176 {
177  if (mLastId == -1)
178  setOn(false); // we don't know the previous selection, so just turn this button off
179  else
180  mGroup->setButton(mLastId);
181  mRevertId = false;
182 }
void init(TQPushButton *button, LineEdit *edit=0)
Initialises the widget.
PickFileRadio(TQPushButton *button, LineEdit *edit, const TQString &text, TQButtonGroup *parent, const char *name=0)
Constructor.
virtual void setEnabled(bool)
Enables or disables the radio button, and adjusts the enabled state of the associated browse button a...
void setFile(const TQString &file)
Notifies the widget of the currently selected file name.
virtual TQString pickFile()=0
Chooses a file, for example by displaying a file selection dialogue.
virtual void setReadOnly(bool readOnly)
Sets whether the radio button and associated widgets are read-only for the user.
TQString file() const
Returns the currently selected file name.