Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Gradient.h"
- #include <QVector>
- #include <QApplication>
- #include <QtCore>
- Gradient::Gradient(QVector<StopColor> gradient)
- {
- this->setSizePolicy(
- QSizePolicy::MinimumExpanding,
- QSizePolicy::MinimumExpanding
- );
- if (gradient.isEmpty())
- {
- m_gradient = gradient;
- }
- else
- {
- // self._gradient = [
- // (0.0, '#000000'),
- // (1.0, '#ffffff'),
- // ]
- }
- this->m_handle_w = 10;
- this->m_handle_h = 10;
- this->m_drag_position = 0;
- }
- void Gradient::paintEvent(QPaintEvent *e)
- {
- QPainter painter(this);
- int width = painter.device()->width();
- int height = painter.device()->height();
- // Draw the linear horizontal gradient.
- QLinearGradient gradient(0, 0, width, 0);
- for (StopColor &StopColor : m_gradient)
- {
- gradient.setColorAt(StopColor.stop, QColor(StopColor.color));
- }
- QRect rect(0, 0, width, height);
- painter.fillRect(rect, gradient);
- QPen pen;
- int y = painter.device()->height() / 2;
- // Draw the stop handles.
- for (StopColor &StopColor : m_gradient)
- {
- pen.setColor(QColor(Qt::white));
- painter.setPen(pen);
- painter.drawLine(StopColor.stop * width, y - this->m_handle_h, StopColor.stop * width, y + this->m_handle_h);
- pen.setColor(QColor(Qt::red));
- painter.setPen(pen);
- QRect rect(
- StopColor.stop * width - this->m_handle_h/2,
- y - this->m_handle_h/2,
- this->m_handle_w,
- this->m_handle_h
- );
- painter.drawRect(rect);
- }
- painter.end();
- }
- void Gradient::_sort_gradient()
- {
- std::sort(this->m_gradient.begin(), this->m_gradient.end());
- }
- void Gradient::_constrain_gradient()
- {
- for (StopColor &stopColor : this->m_gradient)
- stopColor.stop = std::max(0.0, std::min(1.0, stopColor.stop));
- }
- void Gradient::setGradient(QVector<StopColor> gradient)
- {
- // assert all([0.0 <= stop <= 1.0 for stop, _ in gradient])
- this->m_gradient = gradient;
- this->_constrain_gradient();
- this->_sort_gradient();
- //emit this->gradientChanged();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement