Clementine-audio-player-Mac.../src/visualisations/projectmpresetmodel.cpp

130 lines
3.7 KiB
C++
Raw Normal View History

2010-06-07 03:55:21 +02:00
/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
2010-06-07 03:55:21 +02:00
Clementine 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 3 of the License, or
(at your option) any later version.
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
2010-06-07 03:55:21 +02:00
#include "projectmpresetmodel.h"
#include "projectmvisualisation.h"
#ifdef USE_SYSTEM_PROJECTM
# include <libprojectM/projectM.hpp>
#else
# include "projectM.hpp"
#endif
2010-06-07 03:55:21 +02:00
#include <QtDebug>
#include <QDir>
2010-06-07 03:55:21 +02:00
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);
}
2010-06-07 03:55:21 +02:00
}
int ProjectMPresetModel::rowCount(const QModelIndex&) const {
if (!vis_)
return 0;
return all_presets_.count();
2010-06-07 03:55:21 +02:00
}
int ProjectMPresetModel::columnCount(const QModelIndex&) const {
return 1;
}
QModelIndex ProjectMPresetModel::index(int row, int column, const QModelIndex&) const {
return createIndex(row, column);
}
QModelIndex ProjectMPresetModel::parent(const QModelIndex &child) const {
return QModelIndex();
}
QVariant ProjectMPresetModel::data(const QModelIndex &index, int role) const {
switch (role) {
case Qt::DisplayRole:
return all_presets_[index.row()].name_;
2010-06-07 03:55:21 +02:00
case Qt::CheckStateRole: {
bool selected = all_presets_[index.row()].selected_;
2010-06-07 03:55:21 +02:00
return selected ? Qt::Checked : Qt::Unchecked;
}
case Role_Url:
return all_presets_[index.row()].path_;
2010-06-07 03:55:21 +02:00
default:
return QVariant();
}
}
Qt::ItemFlags ProjectMPresetModel::flags(const QModelIndex &index) const {
if (!index.isValid())
return QAbstractItemModel::flags(index);
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable |
Qt::ItemIsEnabled;
}
bool ProjectMPresetModel::setData(const QModelIndex &index,
const QVariant &value, int role) {
if (role == Qt::CheckStateRole) {
all_presets_[index.row()].selected_ = value.toBool();
vis_->SetSelected(QStringList() << all_presets_[index.row()].path_, value.toBool());
2010-06-07 03:55:21 +02:00
return true;
}
return false;
}
void ProjectMPresetModel::SetImmediatePreset(const QModelIndex& index) {
vis_->SetImmediatePreset(all_presets_[index.row()].path_);
2010-06-07 03:55:21 +02:00
}
void ProjectMPresetModel::SelectAll() {
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);
2010-06-07 03:55:21 +02:00
emit dataChanged(index(0, 0), index(rowCount()-1, 0));
}
void ProjectMPresetModel::SelectNone() {
vis_->ClearSelected();
for (int i=0 ; i<all_presets_.count() ; ++i) {
all_presets_[i].selected_ = false;
}
2010-06-07 03:55:21 +02:00
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;
}
}
}