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"
24 #include "kdmpixmap.h"
28 #include "kdmbutton.h"
30 #include <kdm_greet.h> // debug stuff
31 #include <kfdialog.h> // kfmsgbox
35 #include <kconfiggroup.h>
38 #include <QMouseEvent>
39 #include <QPaintEvent>
47 * KdmThemer. The main theming interface
49 KdmThemer::KdmThemer( const QString
&_filename
,
50 const QMap
<QString
, bool> &showTypes
, QWidget
*w
)
52 , m_showTypes( showTypes
)
54 , m_geometryOutdated( true )
55 , m_geometryInvalid( true )
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
) );
71 if (!domTree
.setContent( &opmlFile
)) {
72 KFMsgBox::box( w
, errorbox
, i18n( "Cannot parse theme file %1" , filename
) );
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
) );
83 // Set the root (screen) item
84 rootItem
= new KdmRect( this, theme
);
86 basedir
= QFileInfo( filename
).absolutePath();
88 generateItems( rootItem
, theme
);
91 KdmThemer::~KdmThemer()
96 KdmThemer::setWidget( QWidget
*w
)
99 rootItem
->updateVisible();
100 m_widget
->setPalette( rootItem
->style
.palette
);
101 m_widget
->setFont( rootItem
->style
.font
.font
);
102 rootItem
->plugActions();
107 KdmThemer::findNode( const QString
&item
) const
109 return rootItem
->findChild
<KdmItem
*>( item
);
113 KdmThemer::slotNeedPlacement()
115 m_geometryOutdated
= m_geometryInvalid
= true;
121 KdmThemer::slotNeedPlugging()
124 rootItem
->plugActions();
128 KdmThemer::update( int x
, int y
, int w
, int h
)
131 widget()->update( x
, y
, w
, h
);
134 // BEGIN other functions
137 KdmThemer::widgetEvent( QEvent
*e
)
142 case QEvent::MouseMove
:
144 QMouseEvent
*me
= static_cast<QMouseEvent
*>(e
);
145 rootItem
->mouseEvent( me
->x(), me
->y() );
148 case QEvent::MouseButtonPress
:
150 QMouseEvent
*me
= static_cast<QMouseEvent
*>(e
);
151 rootItem
->mouseEvent( me
->x(), me
->y(), true );
154 case QEvent::MouseButtonRelease
:
156 QMouseEvent
*me
= static_cast<QMouseEvent
*>(e
);
157 rootItem
->mouseEvent( me
->x(), me
->y(), false, true );
161 m_geometryOutdated
= true;
165 if (m_geometryOutdated
) {
166 debug() << "==== updating geometry ====";
168 QRect
rect( QPoint( 0, 0 ), widget()->size() );
169 rootItem
->setGeometry( ps
, rect
, m_geometryInvalid
);
170 if (debugLevel
& DEBUG_THEMING
)
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();
189 KdmThemer::paintBackground( QPaintDevice
*dev
, const QRect
&rect
, bool primaryScreen
)
191 debug() << "==== setting background geometry ====";
193 rootItem
->setGeometry( ps
, rect
, true );
195 rootItem
->paint( &p
, rect
, true, primaryScreen
);
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" );
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
);
231 if (!newItem
->isVisible()) {
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
);
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
);
267 KdmThemer::showStructure()
269 QDebug( QtDebugMsg
) << "======= item tree =======";
270 rootItem
->showStructure( QString() );
274 KdmThemer::setTypeVisible( const QString
&t
, bool show
)
276 m_showTypes
[t
] = show
;
277 rootItem
->updateVisible();
280 #include "kdmthemer.moc"