File indexing completed on 2025-02-23 04:34:24

0001 /**
0002  * \file playtoolbar.h
0003  * Audio player toolbar.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 24-Aug-2010
0008  *
0009  * Copyright (C) 2010-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QToolBar>
0030 #include <QIcon>
0031 #include <QMediaPlayer>
0032 
0033 class QAction;
0034 class QLCDNumber;
0035 class QLabel;
0036 class AudioPlayer;
0037 class QSlider;
0038 
0039 /**
0040  * Audio player toolbar.
0041  */
0042 class PlayToolBar : public QToolBar {
0043   Q_OBJECT
0044 public:
0045   /** What time is displayed in m_timeLcd. */
0046   enum class TimeDisplayMode {
0047     Elapsed,  /**< Elapsed time */
0048     Remaining /**< Remaining time */
0049   };
0050 
0051   /**
0052    * Constructor.
0053    *
0054    * @param player audio player
0055    * @param parent parent widget
0056    */
0057   explicit PlayToolBar(AudioPlayer* player, QWidget* parent);
0058 
0059   /**
0060    * Destructor.
0061    */
0062   ~PlayToolBar() override;
0063 
0064   /**
0065    * Toggle time display mode.
0066    */
0067   void toggleTimeDisplayMode();
0068 
0069   /**
0070    * Get media player actions.
0071    * @return list with named actions for "audio_play", "audio_stop",
0072    * "audio_previous", "audio_next".
0073    */
0074   QList<QAction*> mediaActions() const;
0075 
0076 signals:
0077   /**
0078    * Emitted when an error occurs.
0079    * Parameter: description of error
0080    */
0081   void errorMessage(const QString& msg);
0082 
0083   /**
0084    * Emitted before a file starts playing.
0085    * @param filePath path to file
0086    */
0087   void aboutToPlay(const QString& filePath);
0088 
0089   /**
0090    * Emitted when the window is closed or destroyed.
0091    */
0092   void closed();
0093 
0094 private slots:
0095   /**
0096    * Update displayed time.
0097    *
0098    * @param msec time in ms
0099    */
0100   void tick(qint64 msec);
0101 
0102   /**
0103    * Update button states when the media player state changed.
0104    *
0105    * @param newState new Phonon state
0106    */
0107   void stateChanged(int newState);
0108 
0109   /**
0110    * Update states when a media player error occurs.
0111    *
0112    * @param err error
0113    */
0114   void error(QMediaPlayer::Error err);
0115 
0116   /**
0117    * Called when the duration changes.
0118    * @param duration duration in milliseconds
0119    */
0120   void durationChanged(qint64 duration);
0121 
0122   /**
0123    * Set the tool tip for the volume slider.
0124    * @param volume current volume (0..100)
0125    */
0126   void setVolumeToolTip(int volume);
0127 
0128   /**
0129    * Set current position in track when slider position is changed.
0130    * @param action slider action
0131    */
0132   void seekAction(int action);
0133 
0134   /**
0135    * Set volume when slider position is changed.
0136    * @param action slider action
0137    */
0138   void volumeAction(int action);
0139 
0140   /**
0141    * Toggle muted state.
0142    */
0143   void toggleMute();
0144 
0145   /**
0146    * Update display and button state when the current track is changed.
0147    *
0148    * @param filePath path of currently played audio file
0149    * @param hasPrevious true if a previous track is available
0150    * @param hasNext true if a next track is available
0151    */
0152   void trackChanged(const QString& filePath, bool hasPrevious, bool hasNext);
0153 
0154 protected:
0155   /**
0156    * Stop sound when window is closed.
0157    */
0158   void closeEvent(QCloseEvent*) override;
0159 
0160 private:
0161   QIcon m_playIcon;
0162   QIcon m_pauseIcon;
0163 
0164   QAction* m_playOrPauseAction;
0165   QAction* m_stopAction;
0166   QAction* m_previousAction;
0167   QAction* m_nextAction;
0168 
0169   QLCDNumber* m_timeLcd;
0170   QLabel* m_titleLabel;
0171 
0172   AudioPlayer* m_player;
0173 
0174   QAction* m_muteAction;
0175   QSlider* m_seekSlider;
0176   QSlider* m_volumeSlider;
0177 
0178   qint64 m_duration;
0179   TimeDisplayMode m_timeDisplayMode;
0180 };