/src/libtomahawk/widgets/SeekSlider.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 129 lines · 84 code · 21 blank · 24 comment · 8 complexity · 312e0a3b963ba427092d3bb94f2a5195 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2016, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5. * Copyright 2013, Teo Mrnjavac <teo@kde.org>
  6. *
  7. * Tomahawk is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tomahawk is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "SeekSlider.h"
  21. #include <QtGui/QMouseEvent>
  22. #include <QtCore/QTimeLine>
  23. #include "utils/TomahawkUtils.h"
  24. #include "utils/TomahawkStyle.h"
  25. #include "utils/Logger.h"
  26. SeekSlider::SeekSlider( QWidget* parent )
  27. : QSlider( parent )
  28. , TomahawkUtils::DpiScaler( this )
  29. , m_timeLine( 0 )
  30. , m_acceptWheelEvents( true )
  31. , m_isScrubbing( false )
  32. {
  33. setStyleSheet( QString(
  34. "QSlider::groove:horizontal {"
  35. "margin-top: %1px; margin-bottom: %1px; border: %2px solid rgba(200, 200, 200, 0); background: rgba(200, 200, 200, 40);"
  36. // "border-image: url(" RESPATH "images/seek-slider-bkg.png) %2 %2 %2 %2 stretch stretch;"
  37. "}"
  38. "QSlider::sub-page:horizontal {"
  39. "margin-top: %1px; margin-bottom: %1px; border: %2px solid rgba(0, 0, 0, 0); background: %3;"
  40. // "border-image: url(" RESPATH "images/seek-slider-level.png) %2 %2 %2 %2 stretch stretch;"
  41. "}" )
  42. .arg( scaledX( 7 ) /*margin*/ )
  43. .arg( 0 /*border*/ )
  44. .arg( /*color*/ TomahawkStyle::SEEKSLIDER_FOREGROUND.name() ) +
  45. QString(
  46. "QSlider::handle:horizontal {"
  47. "margin-bottom: -%1px; margin-top: -%1px;"
  48. "margin-left: -%2px; margin-right: -%2px;"
  49. "height: %3px; width: %4px;"
  50. // "background-image: url(" RESPATH "images/seek-and-volume-knob-rest.png);"
  51. "background-repeat: no-repeat;"
  52. "}" )
  53. .arg( /*margin top&bottom*/ 0 )
  54. .arg( /*margin left&right*/ 0 )
  55. .arg( /*height*/ 0 )
  56. .arg( /*width*/ 0 ) );
  57. }
  58. SeekSlider::~SeekSlider()
  59. {
  60. }
  61. void
  62. SeekSlider::mousePressEvent( QMouseEvent* event )
  63. {
  64. if ( event->button() == Qt::LeftButton )
  65. {
  66. m_isScrubbing = true;
  67. QMouseEvent eventSwap( QEvent::MouseButtonRelease, event->pos(), event->globalPos(), Qt::MidButton, Qt::MidButton, event->modifiers() );
  68. QSlider::mousePressEvent( &eventSwap );
  69. }
  70. else
  71. QSlider::mousePressEvent( event );
  72. }
  73. void
  74. SeekSlider::setValue( int value )
  75. {
  76. // int newVal = qBound( minimum(), value, maximum() );
  77. if ( !m_timeLine || sender() != m_timeLine )
  78. {
  79. QSlider::setValue( value );
  80. return;
  81. }
  82. blockSignals( true );
  83. QSlider::setValue( value );
  84. blockSignals( false );
  85. }
  86. void
  87. SeekSlider::wheelEvent( QWheelEvent* event )
  88. {
  89. if ( m_acceptWheelEvents )
  90. {
  91. QAbstractSlider::wheelEvent( event );
  92. return;
  93. }
  94. event->ignore();
  95. }
  96. void
  97. SeekSlider::mouseMoveEvent( QMouseEvent* event )
  98. {
  99. if ( !m_isScrubbing )
  100. return;
  101. // disable further scrubbing when we're past the slider's right margin
  102. if ( event->pos().x() > width() )
  103. {
  104. m_isScrubbing = false;
  105. return;
  106. }
  107. QSlider::mouseMoveEvent( event );
  108. }