Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

metal.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
00004 **
00005 ** This file is part of an example program for Qt.  This example
00006 ** program may be used, distributed and modified without limitation.
00007 **
00008 *****************************************************************************/
00009 
00010 #include "metal.h"
00011 #include <qapplication.h>
00012 #include <qpushbutton.h>
00013 #include <qscrollbar.h>
00014 #include <limits.h>
00015 
00016 
00018 #include "stonedark.xpm"
00019 #include "stone1.xpm"
00020 #include "marble.xpm"
00022 
00023 
00024 
00025 MetalStyle::MetalStyle() : QWindowsStyle() { }
00026 
00030 void MetalStyle::polish( QApplication *app)
00031 {
00032     oldPalette = app->palette();
00033 
00034     // we simply create a nice QColorGroup with a couple of fancy
00035     // pixmaps here and apply to it all widgets
00036 
00037     QFont f("times", app->font().pointSize() );
00038     f.setBold( TRUE );
00039     f.setItalic( TRUE );
00040     app->setFont( f, TRUE, "QMenuBar");
00041     app->setFont( f, TRUE, "QPopupMenu");
00042 
00043 
00044     //QPixmap button( stone1_xpm );
00045     QPixmap button( stonedark_xpm );
00046     QPixmap background(marble_xpm);
00047 #if 0
00048 
00049     int i;
00050     for (i=0; i<img.numColors(); i++) {
00051   QRgb rgb = img.color(i);
00052   QColor c(rgb);
00053   rgb = c.dark().rgb();
00054   img.setColor(i,rgb);
00055     }
00056     QPixmap mid;
00057     mid.convertFromImage(img);
00058 
00059     img = orig;
00060     for (i=0; i<img.numColors(); i++) {
00061   QRgb rgb = img.color(i);
00062   QColor c(rgb);
00063   rgb = c.light().rgb();
00064   img.setColor(i,rgb);
00065     }
00066     QPixmap light;
00067     light.convertFromImage(img);
00068 
00069     img = orig;
00070     for (i=0; i<img.numColors(); i++) {
00071   QRgb rgb = img.color(i);
00072   QColor c(rgb);
00073   rgb = c.dark().rgb();
00074   img.setColor(i,rgb);
00075     }
00076     QPixmap dark;
00077     dark.convertFromImage(img);
00078 #else
00079     QPixmap dark( 1, 1 ); dark.fill( red.dark() );
00080     QPixmap mid( stone1_xpm );
00081     QPixmap light( stone1_xpm );//1, 1 ); light.fill( green );
00082 #endif
00083     QPalette op = app->palette();
00084 
00085     QColor backCol( 227,227,227 );
00086 
00087     // QPalette op(white);
00088     QColorGroup active (op.normal().foreground(),
00089          QBrush(op.normal().button(),button),
00090          QBrush(op.normal().light(), light),
00091          QBrush(op.normal().dark(), dark),
00092          QBrush(op.normal().mid(), mid),
00093          op.normal().text(),
00094          Qt::white,
00095          op.normal().base(),//         QColor(236,182,120),
00096          QBrush(backCol, background)
00097          );
00098     active.setColor( QColorGroup::ButtonText,  Qt::white  );
00099     active.setColor( QColorGroup::Shadow,  Qt::black  );
00100     QColorGroup disabled (op.disabled().foreground(),
00101          QBrush(op.disabled().button(),button),
00102          QBrush(op.disabled().light(), light),
00103          op.disabled().dark(),
00104          QBrush(op.disabled().mid(), mid),
00105          op.disabled().text(),
00106          Qt::white,
00107          op.disabled().base(),//         QColor(236,182,120),
00108          QBrush(backCol, background)
00109          );
00110 
00111     QPalette newPalette( active, disabled, active );
00112     app->setPalette( newPalette, TRUE );
00113 }
00114 
00118 void MetalStyle::unPolish( QApplication *app)
00119 {
00120     app->setPalette(oldPalette, TRUE);
00121     app->setFont( app->font(), TRUE );
00122 }
00123 
00127 void MetalStyle::polish( QWidget* w)
00128 {
00129 
00130    // the polish function sets some widgets to transparent mode and
00131     // some to translate background mode in order to get the full
00132     // benefit from the nice pixmaps in the color group.
00133 
00134     if (w->inherits("QPushButton")){
00135   w->setBackgroundMode( QWidget::NoBackground );
00136   return;
00137     }
00138 
00139     if (w->inherits("QTipLabel") || w->inherits("QLCDNumber") ){
00140   return;
00141     }
00142 
00143     if ( !w->isTopLevel() ) {
00144   if ( w->inherits("QGroupBox")
00145        || w->inherits("QTabWidget") ) {
00146       w->setAutoMask( TRUE );
00147       return;
00148   }
00149   if (w->inherits("QLabel")
00150       || w->inherits("QSlider")
00151       || w->inherits("QButton")
00152       || w->inherits("QProgressBar")
00153       ){
00154       w->setBackgroundOrigin( QWidget::ParentOrigin );
00155   }
00156     }
00157 }
00158 
00159 void MetalStyle::unPolish( QWidget* w)
00160 {
00161 
00162    // the polish function sets some widgets to transparent mode and
00163     // some to translate background mode in order to get the full
00164     // benefit from the nice pixmaps in the color group.
00165 
00166     if (w->inherits("QPushButton")){
00167   w->setBackgroundMode( QWidget::PaletteButton );
00168   return;
00169     }
00170 
00171     if (w->inherits("QTipLabel") || w->inherits("QLCDNumber") ){
00172   return;
00173     }
00174 
00175     if ( !w->isTopLevel() ) {
00176   if ( w->inherits("QGroupBox")
00177        || w->inherits("QTabWidget") ) {
00178       w->setAutoMask( FALSE );
00179       return;
00180   }
00181   if (w->inherits("QLabel")
00182       || w->inherits("QSlider")
00183       || w->inherits("QButton")
00184       || w->inherits("QProgressBar")
00185       ){
00186       w->setBackgroundOrigin( QWidget::WidgetOrigin );
00187   }
00188     }
00189 
00190 }
00191 
00197 void MetalStyle::drawMetalButton( QPainter *p, int x, int y, int w, int h,
00198   bool sunken, bool horz )
00199 {
00200     QColor top1("#878769691515");
00201     QColor top2("#C6C6B4B44949");
00202 
00203     QColor bot2("#70705B5B1414");
00204     QColor bot1("56564A4A0E0E"); //first from the bottom
00205 
00206     QColor highlight("#E8E8DDDD6565");
00207     QColor subh1("#CECEBDBD5151");
00208     QColor subh2("#BFBFACAC4545");
00209 
00210     QColor topgrad("#B9B9A5A54040");
00211     QColor botgrad("#89896C6C1A1A");
00212 
00213 
00214     int x2 = x + w - 1;
00215     int y2 = y + h - 1;
00216 
00217     //frame:
00218 
00219     p->setPen( top1 );
00220     p->drawLine( x, y2, x, y );
00221     p->drawLine( x, y, x2-1, y );
00222     p->setPen( top2 );
00223     p->drawLine( x+1, y2 -1, x+1, y+1 );
00224     p->drawLine( x+1, y+1 , x2-2, y+1 );
00225 
00226     p->setPen( bot1 );
00227     p->drawLine( x+1, y2, x2, y2 );
00228     p->drawLine( x2, y2, x2, y );
00229     p->setPen( bot2 );
00230     p->drawLine( x+1, y2-1, x2-1, y2-1 );
00231     p->drawLine( x2-1, y2-1, x2-1, y+1 );
00232 
00233     // highlight:
00234     int i = 0;
00235     int x1 = x + 2;
00236     int y1 = y + 2;
00237     if ( horz )
00238   x2 = x2 - 2;
00239     else
00240   y2 = y2 - 2;
00241     // Note that x2/y2 mean something else from this point down...
00242 
00243 #define DRAWLINE if (horz) \
00244                     p->drawLine( x1, y1+i, x2, y1+i ); \
00245      else \
00246                     p->drawLine( x1+i, y1, x1+i, y2 ); \
00247                  i++;
00248 
00249     if ( !sunken ) {
00250   p->setPen( highlight );
00251   DRAWLINE;
00252   DRAWLINE;
00253   p->setPen( subh1 );
00254   DRAWLINE;
00255   p->setPen( subh2 );
00256   DRAWLINE;
00257     }
00258     // gradient:
00259     int ng = (horz ? h : w) - 8; // how many lines for the gradient?
00260 
00261     int h1, h2, s1, s2, v1, v2;
00262     if ( !sunken ) {
00263   topgrad.hsv( &h1, &s1, &v1 );
00264   botgrad.hsv( &h2, &s2, &v2 );
00265     } else {
00266   botgrad.hsv( &h1, &s1, &v1 );
00267   topgrad.hsv( &h2, &s2, &v2 );
00268     }
00269 
00270     if ( ng > 1 ) {
00271 
00272   for ( int j =0; j < ng; j++ ) {
00273       p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1),
00274              s1 + ((s2-s1)*j)/(ng-1),
00275              v1 + ((v2-v1)*j)/(ng-1),  QColor::Hsv ) );
00276       DRAWLINE;
00277   }
00278     } else if ( ng == 1 ) {
00279   p->setPen( QColor( (h1+h2)/2, (s1+s2)/2, (v1+v2)/2, QColor::Hsv ) );
00280   DRAWLINE;
00281     }
00282     if ( sunken ) {
00283   p->setPen( subh2 );
00284   DRAWLINE;
00285 
00286   p->setPen( subh1 );
00287   DRAWLINE;
00288 
00289   p->setPen( highlight );
00290   DRAWLINE;
00291   DRAWLINE;
00292     }
00293 
00294 }
00295 
00296 
00297 
00301 void MetalStyle::drawButton( QPainter *p, int x, int y, int w, int h,
00302            const QColorGroup &, bool sunken, const QBrush*)
00303 {
00304 
00305     drawMetalButton( p, x, y, w, h, sunken, TRUE );
00306 }
00307 
00311 void MetalStyle::drawBevelButton( QPainter *p, int x, int y, int w, int h,
00312         const QColorGroup &, bool sunken, const QBrush*)
00313 {
00314     MetalStyle::drawMetalButton(p, x, y, w, h, sunken, TRUE );
00315 }
00316 
00320 void MetalStyle::drawPushButton( QPushButton* btn, QPainter *p)
00321 {
00322     QColorGroup g = btn->colorGroup();
00323     int x1, y1, x2, y2;
00324 
00325     btn->rect().coords( &x1, &y1, &x2, &y2 ); // get coordinates
00326 
00327     p->setPen( g.foreground() );
00328     p->setBrush( QBrush(g.button(),NoBrush) );
00329 
00330     QBrush fill;
00331     if ( btn->isDown() )
00332   fill = g.brush( QColorGroup::Mid );
00333     else if ( btn->isOn() )
00334   fill = QBrush( g.mid(), Dense4Pattern );
00335     else
00336   fill = g.brush( QColorGroup::Button );
00337 
00338     if ( btn->isDefault() ) {
00339   QPointArray a;
00340   a.setPoints( 9,
00341          x1, y1, x2, y1, x2, y2, x1, y2, x1, y1+1,
00342          x2-1, y1+1, x2-1, y2-1, x1+1, y2-1, x1+1, y1+1 );
00343   p->setPen( Qt::black );
00344   p->drawPolyline( a );
00345   x1 += 2;
00346   y1 += 2;
00347   x2 -= 2;
00348   y2 -= 2;
00349     }
00350 
00351     drawMetalButton( p, x1, y1, x2-x1+1, y2-y1+1, btn->isOn() || btn->isDown(),
00352          TRUE ); // always horizontal
00353 
00354 
00355     if ( btn->isMenuButton() ) {
00356   int dx = (y1-y2-4)/3;
00357   drawArrow( p, DownArrow, FALSE,
00358        x2 - dx, dx, y1, y2 - y1,
00359        g, btn->isEnabled() );
00360     }
00361 
00362     if ( p->brush().style() != NoBrush )
00363   p->setBrush( NoBrush );
00364 
00365 }
00366 
00367 
00371 void MetalStyle::drawPushButtonLabel( QPushButton* btn, QPainter *p)
00372 {
00373     QRect r = btn->rect();
00374     int x, y, w, h;
00375     r.rect( &x, &y, &w, &h );
00376 
00377     int x1, y1, x2, y2;
00378     btn->rect().coords( &x1, &y1, &x2, &y2 ); // get coordinates
00379     int dx = 0;
00380     int dy = 0;
00381     if ( btn->isMenuButton() )
00382   dx = (y2-y1) / 3;
00383     if ( btn->isOn() || btn->isDown() ) {
00384   dx--;
00385   dy--;
00386     }
00387     if ( dx || dy )
00388   p->translate( dx, dy );
00389 
00390     x += 2;  y += 2;  w -= 4;  h -= 4;
00391     QColorGroup g = btn->colorGroup();
00392     drawItem( p, x, y, w, h,
00393         AlignCenter|ShowPrefix,
00394         g, btn->isEnabled(),
00395         btn->pixmap(), btn->text(), -1,
00396         (btn->isDown() || btn->isOn())?&btn->colorGroup().brightText():&btn->colorGroup().buttonText());
00397 
00398     if ( dx || dy )
00399   p->translate( -dx, -dy );
00400 }
00401 
00402 
00403 void MetalStyle::drawPanel( QPainter *p, int x, int y, int w, int h,
00404           const QColorGroup &g, bool sunken,
00405           int lineWidth, const QBrush *fill )
00406 {
00407 
00408     QStyle::drawPanel( p,  x,  y,  w,  h,
00409           g, sunken,
00410           lineWidth, fill );
00411 }
00412 
00413 
00418 void MetalStyle::drawSlider( QPainter *p, int x, int y, int w, int h,
00419            const QColorGroup &, Orientation orient,
00420            bool /*tickAbove*/, bool /*tickBelow*/ )
00421 {
00422     drawMetalButton( p, x, y, w, h, FALSE, orient != Horizontal );
00423 }
00424 
00425 
00426 void MetalStyle::drawScrollBarControls( QPainter* p, const QScrollBar* sb,
00427           int sliderStart, uint controls,
00428           uint activeControl )
00429 {
00430     QWindowsStyle::drawScrollBarControls( p, sb, sliderStart, controls & ~(AddLine|SubLine|Slider),
00431           activeControl & ~(AddLine|SubLine|Slider) );
00432     bool horz = sb->orientation() == QScrollBar::Horizontal;
00433     int b = 2;
00434     int w = horz ? sb->height() : sb->width();
00435 
00436     QColorGroup g = sb->colorGroup();
00437 
00438     int sliderMin, sliderMax, sliderLength, buttonDim;
00439     scrollBarMetrics( sb, sliderMin, sliderMax, sliderLength, buttonDim );
00440 
00441     if (sliderStart > sliderMax) { // sanity check
00442   sliderStart = sliderMax;
00443     }
00444     bool maxedOut = (sb->maxValue() == sb->minValue());
00445 
00446 
00447     if ( controls & AddLine ) {
00448   bool sunken = activeControl & AddLine;
00449   QRect r( b, b, w-2*b, w-2*b ) ;
00450   if ( horz )
00451       r.moveBy( sb->width() - w, 0 );
00452   else
00453       r.moveBy( 0, sb->height() - w );
00454 
00455   drawMetalButton( p, r.x(), r.y(), r.width(), r.height(),
00456        sunken, !horz );
00457   drawArrow(  p, horz ? RightArrow : DownArrow,  sunken,
00458         r.x(), r.y(), r.width(), r.height(), g, !maxedOut );
00459 
00460     }
00461     if ( controls & SubLine ) {
00462   bool sunken = activeControl & SubLine;
00463   QRect r( b, b, w-2*b, w-2*b ) ;
00464   drawMetalButton( p, r.x(), r.y(), r.width(), r.height(),
00465        sunken, !horz );
00466   drawArrow(  p, horz ? LeftArrow : UpArrow,  sunken,
00467         r.x(), r.y(), r.width(), r.height(), g, !maxedOut );
00468     }
00469 
00470     QRect sliderR;
00471     if ( horz ) {
00472   sliderR .setRect( sliderStart, b, sliderLength, w-2*b );
00473     } else {
00474   sliderR .setRect( b, sliderStart, w-2*b, sliderLength );
00475     }
00476     if ( controls & Slider ) {
00477   if ( !maxedOut ) {
00478       drawMetalButton( p, sliderR.x(), sliderR.y(),
00479          sliderR.width(), sliderR.height(),
00480          FALSE, horz );
00481   }
00482     }
00483 
00484 }
00485 
00486 
00487 void MetalStyle::drawComboButton( QPainter *p, int x, int y, int w, int h,
00488           const QColorGroup &g, bool sunken,
00489           bool /*editable*/,
00490           bool enabled,
00491           const QBrush *fill)
00492 {
00493 
00494      qDrawWinPanel(p, x, y, w, h, g, TRUE,
00495        fill?fill:(enabled?&g.brush( QColorGroup::Base ):
00496               &g.brush( QColorGroup::Background )));
00497 
00498 
00499      drawMetalButton( p, x+w-2-16, y+2, 16, h-4, sunken, TRUE );
00500 
00501      drawArrow( p, QStyle::DownArrow, sunken,
00502     x+w-2-16+ 2, y+2+ 2, 16- 4, h-4- 4, g, enabled );
00503 
00504 }

Generated on Sat Nov 5 16:17:59 2005 for OPIE by  doxygen 1.4.2