File indexing completed on 2024-05-26 16:15:57

0001 /* This file is part of the KDE project
0002  * Copyright (c) 2012 Boudewijn Rempt <boud@kogmbh.com>
0003  * Copyright (c) 2012 C. Boemann <cbo@boemann.dk>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 #include "KoTextRangeManager.h"
0021 
0022 #include "KoAnnotation.h"
0023 #include "KoBookmark.h"
0024 
0025 #include "TextDebug.h"
0026 
0027 KoTextRangeManager::KoTextRangeManager(QObject *parent)
0028     : QObject(parent)
0029 {
0030 }
0031 
0032 KoTextRangeManager::~KoTextRangeManager()
0033 {
0034 }
0035 
0036 void KoTextRangeManager::insert(KoTextRange *textRange)
0037 {
0038     if (!textRange) {
0039         return;
0040     }
0041 
0042 
0043     if (m_textRanges.contains(textRange)) {
0044         return;
0045     }
0046 
0047     if (m_deletedTextRanges.contains(textRange)) {
0048         m_deletedTextRanges.remove(textRange);
0049         textRange->restore();
0050     } else {
0051         textRange->setManager(this);
0052     }
0053 
0054     KoBookmark *bookmark = dynamic_cast<KoBookmark *>(textRange);
0055     if (bookmark) {
0056         m_bookmarkManager.insert(bookmark->name(), bookmark);
0057     }
0058     else {
0059         KoAnnotation *annotation = dynamic_cast<KoAnnotation *>(textRange);
0060         if (annotation) {
0061             m_annotationManager.insert(annotation->name(), annotation);
0062         }
0063     }
0064     m_textRanges.insert(textRange);
0065 }
0066 
0067 void KoTextRangeManager::remove(KoTextRange *textRange)
0068 {
0069     if (!textRange) {
0070         return;
0071     }
0072 
0073     KoBookmark *bookmark = dynamic_cast<KoBookmark *>(textRange);
0074     if (bookmark) {
0075         m_bookmarkManager.remove(bookmark->name());
0076     }
0077     else {
0078         KoAnnotation *annotation = dynamic_cast<KoAnnotation *>(textRange);
0079         if (annotation) {
0080             m_annotationManager.remove(annotation->name());
0081         }
0082     }
0083 
0084     m_textRanges.remove(textRange);
0085     m_deletedTextRanges.insert(textRange);
0086     textRange->snapshot();
0087 }
0088 
0089 const KoBookmarkManager *KoTextRangeManager::bookmarkManager() const
0090 {
0091     return &m_bookmarkManager;
0092 }
0093 
0094 const KoAnnotationManager *KoTextRangeManager::annotationManager() const
0095 {
0096     return &m_annotationManager;
0097 }
0098 
0099 QList<KoTextRange *> KoTextRangeManager::textRanges() const
0100 {
0101     return m_textRanges.values();
0102 }
0103 
0104 
0105 QHash<int, KoTextRange *> KoTextRangeManager::textRangesChangingWithin(const QTextDocument *doc, int first, int last, int matchFirst, int matchLast) const
0106 {
0107     QHash<int, KoTextRange *> ranges;
0108     foreach (KoTextRange *range, m_textRanges) {
0109         if (range->document() != doc) {
0110             continue;
0111         }
0112         if (!range->hasRange()) {
0113             if (range->rangeStart() >= first && range->rangeStart() <= last) {
0114                 ranges.insertMulti(range->rangeStart(), range);
0115             }
0116         } else {
0117             if (range->rangeStart() >= first && range->rangeStart() <= last) {
0118                 if (matchLast == -1 || range->rangeEnd() <= matchLast) {
0119                     if (range->rangeEnd() >= matchFirst) {
0120                         ranges.insertMulti(range->rangeStart(), range);
0121                     }
0122                 }
0123             }
0124             if (range->rangeEnd() >= first && range->rangeEnd() <= last) {
0125                 if (matchLast == -1 || range->rangeStart() <= matchLast) {
0126                     if (range->rangeStart() >= matchFirst) {
0127                         ranges.insertMulti(range->rangeEnd(), range);
0128                     }
0129                 }
0130             }
0131             if (range->rangeStart() >= first && range->rangeStart() <= last) {
0132                 if (matchLast == -1 || range->rangeEnd() >= matchLast) {
0133                     if (range->rangeEnd() >= matchFirst) {
0134                         ranges.insert(range->rangeStart(), range);
0135                     }
0136                 }
0137             }
0138         }
0139     }
0140     return ranges;
0141 }