mirror-chatterino2/widgets/scrollbar.h

179 lines
3.4 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-02-02 00:25:57 +01:00
Q_PROPERTY(qreal desiredValue READ getDesiredValue WRITE setDesiredValue)
2017-01-26 04:26:40 +01:00
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-02-02 00:25:57 +01:00
setDesiredValue(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));
2017-02-02 00:25:57 +01:00
if (this->desiredValue != value) {
2017-01-26 04:26:40 +01:00
if (animated) {
2017-02-02 00:25:57 +01:00
this->currentValueAnimation.stop();
this->currentValueAnimation.setStartValue(this->currentValue);
2017-01-26 05:26:21 +01:00
2017-02-02 00:25:57 +01:00
this->currentValueAnimation.setEndValue(value);
this->currentValueAnimation.start();
2017-01-26 04:26:40 +01:00
} else {
2017-02-02 00:25:57 +01:00
this->currentValueAnimation.stop();
2017-01-26 04:26:40 +01:00
2017-02-02 00:25:57 +01:00
this->setCurrentValue(value);
}
2017-01-26 04:26:40 +01:00
}
2017-02-02 20:35:12 +01:00
this->desiredValue = value;
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
2017-02-02 00:25:57 +01:00
getDesiredValue() const
2017-01-18 04:33:30 +01:00
{
2017-02-02 00:25:57 +01:00
return this->desiredValue;
}
qreal
getCurrentValue() const
{
return this->currentValue;
2017-01-18 04:33:30 +01:00
}
2017-01-26 04:26:40 +01:00
boost::signals2::signal<void()> &
2017-02-02 00:25:57 +01:00
getCurrentValueChanged()
2017-01-26 04:26:40 +01:00
{
2017-02-02 00:25:57 +01:00
return currentValueChanged;
}
void
setCurrentValue(qreal value)
{
value = std::max(this->minimum,
std::min(this->maximum - this->largeChange, value));
if (this->currentValue != value) {
this->currentValue = value;
this->updateScroll();
this->currentValueChanged();
this->update();
}
2017-01-26 04:26:40 +01:00
}
2017-01-03 21:19:33 +01:00
private:
2017-02-02 00:25:57 +01:00
Q_PROPERTY(qreal currentValue READ getCurrentValue WRITE setCurrentValue)
2017-01-03 21:19:33 +01:00
QMutex mutex;
2017-01-18 04:33:30 +01:00
ScrollBarHighlight *highlights;
2017-01-26 04:26:40 +01:00
2017-02-02 00:25:57 +01:00
QPropertyAnimation currentValueAnimation;
2017-01-26 04:26:40 +01:00
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;
2017-02-02 00:25:57 +01:00
qreal desiredValue;
qreal currentValue;
2017-01-18 04:33:30 +01:00
2017-02-02 00:25:57 +01:00
boost::signals2::signal<void()> currentValueChanged;
2017-01-26 04:26:40 +01:00
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