[PATCH] Play previous track when in shuffle mode

Forum to talk about the development of SMPlayer (code, patches...).

[PATCH] Play previous track when in shuffle mode

Postby capt.d. » Tue Nov 29, 2016 10:41 pm

Aloha

It is possible to go back one track when playing in shuffle mode. Continuous pressing is giving two-tracks loop :)
Code: Select all
--- playlist.cpp   2016-10-30 00:42:20.000000000 +0200
+++ playlist.cpp.new   2016-11-29 21:51:16.000000000 +0100
@@ -76,8 +76,7 @@
 #endif
 
 #define DRAG_ITEMS 0
-#define PL_ALLOW_DUPLICATES 1
-#define SIMULATE_FILE_DELETION 0
+#define PL_ALLOW_DUPLICATES 1w#define SIMULATE_FILE_DELETION 0
 #define USE_ITEM_DELEGATE 0
 
 #define COL_NUM 0
@@ -119,6 +118,7 @@
    setDuration(0);
    setPlayed(false);
    setCurrent(false);
+        setPrevPlayed(false);
 
    col_num->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
 }
@@ -133,6 +133,7 @@
    setDuration(duration);
    setPlayed(false);
    setCurrent(false);
+        setPrevPlayed(false);
 
    col_num->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
 }
@@ -193,6 +194,10 @@
 #endif
 }
 
+void PLItem::setPrevPlayed(bool b) {
+        setData(b, Role_PrevPlayed);
+}
+
 QString PLItem::filename() {
    return col_filename->data().toString();
 }
@@ -217,6 +222,10 @@
    return data(Role_Current).toBool();
 }
 
+bool PLItem::isPrevPlayed() {
+        return data(Role_PrevPlayed).toBool();
+}
+
 QList<QStandardItem *> PLItem::items() {
    QList<QStandardItem *> l;
    l << col_num << this << col_duration << col_filename;
@@ -752,6 +761,16 @@
    return -1;
 }
 
+int Playlist::findPrevPlayed() {
+   int rc = proxy->rowCount();
+        for (int i = 0; i < rc; i++) {
+           if (itemFromProxy(i)->isPrevPlayed()) {
+                                return i;
+                }
+       }
+       return -1;
+}
+
 void Playlist::clear() {
    table->setRowCount(0);
    setCurrentItem(0);
@@ -1358,6 +1377,14 @@
 
    QString filename = itemFromProxy(n)->filename();
    if (!filename.isEmpty()) {
+      int prev = findPrevPlayed();
+      if (prev > -1) {
+         itemFromProxy(prev)->setPrevPlayed(false);
+      }
+      int current = findCurrentItem();
+      if (current > -1) {
+         itemFromProxy(current)->setPrevPlayed(true);
+      }
       setCurrentItem(n);
       if (play_files_from_start) {
          emit requestToPlayFile(filename, 0);
@@ -1397,12 +1424,21 @@
 
 void Playlist::playPrev() {
    qDebug("Playlist::playPrev");
-   int current = findCurrentItem() - 1;
-   if (current >= 0) {
-      playItem(current);
-   } else {
-      if (proxy->rowCount() > 1) playItem(proxy->rowCount() - 1);
-   }
+   int current = findCurrentItem();
+        if (shuffleAct->isChecked()) {
+      int prev = findPrevPlayed();
+                if (prev > -1) {
+                      itemFromProxy(prev)->setPrevPlayed(false);
+                      playItem(prev);
+                }
+        } else {
+                current--;
+           if (current >= 0) {
+            playItem(current);
+           } else {
+            if (proxy->rowCount() > 1) playItem(proxy->rowCount() - 1);
+           }
+      }
 }
 
 void Playlist::playNextAuto() {


Code: Select all
--- playlist.h   2016-09-29 00:49:28.000000000 +0200
+++ playlist.h.new   2016-11-29 19:34:18.000000000 +0100
@@ -32,7 +32,7 @@
 
 class PLItem : public QStandardItem {
 public:
-    enum PLItem_Roles { Role_Played = Qt::UserRole + 2, Role_Current = Qt::UserRole + 3 };
+    enum PLItem_Roles { Role_Played = Qt::UserRole + 2, Role_Current = Qt::UserRole + 3, Role_PrevPlayed = Qt::UserRole + 4 };
 
    PLItem();
    PLItem(const QString filename, const QString name, double duration);
@@ -44,6 +44,7 @@
    void setPlayed(bool played);
    void setPosition(int position);
    void setCurrent(bool b);
+        void setPrevPlayed(bool b);
 
    QString filename();
    QString name();
@@ -51,6 +52,7 @@
    bool played();
    int position();
    bool isCurrent();
+        bool isPrevPlayed();
 
    QList<QStandardItem *> items();
 
@@ -217,6 +219,7 @@
    int findCurrentItem();
    void clearPlayedTag();
    int chooseRandomItem();
+   int findPrevPlayed();
    QString lastDir();
 
    void setPlaylistFilename(const QString &);

---
Live long and prosper.
capt.d.
capt.d.
 
Posts: 9
Joined: Sun Dec 08, 2013 7:02 pm

Return to Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron