• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.14
  • kdepim
  • kalarm
soundpicker.cpp
Go to the documentation of this file.
1 /*
2  * soundpicker.cpp - widget to select a sound file or a beep
3  * Program: kalarm
4  * Copyright © 2002-2013 by David Jarvie <djarvie@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kalarm.h"
22 
23 #include "autoqpointer.h"
24 #include "combobox.h"
25 #include "functions.h"
26 #include "kalarmapp.h"
27 #include "pushbutton.h"
28 #include "sounddlg.h"
29 #include "soundpicker.h"
30 
31 #include <kglobal.h>
32 #include <klocale.h>
33 #include <kfiledialog.h>
34 #include <kstandarddirs.h>
35 #include <kiconloader.h>
36 #include <khbox.h>
37 #include <phonon/backendcapabilities.h>
38 #include <kdebug.h>
39 
40 #include <QTimer>
41 #include <QLabel>
42 #include <QHBoxLayout>
43 
44 
45 static QMap<Preferences::SoundType, int> indexes; // mapping from sound type to combo index
46 
47 
48 // Collect these widget labels together to ensure consistent wording and
49 // translations across different modules.
50 QString SoundPicker::i18n_label_Sound() { return i18nc("@label:listbox Listbox providing audio options", "Sound:"); }
51 QString SoundPicker::i18n_combo_None() { return i18nc("@item:inlistbox No sound", "None"); }
52 QString SoundPicker::i18n_combo_Beep() { return i18nc("@item:inlistbox", "Beep"); }
53 QString SoundPicker::i18n_combo_Speak() { return i18nc("@item:inlistbox", "Speak"); }
54 QString SoundPicker::i18n_combo_File() { return i18nc("@item:inlistbox", "Sound file"); }
55 
56 
57 SoundPicker::SoundPicker(QWidget* parent)
58  : QFrame(parent),
59  mRevertType(false),
60  mReadOnly(false)
61 {
62  QHBoxLayout* soundLayout = new QHBoxLayout(this);
63  soundLayout->setMargin(0);
64  soundLayout->setSpacing(KDialog::spacingHint());
65  mTypeBox = new KHBox(this); // this is to control the QWhatsThis text display area
66  mTypeBox->setMargin(0);
67  mTypeBox->setSpacing(KDialog::spacingHint());
68 
69  QLabel* label = new QLabel(i18n_label_Sound(), mTypeBox);
70  label->setFixedSize(label->sizeHint());
71 
72  // Sound type combo box
73  // The order of combo box entries must correspond with the 'Type' enum.
74  if (indexes.isEmpty())
75  {
76  indexes[Preferences::Sound_None] = 0;
77  indexes[Preferences::Sound_Beep] = 1;
78  indexes[Preferences::Sound_File] = 2;
79  indexes[Preferences::Sound_Speak] = 3;
80  }
81 
82  mTypeCombo = new ComboBox(mTypeBox);
83  mTypeCombo->addItem(i18n_combo_None()); // index None
84  mTypeCombo->addItem(i18n_combo_Beep()); // index Beep
85  mTypeCombo->addItem(i18n_combo_File()); // index PlayFile
86  mSpeakShowing = !theApp()->speechEnabled();
87  showSpeak(!mSpeakShowing); // index Speak (only displayed if appropriate)
88  connect(mTypeCombo, SIGNAL(activated(int)), SLOT(slotTypeSelected(int)));
89  connect(mTypeCombo, SIGNAL(currentIndexChanged(int)), SIGNAL(changed()));
90  label->setBuddy(mTypeCombo);
91  soundLayout->addWidget(mTypeBox);
92 
93  // Sound file picker button
94  mFilePicker = new PushButton(this);
95  mFilePicker->setIcon(KIcon(SmallIcon(QLatin1String("audio-x-generic"))));
96  int size = mFilePicker->sizeHint().height();
97  mFilePicker->setFixedSize(size, size);
98  connect(mFilePicker, SIGNAL(clicked()), SLOT(slotPickFile()));
99  mFilePicker->setToolTip(i18nc("@info:tooltip", "Configure sound file"));
100  mFilePicker->setWhatsThis(i18nc("@info:whatsthis", "Configure a sound file to play when the alarm is displayed."));
101  soundLayout->addWidget(mFilePicker);
102 
103  // Initialise the file picker button state and tooltip
104  mTypeCombo->setCurrentIndex(indexes[Preferences::Sound_None]);
105  mFilePicker->setEnabled(false);
106 }
107 
108 /******************************************************************************
109 * Set the read-only status of the widget.
110 */
111 void SoundPicker::setReadOnly(bool readOnly)
112 {
113  // Don't set the sound file picker read-only since it still needs to
114  // display the read-only SoundDlg.
115  mTypeCombo->setReadOnly(readOnly);
116  mReadOnly = readOnly;
117 }
118 
119 /******************************************************************************
120 * Show or hide the Speak option.
121 */
122 void SoundPicker::showSpeak(bool show)
123 {
124  if (!theApp()->speechEnabled())
125  show = false; // speech capability is not installed
126  if (show == mSpeakShowing)
127  return; // no change
128  if (!show && mTypeCombo->currentIndex() == indexes[Preferences::Sound_Speak])
129  mTypeCombo->setCurrentIndex(indexes[Preferences::Sound_None]);
130  if (mTypeCombo->count() == indexes[Preferences::Sound_Speak]+1)
131  mTypeCombo->removeItem(indexes[Preferences::Sound_Speak]); // precaution in case of mix-ups
132  QString whatsThis;
133  QString opt1 = i18nc("@info:whatsthis", "<interface>%1</interface>: the message is displayed silently.", i18n_combo_None());
134  QString opt2 = i18nc("@info:whatsthis", "<interface>%1</interface>: a simple beep is sounded.", i18n_combo_Beep());
135  QString opt3 = i18nc("@info:whatsthis", "<interface>%1</interface>: an audio file is played. You will be prompted to choose the file and set play options.", i18n_combo_File());
136  if (show)
137  {
138  mTypeCombo->addItem(i18n_combo_Speak());
139  QString opt4 = i18nc("@info:whatsthis", "<interface>%1</interface>: the message text is spoken.", i18n_combo_Speak());
140  whatsThis = i18nc("@info:whatsthis Combination of multiple whatsthis items",
141  "<para>Choose a sound to play when the message is displayed:"
142  "<list><item>%1</item>"
143  "<item>%2</item>"
144  "<item>%3</item>"
145  "<item>%4</item></list></para>", opt1, opt2, opt3, opt4);
146  }
147  else
148  whatsThis = i18nc("@info:whatsthis Combination of multiple whatsthis items",
149  "<para>Choose a sound to play when the message is displayed:"
150  "<list><item>%1</item>"
151  "<item>%2</item>"
152  "<item>%3</item></list></para>", opt1, opt2, opt3);
153  mTypeBox->setWhatsThis(whatsThis);
154  mSpeakShowing = show;
155 }
156 
157 /******************************************************************************
158 * Return the currently selected option.
159 */
160 Preferences::SoundType SoundPicker::sound() const
161 {
162  int current = mTypeCombo->currentIndex();
163  for (QMap<Preferences::SoundType, int>::ConstIterator it = indexes.constBegin(); it != indexes.constEnd(); ++it)
164  if (it.value() == current)
165  return it.key();
166  return Preferences::Sound_None;
167 }
168 
169 /******************************************************************************
170 * Return the selected sound file, if the File option is selected.
171 * Returns empty URL if File is not currently selected.
172 */
173 KUrl SoundPicker::file() const
174 {
175  return (mTypeCombo->currentIndex() == indexes[Preferences::Sound_File]) ? mFile : KUrl();
176 }
177 
178 /******************************************************************************
179 * Return the specified volumes (range 0 - 1).
180 * Returns < 0 if beep is currently selected, or if 'set volume' is not selected.
181 */
182 float SoundPicker::volume(float& fadeVolume, int& fadeSeconds) const
183 {
184  if (mTypeCombo->currentIndex() == indexes[Preferences::Sound_File] && !mFile.isEmpty())
185  {
186  fadeVolume = mFadeVolume;
187  fadeSeconds = mFadeSeconds;
188  return mVolume;
189  }
190  else
191  {
192  fadeVolume = -1;
193  fadeSeconds = 0;
194  return -1;
195  }
196 }
197 
198 /******************************************************************************
199 * Return the pause between sound file repetitions is selected.
200 * Reply = pause in seconds, or -1 if repetition is not selected or beep is selected.
201 */
202 int SoundPicker::repeatPause() const
203 {
204  return mTypeCombo->currentIndex() == indexes[Preferences::Sound_File] && !mFile.isEmpty() ? mRepeatPause : -1;
205 }
206 
207 /******************************************************************************
208 * Initialise the widget's state.
209 */
210 void SoundPicker::set(Preferences::SoundType type, const QString& f, float volume, float fadeVolume, int fadeSeconds, int repeatPause)
211 {
212  if (type == Preferences::Sound_File && f.isEmpty())
213  type = Preferences::Sound_Beep;
214  mFile = KUrl(f);
215  mVolume = volume;
216  mFadeVolume = fadeVolume;
217  mFadeSeconds = fadeSeconds;
218  mRepeatPause = repeatPause;
219  mTypeCombo->setCurrentIndex(indexes[type]); // this doesn't trigger slotTypeSelected()
220  mFilePicker->setEnabled(type == Preferences::Sound_File);
221  mTypeCombo->setToolTip(type == Preferences::Sound_File ? mFile.prettyUrl() : QString());
222  mLastType = type;
223 }
224 
225 /******************************************************************************
226 * Called when the sound option is changed.
227 */
228 void SoundPicker::slotTypeSelected(int id)
229 {
230  Preferences::SoundType newType = Preferences::Sound_None;
231  for (QMap<Preferences::SoundType, int>::ConstIterator it = indexes.constBegin(); it != indexes.constEnd(); ++it)
232  {
233  if (it.value() == id)
234  {
235  newType = it.key();
236  break;
237  }
238  }
239  if (newType == mLastType || mRevertType)
240  return;
241  if (mLastType == Preferences::Sound_File)
242  {
243  mFilePicker->setEnabled(false);
244  mTypeCombo->setToolTip(QString());
245  }
246  else if (newType == Preferences::Sound_File)
247  {
248  if (mFile.isEmpty())
249  {
250  slotPickFile();
251  if (mFile.isEmpty())
252  return; // revert to previously selected type
253  }
254  mFilePicker->setEnabled(true);
255  mTypeCombo->setToolTip(mFile.prettyUrl());
256  }
257  mLastType = newType;
258 }
259 
260 /******************************************************************************
261 * Called when the file picker button is clicked.
262 */
263 void SoundPicker::slotPickFile()
264 {
265  KUrl oldfile = mFile;
266  // Use AutoQPointer to guard against crash on application exit while
267  // the dialogue is still open. It prevents double deletion (both on
268  // deletion of EditAlarmDlg, and on return from this function).
269  AutoQPointer<SoundDlg> dlg = new SoundDlg(mFile.prettyUrl(), mVolume, mFadeVolume, mFadeSeconds, mRepeatPause, i18nc("@title:window", "Sound File"), this);
270  dlg->setReadOnly(mReadOnly);
271  bool accepted = (dlg->exec() == QDialog::Accepted);
272  if (mReadOnly)
273  return;
274  if (accepted)
275  {
276  float volume, fadeVolume;
277  int fadeTime;
278  dlg->getVolume(volume, fadeVolume, fadeTime);
279  KUrl file = dlg->getFile();
280  if (!file.isEmpty())
281  mFile = file;
282  mRepeatPause = dlg->repeatPause();
283  mVolume = volume;
284  mFadeVolume = fadeVolume;
285  mFadeSeconds = fadeTime;
286  }
287  if (mFile.isEmpty())
288  {
289  // No audio file is selected, so revert to previously selected option
290 #if 0
291 // Remove mRevertType, setLastType(), #include QTimer
292  // But wait a moment until setting the radio button, or it won't work.
293  mRevertType = true; // prevent sound dialog popping up twice
294  QTimer::singleShot(0, this, SLOT(setLastType()));
295 #else
296  mTypeCombo->setCurrentIndex(indexes[mLastType]);
297 #endif
298  mTypeCombo->setToolTip(QString());
299  }
300  else
301  mTypeCombo->setToolTip(mFile.prettyUrl());
302  if (accepted || mFile != oldfile)
303  emit changed();
304 }
305 
306 /******************************************************************************
307 * Select the previously selected sound type.
308 */
309 void SoundPicker::setLastType()
310 {
311  mTypeCombo->setCurrentIndex(indexes[mLastType]);
312  mRevertType = false;
313 }
314 
315 /******************************************************************************
316 * Display a dialog to choose a sound file, initially highlighting any
317 * specified file. 'initialFile' must be a full path name or URL.
318 * 'defaultDir' is updated to the directory containing the chosen file.
319 * Reply = URL selected. If none is selected, URL.isEmpty() is true.
320 */
321 QString SoundPicker::browseFile(QString& defaultDir, const QString& initialFile)
322 {
323  static QString kdeSoundDir; // directory containing KDE sound files
324  if (defaultDir.isEmpty())
325  {
326  if (kdeSoundDir.isNull())
327  kdeSoundDir = KGlobal::dirs()->findResourceDir("sound", QLatin1String("KDE-Sys-Warning.ogg"));
328  defaultDir = kdeSoundDir;
329  }
330  QString filter = Phonon::BackendCapabilities::availableMimeTypes().join(QLatin1String(" "));
331  return KAlarm::browseFile(i18nc("@title:window", "Choose Sound File"), defaultDir, initialFile, filter, KFile::ExistingOnly, 0);
332 }
333 #include "moc_soundpicker.cpp"
334 // vim: et sw=4:
SoundPicker::changed
void changed()
QWidget
SoundPicker::sound
Preferences::SoundType sound() const
Returns the selected option.
Definition: soundpicker.cpp:160
SoundPicker::file
KUrl file() const
If the 'file' option is selected, returns the URL of the chosen file.
Definition: soundpicker.cpp:173
SoundPicker::setReadOnly
void setReadOnly(bool readOnly)
Sets whether the widget can be changed the user.
Definition: soundpicker.cpp:111
QMap::constBegin
const_iterator constBegin() const
QMap
SoundDlg::getFile
KUrl getFile() const
Definition: sounddlg.cpp:100
QHBoxLayout
SoundPicker::i18n_label_Sound
static QString i18n_label_Sound()
Definition: soundpicker.cpp:50
SoundPicker::volume
float volume(float &fadeVolume, int &fadeSeconds) const
Returns the volume and fade characteristics for playing a sound file.
Definition: soundpicker.cpp:182
QStringList::join
QString join(const QString &separator) const
SoundPicker::i18n_combo_Speak
static QString i18n_combo_Speak()
Definition: soundpicker.cpp:53
Phonon::BackendCapabilities::availableMimeTypes
QStringList availableMimeTypes()
SoundPicker::browseFile
static QString browseFile(QString &initialDir, const QString &initialFile=QString())
Display a dialog to choose a sound file, initially highlighting initialFile if non-null.
Definition: soundpicker.cpp:321
QString::isNull
bool isNull() const
SoundDlg::getVolume
void getVolume(float &volume, float &fadeVolume, int &fadeSeconds) const
Definition: sounddlg.h:105
PushButton
QLabel::setBuddy
void setBuddy(QWidget *buddy)
kalarmapp.h
the KAlarm application object
QWidget::size
QSize size() const
pushbutton.h
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
SoundDlg::repeatPause
int repeatPause() const
Definition: sounddlg.h:107
SoundPicker::set
void set(Preferences::SoundType type, const QString &filename, float volume, float fadeVolume, int fadeSeconds, int repeatPause)
Initialises the widget's state.
Definition: soundpicker.cpp:210
autoqpointer.h
QString::isEmpty
bool isEmpty() const
QMap::constEnd
const_iterator constEnd() const
SoundPicker::i18n_combo_None
static QString i18n_combo_None()
Definition: soundpicker.cpp:51
SoundDlg::setReadOnly
void setReadOnly(bool)
Definition: sounddlg.cpp:81
QString
QLayout::setMargin
void setMargin(int margin)
ComboBox::setReadOnly
virtual void setReadOnly(bool readOnly)
theApp
KAlarmApp * theApp()
Definition: kalarmapp.h:263
SoundDlg
Definition: sounddlg.h:96
QWidget::setFixedSize
void setFixedSize(const QSize &s)
AutoQPointer
QFrame
SoundPicker::repeatPause
int repeatPause() const
Returns pause in seconds between repetitions of the sound file, or -1 if no repeat or 'file' option i...
Definition: soundpicker.cpp:202
QWidget::whatsThis
QString whatsThis() const
QLatin1String
sounddlg.h
functions.h
miscellaneous functions
kalarm.h
QLabel::sizeHint
virtual QSize sizeHint() const
combobox.h
SoundPicker::i18n_combo_Beep
static QString i18n_combo_Beep()
Definition: soundpicker.cpp:52
soundpicker.h
KHBox
indexes
static QMap< Preferences::SoundType, int > indexes
Definition: soundpicker.cpp:45
QWidget::show
void show()
ComboBox
QMap::isEmpty
bool isEmpty() const
SoundPicker::SoundPicker
SoundPicker(QWidget *parent)
Constructor.
Definition: soundpicker.cpp:57
SoundPicker::i18n_combo_File
static QString i18n_combo_File()
Definition: soundpicker.cpp:54
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
SoundPicker::showSpeak
void showSpeak(bool show)
Show or hide the 'speak' option.
Definition: soundpicker.cpp:122
QLabel
QBoxLayout::setSpacing
void setSpacing(int spacing)
KAlarmApp::speechEnabled
bool speechEnabled() const
Definition: kalarmapp.h:64
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal