| 1 | /* |
|---|
| 2 | * Hydrogen |
|---|
| 3 | * Copyright(c) 2002-2008 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 <QTimer> |
|---|
| 24 | #include <QPainter> |
|---|
| 25 | |
|---|
| 26 | #include <hydrogen/Preferences.h> |
|---|
| 27 | #include <hydrogen/hydrogen.h> |
|---|
| 28 | #include <hydrogen/Pattern.h> |
|---|
| 29 | using namespace H2Core; |
|---|
| 30 | |
|---|
| 31 | #include "PatternEditorRuler.h" |
|---|
| 32 | #include "PatternEditorPanel.h" |
|---|
| 33 | #include "../HydrogenApp.h" |
|---|
| 34 | #include "../Skin.h" |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | PatternEditorRuler::PatternEditorRuler( QWidget* parent ) |
|---|
| 38 | : QWidget( parent ) |
|---|
| 39 | , Object( "PatternEditorRuler" ) |
|---|
| 40 | { |
|---|
| 41 | setAttribute(Qt::WA_NoBackground); |
|---|
| 42 | |
|---|
| 43 | //infoLog( "INIT" ); |
|---|
| 44 | |
|---|
| 45 | Preferences *pPref = Preferences::getInstance(); |
|---|
| 46 | |
|---|
| 47 | //QColor backgroundColor(230, 230, 230); |
|---|
| 48 | UIStyle *pStyle = pPref->getDefaultUIStyle(); |
|---|
| 49 | QColor backgroundColor( pStyle->m_patternEditor_backgroundColor.getRed(), pStyle->m_patternEditor_backgroundColor.getGreen(), pStyle->m_patternEditor_backgroundColor.getBlue() ); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | m_pPattern = NULL; |
|---|
| 53 | m_nGridWidth = Preferences::getInstance()->getPatternEditorGridWidth(); |
|---|
| 54 | |
|---|
| 55 | m_nRulerWidth = 20 + m_nGridWidth * ( MAX_NOTES * 4 ); |
|---|
| 56 | m_nRulerHeight = 25; |
|---|
| 57 | |
|---|
| 58 | resize( m_nRulerWidth, m_nRulerHeight ); |
|---|
| 59 | setFixedSize( size() ); |
|---|
| 60 | |
|---|
| 61 | bool ok = m_tickPosition.load( Skin::getImagePath() + "/patternEditor/tickPosition.png" ); |
|---|
| 62 | if( ok == false ){ |
|---|
| 63 | ERRORLOG( "Error loading pixmap " ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | m_pBackground = new QPixmap( m_nRulerWidth, m_nRulerHeight ); |
|---|
| 67 | m_pBackground->fill( backgroundColor ); |
|---|
| 68 | |
|---|
| 69 | m_pTimer = new QTimer(this); |
|---|
| 70 | connect(m_pTimer, SIGNAL(timeout()), this, SLOT(updateEditor())); |
|---|
| 71 | |
|---|
| 72 | HydrogenApp::getInstance()->addEventListener( this ); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | PatternEditorRuler::~PatternEditorRuler() { |
|---|
| 78 | //infoLog( "DESTROY"); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | void PatternEditorRuler::updateStart(bool start) { |
|---|
| 84 | if (start) { |
|---|
| 85 | m_pTimer->start(50); // update ruler at 20 fps |
|---|
| 86 | } |
|---|
| 87 | else { |
|---|
| 88 | m_pTimer->stop(); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | void PatternEditorRuler::showEvent ( QShowEvent *ev ) |
|---|
| 95 | { |
|---|
| 96 | UNUSED( ev ); |
|---|
| 97 | updateEditor(); |
|---|
| 98 | updateStart(true); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | void PatternEditorRuler::hideEvent ( QHideEvent *ev ) |
|---|
| 104 | { |
|---|
| 105 | UNUSED( ev ); |
|---|
| 106 | updateStart(false); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | void PatternEditorRuler::updateEditor( bool bRedrawAll ) |
|---|
| 112 | { |
|---|
| 113 | static int oldNTicks = 0; |
|---|
| 114 | |
|---|
| 115 | Hydrogen *pEngine = Hydrogen::get_instance(); |
|---|
| 116 | PatternList *pPatternList = pEngine->getSong()->get_pattern_list(); |
|---|
| 117 | int nSelectedPatternNumber = pEngine->getSelectedPatternNumber(); |
|---|
| 118 | if ( (nSelectedPatternNumber != -1) && ( (uint)nSelectedPatternNumber < pPatternList->get_size() ) ) { |
|---|
| 119 | m_pPattern = pPatternList->get( nSelectedPatternNumber ); |
|---|
| 120 | } |
|---|
| 121 | else { |
|---|
| 122 | m_pPattern = NULL; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | bool bActive = false; // is the pattern playing now? |
|---|
| 127 | PatternList *pList = pEngine->getCurrentPatternList(); |
|---|
| 128 | for (uint i = 0; i < pList->get_size(); i++) { |
|---|
| 129 | if ( m_pPattern == pList->get(i) ) { |
|---|
| 130 | bActive = true; |
|---|
| 131 | break; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | int state = pEngine->getState(); |
|---|
| 136 | if ( ( state == STATE_PLAYING ) && (bActive) ) { |
|---|
| 137 | m_nTicks = pEngine->getTickPosition(); |
|---|
| 138 | } |
|---|
| 139 | else { |
|---|
| 140 | m_nTicks = -1; // hide the tickPosition |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | if (oldNTicks != m_nTicks) { |
|---|
| 145 | // redraw all |
|---|
| 146 | bRedrawAll = true; |
|---|
| 147 | //update( 0, 0, width(), height() ); |
|---|
| 148 | } |
|---|
| 149 | oldNTicks = m_nTicks; |
|---|
| 150 | |
|---|
| 151 | if (bRedrawAll) { |
|---|
| 152 | update( 0, 0, width(), height() ); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | void PatternEditorRuler::paintEvent( QPaintEvent *ev) |
|---|
| 159 | { |
|---|
| 160 | if (!isVisible()) { |
|---|
| 161 | return; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | QPainter painter(this); |
|---|
| 165 | |
|---|
| 166 | painter.drawPixmap( ev->rect(), *m_pBackground, ev->rect() ); |
|---|
| 167 | |
|---|
| 168 | // gray background for unusable section of pattern |
|---|
| 169 | if (m_pPattern) { |
|---|
| 170 | int nXStart = 20 + m_pPattern->get_lenght() * m_nGridWidth; |
|---|
| 171 | if ( (m_nRulerWidth - nXStart) != 0 ) { |
|---|
| 172 | painter.fillRect( nXStart, 0, m_nRulerWidth - nXStart, m_nRulerHeight, QColor(170,170,170) ); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | // numbers |
|---|
| 177 | QColor textColor( 100, 100, 100 ); |
|---|
| 178 | QColor lineColor( 170, 170, 170 ); |
|---|
| 179 | |
|---|
| 180 | Preferences *pref = Preferences::getInstance(); |
|---|
| 181 | QString family = pref->getApplicationFontFamily(); |
|---|
| 182 | int size = pref->getApplicationFontPointSize(); |
|---|
| 183 | QFont font( family, size ); |
|---|
| 184 | painter.setFont(font); |
|---|
| 185 | painter.drawLine( 0, 0, m_nRulerWidth, 0 ); |
|---|
| 186 | painter.drawLine( 0, m_nRulerHeight - 1, m_nRulerWidth - 1, m_nRulerHeight - 1); |
|---|
| 187 | |
|---|
| 188 | uint nQuarter = 48; |
|---|
| 189 | |
|---|
| 190 | for ( int i = 0; i < 64; i++ ) { |
|---|
| 191 | int nText_x = 20 + nQuarter / 4 * i * m_nGridWidth; |
|---|
| 192 | if ( ( i % 4 ) == 0 ) { |
|---|
| 193 | painter.setPen( textColor ); |
|---|
| 194 | painter.drawText( nText_x - 30, 0, 60, m_nRulerHeight, Qt::AlignCenter, to_string( i / 4 + 1 ) ); |
|---|
| 195 | } |
|---|
| 196 | else { |
|---|
| 197 | painter.setPen( lineColor ); |
|---|
| 198 | painter.drawLine( nText_x, ( m_nRulerHeight - 5 ) / 2, nText_x, m_nRulerHeight - ( (m_nRulerHeight - 5 ) / 2 ) ); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | // draw tickPosition |
|---|
| 203 | if (m_nTicks != -1) { |
|---|
| 204 | uint x = (uint)( 20 + m_nTicks * m_nGridWidth - 5 - 11 / 2.0 ); |
|---|
| 205 | painter.drawPixmap( QRect( x, height() / 2, 11, 8 ), m_tickPosition, QRect( 0, 0, 11, 8 ) ); |
|---|
| 206 | |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | void PatternEditorRuler::zoomIn() |
|---|
| 212 | { |
|---|
| 213 | if (m_nGridWidth >= 3){ |
|---|
| 214 | m_nGridWidth *= 2; |
|---|
| 215 | }else |
|---|
| 216 | { |
|---|
| 217 | m_nGridWidth *= 1.5; |
|---|
| 218 | } |
|---|
| 219 | update(); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | void PatternEditorRuler::zoomOut() |
|---|
| 224 | { |
|---|
| 225 | if ( m_nGridWidth > 1.5 ) { |
|---|
| 226 | if (m_nGridWidth > 3){ |
|---|
| 227 | m_nGridWidth /= 2; |
|---|
| 228 | }else |
|---|
| 229 | { |
|---|
| 230 | m_nGridWidth /= 1.5; |
|---|
| 231 | } |
|---|
| 232 | update(); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | void PatternEditorRuler::selectedPatternChangedEvent() |
|---|
| 238 | { |
|---|
| 239 | updateEditor( true ); |
|---|
| 240 | } |
|---|