Commit 127b53ef authored by Camilo higuita's avatar Camilo higuita

now playlists list modeling almost ready and more track modeling actions done

parent 6bd86ad4
......@@ -319,7 +319,6 @@ void Babe::notify(const QString &title, const QString &body)
Q_UNUSED(title);
Q_UNUSED(body);
#endif
}
void Babe::notifySong(const QString &url)
......
......@@ -862,7 +862,7 @@ int CollectionDB::getTrackStars(const QString &path)
// return tags;
//}
QStringList CollectionDB::getPlaylists()
QStringList CollectionDB::getPlaylistsList()
{
QStringList playlists;
auto queryTxt = QString("SELECT %1, %2 FROM %3 ORDER BY %2 DESC").arg(FMH::MODEL_NAME[FMH::MODEL_KEY::PLAYLIST],
......@@ -875,6 +875,16 @@ QStringList CollectionDB::getPlaylists()
return playlists;
}
FMH::MODEL_LIST CollectionDB::getPlaylists()
{
auto queryTxt = QString("SELECT %1, %2 FROM %3 ORDER BY %2 DESC").arg(FMH::MODEL_NAME[FMH::MODEL_KEY::PLAYLIST],
FMH::MODEL_NAME[FMH::MODEL_KEY::ADDDATE],
TABLEMAP[TABLE::PLAYLISTS]);
return this->getDBData(queryTxt);
}
bool CollectionDB::removeTrack(const QString &path)
{
auto queryTxt = QString("DELETE FROM %1 WHERE %2 = \"%3\"").arg(TABLEMAP[TABLE::TRACKS],
......
......@@ -93,7 +93,8 @@ class CollectionDB : public QObject
// QStringList getAlbumTags(const QString &album, const QString &artist);
QStringList getArtistAlbums(const QString &artist);
Q_INVOKABLE QStringList getPlaylists();
FMH::MODEL_LIST getPlaylists();
QStringList getPlaylistsList();
Q_INVOKABLE bool removePlaylistTrack(const QString &url, const QString &playlist);
Q_INVOKABLE bool removePlaylist(const QString &playlist);
......@@ -109,7 +110,7 @@ class CollectionDB : public QObject
sourceTypes sourceType(const QString &url);
void openDB(const QString &name);
private:
private:
static CollectionDB* instance;
QString name;
......
......@@ -33,6 +33,7 @@
#include "services/local/player.h"
#include "models/tracks/tracksmodel.h"
#include "models/playlists/playlistsmodel.h"
#include "models/baselist.h"
#include "models/basemodel.h"
......@@ -105,6 +106,7 @@ int main(int argc, char *argv[])
qmlRegisterType<BaseModel>("BaseModel", 1, 0, "BaseModel");
qmlRegisterType<TracksModel>("TracksList", 1, 0, "Tracks");
qmlRegisterType<PlaylistsModel>("PlaylistsList", 1, 0, "Playlists");
qmlRegisterType<Player>("Player", 1, 0, "Player");
......
......@@ -24,6 +24,9 @@ protected:
signals:
void preItemAppended();
void postItemAppended();
void preItemAppendedAt(int index);
void preItemRemoved(int index);
void postItemRemoved();
void updateModel(int index, QVector<int> roles);
......
......@@ -78,6 +78,11 @@ void BaseModel::setList(BaseList *value)
endInsertRows();
});
connect(this->mList, &BaseList::preItemAppendedAt, this, [=](int index)
{
beginInsertRows(QModelIndex(), index, index);
});
connect(this->mList, &BaseList::preItemRemoved, this, [=](int index)
{
beginRemoveRows(QModelIndex(), index, index);
......
#include "playlistsmodel.h"
#include "db/collectionDB.h"
PlaylistsModel::PlaylistsModel(QObject *parent) : BaseList(parent)
{
this->db = CollectionDB::getInstance();
this->setList();
}
FMH::MODEL_LIST PlaylistsModel::items() const
{
return this->list;
}
void PlaylistsModel::setSortBy(const SORTBY &sort)
{
if(this->sort == sort)
return;
this->sort = sort;
this->preListChanged();
this->sortList();
this->postListChanged();
emit this->sortByChanged();
}
PlaylistsModel::SORTBY PlaylistsModel::getSortBy() const
{
return this->sort;
}
void PlaylistsModel::sortList()
{
const auto key = static_cast<FMH::MODEL_KEY>(this->sort);
qDebug()<< "SORTING LIST BY"<< this->sort;
qSort(this->list.begin() + this->defaultPlaylists().size(), this->list.end(), [key](const FMH::MODEL &e1, const FMH::MODEL &e2) -> bool
{
auto role = key;
switch(role)
{
case FMH::MODEL_KEY::ADDDATE:
{
auto currentTime = QDateTime::currentDateTime();
auto date1 = QDateTime::fromString(e1[role], Qt::TextDate);
auto date2 = QDateTime::fromString(e2[role], Qt::TextDate);
if(date1.secsTo(currentTime) < date2.secsTo(currentTime))
return true;
break;
}
case FMH::MODEL_KEY::TITLE:
{
const auto str1 = QString(e1[role]).toLower();
const auto str2 = QString(e2[role]).toLower();
if(str1 < str2)
return true;
break;
}
default:
if(e1[role] < e2[role])
return true;
}
return false;
});
}
void PlaylistsModel::setList()
{
qDebug()<< "trying to set playlists list";
emit this->preListChanged();
this->list = this->defaultPlaylists();
// this->list << this->db->getPlaylists();
// this->sortList();
emit this->postListChanged();
}
FMH::MODEL_LIST PlaylistsModel::defaultPlaylists()
{
return FMH::MODEL_LIST {
{
{FMH::MODEL_KEY::PLAYLIST, "Most Played"},
{FMH::MODEL_KEY::ICON, "view-media-playcount"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Favorites"},
{FMH::MODEL_KEY::ICON, "view-media-favorite"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Recent"},
{FMH::MODEL_KEY::ICON, "view-media-recent"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Favs"},
{FMH::MODEL_KEY::ICON, "love"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Online"},
{FMH::MODEL_KEY::ICON, "internet-services"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Online"},
{FMH::MODEL_KEY::ICON, "internet-services"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Tags"},
{FMH::MODEL_KEY::ICON, "tag"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Relationships"},
{FMH::MODEL_KEY::ICON, "view-media-similarartists"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Popular"},
{FMH::MODEL_KEY::ICON, "view-media-chart"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
},
{
{FMH::MODEL_KEY::PLAYLIST, "Genres"},
{FMH::MODEL_KEY::ICON, "view-media-genre"},
{FMH::MODEL_KEY::ADDDATE,QDateTime::currentDateTime().toString(Qt::DateFormat::TextDate)}
}
};
}
QVariantMap PlaylistsModel::get(const int &index) const
{
if(index >= this->list.size() || index < 0)
return QVariantMap();
QVariantMap res;
const auto item = this->list.at(index);
for(auto key : item.keys())
res.insert(FMH::MODEL_NAME[key], item[key]);
return res;
}
void PlaylistsModel::append(const QVariantMap &item)
{
if(item.isEmpty())
return;
emit this->preItemAppended();
FMH::MODEL model;
for(auto key : item.keys())
model.insert(FMH::MODEL_NAME_KEY[key], item[key].toString());
this->list << model;
emit this->postItemAppended();
}
void PlaylistsModel::append(const QVariantMap &item, const int &at)
{
if(item.isEmpty())
return;
if(at > this->list.size() || at < 0)
return;
qDebug()<< "trying to append at" << at << item["title"];
emit this->preItemAppendedAt(at);
FMH::MODEL model;
for(auto key : item.keys())
model.insert(FMH::MODEL_NAME_KEY[key], item[key].toString());
this->list.insert(at, model);
emit this->postItemAppended();
}
#ifndef PLAYLISTSMODEL_H
#define PLAYLISTSMODEL_H
#include "models/baselist.h"
class CollectionDB;
class PlaylistsModel : public BaseList
{
Q_OBJECT
Q_PROPERTY(PlaylistsModel::SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
public:
enum SORTBY : uint_fast8_t
{
ADDDATE = FMH::MODEL_KEY::ADDDATE,
TITLE = FMH::MODEL_KEY::TITLE
}; Q_ENUM(SORTBY)
explicit PlaylistsModel(QObject *parent = nullptr);
FMH::MODEL_LIST items() const override;
void setSortBy(const PlaylistsModel::SORTBY &sort);
PlaylistsModel::SORTBY getSortBy() const;
private:
CollectionDB *db;
FMH::MODEL_LIST list;
void sortList();
void setList();
FMH::MODEL_LIST defaultPlaylists();
PlaylistsModel::SORTBY sort = PlaylistsModel::SORTBY::ADDDATE;
signals:
void sortByChanged();
public slots:
QVariantMap get(const int &index) const override;
void append(const QVariantMap &item);
void append(const QVariantMap &item, const int &at);
};
#endif // PLAYLISTSMODEL_H
#include "tracksmodel.h"
#include "db/collectionDB.h"
TracksModel::TracksModel(QObject *parent) : BaseList(parent)
{
......@@ -140,6 +142,27 @@ void TracksModel::append(const QVariantMap &item)
emit this->postItemAppended();
}
void TracksModel::append(const QVariantMap &item, const int &at)
{
if(item.isEmpty())
return;
if(at > this->list.size() || at < 0)
return;
qDebug()<< "trying to append at" << at << item["title"];
emit this->preItemAppendedAt(at);
FMH::MODEL model;
for(auto key : item.keys())
model.insert(FMH::MODEL_NAME_KEY[key], item[key].toString());
this->list.insert(at, model);
emit this->postItemAppended();
}
bool TracksModel::color(const int &index, const QString &color)
{
if(index >= this->list.size() || index < 0)
......@@ -148,7 +171,7 @@ bool TracksModel::color(const int &index, const QString &color)
auto item = this->list[index];
if(this->db->colorTagTrack(item[FMH::MODEL_KEY::URL], color))
{
list[index][FMH::MODEL_KEY::COLOR] = color;
this->list[index][FMH::MODEL_KEY::COLOR] = color;
emit this->updateModel(index, {FMH::MODEL_KEY::COLOR});
return true;
}
......@@ -164,7 +187,7 @@ bool TracksModel::fav(const int &index, const bool &value)
auto item = this->list[index];
if(this->db->favTrack(item[FMH::MODEL_KEY::URL], value))
{
list[index].insert(FMH::MODEL_KEY::FAV, value ? "1" : "0");
this->list[index][FMH::MODEL_KEY::FAV] = value ? "1" : "0";
emit this->updateModel(index, {FMH::MODEL_KEY::FAV});
return true;
......
#ifndef TRACKSMODEL_H
#ifndef TRACKSMODEL_H
#define TRACKSMODEL_H
#include <QObject>
#include "models/baselist.h"
#include "db/collectionDB.h"
class CollectionDB;
class TracksModel : public BaseList
{
Q_OBJECT
......@@ -44,9 +44,6 @@ private:
QString query;
TracksModel::SORTBY sort = TracksModel::SORTBY::ADDDATE;
bool addDoc(const FMH::MODEL &doc);
void refreshCollection();
signals:
void queryChanged();
void sortByChanged();
......@@ -54,6 +51,7 @@ signals:
public slots:
QVariantMap get(const int &index) const override;
void append(const QVariantMap &item);
void append(const QVariantMap &item, const int &at);
bool color(const int &index, const QString &color);
bool fav(const int &index, const bool &value);
......
......@@ -56,7 +56,7 @@ BabeSettings::BabeSettings(QObject *parent) : QObject(parent)
connect(this->babeSocket, &Socket::message, this, &BabeSettings::fetchYoutubeTrack);
connect(this->babeSocket, &Socket::connected, [this](const int &index)
{
auto playlists = this->connection->getPlaylists();
auto playlists = this->connection->getPlaylistsList();
bDebug::Instance()->msg("Sending playlists to socket: "+playlists.join(", "));
this->babeSocket->sendMessageTo(index, playlists.join(","));
});
......
......@@ -142,7 +142,7 @@ function appendTracksAt(tracks, at)
{
if(tracks)
for(var i in tracks)
mainPlaylist.list.model.insert(parseInt(at)+parseInt(i), tracks[i])
mainPlaylist.list.append(tracks[i], parseInt(at)+parseInt(i))
}
function appendTrack(track)
......
......@@ -32,7 +32,7 @@ Maui.Page
function clearTable()
{
list.model.clear()
model.clear()
}
Maui.Holder
......
......@@ -208,7 +208,8 @@ BabeList
list.fav(listView.currentIndex, !(list.get(listView.currentIndex).fav == "1"))
}
onQueueClicked: H.queueIt(paths)
onQueueClicked: Player.queueTracks([list.get(listView.currentIndex)])
onSaveToClicked:
{
playlistDialog.tracks = paths
......
......@@ -66,7 +66,8 @@ SOURCES += main.cpp \
services/web/Spotify/spotify.cpp \
models/tracks/tracksmodel.cpp \
models/basemodel.cpp \
models/baselist.cpp
models/baselist.cpp \
models/playlists/playlistsmodel.cpp
RESOURCES += qml.qrc \
......@@ -104,7 +105,8 @@ HEADERS += \
services/web/Spotify/spotify.h \
models/tracks/tracksmodel.h \
models/basemodel.h \
models/baselist.h
models/baselist.h \
models/playlists/playlistsmodel.h
include(install.pri)
......
......@@ -4,6 +4,7 @@ import QtQuick.Controls 2.2
import org.kde.kirigami 2.2 as Kirigami
import org.kde.mauikit 1.0 as Maui
import "../../view_models/BabeTable"
import "../../view_models"
import "../../db/Queries.js" as Q
......@@ -17,6 +18,10 @@ Kirigami.PageRow
property string playlistQuery
property alias playlistViewModel : playlistViewModel
// property alias list : _playlistsList
// property alias listModel: _playlistsModel
// property alias listView : playlistViewModel.listView
signal rowClicked(var track)
signal quickPlayTrack(var track)
signal playAll(var tracks)
......
......@@ -3,6 +3,8 @@ import QtQuick.Layouts 1.3
import QtQuick.Controls 2.2
import org.kde.kirigami 2.2 as Kirigami
import org.kde.mauikit 1.0 as Maui
import PlaylistsList 1.0
import BaseModel 1.0
import "../../utils"
......@@ -42,22 +44,19 @@ BabeList
onClicked: removePlaylist()
}
ListModel
BaseModel
{
id: _playlistsModel
list: _playlistsList
}
Playlists
{
id: playlistListModel
ListElement { playlist: qsTr("Most Played"); icon: "view-media-playcount"; /*query: Q.Query.mostPlayedTracks*/ }
ListElement { playlist: qsTr("Favorites"); icon: "view-media-favorite"}
ListElement { playlist: qsTr("Recent"); icon: "view-media-recent"}
ListElement { playlist: qsTr("Babes"); icon: "love"}
ListElement { playlist: qsTr("Online"); icon: "internet-services"}
ListElement { playlist: qsTr("Tags"); icon: "tag"}
ListElement { playlist: qsTr("Relationships"); icon: "view-media-similarartists"}
ListElement { playlist: qsTr("Popular"); icon: "view-media-chart"}
ListElement { playlist: qsTr("Genres"); icon: "view-media-genre"}
id: _playlistsList
}
model: playlistListModel
model: _playlistsModel
delegate : Maui.ListDelegate
{
......@@ -72,7 +71,7 @@ BabeList
onClicked :
{
currentIndex = index
var playlist = playlistListModel.get(index).playlist
var playlist = list.get(index).playlist
filterList.section.property = ""
switch(playlist)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment