]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/QY2Styler.cc
I wish it had helped :)
[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 #include <QPainter>
8
9 QY2Styler *QY2Styler::_self = 0;
10
11 QY2Styler::QY2Styler( QObject *parent )
12     : QObject( parent )
13 {
14     _self = this;
15 }
16
17 void QY2Styler::setStyleSheet( const QString &filename )
18 {
19     QFile file( themeDir() + filename );
20     if ( file.open( QIODevice::ReadOnly ) )
21     {
22         QString content = file.readAll();
23         processUrls( content );
24         qApp->setStyleSheet( content );
25     }
26 }
27
28 void QY2Styler::processUrls(QString &text)
29 {
30     QString result;
31     QStringList lines = text.split( '\n' );
32     QRegExp urlx( ": *url\\((.*)\\)" );
33     QRegExp backgroundx( "^ */\\* *Background: *([^ ]*) *([^ ]*) *\\*/$" );
34     for ( QStringList::const_iterator it = lines.begin(); it != lines.end(); ++it )
35     {
36         QString line = *it;
37         if ( urlx.indexIn( line ) >= 0 )
38             line.replace( urlx, ": url(" + themeDir() + urlx.cap( 1 ) + ")");
39
40         if ( backgroundx.exactMatch( line ) )
41             _backgroundFn[backgroundx.cap( 1 )] = themeDir() + backgroundx.cap( 2 );
42
43         result += line;
44     }
45     text = result;
46 }
47
48 QString QY2Styler::themeDir() const
49 {
50     return THEMEDIR "/openSUSE/wizard/";
51 }
52
53 void QY2Styler::registerWidget( QWidget *widget )
54 {
55     widget->installEventFilter( this );
56 }
57
58 bool QY2Styler::eventFilter( QObject * obj, QEvent * ev )
59 {
60     QString name = obj->objectName();
61
62     if ( ev->type() != QEvent::Resize )
63         return QObject::eventFilter( obj, ev );
64
65     if ( !_backgroundFn.contains( name ) )
66         return QObject::eventFilter( obj, ev );
67
68     qDebug( "eventFilter %s %s %d", qPrintable( name ), obj->metaObject()->className(), ev->type() );
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         qDebug( "loading %s for %s", qPrintable( back ), qPrintable( name ) );
76     }
77
78     wid->setAutoFillBackground( true );
79
80     QPixmap result( wid->size() );
81     if ( wid->contentsRect() != wid->rect() )
82         result.fill( QColor( 0, 128, 0, 0 ) );
83
84     QPainter pain( &result );
85     QImage scaled = _backgroundPx[name].scaled( wid->contentsRect().width(), wid->contentsRect().height() );
86     pain.drawImage( wid->contentsRect().topLeft(), scaled, QRectF(QPointF(0,0), scaled.size()), Qt::OrderedAlphaDither);
87
88     QPalette p = wid->palette();
89     p.setBrush(QPalette::Window, result );
90     wid->setPalette( p );
91
92     return QObject::eventFilter( obj, ev );
93 }
94
95 #include "QY2Styler.moc"