Skip to content

Commit 3783c0c

Browse files
committed
Add canvas modifier for changing canvas size #92
1 parent c5ecabc commit 3783c0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1728
-589
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* New: Add a new tool for creating resizable movable duplicates of regions. ([#131](https://github.com/ksnip/kImageAnnotator/issues/131))
66
* New: Add support for hiding annotation settings panel. ([#182](https://github.com/ksnip/kImageAnnotator/issues/182))
77
* New: Add config option for numbering tool to only set next number. ([#42](https://github.com/ksnip/kImageAnnotator/issues/42))
8+
* New: Allow manually changing canvas size. ([#92](https://github.com/ksnip/kImageAnnotator/issues/92))
89

910
## Release 0.4.1
1011
* Fixed: Brazilian Portuguese translation not loaded. ([#176](https://github.com/ksnip/kImageAnnotator/issues/176))

example/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ int main(int argc, char **argv)
4343
auto annotationAction = new QAction(QLatin1Literal("Annotation"), &mainWindow);
4444
auto cropAction = new QAction(QLatin1Literal("Crop"), &mainWindow);
4545
auto scaleAction = new QAction(QLatin1Literal("Scale"), &mainWindow);
46+
auto modifyCanvasAction = new QAction(QLatin1Literal("Modify Canvas"), &mainWindow);
4647
mainWindow.connect(annotationAction, &QAction::triggered, kImageAnnotator, &KImageAnnotator::showAnnotator);
4748
mainWindow.connect(cropAction, &QAction::triggered, kImageAnnotator, &KImageAnnotator::showCropper);
4849
mainWindow.connect(scaleAction, &QAction::triggered, kImageAnnotator, &KImageAnnotator::showScaler);
50+
mainWindow.connect(modifyCanvasAction, &QAction::triggered, kImageAnnotator, &KImageAnnotator::showCanvasModifier);
4951
menu->addAction(annotationAction);
5052
menu->addAction(cropAction);
5153
menu->addAction(scaleAction);
54+
menu->addAction(modifyCanvasAction);
5255
menuBar->addMenu(menu);
5356
mainWindow.show();
5457
mainWindow.setMinimumSize(kImageAnnotator->sizeHint());

include/kImageAnnotator/KImageAnnotator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Q_OBJECT
4747
void showAnnotator();
4848
void showCropper();
4949
void showScaler();
50+
void showCanvasModifier();
5051
void setSettingsCollapsed(bool isCollapsed);
5152

5253
public Q_SLOTS:

src/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ set(KIMAGEANNOTATOR_SRCS
1010
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/tabs/AnnotationTabWidget.cpp
1111
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/tabs/AnnotationTabContextMenu.cpp
1212
${CMAKE_CURRENT_SOURCE_DIR}/gui/annotator/tabs/AnnotationTabCloser.cpp
13+
${CMAKE_CURRENT_SOURCE_DIR}/gui/canvasModifier/ModifyCanvasView.cpp
14+
${CMAKE_CURRENT_SOURCE_DIR}/gui/canvasModifier/ModifyCanvasWidget.cpp
15+
${CMAKE_CURRENT_SOURCE_DIR}/gui/canvasModifier/ModifyCanvasSelectionRestrictor.cpp
1316
${CMAKE_CURRENT_SOURCE_DIR}/gui/cropper/CropWidget.cpp
1417
${CMAKE_CURRENT_SOURCE_DIR}/gui/cropper/CropView.cpp
15-
${CMAKE_CURRENT_SOURCE_DIR}/gui/cropper/CropSelectionHandler.cpp
1618
${CMAKE_CURRENT_SOURCE_DIR}/gui/cropper/CropSelectionRestrictor.cpp
17-
${CMAKE_CURRENT_SOURCE_DIR}/gui/cropper/CropHandles.cpp
18-
${CMAKE_CURRENT_SOURCE_DIR}/gui/cropper/CropSelectionMoveHelper.cpp
19+
${CMAKE_CURRENT_SOURCE_DIR}/gui/selection/SelectionHandler.cpp
20+
${CMAKE_CURRENT_SOURCE_DIR}/gui/selection/SelectionMoveHelper.cpp
21+
${CMAKE_CURRENT_SOURCE_DIR}/gui/selection/SelectionHandles.cpp
22+
${CMAKE_CURRENT_SOURCE_DIR}/gui/selection/BaseSelectionView.cpp
1923
${CMAKE_CURRENT_SOURCE_DIR}/gui/scaler/ScaleDialog.cpp
2024
${CMAKE_CURRENT_SOURCE_DIR}/gui/scaler/ScaleWidget.cpp
2125
${CMAKE_CURRENT_SOURCE_DIR}/gui/scaler/ScaleSizeHandler.cpp
@@ -88,6 +92,7 @@ set(KIMAGEANNOTATOR_SRCS
8892
${CMAKE_CURRENT_SOURCE_DIR}/annotations/undo/ScaleCommand.cpp
8993
${CMAKE_CURRENT_SOURCE_DIR}/annotations/undo/PasteCommand.cpp
9094
${CMAKE_CURRENT_SOURCE_DIR}/annotations/undo/ChangePropertiesCommand.cpp
95+
${CMAKE_CURRENT_SOURCE_DIR}/annotations/undo/ModifyCanvasCommand.cpp
9196
${CMAKE_CURRENT_SOURCE_DIR}/annotations/misc/ShadowEffect.cpp
9297
${CMAKE_CURRENT_SOURCE_DIR}/annotations/misc/ImageBlurrer.cpp
9398
${CMAKE_CURRENT_SOURCE_DIR}/annotations/misc/AnnotationItemClipboard.cpp

src/annotations/core/AnnotationArea.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ QImage AnnotationArea::image()
9999
}
100100

101101
mItemModifier->clear();
102-
103-
auto graphicsEffectRect = mImage->graphicsEffect()->boundingRect();
104-
setSceneRect(itemsBoundingRect().united(graphicsEffectRect)); // Cover all items
105-
102+
setSceneRect(canvasRect());
106103
auto scaleFactor = mDevicePixelRatioScaler->scaleFactor();
107104
QImage image(sceneRect().size().toSize() * scaleFactor, QImage::Format_ARGB32_Premultiplied);
108105
image.fill(Qt::white);
@@ -192,6 +189,26 @@ void AnnotationArea::imageEffectChanged(ImageEffects effect)
192189
mImage->setGraphicsEffect(graphicsEffect);
193190
}
194191

192+
void AnnotationArea::setCanvasRect(const QRectF &rect)
193+
{
194+
mCanvasRect = rect;
195+
}
196+
197+
QRectF AnnotationArea::canvasRect() const
198+
{
199+
if(mCanvasRect.isNull()) {
200+
auto graphicsEffectRect = mImage->graphicsEffect()->boundingRect();
201+
return itemsBoundingRect().united(graphicsEffectRect);
202+
} else {
203+
return mCanvasRect;
204+
}
205+
}
206+
207+
void AnnotationArea::modifyCanvas(const QRectF &canvasRect)
208+
{
209+
mUndoStack->push(new ModifyCanvasCommand(canvasRect, this));
210+
}
211+
195212
void AnnotationArea::update()
196213
{
197214
mItemModifier->updateSelection();

src/annotations/core/AnnotationArea.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "src/annotations/undo/ScaleCommand.h"
4646
#include "src/annotations/undo/PasteCommand.h"
4747
#include "src/annotations/undo/ChangePropertiesCommand.h"
48+
#include "src/annotations/undo/ModifyCanvasCommand.h"
4849
#include "src/annotations/core/ZoomValueProvider.h"
4950
#include "src/annotations/core/imageEffects/ImageEffectFactory.h"
5051

@@ -71,6 +72,9 @@ class AnnotationArea : public QGraphicsScene, public ISettingsListener
7172
void numberToolSeedChanged(int numberToolSeed) override;
7273
int numberToolSeed() const override;
7374
void imageEffectChanged(ImageEffects effect) override;
75+
void setCanvasRect(const QRectF &rect);
76+
QRectF canvasRect() const;
77+
void modifyCanvas(const QRectF &canvasRect);
7478

7579
public slots:
7680
virtual void update();
@@ -102,6 +106,7 @@ public slots:
102106
QAction *mRedoAction;
103107
IDevicePixelRatioScaler *mDevicePixelRatioScaler;
104108
KeyEventListener mKeyListener;
109+
QRectF mCanvasRect;
105110

106111
void addItemAtPosition(const QPointF& position);
107112
void addPointToCurrentItem(const QPointF& position);

src/annotations/undo/AddCommand.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ AddCommand::AddCommand(AbstractAnnotationItem *item, AnnotationArea *annotationA
2727
mAnnotationArea = annotationArea;
2828
}
2929

30-
AddCommand::~AddCommand()
31-
{
32-
}
33-
3430
void AddCommand::undo()
3531
{
3632
mAnnotationArea->removeAnnotationItem(mItem);

src/annotations/undo/AddCommand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AddCommand : public QUndoCommand
3333
{
3434
public:
3535
AddCommand(AbstractAnnotationItem *item, AnnotationArea *annotationArea);
36-
~AddCommand();
36+
~AddCommand() = default;
3737
virtual void undo() override;
3838
virtual void redo() override;
3939

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2020 Damir Porobic <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301, USA.
18+
*/
19+
20+
#include "ModifyCanvasCommand.h"
21+
22+
namespace kImageAnnotator {
23+
24+
25+
ModifyCanvasCommand::ModifyCanvasCommand(const QRectF &canvasRect, AnnotationArea *annotationArea) :
26+
mAnnotationArea(annotationArea),
27+
mNewCanvasRect(canvasRect),
28+
mOriginalCanvasRect(annotationArea->canvasRect())
29+
{
30+
31+
}
32+
33+
void ModifyCanvasCommand::undo()
34+
{
35+
mAnnotationArea->setCanvasRect(mOriginalCanvasRect);
36+
}
37+
38+
void ModifyCanvasCommand::redo()
39+
{
40+
mAnnotationArea->setCanvasRect(mNewCanvasRect);
41+
}
42+
43+
} // namespace kImageAnnotator
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2020 Damir Porobic <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301, USA.
18+
*/
19+
20+
#ifndef KIMAGEANNOTATOR_MODIFYCANVASCOMMAND_H
21+
#define KIMAGEANNOTATOR_MODIFYCANVASCOMMAND_H
22+
23+
#include <QUndoCommand>
24+
25+
#include "src/annotations/core/AnnotationArea.h"
26+
27+
namespace kImageAnnotator {
28+
29+
class ModifyCanvasCommand : public QUndoCommand
30+
{
31+
public:
32+
explicit ModifyCanvasCommand(const QRectF &canvasRect, AnnotationArea *annotationArea);
33+
~ModifyCanvasCommand() override = default;
34+
void undo() override;
35+
void redo() override;
36+
37+
private:
38+
AnnotationArea *mAnnotationArea;
39+
QRectF mNewCanvasRect;
40+
QRectF mOriginalCanvasRect;
41+
};
42+
43+
} // namespace kImageAnnotator
44+
45+
#endif //KIMAGEANNOTATOR_MODIFYCANVASCOMMAND_H

0 commit comments

Comments
 (0)