1 /* This file is part of the KDE project
2 * Copyright (c) 2012 Boudewijn Rempt <boud@valdyas.org>
3 * Copyright (c) 2012 C. Boemann <cbo@boemann.dk>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20 #include "KoTextRangeManager.h"
21
22 #include "KoAnnotation.h"
23 #include "KoBookmark.h"
24
25 #include "TextDebug.h"
26
KoTextRangeManager(QObject * parent)27 KoTextRangeManager::KoTextRangeManager(QObject *parent)
28 : QObject(parent)
29 {
30 }
31
~KoTextRangeManager()32 KoTextRangeManager::~KoTextRangeManager()
33 {
34 }
35
insert(KoTextRange * textRange)36 void KoTextRangeManager::insert(KoTextRange *textRange)
37 {
38 if (!textRange) {
39 return;
40 }
41
42
43 if (m_textRanges.contains(textRange)) {
44 return;
45 }
46
47 if (m_deletedTextRanges.contains(textRange)) {
48 m_deletedTextRanges.remove(textRange);
49 textRange->restore();
50 } else {
51 textRange->setManager(this);
52 }
53
54 KoBookmark *bookmark = dynamic_cast<KoBookmark *>(textRange);
55 if (bookmark) {
56 m_bookmarkManager.insert(bookmark->name(), bookmark);
57 }
58 else {
59 KoAnnotation *annotation = dynamic_cast<KoAnnotation *>(textRange);
60 if (annotation) {
61 m_annotationManager.insert(annotation->name(), annotation);
62 }
63 }
64 m_textRanges.insert(textRange);
65 }
66
remove(KoTextRange * textRange)67 void KoTextRangeManager::remove(KoTextRange *textRange)
68 {
69 if (!textRange) {
70 return;
71 }
72
73 KoBookmark *bookmark = dynamic_cast<KoBookmark *>(textRange);
74 if (bookmark) {
75 m_bookmarkManager.remove(bookmark->name());
76 }
77 else {
78 KoAnnotation *annotation = dynamic_cast<KoAnnotation *>(textRange);
79 if (annotation) {
80 m_annotationManager.remove(annotation->name());
81 }
82 }
83
84 m_textRanges.remove(textRange);
85 m_deletedTextRanges.insert(textRange);
86 textRange->snapshot();
87 }
88
bookmarkManager() const89 const KoBookmarkManager *KoTextRangeManager::bookmarkManager() const
90 {
91 return &m_bookmarkManager;
92 }
93
annotationManager() const94 const KoAnnotationManager *KoTextRangeManager::annotationManager() const
95 {
96 return &m_annotationManager;
97 }
98
textRanges() const99 QList<KoTextRange *> KoTextRangeManager::textRanges() const
100 {
101 return m_textRanges.values();
102 }
103
104
textRangesChangingWithin(const QTextDocument * doc,int first,int last,int matchFirst,int matchLast) const105 QHash<int, KoTextRange *> KoTextRangeManager::textRangesChangingWithin(const QTextDocument *doc, int first, int last, int matchFirst, int matchLast) const
106 {
107 QHash<int, KoTextRange *> ranges;
108 foreach (KoTextRange *range, m_textRanges) {
109 if (range->document() != doc) {
110 continue;
111 }
112 if (!range->hasRange()) {
113 if (range->rangeStart() >= first && range->rangeStart() <= last) {
114 ranges.insertMulti(range->rangeStart(), range);
115 }
116 } else {
117 if (range->rangeStart() >= first && range->rangeStart() <= last) {
118 if (matchLast == -1 || range->rangeEnd() <= matchLast) {
119 if (range->rangeEnd() >= matchFirst) {
120 ranges.insertMulti(range->rangeStart(), range);
121 }
122 }
123 }
124 if (range->rangeEnd() >= first && range->rangeEnd() <= last) {
125 if (matchLast == -1 || range->rangeStart() <= matchLast) {
126 if (range->rangeStart() >= matchFirst) {
127 ranges.insertMulti(range->rangeEnd(), range);
128 }
129 }
130 }
131 if (range->rangeStart() >= first && range->rangeStart() <= last) {
132 if (matchLast == -1 || range->rangeEnd() >= matchLast) {
133 if (range->rangeEnd() >= matchFirst) {
134 ranges.insert(range->rangeStart(), range);
135 }
136 }
137 }
138 }
139 }
140 return ranges;
141 }
142