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 <qlayout.h>
00024 #include <qregexp.h>
00025 #include <qtooltip.h>
00026 #include <qtimer.h>
00027 #include <qlabel.h>
00028 #include <qhbox.h>
00029 #include <qwhatsthis.h>
00030
00031 #include <kglobal.h>
00032 #include <klocale.h>
00033 #include <kfiledialog.h>
00034 #include <kstandarddirs.h>
00035 #include <kiconloader.h>
00036 #ifndef WITHOUT_ARTS
00037 #include <arts/kplayobjectfactory.h>
00038 #endif
00039 #include <kdebug.h>
00040
00041 #include "combobox.h"
00042 #include "functions.h"
00043 #include "kalarmapp.h"
00044 #include "pushbutton.h"
00045 #include "sounddlg.h"
00046 #include "soundpicker.moc"
00047
00048
00049
00050
00051 QString SoundPicker::i18n_Sound() { return i18n("An audio sound", "Sound"); }
00052 QString SoundPicker::i18n_None() { return i18n("None"); }
00053 QString SoundPicker::i18n_Beep() { return i18n("Beep"); }
00054 QString SoundPicker::i18n_Speak() { return i18n("Speak"); }
00055 QString SoundPicker::i18n_File() { return i18n("Sound file"); }
00056
00057
00058 SoundPicker::SoundPicker(QWidget* parent, const char* name)
00059 : QFrame(parent, name)
00060 {
00061 setFrameStyle(QFrame::NoFrame);
00062 QHBoxLayout* soundLayout = new QHBoxLayout(this, 0, KDialog::spacingHint());
00063 mTypeBox = new QHBox(this);
00064 mTypeBox->setSpacing(KDialog::spacingHint());
00065
00066 QLabel* label = new QLabel(i18n("An audio sound", "&Sound:"), mTypeBox);
00067 label->setFixedSize(label->sizeHint());
00068
00069
00070
00071 mTypeCombo = new ComboBox(false, mTypeBox);
00072 mTypeCombo->insertItem(i18n_None());
00073 mTypeCombo->insertItem(i18n_Beep());
00074 mTypeCombo->insertItem(i18n_File());
00075 mSpeakShowing = !theApp()->speechEnabled();
00076 showSpeak(!mSpeakShowing);
00077 connect(mTypeCombo, SIGNAL(activated(int)), SLOT(slotTypeSelected(int)));
00078 label->setBuddy(mTypeCombo);
00079 soundLayout->addWidget(mTypeBox);
00080
00081
00082 mFilePicker = new PushButton(this);
00083 mFilePicker->setPixmap(SmallIcon("playsound"));
00084 mFilePicker->setFixedSize(mFilePicker->sizeHint());
00085 connect(mFilePicker, SIGNAL(clicked()), SLOT(slotPickFile()));
00086 QWhatsThis::add(mFilePicker, i18n("Configure a sound file to play when the alarm is displayed."));
00087 soundLayout->addWidget(mFilePicker);
00088
00089
00090 mTypeCombo->setCurrentItem(NONE);
00091 mFilePicker->setEnabled(false);
00092 }
00093
00094
00095
00096
00097 void SoundPicker::setReadOnly(bool readOnly)
00098 {
00099 mTypeCombo->setReadOnly(readOnly);
00100 #ifdef WITHOUT_ARTS
00101 mFilePicker->setReadOnly(readOnly);
00102 #endif
00103 mReadOnly = readOnly;
00104 }
00105
00106
00107
00108
00109 void SoundPicker::showSpeak(bool show)
00110 {
00111 if (!theApp()->speechEnabled())
00112 show = false;
00113 if (show == mSpeakShowing)
00114 return;
00115 QString whatsThis = "<p>" + i18n("Choose a sound to play when the message is displayed.")
00116 + "<br>" + i18n("%1: the message is displayed silently.").arg("<b>" + i18n_None() + "</b>")
00117 + "<br>" + i18n("%1: a simple beep is sounded.").arg("<b>" + i18n_Beep() + "</b>")
00118 + "<br>" + i18n("%1: an audio file is played. You will be prompted to choose the file and set play options.").arg("<b>" + i18n_File() + "</b>");
00119 if (!show && mTypeCombo->currentItem() == SPEAK)
00120 mTypeCombo->setCurrentItem(NONE);
00121 if (mTypeCombo->count() == SPEAK+1)
00122 mTypeCombo->removeItem(SPEAK);
00123 if (show)
00124 {
00125 mTypeCombo->insertItem(i18n_Speak());
00126 whatsThis += "<br>" + i18n("%1: the message text is spoken.").arg("<b>" + i18n_Speak() + "</b>") + "</p>";
00127 }
00128 QWhatsThis::add(mTypeBox, whatsThis + "</p>");
00129 mSpeakShowing = show;
00130 }
00131
00132
00133
00134
00135 SoundPicker::Type SoundPicker::sound() const
00136 {
00137 return static_cast<SoundPicker::Type>(mTypeCombo->currentItem());
00138 }
00139
00140
00141
00142
00143
00144 QString SoundPicker::file() const
00145 {
00146 return (mTypeCombo->currentItem() == PLAY_FILE) ? mFile : QString::null;
00147 }
00148
00149
00150
00151
00152
00153 float SoundPicker::volume(float& fadeVolume, int& fadeSeconds) const
00154 {
00155 if (mTypeCombo->currentItem() == PLAY_FILE && !mFile.isEmpty())
00156 {
00157 fadeVolume = mFadeVolume;
00158 fadeSeconds = mFadeSeconds;
00159 return mVolume;
00160 }
00161 else
00162 {
00163 fadeVolume = -1;
00164 fadeSeconds = 0;
00165 return -1;
00166 }
00167 }
00168
00169
00170
00171
00172
00173 bool SoundPicker::repeat() const
00174 {
00175 return mTypeCombo->currentItem() == PLAY_FILE && !mFile.isEmpty() && mRepeat;
00176 }
00177
00178
00179
00180
00181 void SoundPicker::set(SoundPicker::Type type, const QString& f, float volume, float fadeVolume, int fadeSeconds, bool repeat)
00182 {
00183 if (type == PLAY_FILE && f.isEmpty())
00184 type = BEEP;
00185 mFile = f;
00186 mVolume = volume;
00187 mFadeVolume = fadeVolume;
00188 mFadeSeconds = fadeSeconds;
00189 mRepeat = repeat;
00190 QToolTip::add(mFilePicker, mFile);
00191 mTypeCombo->setCurrentItem(type);
00192 mFilePicker->setEnabled(type == PLAY_FILE);
00193 mLastType = type;
00194 }
00195
00196
00197
00198
00199 void SoundPicker::slotTypeSelected(int id)
00200 {
00201 Type newType = static_cast<Type>(id);
00202 if (newType == mLastType)
00203 return;
00204 if (mLastType == PLAY_FILE)
00205 mFilePicker->setEnabled(false);
00206 else if (newType == PLAY_FILE)
00207 {
00208 if (mFile.isEmpty())
00209 {
00210 slotPickFile();
00211 if (mFile.isEmpty())
00212 return;
00213 }
00214 mFilePicker->setEnabled(true);
00215 }
00216 mLastType = newType;
00217 }
00218
00219
00220
00221
00222 void SoundPicker::slotPickFile()
00223 {
00224 #ifdef WITHOUT_ARTS
00225 QString url = browseFile(mDefaultDir, mFile);
00226 if (!url.isEmpty())
00227 mFile = url;
00228 #else
00229 QString file = mFile;
00230 SoundDlg dlg(mFile, mVolume, mFadeVolume, mFadeSeconds, mRepeat, i18n("Sound File"), this, "soundDlg");
00231 dlg.setReadOnly(mReadOnly);
00232 bool accepted = (dlg.exec() == QDialog::Accepted);
00233 if (mReadOnly)
00234 return;
00235 if (accepted)
00236 {
00237 float volume, fadeVolume;
00238 int fadeTime;
00239 file = dlg.getFile();
00240 mRepeat = dlg.getSettings(volume, fadeVolume, fadeTime);
00241 mVolume = volume;
00242 mFadeVolume = fadeVolume;
00243 mFadeSeconds = fadeTime;
00244 }
00245 if (!file.isEmpty())
00246 {
00247 mFile = file;
00248 mDefaultDir = dlg.defaultDir();
00249 }
00250 #endif
00251 QToolTip::add(mFilePicker, mFile);
00252 if (mFile.isEmpty())
00253 {
00254
00255 mTypeCombo->setCurrentItem(mLastType);
00256 }
00257 }
00258
00259
00260
00261
00262
00263
00264
00265 QString SoundPicker::browseFile(QString& defaultDir, const QString& initialFile)
00266 {
00267 static QString kdeSoundDir;
00268 if (defaultDir.isEmpty())
00269 {
00270 if (kdeSoundDir.isNull())
00271 kdeSoundDir = KGlobal::dirs()->findResourceDir("sound", "KDE_Notify.wav");
00272 defaultDir = kdeSoundDir;
00273 }
00274 #ifdef WITHOUT_ARTS
00275 QString filter = QString::fromLatin1("*.wav *.mp3 *.ogg|%1\n*|%2").arg(i18n("Sound Files")).arg(i18n("All Files"));
00276 #else
00277 QStringList filters = KDE::PlayObjectFactory::mimeTypes();
00278 QString filter = filters.join(" ");
00279 #endif
00280 return KAlarm::browseFile(i18n("Choose Sound File"), defaultDir, initialFile, filter, KFile::ExistingOnly, 0, "pickSoundFile");
00281 }