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

« back to all changes in this revision

Viewing changes to kdm/kfrontend/themer/kdmthemer.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 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 "kdmthemer.h"
 
23
#include "kdmitem.h"
 
24
#include "kdmpixmap.h"
 
25
#include "kdmrect.h"
 
26
#include "kdmlist.h"
 
27
#include "kdmlabel.h"
 
28
#include "kdmbutton.h"
 
29
 
 
30
#include <kdm_greet.h> // debug stuff
 
31
#include <kfdialog.h> // kfmsgbox
 
32
 
 
33
#include <klocale.h>
 
34
#include <kconfig.h>
 
35
#include <kconfiggroup.h>
 
36
 
 
37
#include <QEvent>
 
38
#include <QMouseEvent>
 
39
#include <QPaintEvent>
 
40
#include <QFile>
 
41
#include <QFileInfo>
 
42
#include <QPainter>
 
43
 
 
44
#include <unistd.h>
 
45
 
 
46
/*
 
47
 * KdmThemer. The main theming interface
 
48
 */
 
49
KdmThemer::KdmThemer(const QString &_filename,
 
50
                     const QMap<QString, bool> &showTypes, QWidget *w)
 
51
    : QObject()
 
52
    , m_showTypes(showTypes)
 
53
    , rootItem(0)
 
54
    , m_geometryOutdated(true)
 
55
    , m_geometryInvalid(true)
 
56
    , m_widget(0)
 
57
{
 
58
    // read the XML file and create DOM tree
 
59
    QString filename = _filename;
 
60
    if (!::access(QFile::encodeName(filename + "/KdmGreeterTheme.desktop"), R_OK)) {
 
61
        KConfig _cfg(filename + "/KdmGreeterTheme.desktop", KConfig::SimpleConfig);
 
62
        KConfigGroup cfg(&_cfg, "KdmGreeterTheme");
 
63
        filename += '/' + cfg.readEntry("Greeter");
 
64
    }
 
65
    QFile opmlFile(filename);
 
66
    if (!opmlFile.open(QIODevice::ReadOnly)) {
 
67
        KFMsgBox::box(w, errorbox, i18n("Cannot open theme file %1" , filename));
 
68
        return;
 
69
    }
 
70
    QDomDocument domTree;
 
71
    if (!domTree.setContent(&opmlFile)) {
 
72
        KFMsgBox::box(w, errorbox, i18n("Cannot parse theme file %1" , filename));
 
73
        return;
 
74
    }
 
75
    // generate all the items defined in the theme
 
76
    const QDomElement &theme = domTree.documentElement();
 
77
    // Get its tag, and check it's correct ("greeter")
 
78
    if (theme.tagName() != "greeter") {
 
79
        KFMsgBox::box(w, errorbox, i18n("%1 does not seem to be a correct theme file" , filename));
 
80
        return;
 
81
    }
 
82
 
 
83
    // Set the root (screen) item
 
84
    rootItem = new KdmRect(this, theme);
 
85
 
 
86
    basedir = QFileInfo(filename).absolutePath();
 
87
 
 
88
    generateItems(rootItem, theme);
 
89
}
 
90
 
 
91
KdmThemer::~KdmThemer()
 
92
{
 
93
}
 
94
 
 
95
void
 
96
KdmThemer::setWidget(QWidget *w)
 
97
{
 
98
    if ((m_widget = w)) {
 
99
        rootItem->updateVisible();
 
100
        m_widget->setStyle(rootItem->style.guistyle);
 
101
        m_widget->setPalette(rootItem->style.palette);
 
102
        m_widget->setFont(rootItem->style.font.font);
 
103
        rootItem->plugActions();
 
104
    }
 
105
}
 
106
 
 
107
KdmItem *
 
108
KdmThemer::findNode(const QString &item) const
 
109
{
 
110
    return rootItem->findChild<KdmItem *>(item);
 
111
}
 
112
 
 
113
void
 
114
KdmThemer::slotNeedPlacement()
 
115
{
 
116
    m_geometryOutdated = m_geometryInvalid = true;
 
117
    if (widget())
 
118
        widget()->update();
 
119
}
 
120
 
 
121
void
 
122
KdmThemer::slotNeedPlugging()
 
123
{
 
124
    if (widget())
 
125
        rootItem->plugActions();
 
126
}
 
127
 
 
128
void
 
129
KdmThemer::update(int x, int y, int w, int h)
 
130
{
 
131
    if (widget())
 
132
        widget()->update(x, y, w, h);
 
133
}
 
134
 
 
135
// BEGIN other functions
 
136
 
 
137
void
 
138
KdmThemer::widgetEvent(QEvent *e)
 
139
{
 
140
    if (!rootItem)
 
141
        return;
 
142
    switch (e->type()) {
 
143
    case QEvent::MouseMove: {
 
144
        QMouseEvent *me = static_cast<QMouseEvent *>(e);
 
145
        rootItem->mouseEvent(me->x(), me->y());
 
146
        break; }
 
147
    case QEvent::MouseButtonPress: {
 
148
        QMouseEvent *me = static_cast<QMouseEvent *>(e);
 
149
        rootItem->mouseEvent(me->x(), me->y(), true);
 
150
        break; }
 
151
    case QEvent::MouseButtonRelease: {
 
152
        QMouseEvent *me = static_cast<QMouseEvent *>(e);
 
153
        rootItem->mouseEvent(me->x(), me->y(), false, true);
 
154
        break; }
 
155
    case QEvent::Resize:
 
156
        m_geometryOutdated = true;
 
157
        widget()->update();
 
158
        break;
 
159
    case QEvent::Paint:
 
160
        if (m_geometryOutdated) {
 
161
            debug() << "==== updating geometry ====";
 
162
            QStack<QSize> ps;
 
163
            QRect rect(QPoint(0, 0), widget()->size());
 
164
            rootItem->setGeometry(ps, rect, m_geometryInvalid);
 
165
            if (debugLevel & DEBUG_THEMING)
 
166
                showStructure();
 
167
            m_geometryOutdated = m_geometryInvalid = false;
 
168
        }
 
169
        {
 
170
            QRect paintRect = static_cast<QPaintEvent *>(e)->rect();
 
171
            //kDebug() << "paint on: " << paintRect;
 
172
 
 
173
            QPainter p(widget());
 
174
            rootItem->paint(&p, paintRect, false, true);
 
175
            rootItem->showWidget();
 
176
        }
 
177
        break;
 
178
    default:
 
179
        break;
 
180
    }
 
181
}
 
182
 
 
183
void
 
184
KdmThemer::paintBackground(QPainter *p, const QRect &rect, bool primaryScreen)
 
185
{
 
186
    debug() << "==== setting background geometry ====";
 
187
    QStack<QSize> ps;
 
188
    rootItem->setGeometry(ps, rect, true);
 
189
    rootItem->paint(p, rect, true, primaryScreen);
 
190
}
 
191
 
 
192
void
 
193
KdmThemer::generateItems(KdmItem *parent, const QDomNode &node)
 
194
{
 
195
    /*
 
196
     * Go through each of the child nodes
 
197
     */
 
198
    const QDomNodeList &subnodeList = node.childNodes();
 
199
    for (int nod = 0; nod < subnodeList.count(); nod++) {
 
200
        QDomNode subnode = subnodeList.item(nod);
 
201
        QDomElement el = subnode.toElement();
 
202
        QString tagName = el.tagName();
 
203
 
 
204
        if (tagName == "item") {
 
205
            QString type = el.attribute("type");
 
206
            KdmItem *newItem;
 
207
            if (type == "label") {
 
208
                newItem = new KdmLabel(parent, subnode);
 
209
            } else if (type == "button") {
 
210
                newItem = new KdmButton(parent, subnode);
 
211
            } else if (type == "pixmap") {
 
212
                newItem = new KdmPixmap(parent, subnode);
 
213
            } else if (type == "rect") {
 
214
                newItem = new KdmRect(parent, subnode);
 
215
            } else if (type == "entry") {
 
216
                //newItem = new KdmEntry(parent, subnode);
 
217
                newItem = new KdmRect(parent, subnode);
 
218
                newItem->setType(type);
 
219
            } else if (type == "list") {
 
220
                newItem = new KdmList(parent, subnode);
 
221
            } else if (type == "svg") {
 
222
                newItem = new KdmPixmap(parent, subnode);
 
223
            } else {
 
224
                continue;
 
225
            }
 
226
            if (!newItem->isVisible()) {
 
227
                delete newItem;
 
228
                continue;
 
229
            }
 
230
            connect(newItem, SIGNAL(needUpdate(int, int, int, int)),
 
231
                    SLOT(update(int, int, int, int)));
 
232
            connect(newItem, SIGNAL(needPlacement()),
 
233
                    SLOT(slotNeedPlacement()));
 
234
            connect(newItem, SIGNAL(needPlugging()),
 
235
                    SLOT(slotNeedPlugging()));
 
236
            connect(newItem, SIGNAL(activated(const QString &)),
 
237
                    SIGNAL(activated(const QString &)));
 
238
            generateLayouts(newItem, subnode);
 
239
        }
 
240
    }
 
241
}
 
242
 
 
243
void
 
244
KdmThemer::generateLayouts(KdmItem *parent, const QDomNode &node)
 
245
{
 
246
    const QDomNodeList &subnodeList = node.childNodes();
 
247
    for (int nod = 0; nod < subnodeList.count(); nod++) {
 
248
        QDomNode subnode = subnodeList.item(nod);
 
249
        QString tagName = subnode.toElement().tagName();
 
250
 
 
251
        if (tagName == "box") {
 
252
            parent->setBoxLayout(subnode);
 
253
            generateItems(parent, subnode);
 
254
        } else if (tagName == "fixed") {
 
255
            parent->setFixedLayout(subnode);
 
256
            generateItems(parent, subnode);
 
257
        }
 
258
    }
 
259
}
 
260
 
 
261
void
 
262
KdmThemer::showStructure()
 
263
{
 
264
    QDebug(QtDebugMsg) << "======= item tree =======";
 
265
    rootItem->showStructure(QString());
 
266
}
 
267
 
 
268
void
 
269
KdmThemer::setTypeVisible(const QString &t, bool show)
 
270
{
 
271
    m_showTypes[t] = show;
 
272
    rootItem->updateVisible();
 
273
}
 
274
 
 
275
#include "kdmthemer.moc"