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 "AbstractItemResizeHandles.h"
21 
22 namespace kImageAnnotator {
23 
AbstractItemResizeHandles()24 AbstractItemResizeHandles::AbstractItemResizeHandles()
25 {
26 }
27 
indexOfHandleAt(const QPointF & pos) const28 int AbstractItemResizeHandles::indexOfHandleAt(const QPointF &pos) const
29 {
30     for (auto handle : mHandles) {
31         if (handle.contains(pos)) {
32             return mHandles.indexOf(handle);
33         }
34     }
35     return -1;
36 }
37 
indexOfHandleWithAnchorAt(const QPointF & pos) const38 int AbstractItemResizeHandles::indexOfHandleWithAnchorAt(const QPointF &pos) const
39 {
40     for (auto handle : mHandles) {
41         if (handle.anchor() == pos) {
42             return mHandles.indexOf(handle);
43         }
44     }
45     return -1;
46 }
47 
handle(int index) const48 ResizeHandle AbstractItemResizeHandles::handle(int index) const
49 {
50     if (index < 0 || index >= mHandles.count()) {
51         return ResizeHandle();
52     }
53 
54     return mHandles[index];
55 }
56 
handles() const57 QList<ResizeHandle> AbstractItemResizeHandles::handles() const
58 {
59     return mHandles;
60 }
61 
cursorForPos(const QPointF & pos) const62 Qt::CursorShape AbstractItemResizeHandles::cursorForPos(const QPointF &pos) const
63 {
64     auto index = indexOfHandleAt(pos);
65     return cursorForHandle(index);
66 }
67 
cursorForHandle(int index) const68 Qt::CursorShape AbstractItemResizeHandles::cursorForHandle(int index) const
69 {
70     if (index == -1 || mCursors.isEmpty()) {
71         return CursorHelper::defaultCursor();
72     }
73 
74     return mCursors[index];
75 }
76 
initHandles(int count,double zoomValue)77 void AbstractItemResizeHandles::initHandles(int count, double zoomValue)
78 {
79     mHandles.clear();
80     for (auto i = 0; i < count; i++) {
81         mHandles.append(ResizeHandle(zoomValue));
82     }
83 }
84 
handleSize() const85 double AbstractItemResizeHandles::handleSize() const
86 {
87     return mHandles.empty() ? 0.0 : mHandles.first().handleSize();
88 }
89 
applyZoomValue(double value)90 void AbstractItemResizeHandles::applyZoomValue(double value)
91 {
92     for (auto &handle : mHandles) {
93         handle.applyZoomValue(value);
94     }
95 }
96 
97 } // namespace kImageAnnotator
98