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 24, 2024
1 parent 03adc20 commit bcd004d
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 119 deletions.
28 changes: 14 additions & 14 deletions src/glue/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ void printLoadError_(int res)
Data makeData_(ID channelId, const v::Model::Column& column)
{
const int position = column.getChannelIndex(channelId);
const int columnIndex = g_ui->model.getColumnIndex(column);
const int columnIndex = g_ui->model.columns.getColumnIndex(column);
return Data(g_engine->getChannelsApi().get(channelId), columnIndex, position);
}

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

Column makeColumn_(const v::Model::Column& modelColumn)
{
Column column{g_ui->model.getColumnIndex(modelColumn), modelColumn.width, {}};
Column column{g_ui->model.columns.getColumnIndex(modelColumn), modelColumn.width, {}};

for (const ID channelId : modelColumn.channels)
column.channels.push_back(makeData_(channelId, g_ui->model.getColumnByChannelId(channelId)));
column.channels.push_back(makeData_(channelId, g_ui->model.columns.getColumnByChannelId(channelId)));

return column;
}
Expand Down Expand Up @@ -153,13 +153,13 @@ bool Data::isArmed() const { return g_engine->getChannelsApi().get(id).

Data getData(ID channelId)
{
return makeData_(channelId, g_ui->model.getColumnByChannelId(channelId));
return makeData_(channelId, g_ui->model.columns.getColumnByChannelId(channelId));
}

std::vector<Column> getColumns()
{
std::vector<Column> out;
for (const v::Model::Column& modelColumn : g_ui->model.columns) // Model::columns is the source of truth
for (const v::Model::Column& modelColumn : g_ui->model.columns.getAll()) // Model::columns is the source of truth
out.push_back(makeColumn_(modelColumn));
return out;
}
Expand All @@ -183,7 +183,7 @@ void loadChannel(ID channelId, const std::string& fname)
void addChannel(int columnIndex, ChannelType type)
{
const m::Channel& ch = g_engine->getChannelsApi().add(type);
g_ui->model.addChannelToColumn(ch.id, columnIndex);
g_ui->model.columns.addChannelToColumn(ch.id, columnIndex);
}

/* -------------------------------------------------------------------------- */
Expand All @@ -204,7 +204,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 @@ -219,7 +219,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 @@ -251,40 +251,40 @@ void setOverdubProtection(ID channelId, bool value)
void cloneChannel(ID channelId, int columnIndex)
{
const m::Channel& ch = g_engine->getChannelsApi().clone(channelId);
g_ui->model.addChannelToColumn(ch.id, columnIndex);
g_ui->model.columns.addChannelToColumn(ch.id, columnIndex);
}

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

void moveChannel(ID channelId, int columnIndex, int newPosition)
{
g_ui->model.moveChannel(channelId, columnIndex, newPosition);
g_ui->model.columns.moveChannel(channelId, columnIndex, newPosition);
g_ui->rebuild();
}

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

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

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

void deleteColumn(int index)
{
if (g_ui->model.columns.size() == 1) // One column must stay
if (g_ui->model.columns.getAll().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
202 changes: 110 additions & 92 deletions src/gui/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,108 @@ int Model::Column::getChannelIndex(ID channelId) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

int Model::Columns::getColumnIndex(const Column& target) const
{
for (int i = 0; const Column& column : m_columns)
{
if (&target == &column)
return i;
i++;
}
assert(false);
return -1;
}

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

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);
}

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

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

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

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

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

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

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

void Model::Columns::moveChannel(ID channelId, int columnIndex, int newPosition)
{
const Column& column = getColumnByChannelId(channelId);

if (getColumnIndex(column) == columnIndex) // If in same column
{
const int oldPosition = column.getChannelIndex(channelId);
if (newPosition >= oldPosition) // If moved below, readjust index
newPosition -= 1;
}

removeChannelFromColumn(channelId);
addChannelToColumn(channelId, columnIndex, newPosition);
}

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

void Model::Columns::addChannelToColumn(ID channelId, int columnIndex, int position)
{
std::vector<ID>& channels = getColumnByIndex(columnIndex).channels;
if (position == -1)
channels.push_back(channelId);
else
channels.insert(channels.begin() + position, channelId);
}

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

void Model::Columns::removeChannelFromColumn(ID channelId)
{
for (Column& column : m_columns) // Brute force!
u::vector::remove(column.channels, channelId);
}

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

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

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

Model::Model()
{
reset();
Expand Down Expand Up @@ -96,7 +198,7 @@ void Model::store(m::Patch& patch) const
{
patch.name = projectName;

for (const Column& column : columns)
for (const Column& column : columns.getAll())
{
m::Patch::Column pcolumn;
pcolumn.width = column.width;
Expand All @@ -108,20 +210,6 @@ void Model::store(m::Patch& patch) const

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

int Model::getColumnIndex(const Column& target) const
{
for (int i = 0; const Column& column : columns)
{
if (&target == &column)
return i;
i++;
}
assert(false);
return -1;
}

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

void Model::load(const m::Conf& conf)
{
logMode = conf.logMode;
Expand Down Expand Up @@ -173,95 +261,25 @@ void Model::load(const m::Patch& patch)
Column column{.width = pcolumn.width};
for (ID channelId : pcolumn.channels)
column.channels.push_back(channelId);
addColumn(std::move(column));
columns.addColumn(std::move(column));
}

projectName = patch.name;
}

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

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

return columns.at(index);
}

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

Model::Column& Model::getColumnByChannelId(ID channelId)
{
const auto p = [channelId](auto& col)
{
return u::vector::has(col.channels, [channelId](ID otherId)
{ return channelId == otherId; });
};
return *u::vector::findIfSafe(columns, p);
}

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

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

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

void Model::removeColumn(int columnIndex)
{
columns.erase(columns.begin() + columnIndex);
}

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

void Model::moveChannel(ID channelId, int columnIndex, int newPosition)
{
const Column& column = getColumnByChannelId(channelId);

if (getColumnIndex(column) == columnIndex) // If in same column
{
const int oldPosition = column.getChannelIndex(channelId);
if (newPosition >= oldPosition) // If moved below, readjust index
newPosition -= 1;
}

removeChannelFromColumn(channelId);
addChannelToColumn(channelId, columnIndex, newPosition);
}

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

void Model::addChannelToColumn(ID channelId, int columnIndex, int position)
{
std::vector<ID>& channels = getColumnByIndex(columnIndex).channels;
if (position == -1)
channels.push_back(channelId);
else
channels.insert(channels.begin() + position, channelId);
}

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

void Model::removeChannelFromColumn(ID channelId)
{
for (Column& column : columns) // Brute force!
u::vector::remove(column.channels, channelId);
}
/* -------------------------------------------------------------------------- */

void Model::reset()
{
columns.clear();

/* Add 6 empty columns as initial layout. */

addColumn({G_DEFAULT_COLUMN_WIDTH});
addColumn({G_DEFAULT_COLUMN_WIDTH});
addColumn({G_DEFAULT_COLUMN_WIDTH});
addColumn({G_DEFAULT_COLUMN_WIDTH});
addColumn({G_DEFAULT_COLUMN_WIDTH});
addColumn({G_DEFAULT_COLUMN_WIDTH});
columns.addColumn({G_DEFAULT_COLUMN_WIDTH});
columns.addColumn({G_DEFAULT_COLUMN_WIDTH});
columns.addColumn({G_DEFAULT_COLUMN_WIDTH});
columns.addColumn({G_DEFAULT_COLUMN_WIDTH});
columns.addColumn({G_DEFAULT_COLUMN_WIDTH});
columns.addColumn({G_DEFAULT_COLUMN_WIDTH});
}
} // namespace giada::v
Loading

0 comments on commit bcd004d

Please sign in to comment.