File indexing completed on 2024-05-12 05:14:58

0001 /*
0002  *  soundpicker.h  -  widget to select a sound file or a beep
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2002-2020 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "preferences.h"
0012 
0013 #include <QFrame>
0014 #include <QString>
0015 #include <QUrl>
0016 
0017 class ComboBox;
0018 class PushButton;
0019 
0020 
0021 class SoundPicker : public QFrame
0022 {
0023     Q_OBJECT
0024 public:
0025     /** Constructor.
0026      *  @param parent The parent object of this widget.
0027      */
0028     explicit SoundPicker(QWidget* parent);
0029 
0030     /** Initialises the widget's state.
0031      *  @param type     The option to select.
0032      *  @param filename The full path or URL of the sound file to select.
0033      *                  If the 'file' option is not initially selected,
0034      *                  @p filename provides the default should 'file'
0035      *                  later be selected by the user.
0036      *  @param volume   The volume to play a sound file, or < 0 for no
0037      *                  volume setting. If the 'file' option is not
0038      *                  initially selected, @p volume provides the default
0039      *                  should 'file' later be selected by the user.
0040      *  @param fadeVolume  The initial volume to play a sound file if fading
0041      *                     is to be used, or < 0 for no fading. If the
0042      *                     'file' option is not initially selected, @p
0043      *                     fadeVolume provides the default should 'file'
0044      *                     later be selected by the user.
0045      *  @param fadeSeconds The number of seconds over which the sound file
0046      *                     volume should be faded, or 0 for no fading. If
0047      *                     the 'file' option is not initially selected,
0048      *                     @p fadeSeconds provides the default should
0049      *                     'file' later be selected by the user.
0050      *  @param repeatPause Number of seconds to pause between sound file
0051      *                     repetitions, or -1 for no repetition. If the
0052      *                     'file' option is not initially selected,
0053      *                     @p repeatPause provides the default should 'file'
0054      *                     later be selected by the user.
0055      */
0056     void set(Preferences::SoundType type, const QString& filename, float volume, float fadeVolume, int fadeSeconds, int repeatPause);
0057 
0058     /** Returns true if the widget is read only for the user. */
0059     bool isReadOnly() const          { return mReadOnly; }
0060 
0061     /** Sets whether the widget can be changed the user.
0062      *  @param readOnly True to set the widget read-only, false to set it read-write.
0063      */
0064     void setReadOnly(bool readOnly);
0065 
0066     /** Show or hide the 'file' option.
0067      *  If it is to be hidden and it is currently selected, sound is turned off.
0068      */
0069     void showFile(bool show);
0070 
0071     /** Show or hide the 'speak' option.
0072      *  If it is to be hidden and it is currently selected, sound is turned off.
0073      */
0074     void showSpeak(bool show);
0075 
0076     /** Returns the selected option. */
0077     Preferences::SoundType sound() const;
0078 
0079     /** If the 'file' option is selected, returns the URL of the chosen file.
0080      *  Otherwise returns an empty URL.
0081      */
0082     QUrl file() const;
0083 
0084     /** Returns the volume and fade characteristics for playing a sound file.
0085      *  @param fadeVolume  Receives the initial volume if the volume is to
0086      *                     be faded, else -1.
0087      *  @param fadeSeconds Receives the number of seconds over which the
0088      *                     volume is to be faded, else 0.
0089      *  @return            Volume to play the sound file, or < 0 if the
0090      *                     'file' option is not selected.
0091      */
0092     float volume(float& fadeVolume, int& fadeSeconds) const;
0093 
0094     /** Returns pause in seconds between repetitions of the sound file,
0095      *  or -1 if no repeat or 'file' option is not selected.
0096      */
0097     int repeatPause() const;
0098 
0099     /** Returns the current file URL regardless of whether the 'file' option is selected. */
0100     QUrl fileSetting() const   { return mFile; }
0101 
0102     /** Returns the current file repetition setting regardless of whether
0103      *  the 'file' option is selected.
0104      */
0105     bool repeatPauseSetting() const { return mRepeatPause; }
0106 
0107     /** Display a dialog to choose a sound file, initially highlighting
0108      *  @p initialFile if non-null.
0109      *  @param file        Updated to URL selected, in human readable format,
0110      *                     or empty if none selected.
0111      *  @param initialDir  Initial directory to display if @p initialFile
0112      *                     is null. If a file is chosen, this is updated to
0113      *                     the directory containing the chosen file.
0114      *  @param initialFile Full path name or URL of file to be highlighted
0115      *                     initially.  If null, no file will be highlighted.
0116      *  @return            true if @p file value can be used,
0117      *                     false if the dialog was deleted while visible.
0118      */
0119     static bool browseFile(QString& file, QString& initialDir, const QString& initialFile = QString());
0120 
0121     static QString i18n_label_Sound();  // text of Sound label
0122     static QString i18n_combo_None();   // text of None combo box item
0123     static QString i18n_combo_Beep();   // text of Beep combo box item
0124     static QString i18n_combo_Speak();  // text of Speak combo box item
0125     static QString i18n_combo_File();   // text of File combo box item
0126 
0127 Q_SIGNALS:
0128     void           changed();     // emitted when any contents change
0129 
0130 private Q_SLOTS:
0131     void           slotTypeSelected(int id);
0132     void           slotPickFile();
0133     void           setLastType();
0134 
0135 private:
0136     void           selectType(Preferences::SoundType);
0137 
0138     ComboBox*      mTypeCombo;
0139     QWidget*       mTypeBox;
0140     PushButton*    mFilePicker;
0141     QUrl           mFile;         // sound file to play when alarm is triggered
0142     float          mVolume;       // volume for file, or < 0 to not set volume
0143     float          mFadeVolume;   // initial volume for file, or < 0 for no fading
0144     int            mFadeSeconds;  // fade interval in seconds
0145     int            mRepeatPause;  // seconds to pause between repetitions of the sound file, or -1 if no repeat
0146     Preferences::SoundType mLastType;   // last selected sound option
0147     bool           mFileShowing;        // File option is shown in combo box
0148     bool           mSpeakShowing;       // Speak option is shown in combo box
0149     bool           mRevertType {false}; // reverting to last selected sound option
0150     bool           mReadOnly {false};
0151 };
0152 
0153 // vim: et sw=4: