Skip to content

Commit

Permalink
Add button to fit image into current view
Browse files Browse the repository at this point in the history
  • Loading branch information
precla committed Feb 3, 2022
1 parent 029c6ae commit 442b153
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 3 deletions.
76 changes: 76 additions & 0 deletions resources/icons/dark/fitImage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions resources/icons/light/fitImage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions resources/kImageAnnotator_resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<file alias="italic.svg" >icons/dark/italic.svg</file>
<file alias="underline.svg" >icons/dark/underline.svg</file>
<file alias="cut.svg" >icons/dark/cut.svg</file>
<file alias="fitImage.svg" >icons/dark/fitImage.svg</file>
</qresource>

<qresource prefix="/icons/light">
Expand Down Expand Up @@ -98,6 +99,7 @@
<file alias="italic.svg" >icons/light/italic.svg</file>
<file alias="underline.svg" >icons/light/underline.svg</file>
<file alias="cut.svg" >icons/light/cut.svg</file>
<file alias="fitImage.svg" >icons/light/fitImage.svg</file>
</qresource>

<qresource prefix="/stickers">
Expand Down
1 change: 1 addition & 0 deletions src/gui/annotator/settings/AnnotationGeneralSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void AnnotationGeneralSettings::initGui()
setFocusPolicy(Qt::ClickFocus);

connect(mZoomPicker, &ZoomPicker::zoomValueChanged, this, &AnnotationGeneralSettings::zoomValueChanged);
connect(mZoomPicker, &ZoomPicker::fitImageToViewSignal, this, &AnnotationGeneralSettings::zoomValueChanged);
}

void AnnotationGeneralSettings::updateZoomLevel(double value)
Expand Down
10 changes: 10 additions & 0 deletions src/gui/scrollAndZoomView/ViewZoomer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,18 @@ void ViewZoomer::wheelZoom(QWheelEvent *event)
event->accept(); // supress scrolling
}

void ViewZoomer::fitImage()
{
mView->fitInView(mView->sceneRect(), Qt::KeepAspectRatio);
}

void ViewZoomer::setZoomValue(double value)
{
// ZoomPicker's mFitImageButton() sets value to -1
if (value < 0) {
fitImage();
return;
}
zoom(value - zoomValue());
}

Expand Down
1 change: 1 addition & 0 deletions src/gui/scrollAndZoomView/ViewZoomer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ViewZoomer : public ZoomValueProvider
void zoom(double factor);
void zoomToPoint(double factor, const QPoint &viewPoint);
void wheelZoom(QWheelEvent *event);
void fitImage();

private:
QGraphicsView *mView;
Expand Down
33 changes: 31 additions & 2 deletions src/widgets/settingsPicker/ZoomPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ ZoomPicker::ZoomPicker(QWidget *parent) :
mSpinBox(new CustomSpinBox(this)),
mZoomInAction(new QAction(this)),
mZoomOutAction(new QAction(this)),
mResetZoomAction(new QAction(this))
mResetZoomAction(new QAction(this)),
mFitImageAction(new QAction(this))
{
initGui();
}
Expand All @@ -51,9 +52,14 @@ void ZoomPicker::initGui()
mSpinBox->setSuffix(QLatin1String("%"));
mSpinBox->setWrapping(false);

mFitImageAction = createAction(tr("Fit Image"), IconLoader::load(QLatin1String("fitImage.svg")));
connect(mFitImageAction, &QAction::triggered, this, &ZoomPicker::fitImageToView);
mFitImageButton = createButton(mFitImageAction);

mZoomInAction->setShortcut(QKeySequence::ZoomIn);
mZoomOutAction->setShortcut(QKeySequence::ZoomOut);
mResetZoomAction->setShortcut(Qt::CTRL + Qt::Key_0);
mFitImageAction->setShortcut(Qt::CTRL + Qt::Key_F);

setToolTip(getToolTip());

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

mLayout->addWidget(mLabel);
mLayout->addWidget(mSpinBox);
mLayout->addWidget(mFitImageButton);
mLayout->setAlignment(Qt::AlignLeft);

setLayout(mLayout);
Expand All @@ -80,7 +87,24 @@ QString ZoomPicker::getToolTip() const
auto zoomIn = tr("Zoom In (%1)").arg(mZoomInAction->shortcut().toString());
auto zoomOut = tr("Zoom Out (%1)").arg(mZoomOutAction->shortcut().toString());
auto resetZoom = tr("Reset Zoom (%1)").arg(mResetZoomAction->shortcut().toString());
return zoomIn + newLine + zoomOut + newLine + resetZoom;
auto fitZoom = tr("Fit image to view (%1)").arg(mFitImageAction->shortcut().toString());
return zoomIn + newLine + zoomOut + newLine + resetZoom + newLine + fitZoom;
}

QAction *ZoomPicker::createAction(const QString &tooltip, const QIcon &icon)
{
auto action = new CustomToolButtonAction(this);
action->setIcon(icon);
action->setToolTip(tooltip);
action->updateDefaultWidget();
return action;
}

CustomToolButton *ZoomPicker::createButton(QAction *defaultAction)
{
auto button = new CustomToolButton(this);
button->setAction(defaultAction);
return button;
}

void ZoomPicker::setZoomValue(double value)
Expand All @@ -89,6 +113,11 @@ void ZoomPicker::setZoomValue(double value)
mSpinBox->setValueSilent(zoomValue);
}

void ZoomPicker::fitImageToView()
{
emit fitImageToViewSignal(-1);
}

void ZoomPicker::notifyZoomValueChanged(double value)
{
emit zoomValueChanged(value / 100.0);
Expand Down
11 changes: 10 additions & 1 deletion src/widgets/settingsPicker/ZoomPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
#include <QWidget>
#include <QLabel>
#include <QHBoxLayout>
#include <QButtonGroup>
#include <QAction>

#include "src/widgets/CustomSpinBox.h"
#include "src/widgets/CustomToolButton.h"
#include "src/widgets/CustomToolButtonAction.h"
#include "src/widgets/settingsPicker/SettingsPickerWidget.h"
#include "src/common/helper/IconLoader.h"
#include "src/common/provider/ScaledSizeProvider.h"
#include "src/gui/scrollAndZoomView/ViewZoomer.h"

namespace kImageAnnotator {

Expand All @@ -41,9 +45,11 @@ Q_OBJECT

public slots:
void setZoomValue(double value);
void fitImageToView();

signals:
void zoomValueChanged(double zoomLevel);
void fitImageToViewSignal(double value);

protected:
QWidget* expandingWidget() override;
Expand All @@ -55,7 +61,10 @@ public slots:
QAction *mZoomInAction;
QAction *mZoomOutAction;
QAction *mResetZoomAction;

QAction *mFitImageAction;
CustomToolButton *mFitImageButton;
QAction *createAction(const QString &tooltip, const QIcon &icon);
CustomToolButton *createButton(QAction *defaultAction);
void initGui();

private slots:
Expand Down

0 comments on commit 442b153

Please sign in to comment.