Changeset View
Changeset View
Standalone View
Standalone View
lib/documentview/abstractimageview.cpp
Show All 24 Lines | |||||
25 | 25 | | |||
26 | // KDE | 26 | // KDE | ||
27 | 27 | | |||
28 | // Qt | 28 | // Qt | ||
29 | #include <QDebug> | 29 | #include <QDebug> | ||
30 | #include <QCursor> | 30 | #include <QCursor> | ||
31 | #include <QGraphicsSceneMouseEvent> | 31 | #include <QGraphicsSceneMouseEvent> | ||
32 | #include <QStandardPaths> | 32 | #include <QStandardPaths> | ||
33 | #include <QPainter> | ||||
33 | 34 | | |||
34 | namespace Gwenview | 35 | namespace Gwenview | ||
35 | { | 36 | { | ||
36 | 37 | | |||
37 | static const int UNIT_STEP = 16; | 38 | static const int UNIT_STEP = 16; | ||
38 | 39 | | |||
39 | struct AbstractImageViewPrivate | 40 | struct AbstractImageViewPrivate | ||
40 | { | 41 | { | ||
Show All 10 Lines | |||||
51 | 52 | | |||
52 | qreal mZoom; | 53 | qreal mZoom; | ||
53 | bool mZoomToFit; | 54 | bool mZoomToFit; | ||
54 | bool mZoomToFill; | 55 | bool mZoomToFill; | ||
55 | QPointF mImageOffset; | 56 | QPointF mImageOffset; | ||
56 | QPointF mScrollPos; | 57 | QPointF mScrollPos; | ||
57 | QPointF mLastDragPos; | 58 | QPointF mLastDragPos; | ||
58 | 59 | | |||
60 | const QPixmap mAlphaBackgroundTexture = createAlphaBackgroundTexture(); | ||||
61 | | ||||
59 | void adjustImageOffset(Verbosity verbosity = Notify) | 62 | void adjustImageOffset(Verbosity verbosity = Notify) | ||
60 | { | 63 | { | ||
61 | QSizeF zoomedDocSize = q->documentSize() * mZoom; | 64 | QSizeF zoomedDocSize = q->documentSize() * mZoom; | ||
62 | QSizeF viewSize = q->boundingRect().size(); | 65 | QSizeF viewSize = q->boundingRect().size(); | ||
63 | QPointF offset( | 66 | QPointF offset( | ||
64 | qMax((viewSize.width() - zoomedDocSize.width()) / 2, qreal(0.)), | 67 | qMax((viewSize.width() - zoomedDocSize.width()) / 2, qreal(0.)), | ||
65 | qMax((viewSize.height() - zoomedDocSize.height()) / 2, qreal(0.)) | 68 | qMax((viewSize.height() - zoomedDocSize.height()) / 2, qreal(0.)) | ||
66 | ); | 69 | ); | ||
Show All 37 Lines | |||||
104 | void setupZoomCursor() | 107 | void setupZoomCursor() | ||
105 | { | 108 | { | ||
106 | // We do not use "appdata" here because that does not work when this | 109 | // We do not use "appdata" here because that does not work when this | ||
107 | // code is called from a KPart. | 110 | // code is called from a KPart. | ||
108 | const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("gwenview/cursors/zoom.png")); | 111 | const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("gwenview/cursors/zoom.png")); | ||
109 | QPixmap cursorPixmap = QPixmap(path); | 112 | QPixmap cursorPixmap = QPixmap(path); | ||
110 | mZoomCursor = QCursor(cursorPixmap, 11, 11); | 113 | mZoomCursor = QCursor(cursorPixmap, 11, 11); | ||
111 | } | 114 | } | ||
115 | | ||||
116 | QPixmap createAlphaBackgroundTexture() | ||||
117 | { | ||||
118 | QPixmap pix = QPixmap(32, 32); | ||||
119 | QPainter painter(&pix); | ||||
120 | painter.fillRect(pix.rect(), QColor(128, 128, 128)); | ||||
121 | const QColor light = QColor(192, 192, 192); | ||||
122 | painter.fillRect(0, 0, 16, 16, light); | ||||
123 | painter.fillRect(16, 16, 16, 16, light); | ||||
124 | return pix; | ||||
125 | } | ||||
112 | }; | 126 | }; | ||
113 | 127 | | |||
114 | AbstractImageView::AbstractImageView(QGraphicsItem* parent) | 128 | AbstractImageView::AbstractImageView(QGraphicsItem* parent) | ||
115 | : QGraphicsWidget(parent) | 129 | : QGraphicsWidget(parent) | ||
116 | , d(new AbstractImageViewPrivate) | 130 | , d(new AbstractImageViewPrivate) | ||
117 | { | 131 | { | ||
118 | d->q = this; | 132 | d->q = this; | ||
119 | d->mControlKeyIsDown = false; | 133 | d->mControlKeyIsDown = false; | ||
120 | d->mEnlargeSmallerImages = false; | 134 | d->mEnlargeSmallerImages = false; | ||
121 | d->mZoom = 1; | 135 | d->mZoom = 1; | ||
122 | d->mZoomToFit = true; | 136 | d->mZoomToFit = true; | ||
123 | d->mZoomToFill = false; | 137 | d->mZoomToFill = false; | ||
124 | d->mImageOffset = QPointF(0, 0); | 138 | d->mImageOffset = QPointF(0, 0); | ||
125 | d->mScrollPos = QPointF(0, 0); | 139 | d->mScrollPos = QPointF(0, 0); | ||
126 | setFocusPolicy(Qt::WheelFocus); | 140 | setFocusPolicy(Qt::WheelFocus); | ||
rkflx: That's probably not needed anymore. | |||||
127 | setFlag(ItemIsSelectable); | 141 | setFlag(ItemIsSelectable); | ||
128 | setAcceptHoverEvents(true); | 142 | setAcceptHoverEvents(true); | ||
129 | d->setupZoomCursor(); | 143 | d->setupZoomCursor(); | ||
130 | updateCursor(); | 144 | updateCursor(); | ||
131 | } | 145 | } | ||
132 | 146 | | |||
133 | AbstractImageView::~AbstractImageView() | 147 | AbstractImageView::~AbstractImageView() | ||
134 | { | 148 | { | ||
▲ Show 20 Lines • Show All 117 Lines • ▼ Show 20 Line(s) | 265 | if (on) { | |||
252 | setZoom(computeZoomToFill()); | 266 | setZoom(computeZoomToFill()); | ||
253 | } | 267 | } | ||
254 | // We do not set zoom to 1 if zoomToFit is off, this is up to the code | 268 | // We do not set zoom to 1 if zoomToFit is off, this is up to the code | ||
255 | // calling us. It may went to zoom to some other level and/or to zoom on | 269 | // calling us. It may went to zoom to some other level and/or to zoom on | ||
256 | // a particular position | 270 | // a particular position | ||
257 | zoomToFillChanged(d->mZoomToFill); | 271 | zoomToFillChanged(d->mZoomToFill); | ||
258 | } | 272 | } | ||
259 | 273 | | |||
274 | const QPixmap& AbstractImageView::alphaBackgroundTexture() const | ||||
rkflx: I believe `const QPixmap&` would avoid a bunch of useless copying. | |||||
It's hard to know when to conform to surrounding code, and when to break that consistency. In hindsight, a QPixmap is going to be more expensive to copy than QRect and the like. so this makes sense. huoni: It's hard to know when to conform to surrounding code, and when to break that consistency. In… | |||||
275 | { | ||||
276 | return d->mAlphaBackgroundTexture; | ||||
277 | } | ||||
278 | | ||||
260 | void AbstractImageView::resizeEvent(QGraphicsSceneResizeEvent* event) | 279 | void AbstractImageView::resizeEvent(QGraphicsSceneResizeEvent* event) | ||
261 | { | 280 | { | ||
262 | QGraphicsWidget::resizeEvent(event); | 281 | QGraphicsWidget::resizeEvent(event); | ||
263 | if (d->mZoomToFit) { | 282 | if (d->mZoomToFit) { | ||
264 | // setZoom() calls adjustImageOffset(), but only if the zoom changes. | 283 | // setZoom() calls adjustImageOffset(), but only if the zoom changes. | ||
265 | // If the view is resized but does not cause a zoom change, we call | 284 | // If the view is resized but does not cause a zoom change, we call | ||
266 | // adjustImageOffset() ourself. | 285 | // adjustImageOffset() ourself. | ||
267 | const qreal newZoom = computeZoomToFit(); | 286 | const qreal newZoom = computeZoomToFit(); | ||
▲ Show 20 Lines • Show All 308 Lines • Show Last 20 Lines |
That's probably not needed anymore.