mirror-chatterino2/scrollbar.cpp

114 lines
2.4 KiB
C++
Raw Normal View History

2017-01-03 21:19:33 +01:00
#include "scrollbar.h"
2017-01-11 18:52:09 +01:00
#include "QPainter"
2017-01-03 21:19:33 +01:00
#include "colorscheme.h"
2017-01-18 04:33:30 +01:00
#define MIN_THUMB_HEIGHT 10
2017-01-11 18:52:09 +01:00
ScrollBar::ScrollBar(QWidget *widget)
: QWidget(widget)
, mutex()
2017-01-18 04:33:30 +01:00
, highlights(NULL)
, thumbRect()
, maximum()
, minimum()
, largeChange()
, smallChange()
, value()
2017-01-03 21:19:33 +01:00
{
resize(16, 100);
}
ScrollBar::~ScrollBar()
{
2017-01-18 04:33:30 +01:00
auto highlight = this->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-01-11 18:52:09 +01:00
void
ScrollBar::removeHighlightsWhere(std::function<bool(ScrollBarHighlight &)> func)
2017-01-03 21:19:33 +01:00
{
2017-01-18 04:33:30 +01:00
this->mutex.lock();
2017-01-03 21:19:33 +01:00
2017-01-11 18:52:09 +01:00
ScrollBarHighlight *last = NULL;
2017-01-18 04:33:30 +01:00
ScrollBarHighlight *current = this->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-01-18 04:33:30 +01:00
this->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-01-18 04:33:30 +01:00
this->mutex.unlock();
2017-01-03 21:19:33 +01:00
}
2017-01-11 18:52:09 +01:00
void
ScrollBar::addHighlight(ScrollBarHighlight *highlight)
2017-01-03 21:19:33 +01:00
{
2017-01-18 04:33:30 +01:00
this->mutex.lock();
2017-01-03 21:19:33 +01:00
2017-01-18 04:33:30 +01:00
if (this->highlights == NULL) {
this->highlights = highlight;
2017-01-11 18:52:09 +01:00
} else {
2017-01-18 04:33:30 +01:00
highlight->next = this->highlights->next;
this->highlights->next = highlight;
2017-01-03 21:19:33 +01:00
}
2017-01-18 04:33:30 +01:00
this->mutex.unlock();
2017-01-03 21:19:33 +01:00
}
2017-01-11 18:52:09 +01:00
void
ScrollBar::paintEvent(QPaintEvent *)
2017-01-03 21:19:33 +01:00
{
QPainter painter(this);
2017-01-06 23:28:48 +01:00
painter.fillRect(rect(), ColorScheme::instance().ScrollbarBG);
2017-01-03 21:19:33 +01:00
2017-01-18 04:33:30 +01:00
painter.fillRect(QRect(0, 0, width(), this->buttonHeight),
QColor(255, 0, 0));
painter.fillRect(
QRect(0, height() - this->buttonHeight, width(), this->buttonHeight),
QColor(255, 0, 0));
ScrollBarHighlight *highlight = this->highlights;
this->mutex.lock();
// do {
// painter.fillRect();
// } while ((highlight = highlight->next()) != NULL);
2017-01-03 21:19:33 +01:00
2017-01-18 04:33:30 +01:00
this->mutex.unlock();
}
void
ScrollBar::updateScroll()
{
this->trackHeight = height() - this->buttonHeight - this->buttonHeight -
MIN_THUMB_HEIGHT - 1;
this->thumbRect =
QRect(0,
(int)(this->value / this->maximum * this->trackHeight) + 1 +
this->buttonHeight,
width(),
(int)(this->largeChange / this->maximum * this->trackHeight) +
MIN_THUMB_HEIGHT);
repaint();
2017-01-03 21:19:33 +01:00
}