Warning, file /office/calligra/libs/text/KoTextEditingPlugin.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 "KoTextEditingPlugin.h" 0021 0022 #include <QTextDocument> 0023 #include <QTextBlock> 0024 #include <QTextCursor> 0025 0026 #include "TextDebug.h" 0027 0028 class Q_DECL_HIDDEN KoTextEditingPlugin::Private 0029 { 0030 public: 0031 QHash<QString, QAction*> actionCollection; 0032 }; 0033 0034 KoTextEditingPlugin::KoTextEditingPlugin() 0035 : d(new Private()) 0036 { 0037 } 0038 0039 KoTextEditingPlugin::~KoTextEditingPlugin() 0040 { 0041 delete d; 0042 } 0043 0044 void KoTextEditingPlugin::selectWord(QTextCursor &cursor, int cursorPosition) const 0045 { 0046 cursor.setPosition(cursorPosition); 0047 QTextBlock block = cursor.block(); 0048 cursor.setPosition(block.position()); 0049 cursorPosition -= block.position(); 0050 const QString string = block.text(); 0051 int pos = 0; 0052 bool space = false; 0053 QString::ConstIterator iter = string.begin(); 0054 while (iter != string.end()) { 0055 if (iter->isSpace()) { 0056 if (space) ;// double spaces belong to the previous word 0057 else if (pos < cursorPosition) 0058 cursor.setPosition(pos + block.position() + 1); // +1 because we don't want to set it on the space itself 0059 else 0060 space = true; 0061 } else if (space) 0062 break; 0063 pos++; 0064 ++iter; 0065 } 0066 cursor.setPosition(pos + block.position(), QTextCursor::KeepAnchor); 0067 } 0068 0069 QString KoTextEditingPlugin::paragraph(QTextDocument *document, int cursorPosition) const 0070 { 0071 QTextBlock block = document->findBlock(cursorPosition); 0072 return block.text(); 0073 } 0074 0075 void KoTextEditingPlugin::addAction(const QString &name, QAction *action) 0076 { 0077 d->actionCollection.insert(name, action); 0078 } 0079 0080 void KoTextEditingPlugin::checkSection(QTextDocument *document, int startPosition, int endPosition) 0081 { 0082 QTextBlock block = document->findBlock(startPosition); 0083 int pos = block.position(); 0084 while (true) { 0085 if (!block.contains(startPosition - 1) && !block.contains(endPosition + 1)) // only parags that are completely in 0086 finishedParagraph(document, block.position()); 0087 0088 const QString text = block.text(); 0089 bool space = true; 0090 QString::ConstIterator iter = text.begin(); 0091 while (pos < endPosition && iter != text.end()) { 0092 bool isSpace = iter->isSpace(); 0093 if (pos >= startPosition && space && !isSpace) // for each word, call finishedWord 0094 finishedWord(document, pos); 0095 else if (!isSpace && pos == startPosition) 0096 finishedWord(document, startPosition); 0097 space = isSpace; 0098 pos++; 0099 iter++; 0100 } 0101 0102 if (!(block.isValid() && block.position() + block.length() < endPosition)) 0103 break; 0104 block = block.next(); 0105 } 0106 } 0107 0108 QHash<QString, QAction*> KoTextEditingPlugin::actions() const 0109 { 0110 return d->actionCollection; 0111 } 0112 0113 void KoTextEditingPlugin::setCurrentCursorPosition(QTextDocument *document, int cursorPosition) 0114 { 0115 Q_UNUSED(cursorPosition); 0116 Q_UNUSED(document); 0117 }