Changeset View
Changeset View
Standalone View
Standalone View
src/ImageViewer.cpp
Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Line(s) | |||||
82 | // ------------------------------------------------------------------------ | 82 | // ------------------------------------------------------------------------ | ||
83 | void ImageViewer::setQImage(QImage *img) | 83 | void ImageViewer::setQImage(QImage *img) | ||
84 | { | 84 | { | ||
85 | if (img == nullptr) { | 85 | if (img == nullptr) { | ||
86 | return; | 86 | return; | ||
87 | } | 87 | } | ||
88 | 88 | | |||
89 | d->img = img; | 89 | d->img = img; | ||
90 | d->scene->setSceneRect(0, 0, img->width(), img->height()); | 90 | const auto dpr = devicePixelRatioF(); | ||
91 | d->img->setDevicePixelRatio(dpr); | ||||
92 | d->scene->setSceneRect(0, 0, img->width() / dpr, img->height() / dpr); | ||||
91 | } | 93 | } | ||
92 | 94 | | |||
93 | // ------------------------------------------------------------------------ | 95 | // ------------------------------------------------------------------------ | ||
94 | void ImageViewer::drawBackground(QPainter *painter, const QRectF &rect) | 96 | void ImageViewer::drawBackground(QPainter *painter, const QRectF &rect) | ||
95 | { | 97 | { | ||
96 | painter->fillRect(rect, QColor(0x70, 0x70, 0x70)); | 98 | painter->fillRect(rect, QColor(0x70, 0x70, 0x70)); | ||
97 | painter->drawImage(rect, *d->img, rect); | 99 | QRectF r = rect & sceneRect(); | ||
100 | const qreal dpr = d->img->devicePixelRatio(); | ||||
101 | QRectF srcRect = QRectF(r.topLeft() * dpr, r.size() * dpr); | ||||
102 | painter->drawImage(r, *d->img, srcRect); | ||||
98 | } | 103 | } | ||
99 | 104 | | |||
100 | // ------------------------------------------------------------------------ | 105 | // ------------------------------------------------------------------------ | ||
101 | void ImageViewer::zoomIn() | 106 | void ImageViewer::zoomIn() | ||
102 | { | 107 | { | ||
103 | scale(1.5, 1.5); | 108 | scale(1.5, 1.5); | ||
104 | } | 109 | } | ||
105 | 110 | | |||
106 | // ------------------------------------------------------------------------ | 111 | // ------------------------------------------------------------------------ | ||
107 | void ImageViewer::zoomOut() | 112 | void ImageViewer::zoomOut() | ||
108 | { | 113 | { | ||
109 | scale(1.0 / 1.5, 1.0 / 1.5); | 114 | scale(1.0 / 1.5, 1.0 / 1.5); | ||
110 | } | 115 | } | ||
111 | 116 | | |||
112 | // ------------------------------------------------------------------------ | 117 | // ------------------------------------------------------------------------ | ||
113 | void ImageViewer::zoomActualSize() | 118 | void ImageViewer::zoomActualSize() | ||
114 | { | 119 | { | ||
115 | setMatrix(QMatrix()); | 120 | setMatrix(QMatrix()); | ||
116 | } | 121 | } | ||
117 | 122 | | |||
118 | // ------------------------------------------------------------------------ | 123 | // ------------------------------------------------------------------------ | ||
119 | void ImageViewer::zoom2Fit() | 124 | void ImageViewer::zoom2Fit() | ||
120 | { | 125 | { | ||
121 | fitInView(d->img->rect(), Qt::KeepAspectRatio); | 126 | QRectF r = d->img->rect(); | ||
127 | const auto dpr = d->img->devicePixelRatio(); | ||||
128 | r = QRectF(r.topLeft() / dpr, r.size() / dpr); | ||||
129 | fitInView(r, Qt::KeepAspectRatio); | ||||
122 | } | 130 | } | ||
123 | 131 | | |||
124 | // ------------------------------------------------------------------------ | 132 | // ------------------------------------------------------------------------ | ||
125 | void ImageViewer::wheelEvent(QWheelEvent *e) | 133 | void ImageViewer::wheelEvent(QWheelEvent *e) | ||
126 | { | 134 | { | ||
127 | if (e->modifiers() == Qt::ControlModifier) { | 135 | if (e->modifiers() == Qt::ControlModifier) { | ||
128 | if (e->delta() > 0) { | 136 | if (e->delta() > 0) { | ||
129 | zoomIn(); | 137 | zoomIn(); | ||
Show All 10 Lines |