From 85399c12c266a36fdf0777254a3c39c85c4d3877 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sat, 13 Jan 2024 22:03:03 +0100 Subject: [PATCH] Abstraction in MixerChannelView (#7057) Reduce code repetition in `MixerChannelView` by introducing methods that: * Retrieve the `MixerChannel` that's associated with the view * Check if the associated channel is the master channel This abstracts some functionality and also reduces direct usage of the variable `m_channelIndex`. --- include/MixerChannelView.h | 7 +++++++ src/gui/MixerChannelView.cpp | 29 +++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/include/MixerChannelView.h b/include/MixerChannelView.h index 8d2306f91..1710623d7 100644 --- a/include/MixerChannelView.h +++ b/include/MixerChannelView.h @@ -38,6 +38,11 @@ #include #include +namespace lmms +{ + class MixerChannel; +} + namespace lmms::gui { constexpr int MIXER_CHANNEL_INNER_BORDER_SIZE = 3; @@ -100,6 +105,8 @@ namespace lmms::gui private: QString elideName(const QString& name); + MixerChannel* mixerChannel() const; + auto isMasterChannel() const -> bool { return m_channelIndex == 0; } private: SendButtonIndicator* m_sendButton; diff --git a/src/gui/MixerChannelView.cpp b/src/gui/MixerChannelView.cpp index 0f8ccedea..9ea266238 100644 --- a/src/gui/MixerChannelView.cpp +++ b/src/gui/MixerChannelView.cpp @@ -144,9 +144,9 @@ namespace lmms::gui void MixerChannelView::contextMenuEvent(QContextMenuEvent*) { - auto contextMenu = new CaptionMenu(Engine::mixer()->mixerChannel(m_channelIndex)->m_name, this); + auto contextMenu = new CaptionMenu(mixerChannel()->m_name, this); - if (m_channelIndex != 0) // no move-options in master + if (!isMasterChannel()) // no move-options in master { contextMenu->addAction(tr("Move &left"), this, &MixerChannelView::moveChannelLeft); contextMenu->addAction(tr("Move &right"), this, &MixerChannelView::moveChannelRight); @@ -155,7 +155,7 @@ namespace lmms::gui contextMenu->addAction(tr("Rename &channel"), this, &MixerChannelView::renameChannel); contextMenu->addSeparator(); - if (m_channelIndex != 0) // no remove-option in master + if (!isMasterChannel()) // no remove-option in master { contextMenu->addAction(embed::getIconPixmap("cancel"), tr("R&emove channel"), this, &MixerChannelView::removeChannel); contextMenu->addSeparator(); @@ -178,7 +178,7 @@ namespace lmms::gui void MixerChannelView::paintEvent(QPaintEvent* event) { auto * mixer = Engine::mixer(); - const auto channel = mixer->mixerChannel(m_channelIndex); + const auto channel = mixerChannel(); const bool muted = channel->m_muteModel.value(); const auto name = channel->m_name; const auto elidedName = elideName(name); @@ -351,7 +351,7 @@ namespace lmms::gui m_channelNumberLcd->hide(); m_renameLineEdit->setFixedWidth(m_renameLineEdit->width()); - m_renameLineEdit->setText(Engine::mixer()->mixerChannel(m_channelIndex)->m_name); + m_renameLineEdit->setText(mixerChannel()->m_name); m_renameLineEditView->setFocus(); m_renameLineEdit->selectAll(); @@ -370,27 +370,27 @@ namespace lmms::gui auto newName = m_renameLineEdit->text(); setFocus(); - const auto mixerChannel = Engine::mixer()->mixerChannel(m_channelIndex); - if (!newName.isEmpty() && mixerChannel->m_name != newName) + const auto mc = mixerChannel(); + if (!newName.isEmpty() && mc->m_name != newName) { - mixerChannel->m_name = newName; + mc->m_name = newName; m_renameLineEdit->setText(elideName(newName)); Engine::getSong()->setModified(); } - setToolTip(mixerChannel->m_name); + setToolTip(mc->m_name); } void MixerChannelView::resetColor() { - Engine::mixer()->mixerChannel(m_channelIndex)->setColor(std::nullopt); + mixerChannel()->setColor(std::nullopt); Engine::getSong()->setModified(); update(); } void MixerChannelView::selectColor() { - const auto channel = Engine::mixer()->mixerChannel(m_channelIndex); + const auto channel = mixerChannel(); const auto initialColor = channel->color().value_or(backgroundActive().color()); const auto * colorChooser = ColorChooser{this}.withPalette(ColorChooser::Palette::Mixer); @@ -406,7 +406,7 @@ namespace lmms::gui void MixerChannelView::randomizeColor() { - auto channel = Engine::mixer()->mixerChannel(m_channelIndex); + auto channel = mixerChannel(); channel->setColor(ColorChooser::getPalette(ColorChooser::Palette::Mixer)[rand() % 48]); Engine::getSong()->setModified(); update(); @@ -444,4 +444,9 @@ namespace lmms::gui return elidedName; } + MixerChannel* MixerChannelView::mixerChannel() const + { + return Engine::mixer()->mixerChannel(m_channelIndex); + } + } // namespace lmms::gui \ No newline at end of file