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 <QtDebug>
#include <QDir>
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 ; 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));
}
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));
}
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 {
Q_OBJECT
friend class ProjectMVisualisation;
public:
ProjectMPresetModel(ProjectMVisualisation* vis, QObject* parent = 0);
@ -31,6 +33,8 @@ public:
Role_Url = Qt::UserRole,
};
void MarkSelected(const QString& path, bool selected);
// QAbstractItemModel
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &child) const;
@ -46,7 +50,17 @@ public slots:
void SelectNone();
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_;
QList<Preset> all_presets_;
};
#endif // PROJECTMPRESETMODEL_H

View File

@ -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 ; i<TOTAL_RATING_TYPES ; ++i)
default_rating_list_.push_back(3);
}
ProjectMVisualisation::~ProjectMVisualisation() {
@ -92,6 +97,7 @@ void ProjectMVisualisation::InitProjectM() {
s.softCutRatingsEnabled = false;
projectm_.reset(new projectM(s));
preset_model_ = new ProjectMPresetModel(this, this);
Load();
}
@ -129,61 +135,93 @@ void ProjectMVisualisation::ConsumeBuffer(GstBuffer *buffer, GstEnginePipeline*)
gst_buffer_unref(buffer);
}
void ProjectMVisualisation::set_selected(int preset, bool selected) {
if (selected)
selected_indices_.insert(preset);
else
selected_indices_.remove(preset);
void ProjectMVisualisation::SetSelected(const QStringList& paths, bool selected) {
foreach (const QString& path, paths) {
int index = IndexOfPreset(path);
if (selected && index == -1) {
projectm_->addPresetURL(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 ; i<count ; ++i) {
selected_indices_ << i;
}
}
void ProjectMVisualisation::ClearSelected() {
projectm_->clearPlaylist();
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() {
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 ; i<preset_model_->all_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 ; i<count ; ++i)
selected_indices_ << i;
} else {
foreach (const QVariant& var, presets)
if (var.toInt() < count)
selected_indices_ << var.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);
}
}
}
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();
}

View File

@ -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<int> 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> projectm_;
int mode_;
QSet<int> selected_indices_;
ProjectMPresetModel* preset_model_;
Mode mode_;
std::vector<int> default_rating_list_;
int texture_size_;
};

View File

@ -21,20 +21,23 @@
#include <QPushButton>
#include <projectM.hpp>
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();
}

View File

@ -19,7 +19,6 @@
#include <QDialog>
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