Changeset View
Changeset View
Standalone View
Standalone View
lib/documentview/abstractimageview.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | 40 | { | |||
---|---|---|---|---|---|
46 | QCursor mZoomCursor; | 46 | QCursor mZoomCursor; | ||
47 | Document::Ptr mDocument; | 47 | Document::Ptr mDocument; | ||
48 | 48 | | |||
49 | bool mControlKeyIsDown; | 49 | bool mControlKeyIsDown; | ||
50 | bool mEnlargeSmallerImages; | 50 | bool mEnlargeSmallerImages; | ||
51 | 51 | | |||
52 | qreal mZoom; | 52 | qreal mZoom; | ||
53 | bool mZoomToFit; | 53 | bool mZoomToFit; | ||
54 | bool mZoomToFitWidth; | 54 | bool mZoomToFill; | ||
55 | QPointF mImageOffset; | 55 | QPointF mImageOffset; | ||
56 | QPointF mScrollPos; | 56 | QPointF mScrollPos; | ||
57 | QPointF mLastDragPos; | 57 | QPointF mLastDragPos; | ||
58 | 58 | | |||
59 | void adjustImageOffset(Verbosity verbosity = Notify) | 59 | void adjustImageOffset(Verbosity verbosity = Notify) | ||
60 | { | 60 | { | ||
61 | QSizeF zoomedDocSize = q->documentSize() * mZoom; | 61 | QSizeF zoomedDocSize = q->documentSize() * mZoom; | ||
62 | QSizeF viewSize = q->boundingRect().size(); | 62 | QSizeF viewSize = q->boundingRect().size(); | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | |||||
115 | : QGraphicsWidget(parent) | 115 | : QGraphicsWidget(parent) | ||
116 | , d(new AbstractImageViewPrivate) | 116 | , d(new AbstractImageViewPrivate) | ||
117 | { | 117 | { | ||
118 | d->q = this; | 118 | d->q = this; | ||
119 | d->mControlKeyIsDown = false; | 119 | d->mControlKeyIsDown = false; | ||
120 | d->mEnlargeSmallerImages = false; | 120 | d->mEnlargeSmallerImages = false; | ||
121 | d->mZoom = 1; | 121 | d->mZoom = 1; | ||
122 | d->mZoomToFit = true; | 122 | d->mZoomToFit = true; | ||
123 | d->mZoomToFitWidth = false; | 123 | d->mZoomToFill = false; | ||
124 | d->mImageOffset = QPointF(0, 0); | 124 | d->mImageOffset = QPointF(0, 0); | ||
125 | d->mScrollPos = QPointF(0, 0); | 125 | d->mScrollPos = QPointF(0, 0); | ||
126 | setFocusPolicy(Qt::WheelFocus); | 126 | setFocusPolicy(Qt::WheelFocus); | ||
127 | setFlag(ItemIsSelectable); | 127 | setFlag(ItemIsSelectable); | ||
128 | setAcceptHoverEvents(true); | 128 | setAcceptHoverEvents(true); | ||
129 | d->setupZoomCursor(); | 129 | d->setupZoomCursor(); | ||
130 | updateCursor(); | 130 | updateCursor(); | ||
131 | } | 131 | } | ||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Line(s) | 166 | { | |||
223 | zoomChanged(d->mZoom); | 223 | zoomChanged(d->mZoom); | ||
224 | } | 224 | } | ||
225 | 225 | | |||
226 | bool AbstractImageView::zoomToFit() const | 226 | bool AbstractImageView::zoomToFit() const | ||
227 | { | 227 | { | ||
228 | return d->mZoomToFit; | 228 | return d->mZoomToFit; | ||
229 | } | 229 | } | ||
230 | 230 | | |||
231 | bool AbstractImageView::zoomToFitWidth() const | 231 | bool AbstractImageView::zoomToFill() const | ||
232 | { | 232 | { | ||
233 | return d->mZoomToFitWidth; | 233 | return d->mZoomToFill; | ||
234 | } | 234 | } | ||
235 | 235 | | |||
236 | void AbstractImageView::setZoomToFit(bool on) | 236 | void AbstractImageView::setZoomToFit(bool on) | ||
237 | { | 237 | { | ||
238 | d->mZoomToFit = on; | 238 | d->mZoomToFit = on; | ||
239 | if (on) { | 239 | if (on) { | ||
240 | setZoom(computeZoomToFit()); | 240 | setZoom(computeZoomToFit()); | ||
241 | } | 241 | } | ||
242 | // We do not set zoom to 1 if zoomToFit is off, this is up to the code | 242 | // We do not set zoom to 1 if zoomToFit is off, this is up to the code | ||
243 | // calling us. It may went to zoom to some other level and/or to zoom on | 243 | // calling us. It may went to zoom to some other level and/or to zoom on | ||
244 | // a particular position | 244 | // a particular position | ||
245 | zoomToFitChanged(d->mZoomToFit); | 245 | zoomToFitChanged(d->mZoomToFit); | ||
246 | } | 246 | } | ||
247 | 247 | | |||
248 | void AbstractImageView::setZoomToFitWidth(bool on) | 248 | void AbstractImageView::setZoomToFill(bool on) | ||
249 | { | 249 | { | ||
250 | d->mZoomToFitWidth = on; | 250 | d->mZoomToFill = on; | ||
251 | if (on) { | 251 | if (on) { | ||
252 | setZoom(computeZoomToFitWidth()); | 252 | setZoom(computeZoomToFill()); | ||
253 | } | 253 | } | ||
254 | // We do not set zoom to 1 if zoomToFit is off, this is up to the code | 254 | // 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 | 255 | // calling us. It may went to zoom to some other level and/or to zoom on | ||
256 | // a particular position | 256 | // a particular position | ||
257 | zoomToFitWidthChanged(d->mZoomToFitWidth); | 257 | zoomToFillChanged(d->mZoomToFill); | ||
258 | } | 258 | } | ||
259 | 259 | | |||
260 | void AbstractImageView::resizeEvent(QGraphicsSceneResizeEvent* event) | 260 | void AbstractImageView::resizeEvent(QGraphicsSceneResizeEvent* event) | ||
261 | { | 261 | { | ||
262 | QGraphicsWidget::resizeEvent(event); | 262 | QGraphicsWidget::resizeEvent(event); | ||
263 | if (d->mZoomToFit) { | 263 | if (d->mZoomToFit) { | ||
264 | // setZoom() calls adjustImageOffset(), but only if the zoom changes. | 264 | // setZoom() calls adjustImageOffset(), but only if the zoom changes. | ||
265 | // If the view is resized but does not cause a zoom change, we call | 265 | // If the view is resized but does not cause a zoom change, we call | ||
266 | // adjustImageOffset() ourself. | 266 | // adjustImageOffset() ourself. | ||
267 | const qreal newZoom = computeZoomToFit(); | 267 | const qreal newZoom = computeZoomToFit(); | ||
268 | if (qFuzzyCompare(zoom(), newZoom)) { | 268 | if (qFuzzyCompare(zoom(), newZoom)) { | ||
269 | d->adjustImageOffset(AbstractImageViewPrivate::Notify); | 269 | d->adjustImageOffset(AbstractImageViewPrivate::Notify); | ||
270 | } else { | 270 | } else { | ||
271 | setZoom(newZoom); | 271 | setZoom(newZoom); | ||
272 | } | 272 | } | ||
273 | } else if (d->mZoomToFitWidth) { | 273 | } else if (d->mZoomToFill) { | ||
274 | const qreal newZoom = computeZoomToFitWidth(); | 274 | const qreal newZoom = computeZoomToFill(); | ||
275 | if (qFuzzyCompare(zoom(), newZoom)) { | 275 | if (qFuzzyCompare(zoom(), newZoom)) { | ||
276 | d->adjustImageOffset(AbstractImageViewPrivate::Notify); | 276 | d->adjustImageOffset(AbstractImageViewPrivate::Notify); | ||
277 | } else { | 277 | } else { | ||
278 | setZoom(newZoom); | 278 | setZoom(newZoom); | ||
279 | } | 279 | } | ||
280 | } else { | 280 | } else { | ||
281 | d->adjustImageOffset(); | 281 | d->adjustImageOffset(); | ||
282 | d->adjustScrollPos(); | 282 | d->adjustScrollPos(); | ||
Show All 11 Lines | 287 | { | |||
294 | qreal fitHeight = viewSize.height() / docSize.height(); | 294 | qreal fitHeight = viewSize.height() / docSize.height(); | ||
295 | qreal fit = qMin(fitWidth, fitHeight); | 295 | qreal fit = qMin(fitWidth, fitHeight); | ||
296 | if (!d->mEnlargeSmallerImages) { | 296 | if (!d->mEnlargeSmallerImages) { | ||
297 | fit = qMin(fit, qreal(1.)); | 297 | fit = qMin(fit, qreal(1.)); | ||
298 | } | 298 | } | ||
299 | return fit; | 299 | return fit; | ||
300 | } | 300 | } | ||
301 | 301 | | |||
302 | qreal AbstractImageView::computeZoomToFitWidth() const | 302 | qreal AbstractImageView::computeZoomToFill() const | ||
303 | { | 303 | { | ||
304 | QSizeF docSize = documentSize(); | 304 | QSizeF docSize = documentSize(); | ||
305 | if (docSize.isEmpty()) { | 305 | if (docSize.isEmpty()) { | ||
306 | return 1; | 306 | return 1; | ||
307 | } | 307 | } | ||
308 | QSizeF viewSize = boundingRect().size(); | 308 | QSizeF viewSize = boundingRect().size(); | ||
309 | qreal fitWidth = viewSize.width() / docSize.width(); | 309 | qreal fitWidth = viewSize.width() / docSize.width(); | ||
310 | qreal fitHeight = viewSize.height() / docSize.height(); | ||||
311 | qreal fill = qMax(fitWidth, fitHeight); | ||||
310 | if (!d->mEnlargeSmallerImages) { | 312 | if (!d->mEnlargeSmallerImages) { | ||
311 | fitWidth = qMin(fitWidth, qreal(1.)); | 313 | fill = qMin(fill, qreal(1.)); | ||
312 | } | 314 | } | ||
313 | return fitWidth; | 315 | return fill; | ||
ngraham: Maybe name this variable "fill" to match the feature it's connected to? | |||||
314 | } | 316 | } | ||
315 | 317 | | |||
316 | void AbstractImageView::mousePressEvent(QGraphicsSceneMouseEvent* event) | 318 | void AbstractImageView::mousePressEvent(QGraphicsSceneMouseEvent* event) | ||
317 | { | 319 | { | ||
318 | QGraphicsItem::mousePressEvent(event); | 320 | QGraphicsItem::mousePressEvent(event); | ||
319 | if (event->button() == Qt::MiddleButton) { | 321 | if (event->button() == Qt::MiddleButton) { | ||
320 | bool value = !zoomToFit(); | 322 | bool value = !zoomToFit(); | ||
321 | setZoomToFit(value); | 323 | setZoomToFit(value); | ||
▲ Show 20 Lines • Show All 252 Lines • Show Last 20 Lines |
Maybe name this variable "fill" to match the feature it's connected to?