Skip to content

Commit

Permalink
Allow zooming in crop and modify canvas view #212 #229
Browse files Browse the repository at this point in the history
  • Loading branch information
DamirPorobic committed Apr 27, 2021
1 parent fc24a02 commit 6c0ae04
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 163 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* New: Add invert color image effect. ([#228](https://github.com/ksnip/kImageAnnotator/issues/228))
* New: Allow disabling item shadow per item from UI. ([#223](https://github.com/ksnip/kImageAnnotator/issues/223))
* New: Add a font selection to UI. ([#130](https://github.com/ksnip/kImageAnnotator/issues/130))
* New: Add zoom in/out capability to crop view. ([#212](https://github.com/ksnip/kImageAnnotator/issues/212))
* New: Allow to zoom in modify canvas view. ([#229](https://github.com/ksnip/kImageAnnotator/issues/229))
* Changed: Change drop shadow to cover all sites. ([#202](https://github.com/ksnip/kImageAnnotator/issues/202))
* Fixed: Deleting item outside image doesn't decrease canvas size. ([#164](https://github.com/ksnip/kImageAnnotator/issues/164))
* Fixed: Duplicate region of grayscale image has color. ([#214](https://github.com/ksnip/kImageAnnotator/issues/214))
Expand Down
2 changes: 1 addition & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main(int argc, char **argv)
QPixmap pixmap(QSize(500, 500));
pixmap.fill(QColor(Qt::darkGreen));
auto kImageAnnotator = new KImageAnnotator();
kImageAnnotator->setTabBarAutoHide(false);
kImageAnnotator->setTabBarAutoHide(true);
kImageAnnotator->addTab(pixmap, QLatin1String("image1"), QLatin1String("image1"));
kImageAnnotator->addTab(pixmap, QLatin1String("image2"), QLatin1String("image2"));
kImageAnnotator->adjustSize();
Expand Down
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ set(KIMAGEANNOTATOR_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/gui/CoreView.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/AnnotationWidget.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/AnnotationView.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/AnnotationViewZoomer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gui/scrollAndZoomView/ScrollAndZoomView.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gui/scrollAndZoomView/ViewZoomer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/settings/ItemSettingsWidgetConfigurator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/settings/AnnotationSettingsAdapter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/settings/AnnotationItemSettings.cpp
Expand Down
97 changes: 1 addition & 96 deletions src/gui/annotator/AnnotationView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,9 @@
namespace kImageAnnotator {

AnnotationView::AnnotationView(QWidget *parent) :
mIsDragging(false),
QGraphicsView(parent),
mAnnotationViewZoomer(new AnnotationViewZoomer(this))
ScrollAndZoomView(parent)
{
setTransformationAnchor(QGraphicsView::NoAnchor);
disableDragging();
setViewportUpdateMode(ViewportUpdateMode::FullViewportUpdate);
}

ZoomValueProvider *AnnotationView::zoomValueProvider() const
{
return mAnnotationViewZoomer;
}

void AnnotationView::keyPressEvent(QKeyEvent *event)
{
mKeyHelper.keyPress(event);
if(mKeyHelper.isSpacePressed()) {
enableDragging(mapFromGlobal(QCursor::pos()));
}
QGraphicsView::keyPressEvent(event);
}

void AnnotationView::keyReleaseEvent(QKeyEvent *event)
{
mKeyHelper.keyRelease(event);
if(!mKeyHelper.isSpacePressed()) {
disableDragging();
}
QGraphicsView::keyReleaseEvent(event);
}

void AnnotationView::mouseMoveEvent(QMouseEvent *event)
{
if(mIsDragging) {
scrollTo(event->pos());
return;
}
QGraphicsView::mouseMoveEvent(event);
}

void AnnotationView::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::MidButton) {
enableDragging(event->pos());
return;
}
QGraphicsView::mousePressEvent(event);
}

void AnnotationView::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::MidButton) {
disableDragging();
return;
}
QGraphicsView::mouseReleaseEvent(event);
}

void AnnotationView::wheelEvent(QWheelEvent *event)
{
if(event->modifiers() & Qt::ControlModifier ) {
mAnnotationViewZoomer->wheelZoom(event);
} else {
QGraphicsView::wheelEvent(event);
}
}

void AnnotationView::drawBackground(QPainter *painter, const QRectF &rect)
Expand All @@ -96,36 +33,4 @@ void AnnotationView::drawBackground(QPainter *painter, const QRectF &rect)
mCanvasPainter.paint(painter, annotationArea->canvasRect(), annotationArea->canvasColor());
}

void AnnotationView::scrollTo(const QPoint &pos)
{
auto delta = pos - mLastPosition;
scrollByDelta(horizontalScrollBar(), delta.x());
scrollByDelta(verticalScrollBar(), delta.y());
mLastPosition = pos;
}

void AnnotationView::scrollByDelta(QScrollBar *scrollBar, int delta) const
{
scrollBar->setValue(scrollBar->value() - delta);
}

void AnnotationView::enableDragging(const QPoint &pos)
{
mIsDragging = true;
mLastPosition = pos;
setDragCursorEnabled(true);
}

void AnnotationView::disableDragging()
{
mIsDragging = false;
mLastPosition = {};
setDragCursorEnabled(false);
}

void AnnotationView::setDragCursorEnabled(bool enabled) const
{
enabled ? QGuiApplication::setOverrideCursor(Qt::SizeAllCursor) : QGuiApplication::restoreOverrideCursor();
}

} // namespace kImageAnnotator
23 changes: 3 additions & 20 deletions src/gui/annotator/AnnotationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,30 @@
#ifndef KIMAGEANNOTATOR_ANNOTATIONVIEW_H
#define KIMAGEANNOTATOR_ANNOTATIONVIEW_H

#include <QGraphicsView>
#include <QMouseEvent>
#include <QScrollBar>
#include <QGuiApplication>

#include "src/gui/scrollAndZoomView/ScrollAndZoomView.h"
#include "src/common/helper/KeyHelper.h"
#include "src/annotations/core/AnnotationArea.h"
#include "src/annotations/misc/CanvasPainter.h"
#include "src/gui/annotator/AnnotationViewZoomer.h"
#include "src/gui/scrollAndZoomView/ViewZoomer.h"

namespace kImageAnnotator {

class AnnotationView : public QGraphicsView
class AnnotationView : public ScrollAndZoomView
{
Q_OBJECT
public:
explicit AnnotationView(QWidget *parent);
~AnnotationView() override = default;
ZoomValueProvider *zoomValueProvider() const;

protected:
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
void drawBackground(QPainter *painter, const QRectF &rect) override;

private:
AnnotationViewZoomer *mAnnotationViewZoomer;
bool mIsDragging;
QPoint mLastPosition;
KeyHelper mKeyHelper;
CanvasPainter mCanvasPainter;

void scrollTo(const QPoint &pos);
void scrollByDelta(QScrollBar *scrollBar, int delta) const;
void enableDragging(const QPoint &pos);
void disableDragging();
void setDragCursorEnabled(bool enabled) const;
};

} // namespace kImageAnnotator
Expand Down
2 changes: 1 addition & 1 deletion src/gui/annotator/tabs/AnnotationTabContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "src/gui/annotator/settings/AnnotationItemSettings.h"
#include "src/annotations/core/AnnotationArea.h"
#include "src/common/provider/DevicePixelRatioScaler.h"
#include "src/gui/annotator/AnnotationViewZoomer.h"
#include "src/gui/scrollAndZoomView/ViewZoomer.h"

namespace kImageAnnotator {

Expand Down
4 changes: 2 additions & 2 deletions src/gui/canvasModifier/ModifyCanvasView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

namespace kImageAnnotator {

ModifyCanvasView::ModifyCanvasView(SelectionHandler *selectionHandler, KeyHelper *keyHelper) :
BaseSelectionView(selectionHandler, keyHelper)
ModifyCanvasView::ModifyCanvasView(SelectionHandler *selectionHandler, KeyHelper *keyHelper, QWidget *parent) :
BaseSelectionView(selectionHandler, keyHelper, parent)
{
setViewportUpdateMode(ViewportUpdateMode::FullViewportUpdate);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/canvasModifier/ModifyCanvasView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ModifyCanvasView : public BaseSelectionView
{
Q_OBJECT
public:
explicit ModifyCanvasView(SelectionHandler *selectionHandler, KeyHelper *keyHelper);
explicit ModifyCanvasView(SelectionHandler *selectionHandler, KeyHelper *keyHelper, QWidget *parent);
~ModifyCanvasView() override = default;
void setCanvasRect(const QRectF &rect);
void setCanvasColor(const QColor &color);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/canvasModifier/ModifyCanvasWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace kImageAnnotator {
ModifyCanvasWidget::ModifyCanvasWidget() :
mAnnotationArea(nullptr),
mSelectionHandler(new SelectionHandler(new ModifyCanvasSelectionRestrictor)),
mModifyCanvasView(new ModifyCanvasView(mSelectionHandler, mKeyHelper)),
mModifyCanvasView(new ModifyCanvasView(mSelectionHandler, mKeyHelper, this)),
mKeyHelper(new KeyHelper()),
mApplyButton(new QPushButton),
mCancelButton(new QPushButton),
Expand Down
4 changes: 2 additions & 2 deletions src/gui/cropper/CropView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

namespace kImageAnnotator {

CropView::CropView(SelectionHandler *cropSelectionHandler, KeyHelper *keyHelper) :
BaseSelectionView(cropSelectionHandler, keyHelper)
CropView::CropView(SelectionHandler *cropSelectionHandler, KeyHelper *keyHelper, QWidget *parent) :
BaseSelectionView(cropSelectionHandler, keyHelper, parent)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/cropper/CropView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CropView : public BaseSelectionView
{
Q_OBJECT
public:
explicit CropView(SelectionHandler *selectionHandler, KeyHelper *keyHelper);
explicit CropView(SelectionHandler *selectionHandler, KeyHelper *keyHelper, QWidget *parent);
~CropView() override = default;

protected:
Expand Down
2 changes: 1 addition & 1 deletion src/gui/cropper/CropWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CropWidget::CropWidget() :
mAnnotationArea(nullptr),
mKeyHelper(new KeyHelper()),
mSelectionHandler(new SelectionHandler(new CropSelectionRestrictor)),
mCropView(new CropView(mSelectionHandler, mKeyHelper)),
mCropView(new CropView(mSelectionHandler, mKeyHelper, this)),
mCropButton(new QPushButton),
mCancelButton(new QPushButton),
mPanelLayout(new QHBoxLayout),
Expand Down
130 changes: 130 additions & 0 deletions src/gui/scrollAndZoomView/ScrollAndZoomView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (C) 2021 Damir Porobic <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "ScrollAndZoomView.h"

namespace kImageAnnotator {

ScrollAndZoomView::ScrollAndZoomView(QWidget *parent) :
QGraphicsView(parent),
mIsDragging(false),
mViewZoomer(new ViewZoomer(this))
{
setTransformationAnchor(QGraphicsView::NoAnchor);
disableDragging();
setViewportUpdateMode(ViewportUpdateMode::FullViewportUpdate);
}

ScrollAndZoomView::~ScrollAndZoomView()
{
delete mViewZoomer;
}

ZoomValueProvider *ScrollAndZoomView::zoomValueProvider() const
{
return mViewZoomer;
}

void ScrollAndZoomView::keyPressEvent(QKeyEvent *event)
{
mKeyHelper.keyPress(event);
if(mKeyHelper.isSpacePressed()) {
enableDragging(mapFromGlobal(QCursor::pos()));
}
QGraphicsView::keyPressEvent(event);
}

void ScrollAndZoomView::keyReleaseEvent(QKeyEvent *event)
{
mKeyHelper.keyRelease(event);
if(!mKeyHelper.isSpacePressed()) {
disableDragging();
}
QGraphicsView::keyReleaseEvent(event);
}

void ScrollAndZoomView::mouseMoveEvent(QMouseEvent *event)
{
if (mIsDragging) {
scrollTo(event->pos());
} else {
QGraphicsView::mouseMoveEvent(event);
}
}

void ScrollAndZoomView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton) {
enableDragging(event->pos());
} else {
QGraphicsView::mousePressEvent(event);
}
}

void ScrollAndZoomView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton) {
disableDragging();
} else {
QGraphicsView::mouseReleaseEvent(event);
}
}

void ScrollAndZoomView::wheelEvent(QWheelEvent *event)
{
if (event->modifiers() & Qt::ControlModifier ) {
mViewZoomer->wheelZoom(event);
} else {
QGraphicsView::wheelEvent(event);
}
}

void ScrollAndZoomView::scrollTo(const QPoint &pos)
{
auto delta = pos - mLastPosition;
scrollByDelta(horizontalScrollBar(), delta.x());
scrollByDelta(verticalScrollBar(), delta.y());
mLastPosition = pos;
}

void ScrollAndZoomView::scrollByDelta(QScrollBar *scrollBar, int delta) const
{
scrollBar->setValue(scrollBar->value() - delta);
}

void ScrollAndZoomView::enableDragging(const QPoint &pos)
{
mIsDragging = true;
mLastPosition = pos;
setDragCursorEnabled(true);
}

void ScrollAndZoomView::disableDragging()
{
mIsDragging = false;
mLastPosition = {};
setDragCursorEnabled(false);
}

void ScrollAndZoomView::setDragCursorEnabled(bool enabled) const
{
enabled ? QGuiApplication::setOverrideCursor(Qt::SizeAllCursor) : QGuiApplication::restoreOverrideCursor();
}

} // namespace kImageAnnotator
Loading

0 comments on commit 6c0ae04

Please sign in to comment.