00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <qvbox.h>
00033 #include <qpopupmenu.h>
00034 #include <qpainter.h>
00035 #include <qmenubar.h>
00036 #include <qcheckbox.h>
00037 #include <qpushbutton.h>
00038 #include <qscrollbar.h>
00039 #include <qlayout.h>
00040 #include <qwhatsthis.h>
00041 #include <qpixmapcache.h>
00042 #include <qtimer.h>
00043 #include <qobjectlist.h>
00044 #include <qcommonstyle.h>
00045
00046 #include "sample.h"
00047
00048
00049 class SampleText : public QWidget
00050 {
00051 public:
00052 SampleText( const QString &t, bool h, QWidget *parent )
00053 : QWidget( parent ), hl(h), text(t)
00054 {
00055 if ( hl )
00056 setBackgroundMode( PaletteHighlight );
00057 else
00058 setBackgroundMode( PaletteBase );
00059 }
00060
00061 QSize sizeHint() const
00062 {
00063 QFontMetrics fm(font());
00064 return QSize( fm.width(text)+10, fm.height()+4 );
00065 }
00066
00067 void paintEvent( QPaintEvent * )
00068 {
00069 QPainter p(this);
00070 if ( hl )
00071 p.setPen( colorGroup().highlightedText() );
00072 else
00073 p.setPen( colorGroup().text() );
00074 p.drawText( rect(), AlignCenter, text );
00075 }
00076
00077 private:
00078 bool hl;
00079 QString text;
00080 };
00081
00082
00083 SampleWindow::SampleWindow( QWidget *parent ) : QWidget(parent), iface(0)
00084 {
00085 init();
00086 }
00087
00088 QSize SampleWindow::sizeHint() const
00089 {
00090 return container->sizeHint() + QSize( 10, 35 );
00091 }
00092
00093 void SampleWindow::setFont( const QFont &f )
00094 {
00095 QWidget::setFont( f );
00096 popup->setFont( f );
00097 QTimer::singleShot ( 0, this, SLOT( fixGeometry()));
00098 }
00099
00100 static void setStyleRecursive ( QWidget *w, QStyle *s )
00101 {
00102 w->setStyle( s );
00103 QObjectList *childObjects=(QObjectList*)w->children();
00104 if ( childObjects ) {
00105 QObject * o;
00106 for(o=childObjects->first();o!=0;o=childObjects->next()) {
00107 if( o->isWidgetType() ) {
00108 setStyleRecursive((QWidget *)o,s);
00109 }
00110 }
00111 }
00112 }
00113
00114
00115 void SampleWindow::setStyle2 ( QStyle *sty, const QPalette &pal )
00116 {
00117 typedef void (QStyle::*QDrawMenuBarItemImpl) (QPainter *, int, int, int, int, QMenuItem *, QColorGroup &, bool, bool);
00118
00119 extern QDrawMenuBarItemImpl qt_set_draw_menu_bar_impl(QDrawMenuBarItemImpl);
00120
00121 QPixmapCache::clear ( );
00122 QPalette p = pal;
00123 sty-> polish ( p );
00124 qt_set_draw_menu_bar_impl ( 0 );
00125 setStyleRecursive ( this, sty );
00126 setPalette ( p );
00127 QTimer::singleShot ( 0, this, SLOT( fixGeometry()));
00128 }
00129
00130
00131 void SampleWindow::setDecoration( WindowDecorationInterface *i )
00132 {
00133 iface = i;
00134 wd.rect = QRect( 0, 0, 150, 75 );
00135 wd.caption = tr("Sample");
00136 wd.palette = palette();
00137 wd.flags = WindowDecorationInterface::WindowData::Dialog |
00138 WindowDecorationInterface::WindowData::Active;
00139 wd.reserved = 1;
00140
00141 th = iface->metric(WindowDecorationInterface::TitleHeight, &wd);
00142 tb = iface->metric(WindowDecorationInterface::TopBorder, &wd);
00143 lb = iface->metric(WindowDecorationInterface::LeftBorder, &wd);
00144 rb = iface->metric(WindowDecorationInterface::RightBorder, &wd);
00145 bb = iface->metric(WindowDecorationInterface::BottomBorder, &wd);
00146
00147 int yoff = th + tb;
00148 int xoff = lb;
00149
00150 wd.rect.setX( 0 );
00151 wd.rect.setWidth( width() - lb - rb );
00152 wd.rect.setY( 0 );
00153 wd.rect.setHeight( height() - yoff - bb );
00154
00155 container->setGeometry( xoff, yoff, wd.rect.width(), wd.rect.height() );
00156 setMinimumSize( container->sizeHint().width()+lb+rb,
00157 container->sizeHint().height()+tb+th+bb );
00158 }
00159
00160 void SampleWindow::paintEvent( QPaintEvent * )
00161 {
00162 if ( !iface )
00163 return;
00164
00165 QPainter p( this );
00166
00167 p.translate( lb, th+tb );
00168
00169 iface->drawArea(WindowDecorationInterface::Border, &p, &wd);
00170 iface->drawArea(WindowDecorationInterface::Title, &p, &wd);
00171
00172 p.setPen(palette().active().color(QColorGroup::HighlightedText));
00173 QFont f( font() );
00174 f.setWeight( QFont::Bold );
00175 p.setFont(f);
00176 iface->drawArea(WindowDecorationInterface::TitleText, &p, &wd);
00177
00178 QRect brect( 0, -th, iface->metric(WindowDecorationInterface::HelpWidth,&wd), th );
00179 iface->drawButton( WindowDecorationInterface::Help, &p, &wd,
00180 brect.x(), brect.y(), brect.width(), brect.height(), (QWSButton::State)0 );
00181 brect.moveBy( wd.rect.width() -
00182 iface->metric(WindowDecorationInterface::OKWidth,&wd) -
00183 iface->metric(WindowDecorationInterface::CloseWidth,&wd), 0 );
00184 iface->drawButton( WindowDecorationInterface::Close, &p, &wd,
00185 brect.x(), brect.y(), brect.width(), brect.height(), (QWSButton::State)0 );
00186 brect.moveBy( iface->metric(WindowDecorationInterface::CloseWidth,&wd), 0 );
00187 iface->drawButton( WindowDecorationInterface::OK, &p, &wd,
00188 brect.x(), brect.y(), brect.width(), brect.height(), (QWSButton::State)0 );
00189 }
00190
00191 void SampleWindow::init()
00192 {
00193 container = new QVBox( this );
00194 popup = new QPopupMenu( this );
00195 popup->insertItem( tr("Normal Item"), 1 );
00196 popup->insertItem( tr("Disabled Item"), 2 );
00197 popup->setItemEnabled(2, FALSE);
00198 QMenuBar *mb = new QMenuBar( container );
00199 mb->insertItem( tr("Menu"), popup );
00200 QHBox *hb = new QHBox( container );
00201 QWidget *w = new QWidget( hb );
00202 (void)new QScrollBar( 0, 0, 0, 0, 0, Vertical, hb );
00203
00204 QGridLayout *gl = new QGridLayout( w, 2, 2, 4 );
00205 SampleText *l = new SampleText( tr("Normal Text"), FALSE, w );
00206 gl->addWidget( l, 0, 0 );
00207
00208 l = new SampleText( tr("Highlighted Text"), TRUE, w );
00209 gl->addWidget( l, 1, 0 );
00210
00211 QPushButton *pb = new QPushButton( tr("Button"), w );
00212 gl->addWidget( pb, 0, 1 );
00213 pb->setFocusPolicy( NoFocus );
00214
00215 QCheckBox *cb = new QCheckBox( tr("Check Box"), w );
00216 gl->addWidget( cb, 1, 1 );
00217 cb->setFocusPolicy( NoFocus );
00218 cb->setChecked( TRUE );
00219
00220 QWhatsThis::add( this, tr("Sample window using the selected settings.") );
00221 }
00222
00223 bool SampleWindow::eventFilter( QObject *, QEvent *e )
00224 {
00225 switch ( e->type() ) {
00226 case QEvent::MouseButtonPress:
00227 case QEvent::MouseButtonRelease:
00228 case QEvent::MouseButtonDblClick:
00229 case QEvent::MouseMove:
00230 case QEvent::KeyPress:
00231 case QEvent::KeyRelease:
00232 return TRUE;
00233 default:
00234 break;
00235 }
00236
00237 return FALSE;
00238 }
00239
00240 void SampleWindow::paletteChange( const QPalette &old )
00241 {
00242 QWidget::paletteChange ( old );
00243 wd. palette = palette ( );
00244 popup-> setPalette ( palette ( ));
00245 }
00246
00247
00248 void SampleWindow::setPalette ( const QPalette &pal )
00249 {
00250 QPixmapCache::clear ( );
00251 QPalette p = pal;
00252 style ( ). polish ( p );
00253 QWidget::setPalette ( p );
00254 }
00255
00256 void SampleWindow::resizeEvent( QResizeEvent *re )
00257 {
00258 wd.rect = QRect( 0, 0, 150, 75 );
00259
00260 wd.rect.setX( 0 );
00261 wd.rect.setWidth( width() - lb - rb );
00262 wd.rect.setY( 0 );
00263 wd.rect.setHeight( height() - th - tb - bb );
00264
00265 container->setGeometry( lb, th+tb, wd.rect.width(), wd.rect.height() );
00266 QWidget::resizeEvent( re );
00267 }
00268
00269 void SampleWindow::fixGeometry()
00270 {
00271 setMinimumSize( container->sizeHint().width()+lb+rb,
00272 container->sizeHint().height()+tb+th+bb );
00273 }
00274