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`.
This commit is contained in:
Michael Gregorius
2024-01-13 22:03:03 +01:00
committed by GitHub
parent d945ac1cbe
commit 85399c12c2
2 changed files with 24 additions and 12 deletions

View File

@@ -38,6 +38,11 @@
#include <QPixmap>
#include <QWidget>
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;

View File

@@ -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