Skip to content

Commit

Permalink
Use std::optional for colour interfaces and storage (LMMS#6991)
Browse files Browse the repository at this point in the history
  • Loading branch information
DomClark authored Nov 19, 2023
1 parent aa050ae commit dc8c49a
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 189 deletions.
25 changes: 5 additions & 20 deletions include/Clip.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef LMMS_CLIP_H
#define LMMS_CLIP_H

#include <optional>

#include <QColor>

#include "AutomatableModel.h"
Expand Down Expand Up @@ -109,24 +111,8 @@ class LMMS_EXPORT Clip : public Model, public JournallingObject
return m_autoResize;
}

QColor color() const
{
return m_color;
}

void setColor( const QColor & c )
{
m_color = c;
}

bool hasColor();

void useCustomClipColor( bool b );

bool usesCustomClipColor()
{
return m_useCustomClipColor;
}
auto color() const -> const std::optional<QColor>& { return m_color; }
void setColor(const std::optional<QColor>& color);

virtual void movePosition( const TimePos & pos );
virtual void changeLength( const TimePos & length );
Expand Down Expand Up @@ -177,8 +163,7 @@ public slots:

bool m_selectViewOnCreate;

QColor m_color;
bool m_useCustomClipColor;
std::optional<QColor> m_color;

friend class ClipView;

Expand Down
4 changes: 3 additions & 1 deletion include/ClipView.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef LMMS_GUI_CLIP_VIEW_H
#define LMMS_GUI_CLIP_VIEW_H

#include <optional>

#include <QVector>

Expand Down Expand Up @@ -184,6 +185,7 @@ public slots:

virtual void paintTextLabel(QString const & text, QPainter & painter);

auto hasCustomColor() const -> bool;

protected slots:
void updateLength();
Expand Down Expand Up @@ -241,7 +243,7 @@ protected slots:
bool mouseMovedDistance( QMouseEvent * me, int distance );
TimePos draggedClipPos( QMouseEvent * me );
int knifeMarkerPos( QMouseEvent * me );
void setColor(const QColor* color);
void setColor(const std::optional<QColor>& color);
//! Return true iff the clip could be split. Currently only implemented for samples
virtual bool splitClip( const TimePos pos ){ return false; };
void updateCursor(QMouseEvent * me);
Expand Down
11 changes: 4 additions & 7 deletions include/Mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,18 @@ class MixerChannel : public ThreadableJob
bool requiresProcessing() const override { return true; }
void unmuteForSolo();

void setColor (QColor newColor)
{
m_color = newColor;
}

std::optional<QColor> m_color;
auto color() const -> const std::optional<QColor>& { return m_color; }
void setColor(const std::optional<QColor>& color) { m_color = color; }

std::atomic_int m_dependenciesMet;
void incrementDeps();
void processed();

private:
void doProcessing() override;
};

std::optional<QColor> m_color;
};

class MixerRoute : public QObject
{
Expand Down
15 changes: 3 additions & 12 deletions include/Track.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,9 @@ class LMMS_EXPORT Track : public Model, public JournallingObject
{
return m_processingLock.tryLock();
}

QColor color()
{
return m_color.value();
}
bool useColor()
{
return m_color.has_value();
}

auto color() const -> const std::optional<QColor>& { return m_color; }
void setColor(const std::optional<QColor>& color);

bool isMutedBeforeSolo() const
{
Expand All @@ -220,9 +214,6 @@ public slots:

void toggleSolo();

void setColor(const QColor& c);
void resetColor();

private:
TrackContainer* m_trackContainer;
Type m_type;
Expand Down
11 changes: 5 additions & 6 deletions src/core/AutomationClip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,10 +830,10 @@ void AutomationClip::saveSettings( QDomDocument & _doc, QDomElement & _this )
_this.setAttribute( "prog", QString::number( static_cast<int>(progressionType()) ) );
_this.setAttribute( "tens", QString::number( getTension() ) );
_this.setAttribute( "mute", QString::number( isMuted() ) );
if( usesCustomClipColor() )

if (const auto& c = color())
{
_this.setAttribute( "color", color().name() );
_this.setAttribute("color", c->name());
}

for( timeMap::const_iterator it = m_timeMap.begin();
Expand Down Expand Up @@ -919,10 +919,9 @@ void AutomationClip::loadSettings( const QDomElement & _this )
}
}

if( _this.hasAttribute( "color" ) )
if (_this.hasAttribute("color"))
{
useCustomClipColor( true );
setColor( _this.attribute( "color" ) );
setColor(QColor{_this.attribute("color")});
}

int len = _this.attribute( "len" ).toInt();
Expand Down
17 changes: 3 additions & 14 deletions src/core/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ Clip::Clip( Track * track ) :
m_startPosition(),
m_length(),
m_mutedModel( false, this, tr( "Mute" ) ),
m_selectViewOnCreate( false ),
m_color( 128, 128, 128 ),
m_useCustomClipColor( false )
m_selectViewOnCreate{false}
{
if( getTrack() )
{
Expand Down Expand Up @@ -185,19 +183,10 @@ void Clip::setStartTimeOffset( const TimePos &startTimeOffset )
m_startTimeOffset = startTimeOffset;
}



void Clip::useCustomClipColor( bool b )
void Clip::setColor(const std::optional<QColor>& color)
{
if (b == m_useCustomClipColor) { return; }
m_useCustomClipColor = b;
m_color = color;
emit colorChanged();
}


bool Clip::hasColor()
{
return usesCustomClipColor() || getTrack()->useColor();
}

} // namespace lmms
8 changes: 4 additions & 4 deletions src/core/Mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ void Mixer::clearChannel(mix_ch_t index)
ch->m_volumeModel.setDisplayName( ch->m_name + ">" + tr( "Volume" ) );
ch->m_muteModel.setDisplayName( ch->m_name + ">" + tr( "Mute" ) );
ch->m_soloModel.setDisplayName( ch->m_name + ">" + tr( "Solo" ) );
ch->m_color = std::nullopt;
ch->setColor(std::nullopt);

// send only to master
if( index > 0)
Expand Down Expand Up @@ -759,7 +759,7 @@ void Mixer::saveSettings( QDomDocument & _doc, QDomElement & _this )
ch->m_soloModel.saveSettings( _doc, mixch, "soloed" );
mixch.setAttribute( "num", i );
mixch.setAttribute( "name", ch->m_name );
if (ch->m_color.has_value()) { mixch.setAttribute("color", ch->m_color->name()); }
if (const auto& color = ch->color()) { mixch.setAttribute("color", color->name()); }

// add the channel sends
for (const auto& send : ch->m_sends)
Expand Down Expand Up @@ -805,9 +805,9 @@ void Mixer::loadSettings( const QDomElement & _this )
m_mixerChannels[num]->m_muteModel.loadSettings( mixch, "muted" );
m_mixerChannels[num]->m_soloModel.loadSettings( mixch, "soloed" );
m_mixerChannels[num]->m_name = mixch.attribute( "name" );
if( mixch.hasAttribute( "color" ) )
if (mixch.hasAttribute("color"))
{
m_mixerChannels[num]->m_color = QColor(mixch.attribute("color"));
m_mixerChannels[num]->setColor(QColor{mixch.attribute("color")});
}

m_mixerChannels[num]->m_fxChain.restoreState( mixch.firstChildElement(
Expand Down
16 changes: 5 additions & 11 deletions src/core/PatternClip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ void PatternClip::saveSettings(QDomDocument& doc, QDomElement& element)
element.setAttribute( "len", length() );
element.setAttribute("off", startTimeOffset());
element.setAttribute( "muted", isMuted() );
if( usesCustomClipColor() )
if (const auto& c = color())
{
element.setAttribute( "color", color().name() );
element.setAttribute("color", c->name());
}
}

Expand All @@ -90,20 +90,14 @@ void PatternClip::loadSettings(const QDomElement& element)
if (!element.hasAttribute("usestyle"))
{
// for colors saved in 1.3-onwards
setColor(element.attribute("color"));
useCustomClipColor(true);
setColor(QColor{element.attribute("color")});
}
else
else if (element.attribute("usestyle").toUInt() == 0)
{
// for colors saved before 1.3
setColor(QColor(element.attribute("color").toUInt()));
useCustomClipColor(element.attribute("usestyle").toUInt() == 0);
setColor(QColor{element.attribute("color").toUInt()});
}
}
else
{
useCustomClipColor(false);
}
}


Expand Down
13 changes: 4 additions & 9 deletions src/core/SampleClip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ void SampleClip::saveSettings( QDomDocument & _doc, QDomElement & _this )
}

_this.setAttribute( "sample_rate", m_sampleBuffer->sampleRate());
if( usesCustomClipColor() )
if (const auto& c = color())
{
_this.setAttribute( "color", color().name() );
_this.setAttribute("color", c->name());
}
if (m_sampleBuffer->reversed())
{
Expand Down Expand Up @@ -304,14 +304,9 @@ void SampleClip::loadSettings( const QDomElement & _this )
setMuted( _this.attribute( "muted" ).toInt() );
setStartTimeOffset( _this.attribute( "off" ).toInt() );

if( _this.hasAttribute( "color" ) )
if (_this.hasAttribute("color"))
{
useCustomClipColor( true );
setColor( _this.attribute( "color" ) );
}
else
{
useCustomClipColor(false);
setColor(QColor{_this.attribute("color")});
}

if(_this.hasAttribute("reversed"))
Expand Down
23 changes: 5 additions & 18 deletions src/core/Track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ Track::Track( Type type, TrackContainer * tc ) :
m_mutedModel( false, this, tr( "Mute" ) ), /*!< For controlling track muting */
m_soloModel( false, this, tr( "Solo" ) ), /*!< For controlling track soloing */
m_simpleSerializingMode( false ),
m_clips(), /*!< The clips (segments) */
m_color(std::nullopt)
m_clips() /*!< The clips (segments) */
{
m_trackContainer->addTrack( this );
m_height = -1;
Expand Down Expand Up @@ -263,14 +262,9 @@ void Track::loadSettings( const QDomElement & element )
// Older project files that didn't have this attribute will set the value to false (issue 5562)
m_mutedBeforeSolo = QVariant( element.attribute( "mutedBeforeSolo", "0" ) ).toBool();

if( element.hasAttribute( "color" ) )
if (element.hasAttribute("color"))
{
QColor newColor = QColor(element.attribute("color"));
setColor(newColor);
}
else
{
resetColor();
setColor(QColor{element.attribute("color")});
}

if( m_simpleSerializingMode )
Expand Down Expand Up @@ -633,19 +627,12 @@ void Track::toggleSolo()
}
}

void Track::setColor(const QColor& c)
void Track::setColor(const std::optional<QColor>& color)
{
m_color = c;
m_color = color;
emit colorChanged();
}

void Track::resetColor()
{
m_color = std::nullopt;
emit colorChanged();
}


BoolModel *Track::getMutedModel()
{
return &m_mutedModel;
Expand Down
24 changes: 11 additions & 13 deletions src/gui/MixerLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ void MixerLine::drawMixerLine( QPainter* p, const MixerLine *mixerLine, bool isA
int width = mixerLine->rect().width();
int height = mixerLine->rect().height();

if (channel->m_color.has_value() && !muted)
if (channel->color().has_value() && !muted)
{
p->fillRect(mixerLine->rect(), channel->m_color->darker(isActive ? 120 : 150));
p->fillRect(mixerLine->rect(), channel->color()->darker(isActive ? 120 : 150));
}
else
{
Expand Down Expand Up @@ -415,36 +415,34 @@ void MixerLine::setStrokeInnerInactive( const QColor & c )
m_strokeInnerInactive = c;
}


// Ask user for a color, and set it as the mixer line color
void MixerLine::selectColor()
{
auto channel = Engine::mixer()->mixerChannel( m_channelIndex );
auto new_color = ColorChooser(this).withPalette(ColorChooser::Palette::Mixer)->getColor(channel->m_color.value_or(backgroundActive().color()));
if(!new_color.isValid()) { return; }
channel->setColor (new_color);
const auto channel = Engine::mixer()->mixerChannel(m_channelIndex);
const auto newColor = ColorChooser{this}
.withPalette(ColorChooser::Palette::Mixer)
->getColor(channel->color().value_or(backgroundActive().color()));
if (!newColor.isValid()) { return; }
channel->setColor(newColor);
Engine::getSong()->setModified();
update();
}


// Disable the usage of color on this mixer line
void MixerLine::resetColor()
{
Engine::mixer()->mixerChannel(m_channelIndex)->m_color = std::nullopt;
Engine::mixer()->mixerChannel(m_channelIndex)->setColor(std::nullopt);
Engine::getSong()->setModified();
update();
}


// Pick a random color from the mixer palette and set it as our color
void MixerLine::randomizeColor()
{
auto channel = Engine::mixer()->mixerChannel( m_channelIndex );
channel->setColor (ColorChooser::getPalette(ColorChooser::Palette::Mixer)[rand() % 48]);
const auto channel = Engine::mixer()->mixerChannel(m_channelIndex);
channel->setColor(ColorChooser::getPalette(ColorChooser::Palette::Mixer)[std::rand() % 48]);
Engine::getSong()->setModified();
update();
}


} // namespace lmms::gui
Loading

0 comments on commit dc8c49a

Please sign in to comment.