File indexing completed on 2025-01-05 03:59:26

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2013 Mohammed Nafees <nafees.technocool@gmail.com>
0004 //
0005 
0006 #include "NavigationSlider.h"
0007 
0008 #include <QPainter>
0009 #include <QMouseEvent>
0010 #include <QPixmapCache>
0011 
0012 namespace Marble
0013 {
0014 
0015 namespace {
0016     const int handleImageHeight = 32;
0017 }
0018 
0019 NavigationSlider::NavigationSlider(QWidget *parent) :
0020     QAbstractSlider( parent ),
0021     m_handleImagePath( QLatin1String("marble/navigation/navigational_slider_handle") )
0022 {
0023     setMouseTracking( true );
0024 }
0025 
0026 NavigationSlider::~NavigationSlider()
0027 {
0028     QPixmapCache::remove( QLatin1String("marble/navigation/navigational_slider_groove") );
0029     QPixmapCache::remove( QLatin1String("marble/navigation/navigational_slider_handle") );
0030     QPixmapCache::remove( QLatin1String("marble/navigation/navigational_slider_handle_hover") );
0031     QPixmapCache::remove( QLatin1String("marble/navigation/navigational_slider_handle_press") );
0032 }
0033 
0034 QPixmap NavigationSlider::pixmap( const QString &id )
0035 {
0036     QPixmap result;
0037     if ( !QPixmapCache::find( id, &result ) ) {
0038         result = QPixmap(QLatin1String(":/") + id + QLatin1String(".png"));
0039         QPixmapCache::insert( id, result );
0040     }
0041     return result;
0042 }
0043 
0044 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0045 
0046 void NavigationSlider::enterEvent( QEnterEvent * )
0047 
0048 #else
0049 
0050 void NavigationSlider::enterEvent( QEvent * )
0051 
0052 #endif
0053 
0054 {
0055     setSliderDown( false );
0056     if (m_handleImagePath != QLatin1String("marble/navigation/navigational_slider_handle_hover")) {
0057         m_handleImagePath = QLatin1String("marble/navigation/navigational_slider_handle_hover");
0058         repaint();
0059     }
0060 }
0061 
0062 void NavigationSlider::mouseMoveEvent( QMouseEvent *mouseEvent )
0063 {
0064     if ( !isSliderDown() && mouseEvent->buttons() & Qt::LeftButton ) {
0065         setSliderDown( true );
0066     }
0067     if ( isSliderDown() ) {
0068         qreal const fraction = ( mouseEvent->pos().y() - handleImageHeight/2 ) / qreal ( height() - handleImageHeight );
0069         int v = ( int ) minimum() + ( ( maximum() - minimum() ) ) * ( 1 - fraction );
0070         setValue( v );
0071         repaint();
0072     }
0073 }
0074 
0075 void NavigationSlider::mousePressEvent( QMouseEvent * )
0076 {
0077     setSliderDown( true );
0078     if (m_handleImagePath != QLatin1String("marble/navigation/navigational_slider_handle_press")) {
0079         m_handleImagePath = QLatin1String("marble/navigation/navigational_slider_handle_press");
0080         repaint();
0081     }
0082 }
0083 
0084 void NavigationSlider::mouseReleaseEvent( QMouseEvent * )
0085 {
0086     setSliderDown( false );
0087     if (m_handleImagePath != QLatin1String("marble/navigation/navigational_slider_handle_hover")) {
0088         m_handleImagePath = QLatin1String("marble/navigation/navigational_slider_handle_hover");
0089         repaint();
0090     }
0091 }
0092 
0093 void NavigationSlider::leaveEvent( QEvent * )
0094 {
0095     setSliderDown( false );
0096     if (m_handleImagePath != QLatin1String("marble/navigation/navigational_slider_handle")) {
0097         m_handleImagePath = QLatin1String("marble/navigation/navigational_slider_handle");
0098         repaint();
0099     }
0100 }
0101 
0102 void NavigationSlider::repaint()
0103 {
0104     Q_EMIT repaintNeeded();
0105 }
0106 
0107 void NavigationSlider::paintEvent( QPaintEvent * )
0108 {
0109     QPainter painter( this );
0110     for ( int y = 0; y <= 160; y+=10 ) {
0111         painter.drawPixmap( 0, y, pixmap( QLatin1String("marble/navigation/navigational_slider_groove") ) );
0112 
0113     }
0114     qreal const fraction = ( value() - minimum() ) / qreal( maximum() - minimum() );
0115     int const y = ( height() - handleImageHeight ) * ( 1 - fraction );
0116     painter.drawPixmap( 0, y, pixmap( m_handleImagePath ) );
0117     painter.end();
0118 }
0119 
0120 }
0121 
0122 #include "moc_NavigationSlider.cpp"