amuse/Editor/ProjectModel.cpp

289 lines
8.5 KiB
C++
Raw Normal View History

#include <athena/FileWriter.hpp>
#include <athena/FileReader.hpp>
#include "ProjectModel.hpp"
#include "Common.hpp"
#include "athena/YAMLDocWriter.hpp"
2018-07-17 04:48:38 +00:00
QIcon ProjectModel::GroupNode::Icon;
QIcon ProjectModel::SongGroupNode::Icon;
QIcon ProjectModel::SoundGroupNode::Icon;
ProjectModel::ProjectModel(const QString& path, QObject* parent)
: QAbstractItemModel(parent), m_dir(path)
{
2018-07-17 04:48:38 +00:00
m_root = std::make_unique<RootNode>();
2018-07-17 04:48:38 +00:00
GroupNode::Icon = QIcon(":/icons/IconGroup.svg");
SongGroupNode::Icon = QIcon(":/icons/IconSongGroup.svg");
SoundGroupNode::Icon = QIcon(":/icons/IconSoundGroup.svg");
}
2018-07-18 07:39:26 +00:00
bool ProjectModel::clearProjectData()
{
m_projectDatabase = amuse::ProjectDatabase();
m_groups.clear();
m_needsReset = true;
return true;
}
bool ProjectModel::openGroupData(const QString& groupName, UIMessenger& messenger)
{
m_projectDatabase.setIdDatabases();
QString path = QFileInfo(m_dir, groupName).filePath();
m_groups.insert(std::make_pair(groupName, QStringToSysString(path)));
m_needsReset = true;
return true;
}
2018-07-17 04:48:38 +00:00
bool ProjectModel::importGroupData(const QString& groupName, const amuse::AudioGroupData& data,
ImportMode mode, UIMessenger& messenger)
{
2018-07-17 04:48:38 +00:00
m_projectDatabase.setIdDatabases();
2018-07-17 04:48:38 +00:00
amuse::AudioGroupDatabase& grp = m_groups.insert(std::make_pair(groupName, data)).first->second;
2018-07-15 06:10:50 +00:00
grp.setIdDatabases();
2018-07-17 04:48:38 +00:00
amuse::AudioGroupProject::BootstrapObjectIDs(data);
2018-07-17 04:48:38 +00:00
if (!MkPath(m_dir.path(), messenger))
return false;
QDir dir(QFileInfo(m_dir, groupName).filePath());
if (!MkPath(dir.path(), messenger))
return false;
2018-07-17 04:48:38 +00:00
amuse::SystemString sysDir = QStringToSysString(dir.path());
switch (mode)
{
2018-07-17 04:48:38 +00:00
case ImportMode::Original:
grp.getSdir().extractAllCompressed(sysDir, data.getSamp());
break;
case ImportMode::WAVs:
grp.getSdir().extractAllWAV(sysDir, data.getSamp());
break;
case ImportMode::Both:
grp.getSdir().extractAllWAV(sysDir, data.getSamp());
grp.getSdir().extractAllCompressed(sysDir, data.getSamp());
break;
default:
break;
2018-07-16 07:41:15 +00:00
}
2018-07-17 04:48:38 +00:00
grp.getProj().toYAML(sysDir);
grp.getPool().toYAML(sysDir);
m_needsReset = true;
2018-07-16 07:41:15 +00:00
return true;
}
2018-07-17 04:48:38 +00:00
bool ProjectModel::saveToFile(UIMessenger& messenger)
2018-07-16 07:41:15 +00:00
{
2018-07-17 04:48:38 +00:00
m_projectDatabase.setIdDatabases();
2018-07-16 07:41:15 +00:00
2018-07-17 04:48:38 +00:00
if (!MkPath(m_dir.path(), messenger))
2018-07-16 07:41:15 +00:00
return false;
for (auto& g : m_groups)
{
QDir dir(QFileInfo(m_dir, g.first).filePath());
2018-07-17 04:48:38 +00:00
if (!MkPath(dir.path(), messenger))
2018-07-16 07:41:15 +00:00
return false;
g.second.setIdDatabases();
amuse::SystemString groupPath = QStringToSysString(dir.path());
2018-07-17 04:48:38 +00:00
g.second.getProj().toYAML(groupPath);
g.second.getPool().toYAML(groupPath);
}
return true;
}
2018-07-17 04:48:38 +00:00
void ProjectModel::_resetModelData()
{
beginResetModel();
m_projectDatabase.setIdDatabases();
m_root = std::make_unique<RootNode>();
m_root->reserve(m_groups.size());
for (auto it = m_groups.begin() ; it != m_groups.end() ; ++it)
{
it->second.setIdDatabases();
GroupNode& gn = m_root->makeChild<GroupNode>(it);
2018-07-18 07:39:26 +00:00
amuse::AudioGroup& group = it->second;
auto& songGroups = group.getProj().songGroups();
auto& sfxGroups = group.getProj().sfxGroups();
auto& soundMacros = group.getPool().soundMacros();
auto& tables = group.getPool().tables();
auto& keymaps = group.getPool().keymaps();
auto& layers = group.getPool().layers();
2018-07-17 04:48:38 +00:00
gn.reserve(songGroups.size() + sfxGroups.size() + 4);
for (const auto& grp : SortUnorderedMap(songGroups))
gn.makeChild<SongGroupNode>(grp.first, grp.second.get());
for (const auto& grp : SortUnorderedMap(sfxGroups))
gn.makeChild<SoundGroupNode>(grp.first, grp.second.get());
if (soundMacros.size())
{
CollectionNode& col =
gn.makeChild<CollectionNode>(tr("Sound Macros"), QIcon(":/icons/IconSoundMacro.svg"));
col.reserve(soundMacros.size());
for (const auto& macro : SortUnorderedMap(soundMacros))
2018-07-18 07:39:26 +00:00
col.makeChild<SoundMacroNode>(macro.first, macro.second.get());
2018-07-17 04:48:38 +00:00
}
if (tables.size())
{
auto tablesSort = SortUnorderedMap(tables);
size_t ADSRCount = 0;
size_t curveCount = 0;
for (auto& t : tablesSort)
{
amuse::ITable::Type tp = t.second.get()->Isa();
if (tp == amuse::ITable::Type::ADSR || tp == amuse::ITable::Type::ADSRDLS)
ADSRCount += 1;
else if (tp == amuse::ITable::Type::Curve)
curveCount += 1;
}
if (ADSRCount)
{
CollectionNode& col =
gn.makeChild<CollectionNode>(tr("ADSRs"), QIcon(":/icons/IconADSR.svg"));
col.reserve(ADSRCount);
for (auto& t : tablesSort)
{
amuse::ITable::Type tp = t.second.get()->Isa();
if (tp == amuse::ITable::Type::ADSR || tp == amuse::ITable::Type::ADSRDLS)
2018-07-18 07:39:26 +00:00
col.makeChild<ADSRNode>(t.first, *t.second.get());
2018-07-17 04:48:38 +00:00
}
}
if (curveCount)
{
CollectionNode& col =
gn.makeChild<CollectionNode>(tr("Curves"), QIcon(":/icons/IconCurve.svg"));
col.reserve(curveCount);
for (auto& t : tablesSort)
{
amuse::ITable::Type tp = t.second.get()->Isa();
if (tp == amuse::ITable::Type::Curve)
2018-07-18 07:39:26 +00:00
col.makeChild<CurveNode>(t.first, static_cast<amuse::Curve&>(*t.second.get()));
2018-07-17 04:48:38 +00:00
}
}
}
if (keymaps.size())
{
CollectionNode& col =
gn.makeChild<CollectionNode>(tr("Keymaps"), QIcon(":/icons/IconKeymap.svg"));
col.reserve(keymaps.size());
for (auto& keymap : SortUnorderedMap(keymaps))
2018-07-18 07:39:26 +00:00
col.makeChild<KeymapNode>(keymap.first, keymap.second.get());
2018-07-17 04:48:38 +00:00
}
if (layers.size())
{
CollectionNode& col =
gn.makeChild<CollectionNode>(tr("Layers"), QIcon(":/icons/IconLayers.svg"));
col.reserve(layers.size());
for (auto& keymap : SortUnorderedMap(layers))
2018-07-18 07:39:26 +00:00
col.makeChild<LayersNode>(keymap.first, keymap.second.get());
2018-07-17 04:48:38 +00:00
}
}
endResetModel();
}
void ProjectModel::ensureModelData()
{
if (m_needsReset)
{
_resetModelData();
m_needsReset = false;
}
}
QModelIndex ProjectModel::index(int row, int column, const QModelIndex& parent) const
{
2018-07-17 04:48:38 +00:00
if (!hasIndex(row, column, parent))
return QModelIndex();
INode* parentItem;
if (!parent.isValid())
parentItem = m_root.get();
else
parentItem = static_cast<INode*>(parent.internalPointer());
INode* childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
2018-07-17 04:48:38 +00:00
QModelIndex ProjectModel::parent(const QModelIndex& index) const
{
2018-07-17 04:48:38 +00:00
if (!index.isValid())
return QModelIndex();
INode* childItem = static_cast<INode*>(index.internalPointer());
INode* parentItem = childItem->parent();
if (parentItem == m_root.get())
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int ProjectModel::rowCount(const QModelIndex& parent) const
{
2018-07-17 04:48:38 +00:00
INode* parentItem;
if (!parent.isValid())
parentItem = m_root.get();
else
parentItem = static_cast<INode*>(parent.internalPointer());
return parentItem->childCount();
}
int ProjectModel::columnCount(const QModelIndex& parent) const
{
2018-07-17 04:48:38 +00:00
return 1;
}
QVariant ProjectModel::data(const QModelIndex& index, int role) const
{
2018-07-17 04:48:38 +00:00
if (!index.isValid())
return QVariant();
INode* item = static_cast<INode*>(index.internalPointer());
switch (role)
{
case Qt::DisplayRole:
return item->text();
case Qt::DecorationRole:
return item->icon();
default:
return {};
}
}
Qt::ItemFlags ProjectModel::flags(const QModelIndex& index) const
{
if (!index.isValid())
return 0;
return QAbstractItemModel::flags(index);
}
2018-07-18 07:39:26 +00:00
ProjectModel::INode* ProjectModel::node(const QModelIndex& index) const
{
if (!index.isValid())
return nullptr;
return static_cast<INode*>(index.internalPointer());
}
bool ProjectModel::canDelete() const
{
return false;
}
void ProjectModel::del()
{
}