nheko/src/ui/ScrollBar.cc

60 lines
1.9 KiB
C++
Raw Normal View History

2017-05-24 21:45:13 +02:00
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ScrollBar.h"
ScrollBar::ScrollBar(QScrollArea *area, QWidget *parent)
2017-08-20 12:47:22 +02:00
: QScrollBar(parent)
2017-11-05 22:04:55 +01:00
, area_{area}
{}
2017-05-24 21:45:13 +02:00
2017-08-20 12:47:22 +02:00
void
ScrollBar::paintEvent(QPaintEvent *)
2017-05-24 21:45:13 +02:00
{
2017-09-10 11:59:21 +02:00
if (!width() && !height()) {
hide();
return;
}
2017-05-24 21:45:13 +02:00
2017-09-10 11:59:21 +02:00
QPainter p(this);
p.setRenderHint(QPainter::TextAntialiasing);
p.setRenderHint(QPainter::Antialiasing);
p.setRenderHint(QPainter::SmoothPixmapTransform);
2017-05-24 21:45:13 +02:00
2017-09-10 11:59:21 +02:00
p.setPen(Qt::NoPen);
2017-05-24 21:45:13 +02:00
2017-12-05 13:13:26 +01:00
p.setBrush(backgroundColor());
2017-09-10 11:59:21 +02:00
QRect backgroundArea(Padding, 0, handleWidth_, height());
p.drawRoundedRect(backgroundArea, roundRadius_, roundRadius_);
2017-05-24 21:45:13 +02:00
2017-09-10 11:59:21 +02:00
int areaHeight = area_->height();
int widgetHeight = area_->widget()->height();
2017-05-24 21:45:13 +02:00
2017-09-10 11:59:21 +02:00
double visiblePercentage = (double)areaHeight / (double)widgetHeight;
int handleHeight = std::max(visiblePercentage * areaHeight, (double)minHandleHeight_);
2017-05-24 21:45:13 +02:00
2017-09-10 11:59:21 +02:00
if (maximum() == 0) {
return;
}
2017-05-24 21:45:13 +02:00
2017-09-10 11:59:21 +02:00
int handle_y = (value() * (areaHeight - handleHeight - roundRadius_ / 2)) / maximum();
2017-05-24 21:45:13 +02:00
2017-12-05 13:13:26 +01:00
p.setBrush(handleColor());
2017-09-10 11:59:21 +02:00
QRect handleArea(Padding, handle_y, handleWidth_, handleHeight);
p.drawRoundedRect(handleArea, roundRadius_, roundRadius_);
2017-05-24 21:45:13 +02:00
}