F:/KPlato/koffice/libs/kotext/styles/KoStyleManager.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 "KoStyleManager.h"
00020 #include "KoParagraphStyle.h"
00021 #include "KoCharacterStyle.h"
00022 #include "ChangeFollower.h"
00023 
00024 #include <QTimer>
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 
00028 KoStyleManager::KoStyleManager(QObject *parent)
00029     : QObject(parent),
00030     m_updateTriggered(false)
00031 {
00032     m_standard = new KoParagraphStyle();
00033     m_standard->setLeftMargin(0);
00034     m_standard->setTopMargin(0);
00035     m_standard->setBottomMargin(0);
00036     m_standard->setRightMargin(0);
00037     m_standard->setTextIndent(0);
00038     m_standard->setIndent(0);
00039     m_standard->setAlignment(Qt::AlignLeft);
00040     m_standard->setName( i18n("[No Paragraph Style]"));
00041     add(m_standard);
00042 /* for testing ;)
00043 KoParagraphStyle *a = new KoParagraphStyle();
00044 a->setName("A");
00045 a->setParent(standard);
00046 a->setLeftMargin(40);
00047 add(a);
00048 KoParagraphStyle *b = new KoParagraphStyle();
00049 b->setName("B");
00050 b->setParent(standard);
00051 b->setRightMargin(400);
00052 add(b); */
00053 }
00054 
00055 void KoStyleManager::add(KoCharacterStyle *style) {
00056     if(m_charStyles.contains(style))
00057         return;
00058     style->setStyleId( s_stylesNumber++ );
00059     m_charStyles.append(style);
00060 }
00061 
00062 void KoStyleManager::add(KoParagraphStyle *style) {
00063     if(m_paragStyles.contains(style))
00064         return;
00065     style->setStyleId( s_stylesNumber++ );
00066     m_paragStyles.append(style);
00067     if(style->characterStyle())
00068         add(style->characterStyle());
00069 }
00070 
00071 void KoStyleManager::remove(KoCharacterStyle *style) {
00072     m_charStyles.removeAll(style);
00073 }
00074 
00075 void KoStyleManager::remove(KoParagraphStyle *style) {
00076     m_paragStyles.removeAll(style);
00077 }
00078 
00079 void KoStyleManager::remove(ChangeFollower *cf) {
00080     m_documentUpdaterProxies.removeAll(cf);
00081 }
00082 
00083 void KoStyleManager::alteredStyle(const KoParagraphStyle *style) {
00084     Q_ASSERT(style);
00085     int id = style->styleId();
00086     if(id <= 0) {
00087         kWarning(32500) << "alteredStyle received from a non registered style!" << endl;
00088         return;
00089     }
00090     if(! m_updateQueue.contains(id))
00091         m_updateQueue.append(id);
00092     requestFireUpdate();
00093 
00094     // check if anyone that uses 'style' as a parent needs to be flagged as changed as well.
00095     foreach(KoParagraphStyle *ps, m_paragStyles) {
00096         if(ps->parent() == style)
00097             alteredStyle(ps);
00098     }
00099 }
00100 
00101 void KoStyleManager::alteredStyle(const KoCharacterStyle *style) {
00102     Q_ASSERT(style);
00103     int id = style->styleId();
00104     if(id <= 0) {
00105         kWarning(32500) << "alteredStyle received from a non registered style!" << endl;
00106         return;
00107     }
00108     if(! m_updateQueue.contains(id))
00109         m_updateQueue.append(id);
00110     requestFireUpdate();
00111 }
00112 
00113 void KoStyleManager::updateAlteredStyles() {
00114     foreach(ChangeFollower *cf, m_documentUpdaterProxies) {
00115         cf->processUpdates(m_updateQueue);
00116     }
00117     m_updateQueue.clear();
00118     m_updateTriggered = false;
00119 }
00120 
00121 void KoStyleManager::requestFireUpdate() {
00122     if(m_updateTriggered)
00123         return;
00124     QTimer::singleShot(0, this, SLOT(updateAlteredStyles()));
00125     m_updateTriggered = true;
00126 }
00127 
00128 void KoStyleManager::add(QTextDocument *document) {
00129     foreach(ChangeFollower *cf, m_documentUpdaterProxies) {
00130         if(cf->document() == document) {
00131             return; // already present.
00132         }
00133     }
00134     ChangeFollower *cf = new ChangeFollower(document, this);
00135     m_documentUpdaterProxies.append(cf);
00136 }
00137 
00138 void KoStyleManager::remove(QTextDocument *document) {
00139     foreach(ChangeFollower *cf, m_documentUpdaterProxies) {
00140         if(cf->document() == document) {
00141             m_documentUpdaterProxies.removeAll(cf);
00142             return;
00143         }
00144     }
00145 }
00146 
00147 KoCharacterStyle *KoStyleManager::characterStyle(int id) const {
00148     foreach(KoCharacterStyle *style, m_charStyles) {
00149         if(style->styleId() == id)
00150             return style;
00151     }
00152     return 0;
00153 }
00154 
00155 KoParagraphStyle *KoStyleManager::paragraphStyle(int id) const {
00156     foreach(KoParagraphStyle *style, m_paragStyles) {
00157         if(style->styleId() == id)
00158             return style;
00159     }
00160     return 0;
00161 }
00162 
00163 KoCharacterStyle *KoStyleManager::characterStyle(const QString &name) const {
00164     foreach(KoCharacterStyle *style, m_charStyles) {
00165         if(style->name() == name)
00166             return style;
00167     }
00168     return 0;
00169 }
00170 
00171 KoParagraphStyle *KoStyleManager::paragraphStyle(const QString &name) const {
00172     foreach(KoParagraphStyle *style, m_paragStyles) {
00173         if(style->name() == name)
00174             return style;
00175     }
00176     return 0;
00177 }
00178 
00179 KoParagraphStyle *KoStyleManager::defaultParagraphStyle() const {
00180     return m_standard;
00181 }
00182 
00183 // static
00184 int KoStyleManager::s_stylesNumber = 100;
00185 
00186 #include "KoStyleManager.moc"

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