F:/KPlato/koffice/libs/kotext/styles/ChangeFollower.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; version 2.
00007  *
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Library General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Library General Public License
00014  * along with this library; see the file COPYING.LIB.  If not, write to
00015  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include "ChangeFollower.h"
00020 #include "KoCharacterStyle.h"
00021 #include "KoParagraphStyle.h"
00022 
00023 #include <QVector>
00024 #include <QTextDocument>
00025 #include <QTextCursor>
00026 #include <QTextBlock>
00027 #include <kdebug.h>
00028 
00029 ChangeFollower::ChangeFollower(QTextDocument *parent, KoStyleManager *manager)
00030     : QObject(parent),
00031     m_document(parent),
00032     m_styleManager(manager)
00033 {
00034 }
00035 
00036 ChangeFollower::~ChangeFollower() {
00037     if(m_styleManager)
00038         m_styleManager->remove(this);
00039 }
00040 
00041 void ChangeFollower::processUpdates(const QList<int> &changedStyles) {
00042     if(m_styleManager == 0) {
00043         // since the stylemanager would be the one calling this method, I doubt this
00044         // will ever happen.  But better safe than sorry..
00045         deleteLater();
00046         return;
00047     }
00048 
00049     QTextCursor cursor (m_document);
00050     QTextBlock block = cursor.block();
00051     while(block.isValid()) {
00052         QTextBlockFormat bf = block.blockFormat();
00053         int id = bf.intProperty(KoParagraphStyle::StyleId);
00054         if(id > 0 && changedStyles.contains(id)) {
00055             cursor.setPosition( block.position() );
00056             KoParagraphStyle *style = m_styleManager->paragraphStyle(id);
00057             Q_ASSERT(style);
00058 
00059             style->applyStyle(bf);
00060             cursor.setBlockFormat(bf);
00061         }
00062         QTextBlock::iterator iter = block.begin();
00063         while(! iter.atEnd()) {
00064             QTextFragment fragment = iter.fragment();
00065             QTextCharFormat cf = fragment.charFormat();
00066             id = cf.intProperty(KoCharacterStyle::StyleId);
00067             if(id > 0 && changedStyles.contains(id)) {
00068                 // create selection
00069                 cursor.setPosition(block.position() + fragment.position());
00070                 cursor.setPosition(block.position() + fragment.position() +
00071                         fragment.length(), QTextCursor::KeepAnchor);
00072                 KoCharacterStyle *style = m_styleManager->characterStyle(id);
00073                 Q_ASSERT(style);
00074 
00075                 style->applyStyle(cf);
00076                 cursor.mergeCharFormat(cf);
00077             }
00078             iter++;
00079         }
00080         block = block.next();
00081     }
00082 /*  I think we want to look at Qt to see if this _much_ faster way can ever work.
00083     foreach(QTextFormat format, m_document->allFormats()) {
00084         int id = format.intProperty(KoParagraphStyle::StyleId);
00085         if(id <= 0)
00086             continue;
00087         if(changedStyles.contains(id)) {
00088             if(format.isBlockFormat()) { // aka parag
00089                 KoParagraphStyle *style = m_styleManager->paragraphStyle(id);
00090                 Q_ASSERT(style);
00091                 QTextBlockFormat bf = format.toBlockFormat();
00092                 style->applyStyle(bf);
00093             }
00094             else if(format.isCharFormat()) {
00095                 KoCharacterStyle *style = m_styleManager->characterStyle(id);
00096                 Q_ASSERT(style);
00097                 QTextCharFormat cf = format.toCharFormat();
00098                 style->applyStyle(cf);
00099             }
00100         }
00101     } */
00102 }

Généré le Wed Nov 22 23:41:11 2006 pour KPlato par  doxygen 1.5.1-p1