Warning, file /office/calligra/libs/main/KoFindBase.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) 2010 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 "KoFindBase.h"
0022 
0023 #include "KoFindOptionSet.h"
0024 
0025 class Q_DECL_HIDDEN KoFindBase::Private
0026 {
0027 public:
0028     Private() : currentMatch(0), options(0) { }
0029 
0030     KoFindMatchList matches;
0031     int currentMatch;
0032     KoFindOptionSet *options;
0033 };
0034 
0035 KoFindBase::KoFindBase(QObject *parent)
0036     : QObject(parent), d(new Private)
0037 {
0038 }
0039 
0040 KoFindBase::~KoFindBase()
0041 {
0042     delete d->options;
0043     delete d;
0044 }
0045 
0046 const KoFindBase::KoFindMatchList &KoFindBase::matches() const
0047 {
0048     return d->matches;
0049 }
0050 
0051 bool KoFindBase::hasMatches() const
0052 {
0053     return d->matches.count() > 0;
0054 }
0055 
0056 KoFindMatch KoFindBase::currentMatch() const
0057 {
0058     if (d->matches.count() > 0 && d->currentMatch < d->matches.count()) {
0059         return d->matches.at(d->currentMatch);
0060     }
0061     return KoFindMatch();
0062 }
0063 
0064 KoFindOptionSet *KoFindBase::options() const
0065 {
0066     return d->options;
0067 }
0068 
0069 void KoFindBase::setMatches(const KoFindBase::KoFindMatchList &matches)
0070 {
0071     d->matches = matches;
0072 }
0073 
0074 void KoFindBase::setCurrentMatch(int index)
0075 {
0076     d->currentMatch = index;
0077 }
0078 
0079 int KoFindBase::currentMatchIndex()
0080 {
0081     return d->currentMatch;
0082 }
0083 
0084 void KoFindBase::find(const QString &pattern)
0085 {
0086     clearMatches();
0087     d->matches.clear();
0088     findImplementation(pattern, d->matches);
0089 
0090     emit hasMatchesChanged(d->matches.count() > 0);
0091     if (d->matches.size() > 0) {
0092         if (d->currentMatch >= d->matches.size()) {
0093             d->currentMatch = 0;
0094         }
0095         emit matchFound(d->matches.at(d->currentMatch));
0096     } else {
0097         emit noMatchFound();
0098     }
0099 
0100     emit updateCanvas();
0101 }
0102 
0103 void KoFindBase::findNext()
0104 {
0105     if (d->matches.count() == 0) {
0106         return;
0107     }
0108 
0109     d->currentMatch = (d->currentMatch + 1) % d->matches.count();
0110     emit matchFound(d->matches.at(d->currentMatch));
0111 
0112     if (d->currentMatch == 0) {
0113         emit wrapAround(true);
0114     }
0115 
0116     emit updateCanvas();
0117 }
0118 
0119 void KoFindBase::findPrevious()
0120 {
0121     if (d->matches.count() == 0) {
0122         return;
0123     }
0124 
0125     d->currentMatch = (--d->currentMatch) >= 0 ? d->currentMatch : d->matches.count() - 1;
0126     emit matchFound(d->matches.at(d->currentMatch));
0127 
0128     if (d->currentMatch == d->matches.count() - 1) {
0129         emit wrapAround(false);
0130     }
0131 
0132     emit updateCanvas();
0133 }
0134 
0135 void KoFindBase::finished()
0136 {
0137     clearMatches();
0138     d->matches.clear();
0139     emit updateCanvas();
0140 }
0141 
0142 void KoFindBase::replaceCurrent(const QVariant &value)
0143 {
0144     if(d->matches.count() == 0) {
0145         return;
0146     }
0147 
0148     KoFindMatch match = d->matches.at(d->currentMatch);
0149     d->matches.removeAt(d->currentMatch);
0150     if (d->currentMatch < d->matches.count()) {
0151         replaceImplementation(match, value);
0152     }
0153 
0154     if(d->matches.count() > 0) {
0155         emit matchFound(d->matches.at(0));
0156     } else {
0157         emit noMatchFound();
0158     }
0159     emit updateCanvas();
0160 }
0161 
0162 void KoFindBase::replaceAll(const QVariant &value)
0163 {
0164     foreach(const KoFindMatch &match, d->matches) {
0165         replaceImplementation(match, value);
0166     }
0167 
0168     //Intentionally not using clearMatches since we should not clear
0169     //highlighting here.
0170     d->matches.clear();
0171     emit noMatchFound();
0172     emit updateCanvas();
0173 }
0174 
0175 void KoFindBase::clearMatches()
0176 {
0177     //Intentionally does nothing, only needs to be reimplemented when
0178     //something needs to be done before clearing the list of matches.
0179 }
0180 
0181 void KoFindBase::setOptions(KoFindOptionSet *newOptions)
0182 {
0183     delete d->options;
0184     d->options = newOptions;
0185     d->options->setParent(this);
0186 }