![]() |
Mixxx
|
00001 /*************************************************************************** 00002 wslidercomposed.cpp - description 00003 ------------------- 00004 begin : Tue Jun 25 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 "wslidercomposed.h" 00019 #include <qpixmap.h> 00020 #include <QtDebug> 00021 #include <qpainter.h> 00022 //Added by qt3to4: 00023 #include <QMouseEvent> 00024 #include <QPaintEvent> 00025 #include <QPainter> 00026 #include "defs.h" 00027 #include "wpixmapstore.h" 00028 00029 WSliderComposed::WSliderComposed(QWidget * parent, float defaultValue) 00030 : WAbstractControl(parent,defaultValue) 00031 { 00032 m_pSlider = 0; 00033 m_pHandle = 0; 00034 m_bHorizontal = false; 00035 m_bEventWhileDrag = true; 00036 m_bDrag = false; 00037 00038 // Set default values 00039 m_iSliderLength=0; 00040 m_iHandleLength=0; 00041 00042 m_fValue = defaultValue; // FWI: Fixed slider default positon 00043 } 00044 00045 WSliderComposed::~WSliderComposed() 00046 { 00047 unsetPixmaps(); 00048 } 00049 00050 void WSliderComposed::setup(QDomNode node) 00051 { 00052 // Setup pixmaps 00053 QString pathSlider = getPath(selectNodeQString(node, "Slider")); 00054 QString pathHandle = getPath(selectNodeQString(node, "Handle")); 00055 QString pathHorizontal = selectNodeQString(node, "Horizontal"); 00056 bool h = false; 00057 if (pathHorizontal.contains("true",Qt::CaseInsensitive)) 00058 h = true; 00059 setPixmaps(h, pathSlider, pathHandle); 00060 00061 if (!selectNode(node, "EventWhileDrag").isNull()) 00062 if (selectNodeQString(node, "EventWhileDrag").contains("no")) 00063 m_bEventWhileDrag = false; 00064 } 00065 00066 void WSliderComposed::setPixmaps(bool bHorizontal, const QString &filenameSlider, const QString &filenameHandle) 00067 { 00068 m_bHorizontal = bHorizontal; 00069 unsetPixmaps(); 00070 m_pSlider = WPixmapStore::getPixmap(filenameSlider); 00071 if (!m_pSlider) 00072 qDebug() << "WSliderComposed: Error loading slider pixmap:" << filenameSlider; 00073 m_pHandle = WPixmapStore::getPixmap(filenameHandle); 00074 if (!m_pHandle) 00075 qDebug() << "WSliderComposed: Error loading handle pixmap:" << filenameHandle; 00076 00077 if (m_bHorizontal) 00078 { 00079 m_iSliderLength = m_pSlider->width(); 00080 m_iHandleLength = m_pHandle->width(); 00081 } 00082 else 00083 { 00084 m_iSliderLength = m_pSlider->height(); 00085 m_iHandleLength = m_pHandle->height(); 00086 } 00087 00088 // Set size of widget, using size of slider pixmap 00089 if (m_pSlider) 00090 setFixedSize(m_pSlider->size()); 00091 00092 setValue(m_fValue); 00093 00094 repaint(); 00095 } 00096 00097 void WSliderComposed::unsetPixmaps() 00098 { 00099 if (m_pSlider) 00100 WPixmapStore::deletePixmap(m_pSlider); 00101 if (m_pHandle) 00102 WPixmapStore::deletePixmap(m_pHandle); 00103 m_pSlider = 0; 00104 m_pHandle = 0; 00105 } 00106 00107 void WSliderComposed::mouseMoveEvent(QMouseEvent * e) 00108 { 00109 if (!m_bRightButtonPressed) { 00110 if (m_bHorizontal) 00111 m_iPos = e->x()-m_iHandleLength/2; 00112 else 00113 m_iPos = e->y()-m_iHandleLength/2; 00114 00115 //qDebug() << "start " << m_iStartPos << ", pos " << m_iPos; 00116 m_iPos = m_iStartHandlePos + (m_iPos-m_iStartMousePos); 00117 00118 if (m_iPos>(m_iSliderLength-m_iHandleLength)) 00119 m_iPos = m_iSliderLength-m_iHandleLength; 00120 else if (m_iPos<0) 00121 m_iPos = 0; 00122 00123 // value ranges from 0 to 127 00124 m_fValue = (double)m_iPos*(127./(double)(m_iSliderLength-m_iHandleLength)); 00125 if (!m_bHorizontal) 00126 m_fValue = 127.-m_fValue; 00127 00128 // Emit valueChanged signal 00129 if (m_bEventWhileDrag) 00130 { 00131 if (e->button()==Qt::RightButton) 00132 emit(valueChangedRightUp(m_fValue)); 00133 else 00134 emit(valueChangedLeftUp(m_fValue)); 00135 } 00136 00137 // Update display 00138 update(); 00139 } 00140 } 00141 00142 void WSliderComposed::wheelEvent(QWheelEvent *e) 00143 { 00144 double wheelDirection = ((QWheelEvent *)e)->delta() / 120.; 00145 double newValue = getValue() + (wheelDirection); 00146 this->updateValue(newValue); 00147 00148 e->accept(); 00149 00150 //e->ignore(); 00151 } 00152 00153 void WSliderComposed::mouseReleaseEvent(QMouseEvent * e) 00154 { 00155 if (!m_bEventWhileDrag) 00156 { 00157 mouseMoveEvent(e); 00158 00159 if (e->button()==Qt::RightButton) 00160 emit(valueChangedRightUp(m_fValue)); 00161 else 00162 emit(valueChangedLeftUp(m_fValue)); 00163 00164 m_bDrag = false; 00165 } 00166 if (e->button()==Qt::RightButton) 00167 m_bRightButtonPressed = false; 00168 } 00169 00170 void WSliderComposed::mousePressEvent(QMouseEvent * e) 00171 { 00172 if (!m_bEventWhileDrag) 00173 { 00174 m_iStartMousePos = 0; 00175 m_iStartHandlePos = 0; 00176 mouseMoveEvent(e); 00177 m_bDrag = true; 00178 } 00179 else 00180 { 00181 if (e->button() == Qt::RightButton) 00182 { 00183 reset(); 00184 m_bRightButtonPressed = true; 00185 } 00186 else 00187 { 00188 if (m_bHorizontal) 00189 m_iStartMousePos = e->x()-m_iHandleLength/2; 00190 else 00191 m_iStartMousePos = e->y()-m_iHandleLength/2; 00192 00193 m_iStartHandlePos = m_iPos; 00194 } 00195 } 00196 } 00197 00198 void WSliderComposed::paintEvent(QPaintEvent *) 00199 { 00200 if (m_pSlider && m_pHandle) 00201 { 00202 QPainter p(this); 00203 int posx; 00204 int posy; 00205 if (m_bHorizontal) 00206 { 00207 posx = m_iPos; 00208 posy = 0; 00209 } 00210 else 00211 { 00212 posx = 0; 00213 posy = m_iPos; 00214 } 00215 00216 // Draw slider followed by handle 00217 p.drawPixmap(0, 0, *m_pSlider); 00218 p.drawPixmap(posx, posy, *m_pHandle); 00219 } 00220 } 00221 00222 void WSliderComposed::setValue(double fValue) 00223 { 00224 if (!m_bDrag) 00225 { 00226 // Set value without emitting a valueChanged signal, and force display update 00227 m_fValue = fValue; 00228 00229 // Calculate handle position 00230 if (!m_bHorizontal) 00231 fValue = 127-fValue; 00232 m_iPos = (int)((fValue/127.)*(double)(m_iSliderLength-m_iHandleLength)); 00233 00234 if (m_iPos>(m_iSliderLength-m_iHandleLength)) 00235 m_iPos = m_iSliderLength-m_iHandleLength; 00236 else if (m_iPos<0) 00237 m_iPos = 0; 00238 00239 repaint(); 00240 } 00241 }