Warning, file /office/calligra/libs/main/KoFindStyle.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  *
0003  * Copyright (c) 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
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 
0021 #include "KoFindStyle.h"
0022 #include "KoFindOptionSet.h"
0023 #include "KoFindOption.h"
0024 
0025 #include <KoParagraphStyle.h>
0026 #include <KoCharacterStyle.h>
0027 #include <KoTextDocument.h>
0028 
0029 #include <QTextDocument>
0030 #include <QTextBlock>
0031 #include <QTextCursor>
0032 #include <QAbstractTextDocumentLayout>
0033 
0034 Q_DECLARE_METATYPE(QTextDocument *)
0035 Q_DECLARE_METATYPE(QTextCursor)
0036 
0037 class Q_DECL_HIDDEN KoFindStyle::Private
0038 {
0039 public:
0040     QList<QTextDocument*> documents;
0041     QHash<QTextDocument*, QVector<QAbstractTextDocumentLayout::Selection> > selections;
0042 
0043     static QTextCharFormat highlightFormat;
0044     void updateSelections();
0045 };
0046 
0047 QTextCharFormat KoFindStyle::Private::highlightFormat;
0048 
0049 KoFindStyle::KoFindStyle(QObject* parent)
0050     : KoFindBase(parent), d(new Private)
0051 {
0052     KoFindOptionSet *options = new KoFindOptionSet();
0053     options->addOption("paragraphStyle", "Paragraph Style", QString(), QVariant::fromValue<int>(0));
0054     options->addOption("characterStyle", "Character Style", QString(), QVariant::fromValue<int>(0));
0055     setOptions(options);
0056 
0057     d->highlightFormat.setBackground(Qt::yellow);
0058 }
0059 
0060 KoFindStyle::~KoFindStyle()
0061 {
0062     delete d;
0063 }
0064 
0065 QList< QTextDocument* > KoFindStyle::documents() const
0066 {
0067     return d->documents;
0068 }
0069 
0070 void KoFindStyle::setDocuments(const QList< QTextDocument* >& list)
0071 {
0072     clearMatches();
0073     d->documents = list;
0074 }
0075 
0076 void KoFindStyle::clearMatches()
0077 {
0078     d->selections.clear();
0079     foreach(QTextDocument* doc, d->documents) {
0080         d->selections.insert(doc, QVector<QAbstractTextDocumentLayout::Selection>());
0081     }
0082     d->updateSelections();
0083 }
0084 
0085 void KoFindStyle::replaceImplementation(const KoFindMatch& /*match*/, const QVariant& /*value*/)
0086 {
0087 
0088 }
0089 
0090 void KoFindStyle::findImplementation(const QString& /*pattern*/, KoFindBase::KoFindMatchList& matchList)
0091 {
0092     int charStyle = options()->option("characterStyle")->value().toInt();
0093     int parStyle = options()->option("paragraphStyle")->value().toInt();
0094 
0095     foreach(QTextDocument *document, d->documents) {
0096         QTextBlock block = document->firstBlock();
0097         QVector<QAbstractTextDocumentLayout::Selection> selections;
0098         while(block.isValid()) {
0099             if(block.blockFormat().intProperty(KoParagraphStyle::StyleId) == parStyle) {
0100                 for(QTextBlock::iterator itr = block.begin(); itr != block.end(); ++itr) {
0101                     if(itr.fragment().charFormat().intProperty(KoCharacterStyle::StyleId) == charStyle) {
0102                         QTextCursor cursor(document);
0103                         cursor.setPosition(itr.fragment().position());
0104                         cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, itr.fragment().length());
0105                         matchList.append(KoFindMatch(QVariant::fromValue(document), QVariant::fromValue(cursor)));
0106 
0107                         QAbstractTextDocumentLayout::Selection selection;
0108                         selection.cursor = cursor;
0109                         selection.format = d->highlightFormat;
0110                         selections.append(selection);
0111                     }
0112                 }
0113             }
0114             block = block.next();
0115         }
0116         d->selections.insert(document, selections);
0117     }
0118 
0119     d->updateSelections();
0120 }
0121 
0122 void KoFindStyle::Private::updateSelections()
0123 {
0124     QHash< QTextDocument*, QVector<QAbstractTextDocumentLayout::Selection> >::iterator itr;
0125     for(itr = selections.begin(); itr != selections.end(); ++itr) {
0126         KoTextDocument doc(itr.key());
0127         doc.setSelections(itr.value());
0128     }
0129 }