00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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;
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
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
00110 layout.style = this;
00111 m_paragLayout = layout;
00112
00113
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
00122
00123 m_bOutline = parentElem.attribute( "outline" ) == "true";
00124 }
00125
00126 void KoParagStyle::loadStyle( QDomElement & styleElem, KoOasisContext& context )
00127 {
00128
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
00135
00136
00137 m_bOutline = styleElem.hasAttributeNS( KoXmlNS::style, "default-outline-level" );
00138
00139 context.styleStack().save();
00140 context.addStyles( &styleElem, "paragraph" );
00141 KoParagLayout layout;
00142 KoParagLayout::loadOasisParagLayout( layout, context );
00143
00144
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();
00150 listOK = context.pushOutlineListLevelStyle( level );
00151
00152 if ( !listStyleName.isEmpty() )
00153 context.pushListLevelStyle( listStyleName, level );
00154 }
00155 else {
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 level = styleElem.attributeNS( KoXmlNS::style, "default-level", "1" ).toInt();
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
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
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
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 );
00206 m_paragLayout.counter->saveOasis( listStyle, true );
00207
00208
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
00222
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 )
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
00274 setStyle( layout.style );
00275 }
00276 #endif
00277
00278
00279 }
00280
00281 void KoParagStyle::setOutline( bool b )
00282 {
00283 m_bOutline = b;
00284 }