• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KStyles

webstyle.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2001 Rik Hemsley (rikkus) <rik@kde.org>
00003  *
00004  *  This library is free software; you can redistribute it and/or
00005  *  modify it under the terms of the GNU Library General Public
00006  *  License as published by the Free Software Foundation; either
00007  *  version 2 of the License, or (at your option) any later version.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "webstyle.h"
00021 
00022 #ifndef INCLUDE_MENUITEM_DEF
00023 #define INCLUDE_MENUITEM_DEF
00024 #endif
00025 
00026 #include <QtGui/QMenuItem>
00027 #include <QtGui/QPalette>
00028 #include <QtGui/QBitmap>
00029 #include <QtGui/QTabBar>
00030 #include <Qt3Support/Q3PointArray>
00031 #include <QtGui/QScrollBar>
00032 #include <QtGui/QFrame>
00033 #include <QtGui/QPushButton>
00034 #include <QtGui/qdrawutil.h>
00035 #include <QtGui/QPainter>
00036 
00037 #include <kapplication.h>
00038 #include <kdrawutil.h>
00039 #include <klocale.h>
00040 #include <kiconloader.h>
00041 #include <kdebug.h>
00042 
00043 static const int  _indicatorSize = 13;
00044 static Q3Button *  _highlightedButton = 0;
00045 static const int  _scrollBarExtent = 14;
00046 
00047 static QFrame *   _currentFrame = 0;
00048 static int        _savedFrameLineWidth;
00049 static int        _savedFrameMidLineWidth;
00050 static ulong      _savedFrameStyle;
00051 
00052 static QColor contrastingForeground(const QColor & fg, const QColor & bg)
00053 {
00054   int h, s, vbg, vfg;
00055 
00056   bg.hsv(&h, &s, &vbg);
00057   fg.hsv(&h, &s, &vfg);
00058 
00059   int diff(vbg - vfg);
00060 
00061   if ((diff > -72) && (diff < 72))
00062   {
00063     return (vbg < 128) ? Qt::white : Qt::black;
00064   }
00065   else
00066   {
00067     return fg;
00068   }
00069 }
00070 
00071 // Gotta keep it separated.
00072 
00073   static void
00074 scrollBarControlsMetrics
00075 (
00076  const QScrollBar * sb,
00077  int sliderStart,
00078  int /* sliderMin */,
00079  int sliderMax,
00080  int sliderLength,
00081  int buttonDim,
00082  QRect & rSub,
00083  QRect & rAdd,
00084  QRect & rSubPage,
00085  QRect & rAddPage,
00086  QRect & rSlider
00087  )
00088 {
00089   bool horizontal = sb->orientation() == Qt::Horizontal;
00090 
00091   int len     = horizontal ? sb->width()  : sb->height();
00092 
00093   int extent  = horizontal ? sb->height() : sb->width();
00094 
00095   QColorGroup g = sb->colorGroup();
00096 
00097   if (sliderStart > sliderMax)
00098     sliderStart = sliderMax;
00099 
00100   int sliderEnd = sliderStart + sliderLength;
00101 
00102   int addX, addY;
00103   int subX, subY;
00104   int subPageX, subPageY, subPageW, subPageH;
00105   int addPageX, addPageY, addPageW, addPageH;
00106   int sliderX, sliderY, sliderW, sliderH;
00107 
00108   if (horizontal)
00109   {
00110     subY      = 0;
00111     addY      = 0;
00112     subX      = 0;
00113     addX      = buttonDim;
00114 
00115     subPageX  = buttonDim * 2;
00116     subPageY  = 0;
00117     subPageW  = sliderStart - 1;
00118     subPageH  = extent;
00119 
00120     addPageX  = sliderEnd;
00121     addPageY  = 0;
00122     addPageW  = len - sliderEnd;
00123     addPageH  = extent;
00124 
00125     sliderX   = sliderStart;
00126     sliderY   = 0;
00127     sliderW   = sliderLength;
00128     sliderH   = extent;
00129   }
00130   else
00131   {
00132     subX    = 0;
00133     addX    = 0;
00134     subY    = len - buttonDim * 2;
00135     addY    = len - buttonDim;
00136 
00137     subPageX = 0;
00138     subPageY = 0;
00139     subPageW = extent;
00140     subPageH = sliderStart;
00141 
00142     addPageX  = 0;
00143     addPageY  = sliderEnd;
00144     addPageW  = extent;
00145     addPageH  = subY - sliderEnd;
00146 
00147     sliderX   = 0;
00148     sliderY   = sliderStart;
00149     sliderW   = extent;
00150     sliderH   = sliderLength;
00151   }
00152 
00153   rSub      .setRect(    subX,      subY, buttonDim, buttonDim);
00154   rAdd      .setRect(    addX,      addY, buttonDim, buttonDim);
00155   rSubPage  .setRect(subPageX,  subPageY,  subPageW,  subPageH);
00156   rAddPage  .setRect(addPageX,  addPageY,  addPageW,  addPageH);
00157   rSlider   .setRect( sliderX,   sliderY,   sliderW,   sliderH);
00158 }
00159 
00160 // Rounded rects my way.
00161 
00162   static void
00163 drawFunkyRect
00164 (
00165  QPainter * p,
00166  int x,
00167  int y,
00168  int w,
00169  int h,
00170  bool small
00171 )
00172 {
00173   p->translate(x, y);
00174 
00175   if (small)
00176   {
00177     p->drawLine(      2,      0,  w - 3,      0  );
00178     p->drawLine(  w - 1,      2,  w - 1,  h - 3  );
00179     p->drawLine(  w - 3,  h - 1,      2,  h - 1  );
00180     p->drawLine(      0,  h - 3,      0,      2  );
00181 
00182     // Use an array of points so that there's only one round-trip with the
00183     // X server.
00184 
00185     QCOORD pointList[] =
00186     {
00187           1,      1,
00188       w - 2,      1,
00189       w - 2,  h - 2,
00190           1,  h - 2
00191     };
00192 
00193     p->drawPoints(Q3PointArray(4, pointList));
00194   }
00195   else
00196   {
00197     p->drawLine(      3,      0,  w - 4,      0  );
00198     p->drawLine(  w - 1,      3,  w - 1,  h - 4  );
00199     p->drawLine(  w - 4,  h - 1,      3,  h - 1  );
00200     p->drawLine(      0,  h - 4,      0,      3  );
00201 
00202     QCOORD pointList[] =
00203     {
00204           1,      2,
00205           2,      1,
00206       w - 3,      1,
00207       w - 2,      2,
00208       w - 2,  h - 3,
00209       w - 3,  h - 2,
00210           2,  h - 2,
00211           1,  h - 3
00212     };
00213 
00214     p->drawPoints(Q3PointArray(8, pointList));
00215   }
00216 
00217   p->translate(-x, -y);
00218 }
00219 
00220 WebStyle::WebStyle()
00221   : KStyle()
00222 {
00223   setButtonDefaultIndicatorWidth(6);
00224   setScrollBarExtent(_scrollBarExtent, _scrollBarExtent);
00225 }
00226 
00227 WebStyle::~WebStyle()
00228 {
00229   // Empty.
00230 }
00231 
00232   void
00233 WebStyle::polish(QApplication *)
00234 {
00235   // Empty.
00236 }
00237 
00238   void
00239 WebStyle::polish(QPalette &)
00240 {
00241   // Empty.
00242 }
00243 
00244   void
00245 WebStyle::unPolish(QApplication *)
00246 {
00247   // Empty.
00248 }
00249 
00250   void
00251 WebStyle::polish(QWidget * w)
00252 {
00253   if (w->inherits("QPushButton"))
00254     w->installEventFilter(this);
00255 
00256   else if (w->inherits("QGroupBox") || w->inherits("QFrame"))
00257   {
00258     QFrame * f(static_cast<QFrame *>(w));
00259 
00260     if (f->frameStyle() != QFrame::NoFrame)
00261     {
00262       _currentFrame = f;
00263 
00264       _savedFrameLineWidth = f->lineWidth();
00265       _savedFrameMidLineWidth = f->midLineWidth();
00266       _savedFrameStyle = f->frameStyle();
00267 
00268       if (f->frameShape() == QFrame::HLine || f->frameShape() == QFrame::VLine)
00269       {
00270         f->setMidLineWidth(1);
00271         f->setFrameStyle(f->frameShape() | QFrame::Plain);
00272       }
00273       else
00274       {
00275         f->setLineWidth(1);
00276         f->setFrameStyle(QFrame::Box | QFrame::Plain);
00277       }
00278     }
00279   }
00280 }
00281 
00282   void
00283 WebStyle::unPolish(QWidget * w)
00284 {
00285   if (w->inherits("QPushButton"))
00286     w->removeEventFilter(this);
00287 
00288   else if (w == _currentFrame)
00289   {
00290     QFrame * f(static_cast<QFrame *>(w));
00291 
00292     f->setLineWidth(_savedFrameLineWidth);
00293     f->setMidLineWidth(_savedFrameMidLineWidth);
00294     f->setFrameStyle(_savedFrameStyle);
00295   }
00296 }
00297 
00298   bool
00299 WebStyle::eventFilter(QObject * o, QEvent * e)
00300 {
00301   QPushButton * pb(static_cast<QPushButton *>(o));
00302 
00303   if (e->type() == QEvent::Enter)
00304   {
00305     _highlightedButton = pb;
00306     pb->repaint(false);
00307   }
00308   else if (e->type() == QEvent::Leave)
00309   {
00310     _highlightedButton = 0;
00311     pb->repaint(false);
00312   }
00313 
00314   return false;
00315 }
00316 
00317   void
00318 WebStyle::drawButton
00319 (
00320  QPainter * p,
00321  int x,
00322  int y,
00323  int w,
00324  int h,
00325  const QColorGroup & g,
00326  bool sunken,
00327  const QBrush * fill
00328 )
00329 {
00330   p->save();
00331 
00332   if (sunken)
00333     p->setPen(contrastingForeground(g.light(), g.button()));
00334   else
00335     p->setPen(contrastingForeground(g.mid(), g.button()));
00336 
00337   p->setBrush(0 == fill ? Qt::NoBrush : *fill);
00338 
00339   drawFunkyRect(p, x, y, w, h, true);
00340 
00341   p->restore();
00342 }
00343 
00344   QRect
00345 WebStyle::buttonRect(int x, int y, int w, int h)
00346 {
00347   return QRect(x + 2, y + 2, w - 4, h - 4);
00348 }
00349 
00350   void
00351 WebStyle::drawBevelButton
00352 (
00353  QPainter * p,
00354  int x,
00355  int y,
00356  int w,
00357  int h,
00358  const QColorGroup & g,
00359  bool sunken,
00360  const QBrush * fill
00361 )
00362 {
00363   drawButton(p, x, y, w, h, g, sunken, fill);
00364 }
00365 
00366   void
00367 WebStyle::drawPushButton(QPushButton * b, QPainter * p)
00368 {
00369   // Note: painter is already translated for us.
00370 
00371   bool sunken(b->isDown() || b->isOn());
00372   bool hl(_highlightedButton == b);
00373 
00374   QColor bg(b->colorGroup().button());
00375 
00376   p->save();
00377   p->fillRect(b->rect(), b->colorGroup().brush(QPalette::Background));
00378 
00379   if (b->isDefault())
00380   {
00381     QColor c(hl ? b->colorGroup().highlight() : b->colorGroup().mid());
00382 
00383     p->setPen(contrastingForeground(c, bg));
00384 
00385     drawFunkyRect(p, 0, 0, b->width(), b->height(), false);
00386   }
00387 
00388   p->fillRect
00389     (
00390      4,
00391      4,
00392      b->width() - 8,
00393      b->height() - 8,
00394      b->colorGroup().brush(QPalette::Button)
00395     );
00396 
00397   if (b->isEnabled())
00398   {
00399     if (sunken)
00400     {
00401       p->setPen(contrastingForeground(b->colorGroup().light(), bg));
00402     }
00403     else
00404     {
00405       if (hl)
00406         p->setPen(contrastingForeground(b->colorGroup().highlight(), bg));
00407       else
00408         p->setPen(contrastingForeground(b->colorGroup().mid(), bg));
00409     }
00410   }
00411   else
00412   {
00413     p->setPen(b->colorGroup().button());
00414   }
00415 
00416   drawFunkyRect(p, 3, 3, b->width() - 6, b->height() - 6, true);
00417 
00418   p->restore();
00419 }
00420 
00421   void
00422 WebStyle::drawPushButtonLabel(QPushButton * b, QPainter * p)
00423 {
00424   // This is complicated stuff and we don't really want to mess with it.
00425 
00426   KStyle::drawPushButtonLabel(b, p);
00427 }
00428 
00429   void
00430 WebStyle::drawScrollBarControls
00431 (
00432  QPainter * p,
00433  const QScrollBar * sb,
00434  int sliderStart,
00435  uint controls,
00436  uint activeControl
00437 )
00438 {
00439   p->save();
00440 
00441   int sliderMin, sliderMax, sliderLength, buttonDim;
00442 
00443   scrollBarMetrics(sb, sliderMin, sliderMax, sliderLength, buttonDim);
00444 
00445   QRect rSub, rAdd, rSubPage, rAddPage, rSlider;
00446 
00447   scrollBarControlsMetrics
00448     (
00449      sb,
00450      sliderStart,
00451      sliderMin,
00452      sliderMax,
00453      sliderLength,
00454      buttonDim,
00455      rSub,
00456      rAdd,
00457      rSubPage,
00458      rAddPage,
00459      rSlider
00460     );
00461 
00462   QColorGroup g(sb->colorGroup());
00463 
00464   if (controls & AddLine && rAdd.isValid())
00465   {
00466     bool active(activeControl & AddLine);
00467 
00468     QColor c(active ? g.highlight() : g.dark());
00469 
00470     p->setPen(c);
00471     p->setBrush(g.button());
00472     p->drawRect(rAdd);
00473 
00474     Qt::ArrowType t =
00475       sb->orientation() == Qt::Horizontal ? Qt::RightArrow : Qt::DownArrow;
00476 
00477     // Is it me or is KStyle::drawArrow broken ?
00478 
00479     drawArrow
00480       (
00481        p,
00482        t,
00483        true, // FIXME - down ?
00484        rAdd.x(),
00485        rAdd.y(),
00486        rAdd.width(),
00487        rAdd.height(),
00488        g,
00489        true // FIXME - enabled ?
00490       );
00491   }
00492 
00493   if (controls & SubLine && rSub.isValid())
00494   {
00495     bool active(activeControl & SubLine);
00496 
00497     QColor c(active ? g.highlight() : g.dark());
00498 
00499     p->setPen(c);
00500     p->setBrush(g.button());
00501     p->drawRect(rSub);
00502 
00503     Qt::ArrowType t =
00504       sb->orientation() == Qt::Horizontal ? Qt::LeftArrow : Qt::UpArrow;
00505 
00506     drawArrow
00507       (
00508        p,
00509        t,
00510        true, // FIXME - down ?
00511        rSub.x(),
00512        rSub.y(),
00513        rSub.width(),
00514        rSub.height(),
00515        g,
00516        true // FIXME - enabled ?
00517       );
00518   }
00519 
00520   if (controls & SubPage && rSubPage.isValid())
00521   {
00522     p->setPen(g.mid());
00523     p->setBrush(g.base());
00524     p->drawRect(rSubPage);
00525   }
00526 
00527   if (controls & AddPage && rAddPage.isValid())
00528   {
00529     p->setPen(g.mid());
00530     p->setBrush(g.base());
00531     p->drawRect(rAddPage);
00532   }
00533 
00534   if (controls & Slider && rSlider.isValid())
00535   {
00536     p->setPen(activeControl & Slider ? g.highlight() : g.dark());
00537 
00538     p->setBrush(g.button());
00539     p->drawRect(rSlider);
00540 
00541     p->setBrush(g.light());
00542     p->setPen(g.dark());
00543 
00544     if (sliderLength > _scrollBarExtent * 2)
00545     {
00546       int ellipseSize = 
00547         Qt::Horizontal == sb->orientation()
00548         ?
00549         rSlider.height() - 4
00550         :
00551         rSlider.width()  - 4
00552         ;
00553 
00554       QPoint center(rSlider.center());
00555 
00556       if (Qt::Horizontal == sb->orientation())
00557       {
00558         p->drawEllipse
00559           (
00560            center.x() - ellipseSize / 2, rSlider.y() + 2,
00561            ellipseSize, ellipseSize
00562           );
00563       }
00564       else
00565       { 
00566         p->drawEllipse
00567           (
00568            rSlider.x() + 2, center.y() - ellipseSize / 2,
00569            ellipseSize, ellipseSize
00570           );
00571       }
00572     }
00573   }
00574 
00575   p->restore();
00576 }
00577 
00578   QStyle::ScrollControl
00579 WebStyle::scrollBarPointOver
00580 (
00581  const QScrollBar * sb,
00582  int sliderStart,
00583  const QPoint & point
00584 )
00585 {
00586   if (!sb->rect().contains(point))
00587     return NoScroll;
00588 
00589   int sliderMin, sliderMax, sliderLength, buttonDim;
00590 
00591   scrollBarMetrics(sb, sliderMin, sliderMax, sliderLength, buttonDim);
00592 
00593   if (sb->orientation() == Qt::Horizontal)
00594   {
00595     int x = point.x();
00596 
00597     if (x <= buttonDim)
00598       return SubLine;
00599 
00600     else if (x <= buttonDim * 2)
00601       return AddLine;
00602 
00603     else if (x < sliderStart)
00604       return SubPage;
00605 
00606     else if (x < sliderStart+sliderLength)
00607       return Slider;
00608 
00609     return AddPage;
00610   }
00611   else
00612   {
00613     int y = point.y();
00614 
00615     if (y < sliderStart)
00616       return SubPage;
00617 
00618     else if (y < sliderStart + sliderLength)
00619       return Slider;
00620 
00621     else if (y < sliderMax + sliderLength)
00622       return AddPage;
00623 
00624     else if (y < sliderMax + sliderLength + buttonDim)
00625       return SubLine;
00626 
00627     return AddLine;
00628   }
00629 }
00630 
00631   void
00632 WebStyle::scrollBarMetrics
00633 (
00634  const QScrollBar * sb,
00635  int & sliderMin,
00636  int & sliderMax,
00637  int & sliderLength,
00638  int & buttonDim
00639 )
00640 {
00641   int maxlen;
00642 
00643   bool horizontal = sb->orientation() == Qt::Horizontal;
00644 
00645   int len = (horizontal) ? sb->width() : sb->height();
00646 
00647   int extent = (horizontal) ? sb->height() : sb->width();
00648 
00649   if (len > (extent - 1) * 2)
00650     buttonDim = extent;
00651   else
00652     buttonDim = len / 2 - 1;
00653 
00654   if (horizontal)
00655     sliderMin = buttonDim * 2;
00656   else
00657     sliderMin = 1;
00658 
00659   maxlen = len - buttonDim * 2 - 1;
00660 
00661   sliderLength =
00662     (sb->pageStep() * maxlen) /
00663     (sb->maxValue() - sb->minValue() + sb->pageStep());
00664 
00665   if (sliderLength < _scrollBarExtent)
00666     sliderLength = _scrollBarExtent;
00667 
00668   if (sliderLength > maxlen)
00669     sliderLength = maxlen;
00670 
00671   sliderMax = sliderMin + maxlen - sliderLength;
00672 }
00673 
00674   QSize
00675 WebStyle::indicatorSize() const
00676 {
00677   return QSize(_indicatorSize, _indicatorSize);
00678 }
00679 
00680   void
00681 WebStyle::drawIndicator
00682 (
00683  QPainter * p,
00684  int x,
00685  int y,
00686  int w,
00687  int h,
00688  const QColorGroup & g,
00689  int state,
00690  bool down,
00691  bool enabled
00692 )
00693 {
00694   p->save();
00695 
00696   p->fillRect(x, y, w, h, g.background());
00697 
00698   if (enabled)
00699   {
00700     p->setPen(down ? g.highlight() : contrastingForeground(g.dark(), g.background()));
00701   }
00702   else
00703   {
00704     g.mid();
00705   }
00706 
00707   p->drawRect(x, y, w, h);
00708 
00709   if (state != QCheckBox::Off)
00710   {
00711     p->fillRect(x + 2, y + 2, w - 4, h - 4, enabled ? g.highlight() : g.mid());
00712 
00713     if (state == QCheckBox::NoChange)
00714     {
00715       p->fillRect(x + 4, y + 4, w - 8, h - 8, g.background());
00716     }
00717   }
00718 
00719   p->restore();
00720 }
00721 
00722   QSize
00723 WebStyle::exclusiveIndicatorSize() const
00724 {
00725   return QSize(_indicatorSize, _indicatorSize);
00726 }
00727 
00728   void
00729 WebStyle::drawExclusiveIndicator
00730 (
00731  QPainter * p,
00732  int x,
00733  int y,
00734  int w,
00735  int h,
00736  const QColorGroup & g,
00737  bool on,
00738  bool down,
00739  bool enabled
00740 )
00741 {
00742   p->save();
00743 
00744   p->fillRect(x, y, w, h, g.background());
00745 
00746   if (enabled)
00747   {
00748     p->setPen(down ? g.highlight() : contrastingForeground(g.dark(), g.background()));
00749   }
00750   else
00751   {
00752     p->setPen(g.mid());
00753   }
00754 
00755   p->setBrush(g.brush(QPalette::Background));
00756 
00757   // Avoid misshapen ellipses. Qt or X bug ? Who knows...
00758 
00759   if (0 == w % 2)
00760     --w;
00761 
00762   if (0 == h % 2)
00763     --h;
00764 
00765   p->drawEllipse(x, y, w, h);
00766 
00767   if (on)
00768   {
00769     p->setPen(enabled ? g.highlight() : g.mid());
00770     p->setBrush(enabled ? g.highlight() : g.mid());
00771     p->drawEllipse(x + 3, y + 3, w - 6, h - 6);
00772   }
00773 
00774   p->restore();
00775 }
00776 
00777   void
00778 WebStyle::drawIndicatorMask
00779 (
00780  QPainter * p,
00781  int x,
00782  int y,
00783  int w,
00784  int h,
00785  int /* state */
00786 )
00787 {
00788   p->fillRect(x, y, w, h, Qt::color1);
00789 }
00790 
00791   void
00792 WebStyle::drawExclusiveIndicatorMask
00793 (
00794  QPainter * p,
00795  int x,
00796  int y,
00797  int w,
00798  int h,
00799  bool /* on */
00800 )
00801 {
00802   if (0 == w % 2)
00803     --w;
00804 
00805   if (0 == h % 2)
00806     --h;
00807 
00808   p->setPen(Qt::color1);
00809   p->setBrush(Qt::color1);
00810   p->drawEllipse(x, y, w, h);
00811 }
00812 
00813   void
00814 WebStyle::drawComboButton
00815 (
00816  QPainter * p,
00817  int x,
00818  int y,
00819  int w,
00820  int h,
00821  const QColorGroup & g,
00822  bool sunken,
00823  bool editable,
00824  bool enabled,
00825  const QBrush * fill
00826 )
00827 {
00828   p->save();
00829 
00830   p->setPen(Qt::NoPen);
00831   p->setBrush(0 == fill ? g.brush(QPalette::Background) : *fill);
00832   p->drawRect(x, y, w, h);
00833 
00834   if (enabled)
00835   {
00836     if (sunken)
00837       p->setPen(contrastingForeground(g.highlight(), g.background()));
00838     else
00839       p->setPen(contrastingForeground(g.mid(), g.background()));
00840   }
00841   else
00842   {
00843     p->setPen(contrastingForeground(g.mid(), g.background()));
00844   }
00845 
00846   drawFunkyRect(p, x, y, w, h, true);
00847 
00848   p->drawPoint(w - 10, h - 6);
00849   p->drawPoint(w - 9, h - 6);
00850   p->drawPoint(w - 8, h - 6);
00851   p->drawPoint(w - 7, h - 6);
00852   p->drawPoint(w - 6, h - 6);
00853 
00854   p->drawPoint(w - 9, h - 7);
00855   p->drawPoint(w - 8, h - 7);
00856   p->drawPoint(w - 7, h - 7);
00857   p->drawPoint(w - 6, h - 7);
00858 
00859   p->drawPoint(w - 8, h - 8);
00860   p->drawPoint(w - 7, h - 8);
00861   p->drawPoint(w - 6, h - 8);
00862 
00863   p->drawPoint(w - 7, h - 9);
00864   p->drawPoint(w - 6, h - 9);
00865 
00866   p->drawPoint(w - 6, h - 10);
00867 
00868   if (editable)
00869     p->fillRect(comboButtonFocusRect(x, y, w, h), Qt::red);
00870 
00871   p->restore();
00872 }
00873 
00874   QRect
00875 WebStyle::comboButtonRect(int x, int y, int w, int h)
00876 {
00877   return QRect(x + 2, y + 2, w - 20, h - 4);
00878 }
00879 
00880   QRect
00881 WebStyle::comboButtonFocusRect(int x, int y, int w, int h)
00882 {
00883   return QRect(x + 2, y + 2, w - 20, h - 4);
00884 }
00885 
00886   int
00887 WebStyle::sliderLength() const
00888 {
00889   return 13;
00890 }
00891 
00892   void
00893 WebStyle::drawSliderGroove
00894 (
00895  QPainter * p,
00896  int x,
00897  int y,
00898  int w,
00899  int h,
00900  const QColorGroup & g,
00901  QCOORD /* c */,
00902  Qt::Orientation o
00903 )
00904 {
00905   p->save();
00906 
00907   p->setPen(QPen(g.dark(), 0, Qt::DotLine));
00908 
00909   if( o == Qt::Horizontal )
00910     p->drawLine(x, y + h / 2, w, y + h / 2);
00911   else
00912   if( o == Qt::Vertical )
00913     p->drawLine(x + w / 2, y, x + w / 2, h);
00914 
00915   p->restore();
00916 }
00917 
00918   void
00919 WebStyle::drawArrow
00920 (
00921  QPainter * p,
00922  Qt::ArrowType type,
00923  bool down,
00924  int x,
00925  int y,
00926  int w,
00927  int h,
00928  const QColorGroup & g,
00929  bool enabled,
00930  const QBrush * fill
00931 )
00932 {
00933   KStyle::drawArrow(p, type, down, x, y, w, h, g, enabled, fill);
00934 }
00935 
00936   void
00937 WebStyle::drawSlider
00938 (
00939  QPainter * p,
00940  int x,
00941  int y,
00942  int w,
00943  int h,
00944  const QColorGroup & g,
00945  Qt::Orientation o,
00946  bool /* tickAbove */,
00947  bool /* tickBelow */
00948 )
00949 {
00950   p->save();
00951 
00952   p->fillRect(x + 1, y + 1, w - 2, h - 2, g.background());
00953   p->setPen(g.dark());
00954   p->setBrush(g.light());
00955 
00956   int sl = sliderLength();
00957 
00958   if( o == Qt::Horizontal )
00959     p->drawEllipse(x, y + h / 2 - sl / 2, sl, sl);
00960   else
00961   if( o == Qt::Vertical )
00962     p->drawEllipse(x + w / 2 - sl / 2, y, sl, sl);
00963 
00964   p->restore();
00965 }
00966 
00967   void
00968 WebStyle::drawKToolBar
00969 (
00970  QPainter * p,
00971  int x,
00972  int y,
00973  int w,
00974  int h,
00975  const QColorGroup & g,
00976  KToolBarPos /* pos */,
00977  QBrush * /* fill */
00978 )
00979 {
00980   p->save();
00981   p->setPen(g.background());
00982   p->setBrush(g.background());
00983   p->drawRect(x, y, w, h);
00984   p->restore();
00985 }
00986 
00987   void
00988 WebStyle::drawKBarHandle
00989 (
00990  QPainter * p,
00991  int x,
00992  int y,
00993  int w,
00994  int h,
00995  const QColorGroup & g,
00996  KToolBarPos /* pos */,
00997  QBrush * /* fill */
00998 )
00999 {
01000   p->save();
01001   p->setPen(g.mid());
01002   p->setBrush(g.background());
01003   p->drawRect(x + 1, y + 1, w - 2, h - 2);
01004   p->restore();
01005 }
01006 
01007   void
01008 WebStyle::drawKMenuBar
01009 (
01010  QPainter * p,
01011  int x,
01012  int y,
01013  int w,
01014  int h,
01015  const QColorGroup & g,
01016  bool /* macMode */,
01017  QBrush * /* fill */
01018 )
01019 {
01020   p->save();
01021   p->setPen(g.mid());
01022   p->setBrush(g.background());
01023   p->drawRect(x, y, w, h);
01024   p->restore();
01025 }
01026 
01027   void
01028 WebStyle::drawKToolBarButton
01029 (
01030  QPainter * p,
01031  int x,
01032  int y,
01033  int w,
01034  int h,
01035  const QColorGroup & g,
01036  bool sunken,
01037  bool raised,
01038  bool enabled,
01039  bool popup,
01040  KToolButtonType type,
01041  const QString & btext,
01042  const QPixmap * pixmap,
01043  QFont * font,
01044  QWidget * button
01045 )
01046 {
01047   bool toggleAndOn = false;
01048 
01049   if (button->inherits("QButton"))
01050   {
01051     Q3Button * b = static_cast<Q3Button *>(button);
01052     toggleAndOn = b->isToggleButton() && b->isOn();
01053   }
01054 
01055   p->save();
01056 
01057   QColor borderColour;
01058   QColor textColour;
01059   QColor fillColour;
01060 
01061   if (!enabled)
01062   {
01063     borderColour  = g.background();
01064     fillColour    = g.background();
01065     textColour    = g.text();
01066   }
01067   else
01068   {
01069     if (toggleAndOn)
01070     {
01071       borderColour  = g.dark();
01072       fillColour    = g.button();
01073       textColour    = g.buttonText();
01074     }
01075     else if (sunken)
01076     {
01077       borderColour  = g.light();
01078       fillColour    = g.button();
01079       textColour    = g.buttonText();
01080     }
01081     else if (raised)
01082     {
01083       borderColour  = g.highlight();
01084       fillColour    = g.background();
01085       textColour    = g.text();
01086     }
01087     else
01088     {
01089       borderColour  = g.background();
01090       fillColour    = g.background();
01091       textColour    = g.text();
01092     }
01093   }
01094 
01095   p->setPen(borderColour);
01096   p->setBrush(fillColour);
01097 
01098   p->drawRect(x, y, w, h);
01099 
01100   p->setPen(g.background());
01101 
01102   p->drawPoint(x, y);
01103   p->drawPoint(x + w, y);
01104   p->drawPoint(x, y + h);
01105   p->drawPoint(x + w, y + h);
01106 
01107   switch (type)
01108   {
01109     case Icon:
01110 
01111       if (0 != pixmap)
01112         p->drawPixmap
01113           (
01114            x + (w - pixmap->width()) / 2,
01115            y + (h - pixmap->height()) / 2,
01116            *pixmap
01117           );
01118       break;
01119 
01120     case Text:
01121 
01122     if (!btext.isNull())
01123       {
01124         if (0 != font)
01125           p->setFont(*font);
01126 
01127         p->setPen(textColour);
01128 
01129         p->drawText
01130           (
01131            x,
01132            y,
01133            w,
01134            h,
01135            Qt::AlignCenter,
01136            btext
01137           );
01138       }
01139 
01140       break;
01141 
01142     case IconTextRight:
01143 
01144       if (0 != pixmap)
01145         p->drawPixmap
01146           (
01147            x + 2,
01148            y + (h - pixmap->height()) / 2,
01149            *pixmap
01150           );
01151 
01152       if (!btext.isNull())
01153       {
01154         if (0 != font)
01155           p->setFont(*font);
01156 
01157         p->setPen(textColour);
01158 
01159         if (0 != pixmap)
01160         {
01161           int textLeft = pixmap->width() + 4;
01162 
01163           p->drawText
01164             (
01165              x + textLeft,
01166              y,
01167              w - textLeft,
01168              h,
01169              Qt::AlignVCenter | Qt::AlignLeft,
01170              btext
01171             );
01172         }
01173         else
01174         {
01175           p->drawText
01176             (
01177              x,
01178              y,
01179              w,
01180              h,
01181              Qt::AlignVCenter | Qt::AlignLeft,
01182              btext
01183             );
01184         }
01185       }
01186       break;
01187 
01188     case IconTextBottom:
01189 
01190       if (0 != pixmap)
01191         p->drawPixmap
01192           (
01193            x + (w - pixmap->width()) / 2,
01194            y + 2,
01195            *pixmap
01196           );
01197 
01198       if (!btext.isNull())
01199       {
01200         if (0 != font)
01201           p->setFont(*font);
01202 
01203         p->setPen(textColour);
01204 
01205         if (0 != pixmap)
01206         {
01207           int textTop = y + pixmap->height() + 4;
01208 
01209           p->drawText
01210             (
01211              x + 2,
01212              textTop,
01213              w - 4,
01214              h - x - textTop,
01215              Qt::AlignCenter,
01216              btext
01217             );
01218         }
01219         else
01220         {
01221           p->drawText
01222             (
01223              x,
01224              y,
01225              w,
01226              h,
01227              Qt::AlignCenter,
01228              btext
01229             );
01230         }
01231       }
01232       break;
01233 
01234     default:
01235       break;
01236   }
01237 
01238   if (popup)
01239   {
01240     p->setPen(g.dark());
01241     for (int px = 0; px < 5; ++px)
01242       for (int py = 0; py < 5 - px; ++py)
01243         p->drawPoint(w - 6 - px, h - 6 - py);
01244   }
01245       
01246   p->restore();
01247 }
01248 
01249   void
01250 WebStyle::drawKMenuItem
01251 (
01252  QPainter * p,
01253  int x,
01254  int y,
01255  int w,
01256  int h,
01257  const QColorGroup & g,
01258  bool active,
01259  QMenuItem * mi,
01260  QBrush * /* fill */
01261 )
01262 {
01263   p->save();
01264 
01265   QColor bg(active ? g.highlight() : g.background());
01266 
01267   p->fillRect(x, y, w, h, bg);
01268 
01269   QColor textColour =
01270     active ?
01271     contrastingForeground(g.highlightedText(), bg) :
01272     contrastingForeground(g.text(), bg);
01273 
01274   QApplication::style().drawItem
01275     (
01276      p,
01277      x,
01278      y,
01279      w,
01280      h,
01281      Qt::AlignCenter | Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine,
01282      g,
01283      mi->isEnabled(),
01284      mi->pixmap(),
01285      mi->text(),
01286      -1,
01287      &textColour
01288     );
01289 
01290   p->restore();
01291 }
01292 
01293   void
01294 WebStyle::drawPopupMenuItem
01295 (
01296  QPainter * p,
01297  bool checkable,
01298  int maxpmw,
01299  int tab,
01300  QMenuItem * mi,
01301  const QPalette & pal,
01302  bool act,
01303  bool enabled,
01304  int x,
01305  int y,
01306  int w,
01307  int h
01308 )
01309 {
01310   // TODO
01311   KStyle::drawPopupMenuItem(p, checkable, maxpmw, tab, mi, pal, act, enabled, x, y, w, h);
01312 }
01313 
01314   void
01315 WebStyle::drawKProgressBlock
01316 (
01317  QPainter * p,
01318  int x,
01319  int y,
01320  int w,
01321  int h,
01322  const QColorGroup & g,
01323  QBrush * fill
01324 )
01325 {
01326   p->save();
01327 
01328   p->setBrush(0 == fill ? Qt::NoBrush : *fill);
01329 
01330   p->fillRect(x, y, w, h, g.highlight());
01331 
01332   p->restore();
01333 }
01334 
01335   void
01336 WebStyle::drawFocusRect
01337 (
01338  QPainter * p,
01339  const QRect & r,
01340  const QColorGroup & g,
01341  const QColor * pen,
01342  bool atBorder
01343 )
01344 {
01345   p->save();
01346 
01347   if (0 != pen)
01348   p->setPen(0 == pen ? g.foreground() : *pen);
01349   p->setBrush(Qt::NoBrush);
01350 
01351   if (atBorder)
01352   {
01353     p->drawRect(QRect(r.x() + 1, r.y() + 1, r.width() - 2, r.height() - 2));
01354   }
01355   else
01356   {
01357     p->drawRect(r);
01358   }
01359 
01360   p->restore();
01361 }
01362 
01363   void
01364 WebStyle::drawPanel
01365 (
01366  QPainter * p,
01367  int x,
01368  int y,
01369  int w,
01370  int h,
01371  const QColorGroup & g,
01372  bool /* sunken */,
01373  int /* lineWidth */,
01374  const QBrush * fill
01375 )
01376 {
01377   p->save();
01378 
01379   p->setPen(g.dark());
01380 
01381   p->setBrush(0 == fill ? Qt::NoBrush : *fill);
01382 
01383   p->drawRect(x, y, w, h);
01384 
01385   p->restore();
01386 }
01387 
01388   void
01389 WebStyle::drawPopupPanel
01390 (
01391  QPainter * p,
01392  int x,
01393  int y,
01394  int w,
01395  int h,
01396  const QColorGroup & g,
01397  int /* lineWidth */,
01398  const QBrush * fill
01399 )
01400 {
01401   p->save();
01402 
01403   p->setPen(g.dark());
01404 
01405   p->setBrush(0 == fill ? Qt::NoBrush : *fill);
01406 
01407   p->drawRect(x, y, w, h);
01408 
01409   p->restore();
01410 }
01411 
01412   void
01413 WebStyle::drawSeparator
01414 (
01415  QPainter * p,
01416  int x,
01417  int y,
01418  int w,
01419  int h,
01420  const QColorGroup & g,
01421  bool /* sunken */,
01422  int /* lineWidth */,
01423  int /* midLineWidth */
01424 )
01425 {
01426   p->save();
01427 
01428   p->setPen(g.dark());
01429 
01430   if (w > h)
01431   {
01432     p->drawLine(x, y + h / 2, x + w, y + h / 2);
01433   }
01434   else
01435   {
01436     p->drawLine(x + w / 2, y, x + w / 2, y + h);
01437   }
01438 
01439   p->restore();
01440 }
01441 
01442   void
01443 WebStyle::drawTab
01444 (
01445  QPainter * p,
01446  const QTabBar * tabBar,
01447  QTab * tab,
01448  bool selected
01449 )
01450 {
01451   QRect r(tab->rect());
01452 
01453   QColorGroup g(tabBar->colorGroup());
01454 
01455   p->save();
01456 
01457   p->setPen(selected ? g.dark() : g.mid());
01458   p->fillRect(r, g.brush(QPalette::Background));
01459 
01460   switch (tabBar->shape())
01461   {
01462     case QTabBar::RoundedNorth:
01463     case QTabBar:: TriangularNorth:
01464       p->drawLine(r.left(), r.top(), r.left(), r.bottom());
01465       p->drawLine(r.left(), r.top(), r.right(), r.top());
01466       p->drawLine(r.right(), r.top(), r.right(), r.bottom());
01467       if (!selected)
01468       {
01469         p->setPen(g.dark());
01470         p->drawLine(r.left(), r.bottom(), r.right(), r.bottom());
01471       }
01472       break;
01473     case QTabBar:: RoundedSouth:
01474     case QTabBar:: TriangularSouth:
01475       if (!selected)
01476       {
01477         p->setPen(g.dark());
01478         p->drawLine(r.left(), r.top(), r.right(), r.top());
01479       }
01480       p->drawLine(r.left(), r.top(), r.left(), r.bottom());
01481       p->drawLine(r.left(), r.bottom(), r.right(), r.bottom());
01482       p->drawLine(r.right(), r.top(), r.right(), r.bottom());
01483       break;
01484   }
01485 
01486   p->restore();
01487 }
01488 
01489   void
01490 WebStyle::drawTabMask
01491 (
01492  QPainter * p,
01493  const QTabBar *,
01494  QTab * tab,
01495  bool
01496 )
01497 {
01498   p->fillRect(tab->rect(), Qt::color1);
01499 }
01500 
01501   void
01502 WebStyle::drawKickerHandle
01503 (
01504  QPainter * p,
01505  int x,
01506  int y,
01507  int w,
01508  int h,
01509  const QColorGroup & g,
01510  QBrush * fill
01511 )
01512 {
01513   p->save();
01514 
01515   p->setPen(g.mid());
01516 
01517   p->setBrush(0 == fill ? Qt::NoBrush : *fill);
01518 
01519   p->drawRect(x, y, w, h);
01520   
01521   p->restore();
01522 }
01523 
01524   void
01525 WebStyle::drawKickerAppletHandle
01526 (
01527  QPainter * p,
01528  int x,
01529  int y,
01530  int w,
01531  int h,
01532  const QColorGroup & g,
01533  QBrush * fill
01534 )
01535 {
01536   p->save();
01537 
01538   p->setPen(g.mid());
01539 
01540   p->setBrush(0 == fill ? Qt::NoBrush : *fill);
01541 
01542   p->drawRect(x, y, w, h);
01543   
01544   p->restore();
01545 }
01546 
01547   void
01548 WebStyle::drawKickerTaskButton
01549 (
01550  QPainter * p,
01551  int x,
01552  int y,
01553  int w,
01554  int h,
01555  const QColorGroup & g,
01556  const QString & text,
01557  bool active,
01558  QPixmap * icon,
01559  QBrush * /* fill */
01560 )
01561 {
01562   p->save();
01563 
01564   QColor bg;
01565 
01566   if (active)
01567   {
01568     p->setPen(g.light());
01569     bg = g.highlight();
01570   }
01571   else
01572   {
01573     p->setPen(g.mid());
01574     bg = g.button();
01575   }
01576 
01577   p->setBrush(bg);
01578 
01579   p->drawRect(x, y, w, h);
01580 
01581   if (text.isEmpty() && 0 == icon)
01582   {
01583     p->restore();
01584     return;
01585   }
01586 
01587   const int pxWidth = 20;
01588 
01589   int textPos = pxWidth;
01590 
01591   QRect br(buttonRect(x, y, w, h));
01592 
01593   if ((0 != icon) && !icon->isNull())
01594   {
01595     int dx = (pxWidth - icon->width())  / 2;
01596     int dy = (h - icon->height())       / 2;
01597 
01598     p->drawPixmap(br.x() + dx, dy, *icon);
01599   }
01600 
01601   QString s(text);
01602 
01603   static QString modStr =
01604     QString::fromUtf8("[") + i18n("modified") + QString::fromUtf8("]");
01605 
01606   int modStrPos = s.find(modStr);
01607 
01608   if (-1 != modStrPos)
01609   {
01610     // +1 because we include a space after the closing brace.
01611     s.remove(modStrPos, modStr.length() + 1);
01612 
01613     QPixmap modPixmap = SmallIcon("modified");
01614 
01615     int dx = (pxWidth - modPixmap.width())  / 2;
01616     int dy = (h       - modPixmap.height()) / 2;
01617 
01618     p->drawPixmap(br.x() + textPos + dx, dy, modPixmap);
01619 
01620     textPos += pxWidth;
01621   }
01622 
01623   if (!s.isEmpty())
01624   {
01625     if (p->fontMetrics().width(s) > br.width() - textPos)
01626     {
01627       int maxLen = br.width() - textPos - p->fontMetrics().width("...");
01628 
01629       while ((!s.isEmpty()) && (p->fontMetrics().width(s) > maxLen))
01630         s.truncate(s.length() - 1);
01631 
01632       s.append("...");
01633     }
01634 
01635     if (active)
01636     {
01637       p->setPen(contrastingForeground(g.buttonText(), bg));
01638     }
01639     else
01640     {
01641       p->setPen(contrastingForeground(g.text(), bg));
01642     }
01643 
01644     p->setPen(Qt::white);
01645 
01646     p->drawText
01647       (
01648        br.x() + textPos,
01649        -1,
01650        w - textPos,
01651        h,
01652        Qt::AlignLeft | Qt::AlignVCenter,
01653        s
01654       );
01655   }
01656 
01657   p->restore();
01658   p->setPen(Qt::white);
01659 }
01660 
01661   int
01662 WebStyle::popupMenuItemHeight(bool, QMenuItem * i, const QFontMetrics & fm)
01663 {
01664   if (i->isSeparator())
01665     return 1;
01666 
01667   int h = 0;
01668 
01669   if (0 != i->pixmap())
01670   {
01671     h = i->pixmap()->height();
01672   }
01673 
01674   if (0 != i->iconSet())
01675   {
01676     h = qMax
01677       (
01678        i->iconSet()->pixmap(QIcon::Small, QIcon::Normal).height(),
01679        h
01680       );
01681   }
01682 
01683   h = qMax(fm.height() + 4, h);
01684 
01685   h = qMax(18, h);
01686 
01687   return h;
01688 
01689 }
01690 

KStyles

Skip menu "KStyles"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal