summaryrefslogtreecommitdiffstats
path: root/kalarm/pickfileradio.cpp
blob: bdfb2bb451ab554cf3a837e348da25d53b6ab81b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *  pickfileradio.cpp  -  radio button with an associated file picker
 *  Program:  kalarm
 *  Copyright (C) 2005 by David Jarvie <software@astrojar.org.uk>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kalarm.h"

#include <tqbuttongroup.h>
#include <tqpushbutton.h>
#include <tqtimer.h>

#include <kdebug.h>

#include "lineedit.h"
#include "pickfileradio.moc"


PickFileRadio::PickFileRadio(TQPushButton* button, LineEdit* edit, const TQString& text, TQButtonGroup* parent, const char* name)
	: RadioButton(text, parent, name),
	  mGroup(parent),
	  mEdit(edit),
	  mButton(button),
	  mLastId(-1),     // set to an invalid value
	  mRevertId(false)
{
	Q_ASSERT(parent);
	Q_ASSERT(button);
	mButton->setEnabled(false);
	connect(mButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotPickFile()));
	if (mEdit)
		mEdit->setEnabled(false);
	connect(mGroup, TQ_SIGNAL(buttonSet(int)), TQ_SLOT(slotSelectionChanged(int)));
}

PickFileRadio::PickFileRadio(const TQString& text, TQButtonGroup* parent, const char* name)
	: RadioButton(text, parent, name),
	  mGroup(parent),
	  mEdit(0),
	  mButton(0),
	  mLastId(-1),     // set to an invalid value
	  mRevertId(false)
{
	Q_ASSERT(parent);
}

void PickFileRadio::init(TQPushButton* button, LineEdit* edit)
{
	Q_ASSERT(button);
	mEdit   = edit;
	mButton = button;
	mButton->setEnabled(false);
	connect(mButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotPickFile()));
	if (mEdit)
		mEdit->setEnabled(false);
	connect(mGroup, TQ_SIGNAL(buttonSet(int)), TQ_SLOT(slotSelectionChanged(int)));
	setReadOnly(RadioButton::isReadOnly());
}

void PickFileRadio::setReadOnly(bool ro)
{
	RadioButton::setReadOnly(ro);
	if (mButton)
	{
		if (mEdit)
			mEdit->setReadOnly(ro);
		if (ro)
			mButton->hide();
		else
			mButton->show();
	}
}

void PickFileRadio::setFile(const TQString& file)
{
	mFile = file;
}

TQString PickFileRadio::file() const
{
	return mEdit ? mEdit->text() : mFile;
}

/******************************************************************************
* Set the radio button enabled or disabled.
* Adjusts the enabled/disabled state of other controls appropriately.
*/
void PickFileRadio::setEnabled(bool enable)
{
	Q_ASSERT(mButton);
	RadioButton::setEnabled(enable);
	enable = enable  &&  mGroup->selected() == this;
	if (enable)
	{
		if (!pickFileIfNone())
			enable = false;    // revert to previously selected type
	}
	mButton->setEnabled(enable);
	if (mEdit)
		mEdit->setEnabled(enable);
}

/******************************************************************************
* Called when the selected radio button changes.
*/
void PickFileRadio::slotSelectionChanged(int id)
{
	if (id == mLastId  ||  mRevertId)
		return;
	int radioId = mGroup->id(this);
	if (mLastId == radioId)
	{
		mButton->setEnabled(false);
		if (mEdit)
			mEdit->setEnabled(false);
	}
	else if (id == radioId)
	{
		if (!pickFileIfNone())
			return;    // revert to previously selected type
		mButton->setEnabled(true);
		if (mEdit)
			mEdit->setEnabled(true);
	}
	mLastId = id;
}

/******************************************************************************
* Prompt for a file name if there is none currently entered.
*/
bool PickFileRadio::pickFileIfNone()
{
	if (mEdit)
		mFile = mEdit->text();
	if (!mFile.isEmpty())
		return true;
	slotPickFile();
	return !mFile.isEmpty();
}

/******************************************************************************
* Called when the file picker button is clicked.
*/
void PickFileRadio::slotPickFile()
{
	mFile = pickFile();
	if (mEdit)
		mEdit->setText(mFile);
	if (mFile.isEmpty())
	{
		// No file is selected, so revert to the previous radio button selection.
		// But wait a moment before setting the radio button, or it won't work.
		mRevertId = true;   // prevent picker dialogue popping up twice
		TQTimer::singleShot(0, this, TQ_SLOT(setLastId()));
	}
}

/******************************************************************************
* Select the previously selected radio button in the group.
*/
void PickFileRadio::setLastId()
{
	if (mLastId == -1)
		setOn(false);    // we don't know the previous selection, so just turn this button off
	else
		mGroup->setButton(mLastId);
	mRevertId = false;
}