From c90028ae76904662e68bf381d27ba00d6183faae Mon Sep 17 00:00:00 2001 From: w1z7ard Date: Mon, 5 Nov 2007 04:57:14 +0000 Subject: [PATCH] work on playlist opening git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@663 6778bc44-b910-0410-a7a0-be141de4315d --- src/qprojectM/PresetPlaylists/sample.ppl | 8 - src/qprojectM/src/CMakeLists.txt | 6 +- src/qprojectM/src/QPlaylistModel.cpp | 220 ++++++++++++++------- src/qprojectM/src/QPlaylistModel.hpp | 6 +- src/qprojectM/src/QProjectM_MainWindow.cpp | 33 +++- src/qprojectM/src/QProjectM_MainWindow.hpp | 5 +- 6 files changed, 190 insertions(+), 88 deletions(-) diff --git a/src/qprojectM/PresetPlaylists/sample.ppl b/src/qprojectM/PresetPlaylists/sample.ppl index 34b1e63e5..8a36a7ad0 100644 --- a/src/qprojectM/PresetPlaylists/sample.ppl +++ b/src/qprojectM/PresetPlaylists/sample.ppl @@ -26,11 +26,3 @@ xsi:noNamespaceSchemaLocation="presetplaylist.xsd"> - - -/usr/share/projectM/presets/Aderrasi - Making Time (Swamp Mix).milk -/usr/share/projectM/presets/Aderrasi - Potion of Spirits.milk - -/usr/share/projectM/presets/Aderrasi - Spillswirl.milk -/usr/share/projectM/presets/Aderrasi - What cannot be.milk -/usr/share/projectM/presets/Aderrasi - What Cannot Be Undone.milk diff --git a/src/qprojectM/src/CMakeLists.txt b/src/qprojectM/src/CMakeLists.txt index ea2838b00..2ff1c438f 100644 --- a/src/qprojectM/src/CMakeLists.txt +++ b/src/qprojectM/src/CMakeLists.txt @@ -17,6 +17,7 @@ set(qprojectM_SRCS QProjectM_MainWindow.hpp ConfigFile.h ConfigFile.cpp QProjectMFileDialog.hpp + QPlaylistFileDialog.hpp QPlaylistModel.cpp QPlaylistModel.hpp ) @@ -25,6 +26,7 @@ set(qprojectM_SRCS set(qprojectM_MOC_HDRS QProjectM_MainWindow.hpp QProjectMFileDialog.hpp + QPlaylistFileDialog.hpp QPlaylistModel.hpp ) @@ -48,10 +50,10 @@ qt4_wrap_cpp(qprojectM_MOC_SRCS ${qprojectM_MOC_HDRS}) ADD_LIBRARY(qprojectM SHARED ${qprojectM_SRCS} ${qprojectM_RC_SRCS} ${qprojectM_MOC_SRCS} ${qprojectM_UIS_H}) LINK_DIRECTORIES(${PROJECTM_LDFLAGS}) -include_directories(${QT_INCLUDE_DIR} ${QT_QTGUI_INCLUDE_DIR} ${QT_QTOPENGL_INCLUDE_DIR} ${CMAKE_CURRENT_LIBRARY_DIR}) +include_directories(${QT_INCLUDE_DIR} ${QT_QTGUI_INCLUDE_DIR} ${QT_QTXML_INCLUDE_DIR} ${QT_QTOPENGL_INCLUDE_DIR} ${CMAKE_CURRENT_LIBRARY_DIR}) # link the "qprojectM" target against the Qt libraries. which libraries exactly, is defined by the "include(${QT_USE_FILE})" line above, which sets up this variable. -target_link_libraries(qprojectM projectM ${QT_QTGUI_LIBRARIES} ${QT_QTOPENGL_LIBRARIES} ${QT_LIBRARIES}) +target_link_libraries(qprojectM projectM ${QT_QTGUI_LIBRARIES} ${QT_QTOPENGL_LIBRARIES} ${QT_QTXML_LIBRARIES} ${QT_LIBRARIES}) install(FILES QProjectM_MainWindow.hpp ui_QProjectM_MainWindow.h DESTINATION include/libprojectM) install(TARGETS qprojectM DESTINATION lib) diff --git a/src/qprojectM/src/QPlaylistModel.cpp b/src/qprojectM/src/QPlaylistModel.cpp index e3d812d56..ec9b7d8b2 100644 --- a/src/qprojectM/src/QPlaylistModel.cpp +++ b/src/qprojectM/src/QPlaylistModel.cpp @@ -1,138 +1,216 @@ #include "QPlaylistModel.hpp" #include +#include +#include +#include -QPlaylistModel::QPlaylistModel(projectM & _projectM, QObject * parent): - QAbstractTableModel(parent), m_projectM(_projectM) { - m_ratings = QVector(rowCount(), 3); +QPlaylistModel::QPlaylistModel ( projectM & _projectM, QObject * parent ) : + QAbstractTableModel ( parent ), m_projectM ( _projectM ) +{ + m_ratings = QVector ( rowCount(), 3 ); } -void QPlaylistModel::updateItemHighlights() { - if (rowCount() == 0) +void QPlaylistModel::updateItemHighlights() +{ + if ( rowCount() == 0 ) return; - emit(dataChanged(this->index(0,0), this->index(rowCount()-1,columnCount()-1))) -; + emit ( dataChanged ( this->index ( 0,0 ), this->index ( rowCount()-1,columnCount()-1 ) ) ) + ; } -bool QPlaylistModel::setData(const QModelIndex & index, const QVariant & value, int role) { - if (role == QPlaylistModel::RatingRole) { +bool QPlaylistModel::setData ( const QModelIndex & index, const QVariant & value, int role ) +{ + if ( role == QPlaylistModel::RatingRole ) + { //QAbstractTableModel::setData(index, ratingToIcon(value.toInt()), Qt::DecorationRole); //std::cerr << "here" << std::endl; - m_ratings[index.row()] = value.toInt(); - emit(dataChanged(index, index)); + m_ratings[index.row() ] = value.toInt(); + emit ( dataChanged ( index, index ) ); return true; } - else - return QAbstractTableModel::setData(index, value, role); - + else + return QAbstractTableModel::setData ( index, value, role ); + } -QVariant QPlaylistModel::ratingToIcon(int rating) const { - switch (rating) { - case 0: - return QVariant(QIcon(":/images/icons/face0.png")); - case 1: - return QVariant(QIcon(":/images/icons/face1.png")); - case 2: - return QVariant(QIcon(":/images/icons/face2.png")); - case 3: - return QVariant(QIcon(":/images/icons/face3.png")); - case 4: - return QVariant(QIcon(":/images/icons/face4.png")); - case 5: - return QVariant(QIcon(":/images/icons/face5.png")); - default: - return QVariant(QIcon(":/images/icons/face0.png")); +QVariant QPlaylistModel::ratingToIcon ( int rating ) const +{ + switch ( rating ) + { + case 0: + return QVariant ( QIcon ( ":/images/icons/face0.png" ) ); + case 1: + return QVariant ( QIcon ( ":/images/icons/face1.png" ) ); + case 2: + return QVariant ( QIcon ( ":/images/icons/face2.png" ) ); + case 3: + return QVariant ( QIcon ( ":/images/icons/face3.png" ) ); + case 4: + return QVariant ( QIcon ( ":/images/icons/face4.png" ) ); + case 5: + return QVariant ( QIcon ( ":/images/icons/face5.png" ) ); + default: + return QVariant ( QIcon ( ":/images/icons/face0.png" ) ); } } -QVariant QPlaylistModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const { - - switch (role) { +QVariant QPlaylistModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const +{ + + switch ( role ) + { case Qt::DisplayRole: - if (index.column() == 0) - return QVariant(QString(m_projectM.getPresetName(index.row()).c_str())); + if ( index.column() == 0 ) + return QVariant ( QString ( m_projectM.getPresetName ( index.row() ).c_str() ) ); else - return ratingToIcon(m_ratings[index.row()]); + return ratingToIcon ( m_ratings[index.row() ] ); case Qt::ToolTip: - if (index.column() == 0) - return QVariant(QString(m_projectM.getPresetName(index.row()).c_str())); + if ( index.column() == 0 ) + return QVariant ( QString ( m_projectM.getPresetName ( index.row() ).c_str() ) ); else - return QString("Current rating is %1 / 5").arg(m_ratings[index.row()]); - case Qt::DecorationRole: - if (index.column() == 1) - return ratingToIcon(m_ratings[index.row()]); + return QString ( "Current rating is %1 / 5" ).arg ( m_ratings[index.row() ] ); + case Qt::DecorationRole: + if ( index.column() == 1 ) + return ratingToIcon ( m_ratings[index.row() ] ); else return QVariant(); case QPlaylistModel::RatingRole: - return QVariant(m_ratings[index.row()]); + return QVariant ( m_ratings[index.row() ] ); case Qt::BackgroundRole: - if (m_projectM.isPresetLocked() && (index.row() == m_projectM.selectedPresetIndex())) + if ( m_projectM.isPresetLocked() && ( index.row() == m_projectM.selectedPresetIndex() ) ) return Qt::red; - if (!m_projectM.isPresetLocked() && (index.row() == m_projectM.selectedPresetIndex())) + if ( !m_projectM.isPresetLocked() && ( index.row() == m_projectM.selectedPresetIndex() ) ) return Qt::green; - + return Qt::white; case QPlaylistModel::URLInfoRole: - return QVariant(QString(m_projectM.getPresetURL(index.row()).c_str())); + return QVariant ( QString ( m_projectM.getPresetURL ( index.row() ).c_str() ) ); default: return QVariant(); } } -QVariant QPlaylistModel::headerData ( int section, Qt::Orientation orientation, int role) const { +QVariant QPlaylistModel::headerData ( int section, Qt::Orientation orientation, int role ) const +{ - if (orientation == Qt::Vertical) - return QAbstractTableModel::headerData(section, orientation, role); + if ( orientation == Qt::Vertical ) + return QAbstractTableModel::headerData ( section, orientation, role ); - if ((section == 0) && (role == Qt::SizeHintRole)) - return QVariant(500); + if ( ( section == 0 ) && ( role == Qt::SizeHintRole ) ) + return QVariant ( 500 ); // if ((section == 1) && (role == Qt::SizeHintRole)) // return QVariant(60); - if ((section == 0) && (role == Qt::DisplayRole)) - return QString(tr("Preset")); - if ((section == 1) && (role == Qt::DisplayRole)) - return QString(tr("Rating")); - - return QAbstractTableModel::headerData(section, orientation, role); + if ( ( section == 0 ) && ( role == Qt::DisplayRole ) ) + return QString ( tr ( "Preset" ) ); + if ( ( section == 1 ) && ( role == Qt::DisplayRole ) ) + return QString ( tr ( "Rating" ) ); + + return QAbstractTableModel::headerData ( section, orientation, role ); } -int QPlaylistModel::rowCount ( const QModelIndex & parent) const { +int QPlaylistModel::rowCount ( const QModelIndex & parent ) const +{ return m_projectM.getPlaylistSize(); } -int QPlaylistModel::columnCount ( const QModelIndex & parent) const { +int QPlaylistModel::columnCount ( const QModelIndex & parent ) const +{ // eventually add ratings here so size should be 2 - if (rowCount() > 0) + if ( rowCount() > 0 ) return 2; else return 0; } -void QPlaylistModel::appendRow (const QString & presetURL, const QString & presetName, int rating) { - beginInsertRows(QModelIndex(), rowCount(), rowCount()); - m_projectM.addPresetURL(presetURL.toStdString(), presetName.toStdString()); - m_ratings.push_back(rating); +void QPlaylistModel::appendRow ( const QString & presetURL, const QString & presetName, int rating ) +{ + beginInsertRows ( QModelIndex(), rowCount(), rowCount() ); + m_projectM.addPresetURL ( presetURL.toStdString(), presetName.toStdString() ); + m_ratings.push_back ( rating ); endInsertRows(); } -void QPlaylistModel::removeRow (int index) { - beginRemoveRows(QModelIndex(), index, index); - m_projectM.removePreset(index); - m_ratings.remove(index); +void QPlaylistModel::removeRow ( int index ) +{ + beginRemoveRows ( QModelIndex(), index, index ); + m_projectM.removePreset ( index ); + m_ratings.remove ( index ); endRemoveRows(); } -void QPlaylistModel::clear() { - beginRemoveRows(QModelIndex(), 0, rowCount()-1); +void QPlaylistModel::clear() +{ + beginRemoveRows ( QModelIndex(), 0, rowCount()-1 ); m_projectM.clearPlaylist(); m_ratings.clear(); endRemoveRows(); } + +void QPlaylistModel::readPlaylist ( const QString & file ) +{ + + QXmlStreamReader reader ( file ); + + QXmlStreamReader::TokenType token; + + while ( !reader.atEnd() ) { + token = reader.readNext(); + switch ( token ) + { + case QXmlStreamReader::StartElement: + qDebug() << "start elem:" << reader.name().toString(); + if (reader.name() == "item") { + readPlaylistItem(reader); + break; + } + + case QXmlStreamReader::NoToken: + case QXmlStreamReader::Invalid: + case QXmlStreamReader::StartDocument: + case QXmlStreamReader::EndDocument: + case QXmlStreamReader::EndElement: + case QXmlStreamReader::Characters: + case QXmlStreamReader::Comment: + case QXmlStreamReader::DTD: + case QXmlStreamReader::EntityReference: + case QXmlStreamReader::ProcessingInstruction: + default: + break; + } + } + if (reader.hasError()) { + qDebug() << "error occurred: " << reader.error(); + exit(0); + } +} + +void QPlaylistModel::readPlaylistItem(QXmlStreamReader & reader) { + + + QString url; + int rating; + + while (reader.readNext() != QXmlStreamReader::EndElement) + if (reader.name() == "url") { + reader.readNext(); + url = reader.text().toString(); + reader.readNext(); + } else if (reader.name() == "rating") { + reader.readNext(); + rating = reader.text().toString().toInt(); + } + else { + /// throw a stream exception + qDebug() << (reader.name().toString()); + throw 0; + } + this->appendRow(url, QFileInfo(url).fileName(), rating); +} diff --git a/src/qprojectM/src/QPlaylistModel.hpp b/src/qprojectM/src/QPlaylistModel.hpp index 2d1464b3a..2bb576367 100644 --- a/src/qprojectM/src/QPlaylistModel.hpp +++ b/src/qprojectM/src/QPlaylistModel.hpp @@ -27,7 +27,7 @@ #include #include - +class QXmlStreamReader; class QPlaylistModel : public QAbstractTableModel { Q_OBJECT // must include this if you use Qt signals/slots @@ -50,10 +50,14 @@ QVariant data ( const QModelIndex & index, int role) const; int rowCount ( const QModelIndex & parent = QModelIndex()) const ; int columnCount ( const QModelIndex & parent= QModelIndex()) const ; +void readPlaylist(const QString & file); + public slots: void updateItemHighlights(); private: + void readPlaylistItem(QXmlStreamReader & reader); + QVariant ratingToIcon(int rating) const; projectM & m_projectM; QVector m_ratings; diff --git a/src/qprojectM/src/QProjectM_MainWindow.cpp b/src/qprojectM/src/QProjectM_MainWindow.cpp index cd07116c4..b927b63b9 100644 --- a/src/qprojectM/src/QProjectM_MainWindow.cpp +++ b/src/qprojectM/src/QProjectM_MainWindow.cpp @@ -22,7 +22,7 @@ #include #include "QProjectM_MainWindow.hpp" #include "QProjectMFileDialog.hpp" - +#include "QPlaylistFileDialog.hpp" #include #include #include @@ -30,7 +30,7 @@ #include "QPlaylistModel.hpp" QProjectM_MainWindow::QProjectM_MainWindow(const std::string & config_file) -:m_QProjectMFileDialog(new QProjectMFileDialog(this)), oldPresetIndex(-1) +:m_QProjectMFileDialog(new QProjectMFileDialog(this)), m_QPlaylistFileDialog(new QPlaylistFileDialog(this)), oldPresetIndex(-1) { ui.setupUi(this); @@ -46,6 +46,7 @@ QProjectM_MainWindow::QProjectM_MainWindow(const std::string & config_file) connect(ui.clearPresetList_PushButton, SIGNAL(pressed()), this, SLOT(clearPlaylist())); + connect(m_QProjectMWidget, SIGNAL(projectM_Initialized()), this, SLOT(postProjectM_Initialize())); @@ -210,7 +211,7 @@ void QProjectM_MainWindow::closeEvent(QCloseEvent *event) } -void QProjectM_MainWindow::open() +void QProjectM_MainWindow::addPresets() { @@ -240,6 +241,28 @@ void QProjectM_MainWindow::open() //playlistModel->setHeaderData(0, Qt::Horizontal, tr("Preset"));//, Qt::DisplayRole); } +void QProjectM_MainWindow::openPlaylist() +{ + + clearPlaylist(); + + if ( m_QPlaylistFileDialog->exec()) { + const QStringList & files = m_QPlaylistFileDialog->selectedFiles(); + + for (QStringList::const_iterator pos = files.begin(); + pos != files.end(); ++pos) { + if (*pos != "") { + //QVector items; + playlistModel->readPlaylist(*pos); + + //for (QV + //historyHash.insert(PlaylistItemMetaData( + } + } + } + //playlistModel->setHeaderData(0, Qt::Horizontal, tr("Preset"));//, Qt::DisplayRole); +} + //void QAbstractItemView::clicked ( const QModelIndex & index ) [signal] void QProjectM_MainWindow::refreshPlaylist() { @@ -298,8 +321,8 @@ void QProjectM_MainWindow::createActions() connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close())); - connect(ui.actionAddPresets, SIGNAL(triggered()), this, SLOT(open())); - + connect(ui.actionAddPresets, SIGNAL(triggered()), this, SLOT(addPresets())); +connect(ui.actionOpen_Play_List, SIGNAL(triggered()), this, SLOT(openPlaylist())); connect(ui.actionAbout_qprojectM, SIGNAL(triggered()), this, SLOT(about())); //connect(ui.actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt())); diff --git a/src/qprojectM/src/QProjectM_MainWindow.hpp b/src/qprojectM/src/QProjectM_MainWindow.hpp index 9a568b02d..70572ceaa 100644 --- a/src/qprojectM/src/QProjectM_MainWindow.hpp +++ b/src/qprojectM/src/QProjectM_MainWindow.hpp @@ -35,6 +35,7 @@ class QAction; class QMenu; class QProjectMFileDialog; +class QPlaylistFileDialog; class QPlaylistModel; #include @@ -230,7 +231,8 @@ protected: private slots: void clearPlaylist(); - void open(); + void addPresets(); + void openPlaylist(); void about(); void postProjectM_Initialize(); void updatePlaylistSelection(bool hardCut, int index); @@ -273,6 +275,7 @@ private: QString previousFilter; QProjectMFileDialog * m_QProjectMFileDialog; + QPlaylistFileDialog * m_QPlaylistFileDialog; }; #endif