F:/KPlato/koffice/libs/kotext/styles/KoParagraphStyle.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 #include "KoParagraphStyle.h"
00019 #include "KoCharacterStyle.h"
00020 #include "KoListStyle.h"
00021 #include "KoTextBlockData.h"
00022 
00023 #include "Styles_p.h"
00024 
00025 #include <QTextBlock>
00026 #include <QTextBlockFormat>
00027 #include <QTextCursor>
00028 
00029 KoParagraphStyle::KoParagraphStyle()
00030     : m_charStyle(new KoCharacterStyle(this)),
00031     m_listStyle(0),
00032     m_parent(0),
00033     m_next(0)
00034 {
00035     m_stylesPrivate = new StylePrivate();
00036     setLineHeightPercent(120);
00037 }
00038 
00039 KoParagraphStyle::KoParagraphStyle(const KoParagraphStyle &orig)
00040     : QObject(0),
00041     m_charStyle(new KoCharacterStyle(this)),
00042     m_listStyle(0),
00043     m_parent(0),
00044     m_next(0)
00045 {
00046     m_stylesPrivate = new StylePrivate();
00047     m_stylesPrivate->copyMissing(orig.m_stylesPrivate);
00048     m_name = orig.name();
00049     m_charStyle = orig.m_charStyle;
00050     m_next = orig.m_next;
00051     if(orig.m_listStyle) {
00052         m_listStyle = orig.m_listStyle;
00053         m_listStyle->addUser();
00054     }
00055 }
00056 
00057 KoParagraphStyle::~KoParagraphStyle() {
00058     delete m_stylesPrivate;
00059     m_stylesPrivate = 0;
00060     m_charStyle = 0; // QObject will delete it.
00061     if(m_listStyle) {
00062         m_listStyle->removeUser();
00063         if(m_listStyle->userCount() == 0)
00064             delete m_listStyle;
00065         m_listStyle = 0;
00066     }
00067 }
00068 
00069 void KoParagraphStyle::setParent(KoParagraphStyle *parent) {
00070     Q_ASSERT(parent != this);
00071     if(m_parent)
00072         m_stylesPrivate->copyMissing(m_parent->m_stylesPrivate);
00073     m_parent = parent;
00074     if(m_parent)
00075         m_stylesPrivate->removeDuplicates(m_parent->m_stylesPrivate);
00076 }
00077 
00078 void KoParagraphStyle::setProperty(int key, const QVariant &value) {
00079     if(m_parent) {
00080         QVariant const *var = m_parent->get(key);
00081         if(var && (*var) == value) { // same as parent, so its actually a reset.
00082             m_stylesPrivate->remove(key);
00083             return;
00084         }
00085     }
00086     m_stylesPrivate->add(key, value);
00087 }
00088 
00089 void KoParagraphStyle::remove(int key) {
00090     m_stylesPrivate->remove(key);
00091 }
00092 
00093 QVariant const *KoParagraphStyle::get(int key) const {
00094     QVariant const *var = m_stylesPrivate->get(key);
00095     if(var == 0 && m_parent)
00096         var = m_parent->get(key);
00097     return var;
00098 }
00099 
00100 double KoParagraphStyle::propertyDouble(int key) const {
00101     const QVariant *variant = get(key);
00102     if(variant == 0)
00103         return 0.0;
00104     return variant->toDouble();
00105 }
00106 
00107 int KoParagraphStyle::propertyInt(int key) const {
00108     const QVariant *variant = get(key);
00109     if(variant == 0)
00110         return 0;
00111     return variant->toInt();
00112 }
00113 
00114 bool KoParagraphStyle::propertyBoolean(int key) const {
00115     const QVariant *variant = get(key);
00116     if(variant == 0)
00117         return false;
00118     return variant->toBool();
00119 }
00120 
00121 QColor KoParagraphStyle::propertyColor(int key) const {
00122     const QVariant *variant = get(key);
00123     if(variant == 0) {
00124         QColor color;
00125         return color;
00126     }
00127     return qvariant_cast<QColor>(*variant);
00128 }
00129 
00130 void KoParagraphStyle::applyStyle(QTextBlockFormat &format) const {
00131     // copy all relevant properties.
00132     static const int properties[] = {
00133         QTextFormat::BlockTopMargin,
00134         QTextFormat::BlockBottomMargin,
00135         QTextFormat::BlockLeftMargin,
00136         QTextFormat::BlockRightMargin,
00137         QTextFormat::BlockAlignment,
00138         QTextFormat::TextIndent,
00139         QTextFormat::BlockIndent,
00140         QTextFormat::BlockNonBreakableLines,
00141         StyleId,
00142         FixedLineHeight,
00143         MinimumLineHeight,
00144         LineSpacing,
00145         LineSpacingFromFont,
00146 //       AlignLastLine,
00147 //       WidowThreshold,
00148 //       OrphanThreshold,
00149 //       DropCaps,
00150 //       DropCapsLength,
00151 //       DropCapsLines,
00152 //       DropCapsDistance,
00153 //       FollowDocBaseline,
00154         BreakBefore,
00155         BreakAfter,
00156 //       HasLeftBorder,
00157 //       HasTopBorder,
00158 //       HasRightBorder,
00159 //       HasBottomBorder,
00160 //       BorderLineWidth,
00161 //       SecondBorderLineWidth,
00162 //       DistanceToSecondBorder,
00163         LeftPadding,
00164         TopPadding,
00165         RightPadding,
00166         BottomPadding,
00167         LeftBorderWidth,
00168         LeftInnerBorderWidth,
00169         LeftBorderSpacing,
00170         LeftBorderStyle,
00171         TopBorderWidth,
00172         TopInnerBorderWidth,
00173         TopBorderSpacing,
00174         TopBorderStyle,
00175         RightBorderWidth,
00176         RightInnerBorderWidth,
00177         RightBorderSpacing,
00178         RightBorderStyle,
00179         BottomBorderWidth,
00180         BottomInnerBorderWidth,
00181         BottomBorderSpacing,
00182         BottomBorderStyle,
00183         LeftBorderColor,
00184         TopBorderColor,
00185         RightBorderColor,
00186         BottomBorderColor,
00187         -1
00188     };
00189 
00190     int i=0;
00191     while(properties[i] != -1) {
00192         QVariant const *variant = get(properties[i]);
00193         if(variant)
00194             format.setProperty(properties[i], *variant);
00195         else
00196             format.clearProperty(properties[i]);
00197         i++;
00198     }
00199 }
00200 
00201 void KoParagraphStyle::applyStyle(QTextBlock &block) const {
00202     QTextCursor cursor(block);
00203     QTextBlockFormat format = cursor.blockFormat();
00204     applyStyle(format);
00205     cursor.setBlockFormat(format);
00206     if(m_charStyle)
00207         m_charStyle->applyStyle(block);
00208 
00209     if(m_listStyle) {
00210         // make sure this block becomes a list if its not one already
00211         m_listStyle->applyStyle(block);
00212     } else if(block.textList()) {
00213         // remove
00214         block.textList()->remove(block);
00215         KoTextBlockData *data = dynamic_cast<KoTextBlockData*> (block.userData());
00216         if(data)
00217             data->setCounterWidth(-1);
00218     }
00219 }
00220 
00221 void KoParagraphStyle::setListStyle(const KoListStyle &style) {
00222     if(m_listStyle)
00223         m_listStyle->apply(style);
00224     else {
00225         m_listStyle = new KoListStyle(style);
00226         m_listStyle->addUser();
00227     }
00228 }
00229 
00230 void KoParagraphStyle::removeListStyle() {
00231     delete m_listStyle; m_listStyle = 0;
00232 }
00233 
00234 #include "KoParagraphStyle.moc"

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