mirror-chatterino2/widgets/scrollbar.cpp

306 lines
6.5 KiB
C++
Raw Normal View History

2017-01-18 21:30:23 +01:00
#include "widgets/scrollbar.h"
2017-01-03 21:19:33 +01:00
#include "colorscheme.h"
2017-01-26 04:26:40 +01:00
#include <QMouseEvent>
2017-01-18 04:52:47 +01:00
#include <QPainter>
2017-01-18 04:33:30 +01:00
#define MIN_THUMB_HEIGHT 10
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
2017-01-11 18:52:09 +01:00
ScrollBar::ScrollBar(QWidget *widget)
: QWidget(widget)
2017-04-12 17:46:44 +02:00
, _mutex()
, _currentValueAnimation(this, "currentValue")
, _highlights(NULL)
, _mouseOverIndex(-1)
, _mouseDownIndex(-1)
, _lastMousePosition()
, _buttonHeight(16)
, _trackHeight(100)
, _thumbRect()
, _maximum()
, _minimum()
, _largeChange()
, _smallChange()
, _desiredValue()
, _currentValueChanged()
, _currentValue()
{
resize(16, 100);
_currentValueAnimation.setDuration(250);
_currentValueAnimation.setEasingCurve(QEasingCurve(QEasingCurve::OutCubic));
setMouseTracking(true);
2017-01-03 21:19:33 +01:00
}
ScrollBar::~ScrollBar()
{
2017-04-12 17:46:44 +02:00
auto highlight = _highlights;
2017-01-03 21:19:33 +01:00
2017-01-11 18:52:09 +01:00
while (highlight != NULL) {
2017-01-03 21:19:33 +01:00
auto tmp = highlight->next;
delete highlight;
highlight = tmp;
}
}
2017-04-12 17:46:44 +02:00
void ScrollBar::removeHighlightsWhere(std::function<bool(ScrollBarHighlight &)> func)
2017-01-03 21:19:33 +01:00
{
2017-04-12 17:46:44 +02:00
_mutex.lock();
2017-01-03 21:19:33 +01:00
2017-01-11 18:52:09 +01:00
ScrollBarHighlight *last = NULL;
2017-04-12 17:46:44 +02:00
ScrollBarHighlight *current = _highlights;
2017-01-03 21:19:33 +01:00
2017-01-11 18:52:09 +01:00
while (current != NULL) {
if (func(*current)) {
if (last == NULL) {
2017-04-12 17:46:44 +02:00
_highlights = current->next;
2017-01-11 18:52:09 +01:00
} else {
2017-01-03 21:19:33 +01:00
last->next = current->next;
}
auto oldCurrent = current;
current = current->next;
last = current;
delete oldCurrent;
}
}
2017-04-12 17:46:44 +02:00
_mutex.unlock();
2017-01-03 21:19:33 +01:00
}
2017-04-12 17:46:44 +02:00
void ScrollBar::addHighlight(ScrollBarHighlight *highlight)
2017-01-03 21:19:33 +01:00
{
2017-04-12 17:46:44 +02:00
_mutex.lock();
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
if (_highlights == NULL) {
_highlights = highlight;
2017-01-11 18:52:09 +01:00
} else {
2017-04-12 17:46:44 +02:00
highlight->next = _highlights->next;
_highlights->next = highlight;
}
_mutex.unlock();
}
void ScrollBar::setMaximum(qreal value)
{
_maximum = value;
updateScroll();
}
void ScrollBar::setMinimum(qreal value)
{
_minimum = value;
updateScroll();
}
void ScrollBar::setLargeChange(qreal value)
{
_largeChange = value;
updateScroll();
}
void ScrollBar::setSmallChange(qreal value)
{
_smallChange = value;
updateScroll();
}
void ScrollBar::setDesiredValue(qreal value, bool animated)
{
value = std::max(_minimum, std::min(_maximum - _largeChange, value));
if (_desiredValue != value) {
if (animated) {
_currentValueAnimation.stop();
_currentValueAnimation.setStartValue(_currentValue);
_currentValueAnimation.setEndValue(value);
_currentValueAnimation.start();
} else {
if (_currentValueAnimation.state() != QPropertyAnimation::Running) {
// currentValueAnimation.stop();
setCurrentValue(value);
}
}
2017-01-03 21:19:33 +01:00
}
2017-04-12 17:46:44 +02:00
_desiredValue = value;
}
qreal ScrollBar::getMaximum() const
{
return _maximum;
}
qreal ScrollBar::getMinimum() const
{
return _minimum;
}
qreal ScrollBar::getLargeChange() const
{
return _largeChange;
}
qreal ScrollBar::getSmallChange() const
{
return _smallChange;
}
qreal ScrollBar::getDesiredValue() const
{
return _desiredValue;
}
qreal ScrollBar::getCurrentValue() const
{
return _currentValue;
}
boost::signals2::signal<void()> &ScrollBar::getCurrentValueChanged()
{
return _currentValueChanged;
}
void ScrollBar::setCurrentValue(qreal value)
{
value = std::max(_minimum, std::min(_maximum - _largeChange, value));
if (_currentValue != value) {
_currentValue = value;
updateScroll();
_currentValueChanged();
update();
}
2017-01-03 21:19:33 +01:00
}
2017-04-12 17:46:44 +02:00
void ScrollBar::paintEvent(QPaintEvent *)
2017-01-03 21:19:33 +01:00
{
QPainter painter(this);
2017-01-24 20:15:12 +01:00
painter.fillRect(rect(), ColorScheme::getInstance().ScrollbarBG);
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
painter.fillRect(QRect(0, 0, width(), _buttonHeight), QColor(255, 0, 0));
painter.fillRect(QRect(0, height() - _buttonHeight, width(), _buttonHeight), QColor(255, 0, 0));
2017-01-18 04:33:30 +01:00
2017-04-12 17:46:44 +02:00
painter.fillRect(_thumbRect, QColor(0, 255, 255));
2017-01-26 04:26:40 +01:00
2017-04-12 17:46:44 +02:00
// ScrollBarHighlight *highlight = highlights;
2017-01-18 04:33:30 +01:00
2017-04-12 17:46:44 +02:00
_mutex.lock();
2017-01-18 04:33:30 +01:00
// do {
// painter.fillRect();
// } while ((highlight = highlight->next()) != NULL);
2017-01-03 21:19:33 +01:00
2017-04-12 17:46:44 +02:00
_mutex.unlock();
2017-01-18 04:33:30 +01:00
}
2017-04-12 17:46:44 +02:00
void ScrollBar::mouseMoveEvent(QMouseEvent *event)
2017-01-26 04:26:40 +01:00
{
2017-04-12 17:46:44 +02:00
if (_mouseDownIndex == -1) {
2017-01-26 04:26:40 +01:00
int y = event->pos().y();
2017-04-12 17:46:44 +02:00
auto oldIndex = _mouseOverIndex;
2017-01-26 04:26:40 +01:00
2017-04-12 17:46:44 +02:00
if (y < _buttonHeight) {
_mouseOverIndex = 0;
} else if (y < _thumbRect.y()) {
_mouseOverIndex = 1;
} else if (_thumbRect.contains(2, y)) {
_mouseOverIndex = 2;
} else if (y < height() - _buttonHeight) {
_mouseOverIndex = 3;
2017-01-26 04:26:40 +01:00
} else {
2017-04-12 17:46:44 +02:00
_mouseOverIndex = 4;
2017-01-26 04:26:40 +01:00
}
2017-04-12 17:46:44 +02:00
if (oldIndex != _mouseOverIndex) {
update();
2017-01-26 04:26:40 +01:00
}
2017-04-12 17:46:44 +02:00
} else if (_mouseDownIndex == 2) {
int delta = event->pos().y() - _lastMousePosition.y();
2017-01-26 04:26:40 +01:00
2017-04-12 17:46:44 +02:00
setDesiredValue(_desiredValue + (qreal)delta / _trackHeight * _maximum);
2017-01-26 04:26:40 +01:00
}
2017-04-12 17:46:44 +02:00
_lastMousePosition = event->pos();
2017-01-26 04:26:40 +01:00
}
2017-04-12 17:46:44 +02:00
void ScrollBar::mousePressEvent(QMouseEvent *event)
2017-01-26 04:26:40 +01:00
{
int y = event->pos().y();
2017-04-12 17:46:44 +02:00
if (y < _buttonHeight) {
_mouseDownIndex = 0;
} else if (y < _thumbRect.y()) {
_mouseDownIndex = 1;
} else if (_thumbRect.contains(2, y)) {
_mouseDownIndex = 2;
} else if (y < height() - _buttonHeight) {
_mouseDownIndex = 3;
2017-01-26 04:26:40 +01:00
} else {
2017-04-12 17:46:44 +02:00
_mouseDownIndex = 4;
2017-01-26 04:26:40 +01:00
}
}
2017-04-12 17:46:44 +02:00
void ScrollBar::mouseReleaseEvent(QMouseEvent *event)
2017-01-26 04:26:40 +01:00
{
int y = event->pos().y();
2017-04-12 17:46:44 +02:00
if (y < _buttonHeight) {
if (_mouseDownIndex == 0) {
setDesiredValue(_desiredValue - _smallChange, true);
2017-01-26 04:26:40 +01:00
}
2017-04-12 17:46:44 +02:00
} else if (y < _thumbRect.y()) {
if (_mouseDownIndex == 1) {
setDesiredValue(_desiredValue - _smallChange, true);
2017-01-26 04:26:40 +01:00
}
2017-04-12 17:46:44 +02:00
} else if (_thumbRect.contains(2, y)) {
2017-01-26 04:26:40 +01:00
// do nothing
2017-04-12 17:46:44 +02:00
} else if (y < height() - _buttonHeight) {
if (_mouseDownIndex == 3) {
setDesiredValue(_desiredValue + _smallChange, true);
2017-01-26 04:26:40 +01:00
}
} else {
2017-04-12 17:46:44 +02:00
if (_mouseDownIndex == 4) {
setDesiredValue(_desiredValue + _smallChange, true);
2017-01-26 04:26:40 +01:00
}
}
2017-04-12 17:46:44 +02:00
_mouseDownIndex = -1;
2017-01-26 07:10:46 +01:00
update();
2017-01-26 04:26:40 +01:00
}
2017-04-12 17:46:44 +02:00
void ScrollBar::leaveEvent(QEvent *)
2017-01-26 04:26:40 +01:00
{
2017-04-12 17:46:44 +02:00
_mouseOverIndex = -1;
2017-01-26 04:26:40 +01:00
2017-01-26 07:10:46 +01:00
update();
2017-01-26 04:26:40 +01:00
}
2017-04-12 17:46:44 +02:00
void ScrollBar::updateScroll()
2017-01-18 04:33:30 +01:00
{
2017-04-12 17:46:44 +02:00
_trackHeight = height() - _buttonHeight - _buttonHeight - MIN_THUMB_HEIGHT - 1;
2017-01-18 04:33:30 +01:00
2017-04-12 17:46:44 +02:00
_thumbRect = QRect(0, (int)(_currentValue / _maximum * _trackHeight) + 1 + _buttonHeight,
width(), (int)(_largeChange / _maximum * _trackHeight) + MIN_THUMB_HEIGHT);
2017-01-18 04:33:30 +01:00
2017-01-26 07:10:46 +01:00
update();
2017-01-03 21:19:33 +01:00
}
2017-01-18 21:30:23 +01:00
}
}