fixed coding style for scrollbar

This commit is contained in:
fourtf
2017-12-19 01:11:35 +01:00
parent c950e9c58a
commit 368ec7c903
2 changed files with 117 additions and 115 deletions
+98 -96
View File
@@ -13,20 +13,20 @@ namespace widgets {
ScrollBar::ScrollBar(ChannelView *parent) ScrollBar::ScrollBar(ChannelView *parent)
: BaseWidget(parent) : BaseWidget(parent)
, _currentValueAnimation(this, "_currentValue") , currentValueAnimation(this, "currentValue")
, _highlights(nullptr) , highlights(nullptr)
, smoothScrollingSetting(SettingsManager::getInstance().enableSmoothScrolling) , smoothScrollingSetting(SettingsManager::getInstance().enableSmoothScrolling)
{ {
resize(16, 100); resize(16, 100);
_currentValueAnimation.setDuration(250); this->currentValueAnimation.setDuration(250);
_currentValueAnimation.setEasingCurve(QEasingCurve(QEasingCurve::OutCubic)); this->currentValueAnimation.setEasingCurve(QEasingCurve(QEasingCurve::OutCubic));
setMouseTracking(true); setMouseTracking(true);
} }
ScrollBar::~ScrollBar() ScrollBar::~ScrollBar()
{ {
auto highlight = _highlights; auto highlight = this->highlights;
while (highlight != nullptr) { while (highlight != nullptr) {
auto tmp = highlight->next; auto tmp = highlight->next;
@@ -37,15 +37,15 @@ ScrollBar::~ScrollBar()
void ScrollBar::removeHighlightsWhere(std::function<bool(ScrollBarHighlight &)> func) void ScrollBar::removeHighlightsWhere(std::function<bool(ScrollBarHighlight &)> func)
{ {
_mutex.lock(); this->mutex.lock();
ScrollBarHighlight *last = nullptr; ScrollBarHighlight *last = nullptr;
ScrollBarHighlight *current = _highlights; ScrollBarHighlight *current = this->highlights;
while (current != nullptr) { while (current != nullptr) {
if (func(*current)) { if (func(*current)) {
if (last == nullptr) { if (last == nullptr) {
_highlights = current->next; this->highlights = current->next;
} else { } else {
last->next = current->next; last->next = current->next;
} }
@@ -59,26 +59,26 @@ void ScrollBar::removeHighlightsWhere(std::function<bool(ScrollBarHighlight &)>
} }
} }
_mutex.unlock(); this->mutex.unlock();
} }
void ScrollBar::addHighlight(ScrollBarHighlight *highlight) void ScrollBar::addHighlight(ScrollBarHighlight *highlight)
{ {
_mutex.lock(); this->mutex.lock();
if (_highlights == nullptr) { if (this->highlights == nullptr) {
_highlights = highlight; this->highlights = highlight;
} else { } else {
highlight->next = _highlights->next; highlight->next = this->highlights->next;
_highlights->next = highlight; this->highlights->next = highlight;
} }
_mutex.unlock(); this->mutex.unlock();
} }
void ScrollBar::scrollToBottom() void ScrollBar::scrollToBottom()
{ {
this->setDesiredValue(this->_maximum - this->getLargeChange()); this->setDesiredValue(this->maximum - this->getLargeChange());
} }
bool ScrollBar::isAtBottom() const bool ScrollBar::isAtBottom() const
@@ -88,28 +88,28 @@ bool ScrollBar::isAtBottom() const
void ScrollBar::setMaximum(qreal value) void ScrollBar::setMaximum(qreal value)
{ {
_maximum = value; this->maximum = value;
updateScroll(); updateScroll();
} }
void ScrollBar::setMinimum(qreal value) void ScrollBar::setMinimum(qreal value)
{ {
_minimum = value; this->minimum = value;
updateScroll(); updateScroll();
} }
void ScrollBar::setLargeChange(qreal value) void ScrollBar::setLargeChange(qreal value)
{ {
_largeChange = value; this->largeChange = value;
updateScroll(); updateScroll();
} }
void ScrollBar::setSmallChange(qreal value) void ScrollBar::setSmallChange(qreal value)
{ {
_smallChange = value; this->smallChange = value;
updateScroll(); updateScroll();
} }
@@ -117,24 +117,25 @@ void ScrollBar::setSmallChange(qreal value)
void ScrollBar::setDesiredValue(qreal value, bool animated) void ScrollBar::setDesiredValue(qreal value, bool animated)
{ {
animated &= this->smoothScrollingSetting.getValue(); animated &= this->smoothScrollingSetting.getValue();
value = std::max(_minimum, std::min(_maximum - _largeChange, value)); value = std::max(this->minimum, std::min(this->maximum - this->largeChange, value));
if (_desiredValue + _smoothScrollingOffset != value) { if (this->desiredValue + this->smoothScrollingOffset != value) {
if (animated) { if (animated) {
_currentValueAnimation.stop(); this->currentValueAnimation.stop();
_currentValueAnimation.setStartValue(_currentValue + _smoothScrollingOffset); this->currentValueAnimation.setStartValue(this->currentValue +
this->smoothScrollingOffset);
// if (((this->getMaximum() - this->getLargeChange()) - value) <= 0.01) { // if (((this->getMaximum() - this->getLargeChange()) - value) <= 0.01) {
// value += 1; // value += 1;
// } // }
_currentValueAnimation.setEndValue(value); this->currentValueAnimation.setEndValue(value);
_smoothScrollingOffset = 0; this->smoothScrollingOffset = 0;
_currentValueAnimation.start(); this->currentValueAnimation.start();
} else { } else {
if (_currentValueAnimation.state() != QPropertyAnimation::Running) { if (this->currentValueAnimation.state() != QPropertyAnimation::Running) {
_smoothScrollingOffset = 0; this->smoothScrollingOffset = 0;
_desiredValue = value; this->desiredValue = value;
_currentValueAnimation.stop(); this->currentValueAnimation.stop();
setCurrentValue(value); setCurrentValue(value);
} }
} }
@@ -142,44 +143,44 @@ void ScrollBar::setDesiredValue(qreal value, bool animated)
this->atBottom = ((this->getMaximum() - this->getLargeChange()) - value) <= 0.01; this->atBottom = ((this->getMaximum() - this->getLargeChange()) - value) <= 0.01;
_smoothScrollingOffset = 0; this->smoothScrollingOffset = 0;
_desiredValue = value; this->desiredValue = value;
} }
qreal ScrollBar::getMaximum() const qreal ScrollBar::getMaximum() const
{ {
return _maximum; return this->maximum;
} }
qreal ScrollBar::getMinimum() const qreal ScrollBar::getMinimum() const
{ {
return _minimum; return this->minimum;
} }
qreal ScrollBar::getLargeChange() const qreal ScrollBar::getLargeChange() const
{ {
return _largeChange; return this->largeChange;
} }
qreal ScrollBar::getSmallChange() const qreal ScrollBar::getSmallChange() const
{ {
return _smallChange; return this->smallChange;
} }
qreal ScrollBar::getDesiredValue() const qreal ScrollBar::getDesiredValue() const
{ {
return _desiredValue + _smoothScrollingOffset; return this->desiredValue + this->smoothScrollingOffset;
} }
qreal ScrollBar::getCurrentValue() const qreal ScrollBar::getCurrentValue() const
{ {
return _currentValue; return this->currentValue;
} }
void ScrollBar::offset(qreal value) void ScrollBar::offset(qreal value)
{ {
if (_currentValueAnimation.state() == QPropertyAnimation::Running) { if (this->currentValueAnimation.state() == QPropertyAnimation::Running) {
this->_smoothScrollingOffset += value; this->smoothScrollingOffset += value;
} else { } else {
this->setDesiredValue(this->getDesiredValue() + value); this->setDesiredValue(this->getDesiredValue() + value);
} }
@@ -187,19 +188,19 @@ void ScrollBar::offset(qreal value)
boost::signals2::signal<void()> &ScrollBar::getCurrentValueChanged() boost::signals2::signal<void()> &ScrollBar::getCurrentValueChanged()
{ {
return _currentValueChanged; return this->currentValueChanged;
} }
void ScrollBar::setCurrentValue(qreal value) void ScrollBar::setCurrentValue(qreal value)
{ {
value = value = std::max(this->minimum, std::min(this->maximum - this->largeChange,
std::max(_minimum, std::min(_maximum - _largeChange, value + this->_smoothScrollingOffset)); value + this->smoothScrollingOffset));
if (_currentValue != value) { if (this->currentValue != value) {
_currentValue = value; this->currentValue = value;
updateScroll(); updateScroll();
_currentValueChanged(); this->currentValueChanged();
update(); update();
} }
@@ -219,75 +220,75 @@ void ScrollBar::paintEvent(QPaintEvent *)
QPainter painter(this); QPainter painter(this);
painter.fillRect(rect(), this->colorScheme.ScrollbarBG); painter.fillRect(rect(), this->colorScheme.ScrollbarBG);
painter.fillRect(QRect(0, 0, width(), _buttonHeight), this->colorScheme.ScrollbarArrow); painter.fillRect(QRect(0, 0, width(), this->buttonHeight), this->colorScheme.ScrollbarArrow);
painter.fillRect(QRect(0, height() - _buttonHeight, width(), _buttonHeight), painter.fillRect(QRect(0, height() - this->buttonHeight, width(), this->buttonHeight),
this->colorScheme.ScrollbarArrow); this->colorScheme.ScrollbarArrow);
// mouse over thumb // mouse over thumb
if (this->_mouseDownIndex == 2) { if (this->mouseDownIndex == 2) {
painter.fillRect(_thumbRect, this->colorScheme.ScrollbarThumbSelected); painter.fillRect(this->thumbRect, this->colorScheme.ScrollbarThumbSelected);
} }
// mouse not over thumb // mouse not over thumb
else { else {
painter.fillRect(_thumbRect, this->colorScheme.ScrollbarThumb); painter.fillRect(this->thumbRect, this->colorScheme.ScrollbarThumb);
} }
// ScrollBarHighlight *highlight = highlights; // ScrollBarHighlight *highlight = highlights;
_mutex.lock(); this->mutex.lock();
// do { // do {
// painter.fillRect(); // painter.fillRect();
// } while ((highlight = highlight->next()) != nullptr); // } while ((highlight = highlight->next()) != nullptr);
_mutex.unlock(); this->mutex.unlock();
} }
void ScrollBar::mouseMoveEvent(QMouseEvent *event) void ScrollBar::mouseMoveEvent(QMouseEvent *event)
{ {
if (_mouseDownIndex == -1) { if (this->mouseDownIndex == -1) {
int y = event->pos().y(); int y = event->pos().y();
auto oldIndex = _mouseOverIndex; auto oldIndex = this->mouseOverIndex;
if (y < _buttonHeight) { if (y < this->buttonHeight) {
_mouseOverIndex = 0; this->mouseOverIndex = 0;
} else if (y < _thumbRect.y()) { } else if (y < this->thumbRect.y()) {
_mouseOverIndex = 1; this->mouseOverIndex = 1;
} else if (_thumbRect.contains(2, y)) { } else if (this->thumbRect.contains(2, y)) {
_mouseOverIndex = 2; this->mouseOverIndex = 2;
} else if (y < height() - _buttonHeight) { } else if (y < height() - this->buttonHeight) {
_mouseOverIndex = 3; this->mouseOverIndex = 3;
} else { } else {
_mouseOverIndex = 4; this->mouseOverIndex = 4;
} }
if (oldIndex != _mouseOverIndex) { if (oldIndex != this->mouseOverIndex) {
update(); update();
} }
} else if (_mouseDownIndex == 2) { } else if (this->mouseDownIndex == 2) {
int delta = event->pos().y() - _lastMousePosition.y(); int delta = event->pos().y() - this->lastMousePosition.y();
setDesiredValue(_desiredValue + (qreal)delta / _trackHeight * _maximum); setDesiredValue(this->desiredValue + (qreal)delta / this->trackHeight * this->maximum);
} }
_lastMousePosition = event->pos(); this->lastMousePosition = event->pos();
} }
void ScrollBar::mousePressEvent(QMouseEvent *event) void ScrollBar::mousePressEvent(QMouseEvent *event)
{ {
int y = event->pos().y(); int y = event->pos().y();
if (y < _buttonHeight) { if (y < this->buttonHeight) {
_mouseDownIndex = 0; this->mouseDownIndex = 0;
} else if (y < _thumbRect.y()) { } else if (y < this->thumbRect.y()) {
_mouseDownIndex = 1; this->mouseDownIndex = 1;
} else if (_thumbRect.contains(2, y)) { } else if (this->thumbRect.contains(2, y)) {
_mouseDownIndex = 2; this->mouseDownIndex = 2;
} else if (y < height() - _buttonHeight) { } else if (y < height() - this->buttonHeight) {
_mouseDownIndex = 3; this->mouseDownIndex = 3;
} else { } else {
_mouseDownIndex = 4; this->mouseDownIndex = 4;
} }
} }
@@ -295,43 +296,44 @@ void ScrollBar::mouseReleaseEvent(QMouseEvent *event)
{ {
int y = event->pos().y(); int y = event->pos().y();
if (y < _buttonHeight) { if (y < this->buttonHeight) {
if (_mouseDownIndex == 0) { if (this->mouseDownIndex == 0) {
setDesiredValue(_desiredValue - _smallChange, true); setDesiredValue(this->desiredValue - this->smallChange, true);
} }
} else if (y < _thumbRect.y()) { } else if (y < this->thumbRect.y()) {
if (_mouseDownIndex == 1) { if (this->mouseDownIndex == 1) {
setDesiredValue(_desiredValue - _smallChange, true); setDesiredValue(this->desiredValue - this->smallChange, true);
} }
} else if (_thumbRect.contains(2, y)) { } else if (this->thumbRect.contains(2, y)) {
// do nothing // do nothing
} else if (y < height() - _buttonHeight) { } else if (y < height() - this->buttonHeight) {
if (_mouseDownIndex == 3) { if (this->mouseDownIndex == 3) {
setDesiredValue(_desiredValue + _smallChange, true); setDesiredValue(this->desiredValue + this->smallChange, true);
} }
} else { } else {
if (_mouseDownIndex == 4) { if (this->mouseDownIndex == 4) {
setDesiredValue(_desiredValue + _smallChange, true); setDesiredValue(this->desiredValue + this->smallChange, true);
} }
} }
_mouseDownIndex = -1; this->mouseDownIndex = -1;
update(); update();
} }
void ScrollBar::leaveEvent(QEvent *) void ScrollBar::leaveEvent(QEvent *)
{ {
_mouseOverIndex = -1; this->mouseOverIndex = -1;
update(); update();
} }
void ScrollBar::updateScroll() void ScrollBar::updateScroll()
{ {
_trackHeight = height() - _buttonHeight - _buttonHeight - MIN_THUMB_HEIGHT - 1; this->trackHeight = height() - this->buttonHeight - this->buttonHeight - MIN_THUMB_HEIGHT - 1;
_thumbRect = QRect(0, (int)(_currentValue / _maximum * _trackHeight) + 1 + _buttonHeight, this->thumbRect = QRect(
width(), (int)(_largeChange / _maximum * _trackHeight) + MIN_THUMB_HEIGHT); 0, (int)(this->currentValue / this->maximum * this->trackHeight) + 1 + this->buttonHeight,
width(), (int)(this->largeChange / this->maximum * this->trackHeight) + MIN_THUMB_HEIGHT);
update(); update();
} }
+19 -19
View File
@@ -28,7 +28,7 @@ public:
void removeHighlightsWhere(std::function<bool(ScrollBarHighlight &)> func); void removeHighlightsWhere(std::function<bool(ScrollBarHighlight &)> func);
void addHighlight(ScrollBarHighlight *highlight); void addHighlight(ScrollBarHighlight *highlight);
Q_PROPERTY(qreal _desiredValue READ getDesiredValue WRITE setDesiredValue) Q_PROPERTY(qreal desiredValue READ getDesiredValue WRITE setDesiredValue)
void scrollToBottom(); void scrollToBottom();
@@ -53,13 +53,13 @@ public:
void printCurrentState(const QString &prefix = QString()) const; void printCurrentState(const QString &prefix = QString()) const;
private: private:
Q_PROPERTY(qreal _currentValue READ getCurrentValue WRITE setCurrentValue) Q_PROPERTY(qreal currentValue READ getCurrentValue WRITE setCurrentValue)
QMutex _mutex; QMutex mutex;
QPropertyAnimation _currentValueAnimation; QPropertyAnimation currentValueAnimation;
ScrollBarHighlight *_highlights; ScrollBarHighlight *highlights;
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *);
void mouseMoveEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event);
@@ -69,24 +69,24 @@ private:
bool atBottom = false; bool atBottom = false;
int _mouseOverIndex = -1; int mouseOverIndex = -1;
int _mouseDownIndex = -1; int mouseDownIndex = -1;
QPoint _lastMousePosition; QPoint lastMousePosition;
int _buttonHeight = 16; int buttonHeight = 16;
int _trackHeight = 100; int trackHeight = 100;
QRect _thumbRect; QRect thumbRect;
qreal _maximum = 0; qreal maximum = 0;
qreal _minimum = 0; qreal minimum = 0;
qreal _largeChange = 0; qreal largeChange = 0;
qreal _smallChange = 5; qreal smallChange = 5;
qreal _desiredValue = 0; qreal desiredValue = 0;
qreal _currentValue = 0; qreal currentValue = 0;
qreal _smoothScrollingOffset = 0; qreal smoothScrollingOffset = 0;
boost::signals2::signal<void()> _currentValueChanged; boost::signals2::signal<void()> currentValueChanged;
pajlada::Settings::Setting<bool> &smoothScrollingSetting; pajlada::Settings::Setting<bool> &smoothScrollingSetting;