| 1 | /* |
|---|
| 2 | * Hydrogen |
|---|
| 3 | * Copyright(c) 2002-2007 by Alex >Comix< Cominu [comix@users.sourceforge.net] |
|---|
| 4 | * |
|---|
| 5 | * http://www.hydrogen-music.org |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY, without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include "DrumPatternEditor.h" |
|---|
| 24 | #include "PatternEditorPanel.h" |
|---|
| 25 | #include "NotePropertiesRuler.h" |
|---|
| 26 | |
|---|
| 27 | #include <hydrogen/globals.h> |
|---|
| 28 | #include <hydrogen/Song.h> |
|---|
| 29 | #include <hydrogen/hydrogen.h> |
|---|
| 30 | #include <hydrogen/Preferences.h> |
|---|
| 31 | #include <hydrogen/event_queue.h> |
|---|
| 32 | #include <hydrogen/instrument.h> |
|---|
| 33 | #include <hydrogen/Pattern.h> |
|---|
| 34 | #include <hydrogen/note.h> |
|---|
| 35 | #include <hydrogen/audio_engine.h> |
|---|
| 36 | |
|---|
| 37 | #include "../HydrogenApp.h" |
|---|
| 38 | #include "../Mixer/Mixer.h" |
|---|
| 39 | #include "../Skin.h" |
|---|
| 40 | |
|---|
| 41 | #include <cassert> |
|---|
| 42 | #include <algorithm> |
|---|
| 43 | |
|---|
| 44 | #include <QtGui> |
|---|
| 45 | |
|---|
| 46 | using namespace std; |
|---|
| 47 | using namespace H2Core; |
|---|
| 48 | |
|---|
| 49 | DrumPatternEditor::DrumPatternEditor(QWidget* parent, PatternEditorPanel *pPanel) |
|---|
| 50 | : QWidget( parent ) |
|---|
| 51 | , Object( "DrumPatternEditor" ) |
|---|
| 52 | , m_nResolution( 8 ) |
|---|
| 53 | , m_bUseTriplets( false ) |
|---|
| 54 | , m_pBackground( NULL ) |
|---|
| 55 | , m_pTemp( NULL ) |
|---|
| 56 | , m_bRightBtnPressed( false ) |
|---|
| 57 | , m_pDraggedNote( NULL ) |
|---|
| 58 | , m_pPattern( NULL ) |
|---|
| 59 | , m_pPatternEditorPanel( pPanel ) |
|---|
| 60 | { |
|---|
| 61 | //infoLog("INIT"); |
|---|
| 62 | |
|---|
| 63 | //setAttribute(Qt::WA_NoBackground); |
|---|
| 64 | setFocusPolicy(Qt::ClickFocus); |
|---|
| 65 | |
|---|
| 66 | m_nGridWidth = Preferences::getInstance()->getPatternEditorGridWidth(); |
|---|
| 67 | m_nGridHeight = Preferences::getInstance()->getPatternEditorGridHeight(); |
|---|
| 68 | |
|---|
| 69 | unsigned nEditorWidth = 20 + m_nGridWidth * ( MAX_NOTES * 4 ); |
|---|
| 70 | m_nEditorHeight = m_nGridHeight * MAX_INSTRUMENTS; |
|---|
| 71 | |
|---|
| 72 | // Prepare pixmaps |
|---|
| 73 | m_pBackground = new QPixmap( nEditorWidth, m_nEditorHeight ); |
|---|
| 74 | m_pTemp = new QPixmap( nEditorWidth, m_nEditorHeight ); |
|---|
| 75 | |
|---|
| 76 | createBackground(); |
|---|
| 77 | |
|---|
| 78 | resize( nEditorWidth, m_nEditorHeight ); |
|---|
| 79 | |
|---|
| 80 | HydrogenApp::getInstance()->addEventListener( this ); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | DrumPatternEditor::~DrumPatternEditor() |
|---|
| 86 | { |
|---|
| 87 | //infoLog("DESTROY"); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | void DrumPatternEditor::updateEditor() |
|---|
| 93 | { |
|---|
| 94 | //cout << "*** update pattern editor" << endl; |
|---|
| 95 | |
|---|
| 96 | Hydrogen* engine = Hydrogen::get_instance(); |
|---|
| 97 | |
|---|
| 98 | // check engine state |
|---|
| 99 | int state = engine->getState(); |
|---|
| 100 | if ( (state != STATE_READY) && (state != STATE_PLAYING) ) { |
|---|
| 101 | ERRORLOG( "FIXME: skipping pattern editor update (state shoud be READY or PLAYING)" ); |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | Hydrogen *pEngine = Hydrogen::get_instance(); |
|---|
| 106 | PatternList *pPatternList = pEngine->getSong()->getPatternList(); |
|---|
| 107 | int nSelectedPatternNumber = pEngine->getSelectedPatternNumber(); |
|---|
| 108 | if ( (nSelectedPatternNumber != -1) && ( (uint)nSelectedPatternNumber < pPatternList->getSize() ) ) { |
|---|
| 109 | m_pPattern = pPatternList->get( nSelectedPatternNumber ); |
|---|
| 110 | } |
|---|
| 111 | else { |
|---|
| 112 | m_pPattern = NULL; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | uint nEditorWidth; |
|---|
| 117 | if ( m_pPattern ) { |
|---|
| 118 | nEditorWidth = 20 + m_nGridWidth * m_pPattern->m_nSize; |
|---|
| 119 | } |
|---|
| 120 | else { |
|---|
| 121 | nEditorWidth = 20 + m_nGridWidth * MAX_NOTES; |
|---|
| 122 | } |
|---|
| 123 | resize( nEditorWidth, height() ); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | createBackground(); |
|---|
| 127 | drawPattern(); |
|---|
| 128 | |
|---|
| 129 | // redraw all |
|---|
| 130 | update( 0, 0, width(), height() ); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | int DrumPatternEditor::getColumn(QMouseEvent *ev) |
|---|
| 136 | { |
|---|
| 137 | int nBase; |
|---|
| 138 | if (m_bUseTriplets) { |
|---|
| 139 | nBase = 3; |
|---|
| 140 | } |
|---|
| 141 | else { |
|---|
| 142 | nBase = 4; |
|---|
| 143 | } |
|---|
| 144 | int nWidth = (m_nGridWidth * 4 * MAX_NOTES) / (nBase * m_nResolution); |
|---|
| 145 | |
|---|
| 146 | int x = ev->x(); |
|---|
| 147 | int nColumn; |
|---|
| 148 | nColumn = x - 20 + (nWidth / 2); |
|---|
| 149 | nColumn = nColumn / nWidth; |
|---|
| 150 | nColumn = (nColumn * 4 * MAX_NOTES) / (nBase * m_nResolution); |
|---|
| 151 | return nColumn; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | void DrumPatternEditor::mousePressEvent(QMouseEvent *ev) |
|---|
| 157 | { |
|---|
| 158 | if ( m_pPattern == NULL ) { |
|---|
| 159 | return; |
|---|
| 160 | } |
|---|
| 161 | Song *pSong = Hydrogen::get_instance()->getSong(); |
|---|
| 162 | int nInstruments = pSong->getInstrumentList()->get_size(); |
|---|
| 163 | |
|---|
| 164 | // int row = (int)( (float)( height() - ev->y() ) / (float)m_nGridHeight); |
|---|
| 165 | int row = (int)( ev->y() / (float)m_nGridHeight); |
|---|
| 166 | if (row >= nInstruments) { |
|---|
| 167 | return; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | int nColumn = getColumn( ev ); |
|---|
| 171 | |
|---|
| 172 | if ( nColumn >= (int)m_pPattern->m_nSize ) { |
|---|
| 173 | update( 0, 0, width(), height() ); |
|---|
| 174 | return; |
|---|
| 175 | } |
|---|
| 176 | Instrument *pSelectedInstrument = pSong->getInstrumentList()->get( row ); |
|---|
| 177 | |
|---|
| 178 | if (ev->button() == Qt::LeftButton ) { |
|---|
| 179 | m_bRightBtnPressed = false; |
|---|
| 180 | AudioEngine::get_instance()->lock( "DrumPatternEditor::mousePressEvent" ); // lock the audio engine |
|---|
| 181 | |
|---|
| 182 | bool bNoteAlreadyExist = false; |
|---|
| 183 | std::multimap <int, Note*>::iterator pos; |
|---|
| 184 | for ( pos = m_pPattern->m_noteMap.lower_bound( nColumn ); pos != m_pPattern->m_noteMap.upper_bound( nColumn ); ++pos ) { |
|---|
| 185 | Note *pNote = pos->second; |
|---|
| 186 | assert( pNote ); |
|---|
| 187 | if ( pNote->get_instrument() == pSelectedInstrument ) { |
|---|
| 188 | // the note exists...remove it! |
|---|
| 189 | bNoteAlreadyExist = true; |
|---|
| 190 | delete pNote; |
|---|
| 191 | m_pPattern->m_noteMap.erase( pos ); |
|---|
| 192 | break; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | if ( bNoteAlreadyExist == false ) { |
|---|
| 197 | // create the new note |
|---|
| 198 | const unsigned nPosition = nColumn; |
|---|
| 199 | const float fVelocity = 0.8f; |
|---|
| 200 | const float fPan_L = 0.5f; |
|---|
| 201 | const float fPan_R = 0.5f; |
|---|
| 202 | const int nLength = -1; |
|---|
| 203 | const float fPitch = 0.0f; |
|---|
| 204 | Note *pNote = new Note( pSelectedInstrument, nPosition, fVelocity, fPan_L, fPan_R, nLength, fPitch); |
|---|
| 205 | m_pPattern->m_noteMap.insert( std::make_pair( nPosition, pNote ) ); |
|---|
| 206 | |
|---|
| 207 | // hear note |
|---|
| 208 | Preferences *pref = Preferences::getInstance(); |
|---|
| 209 | if ( pref->getHearNewNotes() ) { |
|---|
| 210 | Note *pNote2 = new Note( pSelectedInstrument, 0, fVelocity, fPan_L, fPan_R, nLength, fPitch); |
|---|
| 211 | AudioEngine::get_instance()->get_sampler()->note_on(pNote2); |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | pSong->m_bIsModified = true; |
|---|
| 215 | AudioEngine::get_instance()->unlock(); // unlock the audio engine |
|---|
| 216 | } |
|---|
| 217 | else if (ev->button() == Qt::RightButton ) { |
|---|
| 218 | m_bRightBtnPressed = true; |
|---|
| 219 | m_pDraggedNote = NULL; |
|---|
| 220 | |
|---|
| 221 | int nRealColumn = (ev->x() - 20) / (int)m_nGridWidth; |
|---|
| 222 | |
|---|
| 223 | AudioEngine::get_instance()->lock( "DrumPatternEditor::mousePressEvent" ); |
|---|
| 224 | |
|---|
| 225 | std::multimap <int, Note*>::iterator pos; |
|---|
| 226 | for ( pos = m_pPattern->m_noteMap.lower_bound( nColumn ); pos != m_pPattern->m_noteMap.upper_bound( nColumn ); ++pos ) { |
|---|
| 227 | Note *pNote = pos->second; |
|---|
| 228 | assert( pNote ); |
|---|
| 229 | |
|---|
| 230 | if ( pNote->get_instrument() == pSelectedInstrument ) { |
|---|
| 231 | //INFOLOG( "TROVATA NOTA!" ); |
|---|
| 232 | m_pDraggedNote = pNote; |
|---|
| 233 | break; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | if ( !m_pDraggedNote ) { |
|---|
| 237 | for ( pos = m_pPattern->m_noteMap.lower_bound( nRealColumn ); pos != m_pPattern->m_noteMap.upper_bound( nRealColumn ); ++pos ) { |
|---|
| 238 | Note *pNote = pos->second; |
|---|
| 239 | assert( pNote ); |
|---|
| 240 | |
|---|
| 241 | if ( pNote->get_instrument() == pSelectedInstrument ) { |
|---|
| 242 | //INFOLOG( "TROVATA NOTA!" ); |
|---|
| 243 | m_pDraggedNote = pNote; |
|---|
| 244 | break; |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | // potrei essere sulla coda di una nota precedente.. |
|---|
| 249 | for ( int nCol = 0; nCol < nRealColumn; ++nCol ) { |
|---|
| 250 | if ( m_pDraggedNote ) break; |
|---|
| 251 | for ( pos = m_pPattern->m_noteMap.lower_bound( nCol ); pos != m_pPattern->m_noteMap.upper_bound( nCol ); ++pos ) { |
|---|
| 252 | Note *pNote = pos->second; |
|---|
| 253 | assert( pNote ); |
|---|
| 254 | |
|---|
| 255 | if ( pNote->get_instrument() == pSelectedInstrument ) { |
|---|
| 256 | //INFOLOG( "TROVATA NOTA sulla coda...!" ); |
|---|
| 257 | m_pDraggedNote = pNote; |
|---|
| 258 | break; |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | AudioEngine::get_instance()->unlock(); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | // update the selected line |
|---|
| 266 | int nSelectedInstrument = Hydrogen::get_instance()->getSelectedInstrumentNumber(); |
|---|
| 267 | if (nSelectedInstrument != row) { |
|---|
| 268 | //m_pPatternEditorPanel->setSelectedInstrument(row); |
|---|
| 269 | Hydrogen::get_instance()->setSelectedInstrumentNumber( row ); |
|---|
| 270 | } |
|---|
| 271 | else { |
|---|
| 272 | //createBackground(); |
|---|
| 273 | drawPattern(); |
|---|
| 274 | update( 0, 0, width(), height() ); |
|---|
| 275 | m_pPatternEditorPanel->getVelocityEditor()->updateEditor(); |
|---|
| 276 | m_pPatternEditorPanel->getPanEditor()->updateEditor(); |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | void DrumPatternEditor::mouseReleaseEvent(QMouseEvent *ev) |
|---|
| 283 | { |
|---|
| 284 | UNUSED( ev ); |
|---|
| 285 | setCursor( QCursor( Qt::ArrowCursor ) ); |
|---|
| 286 | |
|---|
| 287 | if (m_pPattern == NULL) { |
|---|
| 288 | return; |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | void DrumPatternEditor::mouseMoveEvent(QMouseEvent *ev) |
|---|
| 295 | { |
|---|
| 296 | if (m_pPattern == NULL) { |
|---|
| 297 | return; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | int row = MAX_INSTRUMENTS - 1 - (ev->y() / (int)m_nGridHeight); |
|---|
| 301 | if (row >= MAX_INSTRUMENTS) { |
|---|
| 302 | return; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | if (m_bRightBtnPressed && m_pDraggedNote ) { |
|---|
| 306 | int nTickColumn = getColumn( ev ); |
|---|
| 307 | |
|---|
| 308 | AudioEngine::get_instance()->lock("DrumPatternEditor::mouseMoveEvent"); // lock the audio engine |
|---|
| 309 | int nLen = nTickColumn - (int)m_pDraggedNote->get_position(); |
|---|
| 310 | |
|---|
| 311 | if (nLen <= 0) { |
|---|
| 312 | nLen = -1; |
|---|
| 313 | } |
|---|
| 314 | m_pDraggedNote->set_lenght( nLen ); |
|---|
| 315 | |
|---|
| 316 | Hydrogen::get_instance()->getSong()->m_bIsModified = true; |
|---|
| 317 | AudioEngine::get_instance()->unlock(); // unlock the audio engine |
|---|
| 318 | |
|---|
| 319 | drawPattern(); |
|---|
| 320 | update( 0, 0, width(), height() ); |
|---|
| 321 | m_pPatternEditorPanel->getVelocityEditor()->updateEditor(); |
|---|
| 322 | m_pPatternEditorPanel->getPanEditor()->updateEditor(); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | void DrumPatternEditor::keyPressEvent (QKeyEvent *ev) |
|---|
| 328 | { |
|---|
| 329 | ev->ignore(); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | /// |
|---|
| 335 | /// Draws a pattern |
|---|
| 336 | /// |
|---|
| 337 | void DrumPatternEditor::drawPattern() |
|---|
| 338 | { |
|---|
| 339 | //cout << "draw pattern" << endl; |
|---|
| 340 | static const UIStyle *pStyle = Preferences::getInstance()->getDefaultUIStyle(); |
|---|
| 341 | static const QColor selectedRowColor( pStyle->m_patternEditor_selectedRowColor.getRed(), pStyle->m_patternEditor_selectedRowColor.getGreen(), pStyle->m_patternEditor_selectedRowColor.getBlue() ); |
|---|
| 342 | |
|---|
| 343 | QPainter painter( m_pTemp ); |
|---|
| 344 | |
|---|
| 345 | // copy the background image |
|---|
| 346 | painter.drawPixmap( rect(), *m_pBackground, rect() ); |
|---|
| 347 | |
|---|
| 348 | QPainter* p = &painter; |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | if (m_pPattern == NULL) { |
|---|
| 352 | return; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | int nNotes = m_pPattern->m_nSize; |
|---|
| 356 | int nSelectedInstrument = Hydrogen::get_instance()->getSelectedInstrumentNumber(); |
|---|
| 357 | Song *pSong = Hydrogen::get_instance()->getSong(); |
|---|
| 358 | |
|---|
| 359 | InstrumentList * pInstrList = pSong->getInstrumentList(); |
|---|
| 360 | |
|---|
| 361 | if ( m_nEditorHeight != (int)( m_nGridHeight * pInstrList->get_size() ) ) { |
|---|
| 362 | // the number of instruments is changed...recreate all |
|---|
| 363 | m_nEditorHeight = m_nGridHeight * pInstrList->get_size(); |
|---|
| 364 | resize( width(), m_nEditorHeight ); |
|---|
| 365 | createBackground(); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | for ( uint nInstr = 0; nInstr < pInstrList->get_size(); ++nInstr ) { |
|---|
| 370 | //Instrument *pInstr = pSong->getInstrumentList()->get( nInstr ); |
|---|
| 371 | uint y = m_nGridHeight * nInstr; |
|---|
| 372 | if ( nInstr == (uint)nSelectedInstrument ) { // selected instrument |
|---|
| 373 | p->fillRect( 0, y + 1, (20 + nNotes * m_nGridWidth), m_nGridHeight - 1, selectedRowColor ); |
|---|
| 374 | } |
|---|
| 375 | //if ( pInstr->m_bIsMuted ) { |
|---|
| 376 | // p->fillRect( 0, y + 1, ( 20 + nNotes * m_nGridWidth), m_nGridHeight - 1, muteRowColor ); |
|---|
| 377 | //} |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | // draw the grid |
|---|
| 382 | drawGrid( p ); |
|---|
| 383 | |
|---|
| 384 | std::multimap <int, Note*>::iterator pos; |
|---|
| 385 | for ( pos = m_pPattern->m_noteMap.begin(); pos != m_pPattern->m_noteMap.end(); pos++ ) { |
|---|
| 386 | Note *note = pos->second; |
|---|
| 387 | assert( note ); |
|---|
| 388 | drawNote( note, p ); |
|---|
| 389 | } |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | /// |
|---|
| 394 | /// Draws a note |
|---|
| 395 | /// |
|---|
| 396 | void DrumPatternEditor::drawNote( Note *note, QPainter* p ) |
|---|
| 397 | { |
|---|
| 398 | static const UIStyle *pStyle = Preferences::getInstance()->getDefaultUIStyle(); |
|---|
| 399 | static const QColor noteColor( pStyle->m_patternEditor_noteColor.getRed(), pStyle->m_patternEditor_noteColor.getGreen(), pStyle->m_patternEditor_noteColor.getBlue() ); |
|---|
| 400 | |
|---|
| 401 | p->setRenderHint( QPainter::Antialiasing ); |
|---|
| 402 | |
|---|
| 403 | int nInstrument = -1; |
|---|
| 404 | InstrumentList * pInstrList = Hydrogen::get_instance()->getSong()->getInstrumentList(); |
|---|
| 405 | for ( uint nInstr = 0; nInstr < pInstrList->get_size(); ++nInstr ) { |
|---|
| 406 | Instrument *pInstr = pInstrList->get( nInstr ); |
|---|
| 407 | if ( pInstr == note->get_instrument() ) { |
|---|
| 408 | nInstrument = nInstr; |
|---|
| 409 | break; |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | if ( nInstrument == -1 ) { |
|---|
| 413 | ERRORLOG( "Instrument not found..skipping note" ); |
|---|
| 414 | return; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | uint pos = note->get_position(); |
|---|
| 418 | |
|---|
| 419 | p->setPen( noteColor ); |
|---|
| 420 | |
|---|
| 421 | if ( note->get_lenght() == -1 ) { // trigger note |
|---|
| 422 | uint x_pos = 20 + (pos * m_nGridWidth);// - m_nGridWidth / 2.0; |
|---|
| 423 | |
|---|
| 424 | uint y_pos = ( nInstrument * m_nGridHeight) + (m_nGridHeight / 2) - 3; |
|---|
| 425 | |
|---|
| 426 | // draw the "dot" |
|---|
| 427 | p->drawLine(x_pos, y_pos, x_pos + 3, y_pos + 3); // A |
|---|
| 428 | p->drawLine(x_pos, y_pos, x_pos - 3, y_pos + 3); // B |
|---|
| 429 | p->drawLine(x_pos, y_pos + 6, x_pos + 3, y_pos + 3); // C |
|---|
| 430 | p->drawLine(x_pos - 3, y_pos + 3, x_pos, y_pos + 6); // D |
|---|
| 431 | |
|---|
| 432 | p->drawLine(x_pos, y_pos + 1, x_pos + 2, y_pos + 3); |
|---|
| 433 | p->drawLine(x_pos, y_pos + 1, x_pos - 2, y_pos + 3); |
|---|
| 434 | p->drawLine(x_pos, y_pos + 5, x_pos + 2, y_pos + 3); |
|---|
| 435 | p->drawLine(x_pos - 2, y_pos + 3, x_pos, y_pos + 5); |
|---|
| 436 | |
|---|
| 437 | p->drawLine(x_pos, y_pos + 2, x_pos + 1, y_pos + 3); |
|---|
| 438 | p->drawLine(x_pos, y_pos + 2, x_pos - 1, y_pos + 3); |
|---|
| 439 | p->drawLine(x_pos, y_pos + 4, x_pos + 1, y_pos + 3); |
|---|
| 440 | p->drawLine(x_pos - 1, y_pos + 3, x_pos, y_pos + 4); |
|---|
| 441 | } |
|---|
| 442 | else { |
|---|
| 443 | uint x = 20 + (pos * m_nGridWidth); |
|---|
| 444 | int w = m_nGridWidth * note->get_lenght(); |
|---|
| 445 | w = w - 1; // lascio un piccolo spazio tra una nota ed un altra |
|---|
| 446 | |
|---|
| 447 | int y = (int) ( ( nInstrument ) * m_nGridHeight + (m_nGridHeight / 100.0 * 30.0) ); |
|---|
| 448 | int h = (int) (m_nGridHeight - ((m_nGridHeight / 100.0 * 30.0) * 2.0) ); |
|---|
| 449 | |
|---|
| 450 | p->fillRect( x, y + 1, w, h + 1, QColor(100, 100, 200) ); /// \todo: definire questo colore nelle preferenze |
|---|
| 451 | p->drawRect( x, y + 1, w, h + 1 ); |
|---|
| 452 | } |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | void DrumPatternEditor::drawGrid( QPainter* p ) |
|---|
| 459 | { |
|---|
| 460 | //cout << "draw grid" << endl; |
|---|
| 461 | static const UIStyle *pStyle = Preferences::getInstance()->getDefaultUIStyle(); |
|---|
| 462 | static const QColor res_1( pStyle->m_patternEditor_line1Color.getRed(), pStyle->m_patternEditor_line1Color.getGreen(), pStyle->m_patternEditor_line1Color.getBlue() ); |
|---|
| 463 | static const QColor res_2( pStyle->m_patternEditor_line2Color.getRed(), pStyle->m_patternEditor_line2Color.getGreen(), pStyle->m_patternEditor_line2Color.getBlue() ); |
|---|
| 464 | static const QColor res_3( pStyle->m_patternEditor_line3Color.getRed(), pStyle->m_patternEditor_line3Color.getGreen(), pStyle->m_patternEditor_line3Color.getBlue() ); |
|---|
| 465 | static const QColor res_4( pStyle->m_patternEditor_line4Color.getRed(), pStyle->m_patternEditor_line4Color.getGreen(), pStyle->m_patternEditor_line4Color.getBlue() ); |
|---|
| 466 | static const QColor res_5( pStyle->m_patternEditor_line5Color.getRed(), pStyle->m_patternEditor_line5Color.getGreen(), pStyle->m_patternEditor_line5Color.getBlue() ); |
|---|
| 467 | |
|---|
| 468 | // vertical lines |
|---|
| 469 | p->setPen( QPen( res_1, 0, Qt::DotLine ) ); |
|---|
| 470 | |
|---|
| 471 | int nBase; |
|---|
| 472 | if (m_bUseTriplets) { |
|---|
| 473 | nBase = 3; |
|---|
| 474 | } |
|---|
| 475 | else { |
|---|
| 476 | nBase = 4; |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | int n4th = 4 * MAX_NOTES / (nBase * 4); |
|---|
| 480 | int n8th = 4 * MAX_NOTES / (nBase * 8); |
|---|
| 481 | int n16th = 4 * MAX_NOTES / (nBase * 16); |
|---|
| 482 | int n32th = 4 * MAX_NOTES / (nBase * 32); |
|---|
| 483 | int n64th = 4 * MAX_NOTES / (nBase * 64); |
|---|
| 484 | |
|---|
| 485 | int nNotes = MAX_NOTES; |
|---|
| 486 | if ( m_pPattern ) { |
|---|
| 487 | nNotes = m_pPattern->m_nSize; |
|---|
| 488 | } |
|---|
| 489 | if (!m_bUseTriplets) { |
|---|
| 490 | for ( int i = 0; i < nNotes + 1; i++ ) { |
|---|
| 491 | uint x = 20 + i * m_nGridWidth; |
|---|
| 492 | |
|---|
| 493 | if ( (i % n4th) == 0 ) { |
|---|
| 494 | if (m_nResolution >= 4) { |
|---|
| 495 | p->setPen( QPen( res_1, 0 ) ); |
|---|
| 496 | p->drawLine(x, 1, x, m_nEditorHeight - 1); |
|---|
| 497 | } |
|---|
| 498 | } |
|---|
| 499 | else if ( (i % n8th) == 0 ) { |
|---|
| 500 | if (m_nResolution >= 8) { |
|---|
| 501 | p->setPen( QPen( res_2, 0 ) ); |
|---|
| 502 | p->drawLine(x, 1, x, m_nEditorHeight - 1); |
|---|
| 503 | } |
|---|
| 504 | } |
|---|
| 505 | else if ( (i % n16th) == 0 ) { |
|---|
| 506 | if (m_nResolution >= 16) { |
|---|
| 507 | p->setPen( QPen( res_3, 0 ) ); |
|---|
| 508 | p->drawLine(x, 1, x, m_nEditorHeight - 1); |
|---|
| 509 | } |
|---|
| 510 | } |
|---|
| 511 | else if ( (i % n32th) == 0 ) { |
|---|
| 512 | if (m_nResolution >= 32) { |
|---|
| 513 | p->setPen( QPen( res_4, 0 ) ); |
|---|
| 514 | p->drawLine(x, 1, x, m_nEditorHeight - 1); |
|---|
| 515 | } |
|---|
| 516 | } |
|---|
| 517 | else if ( (i % n64th) == 0 ) { |
|---|
| 518 | if (m_nResolution >= 64) { |
|---|
| 519 | p->setPen( QPen( res_5, 0 ) ); |
|---|
| 520 | p->drawLine(x, 1, x, m_nEditorHeight - 1); |
|---|
| 521 | } |
|---|
| 522 | } |
|---|
| 523 | } |
|---|
| 524 | } |
|---|
| 525 | else { // Triplets |
|---|
| 526 | uint nCounter = 0; |
|---|
| 527 | int nSize = 4 * MAX_NOTES / (nBase * m_nResolution); |
|---|
| 528 | |
|---|
| 529 | for ( int i = 0; i < nNotes + 1; i++ ) { |
|---|
| 530 | uint x = 20 + i * m_nGridWidth; |
|---|
| 531 | |
|---|
| 532 | if ( (i % nSize) == 0) { |
|---|
| 533 | if ((nCounter % 3) == 0) { |
|---|
| 534 | p->setPen( QPen( res_1, 0 ) ); |
|---|
| 535 | } |
|---|
| 536 | else { |
|---|
| 537 | p->setPen( QPen( res_3, 0 ) ); |
|---|
| 538 | } |
|---|
| 539 | p->drawLine(x, 1, x, m_nEditorHeight - 1); |
|---|
| 540 | nCounter++; |
|---|
| 541 | } |
|---|
| 542 | } |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | // fill the first half of the rect with a solid color |
|---|
| 547 | static const QColor backgroundColor( pStyle->m_patternEditor_backgroundColor.getRed(), pStyle->m_patternEditor_backgroundColor.getGreen(), pStyle->m_patternEditor_backgroundColor.getBlue() ); |
|---|
| 548 | static const QColor selectedRowColor( pStyle->m_patternEditor_selectedRowColor.getRed(), pStyle->m_patternEditor_selectedRowColor.getGreen(), pStyle->m_patternEditor_selectedRowColor.getBlue() ); |
|---|
| 549 | int nSelectedInstrument = Hydrogen::get_instance()->getSelectedInstrumentNumber(); |
|---|
| 550 | Song *pSong = Hydrogen::get_instance()->getSong(); |
|---|
| 551 | int nInstruments = pSong->getInstrumentList()->get_size(); |
|---|
| 552 | for ( uint i = 0; i < (uint)nInstruments; i++ ) { |
|---|
| 553 | uint y = m_nGridHeight * i + 1; |
|---|
| 554 | if ( i == nSelectedInstrument ) { |
|---|
| 555 | p->fillRect( 0, y, (20 + nNotes * m_nGridWidth), m_nGridHeight * 0.7, selectedRowColor ); |
|---|
| 556 | } |
|---|
| 557 | else { |
|---|
| 558 | p->fillRect( 0, y, (20 + nNotes * m_nGridWidth), m_nGridHeight * 0.7, backgroundColor ); |
|---|
| 559 | } |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | } |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | void DrumPatternEditor::createBackground() |
|---|
| 566 | { |
|---|
| 567 | //cout << "recreate background" << endl; |
|---|
| 568 | |
|---|
| 569 | static const UIStyle *pStyle = Preferences::getInstance()->getDefaultUIStyle(); |
|---|
| 570 | static const QColor backgroundColor( pStyle->m_patternEditor_backgroundColor.getRed(), pStyle->m_patternEditor_backgroundColor.getGreen(), pStyle->m_patternEditor_backgroundColor.getBlue() ); |
|---|
| 571 | static const QColor alternateRowColor( pStyle->m_patternEditor_alternateRowColor.getRed(), pStyle->m_patternEditor_alternateRowColor.getGreen(), pStyle->m_patternEditor_alternateRowColor.getBlue() ); |
|---|
| 572 | static const QColor lineColor( pStyle->m_patternEditor_lineColor.getRed(), pStyle->m_patternEditor_lineColor.getGreen(), pStyle->m_patternEditor_lineColor.getBlue() ); |
|---|
| 573 | |
|---|
| 574 | |
|---|
| 575 | int nNotes = MAX_NOTES; |
|---|
| 576 | if ( m_pPattern ) { |
|---|
| 577 | nNotes = m_pPattern->m_nSize; |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | Song *pSong = Hydrogen::get_instance()->getSong(); |
|---|
| 582 | int nInstruments = pSong->getInstrumentList()->get_size(); |
|---|
| 583 | |
|---|
| 584 | if ( m_nEditorHeight != (int)( m_nGridHeight * nInstruments ) ) { |
|---|
| 585 | // the number of instruments is changed...recreate all |
|---|
| 586 | m_nEditorHeight = m_nGridHeight * nInstruments; |
|---|
| 587 | resize( width(), m_nEditorHeight ); |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | // m_pBackground->fill( QColor( 0,0,0 ) ); |
|---|
| 593 | QPainter p( m_pBackground ); |
|---|
| 594 | |
|---|
| 595 | p.fillRect(0, 0, 20 + nNotes * m_nGridWidth, height(), backgroundColor); |
|---|
| 596 | for ( uint i = 0; i < (uint)nInstruments; i++ ) { |
|---|
| 597 | uint y = m_nGridHeight * i; |
|---|
| 598 | if ( ( i % 2) != 0) { |
|---|
| 599 | p.fillRect( 0, y, (20 + nNotes * m_nGridWidth), m_nGridHeight, alternateRowColor ); |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | // horizontal lines |
|---|
| 605 | p.setPen( lineColor ); |
|---|
| 606 | for ( uint i = 0; i < (uint)nInstruments; i++ ) { |
|---|
| 607 | uint y = m_nGridHeight * i + m_nGridHeight; |
|---|
| 608 | p.drawLine( 0, y, (20 + nNotes * m_nGridWidth), y); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | |
|---|
| 612 | p.drawLine( 0, m_nEditorHeight, (20 + nNotes * m_nGridWidth), m_nEditorHeight ); |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | void DrumPatternEditor::paintEvent( QPaintEvent *ev ) |
|---|
| 618 | { |
|---|
| 619 | //INFOLOG( "paint" ); |
|---|
| 620 | QPainter painter( this ); |
|---|
| 621 | painter.drawPixmap( ev->rect(), *m_pTemp, ev->rect() ); |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | void DrumPatternEditor::showEvent ( QShowEvent *ev ) |
|---|
| 630 | { |
|---|
| 631 | UNUSED( ev ); |
|---|
| 632 | updateEditor(); |
|---|
| 633 | } |
|---|
| 634 | |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | void DrumPatternEditor::hideEvent ( QHideEvent *ev ) |
|---|
| 638 | { |
|---|
| 639 | UNUSED( ev ); |
|---|
| 640 | // updateStart(false); |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | |
|---|
| 645 | void DrumPatternEditor::setResolution(uint res, bool bUseTriplets) |
|---|
| 646 | { |
|---|
| 647 | this->m_nResolution = res; |
|---|
| 648 | this->m_bUseTriplets = bUseTriplets; |
|---|
| 649 | |
|---|
| 650 | // redraw all |
|---|
| 651 | createBackground(); |
|---|
| 652 | drawPattern(); |
|---|
| 653 | update( 0, 0, width(), height() ); |
|---|
| 654 | m_pPatternEditorPanel->getVelocityEditor()->updateEditor(); |
|---|
| 655 | m_pPatternEditorPanel->getPanEditor()->updateEditor(); |
|---|
| 656 | //m_pPatternEditorPanel->getPitchEditor()->updateEditor(); |
|---|
| 657 | /// \todo [DrumPatternEditor::setResolution] aggiornare la risoluzione del Ruler in alto." |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | void DrumPatternEditor::zoomIn() |
|---|
| 662 | { |
|---|
| 663 | m_nGridWidth = m_nGridWidth * 2; |
|---|
| 664 | updateEditor(); |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | void DrumPatternEditor::zoomOut() |
|---|
| 670 | { |
|---|
| 671 | m_nGridWidth = m_nGridWidth / 2; |
|---|
| 672 | if (m_nGridWidth < 3) { |
|---|
| 673 | m_nGridWidth = 3; |
|---|
| 674 | } |
|---|
| 675 | updateEditor(); |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | void DrumPatternEditor::selectedInstrumentChangedEvent() |
|---|
| 680 | { |
|---|
| 681 | //cout << "instrument changed" << endl; |
|---|
| 682 | drawPattern(); |
|---|
| 683 | update( 0, 0, width(), height() ); |
|---|
| 684 | } |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | /// This method is called from another thread (audio engine) |
|---|
| 688 | void DrumPatternEditor::patternModifiedEvent() |
|---|
| 689 | { |
|---|
| 690 | //cout << "pattern modified" << endl; |
|---|
| 691 | drawPattern(); |
|---|
| 692 | update( 0, 0, width(), height() ); |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | |
|---|
| 696 | void DrumPatternEditor::patternChangedEvent() |
|---|
| 697 | { |
|---|
| 698 | //cout << "pattern changed EVENT" << endl; |
|---|
| 699 | //updateEditor(); |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | void DrumPatternEditor::selectedPatternChangedEvent() |
|---|
| 703 | { |
|---|
| 704 | //cout << "selected pattern changed EVENT" << endl; |
|---|
| 705 | updateEditor(); |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | |
|---|