• Skip to content
  • Skip to link menu
KDE 4.3 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 #include <plasma/theme.h>
00033 
00034 namespace Plasma {
00035 
00036 class StylePrivate
00037 {
00038 public:
00039     StylePrivate(Style *style)
00040         : q(style),
00041           scrollbar(0),
00042           textBox(0)
00043     {
00044     }
00045 
00046     ~StylePrivate()
00047     {
00048     }
00049 
00050     void createScrollbar()
00051     {
00052         if (!scrollbar) {
00053             scrollbar = new Plasma::FrameSvg(q);
00054             scrollbar->setImagePath("widgets/scrollbar");
00055             scrollbar->setCacheAllRenderedFrames(true);
00056         }
00057     }
00058 
00059     void createTextBox()
00060     {
00061         if (!textBox) {
00062             textBox = new Plasma::FrameSvg(q);
00063             textBox->setImagePath("widgets/frame");
00064             textBox->setElementPrefix("sunken");
00065         }
00066     }
00067 
00068     Style *q;
00069     Plasma::FrameSvg *scrollbar;
00070     Plasma::FrameSvg *textBox;
00071     static Plasma::Style::Ptr s_sharedStyle;
00072 };
00073 
00074 Style::Ptr StylePrivate::s_sharedStyle(0);
00075 
00076 Style::Ptr Style::sharedStyle()
00077 {
00078     if (!StylePrivate::s_sharedStyle) {
00079         StylePrivate::s_sharedStyle = new Style();
00080     }
00081 
00082     return StylePrivate::s_sharedStyle;
00083 }
00084 
00085 void Style::doneWithSharedStyle()
00086 {
00087     if (StylePrivate::s_sharedStyle.isUnique()) {
00088         StylePrivate::s_sharedStyle = 0;
00089     }
00090 }
00091 
00092 Style::Style()
00093      : QCommonStyle(),
00094        d(new StylePrivate(this))
00095 {
00096 }
00097 
00098 Style::~Style()
00099 {
00100     delete d;
00101 }
00102 
00103 void Style::drawComplexControl(ComplexControl control,
00104                                const QStyleOptionComplex *option,
00105                                QPainter *painter,
00106                                const QWidget *widget) const
00107 {
00108     if (Theme::defaultTheme()->useNativeWidgetStyle()) {
00109         qApp->style()->drawComplexControl(control, option, painter, widget);
00110         return;
00111     }
00112 
00113     switch (control) {
00114     case CC_ScrollBar: {
00115         d->createScrollbar();
00116 
00117         painter->save();
00118         painter->setRenderHint(QPainter::Antialiasing);
00119 
00120         const bool sunken = option->state & State_Sunken;
00121         const QStyleOptionSlider *scrollOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
00122         QString prefix;
00123 
00124         if (option->state & State_MouseOver) {
00125             prefix= "mouseover-";
00126         }
00127 
00128         QRect subLine;
00129         QRect addLine;
00130         if (scrollOption && scrollOption->orientation == Qt::Horizontal) {
00131             subLine = d->scrollbar->elementRect(prefix + "arrow-left").toRect();
00132             addLine = d->scrollbar->elementRect(prefix + "arrow-right").toRect();
00133         } else {
00134             subLine = d->scrollbar->elementRect(prefix + "arrow-up").toRect();
00135             addLine = d->scrollbar->elementRect(prefix + "arrow-down").toRect();
00136         }
00137 
00138         subLine.moveCenter(subControlRect(control, option, SC_ScrollBarSubLine, widget).center());
00139         addLine.moveCenter(subControlRect(control, option, SC_ScrollBarAddLine, widget).center());
00140 
00141     QRect slider = subControlRect(control, option, SC_ScrollBarSlider, widget);
00142 
00143         if (scrollOption && scrollOption->orientation == Qt::Horizontal) {
00144             slider.adjust(0, 1, 0, -1);
00145         } else {
00146             slider.adjust(1, 0, -1, 0);
00147         }
00148 
00149         if (scrollOption && scrollOption->orientation == Qt::Horizontal && d->scrollbar->hasElement("background-horizontal-center")) {
00150             d->scrollbar->setElementPrefix("background-horizontal");
00151         } else if (scrollOption && scrollOption->orientation == Qt::Vertical && d->scrollbar->hasElement("background-vertical-center")) {
00152             d->scrollbar->setElementPrefix("background-vertical");
00153         } else {
00154             d->scrollbar->setElementPrefix("background");
00155         }
00156         d->scrollbar->resizeFrame(option->rect.size());
00157         d->scrollbar->paintFrame(painter);
00158 
00159         if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarSlider) {
00160             d->scrollbar->setElementPrefix("sunken-slider");
00161         } else {
00162             d->scrollbar->setElementPrefix(prefix + "slider");
00163         }
00164 
00165         d->scrollbar->resizeFrame(slider.size());
00166         d->scrollbar->paintFrame(painter, slider.topLeft());
00167 
00168         if (scrollOption && scrollOption->orientation == Qt::Horizontal) {
00169             if (sunken && scrollOption->activeSubControls & SC_ScrollBarAddLine) {
00170                 d->scrollbar->paint(painter, addLine, "sunken-arrow-right");
00171             } else {
00172                 d->scrollbar->paint(painter, addLine, prefix + "arrow-right");
00173             }
00174 
00175             if (sunken && scrollOption->activeSubControls & SC_ScrollBarSubLine) {
00176                 d->scrollbar->paint(painter, subLine, "sunken-arrow-left");
00177             } else {
00178                 d->scrollbar->paint(painter, subLine, prefix + "arrow-left");
00179             }
00180         } else {
00181             if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarAddLine) {
00182                 d->scrollbar->paint(painter, addLine, "sunken-arrow-down");
00183             } else {
00184                 d->scrollbar->paint(painter, addLine, prefix + "arrow-down");
00185             }
00186 
00187             if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarSubLine) {
00188                 d->scrollbar->paint(painter, subLine, "sunken-arrow-up");
00189             } else {
00190                 d->scrollbar->paint(painter, subLine, prefix + "arrow-up");
00191             }
00192         }
00193 
00194         painter->restore();
00195         break;
00196     }
00197     case CC_SpinBox: {
00198         d->createTextBox();
00199 
00200         d->textBox->resizeFrame(option->rect.size());
00201         d->textBox->paintFrame(painter);
00202 
00203         const QStyleOptionSpinBox *spinOpt = qstyleoption_cast<const QStyleOptionSpinBox *>(option);
00204         bool upSunken = (spinOpt->activeSubControls & SC_SpinBoxUp) &&
00205                          (spinOpt->state & (State_Sunken | State_On));
00206         bool downSunken = (spinOpt->activeSubControls & SC_SpinBoxDown) &&
00207                            (spinOpt->state & (State_Sunken | State_On));
00208 
00209         const QSpinBox *spin = qobject_cast<const QSpinBox *>(widget);
00210         PrimitiveElement pe;
00211         if (spin->buttonSymbols() == QSpinBox::PlusMinus) {
00212             pe = PE_IndicatorSpinPlus;
00213         } else {
00214             pe = PE_IndicatorArrowUp;
00215         }
00216 
00217         QStyleOption upOpt;
00218         upOpt = *option;
00219         upOpt.rect = subControlRect(CC_SpinBox, option, SC_SpinBoxUp, widget);
00220 
00221         if (upSunken) {
00222             upOpt.state = State_Sunken|State_Enabled;
00223         } else {
00224             upOpt.state = State_Enabled;
00225         }
00226 
00227         qApp->style()->drawPrimitive(pe, &upOpt, painter, widget);
00228 
00229         if (spin->buttonSymbols() == QSpinBox::PlusMinus) {
00230             pe = PE_IndicatorSpinMinus;
00231         } else {
00232             pe = PE_IndicatorArrowDown;
00233         }
00234 
00235         QStyleOption downOpt;
00236         downOpt= *option;
00237         downOpt.rect = subControlRect(CC_SpinBox, option, SC_SpinBoxDown, widget);
00238 
00239         if (downSunken) {
00240             downOpt.state = State_Sunken|State_Enabled;
00241         } else {
00242             downOpt.state = State_Enabled;
00243         }
00244 
00245         qApp->style()->drawPrimitive(pe, &downOpt, painter, widget);
00246         break;
00247     }
00248     case CC_ComboBox: {
00249         const QComboBox *combo = qobject_cast<const QComboBox *>(widget);
00250         if (!combo->isEditable()) {
00251             qApp->style()->drawComplexControl(control, option, painter, widget);
00252         } else {
00253             d->createTextBox();
00254             d->textBox->resizeFrame(option->rect.size());
00255             d->textBox->paintFrame(painter);
00256 
00257             QStyleOption arrowOpt;
00258             arrowOpt = *option;
00259             arrowOpt.rect = subControlRect(CC_ComboBox, option, SC_ComboBoxArrow, widget);
00260             qApp->style()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, painter, widget);
00261         }
00262         break;
00263     }
00264     default:
00265         qApp->style()->drawComplexControl(control, option, painter, widget);
00266     }
00267 }
00268 
00269 void Style::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
00270 {
00271     if (Theme::defaultTheme()->useNativeWidgetStyle()) {
00272         qApp->style()->drawPrimitive(element, option, painter, widget);
00273         return;
00274     }
00275 
00276     switch (element) {
00277     case PE_PanelLineEdit:
00278         //comboboxes draws their own frame
00279         if (qobject_cast<QComboBox *>(widget->parent())) {
00280             return;
00281         }
00282         d->createTextBox();
00283 
00284         d->textBox->resizeFrame(option->rect.size());
00285         d->textBox->paintFrame(painter);
00286         break;
00287     default:
00288         qApp->style()->drawPrimitive(element, option, painter, widget);
00289     }
00290 }
00291 
00292 QRect Style::subControlRect(ComplexControl control, const QStyleOptionComplex *option,
00293                             SubControl subControl, const QWidget *widget) const
00294 {
00295     QRect rect(QCommonStyle::subControlRect(control, option, subControl, widget));
00296     switch (control) {
00297     case CC_Slider: {
00298         const QStyleOptionSlider *sliderOpt = qstyleoption_cast<const QStyleOptionSlider *>(option);
00299         if (sliderOpt) {
00300             if (sliderOpt->orientation == Qt::Horizontal) {
00301                 rect.moveCenter(QPoint(rect.center().x(), option->rect.center().y()));
00302             } else {
00303                 rect.moveCenter(QPoint(option->rect.center().x(), rect.center().y()));
00304             }
00305         }
00306         return rect;
00307         break;
00308     }
00309     default:
00310         return rect;
00311     }
00312 }
00313 
00314 int Style::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
00315 {
00316     if (Theme::defaultTheme()->useNativeWidgetStyle()) {
00317         return qApp->style()->pixelMetric(metric, option, widget);
00318     }
00319 
00320     switch (metric) {
00321     case PM_ScrollBarExtent: {
00322         d->createScrollbar();
00323         const QStyleOptionSlider *scrollOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
00324         if (scrollOption && scrollOption->orientation == Qt::Vertical) {
00325             return d->scrollbar->elementSize("arrow-down").width() + 2;
00326         } else {
00327             return d->scrollbar->elementSize("arrow-left").height() + 2;
00328         }
00329     }
00330     default:
00331         return qApp->style()->pixelMetric(metric, option, widget);
00332     }
00333 }
00334 
00335 }
00336 
00337 #include "style_p.moc"
00338 
00339 

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
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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