VolumeSlider.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "VolumeSlider.hpp"
  2. #include <QPainter>
  3. #include "moc_VolumeSlider.cpp"
  4. VolumeSlider::VolumeSlider(obs_fader_t *fader, QWidget *parent) : AbsoluteSlider(parent)
  5. {
  6. fad = fader;
  7. }
  8. VolumeSlider::VolumeSlider(obs_fader_t *fader, Qt::Orientation orientation, QWidget *parent)
  9. : AbsoluteSlider(orientation, parent)
  10. {
  11. fad = fader;
  12. }
  13. bool VolumeSlider::getDisplayTicks() const
  14. {
  15. return displayTicks;
  16. }
  17. void VolumeSlider::setDisplayTicks(bool display)
  18. {
  19. displayTicks = display;
  20. }
  21. void VolumeSlider::paintEvent(QPaintEvent *event)
  22. {
  23. if (!getDisplayTicks()) {
  24. QSlider::paintEvent(event);
  25. return;
  26. }
  27. QPainter painter(this);
  28. QColor tickColor(91, 98, 115, 255);
  29. obs_fader_conversion_t fader_db_to_def = obs_fader_db_to_def(fad);
  30. QStyleOptionSlider opt;
  31. initStyleOption(&opt);
  32. QRect groove = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
  33. QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
  34. if (orientation() == Qt::Horizontal) {
  35. const int sliderWidth = groove.width() - handle.width();
  36. float tickLength = groove.height() * 1.5;
  37. tickLength = std::max((int)tickLength + groove.height(), 8 + groove.height());
  38. float yPos = groove.center().y() - (tickLength / 2) + 1;
  39. for (int db = -10; db >= -90; db -= 10) {
  40. float tickValue = fader_db_to_def(db);
  41. float xPos = groove.left() + (tickValue * sliderWidth) + (handle.width() / 2);
  42. painter.fillRect(xPos, yPos, 1, tickLength, tickColor);
  43. }
  44. }
  45. if (orientation() == Qt::Vertical) {
  46. const int sliderHeight = groove.height() - handle.height();
  47. float tickLength = groove.width() * 1.5;
  48. tickLength = std::max((int)tickLength + groove.width(), 8 + groove.width());
  49. float xPos = groove.center().x() - (tickLength / 2) + 1;
  50. for (int db = -10; db >= -96; db -= 10) {
  51. float tickValue = fader_db_to_def(db);
  52. float yPos =
  53. groove.height() + groove.top() - (tickValue * sliderHeight) - (handle.height() / 2);
  54. painter.fillRect(xPos, yPos, tickLength, 1, tickColor);
  55. }
  56. }
  57. QSlider::paintEvent(event);
  58. }