F:/KPlato/koffice/libs/kotext/KoStyleCollection.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 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 as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KoStyleCollection.h"
00021 #include "KoOasisContext.h"
00022 #include "KoParagCounter.h"
00023 
00024 #include <KoOasisStyles.h>
00025 #include <KoGenStyles.h>
00026 #include <KoXmlWriter.h>
00027 #include <KoXmlNS.h>
00028 
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 
00032 #include <QList>
00033 
00034 KoStyleCollection::KoStyleCollection()
00035     : KoUserStyleCollection( "paragsty" )
00036 {
00037 }
00038 
00039 KoStyleCollection::~KoStyleCollection()
00040 {
00041 }
00042 
00043 int KoStyleCollection::loadOasisStyles( KoOasisContext& context )
00044 {
00045     QStringList followingStyles;
00046     Q3ValueVector<QDomElement> userStyles = context.oasisStyles().userStyles();
00047     bool defaultStyleDeleted = false;
00048     int stylesLoaded = 0;
00049     const unsigned int nStyles = userStyles.count();
00050     for (unsigned int item = 0; item < nStyles; item++) {
00051         QDomElement styleElem = userStyles[item];
00052         Q_ASSERT( !styleElem.isNull() );
00053 
00054         if ( styleElem.attributeNS( KoXmlNS::style, "family", QString::null ) != "paragraph" )
00055             continue;
00056 
00057         if( !defaultStyleDeleted ) { // we are going to import at least one style.
00058             KoParagStyle *s = defaultStyle();
00059             //kDebug() << "loadOasisStyles looking for Standard, to delete it. Found " << s << endl;
00060             if(s) // delete the standard style.
00061                 removeStyle(s);
00062             defaultStyleDeleted = true;
00063         }
00064 
00065         KoParagStyle *sty = new KoParagStyle( QString::null );
00066         // Load the style
00067         sty->loadStyle( styleElem, context );
00068         // Style created, now let's try to add it
00069         const int oldStyleCount = count();
00070         sty = addStyle( sty );
00071         // the real value of followingStyle is set below after loading all styles
00072         sty->setFollowingStyle( sty );
00073 
00074         kDebug() << " Loaded style " << sty->name() << endl;
00075 
00076         if ( count() > oldStyleCount )
00077         {
00078             const QString following = styleElem.attributeNS( KoXmlNS::style, "next-style-name", QString::null );
00079             followingStyles.append( following );
00080             ++stylesLoaded;
00081         }
00082         else
00083             kWarning() << "Found duplicate style declaration, overwriting former " << sty->name() << endl;
00084     }
00085 
00086     if( followingStyles.count() != styleList().count() ) {
00087         kDebug() << "Ouch, " << followingStyles.count() << " following-styles, but "
00088                        << styleList().count() << " styles in styleList" << endl;
00089     }
00090 
00091     unsigned int i = 0;
00092     QString tmpString;
00093     foreach( tmpString, followingStyles )
00094     {
00095         const QString followingStyleName = tmpString;
00096         if ( !followingStyleName.isEmpty() ) {
00097             KoParagStyle * style = findStyle( followingStyleName );
00098             if ( style )
00099                 styleAt(i)->setFollowingStyle( style );
00100         }
00101     }
00102 
00103     // TODO the same thing for style inheritance (style:parent-style-name) and setParentStyle()
00104 
00105     Q_ASSERT( defaultStyle() );
00106     return stylesLoaded;
00107 }
00108 
00109 void KoStyleCollection::saveOasis( KoGenStyles& styles, int styleType, KoSavingContext& context ) const
00110 {
00111     // In order to reduce the bloat, we define that the first style (usually Standard)
00112     // is the "parent" (reference) for the others.
00113     // ## This is mostly a hack due to lack of proper style inheritance.
00114     // Once that's implemented, default to 'styles derive from Standard', but save normally.
00115     QString refStyleName;
00116     
00117     KoUserStyle* tmpStyle = 0;
00118     foreach( tmpStyle, m_styleList )
00119     {
00120         KoParagStyle* style = static_cast<KoParagStyle *>( tmpStyle );
00121         style->saveStyle( styles, styleType, refStyleName, context );
00122         kDebug() << k_funcinfo << "Saved style " << style->displayName() << " to OASIS format as " << style->name() << endl;
00123         if ( refStyleName.isEmpty() ) // i.e. first style
00124             refStyleName = style->name();
00125     }
00126     tmpStyle = 0;
00127     // Now edit the kogenstyle and set the next-style-name. This works here
00128     // because the style's m_name is already unique so there's no risk of
00129     // "two styles being only different due to their following-style"; the
00130     // display-name will also be different, and will ensure they get two kogenstyles.
00131     foreach( tmpStyle, m_styleList )
00132     {
00133         KoParagStyle* style = static_cast<KoParagStyle *>( tmpStyle );
00134         if ( style->followingStyle() && style->followingStyle() != style ) {
00135             const QString fsname = style->followingStyle()->name();
00136             KoGenStyle* gs = styles.styleForModification( style->name() );
00137             Q_ASSERT( gs );
00138             if ( gs )
00139                 gs->addAttribute( "style:next-style-name", fsname );
00140         }
00141     }
00142 }
00143 
00144 void KoStyleCollection::importStyles( const KoStyleCollection& styleCollection )
00145 {
00146     const Q3ValueList<KoUserStyle *> styles = styleCollection.styleList();
00147     QMap<QString, QString> followStyle;
00148     for ( Q3ValueList<KoUserStyle *>::const_iterator styleIt = styles.begin(), styleEnd = styles.end() ; styleIt != styleEnd ; ++styleIt ) {
00149         KoParagStyle* p = static_cast<KoParagStyle *>( *styleIt );
00150         KoParagStyle* style = new KoParagStyle( *p );
00151         if ( style->followingStyle() ) {
00152             followStyle.insert( style->name(), style->followingStyle()->name() );
00153         }
00154         style = addStyle( style );
00155     }
00156 
00157     QMap<QString, QString>::iterator itFollow = followStyle.begin();
00158     for ( ; itFollow != followStyle.end(); ++itFollow )
00159     {
00160         KoParagStyle * style = findStyle(itFollow.key());
00161         const QString followingStyleName = followStyle[ itFollow.key() ];
00162         KoParagStyle * styleFollow = findStyle(followingStyleName);
00163         //kDebug() << "    " << style << "  " << itFollow.key() << ": followed by " << styleFollow << " (" << followingStyleName << ")" << endl;
00164         Q_ASSERT(styleFollow);
00165         if ( styleFollow )
00166             style->setFollowingStyle( styleFollow );
00167         else
00168             style->setFollowingStyle( style );
00169     }
00170 }
00171 
00172 void KoStyleCollection::saveOasisOutlineStyles( KoXmlWriter& writer ) const
00173 {
00174     bool first = true;
00175     Q3ValueVector<KoParagStyle *> styles = outlineStyles();
00176     for ( int i = 0 ; i < 10 ; ++i ) {
00177         if ( styles[i] ) {
00178             if ( first ) {
00179                 writer.startElement( "text:outline-style" );
00180                 first = false;
00181             }
00182             writer.startElement( "text:outline-level-style" );
00183             styles[i]->paragLayout().counter->saveOasisListLevel( writer, true, true );
00184             writer.endElement();
00185         }
00186     }
00187     if ( !first )
00188         writer.endElement(); // text:outline-style
00189 }
00190 
00191 Q3ValueVector<KoParagStyle *> KoStyleCollection::outlineStyles() const
00192 {
00193     Q3ValueVector<KoParagStyle *> lst( 10, 0 );
00194     for ( int i = 0 ; i < 10 ; ++i ) {
00195         KoParagStyle* style = outlineStyleForLevel( i );
00196         if ( style )
00197             lst[i] = style;
00198     }
00199     return lst;
00200 }
00201 
00202 
00203 KoParagStyle* KoStyleCollection::outlineStyleForLevel( int level ) const
00204 {
00205     KoUserStyle* tmpStyle = 0;  
00206     foreach( tmpStyle, m_styleList )
00207     {
00208         KoParagStyle* style = static_cast<KoParagStyle *>( tmpStyle );
00209         if ( style->isOutline() && style->paragLayout().counter )
00210         {
00211             int styleLevel = style->paragLayout().counter->depth();
00212             if ( styleLevel == level )
00213                 return style;
00214         }
00215     }
00216     return 0;
00217 }
00218 
00219 KoParagStyle* KoStyleCollection::numberedStyleForLevel( int level ) const
00220 {
00221     KoUserStyle* tmpStyle = 0;  
00222     foreach( tmpStyle, m_styleList )
00223     {
00224         KoParagStyle* style = static_cast<KoParagStyle *>( tmpStyle );
00225         KoParagCounter* counter = style->paragLayout().counter;
00226         if ( !style->isOutline() && counter
00227              && counter->numbering() != KoParagCounter::NUM_NONE
00228              && !counter->isBullet() )
00229         {
00230             int styleLevel = counter->depth();
00231             if ( styleLevel == level )
00232                 return style;
00233         }
00234     }
00235     return 0;
00236 }
00237 
00238 KoParagStyle* KoStyleCollection::defaultStyle() const
00239 {
00240     return findStyle( "Standard" ); // includes the fallback to first style
00241 }
00242 
00243 #ifndef NDEBUG
00244 void KoStyleCollection::printDebug() const
00245 {
00246     KoParagStyle* style = 0;    
00247     KoUserStyle* tmpStyle = 0;  
00248     foreach( tmpStyle, m_styleList )
00249     {
00250         style = static_cast<KoParagStyle *>( tmpStyle );
00251 
00252         // short version:
00253         // kDebug() << style << "  " << style->name() << "    " << style->displayName() << "  followingStyle=" << style->followingStyle() << endl;
00254 
00255         kDebug() << "Style " << style << "  " << style->name() << "  isOutline=" << style->isOutline() << endl;
00256         kDebug() << "   format: " << style->format().key() <<endl;
00257         static const char * const s_align[] = { "Auto", "Left", "Right", "ERROR", "HCenter", "ERR", "ERR", "ERR", "Justify", };
00258         kDebug() << "  align: " << s_align[(Qt::Alignment)style->paragLayout().alignment] << endl;
00259         if ( style->paragLayout().counter )
00260             kDebug() << "  counter level=" << style->paragLayout().counter->depth() << endl;
00261 
00262         kDebug() << "   following style: " << style->followingStyle() << " "
00263                   << ( style->followingStyle() ? style->followingStyle()->name() : QString::null ) << endl;
00264 
00265     }
00266 }
00267 #endif

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