Warning, file /office/calligra/libs/text/KoTextRange.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006-2009 Thomas Zander <zander@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoTextRange.h"
0021 
0022 #include "KoTextRangeManager.h"
0023 #include "KoTextInlineRdf.h"
0024 
0025 #include "TextDebug.h"
0026 #include <QTextCursor>
0027 
0028 class KoTextRangePrivate
0029 {
0030 public:
0031     KoTextRangePrivate()
0032         : manager(0)
0033         , id(-1)
0034         , rdf(0)
0035         , positionOnlyMode(true)
0036     {
0037     }
0038     virtual ~KoTextRangePrivate();
0039 
0040     KoTextRangeManager *manager;
0041     int id;
0042     QTextCursor cursor;
0043     KoTextInlineRdf *rdf; //< A textrange might have RDF, we own it.
0044     bool positionOnlyMode;
0045     int snapAnchor;
0046     int snapPos;
0047 };
0048 
0049 KoTextRange::KoTextRange(const QTextCursor &cursor)
0050     : d_ptr(new KoTextRangePrivate)
0051 {
0052     d_ptr->cursor = cursor;
0053     d_ptr->cursor.setPosition(cursor.selectionStart());
0054     d_ptr->cursor.setKeepPositionOnInsert(true);
0055     if (cursor.hasSelection()) {
0056         setRangeEnd(cursor.selectionEnd());
0057     }
0058 }
0059 
0060 KoTextRangePrivate::~KoTextRangePrivate()
0061 {
0062     delete rdf;
0063 }
0064 
0065 
0066 KoTextRange::~KoTextRange()
0067 {
0068     if (d_ptr->manager) {
0069         d_ptr->manager->remove(this);
0070     }
0071     delete d_ptr;
0072     d_ptr = 0;
0073 }
0074 
0075 void KoTextRange::setManager(KoTextRangeManager *manager)
0076 {
0077     Q_D(KoTextRange);
0078     d->manager = manager;
0079 }
0080 
0081 KoTextRangeManager *KoTextRange::manager() const
0082 {
0083     Q_D(const KoTextRange);
0084     return d->manager;
0085 }
0086 
0087 QTextDocument *KoTextRange::document() const
0088 {
0089     Q_D(const KoTextRange);
0090     return d->cursor.document();
0091 }
0092 
0093 bool KoTextRange::positionOnlyMode() const
0094 {
0095     Q_D(const KoTextRange);
0096     return d->positionOnlyMode;
0097 }
0098 
0099 void KoTextRange::setPositionOnlyMode(bool b)
0100 {
0101     Q_D(KoTextRange);
0102     d->positionOnlyMode = b;
0103 }
0104 
0105 bool KoTextRange::hasRange() const
0106 {
0107     Q_D(const KoTextRange);
0108     return (!d->positionOnlyMode) && d->cursor.hasSelection();
0109 }
0110 
0111 int KoTextRange::rangeStart() const
0112 {
0113     Q_D(const KoTextRange);
0114     return d->positionOnlyMode ? d->cursor.position() : d->cursor.selectionStart();
0115 }
0116 
0117 int KoTextRange::rangeEnd() const
0118 {
0119     Q_D(const KoTextRange);
0120     return d->positionOnlyMode ? d->cursor.position() : d->cursor.selectionEnd();
0121 }
0122 
0123 void KoTextRange::setRangeStart(int position)
0124 {
0125     Q_D(KoTextRange);
0126     d->positionOnlyMode = true;
0127     d->cursor.setPosition(position);
0128 }
0129 
0130 void KoTextRange::setRangeEnd(int position)
0131 {
0132     Q_D(KoTextRange);
0133     d->positionOnlyMode = false;
0134     d->cursor.setPosition(d->cursor.selectionStart());
0135     d->cursor.setPosition(position, QTextCursor::KeepAnchor);
0136 }
0137 
0138 QString KoTextRange::text() const
0139 {
0140     Q_D(const KoTextRange);
0141     return d->positionOnlyMode ? QString() : d->cursor.selectedText();
0142 }
0143 
0144 void KoTextRange::setInlineRdf(KoTextInlineRdf* rdf)
0145 {
0146     Q_D(KoTextRange);
0147     d->rdf = rdf;
0148 }
0149 
0150 KoTextInlineRdf* KoTextRange::inlineRdf() const
0151 {
0152     Q_D(const KoTextRange);
0153     return d->rdf;
0154 }
0155 
0156 void KoTextRange::snapshot()
0157 {
0158     Q_D(KoTextRange);
0159     d->snapAnchor = d->cursor.anchor();
0160     d->snapPos = d->cursor.position();
0161 }
0162 
0163 void KoTextRange::restore()
0164 {
0165     Q_D(KoTextRange);
0166     d->cursor.setPosition(d->snapAnchor);
0167     d->cursor.setPosition(d->snapPos, QTextCursor::KeepAnchor);
0168 }