File indexing completed on 2024-12-22 04:40:21

0001 /*
0002     smplayer, GUI front-end for mplayer.
0003     SPDX-FileCopyrightText: 2007 Ricardo Villalba <rvm@escomposlinux.org>
0004 
0005     modified for inclusion in Subtitle Composer
0006     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "pointingslider.h"
0012 
0013 #include <QApplication>
0014 #include <QStyle>
0015 #include <QMouseEvent>
0016 
0017 PointingSlider::PointingSlider(QWidget *parent) :
0018     QSlider(parent)
0019 {}
0020 
0021 PointingSlider::PointingSlider(Qt::Orientation orientation, QWidget *parent) :
0022     QSlider(orientation, parent)
0023 {}
0024 
0025 PointingSlider::~PointingSlider()
0026 {}
0027 
0028 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0029 #define position pos
0030 #endif
0031 
0032 // The code from the following function is from Javier Díaz,
0033 // taken from a post in the Qt-interest mailing list.
0034 void
0035 PointingSlider::mousePressEvent(QMouseEvent *e)
0036 {
0037     int range = maximum() - minimum();
0038 
0039     int clickedValue;                       // this will contain the value corresponding to the clicked (x,y) screen position
0040     double pixelsPerUnit;           // the will contain the amount of pixels corresponding to each slider range unit
0041 
0042     if(orientation() == Qt::Horizontal) {
0043         int width = this->width();
0044         pixelsPerUnit = (double)width / range;
0045         clickedValue = (e->position().x() * range) / width;
0046 
0047         if((qApp->isRightToLeft() && !invertedAppearance()) || (!qApp->isRightToLeft() && invertedAppearance()))
0048             clickedValue = maximum() - clickedValue;
0049     } else {
0050         int height = this->height();
0051         pixelsPerUnit = (double)height / range;
0052         clickedValue = (e->position().y() * range) / height;
0053 
0054         if(!invertedAppearance())
0055             clickedValue = maximum() - clickedValue;
0056     }
0057 
0058     // calculate how many range units take the slider handle
0059     int sliderHandleUnits = (int)(qApp->style()->pixelMetric(QStyle::PM_SliderLength) / pixelsPerUnit);
0060 
0061     // if the condition is is not true, the user has clicked inside the slider
0062     // (or a screen position with a corresponding range value inside the slider tolerance)
0063     if(qAbs(clickedValue - value()) > sliderHandleUnits) {
0064         setValue(clickedValue);
0065         emit sliderMoved(clickedValue);
0066     } else {
0067         QSlider::mousePressEvent(e);
0068     }
0069 }
0070 
0071