Skip to content

Commit b73df5e

Browse files
committed
refactor: make fitByOrientation() more sane
1 parent 4375fe1 commit b73df5e

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

app/graphicsview.cpp

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,90 @@ void GraphicsView::fitByOrientation(Qt::Orientation ori, bool scaleDownOnly)
171171
{
172172
resetScale();
173173

174-
QRectF viewRect = this->viewport()->rect().adjusted(2, 2, -2, -2);
174+
QRectF viewRect = this->viewport()->rect();
175175
QRectF imageRect = transform().mapRect(sceneRect());
176+
QSize viewSize = viewRect.size().toSize();
176177

177178
qreal ratio;
178179

179180
if (ori == Qt::Horizontal) {
180-
ratio = viewRect.width() / imageRect.width();
181+
// Horizontal fit means fit by width
182+
if (scaleDownOnly && imageRect.width() <= viewSize.width()) {
183+
// Image width already fits, no scaling needed
184+
ratio = 1;
185+
} else {
186+
ratio = viewRect.width() / imageRect.width();
187+
}
181188
} else {
182-
ratio = viewRect.height() / imageRect.height();
189+
// Vertical fit means fit by height
190+
if (scaleDownOnly && imageRect.height() <= viewSize.height()) {
191+
// Image height already fits, no scaling needed
192+
ratio = 1;
193+
} else {
194+
ratio = viewRect.height() / imageRect.height();
195+
}
196+
}
197+
198+
if (ratio != 1) {
199+
scale(ratio, ratio);
183200
}
184201

185-
if (scaleDownOnly && ratio > 1) ratio = 1;
202+
// Position the image correctly based on orientation with rotation consideration
203+
QRectF originalScene = sceneRect();
204+
QTransform currentTransform = transform();
205+
206+
if (ori == Qt::Horizontal) {
207+
// For horizontal fit (fit by width), position at top (for tall images)
208+
// Find the scene point that corresponds to the top-center of the transformed image
209+
QPointF sceneTopCenter;
210+
211+
if (qFuzzyIsNull(currentTransform.m12()) && qFuzzyIsNull(currentTransform.m21())) {
212+
// 0° or 180° rotation
213+
if (currentTransform.m11() > 0 && currentTransform.m22() > 0) {
214+
// 0° rotation: use original top-center
215+
sceneTopCenter = QPointF(originalScene.center().x(), originalScene.top());
216+
} else {
217+
// 180° rotation: the visual "top" is now at the scene bottom
218+
sceneTopCenter = QPointF(originalScene.center().x(), originalScene.bottom());
219+
}
220+
} else {
221+
// 90/270 degree rotation: the "top" in view corresponds to left/right in scene
222+
if (currentTransform.m12() > 0) {
223+
// 90 degree: top in view = left in scene
224+
sceneTopCenter = QPointF(originalScene.left(), originalScene.center().y());
225+
} else {
226+
// 270 degree: top in view = right in scene
227+
sceneTopCenter = QPointF(originalScene.right(), originalScene.center().y());
228+
}
229+
}
230+
centerOn(sceneTopCenter);
231+
} else {
232+
// For vertical fit (fit by height), position at left (for wide images)
233+
// Find the scene point that corresponds to the left-center of the transformed image
234+
QPointF sceneLeftCenter;
235+
236+
if (qFuzzyIsNull(currentTransform.m12()) && qFuzzyIsNull(currentTransform.m21())) {
237+
// 0° or 180° rotation
238+
if (currentTransform.m11() > 0 && currentTransform.m22() > 0) {
239+
// 0° rotation: use original left-center
240+
sceneLeftCenter = QPointF(originalScene.left(), originalScene.center().y());
241+
} else {
242+
// 180° rotation: the visual "left" is now at the scene right
243+
sceneLeftCenter = QPointF(originalScene.right(), originalScene.center().y());
244+
}
245+
} else {
246+
// 90/270 degree rotation: the "left" in view corresponds to top/bottom in scene
247+
if (currentTransform.m21() > 0) {
248+
// 90 degree: left in view = top in scene
249+
sceneLeftCenter = QPointF(originalScene.center().x(), originalScene.top());
250+
} else {
251+
// 270 degree: left in view = bottom in scene
252+
sceneLeftCenter = QPointF(originalScene.center().x(), originalScene.bottom());
253+
}
254+
}
255+
centerOn(sceneLeftCenter);
256+
}
186257

187-
scale(ratio, ratio);
188-
centerOn(imageRect.top(), 0);
189258
m_enableFitInView = false;
190259

191260
applyTransformationModeByScaleFactor();

0 commit comments

Comments
 (0)