From dcdf5d9f36217ea16a099d14f53f531e6831c3bb Mon Sep 17 00:00:00 2001 From: Yann Collette Date: Thu, 5 Jun 2014 22:05:53 +0200 Subject: [PATCH] enable changing the color of a SampleTrack --- include/SampleBuffer.h | 15 +++++- include/SampleTrack.h | 38 +++++++++++-- include/track.h | 4 ++ src/core/SampleBuffer.cpp | 6 ++- src/core/track.cpp | 9 ++++ src/tracks/SampleTrack.cpp | 108 ++++++++++++++++++++++++++++++++++--- 6 files changed, 166 insertions(+), 14 deletions(-) diff --git a/include/SampleBuffer.h b/include/SampleBuffer.h index e0b5fff0fcc..6cffa7e2ae3 100644 --- a/include/SampleBuffer.h +++ b/include/SampleBuffer.h @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -255,7 +256,16 @@ class EXPORT SampleBuffer : public QObject, public sharedObject static QString tryToMakeRelative( const QString & _file ); static QString tryToMakeAbsolute( const QString & _file ); - + + void setColor(QColor _new_color) + { + m_color = _new_color.rgb(); + } + + QColor getColor() const + { + return m_color; + } public slots: void setAudioFile( const QString & _audio_file ); @@ -307,7 +317,8 @@ public slots: f_cnt_t getLoopedIndex( f_cnt_t _index, f_cnt_t _startf, f_cnt_t _endf ) const; f_cnt_t getPingPongIndex( f_cnt_t _index, f_cnt_t _startf, f_cnt_t _endf ) const; - + unsigned int m_color; + signals: void sampleUpdated(); diff --git a/include/SampleTrack.h b/include/SampleTrack.h index a2e8f5b7583..45c10e067a7 100644 --- a/include/SampleTrack.h +++ b/include/SampleTrack.h @@ -62,6 +62,22 @@ class SampleTCO : public trackContentObject virtual trackContentObjectView * createView( trackView * _tv ); + inline unsigned int fgColor() const + { + return( m_fg_color ); + } + inline static unsigned int defaultFgColor() + { + return qRgb( 128, 182, 175 ); + } + inline unsigned int bgColor() const + { + return( m_bg_color ); + } + inline static unsigned int defaultBgColor() + { + return qRgb( 128, 182, 175 ); + } public slots: void setSampleBuffer( SampleBuffer* sb ); @@ -73,6 +89,8 @@ public slots: private: SampleBuffer* m_sampleBuffer; BoolModel m_recordModel; + unsigned int m_fg_color; + unsigned int m_bg_color; friend class SampleTCOView; @@ -88,19 +106,31 @@ public slots: class SampleTCOView : public trackContentObjectView { Q_OBJECT - -// theming qproperties - Q_PROPERTY( QColor fgColor READ fgColor WRITE setFgColor ) - Q_PROPERTY( QColor textColor READ textColor WRITE setTextColor ) public: SampleTCOView( SampleTCO * _tco, trackView * _tv ); virtual ~SampleTCOView(); + QColor fgColor() const + { + return( m_tco->m_fg_color ); + } + void setFgColor( QColor _new_color ); + + QColor bgColor() const + { + return( m_tco->m_bg_color ); + } + void setBgColor( QColor _new_color ); + public slots: void updateSample(); +protected slots: + void changeFgColor(); + void changeBgColor(); + protected: virtual void contextMenuEvent( QContextMenuEvent * _cme ); diff --git a/include/track.h b/include/track.h index d07b2519172..cbf9e399029 100644 --- a/include/track.h +++ b/include/track.h @@ -179,6 +179,7 @@ class trackContentObjectView : public selectableObject, public ModelView // theming qproperties Q_PROPERTY( QColor fgColor READ fgColor WRITE setFgColor ) + Q_PROPERTY( QColor bgColor READ bgColor WRITE setBgColor ) Q_PROPERTY( QColor textColor READ textColor WRITE setTextColor ) public: @@ -193,8 +194,10 @@ class trackContentObjectView : public selectableObject, public ModelView } // qproperty access func QColor fgColor() const; + QColor bgColor() const; QColor textColor() const; void setFgColor( const QColor & _c ); + void setBgColor( const QColor & _c ); void setTextColor( const QColor & _c ); public slots: @@ -257,6 +260,7 @@ protected slots: // qproperty fields QColor m_fgColor; + QColor m_bgColor; QColor m_textColor; inline void setInitialMousePos( QPoint pos ) diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index 22d54c984f1..6ef846b9bed 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -917,7 +917,11 @@ void SampleBuffer::visualize( QPainter & _p, const QRect & _dr, const int yb = h / 2 + _dr.y(); const float y_space = h*0.25f; const int nb_frames = focus_on_range ? _to_frame - _from_frame : m_frames; - + + QColor c; + if (m_color) c = QColor(m_color); + else c = _p.pen().color(); + if( nb_frames < 60000 ) { _p.setRenderHint( QPainter::Antialiasing ); diff --git a/src/core/track.cpp b/src/core/track.cpp index 664fd67a210..59e15b3e2a1 100644 --- a/src/core/track.cpp +++ b/src/core/track.cpp @@ -253,6 +253,7 @@ trackContentObjectView::trackContentObjectView( trackContentObject * _tco, m_initialMouseGlobalPos( QPoint( 0, 0 ) ), m_hint( NULL ), m_fgColor( 0, 0, 0 ), + m_bgColor( 0, 0, 0 ), m_textColor( 0, 0, 0 ) { if( s_textFloat == NULL ) @@ -323,6 +324,10 @@ bool trackContentObjectView::fixedTCOs() QColor trackContentObjectView::fgColor() const { return m_fgColor; } +//! \brief CSS theming qproperty access method +QColor trackContentObjectView::bgColor() const +{ return m_bgColor; } + //! \brief CSS theming qproperty access method QColor trackContentObjectView::textColor() const { return m_textColor; } @@ -331,6 +336,10 @@ QColor trackContentObjectView::textColor() const void trackContentObjectView::setFgColor( const QColor & _c ) { m_fgColor = QColor( _c ); } +//! \brief CSS theming qproperty access method +void trackContentObjectView::setBgColor( const QColor & _c ) +{ m_bgColor = QColor( _c ); } + //! \brief CSS theming qproperty access method void trackContentObjectView::setTextColor( const QColor & _c ) { m_textColor = QColor( _c ); } diff --git a/src/tracks/SampleTrack.cpp b/src/tracks/SampleTrack.cpp index 2cac8792721..baaeaf108cb 100644 --- a/src/tracks/SampleTrack.cpp +++ b/src/tracks/SampleTrack.cpp @@ -24,6 +24,7 @@ */ #include +#include #include #include #include @@ -35,6 +36,7 @@ #include "gui_templates.h" #include "SampleTrack.h" #include "song.h" +#include "SongEditor.h" #include "embed.h" #include "engine.h" #include "tooltip.h" @@ -56,7 +58,9 @@ SampleTCO::SampleTCO( track * _track ) : saveJournallingState( false ); setSampleFile( "" ); restoreJournallingState(); - + m_fg_color = defaultFgColor(); + m_bg_color = defaultBgColor(); + // we need to receive bpm-change-events, because then we have to // change length of this TCO connect( engine::getSong(), SIGNAL( tempoChanged( bpm_t ) ), @@ -155,6 +159,9 @@ void SampleTCO::saveSettings( QDomDocument & _doc, QDomElement & _this ) _this.setAttribute( "data", m_sampleBuffer->toBase64( s ) ); } // TODO: start- and end-frame + _this.setAttribute( "fg_color", m_fg_color ); + _this.setAttribute( "bg_color", m_bg_color ); + } @@ -173,6 +180,15 @@ void SampleTCO::loadSettings( const QDomElement & _this ) } changeLength( _this.attribute( "len" ).toInt() ); setMuted( _this.attribute( "muted" ).toInt() ); + + if( _this.attribute( "fg_color" ).toUInt() != 0 ) + { + m_fg_color = _this.attribute( "fg_color" ).toUInt(); + } + if( _this.attribute( "bg_color" ).toUInt() != 0 ) + { + m_bg_color = _this.attribute( "bg_color" ).toUInt(); + } } @@ -254,9 +270,12 @@ void SampleTCOView::contextMenuEvent( QContextMenuEvent * _cme ) contextMenu.addAction( embed::getIconPixmap( "muted" ), tr( "Mute/unmute ( + middle click)" ), m_tco, SLOT( toggleMute() ) ); - contextMenu.addAction( embed::getIconPixmap( "record" ), - tr( "Set/clear record" ), - m_tco, SLOT( toggleRecord() ) ); + contextMenu.addAction( embed::getIconPixmap( "colorize" ), + tr( "Change foreground color" ), + this, SLOT( changeFgColor() ) ); + contextMenu.addAction( embed::getIconPixmap( "colorize" ), + tr( "Change background color" ), + this, SLOT( changeBgColor() ) ); constructContextMenu( &contextMenu ); contextMenu.exec( QCursor::pos() ); @@ -335,8 +354,9 @@ void SampleTCOView::mouseDoubleClickEvent( QMouseEvent * ) void SampleTCOView::paintEvent( QPaintEvent * _pe ) { QPainter p( this ); - const QColor styleColor = p.pen().brush().color(); - + QColor styleColor = p.pen().brush().color(); + if (m_tco->m_bg_color != 0) styleColor = QColor( m_tco->m_bg_color ); + QColor c; if( !( m_tco->getTrack()->isMuted() || m_tco->isMuted() ) ) c = isSelected() ? QColor( 0, 0, 224 ) @@ -363,13 +383,14 @@ void SampleTCOView::paintEvent( QPaintEvent * _pe ) } else { - p.setPen( fgColor() ); + p.setPen( m_tco->m_fg_color ); } QRect r = QRect( 1, 1, qMax( static_cast( m_tco->sampleLength() * pixelsPerTact() / DefaultTicksPerTact ), 1 ), height() - 4 ); p.setClipRect( QRect( 1, 1, width() - 2, height() - 2 ) ); + m_tco->m_sampleBuffer->setColor( m_tco->m_fg_color ); m_tco->m_sampleBuffer->visualize( p, r, _pe->rect() ); if( r.width() < width() - 1 ) { @@ -396,8 +417,81 @@ void SampleTCOView::paintEvent( QPaintEvent * _pe ) } } +void SampleTCOView::changeFgColor() +{ + QColor _new_color = QColorDialog::getColor( m_tco->m_fg_color ); + if( !_new_color.isValid() ) + { + return; + } + if( isSelected() ) + { + QVector selected = + engine::songEditor()->selectedObjects(); + for( QVector::iterator it = + selected.begin(); + it != selected.end(); ++it ) + { + SampleTCOView * sample_tcov = dynamic_cast( *it ); + if( sample_tcov ) + { + sample_tcov->setFgColor( _new_color ); + } + } + } + else + { + setFgColor( _new_color ); + } +} + +void SampleTCOView::changeBgColor() +{ + QColor _new_color = QColorDialog::getColor( m_tco->m_bg_color ); + if( !_new_color.isValid() ) + { + return; + } + if( isSelected() ) + { + QVector selected = + engine::songEditor()->selectedObjects(); + for( QVector::iterator it = + selected.begin(); + it != selected.end(); ++it ) + { + SampleTCOView * sample_tcov = dynamic_cast( *it ); + if( sample_tcov ) + { + sample_tcov->setBgColor( _new_color ); + } + } + } + else + { + setBgColor( _new_color ); + } +} +void SampleTCOView::setFgColor( QColor _new_color ) +{ + if( _new_color.rgb() != m_tco->m_fg_color ) + { + m_tco->m_fg_color = _new_color.rgb(); + engine::getSong()->setModified(); + update(); + } +} +void SampleTCOView::setBgColor( QColor _new_color ) +{ + if( _new_color.rgb() != m_tco->m_bg_color ) + { + m_tco->m_bg_color = _new_color.rgb(); + engine::getSong()->setModified(); + update(); + } +}