--- a/GUI/player/SearchSlider.cpp
+++ b/GUI/player/DirectSlider.cpp
@@ -1,6 +1,5 @@
-/* SearchSlider.cpp
-
- * Copyright (C) 2012
+/*
+ * Copyright (C) 2012
*
* This file is part of sayonara-player
*
@@ -17,133 +16,63 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
- * created by Lucio Carreras,
- * Sep 14, 2012
+ * created by Lucio Carreras,
+ * Sep 14, 2012
*
*/
#include <QDebug>
+#include <QStyle>
#include <QEvent>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QAbstractSlider>
-#include "GUI/player/SearchSlider.h"
+#include <QStyleOptionSlider>
+
+#include "GUI/player/DirectSlider.h"
#include <cmath>
+// A slider where a click sets the position to the click location
+// instead of moving by a fixed amount as does the standard widget.
+// http://stackoverflow.com/questions/11132597/qslider-mouse-direct-jump
-SearchSlider::SearchSlider(QWidget* parent) : QSlider(parent) {
- _searching = false;
+static int adjustedVal(double halfHandleWidth, int pos, int sz,
+ int min, int max)
+{
+ if ( pos < halfHandleWidth )
+ pos = halfHandleWidth;
+ if ( pos > sz - halfHandleWidth )
+ pos = sz - halfHandleWidth;
+ // get new dimensions accounting for slider handle width
+ double newWidth = (sz - halfHandleWidth) - halfHandleWidth;
+ double normalizedPosition = (pos - halfHandleWidth) / newWidth ;
+
+ return min + ((max-min) * normalizedPosition);
}
-SearchSlider::~SearchSlider() {
- // TODO Auto-generated destructor stub
+void DirectSlider::mousePressEvent(QMouseEvent *event)
+{
+ QStyleOptionSlider opt;
+ initStyleOption(&opt);
+ QRect sr = style()->subControlRect(QStyle::CC_Slider, &opt,
+ QStyle::SC_SliderHandle, this);
+
+ if (event->button() == Qt::LeftButton &&
+ sr.contains(event->pos()) == false) {
+ double halfHandleWidth = (0.5 * sr.width()) + 0.5; // Correct rounding
+
+ int newVal;
+ if (orientation() == Qt::Vertical) {
+ newVal = adjustedVal(halfHandleWidth,
+ height() - event->y(), height(),
+ minimum(), maximum());
+ } else {
+ newVal = adjustedVal(halfHandleWidth, event->x(), width(),
+ minimum(), maximum());
+ }
+ setValue(newVal);
+ event->accept();
+ } else {
+ QSlider::mousePressEvent(event);
+ }
}
-
-bool SearchSlider::isSearching(){
- return _searching;
-}
-
-
-bool SearchSlider::event(QEvent* e){
-
-
-
- int percent;
- int cur_val = this->value() * 1.0;
- QMouseEvent* mouseEvent;
- QWheelEvent* wheelEvent;
-
-
- switch(e->type()){
- case QEvent::MouseTrackingChange:
- break;
-
- case QEvent::MouseButtonDblClick:
- break;
-
- case QEvent::MouseButtonPress:
- if(!isEnabled()) break;
- _searching = true;
-
- e->ignore();
- mouseEvent = (QMouseEvent*) e;
-
- if(this->orientation() == Qt::Horizontal)
- percent = (mouseEvent->x() * 100) / this->width();
- else
- percent = 100 - (mouseEvent->y() * 100 / this->height());
-
- if (percent < 0) percent = 0;
- if (percent > 100) percent = 100;
- this->setValue(percent);
-
- emit searchSliderPressed(percent);
-
- break;
-
- case QEvent::MouseMove:
- if(!isEnabled()) break;
- e->ignore();
- mouseEvent = (QMouseEvent*) e;
-
- if(this->orientation() == Qt::Horizontal)
- percent = (mouseEvent->x() * 100) / this->width();
- else
- percent = 100 - (mouseEvent->y() * 100 / this->height());
-
- if (percent < 0) percent = 0;
- if (percent > 100) percent = 100;
- this->setValue(percent);
-
- if(_searching)
- emit searchSliderMoved(percent);
-
- break;
-
- case QEvent::MouseButtonRelease:
-
- if(!isEnabled()) break;
- e->ignore();
- mouseEvent = (QMouseEvent*) e;
-
- if(this->orientation() == Qt::Horizontal)
- percent = (mouseEvent->x() * 100) / this->width();
- else
- percent = 100 - (mouseEvent->y() * 100 / this->height());
-
- if (percent < 0) percent = 0;
- if (percent > 100) percent = 100;
-
- this->setValue(percent);
-
- emit searchSliderReleased(percent);
- _searching = false;
- break;
-
- case QEvent::Wheel:
-
- if(!isEnabled()) break;
- if(this->orientation() == Qt::Horizontal){
- e->ignore();
- break;
- }
- e->ignore();
-
- wheelEvent = (QWheelEvent*) e;
-
- percent = cur_val + (wheelEvent->delta() / abs(wheelEvent->delta()) * 3);
-
- emit searchSliderMoved(percent);
- _searching = false;
- break;
-
- default:
-
- QSlider::event(e);
- break;
- }
-
-
- return true;
-}
-