![]() |
Mixxx
|
00001 /*************************************************************************** 00002 wknob.cpp - description 00003 ------------------- 00004 begin : Fri Jun 21 2002 00005 copyright : (C) 2002 by Tue & Ken Haste Andersen 00006 email : haste@diku.dk 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include <QPixmap> 00019 #include <QtDebug> 00020 #include <QMouseEvent> 00021 #include <QPainter> 00022 #include <QPaintEvent> 00023 00024 #include "widget/wknob.h" 00025 00026 #include "defs.h" 00027 #include "widget/wpixmapstore.h" 00028 00029 WKnob::WKnob(QWidget * parent, float defaultValue) 00030 : WAbstractControl(parent, defaultValue), 00031 m_iPos(0), 00032 m_iNoPos(0), 00033 m_pPixmaps(NULL), 00034 m_pPixmapBack(NULL), 00035 m_bDisabledLoaded(false) { 00036 } 00037 00038 WKnob::~WKnob() 00039 { 00040 resetPositions(); 00041 if (m_pPixmapBack) { 00042 WPixmapStore::deletePixmap(m_pPixmapBack); 00043 } 00044 } 00045 00046 void WKnob::setup(QDomNode node) 00047 { 00048 // Set background pixmap if available 00049 if (!selectNode(node, "BackPath").isNull()) 00050 setPixmapBackground(getPath(selectNodeQString(node, "BackPath"))); 00051 00052 // Number of states. Depends if disabled pics are defined as well 00053 setPositions(selectNodeInt(node, "NumberStates"), 00054 !selectNode(node, "DisabledPath").isNull()); 00055 00056 // Load knob pixmaps 00057 QString path = selectNodeQString(node, "Path"); 00058 for (int i=0; i<m_iNoPos; ++i) 00059 setPixmap(i, getPath(path.arg(i))); 00060 00061 // See if disabled images is defined, and load them... 00062 if (!selectNode(node, "DisabledPath").isNull()) 00063 { 00064 path = selectNodeQString(node, "DisabledPath"); 00065 for (int i=0; i<m_iNoPos; ++i) 00066 setPixmap(i+m_iNoPos, getPath(path.arg(i))); 00067 m_bDisabledLoaded = true; 00068 } 00069 } 00070 00071 void WKnob::setPositions(int iNoPos, bool bIncludingDisabled) 00072 { 00073 resetPositions(); 00074 00075 m_iNoPos = iNoPos; 00076 m_iPos = 0; 00077 00078 if (m_iNoPos>0) 00079 { 00080 int pics = m_iNoPos; 00081 if (bIncludingDisabled) 00082 pics *= 2; 00083 00084 m_pPixmaps = new QPixmap*[pics]; 00085 for (int i=0; i<pics; i++) 00086 m_pPixmaps[i] = 0; 00087 } 00088 } 00089 00090 void WKnob::resetPositions() 00091 { 00092 if (m_pPixmaps) 00093 { 00094 int pics = m_iNoPos; 00095 if( m_bDisabledLoaded ){ 00096 pics *= 2; 00097 } 00098 for (int i=0; i<pics; i++) { 00099 if (m_pPixmaps[i]) { 00100 WPixmapStore::deletePixmap(m_pPixmaps[i]); 00101 } 00102 } 00103 delete [] m_pPixmaps; 00104 m_pPixmaps = NULL; 00105 } 00106 } 00107 00108 void WKnob::setPixmap(int iPos, const QString &filename) 00109 { 00110 m_pPixmaps[iPos] = WPixmapStore::getPixmap(filename); 00111 if (!m_pPixmaps[iPos]) 00112 qDebug() << "WKnob: Error loading pixmap" << filename; 00113 setFixedSize(m_pPixmaps[iPos]->size()); 00114 } 00115 00116 void WKnob::setPixmapBackground(const QString &filename) 00117 { 00118 // Load background pixmap 00119 if (m_pPixmapBack) { 00120 WPixmapStore::deletePixmap(m_pPixmapBack); 00121 } 00122 m_pPixmapBack = WPixmapStore::getPixmap(filename); 00123 if (!m_pPixmapBack) 00124 qDebug() << "WKnob: Error loading background pixmap:" << filename; 00125 } 00126 00127 void WKnob::mouseMoveEvent(QMouseEvent * e) 00128 { 00129 if (!m_bRightButtonPressed) { 00130 QPoint cur(e->globalPos()); 00131 QPoint diff(cur - m_startPos); 00132 double dist = sqrt(static_cast<double>(diff.x() * diff.x() + diff.y() * diff.y())); 00133 bool y_dominant = abs(diff.y()) > abs(diff.x()); 00134 00135 // if y is dominant, then thread an increase in dy as negative (y is 00136 // pointed downward). Otherwise, if y is not dominant and x has 00137 // decreased, then thread it as negative. 00138 if ((y_dominant && diff.y() > 0) || (!y_dominant && diff.x() < 0)) { 00139 dist = -dist; 00140 } 00141 00142 m_fValue += dist; 00143 QCursor::setPos(m_startPos); 00144 00145 if (m_fValue>127.) 00146 m_fValue = 127.; 00147 else if (m_fValue<0.) 00148 m_fValue = 0.; 00149 emit(valueChangedLeftDown(m_fValue)); 00150 update(); 00151 } 00152 } 00153 00154 void WKnob::mousePressEvent(QMouseEvent * e) 00155 { 00156 switch (e->button()) { 00157 case Qt::RightButton: 00158 reset(); 00159 m_bRightButtonPressed = true; 00160 break; 00161 case Qt::LeftButton: 00162 case Qt::MidButton: 00163 m_startPos = e->globalPos(); 00164 QApplication::setOverrideCursor(Qt::BlankCursor); 00165 break; 00166 default: 00167 break; 00168 } 00169 } 00170 00171 void WKnob::mouseReleaseEvent(QMouseEvent * e) 00172 { 00173 switch (e->button()) { 00174 case Qt::LeftButton: 00175 case Qt::MidButton: 00176 QCursor::setPos(m_startPos); 00177 QApplication::restoreOverrideCursor(); 00178 emit(valueChangedLeftUp(m_fValue)); 00179 break; 00180 case Qt::RightButton: 00181 m_bRightButtonPressed = false; 00182 //emit(valueChangedRightUp(m_fValue)); 00183 break; 00184 default: 00185 break; 00186 } 00187 00188 update(); 00189 } 00190 00191 void WKnob::wheelEvent(QWheelEvent *e) 00192 { 00193 double wheelDirection = ((QWheelEvent *)e)->delta() / 120.; 00194 double newValue = getValue() + wheelDirection; 00195 00196 // Clamp to [0.0, 127.0] 00197 newValue = math_max(0.0, math_min(127.0, newValue)); 00198 00199 updateValue(newValue); 00200 00201 e->accept(); 00202 00203 //e->ignore(); 00204 } 00205 00206 void WKnob::paintEvent(QPaintEvent *) 00207 { 00208 if (m_pPixmaps) 00209 { 00210 int idx = (int)(((m_fValue-64.)*(((float)m_iNoPos-1.)/127.))+((float)m_iNoPos/2.)); 00211 // Range check 00212 if (idx>(m_iNoPos-1)) 00213 idx = m_iNoPos-1; 00214 else if (idx<0) 00215 idx = 0; 00216 00217 // Disabled pixmaps are placed ahead of normal pixmaps in the buffer. Use them 00218 // if the widget is disabled and the disabled pixmaps are loaded. 00219 if (m_bOff && m_bDisabledLoaded) 00220 idx += m_iNoPos; 00221 00222 QPainter p(this); 00223 // Paint background 00224 //p.drawPixmap(0, 0, m_pPixmapBack); 00225 // Paint button 00226 p.drawPixmap(0, 0, *m_pPixmaps[idx]); 00227 } 00228 }