pickfileradio.cpp
00001 /* 00002 * pickfileradio.cpp - radio button with an associated file picker 00003 * Program: kalarm 00004 * Copyright (C) 2005 by David Jarvie <software@astrojar.org.uk> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kalarm.h" 00022 00023 #include <tqbuttongroup.h> 00024 #include <tqpushbutton.h> 00025 #include <tqtimer.h> 00026 00027 #include <kdebug.h> 00028 00029 #include "lineedit.h" 00030 #include "pickfileradio.moc" 00031 00032 00033 PickFileRadio::PickFileRadio(TQPushButton* button, LineEdit* edit, const TQString& text, TQButtonGroup* parent, const char* name) 00034 : RadioButton(text, parent, name), 00035 mGroup(parent), 00036 mEdit(edit), 00037 mButton(button), 00038 mLastId(-1), // set to an invalid value 00039 mRevertId(false) 00040 { 00041 Q_ASSERT(parent); 00042 Q_ASSERT(button); 00043 mButton->setEnabled(false); 00044 connect(mButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotPickFile())); 00045 if (mEdit) 00046 mEdit->setEnabled(false); 00047 connect(mGroup, TQT_SIGNAL(buttonSet(int)), TQT_SLOT(slotSelectionChanged(int))); 00048 } 00049 00050 PickFileRadio::PickFileRadio(const TQString& text, TQButtonGroup* parent, const char* name) 00051 : RadioButton(text, parent, name), 00052 mGroup(parent), 00053 mEdit(0), 00054 mButton(0), 00055 mLastId(-1), // set to an invalid value 00056 mRevertId(false) 00057 { 00058 Q_ASSERT(parent); 00059 } 00060 00061 void PickFileRadio::init(TQPushButton* button, LineEdit* edit) 00062 { 00063 Q_ASSERT(button); 00064 mEdit = edit; 00065 mButton = button; 00066 mButton->setEnabled(false); 00067 connect(mButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotPickFile())); 00068 if (mEdit) 00069 mEdit->setEnabled(false); 00070 connect(mGroup, TQT_SIGNAL(buttonSet(int)), TQT_SLOT(slotSelectionChanged(int))); 00071 setReadOnly(RadioButton::isReadOnly()); 00072 } 00073 00074 void PickFileRadio::setReadOnly(bool ro) 00075 { 00076 RadioButton::setReadOnly(ro); 00077 if (mButton) 00078 { 00079 if (mEdit) 00080 mEdit->setReadOnly(ro); 00081 if (ro) 00082 mButton->hide(); 00083 else 00084 mButton->show(); 00085 } 00086 } 00087 00088 void PickFileRadio::setFile(const TQString& file) 00089 { 00090 mFile = file; 00091 } 00092 00093 TQString PickFileRadio::file() const 00094 { 00095 return mEdit ? mEdit->text() : mFile; 00096 } 00097 00098 /****************************************************************************** 00099 * Set the radio button enabled or disabled. 00100 * Adjusts the enabled/disabled state of other controls appropriately. 00101 */ 00102 void PickFileRadio::setEnabled(bool enable) 00103 { 00104 Q_ASSERT(mButton); 00105 RadioButton::setEnabled(enable); 00106 enable = enable && mGroup->selected() == this; 00107 if (enable) 00108 { 00109 if (!pickFileIfNone()) 00110 enable = false; // revert to previously selected type 00111 } 00112 mButton->setEnabled(enable); 00113 if (mEdit) 00114 mEdit->setEnabled(enable); 00115 } 00116 00117 /****************************************************************************** 00118 * Called when the selected radio button changes. 00119 */ 00120 void PickFileRadio::slotSelectionChanged(int id) 00121 { 00122 if (id == mLastId || mRevertId) 00123 return; 00124 int radioId = mGroup->id(this); 00125 if (mLastId == radioId) 00126 { 00127 mButton->setEnabled(false); 00128 if (mEdit) 00129 mEdit->setEnabled(false); 00130 } 00131 else if (id == radioId) 00132 { 00133 if (!pickFileIfNone()) 00134 return; // revert to previously selected type 00135 mButton->setEnabled(true); 00136 if (mEdit) 00137 mEdit->setEnabled(true); 00138 } 00139 mLastId = id; 00140 } 00141 00142 /****************************************************************************** 00143 * Prompt for a file name if there is none currently entered. 00144 */ 00145 bool PickFileRadio::pickFileIfNone() 00146 { 00147 if (mEdit) 00148 mFile = mEdit->text(); 00149 if (!mFile.isEmpty()) 00150 return true; 00151 slotPickFile(); 00152 return !mFile.isEmpty(); 00153 } 00154 00155 /****************************************************************************** 00156 * Called when the file picker button is clicked. 00157 */ 00158 void PickFileRadio::slotPickFile() 00159 { 00160 mFile = pickFile(); 00161 if (mEdit) 00162 mEdit->setText(mFile); 00163 if (mFile.isEmpty()) 00164 { 00165 // No file is selected, so revert to the previous radio button selection. 00166 // But wait a moment before setting the radio button, or it won't work. 00167 mRevertId = true; // prevent picker dialogue popping up twice 00168 TQTimer::singleShot(0, this, TQT_SLOT(setLastId())); 00169 } 00170 } 00171 00172 /****************************************************************************** 00173 * Select the previously selected radio button in the group. 00174 */ 00175 void PickFileRadio::setLastId() 00176 { 00177 if (mLastId == -1) 00178 setOn(false); // we don't know the previous selection, so just turn this button off 00179 else 00180 mGroup->setButton(mLastId); 00181 mRevertId = false; 00182 }