diff --git a/src/visualisations/projectmpresetmodel.cpp b/src/visualisations/projectmpresetmodel.cpp index 6c3e36cd9..0dc1f88a1 100644 --- a/src/visualisations/projectmpresetmodel.cpp +++ b/src/visualisations/projectmpresetmodel.cpp @@ -20,17 +20,28 @@ #include #include +#include ProjectMPresetModel::ProjectMPresetModel(ProjectMVisualisation* vis, QObject *parent) : QAbstractItemModel(parent), vis_(vis) { + // Find presets + QDir preset_dir(vis_->preset_url()); + QStringList presets(preset_dir.entryList( + QStringList() << "*.milk" << "*.prjm", + QDir::Files | QDir::NoDotAndDotDot | QDir::Readable, + QDir::Name | QDir::IgnoreCase)); + + foreach (const QString& filename, presets) { + all_presets_ << Preset(preset_dir.absoluteFilePath(filename), filename, false); + } } int ProjectMPresetModel::rowCount(const QModelIndex&) const { if (!vis_) return 0; - return vis_->projectm()->getPlaylistSize(); + return all_presets_.count(); } int ProjectMPresetModel::columnCount(const QModelIndex&) const { @@ -48,13 +59,13 @@ QModelIndex ProjectMPresetModel::parent(const QModelIndex &child) const { QVariant ProjectMPresetModel::data(const QModelIndex &index, int role) const { switch (role) { case Qt::DisplayRole: - return QString::fromStdString(vis_->projectm()->getPresetName(index.row())); + return all_presets_[index.row()].name_; case Qt::CheckStateRole: { - bool selected = vis_->is_selected(index.row()); + bool selected = all_presets_[index.row()].selected_; return selected ? Qt::Checked : Qt::Unchecked; } case Role_Url: - return QString::fromStdString(vis_->projectm()->getPresetURL(index.row())); + return all_presets_[index.row()].path_; default: return QVariant(); } @@ -70,23 +81,43 @@ Qt::ItemFlags ProjectMPresetModel::flags(const QModelIndex &index) const { bool ProjectMPresetModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (role == Qt::CheckStateRole) { - vis_->set_selected(index.row(), value.toBool()); + all_presets_[index.row()].selected_ = value.toBool(); + vis_->SetSelected(QStringList() << all_presets_[index.row()].path_, value.toBool()); return true; } return false; } void ProjectMPresetModel::SetImmediatePreset(const QModelIndex& index) { - vis_->projectm()->selectPreset(index.row(), true); + vis_->SetImmediatePreset(all_presets_[index.row()].path_); } void ProjectMPresetModel::SelectAll() { - vis_->set_all_selected(true); + QStringList paths; + for (int i=0 ; iSetSelected(paths, true); + emit dataChanged(index(0, 0), index(rowCount()-1, 0)); } void ProjectMPresetModel::SelectNone() { - vis_->set_all_selected(false); + vis_->ClearSelected(); + for (int i=0 ; i all_presets_; }; #endif // PROJECTMPRESETMODEL_H diff --git a/src/visualisations/projectmvisualisation.cpp b/src/visualisations/projectmvisualisation.cpp index 3b98986b3..e99727e0c 100644 --- a/src/visualisations/projectmvisualisation.cpp +++ b/src/visualisations/projectmvisualisation.cpp @@ -15,6 +15,7 @@ */ #include "config.h" +#include "projectmpresetmodel.h" #include "projectmvisualisation.h" #include "visualisationcontainer.h" @@ -38,10 +39,14 @@ ProjectMVisualisation::ProjectMVisualisation(QObject *parent) : QGraphicsScene(parent), projectm_(NULL), - mode_(0), + preset_model_(NULL), + mode_(Random), texture_size_(512) { connect(this, SIGNAL(sceneRectChanged(QRectF)), SLOT(SceneRectChanged(QRectF))); + + for (int i=0 ; iaddPresetURL(path.toStdString(), std::string(), default_rating_list_); + } else if (!selected && index != -1) { + projectm_->removePreset(index); + } + } Save(); } -void ProjectMVisualisation::set_all_selected(bool selected) { - selected_indices_.clear(); - if (selected) { - int count = projectm_->getPlaylistSize(); - for (int i=0 ; iclearPlaylist(); Save(); } +int ProjectMVisualisation::IndexOfPreset(const QString &path) const { + for (uint i=0 ; igetPlaylistSize() ; ++i) { + if (QString::fromStdString(projectm_->getPresetURL(i)) == path) + return i; + } + return -1; +} + void ProjectMVisualisation::Load() { QSettings s; s.beginGroup(VisualisationContainer::kSettingsGroup); - QVariantList presets(s.value("presets").toList()); + mode_ = Mode(s.value("mode", 0).toInt()); - int count = projectm_->getPlaylistSize(); - selected_indices_.clear(); + projectm_->clearPlaylist(); + switch (mode_) { + case Random: + for (int i=0 ; iall_presets_.count() ; ++i) { + projectm_->addPresetURL(preset_model_->all_presets_[i].path_.toStdString(), + std::string(), default_rating_list_); + preset_model_->all_presets_[i].selected_ = true; + } + break; - if (presets.isEmpty()) { - for (int i=0 ; iaddPresetURL(path.toStdString(), std::string(), default_rating_list_); + preset_model_->MarkSelected(path, true); + } + } } - - mode_ = s.value("mode", 0).toInt(); } void ProjectMVisualisation::Save() { - QVariantList list; + QStringList paths; - foreach (int index, selected_indices_.values()) { - list << index; + foreach (const ProjectMPresetModel::Preset& preset, preset_model_->all_presets_) { + if (preset.selected_) + paths << preset.path_; } QSettings s; s.beginGroup(VisualisationContainer::kSettingsGroup); - s.setValue("presets", list); + s.setValue("preset_paths", paths); s.setValue("mode", mode_); } -void ProjectMVisualisation::set_mode(int mode) { +void ProjectMVisualisation::SetMode(Mode mode) { mode_ = mode; Save(); } +QString ProjectMVisualisation::preset_url() const { + return QString::fromStdString(projectm_->settings().presetURL); +} + +void ProjectMVisualisation::SetImmediatePreset(const QString& path) { + int index = IndexOfPreset(path); + if (index == -1) { + index = projectm_->addPresetURL(path.toStdString(), std::string(), default_rating_list_); + } + + projectm_->selectPreset(index, true); +} + +void ProjectMVisualisation::Lock(bool lock) { + projectm_->setPresetLock(lock); + + if (!lock) + Load(); +} + diff --git a/src/visualisations/projectmvisualisation.h b/src/visualisations/projectmvisualisation.h index 4880a2512..0853a1d9f 100644 --- a/src/visualisations/projectmvisualisation.h +++ b/src/visualisations/projectmvisualisation.h @@ -27,21 +27,23 @@ class projectM; +class ProjectMPresetModel; + class ProjectMVisualisation : public QGraphicsScene, public BufferConsumer { Q_OBJECT public: ProjectMVisualisation(QObject *parent = 0); ~ProjectMVisualisation(); - projectM* projectm() const { return projectm_.get(); } + enum Mode { + Random = 0, + FromList = 1, + }; - QSet selected_indices() const { return selected_indices_; } - bool is_selected(int preset) { return selected_indices_.contains(preset); } - void set_selected(int preset, bool selected); - void set_all_selected(bool selected); + QString preset_url() const; + ProjectMPresetModel* preset_model() const { return preset_model_; } - int mode() const { return mode_; } - void set_mode(int mode); + Mode mode() const { return mode_; } // BufferConsumer void ConsumeBuffer(GstBuffer *buffer, GstEnginePipeline*); @@ -49,6 +51,13 @@ public: public slots: void SetTextureSize(int size); + void SetSelected(const QStringList& paths, bool selected); + void ClearSelected(); + void SetImmediatePreset(const QString& path); + void SetMode(Mode mode); + + void Lock(bool lock); + protected: // QGraphicsScene void drawBackground(QPainter *painter, const QRectF &rect); @@ -61,10 +70,14 @@ private: void Load(); void Save(); + int IndexOfPreset(const QString& path) const; + private: boost::scoped_ptr projectm_; - int mode_; - QSet selected_indices_; + ProjectMPresetModel* preset_model_; + Mode mode_; + + std::vector default_rating_list_; int texture_size_; }; diff --git a/src/visualisations/visualisationselector.cpp b/src/visualisations/visualisationselector.cpp index d071d5b6f..0bb531072 100644 --- a/src/visualisations/visualisationselector.cpp +++ b/src/visualisations/visualisationselector.cpp @@ -21,20 +21,23 @@ #include +#include + VisualisationSelector::VisualisationSelector(QWidget *parent) : QDialog(parent), ui_(new Ui_VisualisationSelector), - vis_(NULL), - preset_model_(NULL) + vis_(NULL) { ui_->setupUi(this); - QPushButton* select_all = + select_all_ = ui_->buttonBox->addButton(tr("Select All"), QDialogButtonBox::ActionRole); - QPushButton* select_none = + select_none_ = ui_->buttonBox->addButton(tr("Select None"), QDialogButtonBox::ActionRole); - connect(select_all, SIGNAL(clicked()), SLOT(SelectAll())); - connect(select_none, SIGNAL(clicked()), SLOT(SelectNone())); + connect(select_all_, SIGNAL(clicked()), SLOT(SelectAll())); + connect(select_none_, SIGNAL(clicked()), SLOT(SelectNone())); + select_all_->setEnabled(false); + select_none_->setEnabled(false); connect(ui_->mode, SIGNAL(currentIndexChanged(int)), SLOT(ModeChanged(int))); } @@ -45,24 +48,33 @@ VisualisationSelector::~VisualisationSelector() { void VisualisationSelector::showEvent(QShowEvent *) { if (!ui_->list->model()) { - preset_model_ = new ProjectMPresetModel(vis_, this); - ui_->list->setModel(preset_model_); + ui_->list->setModel(vis_->preset_model()); connect(ui_->list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), - preset_model_, SLOT(SetImmediatePreset(QModelIndex))); + vis_->preset_model(), SLOT(SetImmediatePreset(QModelIndex))); ui_->mode->setCurrentIndex(vis_->mode()); } + + vis_->Lock(true); +} + +void VisualisationSelector::hideEvent(QHideEvent *) { + vis_->Lock(false); } void VisualisationSelector::ModeChanged(int mode) { - ui_->list->setEnabled(mode == 1); - vis_->set_mode(mode); + bool enabled = mode == 1; + ui_->list->setEnabled(enabled); + select_all_->setEnabled(enabled); + select_none_->setEnabled(enabled); + + vis_->SetMode(ProjectMVisualisation::Mode(mode)); } void VisualisationSelector::SelectAll() { - preset_model_->SelectAll(); + vis_->preset_model()->SelectAll(); } void VisualisationSelector::SelectNone() { - preset_model_->SelectNone(); + vis_->preset_model()->SelectNone(); } diff --git a/src/visualisations/visualisationselector.h b/src/visualisations/visualisationselector.h index d1c845f73..e951656ae 100644 --- a/src/visualisations/visualisationselector.h +++ b/src/visualisations/visualisationselector.h @@ -19,7 +19,6 @@ #include -class ProjectMPresetModel; class ProjectMVisualisation; class Ui_VisualisationSelector; @@ -33,6 +32,7 @@ public: protected: void showEvent(QShowEvent *); + void hideEvent(QHideEvent *); private slots: void ModeChanged(int mode); @@ -41,9 +41,10 @@ private slots: private: Ui_VisualisationSelector* ui_; - ProjectMVisualisation* vis_; + QPushButton* select_all_; + QPushButton* select_none_; - ProjectMPresetModel* preset_model_; + ProjectMVisualisation* vis_; }; #endif // VISUALISATIONSELECTOR_H