Piano: refactored class to be usable without friend classes

This commit is contained in:
Tobias Doerffel
2014-01-14 23:12:02 +01:00
parent 340789c191
commit c40b8350ef
5 changed files with 93 additions and 103 deletions

View File

@@ -1,7 +1,7 @@
/*
* Piano.h - declaration of class Piano
*
* Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
@@ -40,24 +40,40 @@ public:
BlackKey
} ;
Piano( InstrumentTrack * _it );
Piano( InstrumentTrack* track );
virtual ~Piano();
void setKeyState( int _key, bool _on = false );
void setKeyState( int key, bool state );
void handleKeyPress( int _key );
void handleKeyRelease( int _key );
bool isKeyPressed( int key ) const
{
return m_pressedKeys[key];
}
void handleKeyPress( int key );
void handleKeyRelease( int key );
InstrumentTrack* instrumentTrack() const
{
return m_instrumentTrack;
}
MidiEventProcessor* midiEventProcessor() const
{
return m_midiEvProc;
}
private:
InstrumentTrack * m_instrumentTrack;
MidiEventProcessor * m_midiEvProc;
static bool isValidKey( int key )
{
return key >= 0 && key < NumKeys;
}
InstrumentTrack* m_instrumentTrack;
MidiEventProcessor* m_midiEvProc;
bool m_pressedKeys[NumKeys];
friend class PianoView;
friend class pianoRoll;
} ;
#endif

View File

@@ -2,7 +2,7 @@
* piano_roll.h - declaration of class pianoRoll which is a window where you
* can set and edit notes in an easy way
*
* Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2008 Andrew Kelley <superjoe30/at/gmail/dot/com>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
@@ -50,7 +50,6 @@ class pattern;
class timeLine;
class toolButton;
class Piano;
class pianoRoll : public QWidget, public SerializingObject
{
Q_OBJECT
@@ -154,7 +153,6 @@ signals:
private:
Piano * m_piano;
enum editModes
{
ModeDraw,

View File

@@ -2,7 +2,7 @@
* Piano.cpp - implementation of piano-widget used in instrument-track-window
* for testing + according model class
*
* Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
@@ -45,10 +45,10 @@
*
* \param _it the InstrumentTrack window to attach to
*/
Piano::Piano( InstrumentTrack * _it ) :
Piano::Piano( InstrumentTrack* track ) :
Model( NULL ), /*!< base class ctor */
m_instrumentTrack( _it ),
m_midiEvProc( _it ) /*!< the InstrumentTrack Model */
m_instrumentTrack( track ),
m_midiEvProc( track ) /*!< the InstrumentTrack Model */
{
for( int i = 0; i < NumKeys; ++i )
{
@@ -72,13 +72,17 @@ Piano::~Piano()
/*! \brief Turn a key on or off
*
* \param _key the key number to change
* \param _on the state to set the key to
* \param key the key number to change
* \param state the state to set the key to
*/
void Piano::setKeyState( int _key, bool _on )
void Piano::setKeyState( int key, bool state )
{
m_pressedKeys[tLimit( _key, 0, NumKeys-1 )] = _on;
emit dataChanged();
if( isValidKey( key ) )
{
m_pressedKeys[key] = state;
emit dataChanged();
}
}
@@ -86,13 +90,15 @@ void Piano::setKeyState( int _key, bool _on )
/*! \brief Handle a note being pressed on our keyboard display
*
* \param _key the key being pressed
* \param key the key being pressed
*/
void Piano::handleKeyPress( int _key )
void Piano::handleKeyPress( int key )
{
m_midiEvProc->processInEvent( midiEvent( MidiNoteOn, 0, _key,
MidiMaxVelocity ), midiTime() );
m_pressedKeys[_key] = true;
if( isValidKey( key ) )
{
m_midiEvProc->processInEvent( midiEvent( MidiNoteOn, 0, key, MidiMaxVelocity ), midiTime() );
m_pressedKeys[key] = true;
}
}
@@ -101,13 +107,15 @@ void Piano::handleKeyPress( int _key )
/*! \brief Handle a note being released on our keyboard display
*
* \param _key the key being releassed
* \param key the key being releassed
*/
void Piano::handleKeyRelease( int _key )
void Piano::handleKeyRelease( int key )
{
m_midiEvProc->processInEvent( midiEvent( MidiNoteOff, 0, _key, 0 ),
midiTime() );
m_pressedKeys[_key] = false;
if( isValidKey( key ) )
{
m_midiEvProc->processInEvent( midiEvent( MidiNoteOff, 0, key, 0 ), midiTime() );
m_pressedKeys[key] = false;
}
}

View File

@@ -2,7 +2,7 @@
* Piano.cpp - implementation of piano-widget used in instrument-track-window
* for testing + according model class
*
* Copyright (c) 2004-2010 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
@@ -306,8 +306,8 @@ void PianoView::modelChanged()
m_piano = castModel<Piano>();
if( m_piano != NULL )
{
connect( m_piano->m_instrumentTrack->baseNoteModel(),
SIGNAL( dataChanged() ), this, SLOT( update() ) );
connect( m_piano->instrumentTrack()->baseNoteModel(), SIGNAL( dataChanged() ),
this, SLOT( update() ) );
}
}
@@ -413,8 +413,7 @@ void PianoView::contextMenuEvent( QContextMenuEvent * _me )
}
captionMenu contextMenu( tr( "Base note" ) );
AutomatableModelView amv( m_piano->m_instrumentTrack->baseNoteModel(),
&contextMenu );
AutomatableModelView amv( m_piano->instrumentTrack()->baseNoteModel(), &contextMenu );
amv.addDefaultActions( &contextMenu );
contextMenu.exec( QCursor::pos() );
}
@@ -466,11 +465,8 @@ void PianoView::mousePressEvent( QMouseEvent * _me )
velocity = MidiMaxVelocity;
}
// set note on
m_piano->m_instrumentTrack->processInEvent(
midiEvent( MidiNoteOn, 0, key_num,
velocity ),
midiTime() );
m_piano->m_pressedKeys[key_num] = true;
m_piano->midiEventProcessor()->processInEvent( midiEvent( MidiNoteOn, 0, key_num, velocity ), midiTime() );
m_piano->setKeyState( key_num, true );
m_lastKey = key_num;
emit keyPressed( key_num );
@@ -480,17 +476,13 @@ void PianoView::mousePressEvent( QMouseEvent * _me )
if( _me->modifiers() & Qt::ControlModifier )
{
new stringPairDrag( "automatable_model",
QString::number( m_piano->
m_instrumentTrack->
baseNoteModel()->id() ),
QString::number( m_piano->instrumentTrack()->baseNoteModel()->id() ),
QPixmap(), this );
_me->accept();
}
else
{
m_piano->m_instrumentTrack->
baseNoteModel()->
setInitValue( (float) key_num );
m_piano->instrumentTrack()->baseNoteModel()->setInitValue( (float) key_num );
emit baseNoteChanged();
}
@@ -518,10 +510,8 @@ void PianoView::mouseReleaseEvent( QMouseEvent * )
{
if( m_piano != NULL )
{
m_piano->m_midiEvProc->processInEvent(
midiEvent( MidiNoteOff, 0, m_lastKey, 0 ),
midiTime() );
m_piano->m_pressedKeys[m_lastKey] = false;
m_piano->midiEventProcessor()->processInEvent( midiEvent( MidiNoteOff, 0, m_lastKey, 0 ), midiTime() );
m_piano->setKeyState( m_lastKey, false );
}
// and let the user see that he released a key... :)
@@ -581,39 +571,29 @@ void PianoView::mouseMoveEvent( QMouseEvent * _me )
{
if( m_lastKey != -1 )
{
m_piano->m_midiEvProc->processInEvent(
midiEvent( MidiNoteOff, 0, m_lastKey, 0 ),
midiTime() );
m_piano->m_pressedKeys[m_lastKey] = false;
m_piano->midiEventProcessor()->processInEvent( midiEvent( MidiNoteOff, 0, m_lastKey, 0 ), midiTime() );
m_piano->setKeyState( m_lastKey, false );
m_lastKey = -1;
}
if( _me->buttons() & Qt::LeftButton )
{
if( _me->pos().y() > PIANO_BASE )
{
m_piano->m_midiEvProc->processInEvent(
midiEvent( MidiNoteOn, 0, key_num,
velocity ),
midiTime() );
m_piano->m_pressedKeys[key_num] = true;
m_piano->midiEventProcessor()->processInEvent( midiEvent( MidiNoteOn, 0, key_num, velocity ), midiTime() );
m_piano->setKeyState( key_num, true );
m_lastKey = key_num;
}
else
{
m_piano->m_instrumentTrack->
baseNoteModel()->
setInitValue( (float) key_num );
m_piano->instrumentTrack()->baseNoteModel()->setInitValue( (float) key_num );
}
}
// and let the user see that he pressed a key... :)
update();
}
else if( m_piano->m_pressedKeys[key_num] == true )
else if( m_piano->isKeyPressed( key_num ) )
{
m_piano->m_midiEvProc->processInEvent(
midiEvent( MidiKeyPressure, 0, key_num,
velocity ),
midiTime() );
m_piano->midiEventProcessor()->processInEvent( midiEvent( MidiKeyPressure, 0, key_num, velocity ), midiTime() );
}
}
@@ -710,10 +690,8 @@ void PianoView::focusOutEvent( QFocusEvent * )
// hang otherwise
for( int i = 0; i < NumKeys; ++i )
{
m_piano->m_midiEvProc->processInEvent(
midiEvent( MidiNoteOff, 0, i, 0 ),
midiTime() );
m_piano->m_pressedKeys[i] = false;
m_piano->midiEventProcessor()->processInEvent( midiEvent( MidiNoteOff, 0, i, 0 ), midiTime() );
m_piano->setKeyState( i, false );
}
update();
}
@@ -826,7 +804,7 @@ void PianoView::paintEvent( QPaintEvent * )
p.setPen( Qt::white );
const int base_key = ( m_piano != NULL ) ?
m_piano->m_instrumentTrack->baseNoteModel()->value() : 0;
m_piano->instrumentTrack()->baseNoteModel()->value() : 0;
g.setColorAt( 0, QColor( 0, 96, 0 ) );
g.setColorAt( 0.1, QColor( 64, 255, 64 ) );
g.setColorAt( 1, QColor( 0, 96, 0 ) );
@@ -854,7 +832,7 @@ void PianoView::paintEvent( QPaintEvent * )
// draw pressed or not pressed key, depending on state of
// current key
if( m_piano && m_piano->m_pressedKeys[cur_key] == true )
if( m_piano && m_piano->isKeyPressed( cur_key ) )
{
p.drawPixmap( x, PIANO_BASE, *s_whiteKeyPressedPm );
}
@@ -881,19 +859,17 @@ void PianoView::paintEvent( QPaintEvent * )
cur_key = m_startKey;
int white_cnt = 0;
int s_key = m_startKey;
if( s_key > 0 &&
KEY_ORDER[(Keys)(--s_key) % KeysPerOctave] == Piano::BlackKey )
int startKey = m_startKey;
if( startKey > 0 &&
KEY_ORDER[(Keys)(--startKey) % KeysPerOctave] == Piano::BlackKey )
{
if( m_piano && m_piano->m_pressedKeys[s_key] == true )
if( m_piano && m_piano->isKeyPressed( startKey ) )
{
p.drawPixmap( 0 - PW_WHITE_KEY_WIDTH / 2, PIANO_BASE,
*s_blackKeyPressedPm );
p.drawPixmap( 0 - PW_WHITE_KEY_WIDTH / 2, PIANO_BASE, *s_blackKeyPressedPm );
}
else
{
p.drawPixmap( 0 - PW_WHITE_KEY_WIDTH / 2, PIANO_BASE,
*s_blackKeyPm );
p.drawPixmap( 0 - PW_WHITE_KEY_WIDTH / 2, PIANO_BASE, *s_blackKeyPm );
}
}
@@ -904,16 +880,13 @@ void PianoView::paintEvent( QPaintEvent * )
{
// draw pressed or not pressed key, depending on
// state of current key
if( m_piano && m_piano->m_pressedKeys[cur_key] == true )
if( m_piano && m_piano->isKeyPressed( cur_key ) )
{
p.drawPixmap( x + PW_WHITE_KEY_WIDTH / 2,
PIANO_BASE,
*s_blackKeyPressedPm );
p.drawPixmap( x + PW_WHITE_KEY_WIDTH / 2, PIANO_BASE, *s_blackKeyPressedPm );
}
else
{
p.drawPixmap( x + PW_WHITE_KEY_WIDTH / 2,
PIANO_BASE, *s_blackKeyPm );
p.drawPixmap( x + PW_WHITE_KEY_WIDTH / 2, PIANO_BASE, *s_blackKeyPm );
}
x += PW_WHITE_KEY_WIDTH;
white_cnt = 0;

View File

@@ -2,7 +2,7 @@
* piano_roll.cpp - implementation of piano-roll which is used for actual
* writing of melodies
*
* Copyright (c) 2004-2010 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2008 Andrew Kelley <superjoe30/at/gmail/dot/com>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
@@ -139,7 +139,6 @@ const int DEFAULT_PR_PPT = KEY_LINE_HEIGHT * DefaultStepsPerTact;
pianoRoll::pianoRoll() :
m_piano(NULL),
m_nemStr( QVector<QString>() ),
m_noteEditMenu( NULL ),
m_semiToneMarkerMenu( NULL ),
@@ -2814,15 +2813,13 @@ void pianoRoll::paintEvent( QPaintEvent * _pe )
if( prKeyOrder[key % KeysPerOctave] == PR_WHITE_KEY_SMALL )
{
// draw a small one while checking if it is pressed or not
if(m_pattern->instrumentTrack()->pianoModel()->m_pressedKeys[key] == true)
if( m_pattern->instrumentTrack()->pianoModel()->isKeyPressed( key ) )
{
p.drawPixmap( PIANO_X, y - WHITE_KEY_SMALL_HEIGHT,
*s_whiteKeySmallPressedPm );
p.drawPixmap( PIANO_X, y - WHITE_KEY_SMALL_HEIGHT, *s_whiteKeySmallPressedPm );
}
else
{
p.drawPixmap( PIANO_X, y - WHITE_KEY_SMALL_HEIGHT,
*s_whiteKeySmallPm );
p.drawPixmap( PIANO_X, y - WHITE_KEY_SMALL_HEIGHT, *s_whiteKeySmallPm );
}
// update y-pos
y -= WHITE_KEY_SMALL_HEIGHT;
@@ -2832,15 +2829,13 @@ void pianoRoll::paintEvent( QPaintEvent * _pe )
PR_WHITE_KEY_BIG )
{
// draw a big one while checking if it is pressed or not
if(m_pattern->instrumentTrack()->pianoModel()->m_pressedKeys[key] == true)
if(m_pattern->instrumentTrack()->pianoModel()->isKeyPressed( key ) )
{
p.drawPixmap( PIANO_X, y - WHITE_KEY_SMALL_HEIGHT,
*s_whiteKeyBigPressedPm );
p.drawPixmap( PIANO_X, y - WHITE_KEY_SMALL_HEIGHT, *s_whiteKeyBigPressedPm );
}
else
{
p.drawPixmap( PIANO_X, y-WHITE_KEY_BIG_HEIGHT,
*s_whiteKeyBigPm );
p.drawPixmap( PIANO_X, y-WHITE_KEY_BIG_HEIGHT, *s_whiteKeyBigPm );
}
// if a big white key has been the first key,
// black keys needs to be lifted up
@@ -2909,7 +2904,7 @@ void pianoRoll::paintEvent( QPaintEvent * _pe )
// then draw it (calculation of y very complicated,
// but that's the only working solution, sorry...)
// check if the key is pressed or not
if(m_pattern->instrumentTrack()->pianoModel()->m_pressedKeys[key] == true)
if( m_pattern->instrumentTrack()->pianoModel()->isKeyPressed( key ) )
{
p.drawPixmap( PIANO_X, y - ( first_white_key_height -
WHITE_KEY_SMALL_HEIGHT ) -