]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/QY2Styler.cc
Duncan, for you
[duncan/yast2-qt4.git] / src / QY2Styler.cc
1 #include "QY2Styler.h"
2 #include <QFile>
3 #include <QString>
4 #include <QStringList>
5 #include <QApplication>
6 #include <QWidget>
7
8 QY2Styler *QY2Styler::_self = 0;
9
10 QY2Styler::QY2Styler( QObject *parent )
11     : QObject( parent )
12 {
13     _self = this;
14 }
15
16 void QY2Styler::setStyleSheet( const QString &filename )
17 {
18     QFile file( themeDir() + filename );
19     if ( file.open( QIODevice::ReadOnly ) )
20     {
21         QString content = file.readAll();
22         processUrls( content );
23         //qApp->setStyleSheet( content );
24     }
25 }
26
27 void QY2Styler::processUrls(QString &text)
28 {
29     QString result;
30     QStringList lines = text.split( '\n' );
31     QRegExp urlx( ": *url\\((.*)\\)" );
32     QRegExp backgroundx( "^ */\\* *Background: *([^ ]*) *([^ ]*) *\\*/$" );
33     for ( QStringList::const_iterator it = lines.begin(); it != lines.end(); ++it )
34     {
35         QString line = *it;
36         if ( urlx.indexIn( line ) >= 0 )
37             line.replace( urlx, ": url(" + themeDir() + urlx.cap( 1 ) + ")");
38
39         if ( backgroundx.exactMatch( line ) )
40             _backgroundFn[backgroundx.cap( 1 )] = themeDir() + backgroundx.cap( 2 );
41
42         result += line;
43     }
44     text = result;
45 }
46
47 QString QY2Styler::themeDir() const
48 {
49     return THEMEDIR "/openSUSE/wizard/";
50 }
51
52 void QY2Styler::registerWidget( QWidget *widget )
53 {
54     return;
55     widget->installEventFilter( this );
56 }
57
58 bool QY2Styler::eventFilter( QObject * obj, QEvent * ev )
59 {
60     if ( ev->type() != QEvent::Resize )
61         return QObject::eventFilter( obj, ev );
62
63     QString name = obj->objectName();
64
65     qDebug( "eventFilter %s", qPrintable( name ) );
66
67     if ( !_backgroundFn.contains( name ) )
68         return QObject::eventFilter( obj, ev );
69
70     QWidget *wid = qobject_cast<QWidget*>( obj );
71     if ( !_backgroundPx.contains( name ) )
72     {
73         QString back = _backgroundFn[ name ];
74         _backgroundPx[ name ] = QImage( back );
75         wid->setAutoFillBackground( true );
76         qDebug( "loading %s for %s", qPrintable( back ), qPrintable( name ) );
77     }
78
79     QPixmap img = QPixmap::fromImage( _backgroundPx[name].scaled( wid->contentsRect().width(), wid->contentsRect().height() ) );
80     QPalette p = wid->palette();
81     p.setBrush(QPalette::Window, img );
82     wid->setPalette( p );
83
84     return QObject::eventFilter( obj, ev );
85 }
86
87 #include "QY2Styler.moc"