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

Plasma

style.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright © 2008 Fredrik Höglund <fredrik@kde.org>
00003  *   Copyright © 2008 Marco Martin <notmart@gmail.com>
00004  *
00005  *   This program is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU Library General Public License as
00007  *   published by the Free Software Foundation; either version 2, or
00008  *   (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details
00014  *
00015  *   You should have received a copy of the GNU Library General Public
00016  *   License along with this program; if not, write to the
00017  *   Free Software Foundation, Inc.,
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 #include "style_p.h"
00022 
00023 #include <QPainter>
00024 #include <QStyleOptionComplex>
00025 #include <QSpinBox>
00026 #include <QComboBox>
00027 #include <QApplication>
00028 
00029 #include <kdebug.h>
00030 
00031 #include <plasma/framesvg.h>
00032 
00033 namespace Plasma {
00034 
00035 class StylePrivate
00036 {
00037 public:
00038     StylePrivate(Style *style)
00039         : q(style),
00040           scrollbar(0),
00041           textBox(0)
00042     {
00043     }
00044 
00045     ~StylePrivate()
00046     {
00047     }
00048 
00049     void createScrollbar()
00050     {
00051         if (!scrollbar) {
00052             scrollbar = new Plasma::FrameSvg(q);
00053             scrollbar->setImagePath("widgets/scrollbar");
00054             scrollbar->setCacheAllRenderedFrames(true);
00055         }
00056     }
00057 
00058     void createTextBox()
00059     {
00060         if (!textBox) {
00061             textBox = new Plasma::FrameSvg(q);
00062             textBox->setImagePath("widgets/frame");
00063             textBox->setElementPrefix("sunken");
00064         }
00065     }
00066 
00067     Style *q;
00068     Plasma::FrameSvg *scrollbar;
00069     Plasma::FrameSvg *textBox;
00070     static Plasma::Style::Ptr s_sharedStyle;
00071 };
00072 
00073 Style::Ptr StylePrivate::s_sharedStyle(0);
00074 
00075 Style::Ptr Style::sharedStyle()
00076 {
00077     if (!StylePrivate::s_sharedStyle) {
00078         StylePrivate::s_sharedStyle = new Style();
00079     }
00080 
00081     return StylePrivate::s_sharedStyle;
00082 }
00083 
00084 void Style::doneWithSharedStyle()
00085 {
00086     if (StylePrivate::s_sharedStyle.isUnique()) {
00087         StylePrivate::s_sharedStyle = 0;
00088     }
00089 }
00090 
00091 Style::Style()
00092      : QCommonStyle(),
00093        d(new StylePrivate(this))
00094 {
00095 }
00096 
00097 Style::~Style()
00098 {
00099     delete d;
00100 }
00101 
00102 void Style::drawComplexControl(ComplexControl control,
00103                                const QStyleOptionComplex *option,
00104                                QPainter *painter,
00105                                const QWidget *widget) const
00106 {
00107     switch (control) {
00108     case CC_ScrollBar: {
00109         d->createScrollbar();
00110 
00111         painter->save();
00112         painter->setRenderHint(QPainter::Antialiasing);
00113 
00114         const bool sunken = option->state & State_Sunken;
00115         const QStyleOptionSlider *scrollOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
00116         QString prefix;
00117 
00118         if (option->state & State_MouseOver) {
00119             prefix= "mouseover-";
00120         }
00121 
00122         QRect subLine;
00123         QRect addLine;
00124         if (scrollOption && scrollOption->orientation == Qt::Horizontal) {
00125             subLine = d->scrollbar->elementRect(prefix + "arrow-left").toRect();
00126             addLine = d->scrollbar->elementRect(prefix + "arrow-right").toRect();
00127         } else {
00128             subLine = d->scrollbar->elementRect(prefix + "arrow-up").toRect();
00129             addLine = d->scrollbar->elementRect(prefix + "arrow-down").toRect();
00130         }
00131 
00132         subLine.moveCenter(subControlRect(control, option, SC_ScrollBarSubLine, widget).center());
00133         addLine.moveCenter(subControlRect(control, option, SC_ScrollBarAddLine, widget).center());
00134 
00135         const QRect slider =
00136             subControlRect(control, option, SC_ScrollBarSlider, widget).adjusted(1, 0, -1, 0);
00137 
00138         d->scrollbar->setElementPrefix("background");
00139         d->scrollbar->resizeFrame(option->rect.size());
00140         d->scrollbar->paintFrame(painter);
00141 
00142         if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarSlider) {
00143             d->scrollbar->setElementPrefix("sunken-slider");
00144         } else {
00145             d->scrollbar->setElementPrefix(prefix + "slider");
00146         }
00147 
00148         d->scrollbar->resizeFrame(slider.size());
00149         d->scrollbar->paintFrame(painter, slider.topLeft());
00150 
00151         if (scrollOption && scrollOption->orientation == Qt::Horizontal) {
00152             if (sunken && scrollOption->activeSubControls & SC_ScrollBarAddLine) {
00153                 d->scrollbar->paint(painter, addLine, "sunken-arrow-right");
00154             } else {
00155                 d->scrollbar->paint(painter, addLine, prefix + "arrow-right");
00156             }
00157 
00158             if (sunken && scrollOption->activeSubControls & SC_ScrollBarSubLine) {
00159                 d->scrollbar->paint(painter, subLine, "sunken-arrow-left");
00160             } else {
00161                 d->scrollbar->paint(painter, subLine, prefix + "arrow-left");
00162             }
00163         } else {
00164             if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarAddLine) {
00165                 d->scrollbar->paint(painter, addLine, "sunken-arrow-down");
00166             } else {
00167                 d->scrollbar->paint(painter, addLine, prefix + "arrow-down");
00168             }
00169 
00170             if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarSubLine) {
00171                 d->scrollbar->paint(painter, subLine, "sunken-arrow-up");
00172             } else {
00173                 d->scrollbar->paint(painter, subLine, prefix + "arrow-up");
00174             }
00175         }
00176 
00177         painter->restore();
00178         break;
00179     }
00180     case CC_SpinBox: {
00181         d->createTextBox();
00182 
00183         d->textBox->resizeFrame(option->rect.size());
00184         d->textBox->paintFrame(painter);
00185 
00186         const QStyleOptionSpinBox *spinOpt = qstyleoption_cast<const QStyleOptionSpinBox *>(option);
00187         bool upSunken = (spinOpt->activeSubControls & SC_SpinBoxUp) &&
00188                          (spinOpt->state & (State_Sunken | State_On));
00189         bool downSunken = (spinOpt->activeSubControls & SC_SpinBoxDown) &&
00190                            (spinOpt->state & (State_Sunken | State_On));
00191 
00192         const QSpinBox *spin = qobject_cast<const QSpinBox *>(widget);
00193         PrimitiveElement pe;
00194         if (spin->buttonSymbols() == QSpinBox::PlusMinus) {
00195             pe = PE_IndicatorSpinPlus;
00196         } else {
00197             pe = PE_IndicatorArrowUp;
00198         }
00199 
00200         QStyleOption upOpt;
00201         upOpt = *option;
00202         upOpt.rect = subControlRect(CC_SpinBox, option, SC_SpinBoxUp, widget);
00203 
00204         if (upSunken) {
00205             upOpt.state = State_Sunken|State_Enabled;
00206         } else {
00207             upOpt.state = State_Enabled;
00208         }
00209 
00210         qApp->style()->drawPrimitive(pe, &upOpt, painter, widget);
00211 
00212         if (spin->buttonSymbols() == QSpinBox::PlusMinus) {
00213             pe = PE_IndicatorSpinMinus;
00214         } else {
00215             pe = PE_IndicatorArrowDown;
00216         }
00217 
00218         QStyleOption downOpt;
00219         downOpt= *option;
00220         downOpt.rect = subControlRect(CC_SpinBox, option, SC_SpinBoxDown, widget);
00221 
00222         if (downSunken) {
00223             downOpt.state = State_Sunken|State_Enabled;
00224         } else {
00225             downOpt.state = State_Enabled;
00226         }
00227 
00228         qApp->style()->drawPrimitive(pe, &downOpt, painter, widget);
00229         break;
00230     }
00231     case CC_ComboBox: {
00232         const QComboBox *combo = qobject_cast<const QComboBox *>(widget);
00233         if (!combo->isEditable()) {
00234             qApp->style()->drawComplexControl(control, option, painter, widget);
00235         } else {
00236             d->createTextBox();
00237             d->textBox->resizeFrame(option->rect.size());
00238             d->textBox->paintFrame(painter);
00239 
00240             QStyleOption arrowOpt;
00241             arrowOpt = *option;
00242             arrowOpt.rect = subControlRect(CC_ComboBox, option, SC_ComboBoxArrow, widget);
00243             qApp->style()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, painter, widget);
00244         }
00245         break;
00246     }
00247     default:
00248         qApp->style()->drawComplexControl(control, option, painter, widget);
00249     }
00250 }
00251 
00252 void Style::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
00253 {
00254     Q_UNUSED(widget)
00255 
00256     switch (element) {
00257     case PE_PanelLineEdit:
00258         //comboboxes draws their own frame
00259         if (qobject_cast<QComboBox *>(widget->parent())) {
00260             return;
00261         }
00262         d->createTextBox();
00263 
00264         d->textBox->resizeFrame(option->rect.size());
00265         d->textBox->paintFrame(painter);
00266         break;
00267     default:
00268         qApp->style()->drawPrimitive(element, option, painter, widget);
00269     }
00270 }
00271 
00272 int Style::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
00273 {
00274     switch (metric) {
00275     case PM_ScrollBarExtent: {
00276         d->createScrollbar();
00277         const QStyleOptionSlider *scrollOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
00278         if (scrollOption && scrollOption->orientation == Qt::Vertical) {
00279             return d->scrollbar->elementSize("arrow-down").width() + 2;
00280         } else {
00281             return d->scrollbar->elementSize("arrow-left").height() + 2;
00282         }
00283     }
00284     default:
00285         return QCommonStyle::pixelMetric(metric, option, widget);
00286     }
00287 }
00288 
00289 }
00290 
00291 #include "style_p.moc"
00292 
00293 

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs 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