From 87e597510cac8b02e65d7205a81a7f8d80598ad8 Mon Sep 17 00:00:00 2001 From: Jonas Trappenberg Date: Sat, 10 Jan 2015 23:59:06 -0800 Subject: [PATCH] Minor refactorings and codestyle cleanup. --- include/MidiTime.h | 3 +- include/Note.h | 6 +- include/PianoRoll.h | 2 +- src/gui/PianoRoll.cpp | 464 ++++++++++++++++++++--------------------- src/tracks/Pattern.cpp | 21 +- 5 files changed, 247 insertions(+), 249 deletions(-) diff --git a/include/MidiTime.h b/include/MidiTime.h index 5c49f780d..788e6fb57 100644 --- a/include/MidiTime.h +++ b/include/MidiTime.h @@ -137,7 +137,8 @@ public: static int stepsPerTact() { - return qMax( 1, ticksPerTact() / DefaultBeatsPerTact ); + int steps = ticksPerTact() / DefaultBeatsPerTact; + return qMax( 1, steps ); } static void setTicksPerTact( tick_t _tpt ) diff --git a/include/Note.h b/include/Note.h index eb36462a6..d179ba939 100644 --- a/include/Note.h +++ b/include/Note.h @@ -91,9 +91,9 @@ public: virtual ~Note(); // used by GUI - inline void setSelected( const bool _selected ){ m_selected = _selected; } - inline void setOldKey( const int _oldKey ){ m_oldKey = _oldKey; } - inline void setOldPos( const MidiTime & _oldPos ){ m_oldPos = _oldPos; } + inline void setSelected( const bool _selected ) { m_selected = _selected; } + inline void setOldKey( const int _oldKey ) { m_oldKey = _oldKey; } + inline void setOldPos( const MidiTime & _oldPos ) { m_oldPos = _oldPos; } inline void setOldLength( const MidiTime & _oldLength ) { m_oldLength = _oldLength; diff --git a/include/PianoRoll.h b/include/PianoRoll.h index bd27bda2e..84a8ffee8 100644 --- a/include/PianoRoll.h +++ b/include/PianoRoll.h @@ -341,7 +341,7 @@ private: volume_t m_lastNoteVolume; panning_t m_lastNotePanning; - int m_startKey; // first key when drawing + int m_startKey; // first key when drawing int m_lastKey; editModes m_editMode; diff --git a/src/gui/PianoRoll.cpp b/src/gui/PianoRoll.cpp index deeb70d7c..980b24396 100644 --- a/src/gui/PianoRoll.cpp +++ b/src/gui/PianoRoll.cpp @@ -809,7 +809,7 @@ void PianoRoll::setCurrentPattern( Pattern* newPattern ) m_currentNote = NULL; m_startKey = INITIAL_START_KEY; - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { //resizeEvent( NULL ); setWindowTitle( tr( "Piano-Roll - no pattern" ) ); @@ -823,16 +823,17 @@ void PianoRoll::setCurrentPattern( Pattern* newPattern ) const NoteVector & notes = m_pattern->notes(); int central_key = 0; - if( notes.empty() == false ) + if( ! notes.empty() ) { // determine the central key so that we can scroll to it int total_notes = 0; for( NoteVector::ConstIterator it = notes.begin(); it != notes.end(); ++it ) { - if( ( *it )->length() > 0 ) + Note *note = *it; + if( note->length() > 0 ) { - central_key += ( *it )->key(); + central_key += note->key(); ++total_notes; } } @@ -893,7 +894,7 @@ void PianoRoll::loadSettings( const QDomElement & _this ) void PianoRoll::setPauseIcon( bool pause ) { - if( pause == true ) + if( pause ) { m_playButton->setIcon( embed::getIconPixmap( "pause" ) ); } @@ -1056,8 +1057,6 @@ void PianoRoll::removeSelection() m_selectedTick = 0; m_selectStartKey = 0; m_selectedKeys = 0; - - } @@ -1071,12 +1070,10 @@ void PianoRoll::clearSelectedNotes() const NoteVector & notes = m_pattern->notes(); // will be our iterator in the following loop - NoteVector::ConstIterator it = notes.begin(); - while( it != notes.end() ) - { - ( *it )->setSelected( false ); - - ++it; + NoteVector::ConstIterator it; + for( it = notes.begin(); it != notes.end(); ++it ) { + Note *note = *it; + note->setSelected( false ); } } } @@ -1105,21 +1102,21 @@ void PianoRoll::shiftSemiTone( int amount ) // shift notes by amount semitones { bool useAllNotes = ! isSelection(); const NoteVector & notes = m_pattern->notes(); - for( NoteVector::ConstIterator it = notes.begin(); it != notes.end(); - ++it ) + NoteVector::ConstIterator it; + for( it = notes.begin(); it != notes.end(); ++it ) { + Note *note = *it; // if none are selected, move all notes, otherwise // only move selected notes - if( useAllNotes || ( *it )->selected() ) + if( useAllNotes || note->selected() ) { - ( *it )->setKey( ( *it )->key() + amount ); + note->setKey( note->key() + amount ); } } // we modified the song update(); Engine::songEditor()->update(); - } @@ -1129,26 +1126,27 @@ void PianoRoll::shiftPos( int amount ) //shift notes pos by amount { bool useAllNotes = ! isSelection(); const NoteVector & notes = m_pattern->notes(); + NoteVector::ConstIterator it; bool first = true; - for( NoteVector::ConstIterator it = notes.begin(); it != notes.end(); - ++it ) + for( it = notes.begin(); it != notes.end(); ++it ) { + Note *note = *it; // if none are selected, move all notes, otherwise // only move selected notes - if( ( *it )->selected() || (useAllNotes && ( *it )->length() > 0) ) + if( note->selected() || (useAllNotes && note->length() > 0) ) { // don't let notes go to out of bounds if( first ) { - m_moveBoundaryLeft = ( *it )->pos(); + m_moveBoundaryLeft = note->pos(); if( m_moveBoundaryLeft + amount < 0 ) { amount += 0 - (amount + m_moveBoundaryLeft); } first = false; } - ( *it )->setPos( ( *it )->pos() + amount ); + note->setPos( note->pos() + amount ); } } @@ -1163,9 +1161,11 @@ void PianoRoll::shiftPos( int amount ) //shift notes pos by amount bool PianoRoll::isSelection() const // are any notes selected? { const NoteVector & notes = m_pattern->notes(); - for( NoteVector::ConstIterator it = notes.begin(); it != notes.end(); ++it ) + NoteVector::ConstIterator it; + for( it = notes.begin(); it != notes.end(); ++it ) { - if( ( *it )->selected() ) + Note *note = *it; + if( note->selected() ) { return true; } @@ -1181,9 +1181,11 @@ int PianoRoll::selectionCount() const // how many notes are selected? int sum = 0; const NoteVector & notes = m_pattern->notes(); - for( NoteVector::ConstIterator it = notes.begin(); it != notes.end(); ++it ) + NoteVector::ConstIterator it; + for( it = notes.begin(); it != notes.end(); ++it ) { - if( ( *it )->selected() ) + Note *note = *it; + if( note->selected() ) { ++sum; } @@ -1200,7 +1202,7 @@ void PianoRoll::keyPressEvent( QKeyEvent* event ) { const int key_num = PianoView::getKeyFromKeyEvent( event ) + ( DefaultOctave - 1 ) * KeysPerOctave; - if( event->isAutoRepeat() == false && key_num > -1 ) + if( ! event->isAutoRepeat() && key_num > -1 ) { m_pattern->instrumentTrack()->pianoModel()->handleKeyPress( key_num ); event->accept(); @@ -1483,7 +1485,7 @@ void PianoRoll::keyReleaseEvent( QKeyEvent* event ) { const int key_num = PianoView::getKeyFromKeyEvent( event ) + ( DefaultOctave - 1 ) * KeysPerOctave; - if( event->isAutoRepeat() == false && key_num > -1 ) + if( ! event->isAutoRepeat() && key_num > -1 ) { m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( key_num ); event->accept(); @@ -1581,7 +1583,7 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) { m_startedWithShift = _me->modifiers() & Qt::ShiftModifier; - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { return; } @@ -1645,22 +1647,23 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) // loop through whole note-vector... for( int i = 0; i < notes.size(); ++i ) { - MidiTime len = ( *it )->length(); + Note *note = *it; + MidiTime len = note->length(); if( len < 0 ) { len = 4; } // and check whether the user clicked on an // existing note or an edit-line - if( pos_ticks >= ( *it )->pos() && + if( pos_ticks >= note->pos() && len > 0 && ( - ( edit_note == false && - pos_ticks <= ( *it )->pos() + len && - ( *it )->key() == key_num ) + ( ! edit_note && + pos_ticks <= note->pos() + len && + note->key() == key_num ) || - ( edit_note == true && - pos_ticks <= ( *it )->pos() + + ( edit_note && + pos_ticks <= note->pos() + NE_LINE_WIDTH * MidiTime::ticksPerTact() / m_ppt ) @@ -1674,7 +1677,7 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) // first check whether the user clicked in note-edit- // area - if( edit_note == true ) + if( edit_note ) { m_pattern->addJournalCheckPoint(); // scribble note edit changes @@ -1744,58 +1747,55 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) { ++it; } - - } - m_currentNote = *it; - m_lastNotePanning = ( *it )->getPanning(); - m_lastNoteVolume = ( *it )->getVolume(); - m_lenOfNewNotes = ( *it )->length(); + Note *current_note = *it; + m_currentNote = current_note; + m_lastNotePanning = current_note->getPanning(); + m_lastNoteVolume = current_note->getVolume(); + m_lenOfNewNotes = current_note->length(); // remember which key and tick we started with m_mouseDownKey = m_startKey; m_mouseDownTick = m_currentPosition; bool first = true; - it = notes.begin(); - while( it != notes.end() ) + for( it = notes.begin(); it != notes.end(); ++it ) { + Note *note = *it; // remember note starting positions - ( *it )->setOldKey( ( *it )->key() ); - ( *it )->setOldPos( ( *it )->pos() ); - ( *it )->setOldLength( ( *it )->length() ); + note->setOldKey( note->key() ); + note->setOldPos( note->pos() ); + note->setOldLength( note->length() ); - if( ( *it )->selected() ) + if( note->selected() ) { // figure out the bounding box of all the selected notes if( first ) { - m_moveBoundaryLeft = ( *it )->pos().getTicks(); - m_moveBoundaryRight = ( *it )->pos() + ( *it )->length(); - m_moveBoundaryBottom = ( *it )->key(); - m_moveBoundaryTop = ( *it )->key(); + m_moveBoundaryLeft = note->pos().getTicks(); + m_moveBoundaryRight = note->pos() + note->length(); + m_moveBoundaryBottom = note->key(); + m_moveBoundaryTop = note->key(); first = false; } else { m_moveBoundaryLeft = qMin( - ( *it )->pos().getTicks(), - m_moveBoundaryLeft ); - m_moveBoundaryRight = qMax( ( *it )->pos() + - ( *it )->length(), + note->pos().getTicks(), + (tick_t) m_moveBoundaryLeft ); + m_moveBoundaryRight = qMax( note->pos() + + note->length(), m_moveBoundaryRight ); - m_moveBoundaryBottom = qMin( ( *it )->key(), + m_moveBoundaryBottom = qMin( note->key(), m_moveBoundaryBottom ); - m_moveBoundaryTop = qMax( ( *it )->key(), + m_moveBoundaryTop = qMax( note->key(), m_moveBoundaryTop ); } } - - ++it; } // if clicked on an unselected note, remove selection @@ -1812,8 +1812,8 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) // clicked at the "tail" of the note? - if( pos_ticks*m_ppt/MidiTime::ticksPerTact() > - ( m_currentNote->pos() + m_currentNote->length() )*m_ppt/ MidiTime::ticksPerTact() - RESIZE_AREA_WIDTH && + if( pos_ticks * m_ppt/MidiTime::ticksPerTact() > + ( m_currentNote->pos() + m_currentNote->length() ) * m_ppt/ MidiTime::ticksPerTact() - RESIZE_AREA_WIDTH && m_currentNote->length() > 0 ) { m_pattern->addJournalCheckPoint(); @@ -1839,18 +1839,18 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) QApplication::setOverrideCursor( c ); // if they're holding shift, copy all selected notes - if( //*it != created_new_note && - ! is_new_note && _me->modifiers() & Qt::ShiftModifier ) + if( ! is_new_note && _me->modifiers() & Qt::ShiftModifier ) { // vector to hold new notes until we're through the loop QVector newNotes; it = notes.begin(); while( it != notes.end() ) { - if( ( *it )->selected() ) + Note *note = *it; + if( note->selected() ) { // copy this note - Note noteCopy( (Note) **it ); + Note noteCopy( *note ); newNotes.push_back( noteCopy ); } ++it; @@ -1859,7 +1859,7 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) if( newNotes.size() != 0 ) { //put notes from vector into piano roll - for( int i=0; iaddNote( newNotes[i] ); newNote->setSelected( false ); @@ -1886,14 +1886,15 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) m_mouseDownRight = true; if( it != notes.begin()-1 ) { + Note *note = *it; m_pattern->addJournalCheckPoint(); - if( ( *it )->length() > 0 ) + if( note->length() > 0 ) { - m_pattern->removeNote( *it ); + m_pattern->removeNote( note ); } else { - ( *it )->setLength( 0 ); + note->setLength( 0 ); m_pattern->dataChanged(); } Engine::getSong()->setModified(); @@ -1910,7 +1911,6 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) m_selectedKeys = 1; m_action = ActionSelectNotes; - // call mousemove to fix glitch where selection // appears in wrong spot on mousedown mouseMoveEvent( _me ); @@ -1930,11 +1930,8 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) { // left click - play the note m_lastKey = key_num; - //if( ! m_recording && ! engine::getSong()->isPlaying() ) - { - int v = ( (float) x ) / ( (float) WHITE_KEY_WIDTH ) * MidiDefaultVelocity; - m_pattern->instrumentTrack()->pianoModel()->handleKeyPress( key_num, v ); - } + int v = ( (float) x ) / ( (float) WHITE_KEY_WIDTH ) * MidiDefaultVelocity; + m_pattern->instrumentTrack()->pianoModel()->handleKeyPress( key_num, v ); } } else @@ -1945,7 +1942,7 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) m_noteEditMode = (noteEditMode)(((int)m_noteEditMode)+1); if( m_noteEditMode == NoteEditCount ) { - m_noteEditMode = (noteEditMode)0; + m_noteEditMode = (noteEditMode) 0; } repaint(); } @@ -1963,7 +1960,7 @@ void PianoRoll::mousePressEvent( QMouseEvent * _me ) void PianoRoll::mouseDoubleClickEvent( QMouseEvent * _me ) { - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { return; } @@ -2014,7 +2011,8 @@ void PianoRoll::mouseDoubleClickEvent( QMouseEvent * _me ) NoteVector::Iterator it = nv.begin(); while( it != nv.end() ) { - if( ( *it )->pos().getTicks() != closest->pos().getTicks() ) + Note *note = *it; + if( note->pos().getTicks() != closest->pos().getTicks() ) { it = nv.erase( it ); } @@ -2036,7 +2034,7 @@ void PianoRoll::testPlayNote( Note * n ) { m_lastKey = n->key(); - if( n->isPlaying() == false && m_recording == false ) + if( ! n->isPlaying() && ! m_recording ) { n->setIsPlaying( true ); @@ -2061,18 +2059,19 @@ void PianoRoll::pauseTestNotes( bool _pause ) NoteVector::ConstIterator it = notes.begin(); while( it != notes.end() ) { - if( ( *it )->isPlaying() ) + Note *note = *it; + if( note->isPlaying() ) { if( _pause ) { // stop note - m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( ( *it )->key() ); + m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( note->key() ); } else { // start note - ( *it )->setIsPlaying( false ); - testPlayNote( *it ); + note->setIsPlaying( false ); + testPlayNote( note ); } } @@ -2125,20 +2124,21 @@ void PianoRoll::computeSelectedNotes(bool shift) } //int y_base = noteEditTop() - 1; - if( hasValidPattern() == true ) + if( hasValidPattern() ) { const NoteVector & notes = m_pattern->notes(); + NoteVector::ConstIterator it; - for( NoteVector::ConstIterator it = notes.begin(); - it != notes.end(); ++it ) + for( it = notes.begin(); it != notes.end(); ++it ) { + Note *note = *it; // make a new selection unless they're holding shift if( ! shift ) { - ( *it )->setSelected( false ); + note->setSelected( false ); } - int len_ticks = ( *it )->length(); + int len_ticks = note->length(); if( len_ticks == 0 ) { @@ -2149,9 +2149,9 @@ void PianoRoll::computeSelectedNotes(bool shift) len_ticks = 4; } - const int key = ( *it )->key() - m_startKey + 1; + const int key = note->key() - m_startKey + 1; - int pos_ticks = ( *it )->pos(); + int pos_ticks = note->pos(); // if the selection even barely overlaps the note if( key > sel_key_start && @@ -2160,14 +2160,8 @@ void PianoRoll::computeSelectedNotes(bool shift) pos_ticks < sel_pos_end ) { // remove from selection when holding shift - if( shift && ( *it )->selected() ) - { - ( *it )->setSelected(false); - } - else - { - ( *it )->setSelected(true); - } + bool selected = shift && note->selected(); + note->setSelected( ! selected); } } } @@ -2227,7 +2221,7 @@ void PianoRoll::mouseReleaseEvent( QMouseEvent * _me ) } - if( hasValidPattern() == true ) + if( hasValidPattern() ) { // turn off all notes that are playing const NoteVector & notes = m_pattern->notes(); @@ -2235,10 +2229,11 @@ void PianoRoll::mouseReleaseEvent( QMouseEvent * _me ) NoteVector::ConstIterator it = notes.begin(); while( it != notes.end() ) { - if( ( *it )->isPlaying() ) + Note *note = *it; + if( note->isPlaying() ) { - m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( ( *it )->key() ); - ( *it )->setIsPlaying( false ); + m_pattern->instrumentTrack()->pianoModel()->handleKeyRelease( note->key() ); + note->setIsPlaying( false ); } ++it; @@ -2268,7 +2263,7 @@ void PianoRoll::mouseReleaseEvent( QMouseEvent * _me ) void PianoRoll::mouseMoveEvent( QMouseEvent * _me ) { - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { update(); return; @@ -2342,7 +2337,7 @@ void PianoRoll::mouseMoveEvent( QMouseEvent * _me ) pauseTestNotes( false ); } } - else if( ( edit_note == true || m_action == ActionChangeNoteProperty ) && + else if( ( edit_note || m_action == ActionChangeNoteProperty ) && ( _me->buttons() & Qt::LeftButton || _me->buttons() & Qt::MiddleButton || ( _me->buttons() & Qt::RightButton && _me->modifiers() & Qt::ShiftModifier ) ) ) { @@ -2491,13 +2486,14 @@ void PianoRoll::mouseMoveEvent( QMouseEvent * _me ) // loop through whole note-vector... for( int i = 0; i < notes.size(); ++i ) { + Note *note = *it; // and check whether the cursor is over an // existing note - if( pos_ticks >= ( *it )->pos() && - pos_ticks <= ( *it )->pos() + - ( *it )->length() && - ( *it )->key() == key_num && - ( *it )->length() > 0 ) + if( pos_ticks >= note->pos() && + pos_ticks <= note->pos() + + note->length() && + note->key() == key_num && + note->length() > 0 ) { break; } @@ -2508,12 +2504,13 @@ void PianoRoll::mouseMoveEvent( QMouseEvent * _me ) // no note?? if( it != notes.begin()-1 ) { + Note *note = *it; // cursor at the "tail" of the note? - if( ( *it )->length() > 0 && + if( note->length() > 0 && pos_ticks*m_ppt / MidiTime::ticksPerTact() > - ( ( *it )->pos() + - ( *it )->length() )*m_ppt/ + ( note->pos() + + note->length() )*m_ppt/ MidiTime::ticksPerTact()- RESIZE_AREA_WIDTH ) { @@ -2611,22 +2608,23 @@ void PianoRoll::mouseMoveEvent( QMouseEvent * _me ) // loop through whole note-vector... while( it != notes.end() ) { - MidiTime len = ( *it )->length(); + Note *note = *it; + MidiTime len = note->length(); if( len < 0 ) { len = 4; } // and check whether the user clicked on an // existing note or an edit-line - if( pos_ticks >= ( *it )->pos() && + if( pos_ticks >= note->pos() && len > 0 && ( - ( edit_note == false && - pos_ticks <= ( *it )->pos() + len && - ( *it )->key() == key_num ) + ( ! edit_note && + pos_ticks <= note->pos() + len && + note->key() == key_num ) || - ( edit_note == true && - pos_ticks <= ( *it )->pos() + + ( edit_note && + pos_ticks <= note->pos() + NE_LINE_WIDTH * MidiTime::ticksPerTact() / m_ppt ) @@ -2636,13 +2634,13 @@ void PianoRoll::mouseMoveEvent( QMouseEvent * _me ) // delete this note if( it != notes.end() ) { - if( ( *it )->length() > 0 ) + if( note->length() > 0 ) { - m_pattern->removeNote( *it ); + m_pattern->removeNote( note ); } else { - ( *it )->setLength( 0 ); + note->setLength( 0 ); m_pattern->dataChanged(); } Engine::getSong()->setModified(); @@ -2792,50 +2790,41 @@ void PianoRoll::dragNotes( int x, int y, bool alt, bool shift ) NoteVector::ConstIterator it = notes.begin(); while( it != notes.end() ) { - const int pos = ( *it )->pos().getTicks(); + Note *note = *it; + const int pos = note->pos().getTicks(); // when resizing a note and holding shift: shift the following // notes to preserve the melody if( m_action == ActionResizeNote && shift ) { - int shifted_pos = ( *it )->oldPos().getTicks() + shift_offset; + int shifted_pos = note->oldPos().getTicks() + shift_offset; if( shifted_pos && pos == shift_ref_pos ) { shifted_pos -= off_ticks; } - ( *it )->setPos( MidiTime( shifted_pos ) ); + note->setPos( MidiTime( shifted_pos ) ); } - if( ( *it )->selected() ) + if( note->selected() ) { if( m_action == ActionMoveNote && ! ( shift && ! m_startedWithShift ) ) { // moving note - int pos_ticks = ( *it )->oldPos().getTicks() - + off_ticks; - int key_num = ( *it )->oldKey() + off_key; + int pos_ticks = note->oldPos().getTicks() + off_ticks; + int key_num = note->oldKey() + off_key; - if( pos_ticks < 0 ) - { - pos_ticks = 0; - } + // ticks can't be negative + pos_ticks = qMax(0, pos_ticks); // upper/lower bound checks on key_num - if( key_num < 0 ) - { - key_num = 0; - } - else if( key_num > NumKeys ) - { - key_num = NumKeys; - } + key_num = qMax(0, key_num); + key_num = qMin(key_num, NumKeys); - ( *it )->setPos( MidiTime( pos_ticks ) ); - ( *it )->setKey( key_num ); + note->setPos( MidiTime( pos_ticks ) ); + note->setKey( key_num ); } else if( m_action == ActionResizeNote ) { // resizing note - int ticks_new = ( *it )->oldLength().getTicks() - + off_ticks; + int ticks_new = note->oldLength().getTicks() + off_ticks; if( ticks_new <= 0 ) { ticks_new = 1; @@ -2850,20 +2839,20 @@ void PianoRoll::dragNotes( int x, int y, bool alt, bool shift ) shift_ref_pos = pos; } } - ( *it )->setLength( MidiTime( ticks_new ) ); + note->setLength( MidiTime( ticks_new ) ); - m_lenOfNewNotes = ( *it )->length(); + m_lenOfNewNotes = note->length(); } else if( m_action == ActionMoveNote && ( shift && ! m_startedWithShift ) ) { // quick resize, toggled by holding shift after starting a note move, but not before - int ticks_new = ( *it )->oldLength().getTicks() + off_ticks; + int ticks_new = note->oldLength().getTicks() + off_ticks; if( ticks_new <= 0 ) { ticks_new = 1; } - ( *it )->setLength( MidiTime( ticks_new ) ); - m_lenOfNewNotes = ( *it )->length(); + note->setLength( MidiTime( ticks_new ) ); + m_lenOfNewNotes = note->length(); } } ++it; @@ -2876,7 +2865,9 @@ void PianoRoll::dragNotes( int x, int y, bool alt, bool shift ) static QString calculateNoteLabel(QString note, int octave) { if(note.isEmpty()) + { return ""; + } return note + QString::number(octave); } @@ -2910,7 +2901,7 @@ static void printNoteHeights(QPainter& p, int bottom, int width, int startKey) y -= KEY_LINE_HEIGHT, key++) { const unsigned note = key % KeysPerOctave; - assert( note < ( sizeof( labels ) / sizeof( *labels) )); + assert( note < ( sizeof( labels ) / sizeof( labels[0] ) )); const KeyLabel& noteLabel( labels[note] ); const int octave = key / KeysPerOctave; const KeyLabel notes = { @@ -2919,7 +2910,6 @@ static void printNoteHeights(QPainter& p, int bottom, int width, int startKey) calculateNoteLabel(noteLabel.major, octave), }; - const int drawWidth( width - WHITE_KEY_WIDTH ); const int hspace = 300; const int columnCount = drawWidth/hspace + 1; @@ -2947,7 +2937,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) QBrush bgColor = p.background(); // fill with bg color - p.fillRect( 0,0, width(), height(), bgColor ); + p.fillRect( 0, 0, width(), height(), bgColor ); // set font-size to 8 p.setFont( pointSize<8>( p.font() ) ); @@ -2958,8 +2948,8 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) // calculate y_offset according to first key switch( prKeyOrder[m_startKey % KeysPerOctave] ) { - case PR_BLACK_KEY: y_offset = KEY_LINE_HEIGHT/4; break; - case PR_WHITE_KEY_BIG: y_offset = KEY_LINE_HEIGHT/2; break; + case PR_BLACK_KEY: y_offset = KEY_LINE_HEIGHT / 4; break; + case PR_WHITE_KEY_BIG: y_offset = KEY_LINE_HEIGHT / 2; break; case PR_WHITE_KEY_SMALL: if( prKeyOrder[( ( m_startKey + 1 ) % KeysPerOctave)] != PR_BLACK_KEY ) @@ -2990,7 +2980,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) break; } - p.fillRect( WHITE_KEY_WIDTH+1, y-KEY_LINE_HEIGHT/2, + p.fillRect( WHITE_KEY_WIDTH + 1, y - KEY_LINE_HEIGHT / 2, width() - 10, KEY_LINE_HEIGHT, QColor( 0, 80 - ( key_num % KeysPerOctave ) * 3, 64 + key_num / 2) ); } @@ -3034,8 +3024,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) y -= WHITE_KEY_SMALL_HEIGHT; } - else if( prKeyOrder[key % KeysPerOctave] == - PR_WHITE_KEY_BIG ) + else if( prKeyOrder[key % KeysPerOctave] == PR_WHITE_KEY_BIG ) { // draw a big one while checking if it is pressed or not if( hasValidPattern() && m_pattern->instrumentTrack()->pianoModel()->isKeyPressed( key ) ) @@ -3060,7 +3049,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) { const QString cLabel = "C" + QString::number( static_cast( key / KeysPerOctave ) ); p.setPen( QColor( 240, 240, 240 ) ); - p.drawText( C_KEY_LABEL_X + 1, y+14, cLabel ); + p.drawText( C_KEY_LABEL_X + 1, y + 14, cLabel ); p.setPen( QColor( 0, 0, 0 ) ); p.drawText( C_KEY_LABEL_X, y + 13, cLabel ); horizCol.setAlpha( 192 ); @@ -3148,7 +3137,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) // erase the area below the piano, because there might be keys that // should be only half-visible p.fillRect( QRect( 0, keyAreaBottom(), - WHITE_KEY_WIDTH, noteEditBottom()-keyAreaBottom() ), bgColor ); + WHITE_KEY_WIDTH, noteEditBottom() - keyAreaBottom() ), bgColor ); // display note editing info QFont f = p.font(); @@ -3212,7 +3201,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) p.setPen( vertCol ); } - p.drawLine( (int)x, PR_TOP_MARGIN, (int)x, height() - + p.drawLine( (int) x, PR_TOP_MARGIN, (int) x, height() - PR_BOTTOM_MARGIN ); // extra 32nd's line @@ -3220,8 +3209,8 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) { vertCol.setAlpha( 80 ); p.setPen( vertCol ); - p.drawLine( (int)(x + pp16th/2) , PR_TOP_MARGIN, - (int)(x + pp16th/2), height() - + p.drawLine( (int)(x + pp16th / 2) , PR_TOP_MARGIN, + (int)(x + pp16th / 2), height() - PR_BOTTOM_MARGIN ); } } @@ -3248,7 +3237,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) } int y_base = keyAreaBottom() - 1; - if( hasValidPattern() == true ) + if( hasValidPattern() ) { p.setClipRect( WHITE_KEY_WIDTH, PR_TOP_MARGIN, width() - WHITE_KEY_WIDTH, @@ -3260,11 +3249,12 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) KEY_LINE_HEIGHT + 2; QPolygon editHandles; + NoteVector::ConstIterator it; - for( NoteVector::ConstIterator it = notes.begin(); - it != notes.end(); ++it ) + for( it = notes.begin(); it != notes.end(); ++it ) { - int len_ticks = ( *it )->length(); + Note *note = *it; + int len_ticks = note->length(); if( len_ticks == 0 ) { @@ -3275,17 +3265,15 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) len_ticks = 4; } - const int key = ( *it )->key() - m_startKey + 1; + const int key = note->key() - m_startKey + 1; - int pos_ticks = ( *it )->pos(); + int pos_ticks = note->pos(); - int note_width = len_ticks * m_ppt / - MidiTime::ticksPerTact(); + int note_width = len_ticks * m_ppt / MidiTime::ticksPerTact(); const int x = ( pos_ticks - m_currentPosition ) * m_ppt / MidiTime::ticksPerTact(); // skip this note if not in visible area at all - if( !( x + note_width >= 0 && - x <= width() - WHITE_KEY_WIDTH ) ) + if( !( x + note_width >= 0 && x <= width() - WHITE_KEY_WIDTH ) ) { continue; } @@ -3298,22 +3286,22 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) // note drawNoteRect( p, x + WHITE_KEY_WIDTH, y_base - key * KEY_LINE_HEIGHT, - note_width, *it, noteColor() ); + note_width, note, noteColor() ); } // draw note editing stuff int editHandleTop = 0; if( m_noteEditMode == NoteEditVolume ) { - QColor color = barColor().lighter( 30 + ( ( *it )->getVolume() * 90 / MaxVolume ) ); - if( ( *it )->selected() ) + QColor color = barColor().lighter( 30 + ( note->getVolume() * 90 / MaxVolume ) ); + if( note->selected() ) { color.setRgb( 0x00, 0x40, 0xC0 ); } p.setPen( QPen( color, NE_LINE_WIDTH ) ); editHandleTop = noteEditBottom() - - ( (float)( ( *it )->getVolume() - MinVolume ) ) / + ( (float)( note->getVolume() - MinVolume ) ) / ( (float)( MaxVolume - MinVolume ) ) * ( (float)( noteEditBottom() - noteEditTop() ) ); @@ -3324,7 +3312,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) else if( m_noteEditMode == NoteEditPanning ) { QColor color( noteColor() ); - if( ( *it )->selected() ) + if( note->selected() ) { color.setRgb( 0x00, 0x40, 0xC0 ); } @@ -3332,7 +3320,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) p.setPen( QPen( color, NE_LINE_WIDTH ) ); editHandleTop = noteEditBottom() - - ( (float)( ( *it )->getPanning() - PanningLeft ) ) / + ( (float)( note->getPanning() - PanningLeft ) ) / ( (float)( (PanningRight - PanningLeft ) ) ) * ( (float)( noteEditBottom() - noteEditTop() ) ); @@ -3343,7 +3331,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) editHandles << QPoint( x + noteEditLeft(), editHandleTop+1 ); - if( ( *it )->hasDetuningInfo() ) + if( note->hasDetuningInfo() ) { drawDetuningInfo( p, *it, x + WHITE_KEY_WIDTH, @@ -3383,7 +3371,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) p.drawRect( x + WHITE_KEY_WIDTH, y, w, h ); // TODO: Get this out of paint event - int l = ( hasValidPattern() == true )? (int) m_pattern->length() : 0; + int l = ( hasValidPattern() )? (int) m_pattern->length() : 0; // reset scroll-range if( m_leftRightScroll->maximum() != l ) @@ -3396,7 +3384,7 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) horizCol.setAlpha( 64 ); // horizontal line for the key under the cursor - if( hasValidPattern() == true ) + if( hasValidPattern() ) { int key_num = getKey( mapFromGlobal( QCursor::pos() ).y() ); p.fillRect( 10, keyAreaBottom() + 3 - KEY_LINE_HEIGHT * @@ -3448,7 +3436,8 @@ void PianoRoll::paintEvent( QPaintEvent * _pe ) // responsible for moving/resizing scrollbars after window-resizing void PianoRoll::resizeEvent( QResizeEvent * ) { - m_leftRightScroll->setGeometry( WHITE_KEY_WIDTH, height() - + m_leftRightScroll->setGeometry( WHITE_KEY_WIDTH, + height() - SCROLLBAR_SIZE, width()-WHITE_KEY_WIDTH, SCROLLBAR_SIZE ); @@ -3489,9 +3478,9 @@ void PianoRoll::wheelEvent( QWheelEvent * _we ) // get values for going through notes int pixel_range = 8; int x = _we->x() - WHITE_KEY_WIDTH; - int ticks_start = ( x-pixel_range/2 ) * + int ticks_start = ( x - pixel_range / 2 ) * MidiTime::ticksPerTact() / m_ppt + m_currentPosition; - int ticks_end = ( x+pixel_range/2 ) * + int ticks_end = ( x + pixel_range / 2 ) * MidiTime::ticksPerTact() / m_ppt + m_currentPosition; // get note-vector of current pattern @@ -3652,7 +3641,7 @@ Song::PlayModes PianoRoll::desiredPlayModeForAccompany() const void PianoRoll::play() { - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { return; } @@ -3676,7 +3665,7 @@ void PianoRoll::record() { stop(); } - if( m_recording == true || hasValidPattern() == false ) + if( m_recording || ! hasValidPattern() ) { return; } @@ -3695,7 +3684,7 @@ void PianoRoll::recordAccompany() { stop(); } - if( m_recording == true || hasValidPattern() == false ) + if( m_recording || ! hasValidPattern() ) { return; } @@ -3728,7 +3717,7 @@ void PianoRoll::stop() void PianoRoll::startRecordNote( const Note & _n ) { - if( m_recording == true && hasValidPattern() == true && + if( m_recording && hasValidPattern() && Engine::getSong()->isPlaying() && ( Engine::getSong()->playMode() == desiredPlayModeForAccompany() || @@ -3755,7 +3744,7 @@ void PianoRoll::startRecordNote( const Note & _n ) void PianoRoll::finishRecordNote( const Note & _n ) { - if( m_recording == true && hasValidPattern() == true && + if( m_recording && hasValidPattern() && Engine::getSong()->isPlaying() && ( Engine::getSong()->playMode() == desiredPlayModeForAccompany() || @@ -3840,7 +3829,7 @@ void PianoRoll::detuneButtonToggled() void PianoRoll::selectAll() { - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { return; } @@ -3849,16 +3838,18 @@ void PianoRoll::selectAll() // if first_time = true, we HAVE to set the vars for select bool first_time = true; + NoteVector::ConstIterator it; - for( NoteVector::ConstIterator it = notes.begin(); it != notes.end(); ++it ) + for( it = notes.begin(); it != notes.end(); ++it ) { - int len_ticks = ( *it )->length(); + Note *note = *it; + int len_ticks = note->length(); if( len_ticks > 0 ) { - const int key = ( *it )->key(); + const int key = note->key(); - int pos_ticks = ( *it )->pos(); + int pos_ticks = note->pos(); if( key <= m_selectStartKey || first_time ) { // if we move start-key down, we have to add @@ -3898,19 +3889,20 @@ void PianoRoll::selectAll() // returns vector with pointers to all selected notes void PianoRoll::getSelectedNotes( NoteVector & _selected_notes ) { - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { return; } const NoteVector & notes = m_pattern->notes(); + NoteVector::ConstIterator it; - for( NoteVector::ConstIterator it = notes.begin(); it != notes.end(); - ++it ) + for( it = notes.begin(); it != notes.end(); ++it ) { - if( ( *it )->selected() ) + Note *note = *it; + if( note->selected() ) { - _selected_notes.push_back( *it ); + _selected_notes.push_back( note ); } } } @@ -3990,7 +3982,7 @@ void PianoRoll::copySelectedNotes() NoteVector selected_notes; getSelectedNotes( selected_notes ); - if( selected_notes.empty() == false ) + if( ! selected_notes.empty() ) { copy_to_clipboard( selected_notes ); } @@ -4001,7 +3993,7 @@ void PianoRoll::copySelectedNotes() void PianoRoll::cutSelectedNotes() { - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { return; } @@ -4009,18 +4001,19 @@ void PianoRoll::cutSelectedNotes() NoteVector selected_notes; getSelectedNotes( selected_notes ); - if( selected_notes.empty() == false ) + if( ! selected_notes.empty() ) { copy_to_clipboard( selected_notes ); Engine::getSong()->setModified(); + NoteVector::Iterator it; - for( NoteVector::Iterator it = selected_notes.begin(); - it != selected_notes.end(); ++it ) + for( it = selected_notes.begin(); it != selected_notes.end(); ++it ) { + Note *note = *it; // note (the memory of it) is also deleted by // pattern::removeNote(...) so we don't have to do that - m_pattern->removeNote( *it ); + m_pattern->removeNote( note ); } } @@ -4033,7 +4026,7 @@ void PianoRoll::cutSelectedNotes() void PianoRoll::pasteNotes() { - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { return; } @@ -4042,7 +4035,7 @@ void PianoRoll::pasteNotes() ->mimeData( QClipboard::Clipboard ) ->data( Clipboard::mimeType() ); - if( !value.isEmpty() ) + if( ! value.isEmpty() ) { DataFile dataFile( value.toUtf8() ); @@ -4051,12 +4044,12 @@ void PianoRoll::pasteNotes() // remove selection and select the newly pasted notes clearSelectedNotes(); - if( !list.isEmpty() ) + if( ! list.isEmpty() ) { m_pattern->addJournalCheckPoint(); } - for( int i = 0; !list.item( i ).isNull(); ++i ) + for( int i = 0; ! list.item( i ).isNull(); ++i ) { // create the note Note cur_note; @@ -4085,7 +4078,7 @@ void PianoRoll::pasteNotes() void PianoRoll::deleteSelectedNotes() { - if( hasValidPattern() == false ) + if( ! hasValidPattern() ) { return; } @@ -4101,10 +4094,11 @@ void PianoRoll::deleteSelectedNotes() NoteVector::ConstIterator it = notes.begin(); while( it != notes.end() ) { - if( ( *it )->selected() ) + Note *note = *it; + if( note->selected() ) { // delete this note - m_pattern->removeNote( ( *it ) ); + m_pattern->removeNote( note ); update_after_delete = true; // start over, make sure we get all the notes @@ -4116,7 +4110,7 @@ void PianoRoll::deleteSelectedNotes() } } - if( update_after_delete == true ) + if( update_after_delete ) { Engine::getSong()->setModified(); update(); @@ -4133,15 +4127,13 @@ void PianoRoll::autoScroll( const MidiTime & _t ) const int w = width() - WHITE_KEY_WIDTH; if( _t > m_currentPosition + w * MidiTime::ticksPerTact() / m_ppt ) { - m_leftRightScroll->setValue( _t.getTact() * - MidiTime::ticksPerTact() ); + m_leftRightScroll->setValue( _t.getTact() * MidiTime::ticksPerTact() ); } else if( _t < m_currentPosition ) { MidiTime t = qMax( _t - w * MidiTime::ticksPerTact() * - MidiTime::ticksPerTact() / m_ppt, 0 ); - m_leftRightScroll->setValue( t.getTact() * - MidiTime::ticksPerTact() ); + MidiTime::ticksPerTact() / m_ppt, (tick_t) 0 ); + m_leftRightScroll->setValue( t.getTact() * MidiTime::ticksPerTact() ); } m_scrollBack = false; } @@ -4151,11 +4143,10 @@ void PianoRoll::autoScroll( const MidiTime & _t ) void PianoRoll::updatePosition( const MidiTime & _t ) { - if( ( Engine::getSong()->isPlaying() && - Engine::getSong()->playMode() == - Song::Mode_PlayPattern && - m_timeLine->autoScroll() == Timeline::AutoScrollEnabled ) || - m_scrollBack == true ) + if( ( Engine::getSong()->isPlaying() + && Engine::getSong()->playMode() == Song::Mode_PlayPattern + && m_timeLine->autoScroll() == Timeline::AutoScrollEnabled + ) || m_scrollBack ) { autoScroll( _t ); } @@ -4196,7 +4187,6 @@ void PianoRoll::zoomingChanged() m_timeLine->setPixelsPerTact( m_ppt ); update(); - } @@ -4222,8 +4212,7 @@ int PianoRoll::quantization() const } } return DefaultTicksPerTact / m_quantizeModel.currentText().right( - m_quantizeModel.currentText().length() - - 2 ).toInt(); + m_quantizeModel.currentText().length() - 2 ).toInt(); } @@ -4251,8 +4240,7 @@ MidiTime PianoRoll::newNoteLen() const return m_lenOfNewNotes; } return DefaultTicksPerTact / m_noteLenModel.currentText().right( - m_noteLenModel.currentText().length() - - 2 ).toInt(); + m_noteLenModel.currentText().length() - 2 ).toInt(); } @@ -4285,17 +4273,19 @@ Note * PianoRoll::noteUnderMouse() MidiTime::ticksPerTact() / m_ppt + m_currentPosition; // will be our iterator in the following loop - NoteVector::ConstIterator it = notes.begin()+notes.size()-1; + NoteVector::ConstIterator it = notes.end() - 1; + Note *note = *it; // loop through whole note-vector... int i; for( i = 0; i < notes.size(); ++i ) { + note = *it; // and check whether the cursor is over an // existing note - if( pos_ticks >= ( *it )->pos() && - pos_ticks <= ( *it )->pos() + ( *it )->length() && - ( *it )->key() == key_num && ( *it )->length() > 0 ) + if( pos_ticks >= note->pos() && + pos_ticks <= note->endPos() && + note->key() == key_num && note->length() > 0 ) { break; } @@ -4307,5 +4297,5 @@ Note * PianoRoll::noteUnderMouse() return NULL; } - return *it; + return note; } diff --git a/src/tracks/Pattern.cpp b/src/tracks/Pattern.cpp index e527acd03..4c95aa6fc 100644 --- a/src/tracks/Pattern.cpp +++ b/src/tracks/Pattern.cpp @@ -499,12 +499,15 @@ void Pattern::ensureBeatNotes() for( int i = 0; i < m_steps; ++i ) { bool found = false; - for( NoteVector::Iterator it = m_notes.begin(); it != m_notes.end(); ++it ) + NoteVector::Iterator it; + + for( it = m_notes.begin(); it != m_notes.end(); ++it ) { + Note *note = *it; // if a note in this position is the one we want - if( ( *it )->pos() == + if( note->pos() == ( i * MidiTime::ticksPerTact() ) / MidiTime::stepsPerTact() - && ( *it )->length() <= 0 ) + && note->length() <= 0 ) { found = true; break; @@ -524,10 +527,12 @@ void Pattern::ensureBeatNotes() for( NoteVector::Iterator it = m_notes.begin(); it != m_notes.end(); ) { bool needed = false; + Note *note = *it; + for( int i = 0; i < m_steps; ++i ) { - if( ( *it )->pos() == ( i * MidiTime::ticksPerTact() ) / MidiTime::stepsPerTact() - || ( *it )->length() != 0 ) + if( note->pos() == ( i * MidiTime::ticksPerTact() ) / MidiTime::stepsPerTact() + || note->length() != 0 ) { needed = true; break; @@ -535,10 +540,12 @@ void Pattern::ensureBeatNotes() } if( needed == false ) { - delete *it; + delete note; it = m_notes.erase( it ); } - else ++it; + else { + ++it; + } } }