From ad11f78182aea82095f1b341b0c442cae9cbb3b2 Mon Sep 17 00:00:00 2001
From: adamsjensen <adamsjensen@yandex.by>
Date: Tue, 6 Mar 2018 13:28:42 +0300
Subject: [PATCH] added select options into playlistmodel

---
 SoundBand/playlistmodel.cpp | 55 +++++++++++++++++++++++++++++++++++++
 SoundBand/playlistmodel.h   | 27 ++++++++++++++++++
 sync/song.cpp               |  1 +
 sync/song.h                 |  9 ++++++
 4 files changed, 92 insertions(+)

diff --git a/SoundBand/playlistmodel.cpp b/SoundBand/playlistmodel.cpp
index 9dc777c..335a820 100644
--- a/SoundBand/playlistmodel.cpp
+++ b/SoundBand/playlistmodel.cpp
@@ -87,3 +87,58 @@ void PlayListModel::setNewPlayList(const QString &playList){
     playListName = playList;
     onPlayListChanged();
 }
+
+bool PlayListModel::select(int id){
+    if(!playList)
+        return false;
+
+    for(QList<syncLib::SongHeader>::Iterator i = 0; i < playList->end(); i++){
+        if(i->id == id){
+           return i->isSelected = true;
+        }
+    }
+
+    return false;
+}
+
+bool PlayListModel::unSelect(int id){
+    if(!playList)
+        return false;
+
+    for(QList<syncLib::SongHeader>::Iterator i = 0; i < playList->end(); i++){
+        if(i->id == id){
+            i->isSelected = false;
+            return true;
+        }
+    }
+
+    return false;
+}
+
+QList<int> PlayListModel::getSelected(){
+    QList<int> result;
+
+    if(!playList)
+        return result;
+
+    for(QList<syncLib::SongHeader>::Iterator i = 0; i < playList->end(); i++){
+        if(i->isSelected){
+            result.push_back(i->id);
+        }
+    }
+    return result;
+}
+
+bool PlayListModel::isSelected(int id){
+    if(!playList)
+        return false;
+
+    for(QList<syncLib::SongHeader>::Iterator i = 0; i < playList->end(); i++){
+        if(i->id == id){
+            return i->isSelected;
+        }
+    }
+
+    return false;
+}
+
diff --git a/SoundBand/playlistmodel.h b/SoundBand/playlistmodel.h
index 900e932..f56a825 100644
--- a/SoundBand/playlistmodel.h
+++ b/SoundBand/playlistmodel.h
@@ -79,6 +79,33 @@ public slots:
      * @param playList - name of playlist
      */
     void setNewPlayList(const QString &playList);
+
+    /**
+     * @brief select a song from playList;
+     * @param id - if of song
+     * @return true if all done
+     */
+    bool select(int id);
+
+    /**
+     * @brief unselect a song from playList;
+     * @param id - if of song
+     * @return true if all done
+     */
+    bool unSelect(int id);
+
+    /**
+     * @brief getSelected
+     * @return list of selected songs
+     */
+    QList<int> getSelected();
+
+    /**
+     * @brief isSelected
+     * @param id - id of checked song
+     * @return true if song selected
+     */
+    bool isSelected(int id);
 };
 
 #endif // PLAYLISTMODEL_H
diff --git a/sync/song.cpp b/sync/song.cpp
index d447a93..ed62b58 100644
--- a/sync/song.cpp
+++ b/sync/song.cpp
@@ -5,6 +5,7 @@ namespace syncLib{
 static const QStringList ValidSongs = {".mp3", ".wav", ".ogg"};
 SongHeader::SongHeader()
 {
+    this->isSelected = false;
     this->id = -1;
     this->name = "";
     this->size = 0;
diff --git a/sync/song.h b/sync/song.h
index 5230be2..eab15b6 100644
--- a/sync/song.h
+++ b/sync/song.h
@@ -26,6 +26,7 @@ struct Syncer
 class SongHeader{
 
 public:
+    bool isSelected;
     int id;
     QString name;
     int size;
@@ -36,6 +37,14 @@ public:
     bool isNameValid() const;
     virtual bool isValid() const;
     virtual ~SongHeader();
+
+    /**
+     * serialize data:
+     * id,
+     * size,
+     * and,
+     * name
+     */
     friend QDataStream& operator << (QDataStream& stream, const SongHeader& song);
     friend QDataStream& operator >> (QDataStream& stream, SongHeader& song);
 };