@@ -58,15 +58,12 @@ bool ImageViewer::eventFilter(QObject *obj, QEvent *e) {
5858 auto e_type = e->type ();
5959
6060 if (e_type == QEvent::Wheel) {
61- if (ui->buttonFitToWindow ->isChecked ()) {
62- scaleToFitWindow (false );
63- }
6461 auto *wheel_event = static_cast <QWheelEvent*>(e);
6562 auto step = ui->sliderScale ->pageStep ();
6663 if (wheel_event->angleDelta ().y () < 0 ) {
6764 step = -step;
6865 }
69- ui-> sliderScale -> setValue (ui->sliderScale ->value () + step);
66+ scaleImage (ui->sliderScale ->value () + step);
7067 e->accept ();
7168 return true ;
7269 }
@@ -134,13 +131,37 @@ void ImageViewer::scaleToFitWindow(bool enabled)
134131 scaleImage (100 );
135132 } else {
136133 ui->labelView ->setMaximumSize (m_image_size.scaled (ui->scrollArea ->viewport ()->size (), Qt::KeepAspectRatio));
134+ setSliderValueWithoutSignal (ui->labelView ->maximumWidth () / static_cast <double >(m_image_size.width ()) * 100 );
137135 }
138136}
139137
138+ void ImageViewer::setNoFitWithoutSignal ()
139+ {
140+ if (ui->buttonFitToWindow ->isChecked ()) {
141+ ui->buttonFitToWindow ->blockSignals (true );
142+ ui->scrollArea ->setWidgetResizable (false );
143+ ui->buttonFitToWindow ->setChecked (false );
144+ ui->buttonFitToWindow ->blockSignals (false );
145+ }
146+ }
147+
148+ void ImageViewer::setSliderValueWithoutSignal (int value)
149+ {
150+ ui->sliderScale ->blockSignals (true );
151+ ui->sliderScale ->setValue (value);
152+ ui->sliderScale ->blockSignals (false );
153+ }
154+
140155void ImageViewer::scaleImage (int scale)
141156{
157+ // Clamp scale to the sliderScale min/max
158+ scale = std::min (ui->sliderScale ->maximum (), std::max (ui->sliderScale ->minimum (), scale));
159+
142160 // Make sure the slider is updated when this is called programmatically
143- ui->sliderScale ->setValue (scale);
161+ setSliderValueWithoutSignal (scale);
162+
163+ // Uncheck the fit to window button
164+ setNoFitWithoutSignal ();
144165
145166 // Update our scale factor
146167 auto scale_factor = scale / 100.0 ;
@@ -149,10 +170,7 @@ void ImageViewer::scaleImage(int scale)
149170 auto max_size_old = ui->labelView ->maximumSize ();
150171 ui->labelView ->setMaximumSize (m_image_size * scale_factor);
151172 ui->labelView ->resize (ui->labelView ->maximumSize ());
152- auto factor_change = ui->labelView ->maximumSize ().width () / static_cast <double >(max_size_old.width ());
153-
154- // Uncheck the fit to window button
155- ui->buttonFitToWindow ->setChecked (false );
173+ auto factor_change = ui->labelView ->maximumWidth () / static_cast <double >(max_size_old.width ());
156174
157175 // Fix scroll bars to zoom into center of viewport instead of the upper left corner
158176 const auto adjust_scrollbar = [](QScrollBar* scroll_bar, qreal factor) {
0 commit comments