From 74dcdf548fbeaf9ac3064f4135c12b67be3c8db1 Mon Sep 17 00:00:00 2001 From: gvnnz Date: Wed, 15 May 2024 21:43:38 +0200 Subject: [PATCH] Use proper class for v::Model::Columns --- src/glue/channel.cpp | 24 ++--- src/glue/sampleEditor.cpp | 2 +- src/gui/model.cpp | 184 +++++++++++++++++++------------------- src/gui/model.h | 30 +++---- 4 files changed, 121 insertions(+), 119 deletions(-) diff --git a/src/glue/channel.cpp b/src/glue/channel.cpp index 7ab97177a..abed1cc0a 100644 --- a/src/glue/channel.cpp +++ b/src/glue/channel.cpp @@ -156,7 +156,7 @@ bool Data::isArmed() const { return g_engine->getChannelsApi().get(id). Data getData(ID channelId) { - const v::Model::Column& column = g_ui->model.getColumnByChannelId(channelId); + const v::Model::Column& column = g_ui->model.columns.getColumnByChannelId(channelId); const v::Model::Channel& channel = column.getById(channelId); return makeData_(channel); } @@ -189,9 +189,9 @@ void addChannel(int columnIndex, ChannelType type, ID groupChannelId) { const m::Channel& ch = g_engine->getChannelsApi().add(type, columnIndex, groupChannelId); if (groupChannelId > 0) - g_ui->model.addChannelToGroup(ch.id, groupChannelId); + g_ui->model.columns.addChannelToGroup(ch.id, groupChannelId); else - g_ui->model.addChannelToColumn(ch.id, columnIndex); + g_ui->model.columns.addChannelToColumn(ch.id, columnIndex); } /* -------------------------------------------------------------------------- */ @@ -212,7 +212,7 @@ void addAndLoadChannels(int columnIndex, const std::vector& fnames) if (res != G_RES_OK) errors = true; else - g_ui->model.addChannelToColumn(ch.id, columnIndex); + g_ui->model.columns.addChannelToColumn(ch.id, columnIndex); } if (errors) @@ -227,7 +227,7 @@ void deleteChannel(ID channelId) return; g_ui->closeAllSubwindows(); g_engine->getChannelsApi().remove(channelId); - g_ui->model.removeChannelFromColumn(channelId); + g_ui->model.columns.removeChannelFromColumn(channelId); } /* -------------------------------------------------------------------------- */ @@ -261,20 +261,20 @@ void cloneChannel(ID channelId, int columnIndex) const m::Channel& ch = g_engine->getChannelsApi().clone(channelId); if (ch.grouped) - g_ui->model.addChannelToGroup(ch.id, ch.parentId); + g_ui->model.columns.addChannelToGroup(ch.id, ch.parentId); else - g_ui->model.addChannelToColumn(ch.id, columnIndex); + g_ui->model.columns.addChannelToColumn(ch.id, columnIndex); if (ch.type == ChannelType::GROUP) for (const m::Channel& child : ch.groupChannel->channels->getAll()) - g_ui->model.addChannelToGroup(child.id, ch.id); + g_ui->model.columns.addChannelToGroup(child.id, ch.id); } /* -------------------------------------------------------------------------- */ void moveChannel(ID channelId, int newColumnIndex, int newPosition) { - g_ui->model.moveChannel(channelId, newColumnIndex, newPosition); + g_ui->model.columns.moveChannel(channelId, newColumnIndex, newPosition); g_ui->rebuild(); } @@ -282,7 +282,7 @@ void moveChannel(ID channelId, int newColumnIndex, int newPosition) void addColumn() { - g_ui->model.addColumn(); + g_ui->model.columns.addColumn(); g_ui->rebuild(); } @@ -292,7 +292,7 @@ void deleteColumn(int index) { if (g_ui->model.columns.size() == 1) // One column must stay return; - g_ui->model.removeColumn(index); + g_ui->model.columns.removeColumn(index); g_ui->rebuild(); } @@ -300,7 +300,7 @@ void deleteColumn(int index) void setColumnWidth(int index, int w) { - v::Model::Column& column = g_ui->model.getColumnByIndex(index); + v::Model::Column& column = g_ui->model.columns.getColumnByIndex(index); column.width = w; g_ui->rebuild(); diff --git a/src/glue/sampleEditor.cpp b/src/glue/sampleEditor.cpp index 1154b3dc7..81ba8513a 100644 --- a/src/glue/sampleEditor.cpp +++ b/src/glue/sampleEditor.cpp @@ -236,7 +236,7 @@ void toNewChannel(ID channelId, Frame a, Frame b) { const int columnIndex = g_ui->mainWindow->keyboard->getChannelColumnIndex(channelId); const m::Channel& newChannel = g_engine->getSampleEditorApi().toNewChannel(channelId, a, b); - g_ui->model.addChannelToColumn(newChannel.id, columnIndex); + g_ui->model.columns.addChannelToColumn(newChannel.id, columnIndex); } /* -------------------------------------------------------------------------- */ diff --git a/src/gui/model.cpp b/src/gui/model.cpp index 11a5bdf1f..ab070e8a9 100644 --- a/src/gui/model.cpp +++ b/src/gui/model.cpp @@ -51,6 +51,93 @@ Model::Column::Column(int width) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +Model::Column& Model::Columns::getColumnByIndex(std::size_t index) +{ + return getByIndex(index); +} + +/* -------------------------------------------------------------------------- */ + +Model::Column& Model::Columns::getColumnByChannelId(ID channelId) +{ + return *u::vector::findIfSafe(*this, [channelId](auto& col) + { return col.findById(channelId) != nullptr; }); +} + +/* -------------------------------------------------------------------------- */ + +void Model::Columns::addColumn() +{ + add({G_DEFAULT_COLUMN_WIDTH}); +} + +/* -------------------------------------------------------------------------- */ + +void Model::Columns::removeColumn(int columnIndex) +{ + removeByIndex(columnIndex); +} + +/* -------------------------------------------------------------------------- */ + +void Model::Columns::moveChannel(ID channelId, std::size_t newColumnIndex, int newPosition) +{ + Column& sourceColumn = getColumnByChannelId(channelId); + + if (sourceColumn.index == newColumnIndex) // If in same column + { + sourceColumn.moveById(channelId, newPosition); + } + else + { + Channel channel = sourceColumn.getById(channelId); + Column& targetColumn = getColumnByIndex(newColumnIndex); + sourceColumn.removeById(channelId); + targetColumn.insert(std::move(channel), newPosition); + } +} + +/* -------------------------------------------------------------------------- */ + +void Model::Columns::addChannelToColumn(ID channelId, std::size_t columnIndex, int position) +{ + Column& column = getColumnByIndex(columnIndex); + Channel channel = {channelId, /*groupId=*/0, columnIndex}; + if (position == -1) + column.add(std::move(channel)); + else + column.insert(std::move(channel), position); +} + +/* -------------------------------------------------------------------------- */ + +void Model::Columns::addChannelToGroup(ID channelId, ID groupId, int position) +{ + Column& column = getColumnByChannelId(groupId); + Channel& group = column.getById(groupId); + Channel channel = {channelId, groupId, column.index}; + if (position == -1) + group.add(std::move(channel)); + else + group.insert(std::move(channel), position); +} + +/* -------------------------------------------------------------------------- */ + +void Model::Columns::removeChannelFromColumn(ID channelId) +{ + for (Column& column : *this) // Brute force! + { + column.removeById(channelId); + for (Channel& channel : column) + channel.removeById(channelId); + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + Model::Model() { reset(); @@ -179,102 +266,17 @@ void Model::load(const m::Patch& patch) /* -------------------------------------------------------------------------- */ -Model::Column& Model::getColumnByIndex(std::size_t index) -{ - return columns.getByIndex(index); -} - -/* -------------------------------------------------------------------------- */ - -Model::Column& Model::getColumnByChannelId(ID channelId) -{ - return *u::vector::findIfSafe(columns, [channelId](auto& col) - { return col.findById(channelId) != nullptr; }); -} - -/* -------------------------------------------------------------------------- */ - -void Model::addColumn() -{ - columns.add({G_DEFAULT_COLUMN_WIDTH}); -} - -/* -------------------------------------------------------------------------- */ - -void Model::removeColumn(int columnIndex) -{ - columns.removeByIndex(columnIndex); -} - -/* -------------------------------------------------------------------------- */ - -void Model::moveChannel(ID channelId, std::size_t newColumnIndex, int newPosition) -{ - Column& sourceColumn = getColumnByChannelId(channelId); - - if (sourceColumn.index == newColumnIndex) // If in same column - { - sourceColumn.moveById(channelId, newPosition); - } - else - { - Channel channel = sourceColumn.getById(channelId); - Column& targetColumn = getColumnByIndex(newColumnIndex); - sourceColumn.removeById(channelId); - targetColumn.insert(std::move(channel), newPosition); - } -} - -/* -------------------------------------------------------------------------- */ - -void Model::addChannelToColumn(ID channelId, std::size_t columnIndex, int position) -{ - Column& column = getColumnByIndex(columnIndex); - Channel channel = {channelId, /*groupId=*/0, columnIndex}; - if (position == -1) - column.add(std::move(channel)); - else - column.insert(std::move(channel), position); -} - -/* -------------------------------------------------------------------------- */ - -void Model::addChannelToGroup(ID channelId, ID groupId, int position) -{ - Column& column = getColumnByChannelId(groupId); - Channel& group = column.getById(groupId); - Channel channel = {channelId, groupId, column.index}; - if (position == -1) - group.add(std::move(channel)); - else - group.insert(std::move(channel), position); -} - -/* -------------------------------------------------------------------------- */ - -void Model::removeChannelFromColumn(ID channelId) -{ - for (Column& column : columns) // Brute force! - { - column.removeById(channelId); - for (Channel& channel : column) - channel.removeById(channelId); - } -} - -/* -------------------------------------------------------------------------- */ - void Model::reset() { columns.clear(); /* Add 6 empty columns as initial layout. */ - addColumn(); - addColumn(); - addColumn(); - addColumn(); - addColumn(); - addColumn(); + columns.addColumn(); + columns.addColumn(); + columns.addColumn(); + columns.addColumn(); + columns.addColumn(); + columns.addColumn(); } } // namespace giada::v diff --git a/src/gui/model.h b/src/gui/model.h index fa3a82625..4417f2616 100644 --- a/src/gui/model.h +++ b/src/gui/model.h @@ -40,9 +40,6 @@ namespace giada::v { struct Model { - template - using Columns = mcl::Container; - struct Channel : public mcl::Container { Channel(ID id, ID groupId, std::size_t columnIndex); @@ -58,21 +55,24 @@ struct Model int width; }; + struct Columns : mcl::Container + { + Column& getColumnByIndex(std::size_t); + Column& getColumnByChannelId(ID); + void addColumn(); + void removeColumn(int columnIndex); + void moveChannel(ID channelId, std::size_t newColumnIndex, int newPosition); + void addChannelToColumn(ID channelId, std::size_t columnIndex, int position = -1); + void addChannelToGroup(ID channelId, ID groupId, int position = -1); + void removeChannelFromColumn(ID channelId); + }; + Model(); void store(m::Conf&) const; void store(m::Patch&) const; - - void load(const m::Conf&); - void load(const m::Patch&); - Column& getColumnByIndex(std::size_t); - Column& getColumnByChannelId(ID); - void addColumn(); - void removeColumn(int columnIndex); - void moveChannel(ID channelId, std::size_t newColumnIndex, int newPosition); - void addChannelToColumn(ID channelId, std::size_t columnIndex, int position = -1); - void addChannelToGroup(ID channelId, ID groupId, int position = -1); - void removeChannelFromColumn(ID channelId); + void load(const m::Conf&); + void load(const m::Patch&); /* reset Resets the Model to the latest state loaded from m::Conf. Call this when you @@ -120,7 +120,7 @@ struct Model float uiScaling = G_DEFAULT_UI_SCALING; - Columns columns; + Columns columns; }; } // namespace giada::v