kalarm

sounddlg.cpp

00001 /*
00002  *  sounddlg.cpp  -  sound file selection and configuration dialog
00003  *  Program:  kalarm
00004  *  Copyright © 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 #ifndef WITHOUT_ARTS
00022 
00023 #include "kalarm.h"
00024 
00025 #include <qlabel.h>
00026 #include <qhbox.h>
00027 #include <qgroupbox.h>
00028 #include <qlayout.h>
00029 #include <qwhatsthis.h>
00030 #include <qtooltip.h>
00031 
00032 #include <klocale.h>
00033 #include <kstandarddirs.h>
00034 #include <kiconloader.h>
00035 
00036 #include "checkbox.h"
00037 #include "functions.h"
00038 #include "lineedit.h"
00039 #include "pushbutton.h"
00040 #include "slider.h"
00041 #include "soundpicker.h"
00042 #include "spinbox.h"
00043 #include "sounddlg.moc"
00044 
00045 
00046 // Collect these widget labels together to ensure consistent wording and
00047 // translations across different modules.
00048 QString SoundDlg::i18n_SetVolume()   { return i18n("Set volume"); }
00049 QString SoundDlg::i18n_v_SetVolume() { return i18n("Set &volume"); }
00050 QString SoundDlg::i18n_Repeat()      { return i18n("Repeat"); }
00051 QString SoundDlg::i18n_p_Repeat()    { return i18n("Re&peat"); }
00052 
00053 static const char SOUND_DIALOG_NAME[] = "SoundDialog";
00054 
00055 
00056 SoundDlg::SoundDlg(const QString& file, float volume, float fadeVolume, int fadeSeconds, bool repeat,
00057                    const QString& caption, QWidget* parent, const char* name)
00058     : KDialogBase(parent, name, true, caption, Ok|Cancel, Ok, false),
00059       mReadOnly(false)
00060 {
00061     QWidget* page = new QWidget(this);
00062     setMainWidget(page);
00063     QVBoxLayout* layout = new QVBoxLayout(page, 0, spacingHint());
00064 
00065     // File name edit box
00066     QHBox* box = new QHBox(page);
00067     layout->addWidget(box);
00068     mFileEdit = new LineEdit(LineEdit::Url, box);
00069     mFileEdit->setAcceptDrops(true);
00070     QWhatsThis::add(mFileEdit, i18n("Enter the name or URL of a sound file to play."));
00071 
00072     // File browse button
00073     mFileBrowseButton = new PushButton(box);
00074     mFileBrowseButton->setPixmap(SmallIcon("fileopen"));
00075     mFileBrowseButton->setFixedSize(mFileBrowseButton->sizeHint());
00076     connect(mFileBrowseButton, SIGNAL(clicked()), SLOT(slotPickFile()));
00077     QToolTip::add(mFileBrowseButton, i18n("Choose a file"));
00078     QWhatsThis::add(mFileBrowseButton, i18n("Select a sound file to play."));
00079 
00080     // Sound repetition checkbox
00081     mRepeatCheckbox = new CheckBox(i18n_p_Repeat(), page);
00082     mRepeatCheckbox->setFixedSize(mRepeatCheckbox->sizeHint());
00083     QWhatsThis::add(mRepeatCheckbox,
00084           i18n("If checked, the sound file will be played repeatedly for as long as the message is displayed."));
00085     layout->addWidget(mRepeatCheckbox);
00086 
00087     // Volume
00088     QGroupBox* group = new QGroupBox(i18n("Volume"), page);
00089     layout->addWidget(group);
00090     QGridLayout* grid = new QGridLayout(group, 4, 3, marginHint(), spacingHint());
00091     grid->addRowSpacing(0, fontMetrics().lineSpacing()/2);
00092     grid->setColStretch(2, 1);
00093     int indentWidth = 3 * KDialog::spacingHint();
00094     grid->addColSpacing(0, indentWidth);
00095     grid->addColSpacing(1, indentWidth);
00096     // Get alignment to use in QGridLayout (AlignAuto doesn't work correctly there)
00097     int alignment = QApplication::reverseLayout() ? Qt::AlignRight : Qt::AlignLeft;
00098 
00099     // 'Set volume' checkbox
00100     box = new QHBox(group);
00101     box->setSpacing(spacingHint());
00102     grid->addMultiCellWidget(box, 1, 1, 0, 2);
00103     mVolumeCheckbox = new CheckBox(i18n_v_SetVolume(), box);
00104     mVolumeCheckbox->setFixedSize(mVolumeCheckbox->sizeHint());
00105     connect(mVolumeCheckbox, SIGNAL(toggled(bool)), SLOT(slotVolumeToggled(bool)));
00106     QWhatsThis::add(mVolumeCheckbox,
00107           i18n("Select to choose the volume for playing the sound file."));
00108 
00109     // Volume slider
00110     mVolumeSlider = new Slider(0, 100, 10, 0, Qt::Horizontal, box);
00111     mVolumeSlider->setTickmarks(QSlider::Below);
00112     mVolumeSlider->setTickInterval(10);
00113     mVolumeSlider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00114     QWhatsThis::add(mVolumeSlider, i18n("Choose the volume for playing the sound file."));
00115     mVolumeCheckbox->setFocusWidget(mVolumeSlider);
00116 
00117     // Fade checkbox
00118     mFadeCheckbox = new CheckBox(i18n("Fade"), group);
00119     mFadeCheckbox->setFixedSize(mFadeCheckbox->sizeHint());
00120     connect(mFadeCheckbox, SIGNAL(toggled(bool)), SLOT(slotFadeToggled(bool)));
00121     QWhatsThis::add(mFadeCheckbox,
00122           i18n("Select to fade the volume when the sound file first starts to play."));
00123     grid->addMultiCellWidget(mFadeCheckbox, 2, 2, 1, 2, alignment);
00124 
00125     // Fade time
00126     mFadeBox = new QHBox(group);
00127     mFadeBox->setSpacing(spacingHint());
00128     grid->addWidget(mFadeBox, 3, 2, alignment);
00129     QLabel* label = new QLabel(i18n("Time period over which to fade the sound", "Fade time:"), mFadeBox);
00130     label->setFixedSize(label->sizeHint());
00131     mFadeTime = new SpinBox(1, 999, 1, mFadeBox);
00132     mFadeTime->setLineShiftStep(10);
00133     mFadeTime->setFixedSize(mFadeTime->sizeHint());
00134     label->setBuddy(mFadeTime);
00135     label = new QLabel(i18n("seconds"), mFadeBox);
00136     label->setFixedSize(label->sizeHint());
00137     QWhatsThis::add(mFadeBox, i18n("Enter how many seconds to fade the sound before reaching the set volume."));
00138 
00139     // Fade slider
00140     mFadeVolumeBox = new QHBox(group);
00141     mFadeVolumeBox->setSpacing(spacingHint());
00142     grid->addWidget(mFadeVolumeBox, 4, 2);
00143     label = new QLabel(i18n("Initial volume:"), mFadeVolumeBox);
00144     label->setFixedSize(label->sizeHint());
00145     mFadeSlider = new Slider(0, 100, 10, 0, Qt::Horizontal, mFadeVolumeBox);
00146     mFadeSlider->setTickmarks(QSlider::Below);
00147     mFadeSlider->setTickInterval(10);
00148     mFadeSlider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00149     label->setBuddy(mFadeSlider);
00150     QWhatsThis::add(mFadeVolumeBox, i18n("Choose the initial volume for playing the sound file."));
00151 
00152     // Restore the dialogue size from last time
00153     QSize s;
00154     if (KAlarm::readConfigWindowSize(SOUND_DIALOG_NAME, s))
00155         resize(s);
00156 
00157     // Initialise the control values
00158     mFileEdit->setText(file);
00159     mRepeatCheckbox->setChecked(repeat);
00160     mVolumeCheckbox->setChecked(volume >= 0);
00161     mVolumeSlider->setValue(volume >= 0 ? static_cast<int>(volume*100) : 100);
00162     mFadeCheckbox->setChecked(fadeVolume >= 0);
00163     mFadeSlider->setValue(fadeVolume >= 0 ? static_cast<int>(fadeVolume*100) : 100);
00164     mFadeTime->setValue(fadeSeconds);
00165     slotVolumeToggled(volume >= 0);
00166 }
00167 
00168 /******************************************************************************
00169  * Set the read-only status of the dialogue.
00170  */
00171 void SoundDlg::setReadOnly(bool readOnly)
00172 {
00173     if (readOnly != mReadOnly)
00174     {
00175         mFileEdit->setReadOnly(readOnly);
00176         mFileBrowseButton->setReadOnly(readOnly);
00177         mRepeatCheckbox->setReadOnly(readOnly);
00178         mVolumeCheckbox->setReadOnly(readOnly);
00179         mVolumeSlider->setReadOnly(readOnly);
00180         mFadeCheckbox->setReadOnly(readOnly);
00181         mFadeTime->setReadOnly(readOnly);
00182         mFadeSlider->setReadOnly(readOnly);
00183         mReadOnly = readOnly;
00184     }
00185 }
00186 
00187 /******************************************************************************
00188  * Return the entered repetition and volume settings:
00189  * 'volume' is in range 0 - 1, or < 0 if volume is not to be set.
00190  * 'fadeVolume is similar, with 'fadeTime' set to the fade interval in seconds.
00191  * Reply = whether to repeat or not.
00192  */
00193 QString SoundDlg::getFile() const
00194 {
00195     return mFileEdit->text();
00196 }
00197 
00198 /******************************************************************************
00199  * Return the entered repetition and volume settings:
00200  * 'volume' is in range 0 - 1, or < 0 if volume is not to be set.
00201  * 'fadeVolume is similar, with 'fadeTime' set to the fade interval in seconds.
00202  * Reply = whether to repeat or not.
00203  */
00204 bool SoundDlg::getSettings(float& volume, float& fadeVolume, int& fadeSeconds) const
00205 {
00206     volume = mVolumeCheckbox->isChecked() ? (float)mVolumeSlider->value() / 100 : -1;
00207     if (mFadeCheckbox->isChecked())
00208     {
00209         fadeVolume  = (float)mFadeSlider->value() / 100;
00210         fadeSeconds = mFadeTime->value();
00211     }
00212     else
00213     {
00214         fadeVolume  = -1;
00215         fadeSeconds = 0;
00216     }
00217     return mRepeatCheckbox->isChecked();
00218 }
00219 
00220 /******************************************************************************
00221 *  Called when the dialog's size has changed.
00222 *  Records the new size in the config file.
00223 */
00224 void SoundDlg::resizeEvent(QResizeEvent* re)
00225 {
00226     if (isVisible())
00227         KAlarm::writeConfigWindowSize(SOUND_DIALOG_NAME, re->size());
00228     mVolumeSlider->resize(mFadeSlider->size());
00229     KDialog::resizeEvent(re);
00230 }
00231 
00232 void SoundDlg::showEvent(QShowEvent* se)
00233 {
00234     mVolumeSlider->resize(mFadeSlider->size());
00235     KDialog::showEvent(se);
00236 }
00237 
00238 /******************************************************************************
00239 *  Called when the OK button is clicked.
00240 */
00241 void SoundDlg::slotOk()
00242 {
00243     if (mReadOnly)
00244         reject();
00245     accept();
00246 }
00247 
00248 /******************************************************************************
00249  * Called when the file browser button is clicked.
00250  */
00251 void SoundDlg::slotPickFile()
00252 {
00253     QString url = SoundPicker::browseFile(mDefaultDir, mFileEdit->text());
00254     if (!url.isEmpty())
00255         mFileEdit->setText(url);
00256 }
00257 
00258 /******************************************************************************
00259  * Called when the Set Volume checkbox is toggled.
00260  */
00261 void SoundDlg::slotVolumeToggled(bool on)
00262 {
00263     mVolumeSlider->setEnabled(on);
00264     mFadeCheckbox->setEnabled(on);
00265     slotFadeToggled(on  &&  mFadeCheckbox->isChecked());
00266 }
00267 
00268 /******************************************************************************
00269  * Called when the Fade checkbox is toggled.
00270  */
00271 void SoundDlg::slotFadeToggled(bool on)
00272 {
00273     mFadeBox->setEnabled(on);
00274     mFadeVolumeBox->setEnabled(on);
00275 }
00276 
00277 #endif // #ifndef WITHOUT_ARTS
KDE Home | KDE Accessibility Home | Description of Access Keys