22 #include "sounddlg.moc"
34 #include <kstandarddirs.h>
35 #include <kiconloader.h>
37 #include <kio/netaccess.h>
38 #include <phonon/mediaobject.h>
39 #include <phonon/audiooutput.h>
45 #include <QApplication>
46 #include <QVBoxLayout>
47 #include <QGridLayout>
49 #include <QResizeEvent>
59 SoundDlg::SoundDlg(
const QString& file,
float volume,
float fadeVolume,
int fadeSeconds,
int repeatPause,
60 const QString& caption,
QWidget* parent)
65 setMainWidget(mSoundWidget);
67 setButtons(Ok|Cancel);
76 mSoundWidget->
set(file, volume, fadeVolume, fadeSeconds, repeatPause);
84 if (readOnly != mReadOnly)
91 setDefaultButton(Cancel);
95 setButtons(Ok|Cancel);
104 mSoundWidget->
file(url);
116 KDialog::resizeEvent(re);
128 else if (mSoundWidget->
validate(
true))
132 KDialog::slotButtonClicked(button);
140 QString SoundWidget::mDefaultDir;
149 mEmptyFileAllowed(false)
151 QVBoxLayout* layout =
new QVBoxLayout(
this);
152 layout->setMargin(0);
153 layout->setSpacing(KDialog::spacingHint());
158 label =
new QLabel(i18nc(
"@label",
"Sound file:"),
this);
159 layout->addWidget(label);
164 layout->addWidget(box);
170 mFilePlay->setIcon(SmallIcon(QLatin1String(
"media-playback-start")));
171 connect(mFilePlay, SIGNAL(clicked()), SLOT(playSound()));
172 mFilePlay->setToolTip(i18nc(
"@info:tooltip",
"Test the sound"));
173 mFilePlay->setWhatsThis(i18nc(
"@info:whatsthis",
"Play the selected sound file."));
178 mFileEdit->setAcceptDrops(
true);
179 mFileEdit->setWhatsThis(i18nc(
"@info:whatsthis",
"Enter the name or URL of a sound file to play."));
181 label->setBuddy(mFileEdit);
182 connect(mFileEdit, SIGNAL(textChanged(QString)), SIGNAL(
changed()));
186 mFileBrowseButton->setIcon(KIcon(SmallIcon(QLatin1String(
"document-open"))));
187 int size = mFileBrowseButton->sizeHint().height();
188 mFileBrowseButton->setFixedSize(size, size);
189 connect(mFileBrowseButton, SIGNAL(clicked()), SLOT(slotPickFile()));
190 mFileBrowseButton->setToolTip(i18nc(
"@info:tooltip",
"Choose a file"));
191 mFileBrowseButton->setWhatsThis(i18nc(
"@info:whatsthis",
"Select a sound file to play."));
195 int size = qMax(mFilePlay->sizeHint().height(), mFileBrowseButton->sizeHint().height());
196 mFilePlay->setFixedSize(size, size);
197 mFileBrowseButton->setFixedSize(size, size);
204 mRepeatGroupBox->setCheckable(
true);
205 mRepeatGroupBox->setWhatsThis(i18nc(
"@info:whatsthis",
"If checked, the sound file will be played repeatedly for as long as the message is displayed."));
206 connect(mRepeatGroupBox, SIGNAL(toggled(
bool)), SIGNAL(
changed()));
207 layout->addWidget(mRepeatGroupBox);
208 QVBoxLayout* glayout =
new QVBoxLayout(mRepeatGroupBox);
213 box->setSpacing(KDialog::spacingHint());
214 glayout->addWidget(box);
215 label =
new QLabel(i18nc(
"@label:spinbox Length of time to pause between repetitions",
"Pause between repetitions:"), box);
216 label->setFixedSize(label->sizeHint());
217 mRepeatPause =
new SpinBox(0, 999, box);
218 mRepeatPause->setSingleShiftStep(10);
219 mRepeatPause->setFixedSize(mRepeatPause->sizeHint());
220 label->setBuddy(mRepeatPause);
221 connect(mRepeatPause, SIGNAL(valueChanged(
int)), SIGNAL(
changed()));
222 label =
new QLabel(i18nc(
"@label",
"seconds"), box);
223 label->setFixedSize(label->sizeHint());
224 box->setWhatsThis(i18nc(
"@info:whatsthis",
"Enter how many seconds to pause between repetitions."));
229 layout->addWidget(group);
230 QGridLayout* grid =
new QGridLayout(group);
231 grid->setMargin(KDialog::marginHint());
232 grid->setSpacing(KDialog::spacingHint());
233 grid->setColumnStretch(2, 1);
234 int indentWidth = 3 * KDialog::spacingHint();
235 grid->setColumnMinimumWidth(0, indentWidth);
236 grid->setColumnMinimumWidth(1, indentWidth);
239 box =
new KHBox(group);
241 box->setSpacing(KDialog::spacingHint());
242 grid->addWidget(box, 1, 0, 1, 3);
243 mVolumeCheckbox =
new CheckBox(i18nc(
"@option:check",
"Set volume"), box);
244 mVolumeCheckbox->setFixedSize(mVolumeCheckbox->sizeHint());
245 connect(mVolumeCheckbox, SIGNAL(toggled(
bool)), SLOT(slotVolumeToggled(
bool)));
246 mVolumeCheckbox->setWhatsThis(i18nc(
"@info:whatsthis",
"Select to choose the volume for playing the sound file."));
249 mVolumeSlider =
new Slider(0, 100, 10, Qt::Horizontal, box);
250 mVolumeSlider->setTickPosition(QSlider::TicksBelow);
251 mVolumeSlider->setTickInterval(10);
252 mVolumeSlider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
253 mVolumeSlider->setWhatsThis(i18nc(
"@info:whatsthis",
"Choose the volume for playing the sound file."));
255 connect(mVolumeSlider, SIGNAL(valueChanged(
int)), SIGNAL(
changed()));
258 mFadeCheckbox =
new CheckBox(i18nc(
"@option:check",
"Fade"), group);
259 mFadeCheckbox->setFixedSize(mFadeCheckbox->sizeHint());
260 connect(mFadeCheckbox, SIGNAL(toggled(
bool)), SLOT(slotFadeToggled(
bool)));
261 mFadeCheckbox->setWhatsThis(i18nc(
"@info:whatsthis",
"Select to fade the volume when the sound file first starts to play."));
262 grid->addWidget(mFadeCheckbox, 2, 1, 1, 2, Qt::AlignLeft);
265 mFadeBox =
new KHBox(group);
266 mFadeBox->setMargin(0);
267 mFadeBox->setSpacing(KDialog::spacingHint());
268 grid->addWidget(mFadeBox, 3, 2, Qt::AlignLeft);
269 label =
new QLabel(i18nc(
"@label:spinbox Time period over which to fade the sound",
"Fade time:"), mFadeBox);
270 label->setFixedSize(label->sizeHint());
271 mFadeTime =
new SpinBox(1, 999, mFadeBox);
272 mFadeTime->setSingleShiftStep(10);
273 mFadeTime->setFixedSize(mFadeTime->sizeHint());
274 label->setBuddy(mFadeTime);
275 connect(mFadeTime, SIGNAL(valueChanged(
int)), SIGNAL(
changed()));
276 label =
new QLabel(i18nc(
"@label",
"seconds"), mFadeBox);
277 label->setFixedSize(label->sizeHint());
278 mFadeBox->setWhatsThis(i18nc(
"@info:whatsthis",
"Enter how many seconds to fade the sound before reaching the set volume."));
281 mFadeVolumeBox =
new KHBox(group);
282 mFadeVolumeBox->setMargin(0);
283 mFadeVolumeBox->setSpacing(KDialog::spacingHint());
284 grid->addWidget(mFadeVolumeBox, 4, 2);
285 label =
new QLabel(i18nc(
"@label:slider",
"Initial volume:"), mFadeVolumeBox);
286 label->setFixedSize(label->sizeHint());
287 mFadeSlider =
new Slider(0, 100, 10, Qt::Horizontal, mFadeVolumeBox);
288 mFadeSlider->setTickPosition(QSlider::TicksBelow);
289 mFadeSlider->setTickInterval(10);
290 mFadeSlider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
291 label->setBuddy(mFadeSlider);
292 connect(mFadeSlider, SIGNAL(valueChanged(
int)), SIGNAL(
changed()));
293 mFadeVolumeBox->setWhatsThis(i18nc(
"@info:whatsthis",
"Choose the initial volume for playing the sound file."));
295 slotVolumeToggled(
false);
307 void SoundWidget::set(
const QString& file,
float volume,
float fadeVolume,
int fadeSeconds,
int repeatPause)
310 mFileEdit->
setText(KAlarm::pathOrUrl(file));
313 mRepeatGroupBox->setChecked(repeatPause >= 0);
314 mRepeatPause->setValue(repeatPause >= 0 ? repeatPause : 0);
316 mVolumeCheckbox->setChecked(volume >= 0);
317 mVolumeSlider->setValue(volume >= 0 ? static_cast<int>(volume*100) : 100);
318 mFadeCheckbox->setChecked(fadeVolume >= 0);
319 mFadeSlider->setValue(fadeVolume >= 0 ? static_cast<int>(fadeVolume*100) : 100);
320 mFadeTime->setValue(fadeSeconds);
321 slotVolumeToggled(volume >= 0);
329 if (readOnly != mReadOnly)
331 mFileEdit->setReadOnly(readOnly);
338 mFadeTime->setReadOnly(readOnly);
340 mReadOnly = readOnly;
349 return mFileEdit->
text();
357 bool result =
validate(showErrorMessage);
369 volume = mVolumeCheckbox->isChecked() ? (float)mVolumeSlider->value() / 100 : -1;
370 if (mFadeCheckbox->isChecked())
372 fadeVolume = (float)mFadeSlider->value() / 100;
373 fadeSeconds = mFadeTime->value();
388 return mRepeatGroupBox && mRepeatGroupBox->isChecked() ? mRepeatPause->value() : -1;
397 mVolumeSlider->resize(mFadeSlider->size());
398 QWidget::resizeEvent(re);
403 mVolumeSlider->resize(mFadeSlider->size());
404 QWidget::showEvent(se);
410 void SoundWidget::slotPickFile()
414 mFileEdit->
setText(KAlarm::pathOrUrl(url));
420 void SoundWidget::playSound()
431 #warning Phonon::createPlayer() does not work
432 mPlayer = Phonon::createPlayer(Phonon::MusicCategory, mUrl);
433 mPlayer->setParent(
this);
435 mPlayer =
new Phonon::MediaObject(
this);
436 Phonon::AudioOutput* output =
new Phonon::AudioOutput(Phonon::MusicCategory, mPlayer);
437 mPlayer->setCurrentSource(mUrl);
438 Phonon::createPath(mPlayer, output);
440 connect(mPlayer, SIGNAL(finished()), SLOT(playFinished()));
441 mFilePlay->setIcon(SmallIcon(QLatin1String(
"media-playback-stop")));
442 mFilePlay->setToolTip(i18nc(
"@info:tooltip",
"Stop sound"));
443 mFilePlay->setWhatsThis(i18nc(
"@info:whatsthis",
"Stop playing the sound"));
450 void SoundWidget::playFinished()
454 mFilePlay->setIcon(SmallIcon(QLatin1String(
"media-playback-start")));
455 mFilePlay->setToolTip(i18nc(
"@info:tooltip",
"Test the sound"));
456 mFilePlay->setWhatsThis(i18nc(
"@info:whatsthis",
"Play the selected sound file."));
465 if (file == mValidatedFile && !file.isEmpty())
467 mValidatedFile =
file;
468 if (file.isEmpty() && mEmptyFileAllowed)
473 KAlarm::FileErr err = KAlarm::checkFileExists(file, mUrl);
474 if (err == KAlarm::FileErr_None)
476 if (err == KAlarm::FileErr_Nonexistent)
479 if (mUrl.isLocalFile() && !file.startsWith(QLatin1String(
"/")))
483 QStringList soundDirs = KGlobal::dirs()->resourceDirs(
"sound");
484 if (!soundDirs.isEmpty())
487 dir.setFilter(QDir::Files | QDir::Readable);
488 for (
int i = 0, end = soundDirs.count(); i < end; ++i)
491 if (dir.isReadable() && dir.count() > 2)
493 mUrl.setPath(soundDirs[i]);
495 QString f = mUrl.toLocalFile();
496 err = KAlarm::checkFileExists(f, mUrl);
497 if (err == KAlarm::FileErr_None)
499 if (err != KAlarm::FileErr_Nonexistent)
507 if (err == KAlarm::FileErr_Nonexistent)
509 mUrl.setPath(QDir::homePath());
511 QString f = mUrl.toLocalFile();
512 err = KAlarm::checkFileExists(f, mUrl);
513 if (err == KAlarm::FileErr_None)
515 if (err != KAlarm::FileErr_Nonexistent)
520 mFileEdit->setFocus();
522 && KAlarm::showFileErrMessage(file, err, KAlarm::FileErr_BlankPlay, const_cast<SoundWidget*>(
this)))
524 mValidatedFile.clear();
532 void SoundWidget::slotVolumeToggled(
bool on)
534 mVolumeSlider->setEnabled(on);
535 mFadeCheckbox->setEnabled(on);
536 slotFadeToggled(on && mFadeCheckbox->isChecked());
542 void SoundWidget::slotFadeToggled(
bool on)
544 mFadeBox->setEnabled(on);
545 mFadeVolumeBox->setEnabled(on);
virtual void setReadOnly(bool readOnly)
static const char SOUND_DIALOG_NAME[]
virtual void setText(const QString &str)
virtual void resizeEvent(QResizeEvent *)
void setFocusWidget(QWidget *widget, bool enable=true)
static QString browseFile(QString &initialDir, const QString &initialFile=QString())
Display a dialog to choose a sound file, initially highlighting initialFile if non-null.
SoundDlg(const QString &file, float volume, float fadeVolume, int fadeSeconds, int repeatPause, const QString &caption, QWidget *parent)
virtual void setReadOnly(bool readOnly)
virtual void slotButtonClicked(int button)
virtual void setReadOnly(bool readOnly)