Krita Source Code Documentation
Loading...
Searching...
No Matches
KisZoomableScrollBar Class Reference

#include <kis_zoom_scrollbar.h>

+ Inheritance diagram for KisZoomableScrollBar:

Signals

void overscroll (qreal delta)
 
void zoom (qreal delta)
 

Public Member Functions

QPoint barPosition ()
 
bool catchTeleports (QMouseEvent *event)
 
void handleScroll (const QPoint &accel)
 
void handleWrap (const QPoint &accel, const QPoint &globalMouseCoord)
 
 KisZoomableScrollBar (Qt::Orientation orientation, QWidget *parent=0)
 
 KisZoomableScrollBar (QWidget *parent=0)
 
virtual void mouseMoveEvent (QMouseEvent *event) override
 
virtual void mousePressEvent (QMouseEvent *event) override
 
virtual void mouseReleaseEvent (QMouseEvent *event) override
 
void setWheelOverscrollSensitivity (float sensitivity)
 
void setZoomDeadzone (float value)
 
void tabletEvent (QTabletEvent *event) override
 
virtual void wheelEvent (QWheelEvent *event) override
 
 ~KisZoomableScrollBar ()
 

Private Attributes

QVector2D accelerationAccumulator
 
bool catchTeleportCorrection = false
 
QPoint initialPositionRelativeToBar
 
QPoint lastKnownPosition
 
qreal scrollSubPixelAccumulator
 
qreal wheelOverscrollSensitivity
 
bool zoomEnabled = true
 
qreal zoomThreshold
 

Detailed Description

Definition at line 17 of file kis_zoom_scrollbar.h.

Constructor & Destructor Documentation

◆ KisZoomableScrollBar() [1/2]

KisZoomableScrollBar::KisZoomableScrollBar ( QWidget * parent = 0)

Definition at line 16 of file kis_zoom_scrollbar.cpp.

17 : QScrollBar(parent)
21 , zoomThreshold(0.75f)
24{
25 KisConfig config(true);
26 zoomEnabled = config.scrollbarZoomEnabled();
27}

References KisConfig::scrollbarZoomEnabled(), and zoomEnabled.

◆ KisZoomableScrollBar() [2/2]

KisZoomableScrollBar::KisZoomableScrollBar ( Qt::Orientation orientation,
QWidget * parent = 0 )

Definition at line 29 of file kis_zoom_scrollbar.cpp.

30 : KisZoomableScrollBar(parent)
31{
32 setOrientation(orientation);
33}
KisZoomableScrollBar(QWidget *parent=0)

◆ ~KisZoomableScrollBar()

KisZoomableScrollBar::~KisZoomableScrollBar ( )

Definition at line 35 of file kis_zoom_scrollbar.cpp.

36{
37}

Member Function Documentation

◆ barPosition()

QPoint KisZoomableScrollBar::barPosition ( )

Definition at line 39 of file kis_zoom_scrollbar.cpp.

40{
41 float barPositionNormalized = (float)(value() - minimum()) / (float)(maximum() + pageStep() - minimum());
42 QPoint barPosition = orientation() == Qt::Horizontal ?
43 QPoint(barPositionNormalized * width() * devicePixelRatio(), 0) :
44 QPoint(0, barPositionNormalized * height() * devicePixelRatio());
45
46 return mapToGlobal(QPoint(0,0)) + barPosition;
47}
float value(const T *src, size_t ch)

References barPosition(), and value().

◆ catchTeleports()

bool KisZoomableScrollBar::catchTeleports ( QMouseEvent * event)

Definition at line 49 of file kis_zoom_scrollbar.cpp.

49 {
52 event->accept();
53 return true;
54 }
55
56 return false;
57}

References catchTeleportCorrection.

◆ handleScroll()

void KisZoomableScrollBar::handleScroll ( const QPoint & accel)

Definition at line 95 of file kis_zoom_scrollbar.cpp.

96{
97 const qreal sliderMovementPix = (orientation() == Qt::Horizontal) ? accel.x() * devicePixelRatio() : accel.y() * devicePixelRatio();
98 const qreal zoomMovementPix = (orientation() == Qt::Horizontal) ? -accel.y() : -accel.x();
99 const qreal documentLength = maximum() - minimum() + pageStep();
100 const qreal widgetLength = (orientation() == Qt::Horizontal) ? width() * devicePixelRatio() : height() * devicePixelRatio();
101 const qreal widgetThickness = (orientation() == Qt::Horizontal) ? height() * devicePixelRatio() : width() * devicePixelRatio();
102
103 const QVector2D perpendicularDirection = (orientation() == Qt::Horizontal) ? QVector2D(0, 1) : QVector2D(1, 0);
104 const float perpendicularity = QVector2D::dotProduct(perpendicularDirection.normalized(), accelerationAccumulator.normalized());
105
106 if (zoomEnabled && qAbs(perpendicularity) > zoomThreshold && zoomMovementPix != 0) {
107 zoom(qreal(zoomMovementPix) / qreal(widgetThickness * 2));
108 } else if (sliderMovementPix != 0) {
109 const int currentPosition = sliderPosition();
110 scrollSubPixelAccumulator += (documentLength) * (sliderMovementPix / widgetLength);
111
112 setSliderPosition(currentPosition + scrollSubPixelAccumulator);
113 if (currentPosition + scrollSubPixelAccumulator > maximum() ||
114 currentPosition + scrollSubPixelAccumulator < minimum()) {
116 }
117
120 }
121}
void overscroll(qreal delta)
void zoom(qreal delta)
Point abs(const Point &pt)

References accelerationAccumulator, overscroll(), scrollSubPixelAccumulator, sign(), zoom(), zoomEnabled, and zoomThreshold.

◆ handleWrap()

void KisZoomableScrollBar::handleWrap ( const QPoint & accel,
const QPoint & globalMouseCoord )

Definition at line 59 of file kis_zoom_scrollbar.cpp.

60{
61 QRect windowRect = window()->geometry();
62 windowRect = kisGrowRect(windowRect, -2);
63 const int windowWidth = windowRect.width();
64 const int windowHeight = windowRect.height();
65 const int windowX = windowRect.x();
66 const int windowY = windowRect.y();
67 const bool xWrap = true;
68 const bool yWrap = true;
69
70 if (!windowRect.contains(mouseCoord)) {
71 int x = mouseCoord.x();
72 int y = mouseCoord.y();
73
74 if (x < windowX && xWrap ) {
75 x += (windowWidth - 2);
76 } else if (x > (windowX + windowWidth) && xWrap ) {
77 x -= (windowWidth - 2);
78 }
79
80 if (y < windowY && yWrap) {
81 y += (windowHeight - 2);
82 } else if (y > (windowY + windowHeight) && yWrap) {
83 y -= (windowHeight - 2);
84 }
85
86 KisToolUtils::setCursorPos(QPoint(x, y));
87 lastKnownPosition = QPoint(x, y) - accel;
88
89 //Important -- teleportation needs to caught to prevent high-acceleration
90 //values from QCursor::setPos being read in this event.
92 }
93}
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186
void KRITAUI_EXPORT setCursorPos(const QPoint &point)

References catchTeleportCorrection, kisGrowRect(), lastKnownPosition, and KisToolUtils::setCursorPos().

◆ mouseMoveEvent()

void KisZoomableScrollBar::mouseMoveEvent ( QMouseEvent * event)
overridevirtual

Definition at line 164 of file kis_zoom_scrollbar.cpp.

165{
166 QPoint globalMouseCoord = mapToGlobal(event->pos());
167
168 QPoint accel = globalMouseCoord - lastKnownPosition;
169 accelerationAccumulator += QVector2D(accel);
170
171 if (catchTeleports(event)) {
172 return;
173 }
174
175 if (accelerationAccumulator.length() > 5) {
177 }
178
179 handleScroll(accel);
180 lastKnownPosition = globalMouseCoord;
181 handleWrap(accel, mapToGlobal(event->pos()));
182 event->accept();
183}
bool catchTeleports(QMouseEvent *event)
void handleWrap(const QPoint &accel, const QPoint &globalMouseCoord)
void handleScroll(const QPoint &accel)

References accelerationAccumulator, catchTeleports(), handleScroll(), handleWrap(), and lastKnownPosition.

◆ mousePressEvent()

void KisZoomableScrollBar::mousePressEvent ( QMouseEvent * event)
overridevirtual

Definition at line 151 of file kis_zoom_scrollbar.cpp.

152{
153 QScrollBar::mousePressEvent(event);
154
155 lastKnownPosition = mapToGlobal(event->pos());
156 accelerationAccumulator = QVector2D(0,0);
157 QPoint worldPosition = mapToGlobal(event->pos());
158 QPoint barPosition = this->barPosition();
160 setCursor(Qt::BlankCursor);
161}

References accelerationAccumulator, barPosition(), initialPositionRelativeToBar, and lastKnownPosition.

◆ mouseReleaseEvent()

void KisZoomableScrollBar::mouseReleaseEvent ( QMouseEvent * event)
overridevirtual

Definition at line 185 of file kis_zoom_scrollbar.cpp.

186{
187 //If there's nowhere for our slider to go, we should
188 //still Q_EMIT the slider release value.
189 if (maximum() == minimum()) {
190 Q_EMIT sliderReleased();
191 }
192 const QPoint maximumCoordinates = mapToGlobal(QPoint(width() * devicePixelRatio(), height() * devicePixelRatio()));
193 const QPoint minimumCoordinates = mapToGlobal(QPoint(0,0));
194 const QPoint desiredCoordinates = barPosition() + initialPositionRelativeToBar;
195 QPoint cursorPosition = QPoint(
196 qMax(minimumCoordinates.x(), qMin(maximumCoordinates.x(), desiredCoordinates.x())),
197 qMax(minimumCoordinates.y(), qMin(maximumCoordinates.y(), desiredCoordinates.y()))
198 );
199 KisToolUtils::setCursorPos(cursorPosition);
200 setCursor(Qt::ArrowCursor);
201 QScrollBar::mouseReleaseEvent(event);
202}

References barPosition(), initialPositionRelativeToBar, and KisToolUtils::setCursorPos().

◆ overscroll

void KisZoomableScrollBar::overscroll ( qreal delta)
signal

◆ setWheelOverscrollSensitivity()

void KisZoomableScrollBar::setWheelOverscrollSensitivity ( float sensitivity)

Definition at line 220 of file kis_zoom_scrollbar.cpp.

221{
222 wheelOverscrollSensitivity = sensitivity;
223}

References wheelOverscrollSensitivity.

◆ setZoomDeadzone()

void KisZoomableScrollBar::setZoomDeadzone ( float value)

Definition at line 215 of file kis_zoom_scrollbar.cpp.

216{
218}

References value(), and zoomThreshold.

◆ tabletEvent()

void KisZoomableScrollBar::tabletEvent ( QTabletEvent * event)
override

Definition at line 123 of file kis_zoom_scrollbar.cpp.

123 {
124 if ( event->type() == QTabletEvent::TabletMove && isSliderDown() ) {
125 QPoint globalMouseCoord = mapToGlobal(event->pos());
126 QPoint accel = globalMouseCoord - lastKnownPosition;
127 accelerationAccumulator += QVector2D(accel);
128
129 if( accelerationAccumulator.length() > 5) {
131 }
132
133 handleScroll(accel);
134 lastKnownPosition = globalMouseCoord;
135 event->accept();
136 } else {
137 if (event->type() == QTabletEvent::TabletPress) {
138 QPoint globalMouseCoord = mapToGlobal(event->pos());
139 lastKnownPosition = globalMouseCoord;
140 setSliderDown(true);
141 event->accept();
142 } else if(event->type() == QTabletEvent::TabletRelease) {
143 setSliderDown(false);
144 event->accept();
145 } else {
146 QScrollBar::tabletEvent(event);
147 }
148 }
149}

References accelerationAccumulator, handleScroll(), and lastKnownPosition.

◆ wheelEvent()

void KisZoomableScrollBar::wheelEvent ( QWheelEvent * event)
overridevirtual

Definition at line 204 of file kis_zoom_scrollbar.cpp.

204 {
205 const int delta = (event->angleDelta().y() / 8) * singleStep() * -1;
206 const int currentPosition = sliderPosition();
207
208 if (currentPosition + delta > maximum() || currentPosition + delta < minimum()){
210 }
211
212 QScrollBar::wheelEvent(event);
213}

References overscroll(), and wheelOverscrollSensitivity.

◆ zoom

void KisZoomableScrollBar::zoom ( qreal delta)
signal

Member Data Documentation

◆ accelerationAccumulator

QVector2D KisZoomableScrollBar::accelerationAccumulator
private

Definition at line 55 of file kis_zoom_scrollbar.h.

◆ catchTeleportCorrection

bool KisZoomableScrollBar::catchTeleportCorrection = false
private

Definition at line 59 of file kis_zoom_scrollbar.h.

◆ initialPositionRelativeToBar

QPoint KisZoomableScrollBar::initialPositionRelativeToBar
private

Definition at line 53 of file kis_zoom_scrollbar.h.

◆ lastKnownPosition

QPoint KisZoomableScrollBar::lastKnownPosition
private

Definition at line 54 of file kis_zoom_scrollbar.h.

◆ scrollSubPixelAccumulator

qreal KisZoomableScrollBar::scrollSubPixelAccumulator
private

Definition at line 56 of file kis_zoom_scrollbar.h.

◆ wheelOverscrollSensitivity

qreal KisZoomableScrollBar::wheelOverscrollSensitivity
private

Definition at line 58 of file kis_zoom_scrollbar.h.

◆ zoomEnabled

bool KisZoomableScrollBar::zoomEnabled = true
private

Definition at line 60 of file kis_zoom_scrollbar.h.

◆ zoomThreshold

qreal KisZoomableScrollBar::zoomThreshold
private

Definition at line 57 of file kis_zoom_scrollbar.h.


The documentation for this class was generated from the following files: