Make the visualisation chooser actually work

This commit is contained in:
David Sansome 2010-06-07 13:29:57 +00:00
parent cdb8dd8dd5
commit 0e19ca17e5
6 changed files with 173 additions and 64 deletions

View File

@ -20,17 +20,28 @@
#include <projectM.hpp> #include <projectM.hpp>
#include <QtDebug> #include <QtDebug>
#include <QDir>
ProjectMPresetModel::ProjectMPresetModel(ProjectMVisualisation* vis, QObject *parent) ProjectMPresetModel::ProjectMPresetModel(ProjectMVisualisation* vis, QObject *parent)
: QAbstractItemModel(parent), : QAbstractItemModel(parent),
vis_(vis) 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 { int ProjectMPresetModel::rowCount(const QModelIndex&) const {
if (!vis_) if (!vis_)
return 0; return 0;
return vis_->projectm()->getPlaylistSize(); return all_presets_.count();
} }
int ProjectMPresetModel::columnCount(const QModelIndex&) const { 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 { QVariant ProjectMPresetModel::data(const QModelIndex &index, int role) const {
switch (role) { switch (role) {
case Qt::DisplayRole: case Qt::DisplayRole:
return QString::fromStdString(vis_->projectm()->getPresetName(index.row())); return all_presets_[index.row()].name_;
case Qt::CheckStateRole: { case Qt::CheckStateRole: {
bool selected = vis_->is_selected(index.row()); bool selected = all_presets_[index.row()].selected_;
return selected ? Qt::Checked : Qt::Unchecked; return selected ? Qt::Checked : Qt::Unchecked;
} }
case Role_Url: case Role_Url:
return QString::fromStdString(vis_->projectm()->getPresetURL(index.row())); return all_presets_[index.row()].path_;
default: default:
return QVariant(); return QVariant();
} }
@ -70,23 +81,43 @@ Qt::ItemFlags ProjectMPresetModel::flags(const QModelIndex &index) const {
bool ProjectMPresetModel::setData(const QModelIndex &index, bool ProjectMPresetModel::setData(const QModelIndex &index,
const QVariant &value, int role) { const QVariant &value, int role) {
if (role == Qt::CheckStateRole) { 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 true;
} }
return false; return false;
} }
void ProjectMPresetModel::SetImmediatePreset(const QModelIndex& index) { void ProjectMPresetModel::SetImmediatePreset(const QModelIndex& index) {
vis_->projectm()->selectPreset(index.row(), true); vis_->SetImmediatePreset(all_presets_[index.row()].path_);
} }
void ProjectMPresetModel::SelectAll() { void ProjectMPresetModel::SelectAll() {
vis_->set_all_selected(true); QStringList paths;
for (int i=0 ; i<all_presets_.count() ; ++i) {
paths << all_presets_[i].path_;
all_presets_[i].selected_ = true;
}
vis_->SetSelected(paths, true);
emit dataChanged(index(0, 0), index(rowCount()-1, 0)); emit dataChanged(index(0, 0), index(rowCount()-1, 0));
} }
void ProjectMPresetModel::SelectNone() { void ProjectMPresetModel::SelectNone() {
vis_->set_all_selected(false); vis_->ClearSelected();
for (int i=0 ; i<all_presets_.count() ; ++i) {
all_presets_[i].selected_ = false;
}
emit dataChanged(index(0, 0), index(rowCount()-1, 0)); emit dataChanged(index(0, 0), index(rowCount()-1, 0));
} }
void ProjectMPresetModel::MarkSelected(const QString& path, bool selected) {
for (int i=0 ; i<all_presets_.count() ; ++i) {
if (path == all_presets_[i].path_) {
all_presets_[i].selected_ = selected;
return;
}
}
}

View File

@ -24,6 +24,8 @@ class ProjectMVisualisation;
class ProjectMPresetModel : public QAbstractItemModel { class ProjectMPresetModel : public QAbstractItemModel {
Q_OBJECT Q_OBJECT
friend class ProjectMVisualisation;
public: public:
ProjectMPresetModel(ProjectMVisualisation* vis, QObject* parent = 0); ProjectMPresetModel(ProjectMVisualisation* vis, QObject* parent = 0);
@ -31,6 +33,8 @@ public:
Role_Url = Qt::UserRole, Role_Url = Qt::UserRole,
}; };
void MarkSelected(const QString& path, bool selected);
// QAbstractItemModel // QAbstractItemModel
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &child) const; QModelIndex parent(const QModelIndex &child) const;
@ -46,7 +50,17 @@ public slots:
void SelectNone(); void SelectNone();
private: private:
struct Preset {
Preset(const QString& path, const QString& name, bool selected)
: path_(path), name_(name), selected_(selected) {}
QString path_;
QString name_;
bool selected_;
};
ProjectMVisualisation* vis_; ProjectMVisualisation* vis_;
QList<Preset> all_presets_;
}; };
#endif // PROJECTMPRESETMODEL_H #endif // PROJECTMPRESETMODEL_H

View File

@ -15,6 +15,7 @@
*/ */
#include "config.h" #include "config.h"
#include "projectmpresetmodel.h"
#include "projectmvisualisation.h" #include "projectmvisualisation.h"
#include "visualisationcontainer.h" #include "visualisationcontainer.h"
@ -38,10 +39,14 @@
ProjectMVisualisation::ProjectMVisualisation(QObject *parent) ProjectMVisualisation::ProjectMVisualisation(QObject *parent)
: QGraphicsScene(parent), : QGraphicsScene(parent),
projectm_(NULL), projectm_(NULL),
mode_(0), preset_model_(NULL),
mode_(Random),
texture_size_(512) texture_size_(512)
{ {
connect(this, SIGNAL(sceneRectChanged(QRectF)), SLOT(SceneRectChanged(QRectF))); connect(this, SIGNAL(sceneRectChanged(QRectF)), SLOT(SceneRectChanged(QRectF)));
for (int i=0 ; i<TOTAL_RATING_TYPES ; ++i)
default_rating_list_.push_back(3);
} }
ProjectMVisualisation::~ProjectMVisualisation() { ProjectMVisualisation::~ProjectMVisualisation() {
@ -92,6 +97,7 @@ void ProjectMVisualisation::InitProjectM() {
s.softCutRatingsEnabled = false; s.softCutRatingsEnabled = false;
projectm_.reset(new projectM(s)); projectm_.reset(new projectM(s));
preset_model_ = new ProjectMPresetModel(this, this);
Load(); Load();
} }
@ -129,61 +135,93 @@ void ProjectMVisualisation::ConsumeBuffer(GstBuffer *buffer, GstEnginePipeline*)
gst_buffer_unref(buffer); gst_buffer_unref(buffer);
} }
void ProjectMVisualisation::set_selected(int preset, bool selected) { void ProjectMVisualisation::SetSelected(const QStringList& paths, bool selected) {
if (selected) foreach (const QString& path, paths) {
selected_indices_.insert(preset); int index = IndexOfPreset(path);
else if (selected && index == -1) {
selected_indices_.remove(preset); projectm_->addPresetURL(path.toStdString(), std::string(), default_rating_list_);
} else if (!selected && index != -1) {
projectm_->removePreset(index);
}
}
Save(); Save();
} }
void ProjectMVisualisation::set_all_selected(bool selected) { void ProjectMVisualisation::ClearSelected() {
selected_indices_.clear(); projectm_->clearPlaylist();
if (selected) {
int count = projectm_->getPlaylistSize();
for (int i=0 ; i<count ; ++i) {
selected_indices_ << i;
}
}
Save(); Save();
} }
int ProjectMVisualisation::IndexOfPreset(const QString &path) const {
for (uint i=0 ; i<projectm_->getPlaylistSize() ; ++i) {
if (QString::fromStdString(projectm_->getPresetURL(i)) == path)
return i;
}
return -1;
}
void ProjectMVisualisation::Load() { void ProjectMVisualisation::Load() {
QSettings s; QSettings s;
s.beginGroup(VisualisationContainer::kSettingsGroup); s.beginGroup(VisualisationContainer::kSettingsGroup);
QVariantList presets(s.value("presets").toList()); mode_ = Mode(s.value("mode", 0).toInt());
int count = projectm_->getPlaylistSize(); projectm_->clearPlaylist();
selected_indices_.clear(); switch (mode_) {
case Random:
if (presets.isEmpty()) { for (int i=0 ; i<preset_model_->all_presets_.count() ; ++i) {
for (int i=0 ; i<count ; ++i) projectm_->addPresetURL(preset_model_->all_presets_[i].path_.toStdString(),
selected_indices_ << i; std::string(), default_rating_list_);
} else { preset_model_->all_presets_[i].selected_ = true;
foreach (const QVariant& var, presets)
if (var.toInt() < count)
selected_indices_ << var.toInt();
} }
break;
mode_ = s.value("mode", 0).toInt(); case FromList: {
QStringList paths(s.value("preset_paths").toStringList());
foreach (const QString& path, paths) {
projectm_->addPresetURL(path.toStdString(), std::string(), default_rating_list_);
preset_model_->MarkSelected(path, true);
}
}
}
} }
void ProjectMVisualisation::Save() { void ProjectMVisualisation::Save() {
QVariantList list; QStringList paths;
foreach (int index, selected_indices_.values()) { foreach (const ProjectMPresetModel::Preset& preset, preset_model_->all_presets_) {
list << index; if (preset.selected_)
paths << preset.path_;
} }
QSettings s; QSettings s;
s.beginGroup(VisualisationContainer::kSettingsGroup); s.beginGroup(VisualisationContainer::kSettingsGroup);
s.setValue("presets", list); s.setValue("preset_paths", paths);
s.setValue("mode", mode_); s.setValue("mode", mode_);
} }
void ProjectMVisualisation::set_mode(int mode) { void ProjectMVisualisation::SetMode(Mode mode) {
mode_ = mode; mode_ = mode;
Save(); 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();
}

View File

@ -27,21 +27,23 @@
class projectM; class projectM;
class ProjectMPresetModel;
class ProjectMVisualisation : public QGraphicsScene, public BufferConsumer { class ProjectMVisualisation : public QGraphicsScene, public BufferConsumer {
Q_OBJECT Q_OBJECT
public: public:
ProjectMVisualisation(QObject *parent = 0); ProjectMVisualisation(QObject *parent = 0);
~ProjectMVisualisation(); ~ProjectMVisualisation();
projectM* projectm() const { return projectm_.get(); } enum Mode {
Random = 0,
FromList = 1,
};
QSet<int> selected_indices() const { return selected_indices_; } QString preset_url() const;
bool is_selected(int preset) { return selected_indices_.contains(preset); } ProjectMPresetModel* preset_model() const { return preset_model_; }
void set_selected(int preset, bool selected);
void set_all_selected(bool selected);
int mode() const { return mode_; } Mode mode() const { return mode_; }
void set_mode(int mode);
// BufferConsumer // BufferConsumer
void ConsumeBuffer(GstBuffer *buffer, GstEnginePipeline*); void ConsumeBuffer(GstBuffer *buffer, GstEnginePipeline*);
@ -49,6 +51,13 @@ public:
public slots: public slots:
void SetTextureSize(int size); 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: protected:
// QGraphicsScene // QGraphicsScene
void drawBackground(QPainter *painter, const QRectF &rect); void drawBackground(QPainter *painter, const QRectF &rect);
@ -61,10 +70,14 @@ private:
void Load(); void Load();
void Save(); void Save();
int IndexOfPreset(const QString& path) const;
private: private:
boost::scoped_ptr<projectM> projectm_; boost::scoped_ptr<projectM> projectm_;
int mode_; ProjectMPresetModel* preset_model_;
QSet<int> selected_indices_; Mode mode_;
std::vector<int> default_rating_list_;
int texture_size_; int texture_size_;
}; };

View File

@ -21,20 +21,23 @@
#include <QPushButton> #include <QPushButton>
#include <projectM.hpp>
VisualisationSelector::VisualisationSelector(QWidget *parent) VisualisationSelector::VisualisationSelector(QWidget *parent)
: QDialog(parent), : QDialog(parent),
ui_(new Ui_VisualisationSelector), ui_(new Ui_VisualisationSelector),
vis_(NULL), vis_(NULL)
preset_model_(NULL)
{ {
ui_->setupUi(this); ui_->setupUi(this);
QPushButton* select_all = select_all_ =
ui_->buttonBox->addButton(tr("Select All"), QDialogButtonBox::ActionRole); ui_->buttonBox->addButton(tr("Select All"), QDialogButtonBox::ActionRole);
QPushButton* select_none = select_none_ =
ui_->buttonBox->addButton(tr("Select None"), QDialogButtonBox::ActionRole); ui_->buttonBox->addButton(tr("Select None"), QDialogButtonBox::ActionRole);
connect(select_all, SIGNAL(clicked()), SLOT(SelectAll())); connect(select_all_, SIGNAL(clicked()), SLOT(SelectAll()));
connect(select_none, SIGNAL(clicked()), SLOT(SelectNone())); connect(select_none_, SIGNAL(clicked()), SLOT(SelectNone()));
select_all_->setEnabled(false);
select_none_->setEnabled(false);
connect(ui_->mode, SIGNAL(currentIndexChanged(int)), SLOT(ModeChanged(int))); connect(ui_->mode, SIGNAL(currentIndexChanged(int)), SLOT(ModeChanged(int)));
} }
@ -45,24 +48,33 @@ VisualisationSelector::~VisualisationSelector() {
void VisualisationSelector::showEvent(QShowEvent *) { void VisualisationSelector::showEvent(QShowEvent *) {
if (!ui_->list->model()) { if (!ui_->list->model()) {
preset_model_ = new ProjectMPresetModel(vis_, this); ui_->list->setModel(vis_->preset_model());
ui_->list->setModel(preset_model_);
connect(ui_->list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), connect(ui_->list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
preset_model_, SLOT(SetImmediatePreset(QModelIndex))); vis_->preset_model(), SLOT(SetImmediatePreset(QModelIndex)));
ui_->mode->setCurrentIndex(vis_->mode()); ui_->mode->setCurrentIndex(vis_->mode());
} }
vis_->Lock(true);
}
void VisualisationSelector::hideEvent(QHideEvent *) {
vis_->Lock(false);
} }
void VisualisationSelector::ModeChanged(int mode) { void VisualisationSelector::ModeChanged(int mode) {
ui_->list->setEnabled(mode == 1); bool enabled = mode == 1;
vis_->set_mode(mode); ui_->list->setEnabled(enabled);
select_all_->setEnabled(enabled);
select_none_->setEnabled(enabled);
vis_->SetMode(ProjectMVisualisation::Mode(mode));
} }
void VisualisationSelector::SelectAll() { void VisualisationSelector::SelectAll() {
preset_model_->SelectAll(); vis_->preset_model()->SelectAll();
} }
void VisualisationSelector::SelectNone() { void VisualisationSelector::SelectNone() {
preset_model_->SelectNone(); vis_->preset_model()->SelectNone();
} }

View File

@ -19,7 +19,6 @@
#include <QDialog> #include <QDialog>
class ProjectMPresetModel;
class ProjectMVisualisation; class ProjectMVisualisation;
class Ui_VisualisationSelector; class Ui_VisualisationSelector;
@ -33,6 +32,7 @@ public:
protected: protected:
void showEvent(QShowEvent *); void showEvent(QShowEvent *);
void hideEvent(QHideEvent *);
private slots: private slots:
void ModeChanged(int mode); void ModeChanged(int mode);
@ -41,9 +41,10 @@ private slots:
private: private:
Ui_VisualisationSelector* ui_; Ui_VisualisationSelector* ui_;
ProjectMVisualisation* vis_; QPushButton* select_all_;
QPushButton* select_none_;
ProjectMPresetModel* preset_model_; ProjectMVisualisation* vis_;
}; };
#endif // VISUALISATIONSELECTOR_H #endif // VISUALISATIONSELECTOR_H