1 /*
2  * Copyright (C) 2018 Damir Porobic <damir.porobic@gmx.com>
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 "AbstractAnnotationRect.h"
21 
22 namespace kImageAnnotator {
23 
AbstractAnnotationRect(const QPointF & startPosition,const PropertiesPtr & properties)24 AbstractAnnotationRect::AbstractAnnotationRect(const QPointF &startPosition, const PropertiesPtr &properties) :
25 	AbstractAnnotationItem(properties),
26 	mRect(new QRectF())
27 {
28 	mRect->setTopLeft(startPosition);
29 	mRect->setBottomRight(startPosition);
30 }
31 
AbstractAnnotationRect(const AbstractAnnotationRect & other)32 AbstractAnnotationRect::AbstractAnnotationRect(const AbstractAnnotationRect &other) :
33 	AbstractAnnotationItem(other),
34 	mRect(new QRectF(*other.mRect))
35 {
36 }
37 
~AbstractAnnotationRect()38 AbstractAnnotationRect::~AbstractAnnotationRect()
39 {
40 	delete mRect;
41 }
42 
addPoint(const QPointF & position,bool modified)43 void AbstractAnnotationRect::addPoint(const QPointF &position, bool modified)
44 {
45 	prepareGeometryChange();
46 	mRect->setBottomRight(position);
47 	makeSymmetric(modified);
48 	updateShape();
49 }
50 
setPosition(const QPointF & newPosition)51 void AbstractAnnotationRect::setPosition(const QPointF &newPosition)
52 {
53 	prepareGeometryChange();
54 	mRect->translate(newPosition - position());
55 	updateShape();
56 }
57 
rect() const58 QRectF AbstractAnnotationRect::rect() const
59 {
60 	return *mRect;
61 }
62 
setPointAt(const QPointF & point,int index,bool keepAspectRatio)63 void AbstractAnnotationRect::setPointAt(const QPointF &point, int index, bool keepAspectRatio)
64 {
65 	prepareGeometryChange();
66 	auto newRect = ShapeHelper::setRectPointAtIndex(*mRect, index, point, keepAspectRatio);
67 	mRect->setRect(newRect.x(), newRect.y(), newRect.width(), newRect.height());
68 	updateShape();
69 }
70 
pointAt(int index) const71 QPointF AbstractAnnotationRect::pointAt(int index) const
72 {
73 	return ShapeHelper::rectPointAtIndex(*mRect, index);
74 }
75 
scale(qreal sx,qreal sy)76 void AbstractAnnotationRect::scale(qreal sx, qreal sy)
77 {
78 	prepareGeometryChange();
79 	QTransform transform;
80 	transform.scale(sx, sy);
81 	auto scaledRect = transform.mapRect(*mRect);
82 	mRect->setRect(scaledRect.x(), scaledRect.y(), scaledRect.width(), scaledRect.height());
83 	updateShape();
84 }
85 
makeSymmetric(bool enabled)86 void AbstractAnnotationRect::makeSymmetric(bool enabled)
87 {
88 	if (enabled) {
89 		mRect->setHeight(MathHelper::smallerValue(mRect->height(), mRect->width()));
90 		mRect->setWidth(MathHelper::smallerValue(mRect->width(), mRect->height()));
91 	}
92 }
93 
94 } // namespace kImageAnnotator
95