qt-material-widgets/components/qtmaterialscrollbar.cpp

216 lines
4.0 KiB
C++
Raw Normal View History

2017-09-29 17:59:35 +00:00
#include "qtmaterialscrollbar.h"
#include "qtmaterialscrollbar_p.h"
#include <QPainter>
2020-08-03 14:16:38 +00:00
#include <QPainterPath>
#include <QDebug>
2017-09-29 17:59:35 +00:00
#include "qtmaterialscrollbar_internal.h"
#include "lib/qtmaterialstyle.h"
2022-02-17 02:46:48 +00:00
namespace md
{
2017-09-29 17:59:35 +00:00
/*!
* \class QtMaterialScrollBarPrivate
* \internal
*/
2022-02-17 02:46:48 +00:00
ScrollBarPrivate::ScrollBarPrivate(ScrollBar *q)
2017-09-29 17:59:35 +00:00
: q_ptr(q)
{
}
2022-02-17 02:46:48 +00:00
ScrollBarPrivate::~ScrollBarPrivate()
2017-09-29 17:59:35 +00:00
{
}
2022-02-17 02:46:48 +00:00
void ScrollBarPrivate::init()
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_Q(ScrollBar);
2017-09-29 17:59:35 +00:00
2022-02-17 02:46:48 +00:00
stateMachine = new ScrollBarStateMachine(q);
2017-09-29 17:59:35 +00:00
hideOnMouseOut = true;
useThemeColors = true;
q->setMouseTracking(true);
2022-02-17 02:46:48 +00:00
q->setStyle(&Style::instance());
2017-09-29 17:59:35 +00:00
q->setStyleSheet("QScrollBar:vertical { margin: 0; }"
"QScrollBar::add-line:vertical { height: 0; margin: 0; }"
"QScrollBar::sub-line:vertical { height: 0; margin: 0; }");
stateMachine->start();
}
/*!
* \class QtMaterialScrollBar
*/
2022-02-17 02:46:48 +00:00
ScrollBar::ScrollBar(QWidget *parent)
2017-09-29 17:59:35 +00:00
: QScrollBar(parent),
2022-02-17 02:46:48 +00:00
d_ptr(new ScrollBarPrivate(this))
2017-09-29 17:59:35 +00:00
{
d_func()->init();
}
2022-02-17 02:46:48 +00:00
ScrollBar::~ScrollBar()
2017-09-29 17:59:35 +00:00
{
}
/*!
* \reimp
*/
2022-02-17 02:46:48 +00:00
QSize ScrollBar::sizeHint() const
2017-09-29 17:59:35 +00:00
{
if (Qt::Horizontal == orientation()) {
return QSize(1, 10);
} else {
return QSize(10, 1);
}
}
2022-02-17 02:46:48 +00:00
void ScrollBar::setUseThemeColors(bool value)
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(ScrollBar);
2017-09-29 17:59:35 +00:00
if (d->useThemeColors == value) {
return;
}
d->useThemeColors = value;
update();
}
2022-02-17 02:46:48 +00:00
bool ScrollBar::useThemeColors() const
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(const ScrollBar);
2017-09-29 17:59:35 +00:00
return d->useThemeColors;
}
2022-02-17 02:46:48 +00:00
void ScrollBar::setCanvasColor(const QColor &color)
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(ScrollBar);
2017-09-29 17:59:35 +00:00
d->canvasColor = color;
2017-09-29 18:09:22 +00:00
MATERIAL_DISABLE_THEME_COLORS
update();
2017-09-29 17:59:35 +00:00
}
2022-02-17 02:46:48 +00:00
QColor ScrollBar::canvasColor() const
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(const ScrollBar);
2017-09-29 17:59:35 +00:00
if (d->useThemeColors || !d->canvasColor.isValid()) {
return parentWidget()->palette().color(backgroundRole());
} else {
return d->canvasColor;
}
}
2022-02-17 02:46:48 +00:00
void ScrollBar::setBackgroundColor(const QColor &color)
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(ScrollBar);
2017-09-29 17:59:35 +00:00
d->backgroundColor = color;
2017-09-29 18:09:22 +00:00
MATERIAL_DISABLE_THEME_COLORS
update();
2017-09-29 17:59:35 +00:00
}
2022-02-17 02:46:48 +00:00
QColor ScrollBar::backgroundColor() const
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(const ScrollBar);
2017-09-29 17:59:35 +00:00
if (d->useThemeColors || !d->backgroundColor.isValid()) {
2022-02-17 02:46:48 +00:00
return Style::instance().themeColor("border");
2017-09-29 17:59:35 +00:00
} else {
return d->backgroundColor;
}
}
2022-02-17 02:46:48 +00:00
void ScrollBar::setSliderColor(const QColor &color)
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(ScrollBar);
2017-09-29 17:59:35 +00:00
d->sliderColor = color;
2017-09-29 18:09:22 +00:00
MATERIAL_DISABLE_THEME_COLORS
update();
2017-09-29 17:59:35 +00:00
}
2022-02-17 02:46:48 +00:00
QColor ScrollBar::sliderColor() const
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(const ScrollBar);
2017-09-29 17:59:35 +00:00
if (d->useThemeColors || !d->sliderColor.isValid()) {
2022-02-17 02:46:48 +00:00
return Style::instance().themeColor("primary1");
2017-09-29 17:59:35 +00:00
} else {
return d->sliderColor;
}
}
2022-02-17 02:46:48 +00:00
void ScrollBar::setHideOnMouseOut(bool value)
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(ScrollBar);
2017-09-29 17:59:35 +00:00
d->hideOnMouseOut = value;
update();
}
2022-02-17 02:46:48 +00:00
bool ScrollBar::hideOnMouseOut() const
2017-09-29 17:59:35 +00:00
{
2022-02-17 02:46:48 +00:00
Q_D(const ScrollBar);
2017-09-29 17:59:35 +00:00
return d->hideOnMouseOut;
}
/*!
* \reimp
*/
2022-02-17 02:46:48 +00:00
void ScrollBar::paintEvent(QPaintEvent *event)
2017-09-29 17:59:35 +00:00
{
Q_UNUSED(event)
2022-02-17 02:46:48 +00:00
Q_D(ScrollBar);
2017-09-29 17:59:35 +00:00
QPainter painter(this);
2017-10-04 09:40:30 +00:00
painter.setRenderHint(QPainter::Antialiasing);
2017-09-29 17:59:35 +00:00
painter.fillRect(rect(), canvasColor());
int x, y, w, h;
rect().getRect(&x, &y, &w, &h);
QMargins margins(2, 2, 2, 2);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(backgroundColor());
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
if (d->hideOnMouseOut) {
painter.setOpacity(d->stateMachine->opacity());
}
2017-10-04 09:40:30 +00:00
QRect trimmed(rect().marginsRemoved(margins));
QPainterPath path;
path.addRoundedRect(trimmed, 3, 3);
painter.setClipPath(path);
painter.drawRect(trimmed);
2017-09-29 17:59:35 +00:00
2017-10-30 17:17:41 +00:00
const qreal q = (Qt::Horizontal == orientation() ? w : h) /
static_cast<qreal>(maximum()-minimum()+pageStep()-1);
2017-09-29 17:59:35 +00:00
QRect handle = Qt::Horizontal == orientation()
? QRect(sliderPosition()*q, y, pageStep()*q, h)
: QRect(x, sliderPosition()*q, w, pageStep()*q);
brush.setColor(sliderColor());
painter.setBrush(brush);
painter.drawRoundedRect(handle, 9, 9);
2017-09-29 17:59:35 +00:00
}
2022-02-17 02:46:48 +00:00
}