From d866015dc9e46204e5ed32d4897b3b3cdb70858d Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Thu, 11 Jun 2009 14:33:20 +0200 Subject: [PATCH] PianoView: use layout for flexible widget size Use a vertical box layout for PianoView and and track resize events in order to keep scrollbar range up to date. This way PianoView's size is not fixed anymore and the PianoView widget can be placed inside other layouts without any restrictions. Signed-off-by: Tobias Doerffel --- include/piano.h | 1 + src/core/piano.cpp | 47 +++++++++++++++++++++++++-------- src/tracks/instrument_track.cpp | 2 -- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/include/piano.h b/include/piano.h index 67e64bd3f..cd28a78a7 100644 --- a/include/piano.h +++ b/include/piano.h @@ -83,6 +83,7 @@ protected: virtual void mouseReleaseEvent( QMouseEvent * me ); virtual void mouseMoveEvent( QMouseEvent * me ); virtual void focusOutEvent( QFocusEvent * _fe ); + virtual void resizeEvent( QResizeEvent * _event ); private: diff --git a/src/core/piano.cpp b/src/core/piano.cpp index 6f83816ba..63b86c3b0 100644 --- a/src/core/piano.cpp +++ b/src/core/piano.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include "automation_pattern.h" @@ -109,7 +110,7 @@ Piano::Piano( MidiEventProcessor * _mep ) : model( NULL ), /*!< base class ctor */ m_midiEvProc( _mep ) /*!< the instrumentTrack model */ { - for( int i = 0; i < KeysPerOctave * NumOctaves; ++i ) + for( int i = 0; i < NumKeys; ++i ) { m_pressedKeys[i] = false; } @@ -136,7 +137,7 @@ Piano::~Piano() */ void Piano::setKeyState( int _key, bool _on ) { - m_pressedKeys[tLimit( _key, 0, KeysPerOctave * NumOctaves - 1 )] = _on; + m_pressedKeys[tLimit( _key, 0, NumKeys-1 )] = _on; emit dataChanged(); } @@ -210,18 +211,24 @@ PianoView::PianoView( QWidget * _parent ) : setAttribute( Qt::WA_OpaquePaintEvent, true ); setFocusPolicy( Qt::StrongFocus ); + setMaximumWidth( WhiteKeysPerOctave * NumOctaves * PW_WHITE_KEY_WIDTH ); + // create scrollbar at the bottom m_pianoScroll = new QScrollBar( Qt::Horizontal, this ); - m_pianoScroll->setRange( 0, WhiteKeysPerOctave * ( NumOctaves - 3 ) - - 5 ); m_pianoScroll->setSingleStep( 1 ); m_pianoScroll->setPageStep( 20 ); m_pianoScroll->setValue( Octave_3 * WhiteKeysPerOctave ); - m_pianoScroll->setGeometry( 0, PIANO_BASE + PW_WHITE_KEY_HEIGHT, 250, - 16 ); - // ...and connect it to this widget... + + // and connect it to this widget connect( m_pianoScroll, SIGNAL( valueChanged( int ) ), - this, SLOT( pianoScrolled( int ) ) ); + this, SLOT( pianoScrolled( int ) ) ); + + // create a layout for ourselves + QVBoxLayout * layout = new QVBoxLayout( this ); + layout->setSpacing( 0 ); + layout->setMargin( 0 ); + layout->addSpacing( PIANO_BASE+PW_WHITE_KEY_HEIGHT ); + layout->addWidget( m_pianoScroll ); } @@ -441,7 +448,7 @@ int PianoView::getKeyFromMouse( const QPoint & _p ) const { --key_num; } - if( key_num < KeysPerOctave * NumOctaves - 1 && + if( key_num < NumKeys - 1 && KEY_ORDER[( key_num + 1 ) % KeysPerOctave] == Piano::BlackKey && _p.x() % PW_WHITE_KEY_WIDTH >= @@ -453,7 +460,7 @@ int PianoView::getKeyFromMouse( const QPoint & _p ) const } // some range-checking-stuff - return tLimit( key_num, 0, KeysPerOctave * NumOctaves - 1 ); + return tLimit( key_num, 0, NumKeys - 1 ); } @@ -784,7 +791,7 @@ void PianoView::focusOutEvent( QFocusEvent * ) // if we loose focus, we HAVE to note off all running notes because // we don't receive key-release-events anymore and so the notes would // hang otherwise - for( int i = 0; i < KeysPerOctave * NumOctaves; ++i ) + for( int i = 0; i < NumKeys; ++i ) { if( m_piano->m_pressedKeys[i] == true ) { @@ -800,6 +807,24 @@ void PianoView::focusOutEvent( QFocusEvent * ) +/*! \brief update scrollbar range after resize + * + * After resizing we need to adjust range of scrollbar for not allowing + * to scroll too far to the right. + * + * \param _event resize-event object (unused) + */ +void PianoView::resizeEvent( QResizeEvent * _event ) +{ + QWidget::resizeEvent( _event ); + m_pianoScroll->setRange( 0, WhiteKeysPerOctave * NumOctaves - + (int) ceil( (float) width() / + PW_WHITE_KEY_WIDTH ) ); +} + + + + /*! \brief Convert a key number to an X coordinate in the piano display view * * We can immediately discard the trivial case of when the key number is diff --git a/src/tracks/instrument_track.cpp b/src/tracks/instrument_track.cpp index d58387c99..b3ae9f791 100644 --- a/src/tracks/instrument_track.cpp +++ b/src/tracks/instrument_track.cpp @@ -85,7 +85,6 @@ const char * volume_help = QT_TRANSLATE_NOOP( "instrumentTrack", const int INSTRUMENT_WIDTH = 254; const int INSTRUMENT_HEIGHT = INSTRUMENT_WIDTH; -const int PIANO_HEIGHT = 84; const int INSTRUMENT_WINDOW_CACHE_SIZE = 8; @@ -1267,7 +1266,6 @@ instrumentTrackWindow::instrumentTrackWindow( instrumentTrackView * _itv ) : // setup piano-widget m_pianoView= new PianoView( this ); - m_pianoView->setFixedSize( INSTRUMENT_WIDTH, PIANO_HEIGHT ); vlayout->addWidget( m_generalSettingsWidget ); vlayout->addWidget( m_tabWidget );