gets the key from the given mouse-position ! \brief Get the key from the mouse position in the piano display * * First we determine it roughly by the position of the point given in * white key widths from our start. We then add in any black keys that * might have been skipped over (they take a key number, but no 'white * key' space). We then add in our starting key number. * * We the
| 326 | * \param _p The point that the mouse was pressed. |
| 327 | */ |
| 328 | int PianoView::getKeyFromMouse( const QPoint & _p ) const |
| 329 | { |
| 330 | int offset = _p.x() % PW_WHITE_KEY_WIDTH; |
| 331 | if( offset < 0 ) offset += PW_WHITE_KEY_WIDTH; |
| 332 | int key_num = ( _p.x() - offset) / PW_WHITE_KEY_WIDTH; |
| 333 | |
| 334 | for( int i = 0; i <= key_num; ++i ) |
| 335 | { |
| 336 | if ( Piano::isBlackKey( m_startKey+i ) ) |
| 337 | { |
| 338 | ++key_num; |
| 339 | } |
| 340 | } |
| 341 | for( int i = 0; i >= key_num; --i ) |
| 342 | { |
| 343 | if ( Piano::isBlackKey( m_startKey+i ) ) |
| 344 | { |
| 345 | --key_num; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | key_num += m_startKey; |
| 350 | |
| 351 | // is it a black key? |
| 352 | if( _p.y() < PIANO_BASE + PW_BLACK_KEY_HEIGHT ) |
| 353 | { |
| 354 | // then do extra checking whether the mouse-cursor is over |
| 355 | // a black key |
| 356 | if( key_num > 0 && Piano::isBlackKey( key_num-1 ) && |
| 357 | offset <= ( PW_WHITE_KEY_WIDTH / 2 ) - |
| 358 | ( PW_BLACK_KEY_WIDTH / 2 ) ) |
| 359 | { |
| 360 | --key_num; |
| 361 | } |
| 362 | if( key_num < NumKeys - 1 && Piano::isBlackKey( key_num+1 ) && |
| 363 | offset >= ( PW_WHITE_KEY_WIDTH - |
| 364 | PW_BLACK_KEY_WIDTH / 2 ) ) |
| 365 | { |
| 366 | ++key_num; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // some range-checking-stuff |
| 371 | return tLimit( key_num, 0, NumKeys - 1 ); |
| 372 | } |
| 373 | |
| 374 | |
| 375 |