Skip to content

Commit 063e8e9

Browse files
Add scrolling with keyboard arrow keys
1 parent 2313119 commit 063e8e9

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/ImageViewer/src/GUI/ImageViewerWidget.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <QGraphicsScene>
2323
#include <QGraphicsItem>
24+
#include <QScrollBar>
2425
#include <QGraphicsPixmapItem>
2526
#include <QStyleOptionGraphicsItem>
2627
#include <QMatrix>
@@ -41,8 +42,11 @@ class QPinchGesture {};
4142

4243
namespace {
4344

45+
const int SCROLL_STEP = 10;
46+
const qreal ZOOM_STEP = 1.1;
4447
const qreal ZOOM_CHANGE_EPSILON = 1e-5;
4548
const int ZOOM_FIT_SIZE_CORRECTION = 1;
49+
const qreal ROTATION_TRESHOLD = 30;
4650

4751
} // namespace
4852

@@ -145,20 +149,20 @@ struct ImageViewerWidget::Impl
145149
if(changeFlags.testFlag(QPinchGesture::RotationAngleChanged))
146150
{
147151
const qreal rotationAngle = gesture->rotationAngle();
148-
const qreal rotationTreshhold = 30;
149-
if(rotationAngle > rotationTreshhold)
152+
if(rotationAngle > ROTATION_TRESHOLD)
150153
{
151154
imageViewerWidget->rotateClockwise();
152155
gesture->setRotationAngle(0);
153156
}
154-
if(rotationAngle < -rotationTreshhold)
157+
if(rotationAngle < -ROTATION_TRESHOLD)
155158
{
156159
imageViewerWidget->rotateCounterclockwise();
157160
gesture->setRotationAngle(0);
158161
}
159162
}
160163
#else
161164
Q_UNUSED(gesture);
165+
Q_UNUSED(ROTATION_TRESHOLD);
162166
#endif
163167
}
164168

@@ -317,12 +321,12 @@ void ImageViewerWidget::flipVertical()
317321

318322
void ImageViewerWidget::zoomIn()
319323
{
320-
setZoomLevel(m_impl->currentZoomLevel * 1.1);
324+
setZoomLevel(m_impl->currentZoomLevel * ZOOM_STEP);
321325
}
322326

323327
void ImageViewerWidget::zoomOut()
324328
{
325-
setZoomLevel(m_impl->currentZoomLevel / 1.1);
329+
setZoomLevel(m_impl->currentZoomLevel / ZOOM_STEP);
326330
}
327331

328332
void ImageViewerWidget::resetZoom()
@@ -331,6 +335,30 @@ void ImageViewerWidget::resetZoom()
331335
m_impl->updateTransformations();
332336
}
333337

338+
void ImageViewerWidget::scrollLeft()
339+
{
340+
QScrollBar* scrollBar = horizontalScrollBar();
341+
scrollBar->setValue(scrollBar->value() - SCROLL_STEP);
342+
}
343+
344+
void ImageViewerWidget::scrollRight()
345+
{
346+
QScrollBar* scrollBar = horizontalScrollBar();
347+
scrollBar->setValue(scrollBar->value() + SCROLL_STEP);
348+
}
349+
350+
void ImageViewerWidget::scrollUp()
351+
{
352+
QScrollBar* scrollBar = verticalScrollBar();
353+
scrollBar->setValue(scrollBar->value() - SCROLL_STEP);
354+
}
355+
356+
void ImageViewerWidget::scrollDown()
357+
{
358+
QScrollBar* scrollBar = verticalScrollBar();
359+
scrollBar->setValue(scrollBar->value() + SCROLL_STEP);
360+
}
361+
334362
void ImageViewerWidget::setBackgroundColor(const QColor &color)
335363
{
336364
setBackgroundBrush(QBrush(color));

src/ImageViewer/src/GUI/ImageViewerWidget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public slots:
8080
void zoomOut();
8181
void resetZoom();
8282

83+
void scrollLeft();
84+
void scrollRight();
85+
void scrollUp();
86+
void scrollDown();
87+
8388
void setBackgroundColor(const QColor &color);
8489

8590
void setSmoothTransformation(bool enabled);

src/ImageViewer/src/GUI/MainWindow.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,18 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
789789
if(m_impl->isFullScreenMode)
790790
switchFullScreenMode();
791791
break;
792+
case Qt::Key_Up:
793+
m_ui->imageViewerWidget->scrollUp();
794+
break;
795+
case Qt::Key_Down:
796+
m_ui->imageViewerWidget->scrollDown();
797+
break;
798+
case Qt::Key_Left:
799+
m_ui->imageViewerWidget->scrollLeft();
800+
break;
801+
case Qt::Key_Right:
802+
m_ui->imageViewerWidget->scrollRight();
803+
break;
792804
default:
793805
break;
794806
}

0 commit comments

Comments
 (0)