00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 ) {
00058 KoParagStyle *s = defaultStyle();
00059
00060 if(s)
00061 removeStyle(s);
00062 defaultStyleDeleted = true;
00063 }
00064
00065 KoParagStyle *sty = new KoParagStyle( QString::null );
00066
00067 sty->loadStyle( styleElem, context );
00068
00069 const int oldStyleCount = count();
00070 sty = addStyle( sty );
00071
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
00104
00105 Q_ASSERT( defaultStyle() );
00106 return stylesLoaded;
00107 }
00108
00109 void KoStyleCollection::saveOasis( KoGenStyles& styles, int styleType, KoSavingContext& context ) const
00110 {
00111
00112
00113
00114
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() )
00124 refStyleName = style->name();
00125 }
00126 tmpStyle = 0;
00127
00128
00129
00130
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
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();
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" );
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
00253
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