PixelPaint: Make Fit Image To View account for rulers

Because of the way rulers are implemented in the ImageEditor
currently, the `Fit Image To View` action doesn't work correctly
with them enabled. This patch makes them adjust to the effective
viewport area and account for the rulers.

This is a bit of a hack, but the correct way to deal with this would
be to put the rulers in a new widget so they don't interfere with
the actual viewport rect (which is being used all over).
This commit is contained in:
Mustafa Quraish 2021-09-11 14:31:41 -04:00 committed by Andreas Kling
parent 1e1e5bb5f7
commit f890f8529a
Notes: sideshowbarker 2024-07-18 04:14:01 +09:00

View file

@ -562,13 +562,27 @@ void ImageEditor::scale_by(float scale_delta)
void ImageEditor::fit_image_to_view()
{
auto viewport_rect = rect();
m_pan_origin = Gfx::FloatPoint(0, 0);
if (m_show_rulers) {
viewport_rect = {
viewport_rect.x() + m_ruler_thickness,
viewport_rect.y() + m_ruler_thickness,
viewport_rect.width() - m_ruler_thickness,
viewport_rect.height() - m_ruler_thickness
};
}
const float border_ratio = 0.95f;
auto image_size = image().size();
auto height_ratio = rect().height() / (float)image_size.height();
auto width_ratio = rect().width() / (float)image_size.width();
auto height_ratio = viewport_rect.height() / (float)image_size.height();
auto width_ratio = viewport_rect.width() / (float)image_size.width();
m_scale = border_ratio * min(height_ratio, width_ratio);
m_pan_origin = Gfx::FloatPoint(0, 0);
float offset = m_show_rulers ? -m_ruler_thickness / (m_scale * 2.0f) : 0.0f;
m_pan_origin = Gfx::FloatPoint(offset, offset);
relayout();
}