27 #include <tqgroupbox.h>
31 #include <tqwhatsthis.h>
32 #include <tqtooltip.h>
34 #include <tdelocale.h>
35 #include <kstandarddirs.h>
36 #include <kiconloader.h>
38 #include <kaudioplayer.h>
41 #include <arts/kartsdispatcher.h>
42 #include <arts/kartsserver.h>
43 #include <arts/kplayobjectfactory.h>
44 #include <arts/kplayobject.h>
46 #include <tdemessagebox.h>
47 #include <tdeio/netaccess.h>
54 #include "pushbutton.h"
56 #include "soundpicker.h"
58 #include "sounddlg.moc"
63 TQString SoundDlg::i18n_SetVolume() {
return i18n(
"Set volume"); }
64 TQString SoundDlg::i18n_v_SetVolume() {
return i18n(
"Set &volume"); }
65 TQString SoundDlg::i18n_Repeat() {
return i18n(
"Repeat"); }
66 TQString SoundDlg::i18n_p_Repeat() {
return i18n(
"Re&peat"); }
68 static const char SOUND_DIALOG_NAME[] =
"SoundDialog";
71 SoundDlg::SoundDlg(
const TQString& file,
float volume,
float fadeVolume,
int fadeSeconds,
bool repeat,
72 const TQString& caption, TQWidget* parent,
const char* name)
73 : KDialogBase(parent, name, true, caption, Ok|Cancel, Ok, false),
79 TQWidget* page =
new TQWidget(
this);
81 TQVBoxLayout* layout =
new TQVBoxLayout(page, 0, spacingHint());
84 TQHBox* box =
new TQHBox(page);
85 layout->addWidget(box);
86 mFilePlay =
new TQPushButton(box);
87 mFilePlay->setPixmap(SmallIcon(
"media-playback-start"));
88 mFilePlay->setFixedSize(mFilePlay->sizeHint());
89 connect(mFilePlay, TQ_SIGNAL(clicked()), TQ_SLOT(playSound()));
90 TQToolTip::add(mFilePlay, i18n(
"Test the sound"));
91 TQWhatsThis::add(mFilePlay, i18n(
"Play the selected sound file."));
94 mFileEdit =
new LineEdit(LineEdit::Url, box);
95 mFileEdit->setAcceptDrops(
true);
96 TQWhatsThis::add(mFileEdit, i18n(
"Enter the name or URL of a sound file to play."));
99 mFileBrowseButton =
new PushButton(box);
100 mFileBrowseButton->setPixmap(SmallIcon(
"document-open"));
101 mFileBrowseButton->setFixedSize(mFileBrowseButton->sizeHint());
102 connect(mFileBrowseButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotPickFile()));
103 TQToolTip::add(mFileBrowseButton, i18n(
"Choose a file"));
104 TQWhatsThis::add(mFileBrowseButton, i18n(
"Select a sound file to play."));
107 mRepeatCheckbox =
new CheckBox(i18n_p_Repeat(), page);
108 mRepeatCheckbox->setFixedSize(mRepeatCheckbox->sizeHint());
109 TQWhatsThis::add(mRepeatCheckbox,
110 i18n(
"If checked, the sound file will be played repeatedly for as long as the message is displayed."));
111 layout->addWidget(mRepeatCheckbox);
114 TQGroupBox* group =
new TQGroupBox(i18n(
"Volume"), page);
115 layout->addWidget(group);
116 TQGridLayout* grid =
new TQGridLayout(group, 4, 3, marginHint(), spacingHint());
117 grid->addRowSpacing(0, fontMetrics().height() - marginHint() + spacingHint());
118 grid->setColStretch(2, 1);
119 int indentWidth = 3 * KDialog::spacingHint();
120 grid->addColSpacing(0, indentWidth);
121 grid->addColSpacing(1, indentWidth);
123 int alignment = TQApplication::reverseLayout() ? TQt::AlignRight : TQt::AlignLeft;
126 box =
new TQHBox(group);
127 box->setSpacing(spacingHint());
128 grid->addMultiCellWidget(box, 1, 1, 0, 2);
129 mVolumeCheckbox =
new CheckBox(i18n_v_SetVolume(), box);
130 mVolumeCheckbox->setFixedSize(mVolumeCheckbox->sizeHint());
131 connect(mVolumeCheckbox, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(slotVolumeToggled(
bool)));
132 TQWhatsThis::add(mVolumeCheckbox,
133 i18n(
"Select to choose the volume for playing the sound file."));
136 mVolumeSlider =
new Slider(0, 100, 10, 0, TQt::Horizontal, box);
137 mVolumeSlider->setTickmarks(TQSlider::Below);
138 mVolumeSlider->setTickInterval(10);
139 mVolumeSlider->setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Fixed));
140 TQWhatsThis::add(mVolumeSlider, i18n(
"Choose the volume for playing the sound file."));
141 mVolumeCheckbox->setFocusWidget(mVolumeSlider);
144 mFadeCheckbox =
new CheckBox(i18n(
"Fade"), group);
145 mFadeCheckbox->setFixedSize(mFadeCheckbox->sizeHint());
146 connect(mFadeCheckbox, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(slotFadeToggled(
bool)));
147 TQWhatsThis::add(mFadeCheckbox,
148 i18n(
"Select to fade the volume when the sound file first starts to play."));
149 grid->addMultiCellWidget(mFadeCheckbox, 2, 2, 1, 2, alignment);
152 mFadeBox =
new TQHBox(group);
153 mFadeBox->setSpacing(spacingHint());
154 grid->addWidget(mFadeBox, 3, 2, alignment);
155 TQLabel* label =
new TQLabel(i18n(
"Time period over which to fade the sound",
"Fade time:"), mFadeBox);
156 label->setFixedSize(label->sizeHint());
157 mFadeTime =
new SpinBox(1, 999, 1, mFadeBox);
158 mFadeTime->setLineShiftStep(10);
159 mFadeTime->setFixedSize(mFadeTime->sizeHint());
160 label->setBuddy(mFadeTime);
161 label =
new TQLabel(i18n(
"seconds"), mFadeBox);
162 label->setFixedSize(label->sizeHint());
163 TQWhatsThis::add(mFadeBox, i18n(
"Enter how many seconds to fade the sound before reaching the set volume."));
166 mFadeVolumeBox =
new TQHBox(group);
167 mFadeVolumeBox->setSpacing(spacingHint());
168 grid->addWidget(mFadeVolumeBox, 4, 2);
169 label =
new TQLabel(i18n(
"Initial volume:"), mFadeVolumeBox);
170 label->setFixedSize(label->sizeHint());
171 mFadeSlider =
new Slider(0, 100, 10, 0, TQt::Horizontal, mFadeVolumeBox);
172 mFadeSlider->setTickmarks(TQSlider::Below);
173 mFadeSlider->setTickInterval(10);
174 mFadeSlider->setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Fixed));
175 label->setBuddy(mFadeSlider);
176 TQWhatsThis::add(mFadeVolumeBox, i18n(
"Choose the initial volume for playing the sound file."));
180 if (KAlarm::readConfigWindowSize(SOUND_DIALOG_NAME, s))
184 mFileEdit->setText(file);
185 mRepeatCheckbox->setChecked(repeat);
186 mVolumeCheckbox->setChecked(volume >= 0);
187 mVolumeSlider->setValue(volume >= 0 ?
static_cast<int>(volume*100) : 100);
188 mFadeCheckbox->setChecked(fadeVolume >= 0);
189 mFadeSlider->setValue(fadeVolume >= 0 ?
static_cast<int>(fadeVolume*100) : 100);
190 mFadeTime->setValue(fadeSeconds);
191 slotVolumeToggled(volume >= 0);
194 SoundDlg::~SoundDlg()
202 void SoundDlg::setReadOnly(
bool readOnly)
204 if (readOnly != mReadOnly)
206 mFileEdit->setReadOnly(readOnly);
207 mFileBrowseButton->setReadOnly(readOnly);
208 mRepeatCheckbox->setReadOnly(readOnly);
209 mVolumeCheckbox->setReadOnly(readOnly);
210 mVolumeSlider->setReadOnly(readOnly);
211 mFadeCheckbox->setReadOnly(readOnly);
212 mFadeTime->setReadOnly(readOnly);
213 mFadeSlider->setReadOnly(readOnly);
214 mReadOnly = readOnly;
224 bool SoundDlg::getSettings(
float& volume,
float& fadeVolume,
int& fadeSeconds)
const
226 volume = mVolumeCheckbox->isChecked() ? (float)mVolumeSlider->value() / 100 : -1;
227 if (mFadeCheckbox->isChecked())
229 fadeVolume = (float)mFadeSlider->value() / 100;
230 fadeSeconds = mFadeTime->value();
237 return mRepeatCheckbox->isChecked();
244 void SoundDlg::resizeEvent(TQResizeEvent* re)
247 KAlarm::writeConfigWindowSize(SOUND_DIALOG_NAME, re->size());
248 mVolumeSlider->resize(mFadeSlider->size());
249 KDialog::resizeEvent(re);
252 void SoundDlg::showEvent(TQShowEvent* se)
254 mVolumeSlider->resize(mFadeSlider->size());
255 KDialog::showEvent(se);
261 void SoundDlg::slotOk()
273 void SoundDlg::slotPickFile()
275 TQString url = SoundPicker::browseFile(mDefaultDir, mFileEdit->text());
277 mFileEdit->setText(url);
283 void SoundDlg::playSound()
287 KAudioPlayer::play(TQFile::encodeName(mFileName));
297 MainWindow* mmw = MainWindow::mainMainWindow();
298 if (!url.isValid() || !TDEIO::NetAccess::exists(url,
true, mmw)
299 || !TDEIO::NetAccess::download(url, mLocalAudioFile, mmw))
301 kdError(5950) <<
"SoundDlg::playAudio(): Open failure: " << mFileName << endl;
302 KMessageBox::error(
this, i18n(
"Cannot open audio file:\n%1").arg(mFileName));
305 mPlayTimer =
new TQTimer(
this);
306 connect(mPlayTimer, TQ_SIGNAL(timeout()), TQ_SLOT(checkAudioPlay()));
307 mArtsDispatcher =
new KArtsDispatcher;
308 mPlayStarted =
false;
310 Arts::SoundServerV2 sserver = aserver.server();
311 KDE::PlayObjectFactory factory(sserver);
312 mPlayObject = factory.createPlayObject(mLocalAudioFile,
true);
313 mFilePlay->setPixmap(SmallIcon(
"media-playback-stop"));
314 TQToolTip::add(mFilePlay, i18n(
"Stop sound"));
315 TQWhatsThis::add(mFilePlay, i18n(
"Stop playing the sound"));
316 connect(mPlayObject, TQ_SIGNAL(playObjectCreated()), TQ_SLOT(checkAudioPlay()));
317 if (!mPlayObject->object().isNull())
328 void SoundDlg::checkAudioPlay()
333 if (mPlayObject->state() == Arts::posIdle)
344 kdDebug(5950) <<
"SoundDlg::checkAudioPlay(): start\n";
350 Arts::poTime overall = mPlayObject->overallTime();
351 Arts::poTime current = mPlayObject->currentTime();
352 int time = 1000*(overall.seconds - current.seconds) + overall.ms - current.ms;
355 kdDebug(5950) <<
"SoundDlg::checkAudioPlay(): wait for " << (time+100) <<
"ms\n";
356 mPlayTimer->start(time + 100,
true);
364 void SoundDlg::stopPlay()
367 delete mPlayObject; mPlayObject = 0;
368 delete mArtsDispatcher; mArtsDispatcher = 0;
369 delete mPlayTimer; mPlayTimer = 0;
370 if (!mLocalAudioFile.isEmpty())
372 TDEIO::NetAccess::removeTempFile(mLocalAudioFile);
373 mLocalAudioFile = TQString();
375 mFilePlay->setPixmap(SmallIcon(
"media-playback-start"));
376 TQToolTip::add(mFilePlay, i18n(
"Test the sound"));
377 TQWhatsThis::add(mFilePlay, i18n(
"Play the selected sound file."));
385 bool SoundDlg::checkFile()
387 mFileName = mFileEdit->text();
389 if (KURL::isRelativeURL(mFileName))
392 TQFileInfo f(mFileName);
394 url.setPath(mFileName);
397 url = KURL::fromPathOrURL(mFileName);
403 if (url.isLocalFile() && TDEIO::NetAccess::exists(url,
true,
this))
405 mFileName = url.path();
416 TQStringList soundDirs = TDEGlobal::dirs()->resourceDirs(
"sound");
417 if (!soundDirs.isEmpty())
420 dir.setFilter(TQDir::Files | TQDir::Readable);
421 for (TQStringList::ConstIterator it = soundDirs.begin(); it != soundDirs.end(); ++it)
424 if (dir.isReadable() && dir.count() > 2)
427 url.addPath(mFileName);
428 if (TDEIO::NetAccess::exists(url,
true,
this))
430 mFileName = url.path();
436 url.setPath(TQDir::homeDirPath());
437 url.addPath(mFileName);
438 if (TDEIO::NetAccess::exists(url,
true,
this))
440 mFileName = url.path();
445 KMessageBox::sorry(
this, i18n(
"File not found"));
446 mFileName = TQString();
456 void SoundDlg::slotVolumeToggled(
bool on)
458 mVolumeSlider->setEnabled(on);
459 mFadeCheckbox->setEnabled(on);
460 slotFadeToggled(on && mFadeCheckbox->isChecked());
466 void SoundDlg::slotFadeToggled(
bool on)
468 mFadeBox->setEnabled(on);
469 mFadeVolumeBox->setEnabled(on);