Skip to content

Commit 442b153

Browse files
committed
Add button to fit image into current view
1 parent 029c6ae commit 442b153

File tree

8 files changed

+207
-3
lines changed

8 files changed

+207
-3
lines changed

resources/icons/dark/fitImage.svg

Lines changed: 76 additions & 0 deletions
Loading

resources/icons/light/fitImage.svg

Lines changed: 76 additions & 0 deletions
Loading

resources/kImageAnnotator_resources.qrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<file alias="italic.svg" >icons/dark/italic.svg</file>
4949
<file alias="underline.svg" >icons/dark/underline.svg</file>
5050
<file alias="cut.svg" >icons/dark/cut.svg</file>
51+
<file alias="fitImage.svg" >icons/dark/fitImage.svg</file>
5152
</qresource>
5253

5354
<qresource prefix="/icons/light">
@@ -98,6 +99,7 @@
9899
<file alias="italic.svg" >icons/light/italic.svg</file>
99100
<file alias="underline.svg" >icons/light/underline.svg</file>
100101
<file alias="cut.svg" >icons/light/cut.svg</file>
102+
<file alias="fitImage.svg" >icons/light/fitImage.svg</file>
101103
</qresource>
102104

103105
<qresource prefix="/stickers">

src/gui/annotator/settings/AnnotationGeneralSettings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void AnnotationGeneralSettings::initGui()
4545
setFocusPolicy(Qt::ClickFocus);
4646

4747
connect(mZoomPicker, &ZoomPicker::zoomValueChanged, this, &AnnotationGeneralSettings::zoomValueChanged);
48+
connect(mZoomPicker, &ZoomPicker::fitImageToViewSignal, this, &AnnotationGeneralSettings::zoomValueChanged);
4849
}
4950

5051
void AnnotationGeneralSettings::updateZoomLevel(double value)

src/gui/scrollAndZoomView/ViewZoomer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,18 @@ void ViewZoomer::wheelZoom(QWheelEvent *event)
6464
event->accept(); // supress scrolling
6565
}
6666

67+
void ViewZoomer::fitImage()
68+
{
69+
mView->fitInView(mView->sceneRect(), Qt::KeepAspectRatio);
70+
}
71+
6772
void ViewZoomer::setZoomValue(double value)
6873
{
74+
// ZoomPicker's mFitImageButton() sets value to -1
75+
if (value < 0) {
76+
fitImage();
77+
return;
78+
}
6979
zoom(value - zoomValue());
7080
}
7181

src/gui/scrollAndZoomView/ViewZoomer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ViewZoomer : public ZoomValueProvider
4242
void zoom(double factor);
4343
void zoomToPoint(double factor, const QPoint &viewPoint);
4444
void wheelZoom(QWheelEvent *event);
45+
void fitImage();
4546

4647
private:
4748
QGraphicsView *mView;

src/widgets/settingsPicker/ZoomPicker.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ ZoomPicker::ZoomPicker(QWidget *parent) :
2828
mSpinBox(new CustomSpinBox(this)),
2929
mZoomInAction(new QAction(this)),
3030
mZoomOutAction(new QAction(this)),
31-
mResetZoomAction(new QAction(this))
31+
mResetZoomAction(new QAction(this)),
32+
mFitImageAction(new QAction(this))
3233
{
3334
initGui();
3435
}
@@ -51,9 +52,14 @@ void ZoomPicker::initGui()
5152
mSpinBox->setSuffix(QLatin1String("%"));
5253
mSpinBox->setWrapping(false);
5354

55+
mFitImageAction = createAction(tr("Fit Image"), IconLoader::load(QLatin1String("fitImage.svg")));
56+
connect(mFitImageAction, &QAction::triggered, this, &ZoomPicker::fitImageToView);
57+
mFitImageButton = createButton(mFitImageAction);
58+
5459
mZoomInAction->setShortcut(QKeySequence::ZoomIn);
5560
mZoomOutAction->setShortcut(QKeySequence::ZoomOut);
5661
mResetZoomAction->setShortcut(Qt::CTRL + Qt::Key_0);
62+
mFitImageAction->setShortcut(Qt::CTRL + Qt::Key_F);
5763

5864
setToolTip(getToolTip());
5965

@@ -69,6 +75,7 @@ void ZoomPicker::initGui()
6975

7076
mLayout->addWidget(mLabel);
7177
mLayout->addWidget(mSpinBox);
78+
mLayout->addWidget(mFitImageButton);
7279
mLayout->setAlignment(Qt::AlignLeft);
7380

7481
setLayout(mLayout);
@@ -80,7 +87,24 @@ QString ZoomPicker::getToolTip() const
8087
auto zoomIn = tr("Zoom In (%1)").arg(mZoomInAction->shortcut().toString());
8188
auto zoomOut = tr("Zoom Out (%1)").arg(mZoomOutAction->shortcut().toString());
8289
auto resetZoom = tr("Reset Zoom (%1)").arg(mResetZoomAction->shortcut().toString());
83-
return zoomIn + newLine + zoomOut + newLine + resetZoom;
90+
auto fitZoom = tr("Fit image to view (%1)").arg(mFitImageAction->shortcut().toString());
91+
return zoomIn + newLine + zoomOut + newLine + resetZoom + newLine + fitZoom;
92+
}
93+
94+
QAction *ZoomPicker::createAction(const QString &tooltip, const QIcon &icon)
95+
{
96+
auto action = new CustomToolButtonAction(this);
97+
action->setIcon(icon);
98+
action->setToolTip(tooltip);
99+
action->updateDefaultWidget();
100+
return action;
101+
}
102+
103+
CustomToolButton *ZoomPicker::createButton(QAction *defaultAction)
104+
{
105+
auto button = new CustomToolButton(this);
106+
button->setAction(defaultAction);
107+
return button;
84108
}
85109

86110
void ZoomPicker::setZoomValue(double value)
@@ -89,6 +113,11 @@ void ZoomPicker::setZoomValue(double value)
89113
mSpinBox->setValueSilent(zoomValue);
90114
}
91115

116+
void ZoomPicker::fitImageToView()
117+
{
118+
emit fitImageToViewSignal(-1);
119+
}
120+
92121
void ZoomPicker::notifyZoomValueChanged(double value)
93122
{
94123
emit zoomValueChanged(value / 100.0);

src/widgets/settingsPicker/ZoomPicker.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@
2323
#include <QWidget>
2424
#include <QLabel>
2525
#include <QHBoxLayout>
26+
#include <QButtonGroup>
2627
#include <QAction>
2728

2829
#include "src/widgets/CustomSpinBox.h"
30+
#include "src/widgets/CustomToolButton.h"
31+
#include "src/widgets/CustomToolButtonAction.h"
2932
#include "src/widgets/settingsPicker/SettingsPickerWidget.h"
3033
#include "src/common/helper/IconLoader.h"
3134
#include "src/common/provider/ScaledSizeProvider.h"
35+
#include "src/gui/scrollAndZoomView/ViewZoomer.h"
3236

3337
namespace kImageAnnotator {
3438

@@ -41,9 +45,11 @@ Q_OBJECT
4145

4246
public slots:
4347
void setZoomValue(double value);
48+
void fitImageToView();
4449

4550
signals:
4651
void zoomValueChanged(double zoomLevel);
52+
void fitImageToViewSignal(double value);
4753

4854
protected:
4955
QWidget* expandingWidget() override;
@@ -55,7 +61,10 @@ public slots:
5561
QAction *mZoomInAction;
5662
QAction *mZoomOutAction;
5763
QAction *mResetZoomAction;
58-
64+
QAction *mFitImageAction;
65+
CustomToolButton *mFitImageButton;
66+
QAction *createAction(const QString &tooltip, const QIcon &icon);
67+
CustomToolButton *createButton(QAction *defaultAction);
5968
void initGui();
6069

6170
private slots:

0 commit comments

Comments
 (0)