2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Fredrik Höglund <fredrik@kde.org>
|
|
|
|
* Copyright © 2008 Marco Martin <notmart@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2008-11-15 08:08:08 +01:00
|
|
|
#include "style_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOptionComplex>
|
2009-01-17 19:00:33 +01:00
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QApplication>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-04 03:39:56 +01:00
|
|
|
#include <kdebug.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include <plasma/framesvg.h>
|
2009-02-24 23:42:06 +01:00
|
|
|
#include <plasma/theme.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
|
|
|
class StylePrivate
|
|
|
|
{
|
|
|
|
public:
|
2009-01-17 19:00:33 +01:00
|
|
|
StylePrivate(Style *style)
|
|
|
|
: q(style),
|
|
|
|
scrollbar(0),
|
|
|
|
textBox(0)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~StylePrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
void createScrollbar()
|
|
|
|
{
|
|
|
|
if (!scrollbar) {
|
|
|
|
scrollbar = new Plasma::FrameSvg(q);
|
|
|
|
scrollbar->setImagePath("widgets/scrollbar");
|
|
|
|
scrollbar->setCacheAllRenderedFrames(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void createTextBox()
|
|
|
|
{
|
|
|
|
if (!textBox) {
|
|
|
|
textBox = new Plasma::FrameSvg(q);
|
|
|
|
textBox->setImagePath("widgets/frame");
|
|
|
|
textBox->setElementPrefix("sunken");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Style *q;
|
2008-11-04 00:08:39 +01:00
|
|
|
Plasma::FrameSvg *scrollbar;
|
2009-01-17 19:00:33 +01:00
|
|
|
Plasma::FrameSvg *textBox;
|
2008-12-15 22:17:43 +01:00
|
|
|
static Plasma::Style::Ptr s_sharedStyle;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
2008-12-15 22:17:43 +01:00
|
|
|
Style::Ptr StylePrivate::s_sharedStyle(0);
|
|
|
|
|
|
|
|
Style::Ptr Style::sharedStyle()
|
|
|
|
{
|
|
|
|
if (!StylePrivate::s_sharedStyle) {
|
|
|
|
StylePrivate::s_sharedStyle = new Style();
|
|
|
|
}
|
|
|
|
|
|
|
|
return StylePrivate::s_sharedStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Style::doneWithSharedStyle()
|
|
|
|
{
|
|
|
|
if (StylePrivate::s_sharedStyle.isUnique()) {
|
|
|
|
StylePrivate::s_sharedStyle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Style::Style()
|
|
|
|
: QCommonStyle(),
|
2009-01-17 19:00:33 +01:00
|
|
|
d(new StylePrivate(this))
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Style::~Style()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Style::drawComplexControl(ComplexControl control,
|
|
|
|
const QStyleOptionComplex *option,
|
|
|
|
QPainter *painter,
|
|
|
|
const QWidget *widget) const
|
|
|
|
{
|
2009-02-24 23:42:06 +01:00
|
|
|
if (Theme::defaultTheme()->useNativeWidgetStyle()) {
|
|
|
|
qApp->style()->drawComplexControl(control, option, painter, widget);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
switch (control) {
|
|
|
|
case CC_ScrollBar: {
|
|
|
|
d->createScrollbar();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
painter->save();
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
const bool sunken = option->state & State_Sunken;
|
|
|
|
const QStyleOptionSlider *scrollOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
|
|
|
|
QString prefix;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
if (option->state & State_MouseOver) {
|
|
|
|
prefix= "mouseover-";
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
QRect subLine;
|
|
|
|
QRect addLine;
|
|
|
|
if (scrollOption && scrollOption->orientation == Qt::Horizontal) {
|
|
|
|
subLine = d->scrollbar->elementRect(prefix + "arrow-left").toRect();
|
|
|
|
addLine = d->scrollbar->elementRect(prefix + "arrow-right").toRect();
|
|
|
|
} else {
|
|
|
|
subLine = d->scrollbar->elementRect(prefix + "arrow-up").toRect();
|
|
|
|
addLine = d->scrollbar->elementRect(prefix + "arrow-down").toRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
subLine.moveCenter(subControlRect(control, option, SC_ScrollBarSubLine, widget).center());
|
|
|
|
addLine.moveCenter(subControlRect(control, option, SC_ScrollBarAddLine, widget).center());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
const QRect slider =
|
|
|
|
subControlRect(control, option, SC_ScrollBarSlider, widget).adjusted(1, 0, -1, 0);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-03-11 22:03:50 +01:00
|
|
|
if (scrollOption && scrollOption->orientation == Qt::Horizontal && d->scrollbar->hasElement("background-horizontal-center")) {
|
|
|
|
d->scrollbar->setElementPrefix("background-horizontal");
|
|
|
|
} else if (scrollOption && scrollOption->orientation == Qt::Vertical && d->scrollbar->hasElement("background-vertical-center")) {
|
|
|
|
d->scrollbar->setElementPrefix("background-vertical");
|
|
|
|
} else {
|
|
|
|
d->scrollbar->setElementPrefix("background");
|
|
|
|
}
|
2009-01-17 19:00:33 +01:00
|
|
|
d->scrollbar->resizeFrame(option->rect.size());
|
|
|
|
d->scrollbar->paintFrame(painter);
|
|
|
|
|
|
|
|
if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarSlider) {
|
|
|
|
d->scrollbar->setElementPrefix("sunken-slider");
|
|
|
|
} else {
|
|
|
|
d->scrollbar->setElementPrefix(prefix + "slider");
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
d->scrollbar->resizeFrame(slider.size());
|
|
|
|
d->scrollbar->paintFrame(painter, slider.topLeft());
|
|
|
|
|
|
|
|
if (scrollOption && scrollOption->orientation == Qt::Horizontal) {
|
|
|
|
if (sunken && scrollOption->activeSubControls & SC_ScrollBarAddLine) {
|
|
|
|
d->scrollbar->paint(painter, addLine, "sunken-arrow-right");
|
|
|
|
} else {
|
|
|
|
d->scrollbar->paint(painter, addLine, prefix + "arrow-right");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sunken && scrollOption->activeSubControls & SC_ScrollBarSubLine) {
|
|
|
|
d->scrollbar->paint(painter, subLine, "sunken-arrow-left");
|
|
|
|
} else {
|
|
|
|
d->scrollbar->paint(painter, subLine, prefix + "arrow-left");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarAddLine) {
|
|
|
|
d->scrollbar->paint(painter, addLine, "sunken-arrow-down");
|
|
|
|
} else {
|
|
|
|
d->scrollbar->paint(painter, addLine, prefix + "arrow-down");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sunken && scrollOption && scrollOption->activeSubControls & SC_ScrollBarSubLine) {
|
|
|
|
d->scrollbar->paint(painter, subLine, "sunken-arrow-up");
|
|
|
|
} else {
|
|
|
|
d->scrollbar->paint(painter, subLine, prefix + "arrow-up");
|
|
|
|
}
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
painter->restore();
|
|
|
|
break;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-01-17 19:00:33 +01:00
|
|
|
case CC_SpinBox: {
|
|
|
|
d->createTextBox();
|
|
|
|
|
|
|
|
d->textBox->resizeFrame(option->rect.size());
|
|
|
|
d->textBox->paintFrame(painter);
|
|
|
|
|
|
|
|
const QStyleOptionSpinBox *spinOpt = qstyleoption_cast<const QStyleOptionSpinBox *>(option);
|
|
|
|
bool upSunken = (spinOpt->activeSubControls & SC_SpinBoxUp) &&
|
|
|
|
(spinOpt->state & (State_Sunken | State_On));
|
|
|
|
bool downSunken = (spinOpt->activeSubControls & SC_SpinBoxDown) &&
|
|
|
|
(spinOpt->state & (State_Sunken | State_On));
|
|
|
|
|
|
|
|
const QSpinBox *spin = qobject_cast<const QSpinBox *>(widget);
|
|
|
|
PrimitiveElement pe;
|
|
|
|
if (spin->buttonSymbols() == QSpinBox::PlusMinus) {
|
|
|
|
pe = PE_IndicatorSpinPlus;
|
|
|
|
} else {
|
|
|
|
pe = PE_IndicatorArrowUp;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
QStyleOption upOpt;
|
|
|
|
upOpt = *option;
|
|
|
|
upOpt.rect = subControlRect(CC_SpinBox, option, SC_SpinBoxUp, widget);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
if (upSunken) {
|
|
|
|
upOpt.state = State_Sunken|State_Enabled;
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2009-01-17 19:00:33 +01:00
|
|
|
upOpt.state = State_Enabled;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
qApp->style()->drawPrimitive(pe, &upOpt, painter, widget);
|
|
|
|
|
|
|
|
if (spin->buttonSymbols() == QSpinBox::PlusMinus) {
|
|
|
|
pe = PE_IndicatorSpinMinus;
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2009-01-17 19:00:33 +01:00
|
|
|
pe = PE_IndicatorArrowDown;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-01-17 19:00:33 +01:00
|
|
|
|
|
|
|
QStyleOption downOpt;
|
|
|
|
downOpt= *option;
|
|
|
|
downOpt.rect = subControlRect(CC_SpinBox, option, SC_SpinBoxDown, widget);
|
|
|
|
|
|
|
|
if (downSunken) {
|
|
|
|
downOpt.state = State_Sunken|State_Enabled;
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2009-01-17 19:00:33 +01:00
|
|
|
downOpt.state = State_Enabled;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
qApp->style()->drawPrimitive(pe, &downOpt, painter, widget);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CC_ComboBox: {
|
|
|
|
const QComboBox *combo = qobject_cast<const QComboBox *>(widget);
|
|
|
|
if (!combo->isEditable()) {
|
|
|
|
qApp->style()->drawComplexControl(control, option, painter, widget);
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2009-01-17 19:00:33 +01:00
|
|
|
d->createTextBox();
|
|
|
|
d->textBox->resizeFrame(option->rect.size());
|
|
|
|
d->textBox->paintFrame(painter);
|
|
|
|
|
|
|
|
QStyleOption arrowOpt;
|
|
|
|
arrowOpt = *option;
|
|
|
|
arrowOpt.rect = subControlRect(CC_ComboBox, option, SC_ComboBoxArrow, widget);
|
|
|
|
qApp->style()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, painter, widget);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-01-17 19:00:33 +01:00
|
|
|
break;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-01-17 19:00:33 +01:00
|
|
|
default:
|
|
|
|
qApp->style()->drawComplexControl(control, option, painter, widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Style::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
|
|
|
|
{
|
2009-02-24 23:42:06 +01:00
|
|
|
if (Theme::defaultTheme()->useNativeWidgetStyle()) {
|
|
|
|
qApp->style()->drawPrimitive(element, option, painter, widget);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
switch (element) {
|
|
|
|
case PE_PanelLineEdit:
|
|
|
|
//comboboxes draws their own frame
|
|
|
|
if (qobject_cast<QComboBox *>(widget->parent())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
d->createTextBox();
|
|
|
|
|
|
|
|
d->textBox->resizeFrame(option->rect.size());
|
|
|
|
d->textBox->paintFrame(painter);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qApp->style()->drawPrimitive(element, option, painter, widget);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-05-01 19:08:31 +02:00
|
|
|
QRect Style::subControlRect(ComplexControl control, const QStyleOptionComplex *option,
|
|
|
|
SubControl subControl, const QWidget *widget) const
|
|
|
|
{
|
2009-05-01 21:19:06 +02:00
|
|
|
QRect rect(QCommonStyle::subControlRect(control, option, subControl, widget));
|
2009-05-01 19:08:31 +02:00
|
|
|
switch (control) {
|
|
|
|
case CC_Slider: {
|
|
|
|
const QStyleOptionSlider *sliderOpt = qstyleoption_cast<const QStyleOptionSlider *>(option);
|
|
|
|
if (sliderOpt) {
|
|
|
|
if (sliderOpt->orientation == Qt::Horizontal) {
|
|
|
|
rect.moveCenter(QPoint(rect.center().x(), option->rect.center().y()));
|
|
|
|
} else {
|
|
|
|
rect.moveCenter(QPoint(option->rect.center().x(), rect.center().y()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rect;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-05 22:04:33 +01:00
|
|
|
int Style::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
|
|
|
|
{
|
2009-02-24 23:42:06 +01:00
|
|
|
if (Theme::defaultTheme()->useNativeWidgetStyle()) {
|
|
|
|
return qApp->style()->pixelMetric(metric, option, widget);
|
|
|
|
}
|
|
|
|
|
2008-12-05 22:04:33 +01:00
|
|
|
switch (metric) {
|
|
|
|
case PM_ScrollBarExtent: {
|
2009-01-17 19:00:33 +01:00
|
|
|
d->createScrollbar();
|
2008-12-05 22:04:33 +01:00
|
|
|
const QStyleOptionSlider *scrollOption = qstyleoption_cast<const QStyleOptionSlider *>(option);
|
|
|
|
if (scrollOption && scrollOption->orientation == Qt::Vertical) {
|
|
|
|
return d->scrollbar->elementSize("arrow-down").width() + 2;
|
|
|
|
} else {
|
|
|
|
return d->scrollbar->elementSize("arrow-left").height() + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2009-05-01 19:08:31 +02:00
|
|
|
return qApp->style()->pixelMetric(metric, option, widget);
|
2008-12-05 22:04:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2008-11-15 08:08:08 +01:00
|
|
|
#include "style_p.moc"
|
|
|
|
|
|
|
|
|