Warning, file /office/calligra/libs/text/KoTextLocator.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) 2007 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 "KoTextLocator.h"
0021 
0022 #include "KoTextReference.h"
0023 #include "KoTextPage.h"
0024 #include "styles/KoListStyle.h"
0025 
0026 #include <KoShape.h>
0027 
0028 #include "TextDebug.h"
0029 #include <QTextDocument>
0030 #include <QTextList>
0031 #include <QTextInlineObject>
0032 #include <QTextBlock>
0033 #include <QTextCursor>
0034 
0035 // Include Q_UNSUSED classes, for building on Windows
0036 #include <KoShapeLoadingContext.h>
0037 #include <KoShapeSavingContext.h>
0038 
0039 class Q_DECL_HIDDEN KoTextLocator::Private
0040 {
0041 public:
0042     Private(KoTextLocator *q) : q(q), document(0), dirty(false), cursorPosition(0), chapterPosition(-1), pageNumber(0) { }
0043     void update() {
0044         if (dirty == false)
0045             return;
0046         dirty = false;
0047         chapterPosition = -1;
0048 
0049         int pageTmp = pageNumber, chapterTmp = chapterPosition;
0050         if (document == 0)
0051             return;
0052 
0053         QTextBlock block = document->findBlock(cursorPosition);
0054         while (block.isValid()) {
0055             QTextList *list = block.textList();
0056             if (list) {
0057                 QTextListFormat lf = list->format();
0058                 int level = lf.intProperty(KoListStyle::Level);
0059                 if (level == 1) {
0060                     chapterPosition = block.position();
0061                     break;
0062                 }
0063             }
0064             block = block.previous();
0065         }
0066 /*
0067         KoShape *shape = shapeForPosition(document, cursorPosition);
0068         if (shape == 0)
0069             pageNumber = -1;
0070         else {
0071             KoTextShapeData *data = static_cast<KoTextShapeData*>(shape->userData());
0072             KoTextPage* page = data->page();
0073             pageNumber = page->pageNumber();
0074         }
0075 */        if (pageTmp != pageNumber || chapterTmp != chapterPosition) {
0076             foreach(KoTextReference* reference, listeners)
0077                 reference->variableMoved(0, 0);
0078         }
0079     }
0080 
0081     KoTextLocator *q;
0082     const QTextDocument *document;
0083     bool dirty;
0084     int cursorPosition;
0085     int chapterPosition;
0086     int pageNumber;
0087 
0088     QList<KoTextReference*> listeners;
0089 };
0090 
0091 
0092 KoTextLocator::KoTextLocator()
0093         : KoInlineObject(false),
0094         d(new Private(this))
0095 {
0096 }
0097 
0098 KoTextLocator::~KoTextLocator()
0099 {
0100     delete d;
0101 }
0102 
0103 void KoTextLocator::updatePosition(const QTextDocument *document, int posInDocument, const QTextCharFormat &format)
0104 {
0105     Q_UNUSED(format);
0106     if (d->document != document || d->cursorPosition != posInDocument) {
0107         d->dirty = true;
0108         d->document = document;
0109         d->cursorPosition = posInDocument;
0110 //debugText <<"KoTextLocator page:" << pageNumber() <<", chapter:" << chapter() <<", '" << word() <<"'";
0111     }
0112 }
0113 
0114 void KoTextLocator::resize(const QTextDocument *document, QTextInlineObject &object, int posInDocument, const QTextCharFormat &format, QPaintDevice *pd)
0115 {
0116     Q_UNUSED(document);
0117     Q_UNUSED(posInDocument);
0118     Q_UNUSED(format);
0119     Q_UNUSED(pd);
0120     object.setWidth(0);
0121     object.setAscent(0);
0122     object.setDescent(0);
0123 }
0124 
0125 void KoTextLocator::paint(QPainter &, QPaintDevice *, const QTextDocument *, const QRectF &, const QTextInlineObject &, int , const QTextCharFormat &)
0126 {
0127     // nothing to paint.
0128 }
0129 
0130 QString KoTextLocator::chapter() const
0131 {
0132     d->update();
0133     if (d->chapterPosition < 0)
0134         return QString();
0135     QTextBlock block = d->document->findBlock(d->chapterPosition);
0136     return block.text().remove(QChar::ObjectReplacementCharacter);
0137 }
0138 
0139 int KoTextLocator::pageNumber() const
0140 {
0141     d->update();
0142     return d->pageNumber;
0143 }
0144 
0145 int KoTextLocator::indexPosition() const
0146 {
0147     return d->cursorPosition;
0148 }
0149 
0150 QString KoTextLocator::word() const
0151 {
0152     if (d->document == 0) // layout never started
0153         return QString();
0154     QTextCursor cursor(const_cast<QTextDocument*>(d->document));
0155     cursor.setPosition(d->cursorPosition);
0156     cursor.movePosition(QTextCursor::NextWord);
0157     cursor.movePosition(QTextCursor::WordLeft, QTextCursor::KeepAnchor);
0158     return cursor.selectedText().trimmed().remove(QChar::ObjectReplacementCharacter);
0159 }
0160 
0161 void KoTextLocator::addListener(KoTextReference *reference)
0162 {
0163     d->listeners.append(reference);
0164 }
0165 
0166 void KoTextLocator::removeListener(KoTextReference *reference)
0167 {
0168     d->listeners.removeAll(reference);
0169 }
0170 
0171 bool KoTextLocator::loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context)
0172 {
0173     Q_UNUSED(element);
0174     Q_UNUSED(context);
0175     // TODO
0176     return false;
0177 }
0178 
0179 void KoTextLocator::saveOdf(KoShapeSavingContext &context)
0180 {
0181     Q_UNUSED(context);
0182     // TODO
0183 }