~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to kdm/kfrontend/themer/parse.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2003 by Unai Garro <ugarro@users.sourceforge.net>
 
3
 *  Copyright (C) 2004 by Enrico Ros <rosenric@dei.unipd.it>
 
4
 *  Copyright (C) 2004 by Stephan Kulow <coolo@kde.org>
 
5
 *  Copyright (C) 2004,2006 by Oswald Buddenhagen <ossi@kde.org>
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
20
 */
 
21
 
 
22
#include "parse.h"
 
23
 
 
24
#include <kdm_greet.h>
 
25
 
 
26
#include <QComboBox>
 
27
#include <QDomElement>
 
28
#include <QFrame>
 
29
#include <QLineEdit>
 
30
#include <QStack>
 
31
#include <QWidget>
 
32
 
 
33
void
 
34
parseSize( const QString &s, DataPoint &pt )
 
35
{
 
36
        if (s.isEmpty())
 
37
                return;
 
38
 
 
39
        int p;
 
40
        if (s == "box") {       // box value
 
41
                pt.type = DTbox;
 
42
                pt.val = 0;
 
43
        } else if (s == "scale") {
 
44
                pt.type = DTscale;
 
45
                pt.val = 0;
 
46
        } else if ((p = s.indexOf( '%' )) >= 0) {       // percent value
 
47
                pt.type = DTpercent;
 
48
                QString sCopy = s;
 
49
                sCopy.remove( p, 1 );
 
50
                pt.levels = 0;
 
51
                while ((p = sCopy.indexOf( '^' )) >= 0) {
 
52
                        sCopy.remove( p, 1 );
 
53
                        pt.levels++;
 
54
                }
 
55
                sCopy.replace( ',', '.' );
 
56
                pt.val = (int)sCopy.toDouble();
 
57
        } else {                // int value
 
58
                pt.type = DTpixel;
 
59
                QString sCopy = s;
 
60
                if (sCopy.at( 0 ) == '-') {
 
61
                        sCopy.remove( 0, 1 );
 
62
                        pt.type = DTnpixel;
 
63
                }
 
64
                sCopy.replace( ',', '.' );
 
65
                pt.val = (int)sCopy.toDouble();
 
66
        }
 
67
}
 
68
 
 
69
 
 
70
static QString
 
71
getword( QString &rs )
 
72
{
 
73
        int splitAt = rs.lastIndexOf( ' ' ) + 1;
 
74
        QString s( rs.mid( splitAt ) );
 
75
        rs.truncate( splitAt - 1 );
 
76
        return s;
 
77
}
 
78
 
 
79
void
 
80
parseFont( const QString &is, FontType &ft )
 
81
{
 
82
        if (is.isNull())
 
83
                return;
 
84
        QString rs( is.simplified() );
 
85
        if ((ft.present = !rs.isEmpty())) {
 
86
                QString s( getword( rs ) );
 
87
                bool ok;
 
88
                if (s.endsWith( "px" )) {
 
89
                        int ps = s.left( s.length() - 2 ).toInt( &ok );
 
90
                        if (ok) {
 
91
                                ft.font.setPixelSize( ps );
 
92
                                s = getword( rs );
 
93
                        }
 
94
                } else {
 
95
                        double ps = s.toDouble( &ok );
 
96
                        if (ok) {
 
97
                                ft.font.setPointSizeF( ps );
 
98
                                s = getword( rs );
 
99
                        }
 
100
                }
 
101
                forever {
 
102
                        QString ss( s.toLower() );
 
103
                        if (ss == "oblique")
 
104
                                ft.font.setStyle( QFont::StyleOblique );
 
105
                        else if (ss == "italic")
 
106
                                ft.font.setStyle( QFont::StyleItalic );
 
107
                        else if (ss == "ultra-light")
 
108
                                ft.font.setWeight( 13 );
 
109
                        else if (ss == "light")
 
110
                                ft.font.setWeight( QFont::Light );
 
111
                        else if (ss == "medium")
 
112
                                ft.font.setWeight( 50 );
 
113
                        else if (ss == "semi-bold")
 
114
                                ft.font.setWeight( QFont::DemiBold );
 
115
                        else if (ss == "bold")
 
116
                                ft.font.setWeight( QFont::Bold );
 
117
                        else if (ss == "ultra-bold")
 
118
                                ft.font.setWeight( QFont::Black );
 
119
                        else if (ss == "heavy")
 
120
                                ft.font.setWeight( 99 );
 
121
                        else if (ss == "ultra-condensed")
 
122
                                ft.font.setStretch( QFont::UltraCondensed );
 
123
                        else if (ss == "extra-condensed")
 
124
                                ft.font.setStretch( QFont::ExtraCondensed );
 
125
                        else if (ss == "condensed")
 
126
                                ft.font.setStretch( QFont::Condensed );
 
127
                        else if (ss == "semi-condensed")
 
128
                                ft.font.setStretch( QFont::SemiCondensed );
 
129
                        else if (ss == "semi-expanded")
 
130
                                ft.font.setStretch( QFont::SemiExpanded );
 
131
                        else if (ss == "expanded")
 
132
                                ft.font.setStretch( QFont::Expanded );
 
133
                        else if (ss == "extra-expanded")
 
134
                                ft.font.setStretch( QFont::ExtraExpanded );
 
135
                        else if (ss == "ultra-expanded")
 
136
                                ft.font.setStretch( QFont::UltraExpanded );
 
137
                        else if (ss == "normal" || // no-op
 
138
                                 ss == "small-caps" || // this and following ignored
 
139
                                 ss == "not-rotated" || ss == "south" || ss == "upside-down" ||
 
140
                                 ss == "north" ||
 
141
                                 ss == "rotated-left" || ss == "east" ||
 
142
                                 ss == "rotated-right" || ss == "west")
 
143
                        {
 
144
                        } else
 
145
                                break;
 
146
                        s = getword( rs );
 
147
                }
 
148
                if (!rs.isEmpty())
 
149
                        rs.append( ' ' ).append( s );
 
150
                else
 
151
                        rs = s;
 
152
                QStringList ffs = rs.split( QRegExp( " ?, ?" ), QString::SkipEmptyParts );
 
153
                if (!ffs.isEmpty()) {
 
154
                        foreach (QString ff, ffs) {
 
155
                                ft.font.setFamily( ff );
 
156
                                if (ft.font.exactMatch())
 
157
                                        return;
 
158
                        }
 
159
                        ft.font.setFamily( ffs.first() );
 
160
                }
 
161
        }
 
162
        // don't inherit settings from parent widgets
 
163
        ft.font.setFamily( ft.font.family() );
 
164
        if (ft.font.pixelSize() > 0)
 
165
                ft.font.setPixelSize( ft.font.pixelSize() );
 
166
        else
 
167
                ft.font.setPointSize( ft.font.pointSize() );
 
168
        ft.font.setStyle( ft.font.style() );
 
169
        ft.font.setStretch( ft.font.stretch() );
 
170
        ft.font.setWeight( ft.font.weight() );
 
171
}
 
172
 
 
173
void
 
174
parseFont( const QDomElement &el, FontType &ft )
 
175
{
 
176
        parseFont( el.attribute( "font", QString() ), ft );
 
177
}
 
178
 
 
179
 
 
180
void
 
181
parseColor( const QString &s, const QString &a, QColor &color )
 
182
{
 
183
        if (!s.length() || s.at( 0 ) != '#')
 
184
                return;
 
185
        bool ok;
 
186
        QString sCopy = s;
 
187
        uint hexColor = sCopy.remove( 0, 1 ).toUInt( &ok, 16 );
 
188
        if (ok) {
 
189
                if (sCopy.length() == 8)
 
190
                        color.setRgba( hexColor );
 
191
                else {
 
192
                        color.setRgb( hexColor );
 
193
                        if (!a.isNull())
 
194
                                color.setAlpha( int(a.toFloat() * 255) );
 
195
                }
 
196
        }
 
197
}
 
198
 
 
199
void
 
200
parseColor( const QDomElement &el, QColor &color )
 
201
{
 
202
        parseColor( el.attribute( "color", QString() ),
 
203
                    el.attribute( "alpha", "1.0" ), color );
 
204
}
 
205
 
 
206
 
 
207
static void
 
208
parsePalEnt( const QDomElement &el, const QString &core, QPalette &pal, QPalette::ColorRole cr )
 
209
{
 
210
        QColor col;
 
211
        parseColor( el.attribute( core + "-color", QString() ), el.attribute( core + "-alpha", "1.0" ), col );
 
212
        if (col.isValid())
 
213
                pal.setColor( cr, col );
 
214
}
 
215
 
 
216
void
 
217
parseStyle( const QDomElement &el, StyleType &style )
 
218
{
 
219
        parseFont( el, style.font );
 
220
        parseFont( el.attribute( "edit-font", QString() ), style.editfont );
 
221
        parsePalEnt( el, "window", style.palette, QPalette::Window );
 
222
        parsePalEnt( el, "window-text", style.palette, QPalette::WindowText );
 
223
        parsePalEnt( el, "base", style.palette, QPalette::Base );
 
224
        parsePalEnt( el, "alternate-base", style.palette, QPalette::AlternateBase );
 
225
        parsePalEnt( el, "text", style.palette, QPalette::Text );
 
226
        parsePalEnt( el, "highlight", style.palette, QPalette::Highlight );
 
227
        parsePalEnt( el, "highlighted-text", style.palette, QPalette::HighlightedText );
 
228
        parsePalEnt( el, "button", style.palette, QPalette::Button );
 
229
        parsePalEnt( el, "button-text", style.palette, QPalette::ButtonText );
 
230
        parsePalEnt( el, "bright-text", style.palette, QPalette::BrightText );
 
231
        QString frame = el.attribute( "frame", QString() );
 
232
        if (!frame.isNull())
 
233
                style.frame = frame == "true";
 
234
}
 
235
 
 
236
static void
 
237
setWidgetAttribs( QWidget *widget, const StyleType &style, bool frame )
 
238
{
 
239
        widget->setFont(
 
240
                (style.editfont.present &&
 
241
                 (qobject_cast<QLineEdit *>(widget) ||
 
242
                  qobject_cast<QComboBox *>(widget) ||
 
243
                  widget->objectName() == "edit")) ?
 
244
                        style.editfont.font : style.font.font );
 
245
        
 
246
        if (!frame) {
 
247
                if (QFrame *frm = qobject_cast<QFrame *>(widget)) {
 
248
                        if ((widget->windowFlags() & Qt::WindowType_Mask) == Qt::Widget)
 
249
                                frm->setFrameStyle( QFrame::NoFrame );
 
250
                } else if (QLineEdit *le = qobject_cast<QLineEdit *>(widget))
 
251
                        le->setFrame( false );
 
252
                else if (QComboBox *cb = qobject_cast<QComboBox *>(widget))
 
253
                        cb->setFrame( false );
 
254
        }
 
255
 
 
256
        foreach (QObject *child, widget->children())
 
257
                if (QWidget *cw = qobject_cast<QWidget *>(child))
 
258
                        setWidgetAttribs( cw, style, frame ||
 
259
                                (widget->testAttribute( Qt::WA_OpaquePaintEvent ) ||
 
260
                                 (widget->autoFillBackground() &&
 
261
                                  !widget->testAttribute( Qt::WA_NoSystemBackground ) &&
 
262
                                  widget->palette().brush( widget->backgroundRole() ).isOpaque() )));
 
263
}
 
264
 
 
265
void
 
266
setWidgetAttribs( QWidget *widget, const StyleType &style )
 
267
{
 
268
        widget->setPalette( style.palette );
 
269
        if (style.palette.isBrushSet( QPalette::Normal, QPalette::Window ))
 
270
                widget->setAutoFillBackground( true );
 
271
        setWidgetAttribs( widget, style, style.frame );
 
272
}
 
273
 
 
274
 
 
275
#ifndef KDE_NO_DEBUG_OUTPUT
 
276
static QString prefix;
 
277
static QStack<QString> prefixes;
 
278
 
 
279
# define dbgs kDebug( (debugLevel & DEBUG_THEMING) != 0 )
 
280
 
 
281
QDebug
 
282
enter( const char *fct )
 
283
{
 
284
        prefixes.push( prefix );
 
285
        prefix.replace( '-', ' ' ).append( "|- " );
 
286
        return dbgs << prefixes.top() << fct << " ";
 
287
}
 
288
 
 
289
QDebug
 
290
debug()
 
291
{
 
292
        return dbgs << prefix;
 
293
}
 
294
 
 
295
QDebug
 
296
leave()
 
297
{
 
298
        prefix[prefix.length() - 3] = '\\';
 
299
        QString nprefix( prefix );
 
300
        prefix = prefixes.pop();
 
301
        return dbgs << nprefix;
 
302
}
 
303
#endif