Advertisement
Guest User

CPP File

a guest
Dec 7th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include "Gradient.h"
  2. #include <QVector>
  3. #include <QApplication>
  4. #include <QtCore>
  5.  
  6. Gradient::Gradient(QVector<StopColor> gradient)
  7. {
  8.     this->setSizePolicy(
  9.                 QSizePolicy::MinimumExpanding,
  10.                 QSizePolicy::MinimumExpanding
  11.                 );
  12.  
  13.     if (gradient.isEmpty())
  14.     {
  15.         m_gradient = gradient;
  16.     }
  17.     else
  18.     {
  19.         // self._gradient = [
  20.         //     (0.0, '#000000'),
  21.         //     (1.0, '#ffffff'),
  22.         // ]
  23.     }
  24.  
  25.     this->m_handle_w = 10;
  26.     this->m_handle_h = 10;
  27.     this->m_drag_position = 0;
  28. }
  29.  
  30. void Gradient::paintEvent(QPaintEvent *e)
  31. {
  32.     QPainter painter(this);
  33.     int width = painter.device()->width();
  34.     int height = painter.device()->height();
  35.  
  36.     // Draw the linear horizontal gradient.
  37.  
  38.     QLinearGradient gradient(0, 0, width, 0);
  39.  
  40.     for (StopColor &StopColor : m_gradient)
  41.     {
  42.         gradient.setColorAt(StopColor.stop, QColor(StopColor.color));
  43.     }
  44.  
  45.     QRect rect(0, 0, width, height);
  46.     painter.fillRect(rect, gradient);
  47.  
  48.     QPen pen;
  49.  
  50.     int y = painter.device()->height() / 2;
  51.  
  52.     // Draw the stop handles.
  53.     for (StopColor &StopColor : m_gradient)
  54.     {
  55.         pen.setColor(QColor(Qt::white));
  56.         painter.setPen(pen);
  57.  
  58.         painter.drawLine(StopColor.stop * width, y - this->m_handle_h, StopColor.stop * width, y + this->m_handle_h);
  59.  
  60.         pen.setColor(QColor(Qt::red));
  61.         painter.setPen(pen);
  62.  
  63.         QRect rect(
  64.                     StopColor.stop * width - this->m_handle_h/2,
  65.                     y - this->m_handle_h/2,
  66.                     this->m_handle_w,
  67.                     this->m_handle_h
  68.                     );
  69.         painter.drawRect(rect);
  70.     }
  71.     painter.end();
  72. }
  73.  
  74. void Gradient::_sort_gradient()
  75. {
  76.     std::sort(this->m_gradient.begin(), this->m_gradient.end());
  77. }
  78.  
  79. void Gradient::_constrain_gradient()
  80. {
  81.     for (StopColor &stopColor : this->m_gradient)
  82.         stopColor.stop = std::max(0.0, std::min(1.0, stopColor.stop));
  83. }
  84.  
  85. void Gradient::setGradient(QVector<StopColor> gradient)
  86. {
  87.     // assert all([0.0 <= stop <= 1.0 for stop, _ in gradient])
  88.     this->m_gradient = gradient;
  89.     this->_constrain_gradient();
  90.     this->_sort_gradient();
  91.     //emit this->gradientChanged();
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement