Skip to content

Commit

Permalink
Use mc::container for Model::Column
Browse files Browse the repository at this point in the history
  • Loading branch information
gvnnz committed Apr 23, 2024
1 parent 28dd15f commit 70f3f38
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/glue/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Column makeColumn_(const v::Model::Column& modelColumn)
{
Column column{modelColumn.index, modelColumn.width, {}};

for (const v::Model::Channel& channel : modelColumn.channels.getAll())
for (const v::Model::Channel& channel : modelColumn)
{
column.channels.push_back(makeData_(channel));
for (const v::Model::Channel& child : channel.channels.getAll()) // for groups
Expand Down Expand Up @@ -157,7 +157,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::Channel& channel = column.channels.getById(channelId);
const v::Model::Channel& channel = column.getById(channelId);
return makeData_(channel);
}

Expand Down
2 changes: 1 addition & 1 deletion src/glue/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct Data

struct Column
{
int index;
std::size_t index;
int width;
std::vector<Data> channels;
};
Expand Down
34 changes: 17 additions & 17 deletions src/gui/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void Model::store(m::Patch& patch) const
{
m::Patch::Column pcolumn;
pcolumn.width = column.width;
for (const v::Model::Channel& channel : column.channels.getAll())
for (const v::Model::Channel& channel : column)
pcolumn.channels.push_back(channel.id);
patch.columns.push_back(pcolumn);
}
Expand Down Expand Up @@ -147,10 +147,10 @@ void Model::load(const m::Patch& patch)
columns.clear();
for (const m::Patch::Column& pcolumn : patch.columns)
{
int columnIndex = 0;
Column column{.width = pcolumn.width};
std::size_t columnIndex = 0;
Column column{pcolumn.width};
for (ID channelId : pcolumn.channels)
column.channels.add({channelId, columnIndex++});
column.add({channelId, columnIndex++});
columns.add(std::move(column));
}

Expand All @@ -159,7 +159,7 @@ void Model::load(const m::Patch& patch)

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

Model::Column& Model::getColumnByIndex(int index)
Model::Column& Model::getColumnByIndex(std::size_t index)
{
return columns.getByIndex(index);
}
Expand All @@ -169,7 +169,7 @@ Model::Column& Model::getColumnByIndex(int index)
Model::Column& Model::getColumnByChannelId(ID channelId)
{
return *u::vector::findIfSafe(columns, [channelId](auto& col)
{ return col.channels.findById(channelId) != nullptr; });
{ return col.findById(channelId) != nullptr; });
}

/* -------------------------------------------------------------------------- */
Expand All @@ -188,40 +188,40 @@ void Model::removeColumn(int columnIndex)

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

void Model::moveChannel(ID channelId, int newColumnIndex, int newPosition)
void Model::moveChannel(ID channelId, std::size_t newColumnIndex, int newPosition)
{
Column& sourceColumn = getColumnByChannelId(channelId);

if (sourceColumn.index == newColumnIndex) // If in same column
{
sourceColumn.channels.moveById(channelId, newPosition);
sourceColumn.moveById(channelId, newPosition);
}
else
{
Channel channel = sourceColumn.channels.getById(channelId);
Channel channel = sourceColumn.getById(channelId);
Column& targetColumn = getColumnByIndex(newColumnIndex);
sourceColumn.channels.removeById(channelId);
targetColumn.channels.insert(std::move(channel), newPosition);
sourceColumn.removeById(channelId);
targetColumn.insert(std::move(channel), newPosition);
}
}

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

void Model::addChannelToColumn(ID channelId, int columnIndex, int position)
void Model::addChannelToColumn(ID channelId, std::size_t columnIndex, int position)
{
Column& column = getColumnByIndex(columnIndex);
if (position == -1)
column.channels.add({channelId, columnIndex});
column.add({channelId, columnIndex});
else
column.channels.insert({channelId, columnIndex}, position);
column.insert({channelId, columnIndex}, position);
}

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

void Model::addChannelToGroup(ID channelId, ID groupId, int position)
{
Column& column = getColumnByChannelId(groupId);
Channel& group = column.channels.getById(groupId);
Channel& group = column.getById(groupId);
if (position == -1)
group.channels.add({channelId, column.index});
else
Expand All @@ -234,8 +234,8 @@ void Model::removeChannelFromColumn(ID channelId)
{
for (Column& column : columns) // Brute force!
{
column.channels.removeById(channelId);
for (Channel& channel : column.channels.getAll())
column.removeById(channelId);
for (Channel& channel : column)
channel.channels.removeById(channelId);
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/gui/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ struct Model
struct Channel
{
ID id;
int columnIndex;
std::size_t columnIndex;
int index = -1;
Channels<Channel> channels = {};
};

struct Column
struct Column : public mcl::Container<Channel, /*Identifiable=*/false, /*Sortable=*/true>
{
int width;
int index = -1;
Channels<Channel> channels = {};
Column(int width)
: width(width)
{
}

int width;
};

Model();
Expand All @@ -69,12 +72,12 @@ struct Model

void load(const m::Conf&);
void load(const m::Patch&);
Column& getColumnByIndex(int);
Column& getColumnByIndex(std::size_t);
Column& getColumnByChannelId(ID);
void addColumn();
void removeColumn(int columnIndex);
void moveChannel(ID channelId, int newColumnIndex, int newPosition);
void addChannelToColumn(ID channelId, int columnIndex, int position = -1);
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);

Expand Down

0 comments on commit 70f3f38

Please sign in to comment.