00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kalarm.h"
00022
00023 #include <QLabel>
00024 #include <QDir>
00025 #include <QGroupBox>
00026 #include <QApplication>
00027 #include <QVBoxLayout>
00028 #include <QGridLayout>
00029 #include <QShowEvent>
00030 #include <QResizeEvent>
00031
00032 #include <klocale.h>
00033 #include <kstandarddirs.h>
00034 #include <kiconloader.h>
00035 #include <khbox.h>
00036 #include <kio/netaccess.h>
00037 #include <Phonon/MediaObject>
00038
00039 #include "checkbox.h"
00040 #include "functions.h"
00041 #include "lineedit.h"
00042 #include "pushbutton.h"
00043 #include "slider.h"
00044 #include "soundpicker.h"
00045 #include "spinbox.h"
00046 #include "sounddlg.moc"
00047
00048
00049
00050
00051 QString SoundDlg::i18n_chk_Repeat() { return i18nc("@option:check", "Repeat"); }
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)
00058 : KDialog(parent),
00059 mPlayer(0),
00060 mReadOnly(false)
00061 {
00062 QWidget* page = new QWidget(this);
00063 setMainWidget(page);
00064 setCaption(caption);
00065 setButtons(Ok|Cancel);
00066 setDefaultButton(Ok);
00067 connect(this, SIGNAL(okClicked()), SLOT(slotOk()));
00068
00069 QVBoxLayout* layout = new QVBoxLayout(page);
00070 layout->setMargin(0);
00071 layout->setSpacing(spacingHint());
00072
00073
00074 KHBox* box = new KHBox(page);
00075 box->setMargin(0);
00076 layout->addWidget(box);
00077 mFilePlay = new QPushButton(box);
00078 mFilePlay->setIcon(SmallIcon("media-playback-start"));
00079 connect(mFilePlay, SIGNAL(clicked()), SLOT(playSound()));
00080 mFilePlay->setToolTip(i18nc("@info:tooltip", "Test the sound"));
00081 mFilePlay->setWhatsThis(i18nc("@info:whatsthis", "Play the selected sound file."));
00082
00083
00084 mFileEdit = new LineEdit(LineEdit::Url, box);
00085 mFileEdit->setAcceptDrops(true);
00086 mFileEdit->setWhatsThis(i18nc("@info:whatsthis", "Enter the name or URL of a sound file to play."));
00087
00088
00089 mFileBrowseButton = new PushButton(box);
00090 mFileBrowseButton->setIcon(SmallIcon("document-open"));
00091 connect(mFileBrowseButton, SIGNAL(clicked()), SLOT(slotPickFile()));
00092 mFileBrowseButton->setToolTip(i18nc("@info:tooltip", "Choose a file"));
00093 mFileBrowseButton->setWhatsThis(i18nc("@info:whatsthis", "Select a sound file to play."));
00094
00095 int size = qMax(mFilePlay->sizeHint().height(), mFileBrowseButton->sizeHint().height());
00096 mFilePlay->setFixedSize(size, size);
00097 mFileBrowseButton->setFixedSize(size, size);
00098
00099
00100
00101 mRepeatCheckbox = new CheckBox(i18n_chk_Repeat(), page);
00102 mRepeatCheckbox->setFixedSize(mRepeatCheckbox->sizeHint());
00103 mRepeatCheckbox->setWhatsThis(i18nc("@info:whatsthis", "If checked, the sound file will be played repeatedly for as long as the message is displayed."));
00104 layout->addWidget(mRepeatCheckbox);
00105
00106
00107 QGroupBox* group = new QGroupBox(i18nc("@title:group Sound volume", "Volume"), page);
00108 layout->addWidget(group);
00109 QGridLayout* grid = new QGridLayout(group);
00110 grid->setMargin(marginHint());
00111 grid->setSpacing(spacingHint());
00112 grid->setColumnStretch(2, 1);
00113 int indentWidth = 3 * KDialog::spacingHint();
00114 grid->setColumnMinimumWidth(0, indentWidth);
00115 grid->setColumnMinimumWidth(1, indentWidth);
00116
00117
00118 box = new KHBox(group);
00119 box->setMargin(0);
00120 box->setSpacing(spacingHint());
00121 grid->addWidget(box, 1, 0, 1, 3);
00122 mVolumeCheckbox = new CheckBox(i18nc("@option:check", "Set volume"), box);
00123 mVolumeCheckbox->setFixedSize(mVolumeCheckbox->sizeHint());
00124 connect(mVolumeCheckbox, SIGNAL(toggled(bool)), SLOT(slotVolumeToggled(bool)));
00125 mVolumeCheckbox->setWhatsThis(i18nc("@info:whatsthis", "Select to choose the volume for playing the sound file."));
00126
00127
00128 mVolumeSlider = new Slider(0, 100, 10, Qt::Horizontal, box);
00129 mVolumeSlider->setTickPosition(QSlider::TicksBelow);
00130 mVolumeSlider->setTickInterval(10);
00131 mVolumeSlider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00132 mVolumeSlider->setWhatsThis(i18nc("@info:whatsthis", "Choose the volume for playing the sound file."));
00133 mVolumeCheckbox->setFocusWidget(mVolumeSlider);
00134
00135
00136 mFadeCheckbox = new CheckBox(i18nc("@option:check", "Fade"), group);
00137 mFadeCheckbox->setFixedSize(mFadeCheckbox->sizeHint());
00138 connect(mFadeCheckbox, SIGNAL(toggled(bool)), SLOT(slotFadeToggled(bool)));
00139 mFadeCheckbox->setWhatsThis(i18nc("@info:whatsthis", "Select to fade the volume when the sound file first starts to play."));
00140 grid->addWidget(mFadeCheckbox, 2, 1, 1, 2, Qt::AlignLeft);
00141
00142
00143 mFadeBox = new KHBox(group);
00144 mFadeBox->setMargin(0);
00145 mFadeBox->setSpacing(spacingHint());
00146 grid->addWidget(mFadeBox, 3, 2, Qt::AlignLeft);
00147 QLabel* label = new QLabel(i18nc("@label:spinbox Time period over which to fade the sound", "Fade time:"), mFadeBox);
00148 label->setFixedSize(label->sizeHint());
00149 mFadeTime = new SpinBox(1, 999, mFadeBox);
00150 mFadeTime->setSingleShiftStep(10);
00151 mFadeTime->setFixedSize(mFadeTime->sizeHint());
00152 label->setBuddy(mFadeTime);
00153 label = new QLabel(i18nc("@label", "seconds"), mFadeBox);
00154 label->setFixedSize(label->sizeHint());
00155 mFadeBox->setWhatsThis(i18nc("@info:whatsthis", "Enter how many seconds to fade the sound before reaching the set volume."));
00156
00157
00158 mFadeVolumeBox = new KHBox(group);
00159 mFadeVolumeBox->setMargin(0);
00160 mFadeVolumeBox->setSpacing(spacingHint());
00161 grid->addWidget(mFadeVolumeBox, 4, 2);
00162 label = new QLabel(i18nc("@label:slider", "Initial volume:"), mFadeVolumeBox);
00163 label->setFixedSize(label->sizeHint());
00164 mFadeSlider = new Slider(0, 100, 10, Qt::Horizontal, mFadeVolumeBox);
00165 mFadeSlider->setTickPosition(QSlider::TicksBelow);
00166 mFadeSlider->setTickInterval(10);
00167 mFadeSlider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00168 label->setBuddy(mFadeSlider);
00169 mFadeVolumeBox->setWhatsThis(i18nc("@info:whatsthis", "Choose the initial volume for playing the sound file."));
00170
00171
00172 QSize s;
00173 if (KAlarm::readConfigWindowSize(SOUND_DIALOG_NAME, s))
00174 resize(s);
00175
00176
00177 mFileEdit->setText(file);
00178 mRepeatCheckbox->setChecked(repeat);
00179 mVolumeCheckbox->setChecked(volume >= 0);
00180 mVolumeSlider->setValue(volume >= 0 ? static_cast<int>(volume*100) : 100);
00181 mFadeCheckbox->setChecked(fadeVolume >= 0);
00182 mFadeSlider->setValue(fadeVolume >= 0 ? static_cast<int>(fadeVolume*100) : 100);
00183 mFadeTime->setValue(fadeSeconds);
00184 slotVolumeToggled(volume >= 0);
00185 }
00186
00187 SoundDlg::~SoundDlg()
00188 {
00189 delete mPlayer;
00190 mPlayer = 0;
00191 }
00192
00193
00194
00195
00196 void SoundDlg::setReadOnly(bool readOnly)
00197 {
00198 if (readOnly != mReadOnly)
00199 {
00200 mFileEdit->setReadOnly(readOnly);
00201 mFileBrowseButton->setReadOnly(readOnly);
00202 mRepeatCheckbox->setReadOnly(readOnly);
00203 mVolumeCheckbox->setReadOnly(readOnly);
00204 mVolumeSlider->setReadOnly(readOnly);
00205 mFadeCheckbox->setReadOnly(readOnly);
00206 mFadeTime->setReadOnly(readOnly);
00207 mFadeSlider->setReadOnly(readOnly);
00208 mReadOnly = readOnly;
00209 }
00210 }
00211
00212
00213
00214
00215
00216
00217
00218 bool SoundDlg::getSettings(float& volume, float& fadeVolume, int& fadeSeconds) const
00219 {
00220 volume = mVolumeCheckbox->isChecked() ? (float)mVolumeSlider->value() / 100 : -1;
00221 if (mFadeCheckbox->isChecked())
00222 {
00223 fadeVolume = (float)mFadeSlider->value() / 100;
00224 fadeSeconds = mFadeTime->value();
00225 }
00226 else
00227 {
00228 fadeVolume = -1;
00229 fadeSeconds = 0;
00230 }
00231 return mRepeatCheckbox->isChecked();
00232 }
00233
00234
00235
00236
00237
00238 void SoundDlg::resizeEvent(QResizeEvent* re)
00239 {
00240 if (isVisible())
00241 KAlarm::writeConfigWindowSize(SOUND_DIALOG_NAME, re->size());
00242 mVolumeSlider->resize(mFadeSlider->size());
00243 KDialog::resizeEvent(re);
00244 }
00245
00246 void SoundDlg::showEvent(QShowEvent* se)
00247 {
00248 mVolumeSlider->resize(mFadeSlider->size());
00249 KDialog::showEvent(se);
00250 }
00251
00252
00253
00254
00255 void SoundDlg::slotOk()
00256 {
00257 if (mReadOnly)
00258 reject();
00259 if (!checkFile())
00260 return;
00261 accept();
00262 }
00263
00264
00265
00266
00267 void SoundDlg::slotPickFile()
00268 {
00269 QString url = SoundPicker::browseFile(mDefaultDir, mFileEdit->text());
00270 if (!url.isEmpty())
00271 mFileEdit->setText(url);
00272 }
00273
00274
00275
00276
00277 void SoundDlg::playSound()
00278 {
00279 if (mPlayer)
00280 {
00281
00282 playFinished();
00283 return;
00284 }
00285 if (!checkFile())
00286 return;
00287 mPlayer = Phonon::createPlayer(Phonon::MusicCategory, mUrl);
00288 connect(mPlayer, SIGNAL(finished()), SLOT(playFinished()));
00289 mFilePlay->setIcon(SmallIcon("media-playback-stop"));
00290 mFilePlay->setToolTip(i18nc("@info:tooltip", "Stop sound"));
00291 mFilePlay->setWhatsThis(i18nc("@info:whatsthis", "Stop playing the sound"));
00292 mPlayer->play();
00293 }
00294
00295
00296
00297
00298 void SoundDlg::playFinished()
00299 {
00300 delete mPlayer;
00301 mPlayer = 0;
00302 mFilePlay->setIcon(SmallIcon("media-playback-start"));
00303 mFilePlay->setToolTip(i18nc("@info:tooltip", "Test the sound"));
00304 mFilePlay->setWhatsThis(i18nc("@info:whatsthis", "Play the selected sound file."));
00305 }
00306
00307
00308
00309
00310 bool SoundDlg::checkFile()
00311 {
00312 QString file = mFileEdit->text();
00313 mUrl = KUrl(file);
00314 if (KIO::NetAccess::exists(mUrl, KIO::NetAccess::SourceSide, this))
00315 return true;
00316 if (mUrl.isLocalFile() && !file.startsWith(QLatin1String("/")))
00317 {
00318
00319
00320 QStringList soundDirs = KGlobal::dirs()->resourceDirs("sound");
00321 if (!soundDirs.isEmpty())
00322 {
00323 QDir dir;
00324 dir.setFilter(QDir::Files | QDir::Readable);
00325 for (int i = 0, end = soundDirs.count(); i < end; ++i)
00326 {
00327 dir = soundDirs[i];
00328 if (dir.isReadable() && dir.count() > 2)
00329 {
00330 mUrl.setPath(soundDirs[i]);
00331 mUrl.addPath(file);
00332 if (KIO::NetAccess::exists(mUrl, KIO::NetAccess::SourceSide, this))
00333 return true;
00334 }
00335 }
00336 }
00337 mUrl.setPath(QDir::homePath());
00338 mUrl.addPath(file);
00339 if (KIO::NetAccess::exists(mUrl, KIO::NetAccess::SourceSide, this))
00340 return true;
00341 }
00342 KMessageBox::sorry(this, i18nc("@info", "File not found"));
00343 mUrl.clear();
00344 return false;
00345 }
00346
00347
00348
00349
00350 void SoundDlg::slotVolumeToggled(bool on)
00351 {
00352 mVolumeSlider->setEnabled(on);
00353 mFadeCheckbox->setEnabled(on);
00354 slotFadeToggled(on && mFadeCheckbox->isChecked());
00355 }
00356
00357
00358
00359
00360 void SoundDlg::slotFadeToggled(bool on)
00361 {
00362 mFadeBox->setEnabled(on);
00363 mFadeVolumeBox->setEnabled(on);
00364 }