amuse/Editor/ProjectModel.hpp

276 lines
9.8 KiB
C++
Raw Normal View History

#ifndef AMUSE_PROJECT_MODEL_HPP
#define AMUSE_PROJECT_MODEL_HPP
#include <QAbstractItemModel>
2018-07-28 04:34:29 +00:00
#include <QIdentityProxyModel>
#include <QDir>
2018-07-17 04:48:38 +00:00
#include <QIcon>
#include <map>
2018-07-17 04:48:38 +00:00
#include "Common.hpp"
#include "amuse/AudioGroup.hpp"
#include "amuse/AudioGroupData.hpp"
#include "amuse/AudioGroupProject.hpp"
#include "amuse/AudioGroupPool.hpp"
#include "amuse/AudioGroupSampleDirectory.hpp"
2018-07-28 04:34:29 +00:00
class ProjectModel;
class NullItemProxyModel : public QIdentityProxyModel
{
Q_OBJECT
public:
explicit NullItemProxyModel(ProjectModel* source);
QModelIndex mapFromSource(const QModelIndex& sourceIndex) const;
QModelIndex mapToSource(const QModelIndex& proxyIndex) const;
int rowCount(const QModelIndex& parent) const;
QModelIndex index(int row, int column, const QModelIndex& parent) const;
QVariant data(const QModelIndex& proxyIndex, int role) const;
};
class ProjectModel : public QAbstractItemModel
{
Q_OBJECT
public:
enum class ImportMode
{
Original,
WAVs,
Both
};
2018-07-17 04:48:38 +00:00
private:
QDir m_dir;
2018-07-28 04:34:29 +00:00
NullItemProxyModel m_nullProxy;
2018-07-17 04:48:38 +00:00
amuse::ProjectDatabase m_projectDatabase;
std::map<QString, amuse::AudioGroupDatabase> m_groups;
2018-07-18 07:39:26 +00:00
public:
2018-07-30 06:20:03 +00:00
class INode : public amuse::IObj
{
2018-07-18 07:39:26 +00:00
public:
2018-07-17 04:48:38 +00:00
enum class Type
{
2018-07-28 04:34:29 +00:00
Null,
2018-07-18 07:39:26 +00:00
Root,
2018-07-17 04:48:38 +00:00
Group, // Top-level group
SongGroup,
2018-07-18 07:39:26 +00:00
SoundGroup,
2018-07-17 04:48:38 +00:00
Collection, // Classified object collection, one of the following:
SoundMacro,
ADSR,
Curve,
Keymap,
2018-07-29 03:37:06 +00:00
Layer,
Sample
2018-07-17 04:48:38 +00:00
};
2018-07-28 04:34:29 +00:00
protected:
2018-07-17 04:48:38 +00:00
INode* m_parent;
2018-07-30 06:20:03 +00:00
std::vector<amuse::IObjToken<INode>> m_children;
amuse::IObjToken<INode> m_nullChild;
2018-07-17 04:48:38 +00:00
int m_row;
public:
virtual ~INode() = default;
2018-07-28 04:34:29 +00:00
INode(INode* parent) : m_parent(parent), m_row(0)
{
/* ONLY USED BY NULL NODE! */
}
INode(INode* parent, int row);
2018-07-17 04:48:38 +00:00
int childCount() const { return int(m_children.size()); }
2018-07-28 04:34:29 +00:00
INode* child(int row) const
{
if (row == m_children.size())
return nullChild();
return m_children[row].get();
}
INode* nullChild() const { return m_nullChild.get(); }
2018-07-17 04:48:38 +00:00
INode* parent() const { return m_parent; }
int row() const { return m_row; }
2018-07-28 04:34:29 +00:00
void reindexRows(int row)
{
for (auto it = m_children.begin() + row; it != m_children.end(); ++it)
(*it)->m_row = row++;
m_nullChild->m_row = row;
}
2018-07-30 06:20:03 +00:00
void insertChild(int row, amuse::ObjToken<INode> n)
2018-07-28 04:34:29 +00:00
{
2018-07-30 06:20:03 +00:00
m_children.insert(m_children.begin() + row, n.get());
2018-07-28 04:34:29 +00:00
reindexRows(row);
}
2018-07-30 06:20:03 +00:00
amuse::ObjToken<INode> removeChild(int row)
2018-07-28 04:34:29 +00:00
{
2018-07-30 06:20:03 +00:00
amuse::ObjToken<INode> ret = m_children[row].get();
2018-07-28 04:34:29 +00:00
m_children.erase(m_children.begin() + row);
reindexRows(row);
return ret;
}
2018-07-17 04:48:38 +00:00
void reserve(size_t sz) { m_children.reserve(sz); }
template<class T, class... _Args>
T& makeChild(_Args&&... args)
2018-07-15 06:10:50 +00:00
{
2018-07-30 06:20:03 +00:00
auto tok = amuse::MakeObj<T>(this, m_children.size(), std::forward<_Args>(args)...);
m_children.push_back(tok.get());
2018-07-28 04:34:29 +00:00
m_nullChild->m_row = int(m_children.size());
2018-07-17 04:48:38 +00:00
return static_cast<T&>(*m_children.back());
2018-07-15 06:10:50 +00:00
}
2018-07-17 04:48:38 +00:00
2018-07-28 04:34:29 +00:00
bool depthTraverse(const std::function<bool(INode* node)>& func)
{
for (auto& n : m_children)
if (!n->depthTraverse(func))
break;
return func(this);
}
2018-07-18 07:39:26 +00:00
virtual Type type() const = 0;
2018-07-17 04:48:38 +00:00
virtual QString text() const = 0;
virtual QIcon icon() const = 0;
2018-07-28 04:34:29 +00:00
virtual Qt::ItemFlags flags() const { return Qt::ItemIsEnabled | Qt::ItemIsSelectable; }
};
struct NullNode : INode
{
NullNode(INode* parent) : INode(parent) {}
Type type() const { return Type::Null; }
QString text() const { return {}; }
QIcon icon() const { return {}; }
};
2018-07-17 04:48:38 +00:00
struct RootNode : INode
{
RootNode() : INode(nullptr, 0) {}
2018-07-18 07:39:26 +00:00
Type type() const { return Type::Root; }
2018-07-17 04:48:38 +00:00
QString text() const { return {}; }
QIcon icon() const { return {}; }
2018-07-28 04:34:29 +00:00
Qt::ItemFlags flags() const { return Qt::ItemIsEnabled; }
2018-07-17 04:48:38 +00:00
};
2018-07-28 04:34:29 +00:00
struct CollectionNode;
2018-07-17 04:48:38 +00:00
struct GroupNode : INode
{
std::map<QString, amuse::AudioGroupDatabase>::iterator m_it;
GroupNode(INode* parent, int row, std::map<QString, amuse::AudioGroupDatabase>::iterator it)
: INode(parent, row), m_it(it) {}
2018-07-17 04:48:38 +00:00
static QIcon Icon;
2018-07-18 07:39:26 +00:00
Type type() const { return Type::Group; }
2018-07-17 04:48:38 +00:00
QString text() const { return m_it->first; }
QIcon icon() const { return Icon; }
2018-07-28 04:34:29 +00:00
CollectionNode* getCollectionOfType(Type tp) const;
amuse::AudioGroupDatabase* getAudioGroup() const { return &m_it->second; }
2018-07-17 04:48:38 +00:00
};
struct SongGroupNode : INode
{
amuse::GroupId m_id;
QString m_name;
2018-07-29 03:37:06 +00:00
amuse::ObjToken<amuse::SongGroupIndex> m_index;
SongGroupNode(INode* parent, int row, amuse::GroupId id, amuse::ObjToken<amuse::SongGroupIndex> index)
2018-07-17 04:48:38 +00:00
: INode(parent, row), m_id(id), m_name(amuse::GroupId::CurNameDB->resolveNameFromId(id).data()), m_index(index) {}
static QIcon Icon;
2018-07-18 07:39:26 +00:00
Type type() const { return Type::SongGroup; }
2018-07-17 04:48:38 +00:00
QString text() const { return m_name; }
QIcon icon() const { return Icon; }
};
struct SoundGroupNode : INode
{
amuse::GroupId m_id;
QString m_name;
2018-07-29 03:37:06 +00:00
amuse::ObjToken<amuse::SFXGroupIndex> m_index;
SoundGroupNode(INode* parent, int row, amuse::GroupId id, amuse::ObjToken<amuse::SFXGroupIndex> index)
2018-07-17 04:48:38 +00:00
: INode(parent, row), m_id(id), m_name(amuse::GroupId::CurNameDB->resolveNameFromId(id).data()), m_index(index) {}
static QIcon Icon;
2018-07-18 07:39:26 +00:00
Type type() const { return Type::SoundGroup; }
2018-07-17 04:48:38 +00:00
QString text() const { return m_name; }
QIcon icon() const { return Icon; }
};
2018-07-28 04:34:29 +00:00
struct BasePoolObjectNode;
2018-07-17 04:48:38 +00:00
struct CollectionNode : INode
{
QString m_name;
QIcon m_icon;
2018-07-28 04:34:29 +00:00
Type m_collectionType;
CollectionNode(INode* parent, int row, const QString& name, const QIcon& icon, Type collectionType)
: INode(parent, row), m_name(name), m_icon(icon), m_collectionType(collectionType) {}
2018-07-18 07:39:26 +00:00
Type type() const { return Type::Collection; }
2018-07-17 04:48:38 +00:00
QString text() const { return m_name; }
QIcon icon() const { return m_icon; }
2018-07-28 04:34:29 +00:00
Qt::ItemFlags flags() const { return Qt::ItemIsEnabled; }
Type collectionType() const { return m_collectionType; }
int indexOfId(amuse::ObjectId id) const;
amuse::ObjectId idOfIndex(int idx) const;
BasePoolObjectNode* nodeOfIndex(int idx) const;
2018-08-07 07:09:23 +00:00
BasePoolObjectNode* nodeOfId(amuse::ObjectId id) const;
2018-07-17 04:48:38 +00:00
};
2018-07-28 04:34:29 +00:00
struct BasePoolObjectNode : INode
{
amuse::ObjectId m_id;
2018-07-29 03:37:06 +00:00
QString m_name;
BasePoolObjectNode(INode* parent, int row, amuse::ObjectId id, const QString& name)
: INode(parent, row), m_id(id), m_name(name) {}
2018-07-28 04:34:29 +00:00
amuse::ObjectId id() const { return m_id; }
2018-07-29 03:37:06 +00:00
QString text() const { return m_name; }
QIcon icon() const { return {}; }
2018-07-28 04:34:29 +00:00
};
2018-07-18 07:39:26 +00:00
template <class ID, class T, INode::Type TP>
2018-07-28 04:34:29 +00:00
struct PoolObjectNode : BasePoolObjectNode
2018-07-15 06:10:50 +00:00
{
2018-07-29 03:37:06 +00:00
amuse::ObjToken<T> m_obj;
PoolObjectNode(INode* parent, int row, ID id, amuse::ObjToken<T> obj)
: BasePoolObjectNode(parent, row, id, ID::CurNameDB->resolveNameFromId(id).data()), m_obj(obj) {}
2018-07-17 04:48:38 +00:00
2018-07-18 07:39:26 +00:00
Type type() const { return TP; }
2018-07-17 04:48:38 +00:00
};
2018-07-18 07:39:26 +00:00
using SoundMacroNode = PoolObjectNode<amuse::SoundMacroId, amuse::SoundMacro, INode::Type::SoundMacro>;
2018-07-29 03:37:06 +00:00
using ADSRNode = PoolObjectNode<amuse::TableId, std::unique_ptr<amuse::ITable>, INode::Type::ADSR>;
using CurveNode = PoolObjectNode<amuse::TableId, std::unique_ptr<amuse::ITable>, INode::Type::Curve>;
2018-08-06 04:20:42 +00:00
using KeymapNode = PoolObjectNode<amuse::KeymapId, std::array<amuse::Keymap, 128>, INode::Type::Keymap>;
2018-07-18 07:39:26 +00:00
using LayersNode = PoolObjectNode<amuse::LayersId, std::vector<amuse::LayerMapping>, INode::Type::Layer>;
2018-07-29 03:37:06 +00:00
using SampleNode = PoolObjectNode<amuse::SampleId, amuse::SampleEntry, INode::Type::Sample>;
2018-07-17 04:48:38 +00:00
2018-07-30 06:20:03 +00:00
amuse::ObjToken<RootNode> m_root;
2018-07-17 04:48:38 +00:00
bool m_needsReset = false;
void _resetModelData();
2018-07-15 06:10:50 +00:00
public:
explicit ProjectModel(const QString& path, QObject* parent = Q_NULLPTR);
2018-07-18 07:39:26 +00:00
bool clearProjectData();
bool openGroupData(const QString& groupName, UIMessenger& messenger);
2018-07-29 03:37:06 +00:00
bool reloadSampleData(const QString& groupName, UIMessenger& messenger);
2018-07-17 04:48:38 +00:00
bool importGroupData(const QString& groupName, const amuse::AudioGroupData& data,
ImportMode mode, UIMessenger& messenger);
bool saveToFile(UIMessenger& messenger);
2018-08-03 03:45:48 +00:00
bool ensureModelData();
2018-07-28 04:34:29 +00:00
QModelIndex proxyCreateIndex(int arow, int acolumn, void *adata) const;
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const;
2018-07-28 04:34:29 +00:00
QModelIndex index(INode* node) const;
QModelIndex parent(const QModelIndex& child) const;
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
2018-07-17 04:48:38 +00:00
Qt::ItemFlags flags(const QModelIndex& index) const;
2018-07-18 07:39:26 +00:00
INode* node(const QModelIndex& index) const;
2018-07-28 04:34:29 +00:00
GroupNode* getGroupNode(INode* node) const;
bool canEdit(const QModelIndex& index) const;
2018-07-30 06:20:03 +00:00
void _undoDel(const QModelIndex& index, amuse::ObjToken<ProjectModel::INode> node);
amuse::ObjToken<ProjectModel::INode> _redoDel(const QModelIndex& index);
2018-07-28 04:34:29 +00:00
void del(const QModelIndex& index);
QString path() const { return m_dir.path(); }
2018-07-28 04:34:29 +00:00
NullItemProxyModel* getNullProxy() { return &m_nullProxy; }
};
#endif //AMUSE_PROJECT_MODEL_HPP