00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "KoGenStyles.h"
00020 #include <KoXmlWriter.h>
00021 #include <float.h>
00022 #include <kdebug.h>
00023
00024 #include <Q3ValueList>
00025
00026 KoGenStyles::KoGenStyles()
00027 {
00028 }
00029
00030 KoGenStyles::~KoGenStyles()
00031 {
00032 }
00033
00034 QString KoGenStyles::lookup( const KoGenStyle& style, const QString& name, int flags )
00035 {
00036 StyleMap::iterator it = m_styleMap.find( style );
00037 if ( it == m_styleMap.end() ) {
00038
00039
00040 if ( !style.parentName().isEmpty() ) {
00041 KoGenStyle testStyle( style );
00042 const KoGenStyle* parentStyle = this->style( style.parentName() );
00043 if( !parentStyle ) {
00044 kDebug(30003) << "KoGenStyles::lookup(" << name << "): parent style '" << style.parentName() << "' not found in collection" << endl;
00045 } else {
00046 if ( testStyle.m_familyName != parentStyle->m_familyName )
00047 {
00048 kWarning(30003) << "KoGenStyles::lookup(" << name << ", family=" << testStyle.m_familyName << ") parent style '" << style.parentName() << "' has a different family: " << parentStyle->m_familyName << endl;
00049 }
00050
00051 testStyle.m_parentName = parentStyle->m_parentName;
00052
00053
00054 testStyle.m_type = parentStyle->m_type;
00055
00056
00057 QMap<QString, QString>::const_iterator it = parentStyle->m_attributes.find( "style:display-name" );
00058 if ( it != parentStyle->m_attributes.end() )
00059 testStyle.addAttribute( "style:display-name", *it );
00060
00061 if ( *parentStyle == testStyle )
00062 return style.parentName();
00063 }
00064 }
00065
00066 QString styleName( name );
00067 if ( styleName.isEmpty() ) {
00068 styleName = 'A';
00069 flags &= ~DontForceNumbering;
00070 }
00071 styleName = makeUniqueName( styleName, flags );
00072 if ( style.autoStyleInStylesDotXml() )
00073 m_autoStylesInStylesDotXml.insert( styleName, true );
00074 else
00075 m_styleNames.insert( styleName, true );
00076 it = m_styleMap.insert( style, styleName );
00077 NamedStyle s;
00078 s.style = &it.key();
00079 s.name = styleName;
00080 m_styleArray.append( s );
00081 }
00082 return it.value();
00083 }
00084
00085 QString KoGenStyles::makeUniqueName( const QString& base, int flags ) const
00086 {
00087
00088 if ( ( flags & DontForceNumbering )
00089 && m_autoStylesInStylesDotXml.find( base ) == m_autoStylesInStylesDotXml.end()
00090 && m_styleNames.find( base ) == m_styleNames.end() )
00091 return base;
00092 int num = 1;
00093 QString name;
00094 do {
00095 name = base;
00096 name += QString::number( num++ );
00097 } while ( m_autoStylesInStylesDotXml.find( name ) != m_autoStylesInStylesDotXml.end()
00098 || m_styleNames.find( name ) != m_styleNames.end() );
00099 return name;
00100 }
00101
00102 Q3ValueList<KoGenStyles::NamedStyle> KoGenStyles::styles( int type, bool markedForStylesXml ) const
00103 {
00104 Q3ValueList<KoGenStyles::NamedStyle> lst;
00105 const NameMap& nameMap = markedForStylesXml ? m_autoStylesInStylesDotXml : m_styleNames;
00106 StyleArray::const_iterator it = m_styleArray.begin();
00107 const StyleArray::const_iterator end = m_styleArray.end();
00108 for ( ; it != end ; ++it ) {
00109
00110 if ( (*it).style->type() == type && nameMap.find((*it).name) != nameMap.end() ) {
00111 lst.append( *it );
00112 }
00113 }
00114 return lst;
00115 }
00116
00117 const KoGenStyle* KoGenStyles::style( const QString& name ) const
00118 {
00119 StyleArray::const_iterator it = m_styleArray.begin();
00120 const StyleArray::const_iterator end = m_styleArray.end();
00121 for ( ; it != end ; ++it ) {
00122 if ( (*it).name == name )
00123 return (*it).style;
00124 }
00125 return 0;
00126 }
00127
00128 KoGenStyle* KoGenStyles::styleForModification( const QString& name )
00129 {
00130 return const_cast<KoGenStyle *>( style( name ) );
00131 }
00132
00133 void KoGenStyles::markStyleForStylesXml( const QString& name )
00134 {
00135 Q_ASSERT( m_styleNames.find( name ) != m_styleNames.end() );
00136 m_styleNames.remove( name );
00137 m_autoStylesInStylesDotXml.insert( name, true );
00138 styleForModification( name )->setAutoStyleInStylesDotXml( true );
00139 }
00140
00141 void KoGenStyles::dump()
00142 {
00143 kDebug() << "Style array:" << endl;
00144 StyleArray::const_iterator it = m_styleArray.begin();
00145 const StyleArray::const_iterator end = m_styleArray.end();
00146 for ( ; it != end ; ++it ) {
00147 kDebug() << (*it).name << endl;
00148 }
00149 for ( NameMap::const_iterator it = m_styleNames.begin(); it != m_styleNames.end(); ++it ) {
00150 kDebug() << "style: " << it.key() << endl;
00151 }
00152 for ( NameMap::const_iterator it = m_autoStylesInStylesDotXml.begin(); it != m_autoStylesInStylesDotXml.end(); ++it ) {
00153 kDebug() << "auto style for style.xml: " << it.key() << endl;
00154 const KoGenStyle* s = style( it.key() );
00155 Q_ASSERT( s );
00156 Q_ASSERT( s->autoStyleInStylesDotXml() );
00157 }
00158 }
00159