File indexing completed on 2024-05-19 16:08:32

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (c) 2011 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 "Find.h"
0022 
0023 #include <QApplication>
0024 
0025 #include <KoFindOptionSet.h>
0026 #include <KoFindOption.h>
0027 
0028 #include "Sheet.h"
0029 #include "Value.h"
0030 #include "ValueStorage.h"
0031 #include "ui/SheetView.h"
0032 
0033 using namespace Calligra::Sheets;
0034 
0035 class Q_DECL_HIDDEN Find::Private
0036 {
0037 public:
0038     Private() : currentSheet(0), currentSheetView(0) { }
0039     
0040     Sheet *currentSheet;
0041     SheetView *currentSheetView;
0042 };
0043 
0044 Find::Find(QObject *parent)
0045     : KoFindBase(parent), d(new Private)
0046 {
0047     KoFindOptionSet *options = new KoFindOptionSet();
0048     options->addOption("caseSensitive", i18n("Case Sensitive"), i18n("Match cases when searching"), QVariant::fromValue<bool>(false));
0049     setOptions(options);
0050     
0051     connect(this, SIGNAL(matchFound(KoFindMatch)), SLOT(setActiveMatch(KoFindMatch)));
0052 }
0053 
0054 void Find::setCurrentSheet( Sheet* sheet, SheetView* view)
0055 {
0056     if(d->currentSheetView) {
0057         clearMatches();
0058     }
0059     
0060     d->currentSheet = sheet;
0061     d->currentSheetView = view;
0062 }
0063 
0064 void Find::replaceImplementation(const KoFindMatch &match, const QVariant &value)
0065 {
0066     //No implementation yet.
0067     Q_UNUSED(match);
0068     Q_UNUSED(value);
0069 }
0070 
0071 void Find::findImplementation(const QString &pattern, KoFindBase::KoFindMatchList &matchList)
0072 {
0073     //int row = 1;
0074     //int column = 1;
0075 
0076     const ValueStorage *values = d->currentSheet->valueStorage();
0077     Qt::CaseSensitivity sensitivity = options()->option("caseSensitive")->value().toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
0078     for(int i = 0; i < values->count(); ++i) {
0079         Value val = values->data(i);
0080         if(val.isString() && val.asString().contains(pattern, sensitivity)) {
0081             KoFindMatch match;
0082             match.setContainer(QVariant::fromValue(d->currentSheet));
0083             Cell cell(d->currentSheet, values->col(i), values->row(i));
0084             match.setLocation(QVariant::fromValue(cell));
0085             matchList.append(match);
0086             d->currentSheetView->setHighlighted(cell.cellPosition(), true);
0087         }
0088     }
0089 }
0090 
0091 void Find::clearMatches()
0092 {
0093     KoFindMatchList list = matches();
0094     foreach(const KoFindMatch &match, list) {
0095         d->currentSheetView->setHighlighted(match.location().value<Cell>().cellPosition(), false);
0096     }
0097 }
0098 
0099 void Find::setActiveMatch ( const KoFindMatch& match )
0100 {
0101     d->currentSheetView->setActiveHighlight(match.location().value<Cell>().cellPosition());
0102 }