From cb08c4b14bf528491ebf8c92bec0a2f59e7a8cb9 Mon Sep 17 00:00:00 2001 From: w1z7ard Date: Mon, 14 Jan 2008 01:40:05 +0000 Subject: [PATCH] added unfinished qt model to device list git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@744 6778bc44-b910-0410-a7a0-be141de4315d --- .../QPulseAudioDevChooser.cpp | 2 + .../QPulseAudioDeviceModel.cpp | 199 ++++++++++++++++++ .../QPulseAudioDeviceModel.hpp | 61 ++++++ .../QPulseAudioThread.cpp | 2 +- .../QPulseAudioThread.hpp | 3 - .../qprojectM-pulseaudio.cpp | 2 +- 6 files changed, 264 insertions(+), 5 deletions(-) create mode 100644 src/qprojectM-pulseaudio/QPulseAudioDeviceModel.cpp create mode 100644 src/qprojectM-pulseaudio/QPulseAudioDeviceModel.hpp diff --git a/src/qprojectM-pulseaudio/QPulseAudioDevChooser.cpp b/src/qprojectM-pulseaudio/QPulseAudioDevChooser.cpp index bfb36dda6..f1824597f 100644 --- a/src/qprojectM-pulseaudio/QPulseAudioDevChooser.cpp +++ b/src/qprojectM-pulseaudio/QPulseAudioDevChooser.cpp @@ -26,8 +26,10 @@ void QPulseAudioDevChooser::updateDevice(int index, const QString & name) { void QPulseAudioDevChooser::removeDevice(int index) { +this->deviceListWidget->clear(); } + void QPulseAudioDevChooser::refreshDevices (SourceContainer::const_iterator beginPos, SourceContainer::const_iterator endPos) { diff --git a/src/qprojectM-pulseaudio/QPulseAudioDeviceModel.cpp b/src/qprojectM-pulseaudio/QPulseAudioDeviceModel.cpp new file mode 100644 index 000000000..08e4380e2 --- /dev/null +++ b/src/qprojectM-pulseaudio/QPulseAudioDeviceModel.cpp @@ -0,0 +1,199 @@ +#include "QPulseAudioDeviceModel.hpp" +#include +#include +#include + + +QPulseAudioDeviceModel::QPulseAudioDeviceModel ( projectM & _projectM, QObject * parent ) : + QAbstractTableModel ( parent ), m_projectM ( _projectM ) +{ + m_ratings = QVector ( rowCount(), 3 ); +} + + +void QPulseAudioDeviceModel::updateItemHighlights() +{ + if ( rowCount() == 0 ) + return; + + emit ( dataChanged ( this->index ( 0,0 ), this->index ( rowCount()-1,columnCount()-1 ) ) ) + ; +} + +bool QPulseAudioDeviceModel::setData ( const QModelIndex & index, const QVariant & value, int role ) +{ + if ( role == QPulseAudioDeviceModel::RatingRole ) + { + //QAbstractTableModel::setData(index, ratingToIcon(value.toInt()), Qt::DecorationRole); + //std::cerr << "here" << std::endl; + m_ratings[index.row() ] = value.toInt(); + emit ( dataChanged ( index, index ) ); + return true; + } + else + return QAbstractTableModel::setData ( index, value, role ); + +} + +QVariant QPulseAudioDeviceModel::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 QPulseAudioDeviceModel::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() ) ); + else + return ratingToIcon ( m_ratings[index.row() ] ); + case Qt::ToolTip: + 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() ] ); + else + return QVariant(); + case QPulseAudioDeviceModel::RatingRole: + return QVariant ( m_ratings[index.row() ] ); + + case Qt::BackgroundRole: + + if ( m_projectM.isPresetLocked() && ( index.row() == m_projectM.selectedPresetIndex() ) ) + return Qt::red; + if ( !m_projectM.isPresetLocked() && ( index.row() == m_projectM.selectedPresetIndex() ) ) + return Qt::green; + + return Qt::white; + + case QPulseAudioDeviceModel::URLInfoRole: + return QVariant ( QString ( m_projectM.getPresetURL ( index.row() ).c_str() ) ); + default: + return QVariant(); + } +} + +QVariant QPulseAudioDeviceModel::headerData ( int section, Qt::Orientation orientation, int role ) const +{ + + if ( orientation == Qt::Vertical ) + return QAbstractTableModel::headerData ( section, orientation, role ); + + 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 ); +} + +int QPulseAudioDeviceModel::rowCount ( const QModelIndex & parent ) const +{ + return m_projectM.getPlaylistSize(); +} + + +int QPulseAudioDeviceModel::columnCount ( const QModelIndex & parent ) const +{ + + // eventually add ratings here so size should be 2 + if ( rowCount() > 0 ) + return 2; + else + return 0; +} + +void QPulseAudioDeviceModel::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 QPulseAudioDeviceModel::removeRow ( int index ) +{ + beginRemoveRows ( QModelIndex(), index, index ); + m_projectM.removePreset ( index ); + m_ratings.remove ( index ); + endRemoveRows(); +} + +void QPulseAudioDeviceModel::clear() +{ + clearItems(); + m_playlistName = ""; + m_playlistDesc = ""; +} + +void QPulseAudioDeviceModel::clearItems() +{ + beginRemoveRows ( QModelIndex(), 0, rowCount()-1 ); + m_projectM.clearPlaylist(); + m_ratings.clear(); + endRemoveRows(); +} + + +bool QPulseAudioDeviceModel::writePlaylist ( const QString & file ) { + + QFile qfile(file); + + if (!qfile.open(QIODevice::WriteOnly)) { + QMessageBox::warning (0, "Playlist Save Error", QString("There was a problem trying to save the playlist \"%1\". You may not have permission to modify this file.").arg(file)); + return false; + } + + + XmlWriteFunctor writeFunctor(*this); + + QXmlPlaylistHandler::writePlaylist(&qfile, writeFunctor); + return true; +} + +bool QPulseAudioDeviceModel::readPlaylist ( const QString & file ) +{ + + QFile qfile(file); + if (!qfile.open(QIODevice::ReadOnly)) { + QMessageBox::warning (0, "Playlist File Error", QString("There was a problem trying to open the playlist \"%1\". You may not have permission to read the file.").arg(file)); + return false; + } + + XmlReadFunctor readFunctor(*this); + + + if (QXmlPlaylistHandler::readPlaylist(&qfile, readFunctor) != QXmlStreamReader::NoError) { + QMessageBox::warning ( 0, "Playlist Parse Error", QString(tr("There was a problem trying to parse the playlist \"%1\". Some of your playlist items may have loaded correctly, but don't expect miracles.")).arg(file)); + } + + return true; +} + diff --git a/src/qprojectM-pulseaudio/QPulseAudioDeviceModel.hpp b/src/qprojectM-pulseaudio/QPulseAudioDeviceModel.hpp new file mode 100644 index 000000000..e573689b7 --- /dev/null +++ b/src/qprojectM-pulseaudio/QPulseAudioDeviceModel.hpp @@ -0,0 +1,61 @@ +/*************************************************************************** + * Copyright (C) 2007 by carm * + * carmelo.piccione@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + + +#ifndef QPULSEAUDIODEVICEMODEL_HPP +#define QPULSEAUDIODEVICEMODEL_HPP + +#include +#include "libprojectM/projectM.hpp" + +#include +#include +class QXmlStreamReader; + class QPulseAudioDeviceModel : public QAbstractTableModel + { + Q_OBJECT // must include this if you use Qt signals/slots + + public: +static const int URLInfoRole = Qt::UserRole; +static const int RatingRole = Qt::UserRole+1; + QPulseAudioDeviceModel(projectM & _projectM, QObject * parent = 0); + ~QPulseAudioDeviceModel() { } +bool setData(const QModelIndex & index, const QVariant & value, int role=Qt::EditRole); + +void appendRow (const QString & text, ); +void removeRow (int index); +QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; + +void clear(); + + +QVariant data ( const QModelIndex & index, int role) const; +int rowCount ( const QModelIndex & parent = QModelIndex()) const ; +int columnCount ( const QModelIndex & parent= QModelIndex()) const ; + +void clearItems(); + +public slots: + void updateItemHighlights(); + + private: + PulseAudioDevChooser::SourceContainer & devices; +}; +#endif diff --git a/src/qprojectM-pulseaudio/QPulseAudioThread.cpp b/src/qprojectM-pulseaudio/QPulseAudioThread.cpp index ea8f5f2f1..a4c7bc6a7 100644 --- a/src/qprojectM-pulseaudio/QPulseAudioThread.cpp +++ b/src/qprojectM-pulseaudio/QPulseAudioThread.cpp @@ -418,7 +418,7 @@ void QPulseAudioThread::pa_source_info_callback ( pa_context *c, const pa_source pulseThread->insertSource(index,name); - + qDebug() << "Added" ; } diff --git a/src/qprojectM-pulseaudio/QPulseAudioThread.hpp b/src/qprojectM-pulseaudio/QPulseAudioThread.hpp index 0d4242225..3520140c4 100644 --- a/src/qprojectM-pulseaudio/QPulseAudioThread.hpp +++ b/src/qprojectM-pulseaudio/QPulseAudioThread.hpp @@ -32,9 +32,6 @@ class QPulseAudioThread : public QThread return sourceList; } - signals: - void sourceInsertEvent(int index, const QString & name); - public slots: inline void insertSource(int index, const QString & name) { sourceList[index]= name; diff --git a/src/qprojectM-pulseaudio/qprojectM-pulseaudio.cpp b/src/qprojectM-pulseaudio/qprojectM-pulseaudio.cpp index bb9494c7c..862b2af5e 100644 --- a/src/qprojectM-pulseaudio/qprojectM-pulseaudio.cpp +++ b/src/qprojectM-pulseaudio/qprojectM-pulseaudio.cpp @@ -102,7 +102,7 @@ int main ( int argc, char*argv[] ) devChooser.setupUi(&devChooser); - QApplication::connect(&pulseAction, SIGNAL(triggered()), &devChooser, SLOT(show())); + QApplication::connect(&pulseAction, SIGNAL(triggered()), &devChooser, SLOT(open())); mainWindow->registerSettingsAction(&pulseAction); mainWindow->show();