From 02c64bf99b0bd908933d50ed1eeb7321887022f7 Mon Sep 17 00:00:00 2001 From: Tobias Rundstrom Date: Sat, 19 Aug 2006 14:50:55 -0400 Subject: [PATCH] Added MedialibModels but still are disabled because they don't work. --- MedialibItem.h | 62 ++++++++++++++++++++ MedialibTreeModel.cpp | 129 ++++++++++++++++++++++++++++++++++++++---- MedialibTreeModel.h | 19 +++++-- MedialibView.cpp | 4 +- MedialibWindow.cpp | 4 +- TitleBar.cpp | 2 + XMMSHandler.cpp | 15 ++++- XMMSHandler.h | 4 +- promoe.pro | 14 +---- 9 files changed, 218 insertions(+), 35 deletions(-) create mode 100644 MedialibItem.h diff --git a/MedialibItem.h b/MedialibItem.h new file mode 100644 index 0000000..8aa0731 --- /dev/null +++ b/MedialibItem.h @@ -0,0 +1,62 @@ +#ifndef __MEDIALIBITEM_H__ +#define __MEDIALIBITEM_H__ + +#include + +class MedialibItem { + public: + enum MedialibItemType { + NoneItem, + ArtistItem, + AlbumItem, + SongItem + }; + + MedialibItem (const QStringList &text = QStringList (), MedialibItem *parent = NULL, MedialibItemType type = NoneItem) { + m_text = text; + m_type = type; + m_parent = parent; + if (parent) { + parent->m_childs.append (this); + } + }; + + bool mayHaveChilds () const { + if (m_type == NoneItem || m_type == SongItem) + return false; + return true; + }; + + QString data (int c) const { + if (m_text.size () > c) { + return m_text.at (c); + } + return QString (); + }; + + MedialibItem *child (int c) const { + return m_childs.value (c); + } + + int childCount () const { + return m_childs.count (); + }; + + MedialibItem *parent () const { + return m_parent; + }; + + int row () const { + if (m_parent) + return m_parent->m_childs.indexOf (const_cast (this)); + return 0; + } + + private: + MedialibItemType m_type; + QStringList m_text; + MedialibItem *m_parent; + QList m_childs; +}; + +#endif diff --git a/MedialibTreeModel.cpp b/MedialibTreeModel.cpp index 4c88770..210a7a7 100644 --- a/MedialibTreeModel.cpp +++ b/MedialibTreeModel.cpp @@ -1,33 +1,140 @@ -#include "XMMSSocket.h" +#include +#include "XmmsQt4.h" #include "MedialibTreeModel.h" +#include "MedialibItem.h" -#include +#include +#include +#include -MedialibTreeModel::MedialibTreeModel (QObject *parent) : QAbstractTableModel (parent), m_socket ("PromoeMedialibTree") +MedialibTreeModel::MedialibTreeModel (QObject *parent) : + QAbstractItemModel (parent), m_client ("PromoeMedialibTree") { - m_socket.connect (getenv ("XMMS_PATH")); + const char *path = getenv("XMMS_PATH"); + try { + m_client.connect (path ? path : ""); + } + catch (Xmms::connection_error& e) { + QErrorMessage *err = new QErrorMessage (); + err->showMessage ("Couldn't connect to XMMS2, please try again."); + err->exec (); + delete err; + } + + m_client.setMainloop (new XmmsQT4 (m_client.getConnection ())); + + m_rootitem = new MedialibItem (); + + using Xmms::bind; + + m_client.medialib.select ("select distinct value from Media where key='artist' order by lower(value)", + bind (&MedialibTreeModel::artist_list, this)); + +} + +bool +MedialibTreeModel::artist_list (const Xmms::List< Xmms::Dict > &artists) +{ + QString s; + + for (artists.first (); artists.isValid (); ++artists) { + s = QString::fromUtf8 ((*artists).get ("value").c_str ()); + new MedialibItem (QStringList (s), m_rootitem, MedialibItem::ArtistItem); + } + + reset (); + + return true; } /* QModel overrides */ + +Qt::ItemFlags +MedialibTreeModel::flags (const QModelIndex &index) const +{ + if (!index.isValid()) + return Qt::ItemIsEnabled; + + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} + int MedialibTreeModel::rowCount (const QModelIndex &parent) const { - return 2; + MedialibItem *item; + + if (!parent.isValid ()) { + item = m_rootitem; + } else { + item = static_cast (parent.internalPointer ()); + } + + int n = item->childCount (); + + if (n == 0 && item->mayHaveChilds ()) + n = 1; + + return n; +} + +QModelIndex +MedialibTreeModel::parent (const QModelIndex &index) const +{ + if (!index.isValid ()) + return QModelIndex (); + + MedialibItem *item = static_cast (index.internalPointer ()); + MedialibItem *parent = item->parent (); + + if (parent == m_rootitem) { + return QModelIndex (); + } + + return createIndex (parent->row (), 0, parent); +} + +QModelIndex +MedialibTreeModel::index (int row, int column, + const QModelIndex &parent) const +{ + MedialibItem *p; + if (!parent.isValid ()) { + p = m_rootitem; + } else { + p = static_cast (parent.internalPointer ()); + } + + MedialibItem *child = p->child (row); + if (child) { + return createIndex (row, column, child); + } else { + if (p->columnCount () == 0 && !p->loading ()) { + qDebug ("Loading items..."); + switch (p->type ()) { + case MedialibItem::ArtistItem: + m_client.medialib.select ("select distinct m1.value as value from Media m1 join Media m2 on m1.id = m2.id and m2.key = 'artist' where m1.key='album' and m2.value = '%s' order by lower(m1.value)", MedialibTreeModel::sub_list); + + return QModelIndex (); } int MedialibTreeModel::columnCount (const QModelIndex &parent) const { - return 2; + return 1; } QVariant -MedialibTreeModel::data (const QModelIndex &parent, +MedialibTreeModel::data (const QModelIndex &index, int role) const { - if (role == Qt::DisplayRole) - return QVariant("hej"); - return QVariant (); + if (!index.isValid ()) + return QVariant (); + + if (role != Qt::DisplayRole) + return QVariant (); + + MedialibItem *item = static_cast (index.internalPointer ()); + return QVariant (item->data (index.column ())); } QVariant @@ -36,7 +143,7 @@ MedialibTreeModel::headerData (int section, int role) const { if (role == Qt::DisplayRole) - return QVariant("header"); + return QVariant("Title"); return QVariant (); } diff --git a/MedialibTreeModel.h b/MedialibTreeModel.h index 4b9087d..56e58f6 100644 --- a/MedialibTreeModel.h +++ b/MedialibTreeModel.h @@ -1,10 +1,14 @@ #ifndef __MEDIALIBTREEMODEL_H__ #define __MEDIALIBTREEMODEL_H__ -#include "XMMSSocket.h" -#include +#include -class MedialibTreeModel : public QAbstractTableModel +#include "MedialibItem.h" + +#include +#include + +class MedialibTreeModel : public QAbstractItemModel { Q_OBJECT public: @@ -14,11 +18,16 @@ class MedialibTreeModel : public QAbstractTableModel /* QModel overrides */ int rowCount (const QModelIndex &parent) const; int columnCount (const QModelIndex &parent) const; - QVariant data (const QModelIndex &parent, int role = Qt::DisplayRole) const; + QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + Qt::ItemFlags flags (const QModelIndex &index) const; + QModelIndex index (int row, int column, const QModelIndex &parent) const; + QModelIndex parent (const QModelIndex &index) const; private: - XMMSSocket m_socket; + bool artist_list (const Xmms::List< Xmms::Dict > &artists); + Xmms::Client m_client; + MedialibItem *m_rootitem; }; #endif diff --git a/MedialibView.cpp b/MedialibView.cpp index 5172385..58215b7 100644 --- a/MedialibView.cpp +++ b/MedialibView.cpp @@ -1,8 +1,10 @@ +#include "MedialibTreeModel.h" #include "MedialibView.h" - #include MedialibView::MedialibView (QWidget *parent) : QTreeView (parent) { + setAlternatingRowColors (true); + setModel (new MedialibTreeModel (this)); } diff --git a/MedialibWindow.cpp b/MedialibWindow.cpp index c484f1f..bc9fadf 100644 --- a/MedialibWindow.cpp +++ b/MedialibWindow.cpp @@ -1,7 +1,6 @@ #include "XMMSSocket.h" #include "MedialibWindow.h" #include "MedialibView.h" -#include "MedialibTreeModel.h" #include @@ -15,7 +14,8 @@ MedialibWindow::MedialibWindow (QWidget *parent) : QMainWindow (parent) setAttribute (Qt::WA_DeleteOnClose); m_view = new MedialibView (this); - m_view->setModel (new MedialibTreeModel (this)); setCentralWidget (m_view); + + resize (500, 600); } diff --git a/TitleBar.cpp b/TitleBar.cpp index b3b00a1..fba1e1c 100644 --- a/TitleBar.cpp +++ b/TitleBar.cpp @@ -70,8 +70,10 @@ TitleBar::showMenu (void) void TitleBar::showMlib () { + /* MedialibWindow *mw = new MedialibWindow (window ()); mw->show (); + */ } void diff --git a/XMMSHandler.cpp b/XMMSHandler.cpp index 2c313a0..adf84d3 100644 --- a/XMMSHandler.cpp +++ b/XMMSHandler.cpp @@ -1,7 +1,6 @@ #include #include "XmmsQT4.h" -#include "XMMSSocket.h" #include "XMMSHandler.h" #include @@ -30,7 +29,7 @@ XMMSHandler &XMMSHandler::getInstance () return singleton; } -XMMSHandler::XMMSHandler () : QObject (), XMMSSocket () +XMMSHandler::XMMSHandler () : QObject (), m_client ("Prome_Main") { connect (std::getenv ( "XMMS_PATH" )); } @@ -38,8 +37,18 @@ XMMSHandler::XMMSHandler () : QObject (), XMMSSocket () bool XMMSHandler::connect (const char *path) { - if (!XMMSSocket::connect (path)) + try { + m_client.connect (path ? path : ""); + } + catch (Xmms::connection_error& e) { + QErrorMessage *err = new QErrorMessage (); + err->showMessage ("Couldn't connect to XMMS2, please try again."); + err->exec (); + delete err; return false; + } + + m_client.setMainloop (new XmmsQT4 (m_client.getConnection ())); using Xmms::bind; m_client.playlist.list (bind (&XMMSHandler::playlist_list, this)); diff --git a/XMMSHandler.h b/XMMSHandler.h index b33052e..8b747e2 100644 --- a/XMMSHandler.h +++ b/XMMSHandler.h @@ -4,13 +4,12 @@ #include #include "XmmsQT4.h" -#include "XMMSSocket.h" #include #include #include -class XMMSHandler : public QObject, XMMSSocket { +class XMMSHandler : public QObject { Q_OBJECT public: static XMMSHandler &getInstance (); @@ -91,6 +90,7 @@ class XMMSHandler : public QObject, XMMSSocket { bool volume_get (const Xmms::Dict &levels); bool volume_error (const std::string &error); + Xmms::Client m_client; XmmsQT4 *m_qt4; unsigned int m_currentid; bool m_masterchan; diff --git a/promoe.pro b/promoe.pro index 629eba7..23a8962 100644 --- a/promoe.pro +++ b/promoe.pro @@ -10,7 +10,6 @@ SOURCES += XmmsQT4.cpp \ NumberDisplay.cpp \ TimeDisplay.cpp \ XMMSHandler.cpp \ - XMMSSocket.cpp \ SmallNumberDisplay.cpp \ StereoMono.cpp \ Slider.cpp \ @@ -26,10 +25,7 @@ SOURCES += XmmsQT4.cpp \ VolumeSlider.cpp \ ClutterBar.cpp \ Equalizer.cpp \ - FileDialog.cpp \ - MedialibView.cpp \ - MedialibWindow.cpp \ - MedialibTreeModel.cpp + FileDialog.cpp HEADERS += XmmsQT4.h \ @@ -44,7 +40,6 @@ HEADERS += XmmsQT4.h \ NumberDisplay.h \ TimeDisplay.h \ XMMSHandler.h \ - XMMSSocket.h \ SmallNumberDisplay.h \ StereoMono.h \ Slider.h \ @@ -60,10 +55,7 @@ HEADERS += XmmsQT4.h \ VolumeSlider.h \ ClutterBar.h \ Equalizer.h \ - FileDialog.h \ - MedialibView.h \ - MedialibWindow.h \ - MedialibTreeModel.h + FileDialog.h @@ -77,7 +69,7 @@ QMAKE_LFLAGS += -L$$[QT_INSTALL_PLUGINS]/imageformats CONFIG += link_pkgconfig -;QMAKE_CXXFLAGS += -g +QMAKE_CXXFLAGS += -g ;CONFIG += debug warn_on QMAKE_CXXFLAGS_WARN_ON += -Wno-unused-parameter PKGCONFIG += xmms2-client xmms2-client-cpp