1 /*
2 * Copyright (C) 2020 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 "AnnotationItemEditor.h"
21
22 namespace kImageAnnotator {
23
AnnotationItemEditor()24 AnnotationItemEditor::AnnotationItemEditor()
25 : mCurrentEditItem(nullptr)
26 {
27 }
28
handleEditAt(const QPointF & pos,QList<AbstractAnnotationItem * > * items)29 void AnnotationItemEditor::handleEditAt(const QPointF &pos, QList<AbstractAnnotationItem *> *items)
30 {
31 mCurrentEditItem = findEditableItemAt(pos, items);
32 if (mCurrentEditItem != nullptr) {
33 mCurrentEditItem->enableEditing();
34 }
35 }
36
isEditing() const37 bool AnnotationItemEditor::isEditing() const
38 {
39 return mCurrentEditItem != nullptr;
40 }
41
clear()42 void AnnotationItemEditor::clear()
43 {
44 mCurrentEditItem = nullptr;
45 }
46
findEditableItemAt(const QPointF & position,QList<AbstractAnnotationItem * > * items)47 EditableItem *AnnotationItemEditor::findEditableItemAt(const QPointF &position, QList<AbstractAnnotationItem *> *items)
48 {
49 QRectF rect(position - QPointF(2, 2), QSize(4, 4));
50
51 for (auto item : *items) {
52 auto editableItem = dynamic_cast<EditableItem *>(item);
53 if (editableItem != nullptr && item->intersects(rect)) {
54 return editableItem;
55 }
56 }
57
58 return nullptr;
59 }
60
61 } // namespace kImageAnnotator
62