Skip to content

Commit

Permalink
mcl-container-based v::Model::Columns class
Browse files Browse the repository at this point in the history
  • Loading branch information
gvnnz committed Jun 9, 2024
1 parent ad098d9 commit 102b572
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/glue/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Data getData(ID channelId)
std::vector<Column> getColumns()
{
std::vector<Column> out;
for (const v::Model::Column& modelColumn : g_ui->model.columns.getAll()) // Model::columns is the source of truth
for (const v::Model::Column& modelColumn : g_ui->model.columns) // Model::columns is the source of truth
out.push_back(makeColumn_(modelColumn));
return out;
}
Expand Down Expand Up @@ -278,7 +278,7 @@ void addColumn()

void deleteColumn(int index)
{
if (g_ui->model.columns.getAll().size() == 1) // One column must stay
if (g_ui->model.columns.size() == 1) // One column must stay
return;
g_ui->model.columns.removeColumn(index);
g_ui->rebuild();
Expand Down
35 changes: 7 additions & 28 deletions src/gui/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,17 @@ void Model::Column::rebuildIds()
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

const std::vector<Model::Column>& Model::Columns::getAll() const
{

return m_columns;
}

/* -------------------------------------------------------------------------- */

Model::Column& Model::Columns::getColumnByIndex(int index)
{
assert(index < static_cast<int>(m_columns.size()));

return m_columns.at(index);
return getByIndex(index);
}

/* -------------------------------------------------------------------------- */

Model::Column& Model::Columns::getColumnByChannelId(ID channelId)
{
const auto p = [channelId](auto& col)
{
return u::vector::has(col.getChannels(), [channelId](ID otherId)
{ return channelId == otherId; });
};
return *u::vector::findIfSafe(m_columns, p);
return getIf([channelId](const Column& column)
{ return column.contains(channelId); });
}

/* -------------------------------------------------------------------------- */
Expand All @@ -135,14 +121,14 @@ void Model::Columns::addDefaultColumn()

void Model::Columns::addColumn(Column&& column)
{
m_columns.push_back(std::move(column));
add(std::move(column));
}

/* -------------------------------------------------------------------------- */

void Model::Columns::removeColumn(int columnIndex)
{
m_columns.erase(m_columns.begin() + columnIndex);
removeByIndex(columnIndex);
}

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -185,17 +171,10 @@ void Model::Columns::addChannelToGroup(ID channelId, ID groupId)

void Model::Columns::removeChannel(ID channelId)
{
for (Column& column : m_columns) // Brute force!
for (Column& column : *this) // Brute force!
column.removeChannel(channelId);
}

/* -------------------------------------------------------------------------- */

void Model::Columns::clear()
{
m_columns.clear();
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -256,7 +235,7 @@ void Model::store(m::Patch& patch) const
{
patch.name = projectName;

for (const Column& column : columns.getAll())
for (const Column& column : columns)
{
m::Patch::Column pcolumn;
pcolumn.width = column.width;
Expand Down
8 changes: 1 addition & 7 deletions src/gui/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ struct Model
std::vector<ID> m_channelIds;
};

class Columns
class Columns : public mcl::Container<Column>
{
public:
const std::vector<Column>& getAll() const;

Column& getColumnByIndex(int);
Column& getColumnByChannelId(ID);
void addDefaultColumn();
Expand All @@ -77,10 +75,6 @@ struct Model
void addChannelToColumn(ID channelId, int columnIndex, int position = -1);
void addChannelToGroup(ID channelId, ID groupId);
void removeChannel(ID channelId);
void clear();

private:
std::vector<Column> m_columns;
};

Model();
Expand Down

0 comments on commit 102b572

Please sign in to comment.