19#include "KoGenStyles.h"
20#include <KoXmlWriter.h>
24KoGenStyles::KoGenStyles()
28KoGenStyles::~KoGenStyles()
34 StyleMap::iterator it = m_styleMap.find(
style );
35 if ( it == m_styleMap.end() ) {
38 if ( !
style.parentName().isEmpty() ) {
42 kdDebug(30003) <<
"KoGenStyles::lookup(" << name <<
"): parent style '" <<
style.parentName() <<
"' not found in collection" << endl;
44 if ( testStyle.m_familyName != parentStyle->m_familyName )
46 kdWarning(30003) <<
"KoGenStyles::lookup(" << name <<
", family=" << testStyle.m_familyName <<
") parent style '" <<
style.parentName() <<
"' has a different family: " << parentStyle->m_familyName << endl;
49 testStyle.m_parentName = parentStyle->m_parentName;
52 testStyle.m_type = parentStyle->m_type;
55 TQMap<TQString, TQString>::const_iterator it = parentStyle->m_attributes.find(
"style:display-name" );
56 if ( it != parentStyle->m_attributes.end() )
59 if ( *parentStyle == testStyle )
60 return style.parentName();
64 TQString styleName( name );
65 if ( styleName.isEmpty() ) {
67 flags &= ~DontForceNumbering;
69 styleName = makeUniqueName( styleName, flags );
70 if (
style.autoStyleInStylesDotXml() )
71 m_autoStylesInStylesDotXml.insert( styleName,
true );
73 m_styleNames.insert( styleName,
true );
74 it = m_styleMap.insert(
style, styleName );
78 m_styleArray.append( s );
83TQString KoGenStyles::makeUniqueName(
const TQString& base,
int flags )
const
86 if ( ( flags & DontForceNumbering )
87 && m_autoStylesInStylesDotXml.find( base ) == m_autoStylesInStylesDotXml.end()
88 && m_styleNames.find( base ) == m_styleNames.end() )
94 name += TQString::number( num++ );
95 }
while ( m_autoStylesInStylesDotXml.find( name ) != m_autoStylesInStylesDotXml.end()
96 || m_styleNames.find( name ) != m_styleNames.end() );
102 TQValueList<KoGenStyles::NamedStyle> lst;
103 const NameMap& nameMap = markedForStylesXml ? m_autoStylesInStylesDotXml : m_styleNames;
104 StyleArray::const_iterator it = m_styleArray.begin();
105 const StyleArray::const_iterator end = m_styleArray.end();
106 for ( ; it != end ; ++it ) {
108 if ( (*it).style->type() == type && nameMap.find((*it).name) != nameMap.end() ) {
117 StyleArray::const_iterator it = m_styleArray.begin();
118 const StyleArray::const_iterator end = m_styleArray.end();
119 for ( ; it != end ; ++it ) {
120 if ( (*it).name == name )
133 Q_ASSERT( m_styleNames.find( name ) != m_styleNames.end() );
134 m_styleNames.remove( name );
135 m_autoStylesInStylesDotXml.insert( name,
true );
141 kdDebug() <<
"Style array:" << endl;
142 StyleArray::const_iterator it = m_styleArray.begin();
143 const StyleArray::const_iterator end = m_styleArray.end();
144 for ( ; it != end ; ++it ) {
145 kdDebug() << (*it).name << endl;
147 for ( NameMap::const_iterator it = m_styleNames.begin(); it != m_styleNames.end(); ++it ) {
148 kdDebug() <<
"style: " << it.key() << endl;
150 for ( NameMap::const_iterator it = m_autoStylesInStylesDotXml.begin(); it != m_autoStylesInStylesDotXml.end(); ++it ) {
151 kdDebug() <<
"auto style for style.xml: " << it.key() << endl;
159static int compareMap(
const TQMap<TQString, TQString>& map1,
const TQMap<TQString, TQString>& map2 )
161 TQMap<TQString, TQString>::const_iterator it = map1.begin();
162 TQMap<TQString, TQString>::const_iterator oit = map2.begin();
163 for ( ; it != map1.end(); ++it, ++oit ) {
164 if ( it.key() != oit.key() )
165 return it.key() < oit.key() ? -1 : +1;
166 if ( it.data() != oit.data() )
167 return it.data() < oit.data() ? -1 : +1;
178 m_autoStyleInStylesDotXml( false ), m_defaultStyle( false )
182KoGenStyle::~KoGenStyle()
186void KoGenStyle::writeStyleProperties(
KoXmlWriter* writer, PropertyType i,
187 const char* elementName,
const KoGenStyle* parentStyle )
const
189 if ( !m_properties[i].
isEmpty() ) {
191 TQMap<TQString, TQString>::const_iterator it = m_properties[i].begin();
192 const TQMap<TQString, TQString>::const_iterator end = m_properties[i].end();
193 for ( ; it != end; ++it ) {
194 if ( !parentStyle || parentStyle->property( it.key(), i ) != it.data() )
195 writer->
addAttribute( it.key().utf8(), it.data().utf8() );
201void KoGenStyle::writeStyle(
KoXmlWriter* writer, KoGenStyles& styles,
const char* elementName,
const TQString& name,
const char* propertiesElementName,
bool closeElement,
bool drawElement )
const
206 if ( !m_defaultStyle ) {
211 if ( !m_parentName.isEmpty() ) {
212 parentStyle = styles.
style( m_parentName );
213 if ( parentStyle && m_familyName.
isEmpty() ) {
217 m_familyName = parentStyle->attribute(
"style:family" ).latin1();
220 writer->
addAttribute(
"style:parent-style-name", m_parentName );
223 Q_ASSERT( qstrcmp( elementName,
"style:default-style" ) == 0 );
224 Q_ASSERT( m_parentName.isEmpty() );
226 if ( !m_familyName.isEmpty() )
228 addAttribute(
"style:family", TQString::fromLatin1( m_familyName ) );
230 if ( qstrcmp( elementName,
"style:style" ) == 0 )
231 kdWarning(30003) <<
"User style " << name <<
" is without family - invalid. m_type=" << m_type << endl;
235 kdDebug() <<
"style: " << name << endl;
238 kdDebug() <<
" parent: " << m_parentName << endl;
239 parentStyle->printDebug();
247 TQMap<TQString, TQString>::const_iterator it = m_attributes.begin();
248 for ( ; it != m_attributes.end(); ++it ) {
250 if ( parentStyle && it.key() !=
"style:family"
251 && parentStyle->attribute( it.key() ) == it.data() )
254 writer->
addAttribute( it.key().utf8(), it.data().utf8() );
256 bool createPropertiesTag = propertiesElementName && propertiesElementName[0] !=
'\0';
258 if ( !m_properties[i].
isEmpty() ||
259 !m_properties[KoGenStyle::ChildElement].
isEmpty() ) {
260 if ( createPropertiesTag )
262 it = m_properties[i].begin();
263 for ( ; it != m_properties[i].end(); ++it ) {
264 if ( !parentStyle || parentStyle->property( it.key(), i ) != it.data() )
265 writer->
addAttribute( it.key().utf8(), it.data().utf8() );
267 i = KoGenStyle::ChildElement;
268 it = m_properties[i].begin();
269 for ( ; it != m_properties[i].end(); ++it ) {
270 if ( !parentStyle || parentStyle->property( it.key(), i ) != it.data() ) {
274 if ( createPropertiesTag )
282 for ( uint i = 0; i < m_maps.count(); ++i ) {
284 if ( parentStyle && compareMap( m_maps[i], parentStyle->m_maps[i] ) == 0 )
288 TQMap<TQString, TQString>::const_iterator it = m_maps[i].begin();
289 for ( ; it != m_maps[i].end(); ++it ) {
290 writer->
addAttribute( it.key().utf8(), it.data().utf8() );
302 str.setNum( propValue,
'g', DBL_DIG );
304 m_properties[
type].insert( propName, str );
310 str.setNum( attrValue,
'g', DBL_DIG );
312 m_attributes.insert( attrName, str );
316void KoGenStyle::printDebug()
const
319 kdDebug() << m_properties[i].count() <<
" properties." << endl;
320 for( TQMap<TQString,TQString>::ConstIterator it = m_properties[i].begin(); it != m_properties[i].end(); ++it ) {
321 kdDebug() <<
" " << it.key() <<
" = " << it.data() << endl;
324 kdDebug() << m_properties[i].count() <<
" text properties." << endl;
325 for( TQMap<TQString,TQString>::ConstIterator it = m_properties[i].begin(); it != m_properties[i].end(); ++it ) {
326 kdDebug() <<
" " << it.key() <<
" = " << it.data() << endl;
329 kdDebug() << m_properties[i].count() <<
" paragraph properties." << endl;
330 for( TQMap<TQString,TQString>::ConstIterator it = m_properties[i].begin(); it != m_properties[i].end(); ++it ) {
331 kdDebug() <<
" " << it.key() <<
" = " << it.data() << endl;
334 kdDebug() << m_properties[i].count() <<
" child elements." << endl;
335 for( TQMap<TQString,TQString>::ConstIterator it = m_properties[i].begin(); it != m_properties[i].end(); ++it ) {
336 kdDebug() <<
" " << it.key() <<
" = " << it.data() << endl;
338 kdDebug() << m_attributes.count() <<
" attributes." << endl;
339 for( TQMap<TQString,TQString>::ConstIterator it = m_attributes.begin(); it != m_attributes.end(); ++it ) {
340 kdDebug() <<
" " << it.key() <<
" = " << it.data() << endl;
342 kdDebug() << m_maps.count() <<
" maps." << endl;
343 for ( uint i = 0; i < m_maps.count(); ++i ) {
344 kdDebug() <<
"map " << i <<
":" << endl;
345 for( TQMap<TQString,TQString>::ConstIterator it = m_maps[i].begin(); it != m_maps[i].end(); ++it ) {
346 kdDebug() <<
" " << it.key() <<
" = " << it.data() << endl;
355 if ( m_type != other.m_type )
return m_type < other.m_type;
356 if ( m_parentName != other.m_parentName )
return m_parentName < other.m_parentName;
357 if ( m_autoStyleInStylesDotXml != other.m_autoStyleInStylesDotXml )
return m_autoStyleInStylesDotXml;
358 for ( uint i = 0 ; i < N_NumTypes ; ++i )
359 if ( m_properties[i].count() != other.m_properties[i].count() )
360 return m_properties[i].count() < other.m_properties[i].count();
361 if ( m_attributes.count() != other.m_attributes.count() )
return m_attributes.count() < other.m_attributes.count();
362 if ( m_maps.count() != other.m_maps.count() )
return m_maps.count() < other.m_maps.count();
364 for ( uint i = 0 ; i < N_NumTypes ; ++i ) {
365 int comp = compareMap( m_properties[i], other.m_properties[i] );
369 int comp = compareMap( m_attributes, other.m_attributes );
372 for ( uint i = 0 ; i < m_maps.count() ; ++i ) {
373 int comp = compareMap( m_maps[i], other.m_maps[i] );
382 if ( m_type != other.m_type )
return false;
383 if ( m_parentName != other.m_parentName )
return false;
384 if ( m_autoStyleInStylesDotXml != other.m_autoStyleInStylesDotXml )
return false;
385 for ( uint i = 0 ; i < N_NumTypes ; ++i )
386 if ( m_properties[i].count() != other.m_properties[i].count() )
388 if ( m_attributes.count() != other.m_attributes.count() )
return false;
389 if ( m_maps.count() != other.m_maps.count() )
return false;
391 for ( uint i = 0 ; i < N_NumTypes ; ++i ) {
392 int comp = compareMap( m_properties[i], other.m_properties[i] );
396 int comp = compareMap( m_attributes, other.m_attributes );
399 for ( uint i = 0 ; i < m_maps.count() ; ++i ) {
400 int comp = compareMap( m_maps[i], other.m_maps[i] );
KoGenStyle(int type=0, const char *familyName=0, const TQString &parentName=TQString())
Start the definition of a new style.
bool operator<(const KoGenStyle &other) const
TQMap requires a complete sorting order.
const char * familyName() const
Return the family name.
void writeStyle(KoXmlWriter *writer, KoGenStyles &styles, const char *elementName, const TQString &name, const char *propertiesElementName, bool closeElement=true, bool drawElement=false) const
Write the definition of this style to writer, using the OASIS format.
bool operator==(const KoGenStyle &other) const
Not needed for TQMap, but can still be useful.
void addAttributePt(const TQString &attrName, double attrValue)
Add an attribute which represents a distance, measured in pt The number is written out with the highe...
PropertyType
The types of properties.
@ DefaultType
DefaultType depends on family: e.g.
@ GraphicType
GraphicType is always graphic-properties.
@ TextType
TextType is always text-properties.
@ ParagraphType
ParagraphType is always paragraph-properties.
TQString parentName() const
Return the name of style's parent, if set.
int type() const
Return the type of this style, as set in the constructor.
void addPropertyPt(const TQString &propName, double propValue, PropertyType type=DefaultType)
Add a property which represents a distance, measured in pt The number is written out with the highest...
bool autoStyleInStylesDotXml() const
void addAttribute(const TQString &attrName, const TQString &attrValue)
Add an attribute to the style The difference between property and attributes is a bit oasis-format-sp...
void dump()
Outputs debug information.
const StyleMap & styles() const
Return the entire collection of styles Use this for saving the styles.
KoGenStyle * styleForModification(const TQString &name)
void markStyleForStylesXml(const TQString &name)
Mark a given automatic style as being needed in styles.xml.
const KoGenStyle * style(const TQString &name) const
TQString lookup(const KoGenStyle &style, const TQString &name=TQString(), int flags=NoFlag)
Look up a style in the collection, inserting it if necessary.
A class for writing out XML (to any TQIODevice), with a special attention on performance.
void startElement(const char *tagName, bool indentInside=true)
Start a new element, as a child of the current element.
void endElement()
Terminate the current element.
void addCompleteElement(const char *cstr)
This is quite a special-purpose method, not for everyday use.
void addAttribute(const char *attrName, const TQString &value)
Overloaded version of addAttribute( const char*, const char* ), which is a bit slower because it need...
const char * name(StdAction id)