lib

KoParagStyle.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001-2005 David Faure <faure@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 version 2 as published by the Free Software Foundation.
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 "KoParagStyle.h"
00020 #include "KoOasisContext.h"
00021 #include "KoParagCounter.h"
00022 
00023 #include <KoGenStyles.h>
00024 #include <KoXmlWriter.h>
00025 #include <KoXmlNS.h>
00026 
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029 
00030 #include <qdom.h>
00031 
00032 KoCharStyle::KoCharStyle( const QString & name )
00033     : KoUserStyle( name )
00034 {
00035 }
00036 
00037 const KoTextFormat & KoCharStyle::format() const
00038 {
00039     return m_format;
00040 }
00041 
00042 KoTextFormat & KoCharStyle::format()
00043 {
00044     return m_format;
00045 }
00046 
00048 
00049 KoParagStyle::KoParagStyle( const QString & name )
00050     : KoCharStyle( name )
00051 {
00052     m_followingStyle = this;
00053 
00054     // This way, KoTextParag::setParagLayout also sets the style pointer, to this style
00055     m_paragLayout.style = this;
00056     m_parentStyle = 0L;
00057     m_inheritedParagLayoutFlag = 0;
00058     m_inheritedFormatFlag = 0;
00059     m_bOutline = false;
00060 }
00061 
00062 KoParagStyle::KoParagStyle( const KoParagStyle & rhs )
00063     : KoCharStyle( rhs)
00064 {
00065     *this = rhs;
00066 }
00067 
00068 KoParagStyle::~KoParagStyle()
00069 {
00070 }
00071 
00072 void KoParagStyle::operator=( const KoParagStyle &rhs )
00073 {
00074     KoCharStyle::operator=( rhs );
00075     m_paragLayout = rhs.m_paragLayout;
00076     m_followingStyle = rhs.m_followingStyle;
00077     m_paragLayout.style = this; // must always be "this"
00078     m_parentStyle = rhs.m_parentStyle;
00079     m_inheritedParagLayoutFlag = rhs.m_inheritedParagLayoutFlag;
00080     m_inheritedFormatFlag = rhs.m_inheritedFormatFlag;
00081     m_bOutline = rhs.m_bOutline;
00082 }
00083 
00084 void KoParagStyle::setFollowingStyle( KoParagStyle *fst )
00085 {
00086   m_followingStyle = fst;
00087 }
00088 
00089 void KoParagStyle::saveStyle( QDomElement & parentElem )
00090 {
00091     m_paragLayout.saveParagLayout( parentElem, m_paragLayout.alignment );
00092 
00093     if ( followingStyle() )
00094     {
00095         QDomElement element = parentElem.ownerDocument().createElement( "FOLLOWING" );
00096         parentElem.appendChild( element );
00097         element.setAttribute( "name", followingStyle()->displayName() );
00098     }
00099     // TODO save parent style, and inherited flags.
00100 
00101     parentElem.setAttribute( "outline", m_bOutline ? "true" : "false" );
00102 }
00103 
00104 void KoParagStyle::loadStyle( QDomElement & parentElem, int docVersion )
00105 {
00106     KoParagLayout layout;
00107     KoParagLayout::loadParagLayout( layout, parentElem, docVersion );
00108 
00109     // This way, KoTextParag::setParagLayout also sets the style pointer, to this style
00110     layout.style = this;
00111     m_paragLayout = layout;
00112 
00113     // Load name
00114     QDomElement nameElem = parentElem.namedItem("NAME").toElement();
00115     if ( !nameElem.isNull() ) {
00116         m_name = nameElem.attribute("value");
00117         m_displayName = i18n( "Style name", m_name.utf8() );
00118     } else
00119         kdWarning() << "No NAME tag in LAYOUT -> no name for this style!" << endl;
00120 
00121     // The followingStyle stuff has to be done after loading all styles.
00122 
00123     m_bOutline = parentElem.attribute( "outline" ) == "true";
00124 }
00125 
00126 void KoParagStyle::loadStyle( QDomElement & styleElem, KoOasisContext& context )
00127 {
00128     // Load name
00129     m_name = styleElem.attributeNS( KoXmlNS::style, "name", QString::null );
00130     m_displayName = styleElem.attributeNS( KoXmlNS::style, "display-name", QString::null );
00131     if ( m_displayName.isEmpty() )
00132         m_displayName = m_name;
00133 
00134     // OOo hack
00135     //m_bOutline = m_name.startsWith( "Heading" );
00136     // real OASIS solution:
00137     m_bOutline = styleElem.hasAttributeNS( KoXmlNS::style, "default-outline-level" );
00138 
00139     context.styleStack().save();
00140     context.addStyles( &styleElem, "paragraph" ); // Load all parents - only because we don't support inheritance.
00141     KoParagLayout layout;
00142     KoParagLayout::loadOasisParagLayout( layout, context );
00143 
00144     // loadOasisParagLayout doesn't load the counter. It's modelled differently for parags and for styles.
00145     int level = 0;
00146     bool listOK = false;
00147     const QString listStyleName = styleElem.attributeNS( KoXmlNS::style, "list-style-name", QString::null );
00148     if ( m_bOutline ) {
00149         level = styleElem.attributeNS( KoXmlNS::style, "default-outline-level", QString::null ).toInt(); // 1-based
00150         listOK = context.pushOutlineListLevelStyle( level );
00151         // allow overriding the outline numbering, see http://lists.oasis-open.org/archives/office/200310/msg00033.html
00152         if ( !listStyleName.isEmpty() )
00153             context.pushListLevelStyle( listStyleName, level );
00154     }
00155     else {
00156         // ######## BIG difference here. In the OOo/OASIS format, one list style has infos for 10 list levels...
00157         // ###### so we can't know a level at this point...
00158 
00159         // The only solution I can think of, to preserve document content when importing OO but
00160         // not necessarily the styles used when editing, is:
00161         // 1) when importing from OOo, convert each non-heading style with numbering
00162         // into 10 kotext styles (at least those used by the document) [TODO]
00163         // 2) for KWord's own loading/saving, to add a hack into the file format, say
00164         // style:default-level.
00165         // Note that default-level defaults to "1", i.e. works for non-nested OOo lists too.
00166         level = styleElem.attributeNS( KoXmlNS::style, "default-level", "1" ).toInt(); // 1-based
00167         listOK = !listStyleName.isEmpty();
00168         if ( listOK )
00169             listOK = context.pushListLevelStyle( listStyleName, level );
00170     }
00171     if ( listOK ) {
00172         const QDomElement listStyle = context.listStyleStack().currentListStyle();
00173         // The tag is either text:list-level-style-number or text:list-level-style-bullet
00174         const bool ordered = listStyle.localName() == "list-level-style-number";
00175         Q_ASSERT( !layout.counter );
00176         layout.counter = new KoParagCounter;
00177         layout.counter->loadOasis( context, -1, ordered, m_bOutline, level, true );
00178         context.listStyleStack().pop();
00179     }
00180 
00181     // This way, KoTextParag::setParagLayout also sets the style pointer, to this style
00182     layout.style = this;
00183     m_paragLayout = layout;
00184 
00185     m_format.load( context );
00186 
00187     context.styleStack().restore();
00188 }
00189 
00190 QString KoParagStyle::saveStyle( KoGenStyles& genStyles, int styleType, const QString& parentStyleName, KoSavingContext& context ) const
00191 {
00192     KoGenStyle gs( styleType, "paragraph", parentStyleName );
00193 
00194     gs.addAttribute( "style:display-name", m_displayName );
00195     if ( m_paragLayout.counter ) {
00196         if ( m_bOutline )
00197             gs.addAttribute( "style:default-outline-level", (int)m_paragLayout.counter->depth() + 1 );
00198         else if ( m_paragLayout.counter->depth() )
00199             // ### kword-specific attribute, see loadOasis
00200             gs.addAttribute( "style:default-level", (int)m_paragLayout.counter->depth() + 1 );
00201 
00202         if ( m_paragLayout.counter->numbering() != KoParagCounter::NUM_NONE &&
00203              m_paragLayout.counter->style() != KoParagCounter::STYLE_NONE )
00204         {
00205             KoGenStyle listStyle( KoGenStyle::STYLE_LIST /*, no family*/ );
00206             m_paragLayout.counter->saveOasis( listStyle, true );
00207             // This display-name will probably look nicer in OO, but this also means
00208             // no re-use possible between list styles...
00209             listStyle.addAttribute( "style:display-name",
00210                                     i18n( "Numbering Style for %1" ).arg( m_displayName ) );
00211 
00212             QString autoListStyleName = genStyles.lookup( listStyle, "L", KoGenStyles::ForceNumbering );
00213             gs.addAttribute( "style:list-style-name", autoListStyleName );
00214         }
00215     }
00216 
00217     m_paragLayout.saveOasis( gs, context, true );
00218 
00219     m_format.save( gs, context );
00220 
00221     // try to preserve existing internal name, if it looks adequate (no spaces)
00222     // ## TODO: check XML-Schemacs NCName conformity
00223     bool nameIsConform = !m_name.isEmpty() && m_name.find( ' ' ) == -1;
00224     QString newName;
00225     if ( nameIsConform )
00226         newName = genStyles.lookup( gs, m_name, KoGenStyles::DontForceNumbering );
00227     else
00228         newName = genStyles.lookup( gs, "U", KoGenStyles::ForceNumbering );
00229     const_cast<KoParagStyle*>( this )->m_name = newName;
00230     return m_name;
00231 }
00232 
00233 const KoParagLayout & KoParagStyle::paragLayout() const
00234 {
00235     return m_paragLayout;
00236 }
00237 
00238 KoParagLayout & KoParagStyle::paragLayout()
00239 {
00240     return m_paragLayout;
00241 }
00242 
00243 void KoParagStyle::propagateChanges( int paragLayoutFlag, int /*formatFlag*/ )
00244 {
00245     if ( !m_parentStyle )
00246         return;
00247     if ( !(paragLayoutFlag & KoParagLayout::Alignment) )
00248         m_paragLayout.alignment = m_parentStyle->paragLayout().alignment;
00249     if ( !(paragLayoutFlag & KoParagLayout::Margins) )
00250         for ( int i = 0 ; i < 5 ; ++i )
00251             m_paragLayout.margins[i] = m_parentStyle->paragLayout().margins[i];
00252     if ( !(paragLayoutFlag & KoParagLayout::LineSpacing) )
00253     {
00254         m_paragLayout.setLineSpacingValue(m_parentStyle->paragLayout().lineSpacingValue());
00255         m_paragLayout.lineSpacingType = m_parentStyle->paragLayout().lineSpacingType;
00256     }
00257     if ( !(paragLayoutFlag & KoParagLayout::Borders) )
00258     {
00259         m_paragLayout.leftBorder = m_parentStyle->paragLayout().leftBorder;
00260         m_paragLayout.rightBorder = m_parentStyle->paragLayout().rightBorder;
00261         m_paragLayout.topBorder = m_parentStyle->paragLayout().topBorder;
00262         m_paragLayout.bottomBorder = m_parentStyle->paragLayout().bottomBorder;
00263         m_paragLayout.joinBorder = m_parentStyle->paragLayout().joinBorder;
00264     }
00265     if ( !(paragLayoutFlag & KoParagLayout::BulletNumber) )
00266         m_paragLayout.counter = m_parentStyle->paragLayout().counter;
00267     if ( !(paragLayoutFlag & KoParagLayout::Tabulator) )
00268         m_paragLayout.setTabList(m_parentStyle->paragLayout().tabList());
00269 #if 0
00270     if ( paragLayoutFlag == KoParagLayout::All )
00271     {
00272         setDirection( static_cast<QChar::Direction>(layout.direction) );
00273         // Don't call applyStyle from here, it would overwrite any paragraph-specific settings
00274         setStyle( layout.style );
00275     }
00276 #endif
00277     // TODO a flag for the "is outline" bool? Otherwise we have no way to inherit
00278     // that property (and possibly reset it).
00279 }
00280 
00281 void KoParagStyle::setOutline( bool b )
00282 {
00283     m_bOutline = b;
00284 }
KDE Home | KDE Accessibility Home | Description of Access Keys