1 /*
2  *  soundpicker.h  -  widget to select a sound file or a beep
3  *  Program:  kalarm
4  *  SPDX-FileCopyrightText: 2002-2020 David Jarvie <djarvie@kde.org>
5  *
6  *  SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #pragma once
10 
11 #include "preferences.h"
12 
13 #include <QFrame>
14 #include <QString>
15 #include <QUrl>
16 
17 class ComboBox;
18 class PushButton;
19 
20 
21 class SoundPicker : public QFrame
22 {
23     Q_OBJECT
24 public:
25     /** Constructor.
26      *  @param parent The parent object of this widget.
27      */
28     explicit SoundPicker(QWidget* parent);
29 
30     /** Initialises the widget's state.
31      *  @param type     The option to select.
32      *  @param filename The full path or URL of the sound file to select.
33      *                  If the 'file' option is not initially selected,
34      *                  @p filename provides the default should 'file'
35      *                  later be selected by the user.
36      *  @param volume   The volume to play a sound file, or < 0 for no
37      *                  volume setting. If the 'file' option is not
38      *                  initially selected, @p volume provides the default
39      *                  should 'file' later be selected by the user.
40      *  @param fadeVolume  The initial volume to play a sound file if fading
41      *                     is to be used, or < 0 for no fading. If the
42      *                     'file' option is not initially selected, @p
43      *                     fadeVolume provides the default should 'file'
44      *                     later be selected by the user.
45      *  @param fadeSeconds The number of seconds over which the sound file
46      *                     volume should be faded, or 0 for no fading. If
47      *                     the 'file' option is not initially selected,
48      *                     @p fadeSeconds provides the default should
49      *                     'file' later be selected by the user.
50      *  @param repeatPause Number of seconds to pause between sound file
51      *                     repetitions, or -1 for no repetition. If the
52      *                     'file' option is not initially selected,
53      *                     @p repeatPause provides the default should 'file'
54      *                     later be selected by the user.
55      */
56     void           set(Preferences::SoundType type, const QString& filename, float volume, float fadeVolume, int fadeSeconds, int repeatPause);
57 
58     /** Returns true if the widget is read only for the user. */
isReadOnly()59     bool           isReadOnly() const          { return mReadOnly; }
60 
61     /** Sets whether the widget can be changed the user.
62      *  @param readOnly True to set the widget read-only, false to set it read-write.
63      */
64     void           setReadOnly(bool readOnly);
65 
66     /** Show or hide the 'file' option.
67      *  If it is to be hidden and it is currently selected, sound is turned off.
68      */
69     void           showFile(bool show);
70 
71     /** Show or hide the 'speak' option.
72      *  If it is to be hidden and it is currently selected, sound is turned off.
73      */
74     void           showSpeak(bool show);
75 
76     /** Returns the selected option. */
77     Preferences::SoundType sound() const;
78 
79     /** If the 'file' option is selected, returns the URL of the chosen file.
80      *  Otherwise returns an empty URL.
81      */
82     QUrl           file() const;
83 
84     /** Returns the volume and fade characteristics for playing a sound file.
85      *  @param fadeVolume  Receives the initial volume if the volume is to
86      *                     be faded, else -1.
87      *  @param fadeSeconds Receives the number of seconds over which the
88      *                     volume is to be faded, else 0.
89      *  @return            Volume to play the sound file, or < 0 if the
90      *                     'file' option is not selected.
91      */
92     float          volume(float& fadeVolume, int& fadeSeconds) const;
93 
94     /** Returns pause in seconds between repetitions of the sound file,
95      *  or -1 if no repeat or 'file' option is not selected.
96      */
97     int            repeatPause() const;
98 
99     /** Returns the current file URL regardless of whether the 'file' option is selected. */
fileSetting()100     QUrl           fileSetting() const   { return mFile; }
101 
102     /** Returns the current file repetition setting regardless of whether
103      *  the 'file' option is selected.
104      */
repeatPauseSetting()105     bool           repeatPauseSetting() const { return mRepeatPause; }
106 
107     /** Display a dialog to choose a sound file, initially highlighting
108      *  @p initialFile if non-null.
109      *  @param file        Updated to URL selected, in human readable format,
110      *                     or empty if none selected.
111      *  @param initialDir  Initial directory to display if @p initialFile
112      *                     is null. If a file is chosen, this is updated to
113      *                     the directory containing the chosen file.
114      *  @param initialFile Full path name or URL of file to be highlighted
115      *                     initially.  If null, no file will be highlighted.
116      *  @return            true if @p file value can be used,
117      *                     false if the dialog was deleted while visible.
118      */
119     static bool    browseFile(QString& file, QString& initialDir, const QString& initialFile = QString());
120 
121     static QString i18n_label_Sound();  // text of Sound label
122     static QString i18n_combo_None();   // text of None combo box item
123     static QString i18n_combo_Beep();   // text of Beep combo box item
124     static QString i18n_combo_Speak();  // text of Speak combo box item
125     static QString i18n_combo_File();   // text of File combo box item
126 
127 Q_SIGNALS:
128     void           changed();     // emitted when any contents change
129 
130 private Q_SLOTS:
131     void           slotTypeSelected(int id);
132     void           slotPickFile();
133     void           setLastType();
134 
135 private:
136     void           selectType(Preferences::SoundType);
137 
138     ComboBox*      mTypeCombo;
139     QWidget*       mTypeBox;
140     PushButton*    mFilePicker;
141     QUrl           mFile;         // sound file to play when alarm is triggered
142     float          mVolume;       // volume for file, or < 0 to not set volume
143     float          mFadeVolume;   // initial volume for file, or < 0 for no fading
144     int            mFadeSeconds;  // fade interval in seconds
145     int            mRepeatPause;  // seconds to pause between repetitions of the sound file, or -1 if no repeat
146     Preferences::SoundType mLastType;   // last selected sound option
147     bool           mFileShowing;        // File option is shown in combo box
148     bool           mSpeakShowing;       // Speak option is shown in combo box
149     bool           mRevertType {false}; // reverting to last selected sound option
150     bool           mReadOnly {false};
151 };
152 
153 
154 // vim: et sw=4:
155