~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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 <KGlobalSettings>
 
27
#include <KStandardDirs>
 
28
 
 
29
#include <QComboBox>
 
30
#include <QDomElement>
 
31
#include <QFrame>
 
32
#include <QLineEdit>
 
33
#include <QStack>
 
34
#include <QStyleFactory>
 
35
#include <QWidget>
 
36
 
 
37
void
 
38
parseSize(const QString &s, DataPoint &pt)
 
39
{
 
40
    if (s.isEmpty())
 
41
        return;
 
42
 
 
43
    int p;
 
44
    if (s == "box") { // box value
 
45
        pt.type = DTbox;
 
46
        pt.val = 0;
 
47
    } else if (s == "scale") {
 
48
        pt.type = DTscale;
 
49
        pt.val = 0;
 
50
    } else if ((p = s.indexOf('%')) >= 0) { // percent value
 
51
        pt.type = DTpercent;
 
52
        QString sCopy = s;
 
53
        sCopy.remove(p, 1);
 
54
        pt.levels = 0;
 
55
        while ((p = sCopy.indexOf('^')) >= 0) {
 
56
            sCopy.remove(p, 1);
 
57
            pt.levels++;
 
58
        }
 
59
        sCopy.replace(',', '.');
 
60
        pt.val = (int)sCopy.toDouble();
 
61
    } else { // int value
 
62
        pt.type = DTpixel;
 
63
        QString sCopy = s;
 
64
        if (sCopy.at(0) == '-') {
 
65
            sCopy.remove(0, 1);
 
66
            pt.type = DTnpixel;
 
67
        }
 
68
        sCopy.replace(',', '.');
 
69
        pt.val = (int)sCopy.toDouble();
 
70
    }
 
71
}
 
72
 
 
73
 
 
74
static QString
 
75
getword(QString &rs)
 
76
{
 
77
    int splitAt = rs.lastIndexOf(' ') + 1;
 
78
    QString s(rs.mid(splitAt));
 
79
    rs.truncate(splitAt - 1);
 
80
    return s;
 
81
}
 
82
 
 
83
void
 
84
parseFont(const QString &is, FontType &ft)
 
85
{
 
86
    if (is.isNull())
 
87
        return;
 
88
    QString rs(is.simplified());
 
89
    if ((ft.present = !rs.isEmpty())) {
 
90
        QString s(getword(rs));
 
91
        bool ok;
 
92
        if (s.endsWith("px")) {
 
93
            int ps = s.left(s.length() - 2).toInt(&ok);
 
94
            if (ok) {
 
95
                ft.font.setPixelSize(ps);
 
96
                s = getword(rs);
 
97
            }
 
98
        } else {
 
99
            double ps = s.toDouble(&ok);
 
100
            if (ok) {
 
101
                ft.font.setPointSizeF(ps);
 
102
                s = getword(rs);
 
103
            }
 
104
        }
 
105
        forever {
 
106
            QString ss(s.toLower());
 
107
            if (ss == "oblique")
 
108
                ft.font.setStyle(QFont::StyleOblique);
 
109
            else if (ss == "italic")
 
110
                ft.font.setStyle(QFont::StyleItalic);
 
111
            else if (ss == "ultra-light")
 
112
                ft.font.setWeight(13);
 
113
            else if (ss == "light")
 
114
                ft.font.setWeight(QFont::Light);
 
115
            else if (ss == "medium")
 
116
                ft.font.setWeight(50);
 
117
            else if (ss == "semi-bold")
 
118
                ft.font.setWeight(QFont::DemiBold);
 
119
            else if (ss == "bold")
 
120
                ft.font.setWeight(QFont::Bold);
 
121
            else if (ss == "ultra-bold")
 
122
                ft.font.setWeight(QFont::Black);
 
123
            else if (ss == "heavy")
 
124
                ft.font.setWeight(99);
 
125
            else if (ss == "ultra-condensed")
 
126
                ft.font.setStretch(QFont::UltraCondensed);
 
127
            else if (ss == "extra-condensed")
 
128
                ft.font.setStretch(QFont::ExtraCondensed);
 
129
            else if (ss == "condensed")
 
130
                ft.font.setStretch(QFont::Condensed);
 
131
            else if (ss == "semi-condensed")
 
132
                ft.font.setStretch(QFont::SemiCondensed);
 
133
            else if (ss == "semi-expanded")
 
134
                ft.font.setStretch(QFont::SemiExpanded);
 
135
            else if (ss == "expanded")
 
136
                ft.font.setStretch(QFont::Expanded);
 
137
            else if (ss == "extra-expanded")
 
138
                ft.font.setStretch(QFont::ExtraExpanded);
 
139
            else if (ss == "ultra-expanded")
 
140
                ft.font.setStretch(QFont::UltraExpanded);
 
141
            else if (ss == "normal" || // no-op
 
142
                     ss == "small-caps" || // this and following ignored
 
143
                     ss == "not-rotated" || ss == "south" || ss == "upside-down" ||
 
144
                     ss == "north" ||
 
145
                     ss == "rotated-left" || ss == "east" ||
 
146
                     ss == "rotated-right" || ss == "west")
 
147
                ;
 
148
            else
 
149
                break;
 
150
            s = getword(rs);
 
151
        }
 
152
        if (!rs.isEmpty())
 
153
            rs.append(' ').append(s);
 
154
        else
 
155
            rs = s;
 
156
        QStringList ffs = rs.split(QRegExp(" ?, ?"), QString::SkipEmptyParts);
 
157
        if (!ffs.isEmpty()) {
 
158
            foreach (const QString& ff, ffs) {
 
159
                ft.font.setFamily(ff);
 
160
                if (ft.font.exactMatch())
 
161
                    return;
 
162
            }
 
163
            ft.font.setFamily(ffs.first());
 
164
        }
 
165
    }
 
166
    // don't inherit settings from parent widgets
 
167
    ft.font.setFamily(ft.font.family());
 
168
    if (ft.font.pixelSize() > 0)
 
169
        ft.font.setPixelSize(ft.font.pixelSize());
 
170
    else
 
171
        ft.font.setPointSize(ft.font.pointSize());
 
172
    ft.font.setStyle(ft.font.style());
 
173
    ft.font.setStretch(ft.font.stretch());
 
174
    ft.font.setWeight(ft.font.weight());
 
175
}
 
176
 
 
177
void
 
178
parseFont(const QDomElement &el, FontType &ft)
 
179
{
 
180
    parseFont(el.attribute("font", QString()), ft);
 
181
}
 
182
 
 
183
 
 
184
bool
 
185
parseColor(const QString &s, const QString &a, QColor &color)
 
186
{
 
187
    bool ok = false;
 
188
    if (s.startsWith('#')) {
 
189
        uint hexColor = s.mid(1).toUInt(&ok, 16);
 
190
        if (ok) {
 
191
            if (s.length() == 9)
 
192
                color.setRgba(hexColor);
 
193
            else
 
194
                color.setRgb(hexColor);
 
195
        } else {
 
196
            return false;
 
197
        }
 
198
    } else if (!s.isNull() && s.isEmpty()) {
 
199
        color = QColor();
 
200
        ok = true;
 
201
    }
 
202
    if (!a.isEmpty()) {
 
203
        float fltAlpha = a.toFloat(&ok);
 
204
        if (ok)
 
205
            color.setAlphaF(fltAlpha);
 
206
    }
 
207
    return ok;
 
208
}
 
209
 
 
210
void
 
211
parseColor(const QDomElement &el, QColor &color)
 
212
{
 
213
    parseColor(el.attribute("color"), el.attribute("alpha"), color);
 
214
}
 
215
 
 
216
 
 
217
static void
 
218
parsePalEnt(const QDomElement &el, const QString &pfx, const QString &core,
 
219
            QPalette &pal, QPalette::ColorGroup cg, QPalette::ColorRole cr)
 
220
{
 
221
    QColor col = pal.color(cg, cr);
 
222
    if (parseColor(el.attribute(pfx + core + "-color"),
 
223
                   el.attribute(pfx + core + "-alpha"), col))
 
224
        pal.setColor(cg, cr, col);
 
225
}
 
226
 
 
227
static void
 
228
parsePalEnt(const QDomElement &el, const QString &core,
 
229
            QPalette &pal, QPalette::ColorRole cr)
 
230
{
 
231
    parsePalEnt(el, "all-", core, pal, QPalette::Active, cr);
 
232
    parsePalEnt(el, "all-", core, pal, QPalette::Inactive, cr);
 
233
    parsePalEnt(el, "all-", core, pal, QPalette::Disabled, cr);
 
234
    parsePalEnt(el, QString(), core, pal, QPalette::Active, cr);
 
235
    parsePalEnt(el, QString(), core, pal, QPalette::Inactive, cr);
 
236
    parsePalEnt(el, "active-", core, pal, QPalette::Active, cr);
 
237
    parsePalEnt(el, "inactive-", core, pal, QPalette::Inactive, cr);
 
238
    parsePalEnt(el, "disabled-", core, pal, QPalette::Disabled, cr);
 
239
}
 
240
 
 
241
void
 
242
parseStyle(const QDomElement &el, StyleType &style)
 
243
{
 
244
    parseFont(el, style.font);
 
245
    parseFont(el.attribute("edit-font", QString()), style.editfont);
 
246
    QString colorscheme = el.attribute("colorscheme", QString());
 
247
    if (!colorscheme.isNull()) {
 
248
        colorscheme = KStandardDirs::locate("data", "color-schemes/" + colorscheme + ".colors");
 
249
        if (!colorscheme.isEmpty()) {
 
250
            KSharedConfigPtr config = KSharedConfig::openConfig(colorscheme, KConfig::SimpleConfig);
 
251
            style.palette = KGlobalSettings::createApplicationPalette(config);
 
252
        }
 
253
    }
 
254
    parsePalEnt(el, "window", style.palette, QPalette::Window);
 
255
    parsePalEnt(el, "window-text", style.palette, QPalette::WindowText);
 
256
    parsePalEnt(el, "base", style.palette, QPalette::Base);
 
257
    parsePalEnt(el, "alternate-base", style.palette, QPalette::AlternateBase);
 
258
    parsePalEnt(el, "text", style.palette, QPalette::Text);
 
259
    parsePalEnt(el, "highlight", style.palette, QPalette::Highlight);
 
260
    parsePalEnt(el, "highlighted-text", style.palette, QPalette::HighlightedText);
 
261
    parsePalEnt(el, "button", style.palette, QPalette::Button);
 
262
    parsePalEnt(el, "button-text", style.palette, QPalette::ButtonText);
 
263
    parsePalEnt(el, "bright-text", style.palette, QPalette::BrightText);
 
264
    QString frame = el.attribute("frame", QString());
 
265
    if (!frame.isNull())
 
266
        style.frame = frame == "true";
 
267
    QString guistyle = el.attribute("guistyle", QString());
 
268
    if (!guistyle.isNull())
 
269
        style.guistyle = QStyleFactory::create(guistyle);
 
270
}
 
271
 
 
272
void
 
273
setWidgetAttribs(QWidget *widget, const StyleType &style, bool frame)
 
274
{
 
275
    widget->setFont(
 
276
        (style.editfont.present &&
 
277
         (qobject_cast<QLineEdit *>(widget) ||
 
278
          qobject_cast<QComboBox *>(widget) ||
 
279
          widget->objectName() == "edit")) ?
 
280
            style.editfont.font : style.font.font);
 
281
    
 
282
    if (!frame) {
 
283
        if (QFrame *frm = qobject_cast<QFrame *>(widget)) {
 
284
            if ((widget->windowFlags() & Qt::WindowType_Mask) == Qt::Widget)
 
285
                frm->setFrameStyle(QFrame::NoFrame);
 
286
        } else if (QLineEdit *le = qobject_cast<QLineEdit *>(widget)) {
 
287
            le->setFrame(false);
 
288
        } else if (QComboBox *cb = qobject_cast<QComboBox *>(widget)) {
 
289
            cb->setFrame(false);
 
290
        }
 
291
    }
 
292
 
 
293
    foreach (QObject *child, widget->children())
 
294
        if (QWidget *cw = qobject_cast<QWidget *>(child))
 
295
            setWidgetAttribs(cw, style, frame ||
 
296
                (widget->testAttribute(Qt::WA_OpaquePaintEvent) ||
 
297
                 (widget->autoFillBackground() &&
 
298
                  !widget->testAttribute(Qt::WA_NoSystemBackground) &&
 
299
                  widget->palette().brush(widget->backgroundRole()).isOpaque())));
 
300
}
 
301
 
 
302
static QString prefix;
 
303
static QStack<QString> prefixes;
 
304
 
 
305
#define dbgs ((debugLevel & DEBUG_THEMING) ? QDebug(QtDebugMsg) : kDebugDevNull())
 
306
 
 
307
QDebug
 
308
enter(const char *fct)
 
309
{
 
310
    prefixes.push(prefix);
 
311
    prefix.replace('-', ' ').append(" |-");
 
312
    if (prefixes.top().isEmpty())
 
313
        return dbgs << fct;
 
314
    return dbgs << (qPrintable(prefixes.top()) + 1) << fct;
 
315
}
 
316
 
 
317
QDebug
 
318
debug()
 
319
{
 
320
    if (prefix.isEmpty())
 
321
        return dbgs;
 
322
    return dbgs << (qPrintable(prefix) + 1);
 
323
}
 
324
 
 
325
QDebug
 
326
leave()
 
327
{
 
328
    prefix[prefix.length() - 2] = '\\';
 
329
    QString nprefix(prefix);
 
330
    prefix = prefixes.pop();
 
331
    return dbgs << (qPrintable(nprefix) + 1);
 
332
}