Skip to content

Commit

Permalink
Use proper class for v::Model::Columns
Browse files Browse the repository at this point in the history
  • Loading branch information
gvnnz committed May 15, 2024
1 parent 2fea159 commit 74dcdf5
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 119 deletions.
24 changes: 12 additions & 12 deletions src/glue/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

/* -------------------------------------------------------------------------- */
Expand All @@ -212,7 +212,7 @@ void addAndLoadChannels(int columnIndex, const std::vector<std::string>& 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)
Expand All @@ -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);
}

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -261,28 +261,28 @@ 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();
}

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

void addColumn()
{
g_ui->model.addColumn();
g_ui->model.columns.addColumn();
g_ui->rebuild();
}

Expand All @@ -292,15 +292,15 @@ 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();
}

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

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();
Expand Down
2 changes: 1 addition & 1 deletion src/glue/sampleEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/* -------------------------------------------------------------------------- */
Expand Down
184 changes: 93 additions & 91 deletions src/gui/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
30 changes: 15 additions & 15 deletions src/gui/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ namespace giada::v
{
struct Model
{
template <typename T>
using Columns = mcl::Container<T>;

struct Channel : public mcl::Container<Channel, /*Identifiable=*/true, /*Sortable=*/true>
{
Channel(ID id, ID groupId, std::size_t columnIndex);
Expand All @@ -58,21 +55,24 @@ struct Model
int width;
};

struct Columns : mcl::Container<Column>
{
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
Expand Down Expand Up @@ -120,7 +120,7 @@ struct Model

float uiScaling = G_DEFAULT_UI_SCALING;

Columns<Column> columns;
Columns columns;
};
} // namespace giada::v

Expand Down

0 comments on commit 74dcdf5

Please sign in to comment.