mirror-chatterino2/widgets/scrollbar.h

154 lines
2.8 KiB
C
Raw Normal View History

2017-01-03 21:19:33 +01:00
#ifndef SCROLLBAR_H
#define SCROLLBAR_H
2017-01-18 21:30:23 +01:00
#include "widgets/scrollbarhighlight.h"
2017-01-03 21:19:33 +01:00
2017-01-18 04:52:47 +01:00
#include <QMutex>
2017-01-26 04:26:40 +01:00
#include <QPropertyAnimation>
2017-01-18 04:52:47 +01:00
#include <QWidget>
2017-01-26 04:26:40 +01:00
#include <boost/signals2.hpp>
2017-01-18 04:52:47 +01:00
#include <functional>
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace widgets {
2017-01-03 21:19:33 +01:00
class ScrollBar : public QWidget
{
Q_OBJECT
public:
2017-01-11 18:52:09 +01:00
ScrollBar(QWidget *parent = 0);
2017-01-03 21:19:33 +01:00
~ScrollBar();
2017-01-11 18:52:09 +01:00
void removeHighlightsWhere(std::function<bool(ScrollBarHighlight &)> func);
void addHighlight(ScrollBarHighlight *highlight);
2017-01-03 21:19:33 +01:00
2017-01-26 04:26:40 +01:00
Q_PROPERTY(qreal value READ getValue WRITE setValue)
2017-01-18 04:33:30 +01:00
void
setMaximum(qreal value)
{
2017-01-18 14:48:42 +01:00
this->maximum = value;
2017-01-18 04:33:30 +01:00
2017-01-18 14:48:42 +01:00
this->updateScroll();
2017-01-18 04:33:30 +01:00
}
void
setMinimum(qreal value)
{
2017-01-18 14:48:42 +01:00
this->minimum = value;
2017-01-18 04:33:30 +01:00
2017-01-18 14:48:42 +01:00
this->updateScroll();
2017-01-18 04:33:30 +01:00
}
void
setLargeChange(qreal value)
{
2017-01-18 14:48:42 +01:00
this->largeChange = value;
2017-01-18 04:33:30 +01:00
2017-01-18 14:48:42 +01:00
this->updateScroll();
2017-01-18 04:33:30 +01:00
}
void
setSmallChange(qreal value)
{
2017-01-18 14:48:42 +01:00
this->smallChange = value;
2017-01-18 04:33:30 +01:00
2017-01-18 14:48:42 +01:00
this->updateScroll();
2017-01-18 04:33:30 +01:00
}
void
2017-01-26 04:26:40 +01:00
setValue(qreal value, bool animated = false)
2017-01-18 04:33:30 +01:00
{
2017-01-26 04:26:40 +01:00
value = std::max(this->minimum,
std::min(this->maximum - this->largeChange, value));
if (this->value != value) {
if (animated) {
this->valueAnimation.stop();
this->valueAnimation.setStartValue(this->value);
this->valueAnimation.setEndValue(value);
this->valueAnimation.start();
} else {
this->value = value;
}
this->updateScroll();
this->valueChanged();
this->repaint();
}
2017-01-18 04:33:30 +01:00
}
qreal
getMaximum() const
{
2017-01-18 14:48:42 +01:00
return this->maximum;
2017-01-18 04:33:30 +01:00
}
qreal
getMinimum() const
{
2017-01-18 14:48:42 +01:00
return this->minimum;
2017-01-18 04:33:30 +01:00
}
qreal
getLargeChange() const
{
2017-01-18 14:48:42 +01:00
return this->largeChange;
2017-01-18 04:33:30 +01:00
}
qreal
getSmallChange() const
{
2017-01-18 14:48:42 +01:00
return this->smallChange;
2017-01-18 04:33:30 +01:00
}
qreal
getValue() const
{
2017-01-18 14:48:42 +01:00
return this->value;
2017-01-18 04:33:30 +01:00
}
2017-01-26 04:26:40 +01:00
boost::signals2::signal<void()> &
getValueChanged()
{
return valueChanged;
}
2017-01-03 21:19:33 +01:00
private:
QMutex mutex;
2017-01-18 04:33:30 +01:00
ScrollBarHighlight *highlights;
2017-01-26 04:26:40 +01:00
QPropertyAnimation valueAnimation;
2017-01-03 21:19:33 +01:00
void paintEvent(QPaintEvent *);
2017-01-26 04:26:40 +01:00
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void leaveEvent(QEvent *);
int mouseOverIndex;
int mouseDownIndex;
QPoint lastMousePosition;
2017-01-03 21:19:33 +01:00
2017-01-18 04:33:30 +01:00
int buttonHeight;
int trackHeight;
2017-01-03 21:19:33 +01:00
QRect thumbRect;
2017-01-18 04:33:30 +01:00
qreal maximum;
qreal minimum;
qreal largeChange;
qreal smallChange;
qreal value;
2017-01-26 04:26:40 +01:00
boost::signals2::signal<void()> valueChanged;
2017-01-18 04:33:30 +01:00
void updateScroll();
2017-01-03 21:19:33 +01:00
};
2017-01-18 21:30:23 +01:00
}
}
2017-01-03 21:19:33 +01:00
2017-01-11 18:52:09 +01:00
#endif // SCROLLBAR_H