[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [kmix] gui: Adding a tooltip to VolumeSlider. Done by Ronnie Thomas
From: Christian Esken <esken () kde ! org>
Date: 2015-08-25 20:42:24
Message-ID: E1ZUL32-0005MD-Ec () scm ! kde ! org
[Download RAW message or body]
Git commit 13831929d8ecc8dbdcbbb5bf77f7c3ad786f37a8 by Christian Esken.
Committed on 25/08/2015 at 20:40.
Pushed by esken into branch 'master'.
Adding a tooltip to VolumeSlider. Done by Ronnie Thomas
REVIEW: 123121
M +93 -0 gui/volumeslider.cpp
M +10 -0 gui/volumeslider.h
http://commits.kde.org/kmix/13831929d8ecc8dbdcbbb5bf77f7c3ad786f37a8
diff --git a/gui/volumeslider.cpp b/gui/volumeslider.cpp
index 1a4166e..6d7da0c 100644
--- a/gui/volumeslider.cpp
+++ b/gui/volumeslider.cpp
@@ -19,11 +19,104 @@
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA \
02110-1301, USA.
*/
+#include <QStyleOption>
+#include <QFontMetrics>
#include "volumeslider.h"
+#include <kdebug.h>
VolumeSlider::VolumeSlider(Qt::Orientation orientation, QWidget* parent) : \
QSlider(orientation, parent) +,m_orientation(orientation),m_tooltip(new \
QLabel(parent,Qt::ToolTip)) {
+ QFontMetrics metrics(m_tooltip->font());
+ //Setting a size big enough for all values less than 100
+ QRect labelRect = metrics.boundingRect("88");
+// m_tooltip->setContentsMargins(3,1,3,1);
+ m_tooltip->setMinimumWidth(labelRect.width()+5);
+ m_tooltip->setMinimumHeight(labelRect.height()+3);
+ m_tooltip->setAlignment(Qt::AlignCenter);
+// m_tooltip->setAlignment(Qt::AlignVCenter);
+}
+
+void VolumeSlider::mousePressEvent(QMouseEvent* event)
+{
+ QSlider::mousePressEvent(event);
+
+ QStyleOptionSlider opt;
+ initStyleOption(&opt);
+ QRect sliderHandle = \
style()->subControlRect(QStyle::CC_Slider,&opt,QStyle::SC_SliderHandle,this); +
+ if(sliderHandle.contains(event->pos()))
+ {
+ if (m_orientation == Qt::Vertical)
+ {
+ m_tooltip->move(mapToGlobal(sliderHandle.topLeft()).x()+width(),mapToGlobal(sliderHandle.topLeft()).y());
+ }
+ else
+ {
+ m_tooltip->move(mapToGlobal(sliderHandle.topLeft()).x(),mapToGlobal(sliderHandle.topLeft()).y()+height());
+ }
+
+ /**
+ * 1) The following operates on the value of one individual channel slider, and \
thus is representing only the + * value of that channel. This is technically not \
100% sound for a corner case: The average of all channels + * should be displayed \
in the "joined slider mode". On the other hand, in "joined slider mode", the slider \
+ * should already represent the average of all channels. And as soon as you move \
the slider, all channels + * are changed. So it is likely really exact enough.
+ *
+ * 2) Future directions: It would be better to do the percentage calculation via \
the Volume class, as it + * handles corner cases like muting. But due to \
"auto-unmuting" the value is factually also correct. As the + * VolumeSlider class \
currently holds no pointer/reference to the "underlying" Volume object, a bit code \
+ * "duplication" is acceptable here. + */
+ qreal percentReal = ((qreal)100 * value() ) / ( maximum() - minimum());
+ int percent = qRound(percentReal);
+ m_tooltip->setText(QString::number(percent));
+ m_tooltip->show();
+ }
+}
+
+void VolumeSlider::mouseReleaseEvent(QMouseEvent* event)
+{
+ QSlider::mouseReleaseEvent(event);
+ m_tooltip->hide();
+}
+
+void VolumeSlider::mouseMoveEvent(QMouseEvent* event)
+{
+ QSlider::mouseMoveEvent(event);
+
+ QStyleOptionSlider opt;
+ initStyleOption(&opt);
+ QRect sliderHandle = \
style()->subControlRect(QStyle::CC_Slider,&opt,QStyle::SC_SliderHandle,this); +
+ qreal percentReal = ((qreal)100 * value() ) / ( maximum() - minimum() );
+ int percent = qRound(percentReal);
+
+ //Change width of label if percent becomes 100
+ if (percent == 100)
+ {
+ QFontMetrics metrics(m_tooltip->font());
+ QRect labelRect = metrics.boundingRect("100");
+ m_tooltip->resize(labelRect.width(),m_tooltip->height());
+ }
+ else if(m_tooltip->width() > m_tooltip->minimumWidth())
+ {
+ m_tooltip->resize(m_tooltip->minimumWidth(),m_tooltip->height());
+ }
+ m_tooltip->setText(QString::number(percent));
+
+ if (m_orientation == Qt::Vertical)
+ {
+ m_tooltip->move(mapToGlobal(sliderHandle.topLeft()).x()+width(),mapToGlobal(sliderHandle.topLeft()).y());
+ }
+ else
+ {
+ m_tooltip->move(mapToGlobal(sliderHandle.topLeft()).x(),mapToGlobal(sliderHandle.topLeft()).y()+height());
+ }
+
+ //kDebug() << "Position \
is"<<mapToGlobal(sliderHandle.topLeft()).x()<<","<<mapToGlobal(sliderHandle.topLeft()).y();
+ //kDebug() << "Volume is "<<percent;
}
#include "volumeslider.moc"
diff --git a/gui/volumeslider.h b/gui/volumeslider.h
index 616c9a0..2dd4098 100644
--- a/gui/volumeslider.h
+++ b/gui/volumeslider.h
@@ -24,12 +24,22 @@
#define VOLUMESLIDER_H
#include <QSlider>
+#include <QLabel>
+#include <QMouseEvent>
#include "volumesliderextradata.h"
class VolumeSlider : public QSlider
{
Q_OBJECT
+ Qt::Orientation m_orientation;
+ QLabel* m_tooltip;
+
+protected:
+ void mousePressEvent(QMouseEvent *event);
+ void mouseMoveEvent(QMouseEvent *event);
+ void mouseReleaseEvent(QMouseEvent *event);
+
public:
VolumeSlider(Qt::Orientation orientation, QWidget* parent); // : \
QSlider(orientation, parent);
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic