aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/General
diff options
context:
space:
mode:
authortrialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38>2021-01-02 18:30:38 +0000
committertrialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38>2021-01-02 18:30:38 +0000
commit7596bcb971e27330e089a7067b024e476b2ce15e (patch)
tree5cf9ad0c2b43a1515e65dbc97742aa5c00dac873 /src/plugins/General
parentbc85b9be80c7705d646a61667f9a3f4b3a8fd4fc (diff)
downloadqmmp-7596bcb971e27330e089a7067b024e476b2ce15e.tar.gz
qmmp-7596bcb971e27330e089a7067b024e476b2ce15e.tar.bz2
qmmp-7596bcb971e27330e089a7067b024e476b2ce15e.zip
library: added tree model
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@9613 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src/plugins/General')
-rw-r--r--src/plugins/General/library/library.cpp3
-rw-r--r--src/plugins/General/library/librarymodel.cpp181
-rw-r--r--src/plugins/General/library/librarymodel.h12
-rw-r--r--src/plugins/General/library/librarywidget.cpp6
-rw-r--r--src/plugins/General/library/librarywidget.h3
-rw-r--r--src/plugins/General/library/librarywidget.ui6
6 files changed, 206 insertions, 5 deletions
diff --git a/src/plugins/General/library/library.cpp b/src/plugins/General/library/library.cpp
index cf229324b..7d9c92365 100644
--- a/src/plugins/General/library/library.cpp
+++ b/src/plugins/General/library/library.cpp
@@ -35,13 +35,14 @@
#include <qmmp/qmmp.h>
#include <qmmp/metadatamanager.h>
#include <qmmpui/uihelper.h>
-//#include "historywindow.h"
+#include "librarymodel.h"
#include "library.h"
#define CONNECTION_NAME "qmmp_library"
Library::Library(QObject *parent) : QObject(parent)
{
+ qDebug() << Q_FUNC_INFO;
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME);
if(db.isValid() && !db.isOpen())
diff --git a/src/plugins/General/library/librarymodel.cpp b/src/plugins/General/library/librarymodel.cpp
index 85221b55e..b24e64e6a 100644
--- a/src/plugins/General/library/librarymodel.cpp
+++ b/src/plugins/General/library/librarymodel.cpp
@@ -17,26 +17,205 @@
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
+
+#include <QSqlDatabase>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QtDebug>
+#include <qmmp/qmmp.h>
#include "librarymodel.h"
+class LibraryTreeItem
+{
+public:
+ LibraryTreeItem() {}
+ ~LibraryTreeItem()
+ {
+ clear();
+ }
+
+ void clear()
+ {
+ name.clear();
+ type = Qmmp::UNKNOWN;
+ parent = nullptr;
+ qDeleteAll(children);
+ children.clear();
+ }
+
+ QString name;
+ Qmmp::MetaData type = Qmmp::UNKNOWN;
+ QList<LibraryTreeItem *> children;
+ LibraryTreeItem *parent = nullptr;
+};
+
LibraryModel::LibraryModel(QObject *parent) : QAbstractItemModel(parent)
{
+ m_rootItem = new LibraryTreeItem;
+ refresh();
+}
+
+LibraryModel::~LibraryModel()
+{
+ delete m_rootItem;
+}
+
+bool LibraryModel::canFetchMore(const QModelIndex &parent) const
+{
+ if(!parent.isValid())
+ return false;
+
+ LibraryTreeItem *parentItem = static_cast<LibraryTreeItem *>(parent.internalPointer());
+ if(parentItem == m_rootItem || parentItem->type == Qmmp::TITLE)
+ return false;
+ else
+ return parentItem->children.isEmpty();
+}
+
+void LibraryModel::fetchMore(const QModelIndex &parent)
+{
+ if(!parent.isValid())
+ return;
+
+ LibraryTreeItem *parentItem = static_cast<LibraryTreeItem *>(parent.internalPointer());
+
+ QSqlDatabase db = QSqlDatabase::database("qmmp_library_1");
+ if(!db.isOpen())
+ return;
+
+ if(parentItem->type == Qmmp::ARTIST)
+ {
+ QSqlQuery query(db);
+ query.prepare("SELECT DISTINCT Album from track_library WHERE Artist = :artist");
+ query.bindValue(":artist", parentItem->name);
+ bool ok = query.exec();
+
+ if(!ok)
+ {
+ qWarning("Library: exec error: %s", qPrintable(query.lastError().text()));
+ return;
+ }
+
+ while(query.next())
+ {
+ LibraryTreeItem *item = new LibraryTreeItem;
+ item->name = query.value("Album").toString();
+ item->type = Qmmp::ALBUM;
+ item->parent = parentItem;
+ parentItem->children << item;
+ qDebug() << parentItem->name << item->name;
+ }
+ }
+ else if(parentItem->type == Qmmp::ALBUM)
+ {
+ QSqlQuery query(db);
+ query.prepare("SELECT Title from track_library WHERE Artist = :artist AND Album = :album");
+ query.bindValue(":artist", parentItem->parent->name);
+ query.bindValue(":album", parentItem->name);
+ bool ok = query.exec();
+
+ if(!ok)
+ {
+ qWarning("Library: exec error: %s", qPrintable(query.lastError().text()));
+ return;
+ }
+
+ while(query.next())
+ {
+ LibraryTreeItem *item = new LibraryTreeItem;
+ item->name = query.value("Title").toString();
+ item->type = Qmmp::TITLE;
+ item->parent = parentItem;
+ parentItem->children << item;
+ }
+
+ qDebug() << parentItem->children.count();
+ }
+}
+
+QVariant LibraryModel::data(const QModelIndex &index, int role) const
+{
+ if(!index.isValid() || role != Qt::DisplayRole)
+ return QVariant();
+
+ QString name = static_cast<LibraryTreeItem *>(index.internalPointer())->name;
+ return name.isEmpty() ? tr("Unknown") : name;
+}
+
+QModelIndex LibraryModel::parent(const QModelIndex &child) const
+{
+ if(!child.isValid())
+ return QModelIndex();
+
+ LibraryTreeItem *childItem = static_cast<LibraryTreeItem *>(child.internalPointer());
+ LibraryTreeItem *parentItem = childItem->parent;
+
+ if(parentItem == m_rootItem || !parentItem || !parentItem->parent)
+ return QModelIndex();
+ return createIndex(parentItem->parent->children.indexOf(parentItem), 0, parentItem);
+}
+
+QModelIndex LibraryModel::index(int row, int column, const QModelIndex &parent) const
+{
+ if(parent.isValid() && parent.column() != 0)
+ return QModelIndex();
+
+ LibraryTreeItem *parentItem = parent.isValid() ? static_cast<LibraryTreeItem *>(parent.internalPointer()) :
+ m_rootItem;
+
+ if(row >= 0 && row < parentItem->children.count())
+ return createIndex(row, column, parentItem->children.at(row));
+ else
+ return QModelIndex();
}
int LibraryModel::columnCount(const QModelIndex &parent) const
{
+ Q_UNUSED(parent);
return 1;
}
int LibraryModel::rowCount(const QModelIndex &parent) const
{
- return m_artists.count();
+ if(!parent.isValid())
+ return m_rootItem->children.count();
+
+ LibraryTreeItem *parentItem = static_cast<LibraryTreeItem *>(parent.internalPointer());
+ if(parentItem->type == Qmmp::TITLE)
+ return 0;
+ else
+ return qMax(1, parentItem->children.count());
}
void LibraryModel::refresh()
{
beginResetModel();
+ m_rootItem->clear();
+
+ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "qmmp_library_1");
+ db.setDatabaseName(Qmmp::configDir() + "/" + "library.sqlite");
+ db.open();
+
+ if(!db.isOpen())
+ {
+ endResetModel();
+ return;
+ }
+
+ QSqlQuery query(db);
+ bool ok = query.exec("SELECT DISTINCT Artist from track_library");
+
+ if(!ok)
+ qWarning("Library: exec error: %s", qPrintable(query.lastError().text()));
+ while(query.next())
+ {
+ LibraryTreeItem *item = new LibraryTreeItem;
+ item->name = query.value("Artist").toString();
+ item->type = Qmmp::ARTIST;
+ item->parent = m_rootItem;
+ m_rootItem->children << item;
+ }
endResetModel();
}
diff --git a/src/plugins/General/library/librarymodel.h b/src/plugins/General/library/librarymodel.h
index 492b61e56..a13081bdd 100644
--- a/src/plugins/General/library/librarymodel.h
+++ b/src/plugins/General/library/librarymodel.h
@@ -25,19 +25,29 @@
#include <QStringList>
#include <QAbstractItemModel>
+class QSqlDatabase;
+class LibraryTreeItem;
+
class LibraryModel : public QAbstractItemModel
{
Q_OBJECT
public:
LibraryModel(QObject *parent = nullptr);
+ ~LibraryModel();
+ bool canFetchMore(const QModelIndex &parent) const override;
+ void fetchMore(const QModelIndex &parent) override;
+ QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+ QModelIndex parent(const QModelIndex &child) const override;
+ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
void refresh();
private:
- QStringList m_artists;
+ LibraryTreeItem *m_rootItem;
+
};
#endif // LIBRARYMODEL_H
diff --git a/src/plugins/General/library/librarywidget.cpp b/src/plugins/General/library/librarywidget.cpp
index 8ebe1f06f..fae3ea218 100644
--- a/src/plugins/General/library/librarywidget.cpp
+++ b/src/plugins/General/library/librarywidget.cpp
@@ -18,14 +18,18 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
-#include "librarywidget.h"
+#include "librarymodel.h"
#include "ui_librarywidget.h"
+#include "librarywidget.h"
+
LibraryWidget::LibraryWidget(bool dialog, QWidget *parent) :
QWidget(parent),
m_ui(new Ui::LibraryWidget)
{
m_ui->setupUi(this);
+ m_model = new LibraryModel(this);
+ m_ui->treeView->setModel(m_model);
if(dialog)
{
diff --git a/src/plugins/General/library/librarywidget.h b/src/plugins/General/library/librarywidget.h
index 24e635eae..e67150015 100644
--- a/src/plugins/General/library/librarywidget.h
+++ b/src/plugins/General/library/librarywidget.h
@@ -27,6 +27,8 @@ namespace Ui {
class LibraryWidget;
}
+class LibraryModel;
+
class LibraryWidget : public QWidget
{
Q_OBJECT
@@ -36,6 +38,7 @@ public:
private:
Ui::LibraryWidget *m_ui;
+ LibraryModel *m_model;
};
#endif // LIBRARYWIDGET_H
diff --git a/src/plugins/General/library/librarywidget.ui b/src/plugins/General/library/librarywidget.ui
index c4b42f9b6..830180bb5 100644
--- a/src/plugins/General/library/librarywidget.ui
+++ b/src/plugins/General/library/librarywidget.ui
@@ -24,7 +24,11 @@
<number>6</number>
</property>
<item>
- <widget class="QTreeView" name="treeView"/>
+ <widget class="QTreeView" name="treeView">
+ <attribute name="headerVisible">
+ <bool>false</bool>
+ </attribute>
+ </widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">