Skip to content

Commit d3c69ad

Browse files
committed
Add zooming to image viewer in the Edit Dialog
This adds some basic zooming functionality to the image viewer used in the Edit Dialog. See issue #2029.
1 parent d0978ef commit d3c69ad

File tree

5 files changed

+149
-4
lines changed

5 files changed

+149
-4
lines changed

src/ImageViewer.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
#include <QPrinter>
55
#include <QPrintPreviewDialog>
66
#include <QPainter>
7+
#include <QScrollBar>
78

89
ImageViewer::ImageViewer(QWidget* parent) :
910
QWidget(parent),
1011
ui(new Ui::ImageViewer)
1112
{
1213
ui->setupUi(this);
14+
connect(ui->buttonOriginalSize, &QToolButton::clicked, [this]{ scaleImage(100); });
1315

1416
ui->labelView->addAction(ui->actionPrintImage);
1517
}
@@ -27,6 +29,9 @@ void ImageViewer::resetImage()
2729
void ImageViewer::setImage(const QImage& image)
2830
{
2931
ui->labelView->setPixmap(QPixmap::fromImage(image));
32+
33+
// Reset the scaling to show the image in its original size
34+
scaleImage(100);
3035
}
3136

3237
void ImageViewer::openPrintImageDialog()
@@ -46,3 +51,36 @@ void ImageViewer::openPrintImageDialog()
4651

4752
dialog.exec();
4853
}
54+
55+
void ImageViewer::scaleToFitWindow(bool enabled)
56+
{
57+
// Enable/disable the automatic resizing of the label inside the scrollbar
58+
ui->scrollArea->setWidgetResizable(enabled);
59+
60+
// When disabling the fit to window scaling, revert back to the original image size
61+
if(!enabled)
62+
scaleImage(100);
63+
}
64+
65+
void ImageViewer::scaleImage(int scale)
66+
{
67+
// Make sure the slider is updated when this is called programatically
68+
ui->sliderScale->setValue(scale);
69+
70+
// Update our scale factor
71+
qreal factor_change = (scale / 100.0) / m_scale_factor;
72+
m_scale_factor = scale / 100.0;
73+
74+
// Resize the image
75+
ui->labelView->resize(m_scale_factor * ui->labelView->pixmap()->size());
76+
77+
// Uncheck the fit to window button
78+
ui->buttonFitToWindow->setChecked(false);
79+
80+
// Fix scroll bars to zoom into center of viewport instead of tthe upper left corner
81+
const auto adjust_scrollbar = [](QScrollBar* scroll_bar, qreal factor) {
82+
scroll_bar->setValue(static_cast<int>(factor * scroll_bar->value() + ((factor - 1) * scroll_bar->pageStep() / 2)));
83+
};
84+
adjust_scrollbar(ui->scrollArea->horizontalScrollBar(), factor_change);
85+
adjust_scrollbar(ui->scrollArea->verticalScrollBar(), factor_change);
86+
}

src/ImageViewer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ class ImageViewer : public QWidget
2121
public slots:
2222
void openPrintImageDialog();
2323

24+
private slots:
25+
void scaleToFitWindow(bool enabled);
26+
void scaleImage(int scale);
27+
2428
private:
2529
Ui::ImageViewer* ui;
30+
31+
qreal m_scale_factor;
2632
};
2733

2834
#endif

src/ImageViewer.ui

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,88 @@
3131
</property>
3232
<item>
3333
<widget class="QScrollArea" name="scrollArea">
34-
<property name="widgetResizable">
35-
<bool>true</bool>
36-
</property>
3734
<widget class="QLabel" name="labelView">
3835
<property name="geometry">
3936
<rect>
4037
<x>0</x>
4138
<y>0</y>
4239
<width>431</width>
43-
<height>305</height>
40+
<height>280</height>
4441
</rect>
4542
</property>
43+
<property name="sizePolicy">
44+
<sizepolicy hsizetype="Ignored" vsizetype="Ignored">
45+
<horstretch>0</horstretch>
46+
<verstretch>0</verstretch>
47+
</sizepolicy>
48+
</property>
4649
<property name="contextMenuPolicy">
4750
<enum>Qt::ActionsContextMenu</enum>
4851
</property>
52+
<property name="scaledContents">
53+
<bool>true</bool>
54+
</property>
4955
</widget>
5056
</widget>
5157
</item>
58+
<item>
59+
<layout class="QHBoxLayout" name="horizontalLayout">
60+
<item>
61+
<spacer name="horizontalSpacer">
62+
<property name="orientation">
63+
<enum>Qt::Horizontal</enum>
64+
</property>
65+
<property name="sizeHint" stdset="0">
66+
<size>
67+
<width>40</width>
68+
<height>20</height>
69+
</size>
70+
</property>
71+
</spacer>
72+
</item>
73+
<item>
74+
<widget class="QSlider" name="sliderScale">
75+
<property name="minimum">
76+
<number>1</number>
77+
</property>
78+
<property name="maximum">
79+
<number>800</number>
80+
</property>
81+
<property name="value">
82+
<number>100</number>
83+
</property>
84+
<property name="orientation">
85+
<enum>Qt::Horizontal</enum>
86+
</property>
87+
</widget>
88+
</item>
89+
<item>
90+
<widget class="QToolButton" name="buttonOriginalSize">
91+
<property name="toolTip">
92+
<string>Reset the scaling to match the original size of the image.</string>
93+
</property>
94+
<property name="icon">
95+
<iconset resource="icons/icons.qrc">
96+
<normaloff>:/icons/view</normaloff>:/icons/view</iconset>
97+
</property>
98+
</widget>
99+
</item>
100+
<item>
101+
<widget class="QToolButton" name="buttonFitToWindow">
102+
<property name="toolTip">
103+
<string>Set the scaling to match the size of the viewport.</string>
104+
</property>
105+
<property name="icon">
106+
<iconset resource="icons/icons.qrc">
107+
<normaloff>:/icons/monitor_link.png</normaloff>:/icons/monitor_link.png</iconset>
108+
</property>
109+
<property name="checkable">
110+
<bool>true</bool>
111+
</property>
112+
</widget>
113+
</item>
114+
</layout>
115+
</item>
52116
</layout>
53117
<action name="actionPrintImage">
54118
<property name="icon">
@@ -86,5 +150,41 @@
86150
</hint>
87151
</hints>
88152
</connection>
153+
<connection>
154+
<sender>sliderScale</sender>
155+
<signal>valueChanged(int)</signal>
156+
<receiver>ImageViewer</receiver>
157+
<slot>scaleImage(int)</slot>
158+
<hints>
159+
<hint type="sourcelabel">
160+
<x>280</x>
161+
<y>294</y>
162+
</hint>
163+
<hint type="destinationlabel">
164+
<x>216</x>
165+
<y>153</y>
166+
</hint>
167+
</hints>
168+
</connection>
169+
<connection>
170+
<sender>buttonFitToWindow</sender>
171+
<signal>toggled(bool)</signal>
172+
<receiver>ImageViewer</receiver>
173+
<slot>scaleToFitWindow(bool)</slot>
174+
<hints>
175+
<hint type="sourcelabel">
176+
<x>419</x>
177+
<y>294</y>
178+
</hint>
179+
<hint type="destinationlabel">
180+
<x>216</x>
181+
<y>153</y>
182+
</hint>
183+
</hints>
184+
</connection>
89185
</connections>
186+
<slots>
187+
<slot>scaleImage(int)</slot>
188+
<slot>scaleToFitWindow(bool)</slot>
189+
</slots>
90190
</ui>

src/icons/icons.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,6 @@
101101
<file alias="open_in_app">application_link.png</file>
102102
<file alias="document_link">document-link.png</file>
103103
<file alias="open_data_in_app">application_go.png</file>
104+
<file>monitor_link.png</file>
104105
</qresource>
105106
</RCC>

src/icons/monitor_link.png

736 Bytes
Loading

0 commit comments

Comments
 (0)