Get rid of the baroque custom slider code, make the mute button actually mute/unmute, get the volume and seek adjustments to behave

Jean-Francois Dockes Jean-Francois Dockes 2014-10-15

changed GUI/player/GUI_Player.cpp
changed GUI/player/GUI_Player.h
changed GUI/player/GUI_Player.ui
changed GUI/player/GUI_PlayerConnections.cpp
changed GUI/player/GUI_PlayerControls.cpp
changed application.cpp
changed upplay.pro
copied GUI/player/SearchSlider.cpp -> GUI/player/DirectSlider.cpp
copied GUI/player/SearchSlider.h -> GUI/player/DirectSlider.h
GUI/player/GUI_Player.cpp Diff Switch to side-by-side view
Loading...
GUI/player/GUI_Player.h Diff Switch to side-by-side view
Loading...
GUI/player/GUI_Player.ui Diff Switch to side-by-side view
Loading...
GUI/player/GUI_PlayerConnections.cpp Diff Switch to side-by-side view
Loading...
GUI/player/GUI_PlayerControls.cpp Diff Switch to side-by-side view
Loading...
application.cpp Diff Switch to side-by-side view
Loading...
upplay.pro Diff Switch to side-by-side view
Loading...
GUI/player/SearchSlider.cpp to GUI/player/DirectSlider.cpp
--- 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;
-}
-
GUI/player/SearchSlider.h to GUI/player/DirectSlider.h
--- a/GUI/player/SearchSlider.h
+++ b/GUI/player/DirectSlider.h
@@ -1,5 +1,4 @@
-/* SearchSlider.h
-
+/*
  * Copyright (C) 2012  
  *
  * This file is part of sayonara-player
@@ -22,38 +21,30 @@
  *
  */
 
-#ifndef SEARCHSLIDER_H_
-#define SEARCHSLIDER_H_
+#ifndef DIRECTSLIDER_H_
+#define DIRECTSLIDER_H_
 
 #include <QWidget>
 #include <QSlider>
 
-class SearchSlider: public QSlider {
+class QMouseEvent;
 
-	Q_OBJECT
+// 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
+class DirectSlider: public QSlider {
 
-	signals:
-	void searchSliderPressed(int);
-	void searchSliderReleased(int);
-	void searchSliderMoved(int);
-
+    Q_OBJECT
 
 public:
-	SearchSlider(QWidget* parent=0);
-	virtual ~SearchSlider();
+    DirectSlider(QWidget* parent=0)
+        : QSlider(parent)
+        {}
 
-	bool isSearching();
-
-
+    virtual ~DirectSlider() {}
 
 protected:
-	virtual bool event(QEvent* e);
-
-private:
-	bool	_searching;
-
-
-
+    virtual void mousePressEvent ( QMouseEvent * event );
 };
 
-#endif /* SEARCHSLIDER_H_ */
+#endif /* DIRECTSLIDER_H_ */