[multimedia/kid3] /: Click on play tool bar time toggles between elapsed and remaining time

Urs Fleisch null at kde.org
Sat Feb 11 15:22:08 GMT 2023


Git commit 69dfc21e1b9a6eb536894286952d4a374724000d by Urs Fleisch.
Committed on 11/02/2023 at 15:21.
Pushed by ufleisch into branch 'master'.

Click on play tool bar time toggles between elapsed and remaining time

BUG 465371

M  +3    -0    doc/en/index.docbook
M  +72   -9    src/gui/widgets/playtoolbar.cpp
M  +14   -0    src/gui/widgets/playtoolbar.h

https://invent.kde.org/multimedia/kid3/commit/69dfc21e1b9a6eb536894286952d4a374724000d

diff --git a/doc/en/index.docbook b/doc/en/index.docbook
index 31c3e5af..0b6e34c3 100644
--- a/doc/en/index.docbook
+++ b/doc/en/index.docbook
@@ -2129,6 +2129,9 @@ basic operations (<guibutton>Play/Pause</guibutton>,
 sliders for position and volume and a display of the current position.
 If multiple files are selected, the selected tracks are played, else all
 files will be played.
+</para><para>
+The time displayed can be toggled between elapsed and remaining time by
+clicking on the display.
 </para></listitem>
 </varlistentry>
 
diff --git a/src/gui/widgets/playtoolbar.cpp b/src/gui/widgets/playtoolbar.cpp
index 4cd9c47c..b32737c6 100644
--- a/src/gui/widgets/playtoolbar.cpp
+++ b/src/gui/widgets/playtoolbar.cpp
@@ -46,6 +46,47 @@ namespace {
 
 const QString zeroTime(QLatin1String(" 0:00"));
 
+/**
+ * Event filter for click on time LCD.
+ */
+class TimeLcdClickHandler : public QObject {
+public:
+  /**
+   * Constructor.
+   * @param playToolBar play tool bar
+   */
+  explicit TimeLcdClickHandler(PlayToolBar* playToolBar)
+    : QObject(playToolBar), m_playToolBar(playToolBar) {}
+  virtual ~TimeLcdClickHandler() override = default;
+
+protected:
+  /**
+   * Event filter function, calls PlayToolBar::toggleTimeDisplayMode().
+   *
+   * @param obj watched object
+   * @param event event for object
+   *
+   * @return true if event is filtered.
+   */
+  virtual bool eventFilter(QObject* obj, QEvent* event) override;
+
+private:
+  Q_DISABLE_COPY(TimeLcdClickHandler)
+
+  PlayToolBar* m_playToolBar;
+};
+
+bool TimeLcdClickHandler::eventFilter(QObject* obj, QEvent* event)
+{
+  if (event->type() == QEvent::MouseButtonRelease) {
+    m_playToolBar->toggleTimeDisplayMode();
+    return true;
+  } else {
+    // standard event processing
+    return QObject::eventFilter(obj, event);
+  }
+}
+
 }
 
 /**
@@ -55,7 +96,8 @@ const QString zeroTime(QLatin1String(" 0:00"));
  * @param parent parent widget
  */
 PlayToolBar::PlayToolBar(AudioPlayer* player, QWidget* parent)
-  : QToolBar(parent), m_player(player)
+  : QToolBar(parent), m_player(player),
+    m_timeDisplayMode(TimeDisplayMode::Elapsed)
 {
   setObjectName(QLatin1String("Kid3Player"));
   setWindowTitle(tr("Play"));
@@ -80,8 +122,9 @@ PlayToolBar::PlayToolBar(AudioPlayer* player, QWidget* parent)
   m_seekSlider = new QSlider(Qt::Horizontal, splitter);
   m_seekSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
   m_seekSlider->setMinimum(0);
+  m_duration = mediaPlayer->duration();
   // Setting a maximum of 0 crashes with Qt 5.4.0 on Mac OS X.
-  int maximum = mediaPlayer->duration() / 1000;
+  int maximum = m_duration / 1000;
   if (maximum > 0) {
     m_seekSlider->setMaximum(maximum);
   }
@@ -107,6 +150,7 @@ PlayToolBar::PlayToolBar(AudioPlayer* player, QWidget* parent)
   m_timeLcd->setFrameStyle(QFrame::NoFrame);
   m_timeLcd->display(zeroTime);
   m_timeLcd->setDigitCount(7);
+  m_timeLcd->installEventFilter(new TimeLcdClickHandler(this));
 
   addAction(m_playOrPauseAction);
   addAction(m_stopAction);
@@ -179,18 +223,26 @@ void PlayToolBar::closeEvent(QCloseEvent*)
  */
 void PlayToolBar::tick(qint64 msec)
 {
-  int hours = msec / (60 * 60 * 1000);
-  int minutes = (msec / (60 * 1000)) % 60;
-  int seconds = (msec / 1000) % 60;
-  if (msec % 1000 >= 500) {
+  qint64 displayedMsecs = msec;
+  QString sign;
+  if (m_timeDisplayMode == TimeDisplayMode::Remaining) {
+    displayedMsecs = qAbs(m_duration - msec);
+    sign = QLatin1String("-");
+  }
+  int hours = displayedMsecs / (60 * 60 * 1000);
+  int minutes = (displayedMsecs / (60 * 1000)) % 60;
+  int seconds = (displayedMsecs / 1000) % 60;
+  if (displayedMsecs % 1000 >= 500) {
     ++seconds;
   }
   if (hours == 0) {
-    m_timeLcd->display(QString(QLatin1String("%1:%2"))
+    m_timeLcd->display(QString(QLatin1String("%1%2:%3"))
+                       .arg(sign)
                        .arg(minutes, 2, 10, QLatin1Char(' '))
                        .arg(seconds, 2, 10, QLatin1Char('0')));
   } else {
-    m_timeLcd->display(QString(QLatin1String("%1:%2:%3"))
+    m_timeLcd->display(QString(QLatin1String("%1%2:%3:%4"))
+                       .arg(sign)
                        .arg(hours, 2, 10, QLatin1Char(' '))
                        .arg(minutes, 2, 10, QLatin1Char('0'))
                        .arg(seconds, 2, 10, QLatin1Char('0')));
@@ -249,6 +301,7 @@ void PlayToolBar::error(QMediaPlayer::Error err)
  */
 void PlayToolBar::durationChanged(qint64 duration)
 {
+  m_duration = duration;
   int maximum = duration / 1000;
   // Setting a maximum of 0 crashes with Qt 5.4.0 on Mac OS X.
   if (maximum > 0) {
@@ -306,6 +359,15 @@ void PlayToolBar::toggleMute()
       ? QStyle::SP_MediaVolumeMuted : QStyle::SP_MediaVolume));
 }
 
+/**
+ * Toggle time display mode.
+ */
+void PlayToolBar::toggleTimeDisplayMode()
+{
+  m_timeDisplayMode = m_timeDisplayMode == TimeDisplayMode::Elapsed
+      ? TimeDisplayMode::Remaining : TimeDisplayMode::Elapsed;
+}
+
 /**
  * Update display and button state when the current track is changed.
  *
@@ -322,7 +384,8 @@ void  PlayToolBar::trackChanged(const QString& filePath,
   m_previousAction->setEnabled(hasPrevious);
   m_nextAction->setEnabled(hasNext);
 
-  int maximum = m_player->mediaPlayer()->duration() / 1000;
+  m_duration = m_player->mediaPlayer()->duration();
+  int maximum = m_duration / 1000;
   // Setting a maximum of 0 crashes with Qt 5.4.0 on Mac OS X.
   if (maximum > 0) {
     m_seekSlider->setMaximum(maximum);
diff --git a/src/gui/widgets/playtoolbar.h b/src/gui/widgets/playtoolbar.h
index a45141d8..36a2c6b6 100644
--- a/src/gui/widgets/playtoolbar.h
+++ b/src/gui/widgets/playtoolbar.h
@@ -42,6 +42,12 @@ class QSlider;
 class PlayToolBar : public QToolBar {
   Q_OBJECT
 public:
+  /** What time is displayed in m_timeLcd. */
+  enum class TimeDisplayMode {
+    Elapsed,  /**< Elapsed time */
+    Remaining /**< Remaining time */
+  };
+
   /**
    * Constructor.
    *
@@ -55,6 +61,11 @@ public:
    */
   virtual ~PlayToolBar() override;
 
+  /**
+   * Toggle time display mode.
+   */
+  void toggleTimeDisplayMode();
+
 signals:
   /**
    * Emitted when an error occurs.
@@ -156,4 +167,7 @@ private:
   QAction* m_muteAction;
   QSlider* m_seekSlider;
   QSlider* m_volumeSlider;
+
+  qint64 m_duration;
+  TimeDisplayMode m_timeDisplayMode;
 };



More information about the kde-doc-english mailing list