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

« back to all changes in this revision

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