add more spacing
[personal-kdebase.git] / workspace / kdm / kfrontend / themer / kdmthemer.cpp
blob5ea1b18a25067bb5e5865f16a709e0b1c93ab927
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>
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.
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.
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.
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"
30 #include <kdm_greet.h> // debug stuff
31 #include <kfdialog.h> // kfmsgbox
33 #include <klocale.h>
34 #include <kconfig.h>
35 #include <kconfiggroup.h>
37 #include <QEvent>
38 #include <QMouseEvent>
39 #include <QPaintEvent>
40 #include <QFile>
41 #include <QFileInfo>
42 #include <QPainter>
44 #include <unistd.h>
47 * KdmThemer. The main theming interface
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 )
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" );
65 QFile opmlFile( filename );
66 if (!opmlFile.open( QIODevice::ReadOnly )) {
67 KFMsgBox::box( w, errorbox, i18n( "Cannot open theme file %1" , filename) );
68 return;
70 QDomDocument domTree;
71 if (!domTree.setContent( &opmlFile )) {
72 KFMsgBox::box( w, errorbox, i18n( "Cannot parse theme file %1" , filename) );
73 return;
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;
83 // Set the root (screen) item
84 rootItem = new KdmRect( this, theme );
86 basedir = QFileInfo( filename ).absolutePath();
88 generateItems( rootItem, theme );
91 KdmThemer::~KdmThemer()
95 void
96 KdmThemer::setWidget( QWidget *w )
98 if ((m_widget = w)) {
99 rootItem->updateVisible();
100 m_widget->setPalette( rootItem->style.palette );
101 m_widget->setFont( rootItem->style.font.font );
102 rootItem->plugActions();
106 KdmItem *
107 KdmThemer::findNode( const QString &item ) const
109 return rootItem->findChild<KdmItem *>( item );
112 void
113 KdmThemer::slotNeedPlacement()
115 m_geometryOutdated = m_geometryInvalid = true;
116 if (widget())
117 widget()->update();
120 void
121 KdmThemer::slotNeedPlugging()
123 if (widget())
124 rootItem->plugActions();
127 void
128 KdmThemer::update( int x, int y, int w, int h )
130 if (widget())
131 widget()->update( x, y, w, h );
134 // BEGIN other functions
136 void
137 KdmThemer::widgetEvent( QEvent *e )
139 if (!rootItem)
140 return;
141 switch (e->type()) {
142 case QEvent::MouseMove:
144 QMouseEvent *me = static_cast<QMouseEvent *>(e);
145 rootItem->mouseEvent( me->x(), me->y() );
147 break;
148 case QEvent::MouseButtonPress:
150 QMouseEvent *me = static_cast<QMouseEvent *>(e);
151 rootItem->mouseEvent( me->x(), me->y(), true );
153 break;
154 case QEvent::MouseButtonRelease:
156 QMouseEvent *me = static_cast<QMouseEvent *>(e);
157 rootItem->mouseEvent( me->x(), me->y(), false, true );
159 break;
160 case QEvent::Resize:
161 m_geometryOutdated = true;
162 widget()->update();
163 break;
164 case QEvent::Paint:
165 if (m_geometryOutdated) {
166 debug() << "==== updating geometry ====";
167 QStack<QSize> ps;
168 QRect rect( QPoint( 0, 0 ), widget()->size() );
169 rootItem->setGeometry( ps, rect, m_geometryInvalid );
170 if (debugLevel & DEBUG_THEMING)
171 showStructure();
172 m_geometryOutdated = m_geometryInvalid = false;
175 QRect paintRect = static_cast<QPaintEvent *>(e)->rect();
176 //kDebug() << "paint on: " << paintRect;
178 QPainter p( widget() );
179 rootItem->paint( &p, paintRect, false, true );
180 rootItem->showWidget();
182 break;
183 default:
184 break;
188 void
189 KdmThemer::paintBackground( QPaintDevice *dev, const QRect &rect, bool primaryScreen )
191 debug() << "==== setting background geometry ====";
192 QStack<QSize> ps;
193 rootItem->setGeometry( ps, rect, true );
194 QPainter p( dev );
195 rootItem->paint( &p, rect, true, primaryScreen );
198 void
199 KdmThemer::generateItems( KdmItem *parent, const QDomNode &node )
202 * Go through each of the child nodes
204 const QDomNodeList &subnodeList = node.childNodes();
205 for (int nod = 0; nod < subnodeList.count(); nod++) {
206 QDomNode subnode = subnodeList.item( nod );
207 QDomElement el = subnode.toElement();
208 QString tagName = el.tagName();
210 if (tagName == "item") {
211 QString type = el.attribute( "type" );
212 KdmItem *newItem;
213 if (type == "label")
214 newItem = new KdmLabel( parent, subnode );
215 else if (type == "button")
216 newItem = new KdmButton( parent, subnode );
217 else if (type == "pixmap")
218 newItem = new KdmPixmap( parent, subnode );
219 else if (type == "rect")
220 newItem = new KdmRect( parent, subnode );
221 else if (type == "entry") {
222 //newItem = new KdmEntry( parent, subnode );
223 newItem = new KdmRect( parent, subnode );
224 newItem->setType( type );
225 } else if (type=="list")
226 newItem = new KdmList( parent, subnode );
227 else if (type == "svg")
228 newItem = new KdmPixmap( parent, subnode );
229 else
230 continue;
231 if (!newItem->isVisible()) {
232 delete newItem;
233 continue;
235 connect( newItem, SIGNAL(needUpdate( int, int, int, int )),
236 SLOT(update( int, int, int, int )) );
237 connect( newItem, SIGNAL(needPlacement()),
238 SLOT(slotNeedPlacement()) );
239 connect( newItem, SIGNAL(needPlugging()),
240 SLOT(slotNeedPlugging()) );
241 connect( newItem, SIGNAL(activated( const QString & )),
242 SIGNAL(activated( const QString & )) );
243 generateLayouts( newItem, subnode );
248 void
249 KdmThemer::generateLayouts( KdmItem *parent, const QDomNode &node )
251 const QDomNodeList &subnodeList = node.childNodes();
252 for (int nod = 0; nod < subnodeList.count(); nod++) {
253 QDomNode subnode = subnodeList.item( nod );
254 QString tagName = subnode.toElement().tagName();
256 if (tagName == "box") {
257 parent->setBoxLayout( subnode );
258 generateItems( parent, subnode );
259 } else if (tagName == "fixed") {
260 parent->setFixedLayout( subnode );
261 generateItems( parent, subnode );
266 void
267 KdmThemer::showStructure()
269 QDebug( QtDebugMsg ) << "======= item tree =======";
270 rootItem->showStructure( QString() );
273 void
274 KdmThemer::setTypeVisible( const QString &t, bool show )
276 m_showTypes[t] = show;
277 rootItem->updateVisible();
280 #include "kdmthemer.moc"