~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to lib/kofficecore/koStyleStack.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of the KDE project
2
 
   Copyright (c) 2003 Lukas Tinkl <lukas@kde.org>
3
 
   Copyright (c) 2003 David Faure <faure@kde.org>
4
 
 
5
 
   This library is free software; you can redistribute it and/or
6
 
   modify it under the terms of the GNU Library General Public
7
 
   License as published by the Free Software Foundation; either
8
 
   version 2 of the License, or (at your option) any later version.
9
 
 
10
 
   This library is distributed in the hope that it will be useful,
11
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
   Library General Public License for more details.
14
 
 
15
 
   You should have received a copy of the GNU Library General Public License
16
 
   along with this library; see the file COPYING.LIB.  If not, write to
17
 
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 
   Boston, MA 02111-1307, USA.
19
 
*/
20
 
 
21
 
#include "koStyleStack.h"
22
 
#include "koUnit.h"
23
 
#include "kodom.h"
24
 
#include "koxmlns.h"
25
 
 
26
 
#include <kdebug.h>
27
 
 
28
 
//#define DEBUG_STYLESTACK
29
 
 
30
 
KoStyleStack::KoStyleStack()
31
 
    : m_styleNSURI( KoXmlNS::style ), m_foNSURI( KoXmlNS::fo )
32
 
{
33
 
    clear();
34
 
}
35
 
 
36
 
KoStyleStack::KoStyleStack( const char* styleNSURI, const char* foNSURI )
37
 
    : m_propertiesTagName( "properties" ), m_styleNSURI( styleNSURI ), m_foNSURI( foNSURI )
38
 
{
39
 
    clear();
40
 
}
41
 
 
42
 
KoStyleStack::~KoStyleStack()
43
 
{
44
 
}
45
 
 
46
 
void KoStyleStack::clear()
47
 
{
48
 
    m_stack.clear();
49
 
#ifdef DEBUG_STYLESTACK
50
 
    kdDebug(30003) << "clear!" << endl;
51
 
#endif
52
 
}
53
 
 
54
 
void KoStyleStack::save()
55
 
{
56
 
    m_marks.push( m_stack.count() );
57
 
#ifdef DEBUG_STYLESTACK
58
 
    kdDebug(30003) << "save (level " << m_marks.count() << ") -> index " << m_stack.count() << endl;
59
 
#endif
60
 
}
61
 
 
62
 
void KoStyleStack::restore()
63
 
{
64
 
    Q_ASSERT( !m_marks.isEmpty() );
65
 
    int toIndex = m_marks.pop();
66
 
#ifdef DEBUG_STYLESTACK
67
 
    kdDebug(30003) << "restore (level " << m_marks.count()+1 << ") -> to index " << toIndex << endl;
68
 
#endif
69
 
    Q_ASSERT( toIndex > -1 );
70
 
    Q_ASSERT( toIndex <= (int)m_stack.count() ); // If equal, nothing to remove. If greater, bug.
71
 
    for ( int index = (int)m_stack.count() - 1; index >= toIndex; --index )
72
 
        m_stack.pop_back();
73
 
}
74
 
 
75
 
void KoStyleStack::pop()
76
 
{
77
 
    Q_ASSERT( !m_stack.isEmpty() );
78
 
    m_stack.pop_back();
79
 
#ifdef DEBUG_STYLESTACK
80
 
    kdDebug(30003) << "pop -> count=" << m_stack.count() << endl;
81
 
#endif
82
 
}
83
 
 
84
 
void KoStyleStack::push( const QDomElement& style )
85
 
{
86
 
    m_stack.append( style );
87
 
#ifdef DEBUG_STYLESTACK
88
 
    kdDebug(30003) << "pushed " << style.attributeNS( m_styleNSURI, "name", QString::null ) << " -> count=" << m_stack.count() << endl;
89
 
#endif
90
 
}
91
 
 
92
 
bool KoStyleStack::hasAttribute( const QString& name, const QString& detail ) const
93
 
{
94
 
    QString fullName( name );
95
 
    if ( !detail.isEmpty() )
96
 
    {
97
 
        fullName += '-';
98
 
        fullName += detail;
99
 
    }
100
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
101
 
    while ( it != m_stack.begin() )
102
 
    {
103
 
        --it;
104
 
        QDomElement properties = (*it).namedItem( "style:"+m_propertiesTagName ).toElement();
105
 
        if ( properties.hasAttribute( name ) ||
106
 
             ( !detail.isEmpty() && properties.hasAttribute( fullName ) ) )
107
 
            return true;
108
 
    }
109
 
    return false;
110
 
}
111
 
 
112
 
QString KoStyleStack::attribute( const QString& name, const QString& detail ) const
113
 
{
114
 
    QString fullName( name );
115
 
    if ( !detail.isEmpty() )
116
 
    {
117
 
        fullName += '-';
118
 
        fullName += detail;
119
 
    }
120
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
121
 
    while ( it != m_stack.begin() )
122
 
    {
123
 
        --it;
124
 
        QDomElement properties = (*it).namedItem( "style:"+m_propertiesTagName ).toElement();
125
 
        if ( properties.hasAttribute( name ) )
126
 
            return properties.attribute( name );
127
 
        if ( !detail.isEmpty() && properties.hasAttribute( fullName ) )
128
 
            return properties.attribute( fullName );
129
 
    }
130
 
    return QString::null;
131
 
}
132
 
 
133
 
QString KoStyleStack::attributeNS( const char* nsURI, const char* name, const char* detail ) const
134
 
{
135
 
    QString fullName( name );
136
 
    if ( detail )
137
 
    {
138
 
        fullName += '-';
139
 
        fullName += detail;
140
 
    }
141
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
142
 
    while ( it != m_stack.begin() )
143
 
    {
144
 
        --it;
145
 
        QDomElement properties = KoDom::namedItemNS( *it, m_styleNSURI, m_propertiesTagName );
146
 
        if ( properties.hasAttributeNS( nsURI, name ) )
147
 
            return properties.attributeNS( nsURI, name, QString::null );
148
 
        if ( detail && properties.hasAttributeNS( nsURI, fullName ) )
149
 
            return properties.attributeNS( nsURI, fullName, QString::null );
150
 
    }
151
 
    return QString::null;
152
 
}
153
 
 
154
 
bool KoStyleStack::hasAttributeNS( const char* nsURI, const char* name, const char* detail ) const
155
 
{
156
 
    QString fullName( name );
157
 
    if ( detail )
158
 
    {
159
 
        fullName += '-';
160
 
        fullName += detail;
161
 
    }
162
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
163
 
    while ( it != m_stack.begin() )
164
 
    {
165
 
        --it;
166
 
        QDomElement properties = KoDom::namedItemNS( *it, m_styleNSURI, m_propertiesTagName );
167
 
        if ( properties.hasAttributeNS( nsURI, name ) ||
168
 
             ( detail && properties.hasAttributeNS( nsURI, fullName ) ) )
169
 
            return true;
170
 
    }
171
 
    return false;
172
 
}
173
 
 
174
 
// Font size is a bit special. "115%" applies to "the fontsize of the parent style".
175
 
// This can be generalized though (hasAttributeThatCanBePercentOfParent() ? :)
176
 
// Although, if we also add support for fo:font-size-rel here then it's not general anymore.
177
 
double KoStyleStack::fontSize() const
178
 
{
179
 
    const QString name = "font-size";
180
 
    double percent = 1;
181
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end(); // reverse iterator
182
 
 
183
 
    while ( it != m_stack.begin() )
184
 
    {
185
 
        --it;
186
 
        QDomElement properties = KoDom::namedItemNS( *it, m_styleNSURI, m_propertiesTagName ).toElement();
187
 
        if ( properties.hasAttributeNS( m_foNSURI, name ) ) {
188
 
            const QString value = properties.attributeNS( m_foNSURI, name, QString::null );
189
 
            if ( value.endsWith( "%" ) )
190
 
                percent *= value.left( value.length() - 1 ).toDouble() / 100.0;
191
 
            else
192
 
                return percent * KoUnit::parseValue( value ); // e.g. 12pt
193
 
        }
194
 
    }
195
 
    return 0;
196
 
}
197
 
 
198
 
bool KoStyleStack::hasChildNode(const QString & name) const
199
 
{
200
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
201
 
    while ( it != m_stack.begin() )
202
 
    {
203
 
        --it;
204
 
        QDomElement properties = (*it).namedItem( "style:"+m_propertiesTagName ).toElement();
205
 
        if ( !properties.namedItem( name ).isNull() )
206
 
            return true;
207
 
    }
208
 
 
209
 
    return false;
210
 
}
211
 
 
212
 
QDomElement KoStyleStack::childNode(const QString & name) const
213
 
{
214
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
215
 
 
216
 
    while ( it != m_stack.begin() )
217
 
    {
218
 
        --it;
219
 
        QDomElement properties = (*it).namedItem( "style:"+m_propertiesTagName ).toElement();
220
 
        if ( !properties.namedItem( name ).isNull() )
221
 
            return properties.namedItem( name ).toElement();
222
 
    }
223
 
 
224
 
    return QDomElement();          // a null element
225
 
}
226
 
 
227
 
bool KoStyleStack::hasChildNodeNS( const char* nsURI, const char* localName ) const
228
 
{
229
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
230
 
    while ( it != m_stack.begin() )
231
 
    {
232
 
        --it;
233
 
        QDomElement properties = KoDom::namedItemNS( *it, m_styleNSURI, m_propertiesTagName );
234
 
        if ( !KoDom::namedItemNS( properties, nsURI, localName ).isNull() )
235
 
            return true;
236
 
    }
237
 
 
238
 
    return false;
239
 
}
240
 
 
241
 
QDomElement KoStyleStack::childNodeNS( const char* nsURI, const char* localName) const
242
 
{
243
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
244
 
 
245
 
    while ( it != m_stack.begin() )
246
 
    {
247
 
        --it;
248
 
        QDomElement properties = KoDom::namedItemNS( *it, m_styleNSURI, m_propertiesTagName );
249
 
        QDomElement e = KoDom::namedItemNS( properties, nsURI, localName );
250
 
        if ( !e.isNull() )
251
 
            return e;
252
 
    }
253
 
 
254
 
    return QDomElement();          // a null element
255
 
}
256
 
 
257
 
bool KoStyleStack::isUserStyle( const QDomElement& e ) const
258
 
{
259
 
    const QDomElement parent = e.parentNode().toElement();
260
 
    //kdDebug(30003) << k_funcinfo << "tagName=" << e.tagName() << " parent-tagName=" << parent.tagName() << endl;
261
 
    return parent.localName() == "styles" /*&& parent.namespaceURI() == KoXmlNS::office*/;
262
 
}
263
 
 
264
 
QString KoStyleStack::userStyleName() const
265
 
{
266
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
267
 
    while ( it != m_stack.begin() )
268
 
    {
269
 
        --it;
270
 
        //kdDebug(30003) << k_funcinfo << (*it).attributeNS( m_styleNSURI, "name") << endl;
271
 
        if ( isUserStyle( *it ) )
272
 
            return (*it).attributeNS( m_styleNSURI, "name", QString::null );
273
 
    }
274
 
    // Can this ever happen?
275
 
    return "Standard";
276
 
}
277
 
 
278
 
QString KoStyleStack::userStyleDisplayName() const
279
 
{
280
 
    QValueList<QDomElement>::ConstIterator it = m_stack.end();
281
 
    while ( it != m_stack.begin() )
282
 
    {
283
 
        --it;
284
 
        //kdDebug(30003) << k_funcinfo << (*it).attributeNS( m_styleNSURI, "display-name") << endl;
285
 
        if ( isUserStyle( *it ) )
286
 
            return (*it).attributeNS( m_styleNSURI, "display-name", QString::null );
287
 
    }
288
 
    return QString::null; // no display name, this can happen since it's optional
289
 
}
290
 
 
291
 
void KoStyleStack::setTypeProperties( const char* typeProperties )
292
 
{
293
 
    m_propertiesTagName = typeProperties == 0 ? QCString( "properties" ) : ( QCString( typeProperties ) + "-properties" );
294
 
}