| 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 | #include "Rotary.h" |
|---|
| 23 | #include "LCD.h" |
|---|
| 24 | #include "../Skin.h" |
|---|
| 25 | |
|---|
| 26 | #include <hydrogen/globals.h> |
|---|
| 27 | |
|---|
| 28 | RotaryTooltip::RotaryTooltip( QPoint pos ) |
|---|
| 29 | // : QWidget( 0, "RotaryTooltip", Qt::WStyle_Customize| Qt::WStyle_NoBorder | Qt::WStyle_StaysOnTop| Qt::WX11BypassWM ) |
|---|
| 30 | : QWidget( 0, Qt::ToolTip ) |
|---|
| 31 | { |
|---|
| 32 | UNUSED( pos ); |
|---|
| 33 | |
|---|
| 34 | m_pDisplay = new LCDDisplay( this, LCDDigit::SMALL_BLUE, 4); |
|---|
| 35 | m_pDisplay->move( 0, 0 ); |
|---|
| 36 | resize( m_pDisplay->size() ); |
|---|
| 37 | |
|---|
| 38 | QPalette defaultPalette; |
|---|
| 39 | defaultPalette.setColor( QPalette::Background, QColor( 49, 53, 61 ) ); |
|---|
| 40 | this->setPalette( defaultPalette ); |
|---|
| 41 | |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | void RotaryTooltip::showTip( QPoint pos, QString sText ) |
|---|
| 46 | { |
|---|
| 47 | move( pos ); |
|---|
| 48 | m_pDisplay->setText( sText ); |
|---|
| 49 | show(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | RotaryTooltip::~RotaryTooltip() |
|---|
| 53 | { |
|---|
| 54 | // delete m_pDisplay; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /////////////////// |
|---|
| 62 | |
|---|
| 63 | QPixmap* Rotary::m_background_normal = NULL; |
|---|
| 64 | QPixmap* Rotary::m_background_center = NULL; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | Rotary::Rotary( QWidget* parent, RotaryType type, QString sToolTip, bool bUseIntSteps, bool bUseValueTip ) |
|---|
| 68 | : QWidget( parent ) |
|---|
| 69 | , Object( "Rotary" ) |
|---|
| 70 | , m_bUseIntSteps( bUseIntSteps ) |
|---|
| 71 | , m_type( type ) |
|---|
| 72 | , m_fMin( 0.0 ) |
|---|
| 73 | , m_fMax( 1.0 ) |
|---|
| 74 | , m_bShowValueToolTip( bUseValueTip ) |
|---|
| 75 | { |
|---|
| 76 | setAttribute(Qt::WA_NoBackground); |
|---|
| 77 | setToolTip( sToolTip ); |
|---|
| 78 | |
|---|
| 79 | m_pValueToolTip = new RotaryTooltip( mapToGlobal( QPoint( 0, 0 ) ) ); |
|---|
| 80 | |
|---|
| 81 | m_nWidgetWidth = 28; |
|---|
| 82 | m_nWidgetHeight = 26; |
|---|
| 83 | m_fValue = 0.0; |
|---|
| 84 | |
|---|
| 85 | if ( m_background_normal == NULL ) { |
|---|
| 86 | m_background_normal = new QPixmap(); |
|---|
| 87 | if ( m_background_normal->load( Skin::getImagePath() + "/mixerPanel/rotary_images.png" ) == false ){ |
|---|
| 88 | ERRORLOG( "Error loading pixmap" ); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | if ( m_background_center == NULL ) { |
|---|
| 92 | m_background_center = new QPixmap(); |
|---|
| 93 | if ( m_background_center->load( Skin::getImagePath() + "/mixerPanel/rotary_center_images.png" ) == false ){ |
|---|
| 94 | ERRORLOG( "Error loading pixmap" ); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | resize( m_nWidgetWidth, m_nWidgetHeight ); |
|---|
| 99 | // m_temp.resize( m_nWidgetWidth, m_nWidgetHeight ); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | Rotary::~ Rotary() |
|---|
| 105 | { |
|---|
| 106 | delete m_pValueToolTip; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | void Rotary::paintEvent( QPaintEvent* ev ) |
|---|
| 112 | { |
|---|
| 113 | UNUSED( ev ); |
|---|
| 114 | QPainter painter(this); |
|---|
| 115 | |
|---|
| 116 | float fRange = fabs( m_fMax ) + fabs( m_fMin ); |
|---|
| 117 | float fValue = fabs( m_fMin ) + m_fValue; |
|---|
| 118 | |
|---|
| 119 | int nFrame; |
|---|
| 120 | if ( m_bUseIntSteps ) { |
|---|
| 121 | nFrame = (int)( 63.0 * ( (int)fValue / fRange ) ); |
|---|
| 122 | } |
|---|
| 123 | else { |
|---|
| 124 | nFrame = (int)( 63.0 * ( fValue / fRange ) ); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // INFOLOG( "\nrange: " + toString( fRange ) ); |
|---|
| 128 | // INFOLOG( "norm value: " + toString( fValue ) ); |
|---|
| 129 | // INFOLOG( "frame: " + toString( nFrame ) ); |
|---|
| 130 | |
|---|
| 131 | if ( m_type == TYPE_NORMAL ) { |
|---|
| 132 | |
|---|
| 133 | int xPos = m_nWidgetWidth * nFrame; |
|---|
| 134 | painter.drawPixmap( rect(), *m_background_normal, QRect( xPos, 0, m_nWidgetWidth, m_nWidgetHeight ) ); |
|---|
| 135 | } |
|---|
| 136 | else { |
|---|
| 137 | // the image is broken... |
|---|
| 138 | if ( nFrame > 62 ) { |
|---|
| 139 | nFrame = 62; |
|---|
| 140 | } |
|---|
| 141 | int xPos = m_nWidgetWidth * nFrame; |
|---|
| 142 | painter.drawPixmap( rect(), *m_background_center, QRect( xPos, 0, m_nWidgetWidth, m_nWidgetHeight ) ); |
|---|
| 143 | |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | void Rotary::setValue( float fValue ) |
|---|
| 150 | { |
|---|
| 151 | if ( fValue == m_fValue ) { |
|---|
| 152 | return; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | if ( fValue < m_fMin ) { |
|---|
| 156 | fValue = m_fMin; |
|---|
| 157 | } |
|---|
| 158 | else if ( fValue > m_fMax ) { |
|---|
| 159 | fValue = m_fMax; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | if ( fValue != m_fValue ) { |
|---|
| 163 | m_fValue = fValue; |
|---|
| 164 | update(); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | void Rotary::mousePressEvent(QMouseEvent *ev) |
|---|
| 171 | { |
|---|
| 172 | setCursor( QCursor( Qt::SizeVerCursor ) ); |
|---|
| 173 | |
|---|
| 174 | m_fMousePressValue = m_fValue; |
|---|
| 175 | m_fMousePressY = ev->y(); |
|---|
| 176 | |
|---|
| 177 | if ( m_bShowValueToolTip ) { |
|---|
| 178 | char tmp[20]; |
|---|
| 179 | sprintf( tmp, "%#.2f", m_fValue ); |
|---|
| 180 | m_pValueToolTip->showTip( mapToGlobal( QPoint( -38, 1 ) ), QString( tmp ) ); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | if ( ev->button() == Qt::LeftButton && ev->modifiers() == Qt::AltModifier ){ |
|---|
| 184 | MidiSenseWidget midiSense( this, true, this->getAction() ); |
|---|
| 185 | midiSense.exec(); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | void Rotary::mouseReleaseEvent( QMouseEvent *ev ) |
|---|
| 193 | { |
|---|
| 194 | UNUSED( ev ); |
|---|
| 195 | |
|---|
| 196 | setCursor( QCursor( Qt::ArrowCursor ) ); |
|---|
| 197 | m_pValueToolTip->hide(); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | void Rotary::wheelEvent ( QWheelEvent *ev ) |
|---|
| 204 | { |
|---|
| 205 | ev->accept(); |
|---|
| 206 | |
|---|
| 207 | float stepfactor = 5.0; // course adjustment |
|---|
| 208 | float delta = 1.0; |
|---|
| 209 | |
|---|
| 210 | // Control Modifier = fine adjustment |
|---|
| 211 | if (ev->modifiers() == Qt::ControlModifier) { |
|---|
| 212 | stepfactor = 1.0; |
|---|
| 213 | } |
|---|
| 214 | if ( !m_bUseIntSteps ) { |
|---|
| 215 | float fRange = fabs( m_fMax ) + fabs( m_fMin ); |
|---|
| 216 | delta = fRange / 100.0; |
|---|
| 217 | } |
|---|
| 218 | if ( ev->delta() < 0 ) { |
|---|
| 219 | delta = delta * -1.0; |
|---|
| 220 | } |
|---|
| 221 | setValue( getValue() + (delta * stepfactor) ); |
|---|
| 222 | emit valueChanged(this); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | void Rotary::mouseMoveEvent( QMouseEvent *ev ) { |
|---|
| 228 | float fRange = fabs( m_fMax ) + fabs( m_fMin ); |
|---|
| 229 | |
|---|
| 230 | float deltaY = ev->y() - m_fMousePressY; |
|---|
| 231 | float fNewValue = ( m_fMousePressValue - ( deltaY / 100.0 * fRange ) ); |
|---|
| 232 | |
|---|
| 233 | setValue( fNewValue ); |
|---|
| 234 | emit valueChanged(this); |
|---|
| 235 | |
|---|
| 236 | if ( m_bShowValueToolTip ) { |
|---|
| 237 | char tmp[20]; |
|---|
| 238 | sprintf( tmp, "%#.2f", m_fValue ); |
|---|
| 239 | m_pValueToolTip->showTip( mapToGlobal( QPoint( -38, 1 ) ), QString( tmp ) ); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | void Rotary::setMin( float fMin ) |
|---|
| 246 | { |
|---|
| 247 | m_fMin = fMin; |
|---|
| 248 | update(); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | float Rotary::getMin() |
|---|
| 252 | { |
|---|
| 253 | return m_fMin; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | void Rotary::setMax( float fMax ) |
|---|
| 259 | { |
|---|
| 260 | m_fMax = fMax; |
|---|
| 261 | update(); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | float Rotary::getMax() |
|---|
| 266 | { |
|---|
| 267 | return m_fMax; |
|---|
| 268 | } |
|---|