From 76d6071e46b76cab04a0733bc0b4ec10de07e673 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Wed, 13 Aug 2008 14:47:27 +0000 Subject: [PATCH] svg themed scrollbars: scrollbar widget in libplasma private qstyle that draws scrollbars with svg folderview uses the scrollbar widget textview scrollbars themed svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=846473 --- CMakeLists.txt | 4 ++ private/style.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++ private/style.h | 51 ++++++++++++++++++++ widgets/scrollbar.cpp | 94 ++++++++++++++++++++++++++++++++++++ widgets/scrollbar.h | 48 +++++++++++++++++++ widgets/tabbar.cpp | 35 ++++++++++++++ widgets/tabbar.h | 5 ++ widgets/textedit.cpp | 5 ++ 8 files changed, 351 insertions(+) create mode 100644 private/style.cpp create mode 100644 private/style.h create mode 100644 widgets/scrollbar.cpp create mode 100644 widgets/scrollbar.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a49d8fec..f6d276bd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ set(plasma_LIB_SRCS private/nativetabbar.cpp private/packages.cpp private/paneltoolbox.cpp + private/style.cpp private/toolbox.cpp private/tooltip.cpp querymatch.cpp @@ -76,6 +77,7 @@ set(plasma_LIB_SRCS widgets/meter.cpp widgets/pushbutton.cpp widgets/radiobutton.cpp + widgets/scrollbar.cpp widgets/signalplotter.cpp widgets/slider.cpp widgets/tabbar.cpp @@ -181,6 +183,7 @@ install(FILES widgets/meter.h widgets/pushbutton.h widgets/radiobutton.h + widgets/scrollbar.h widgets/signalplotter.h widgets/slider.h widgets/tabbar.h @@ -236,6 +239,7 @@ includes/RunnerContext includes/RunnerManager includes/RunnerScript includes/ScriptEngine +includes/ScrollBar includes/Service includes/ServiceJob includes/SignalPlotter diff --git a/private/style.cpp b/private/style.cpp new file mode 100644 index 000000000..9e19dffdc --- /dev/null +++ b/private/style.cpp @@ -0,0 +1,109 @@ +/* + * Copyright © 2008 Fredrik Höglund + * Copyright © 2008 Marco Martin + * + * 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. + */ + +#include "style.h" + +#include +#include + +#include + +#include + +namespace Plasma { + +class StylePrivate { +public: + StylePrivate() + : scrollbar(0) + { + } + + ~StylePrivate() + { + } + + Plasma::PanelSvg *scrollbar; +}; + +Style::Style() + : QCommonStyle(), + d(new StylePrivate) +{ + d->scrollbar = new Plasma::PanelSvg(this); + d->scrollbar->setImagePath("widgets/scrollbar"); + d->scrollbar->setCacheAllRenderedPanels(true); +} + +Style::~Style() +{ +} + +void Style::drawComplexControl(ComplexControl control, + const QStyleOptionComplex *option, + QPainter *painter, + const QWidget *widget) const +{ + if (control != CC_ScrollBar) { + QCommonStyle::drawComplexControl(control, option, painter, widget); + return; + } + + painter->save(); + painter->setRenderHint(QPainter::Antialiasing); + + const QStyleOptionSlider *scrollOption = qstyleoption_cast(option); + + QRect subLine; + QRect addLine; + if (scrollOption && scrollOption->orientation == Qt::Horizontal) { + subLine = d->scrollbar->elementRect("arrow-left").toRect(); + addLine = d->scrollbar->elementRect("arrow-right").toRect(); + } else { + subLine = d->scrollbar->elementRect("arrow-up").toRect(); + addLine = d->scrollbar->elementRect("arrow-down").toRect(); + } + + subLine.moveCenter(subControlRect(control, option, SC_ScrollBarSubLine, widget).center()); + addLine.moveCenter(subControlRect(control, option, SC_ScrollBarAddLine, widget).center()); + + const QRect slider = subControlRect(control, option, SC_ScrollBarSlider, widget).adjusted(1, 0, -1, 0); + + d->scrollbar->setElementPrefix("background"); + d->scrollbar->resizePanel(option->rect.size()); + d->scrollbar->paintPanel(painter); + + d->scrollbar->setElementPrefix("slider"); + d->scrollbar->resizePanel(slider.size()); + d->scrollbar->paintPanel(painter, slider.topLeft()); + + if (scrollOption && scrollOption->orientation == Qt::Horizontal) { + d->scrollbar->paint(painter, addLine.topLeft(), "arrow-left"); + d->scrollbar->paint(painter, subLine.topLeft(), "arrow-right"); + } else { + d->scrollbar->paint(painter, addLine.topLeft(), "arrow-down"); + d->scrollbar->paint(painter, subLine.topLeft(), "arrow-up"); + } + + painter->restore(); +} + +}; + diff --git a/private/style.h b/private/style.h new file mode 100644 index 000000000..61743e119 --- /dev/null +++ b/private/style.h @@ -0,0 +1,51 @@ +/* + * Copyright © 2008 Fredrik Höglund + * Copyright © 2008 Marco Martin + * + * 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. + */ + + +#ifndef PLASMA_STYLE_H +#define PLASMA_STYLE_H + +#include + +namespace Plasma +{ + +class StylePrivate; + +class Style : public QCommonStyle +{ + Q_OBJECT + +public: + explicit Style(); + ~Style(); + +protected: + void drawComplexControl(ComplexControl control, + const QStyleOptionComplex *option, + QPainter *painter, + const QWidget *widget) const; +private: + StylePrivate *d; +}; + +} // namespace Plasma + +#endif // multiple inclusion guard diff --git a/widgets/scrollbar.cpp b/widgets/scrollbar.cpp new file mode 100644 index 000000000..a867e3b8e --- /dev/null +++ b/widgets/scrollbar.cpp @@ -0,0 +1,94 @@ +/* + * Copyright © 2008 Fredrik Höglund + * Copyright © 2008 Marco Martin + * + * 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. + */ + +#include "scrollbar.h" + +#include + +namespace Plasma +{ + +ScrollBar::ScrollBar(Qt::Orientation orientation, QGraphicsWidget *parent) + : QGraphicsProxyWidget(parent) +{ + QScrollBar *scrollbar = new QScrollBar(orientation); + scrollbar->setAttribute(Qt::WA_NoSystemBackground); + setWidget(scrollbar); + Plasma::Style *style = new Plasma::Style(); + scrollbar->setStyle(style); +} + +ScrollBar::~ScrollBar() +{ +} + +void ScrollBar::setRange(int min, int max) +{ + static_cast(widget())->setRange(min, max); +} + +void ScrollBar::setSingleStep(int val) +{ + static_cast(widget())->setSingleStep(val); +} + +void ScrollBar::setPageStep(int val) +{ + static_cast(widget())->setPageStep(val); +} + +void ScrollBar::setValue(int val) +{ + static_cast(widget())->setValue(val); +} + +int ScrollBar::value() const +{ + return static_cast(widget())->value(); +} + +int ScrollBar::minimum() const +{ + return static_cast(widget())->minimum(); +} + +int ScrollBar::maximum() const +{ + return static_cast(widget())->maximum(); +} + +void ScrollBar::setStyleSheet(const QString &stylesheet) +{ + widget()->setStyleSheet(stylesheet); +} + +QString ScrollBar::styleSheet() +{ + return widget()->styleSheet(); +} + +QScrollBar *ScrollBar::nativeWidget() const +{ + return static_cast(widget()); +} + +}; + +#include diff --git a/widgets/scrollbar.h b/widgets/scrollbar.h new file mode 100644 index 000000000..39c6f8ad5 --- /dev/null +++ b/widgets/scrollbar.h @@ -0,0 +1,48 @@ +/* + * Copyright © 2008 Fredrik Höglund + * Copyright © 2008 Marco Martin + * + * 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. + */ + +#include +#include + +#include + +namespace Plasma +{ + +// Wraps a QScrollBar in a QGraphicsProxyWidget +class PLASMA_EXPORT ScrollBar : public QGraphicsProxyWidget +{ +public: + explicit ScrollBar(Qt::Orientation orientation, QGraphicsWidget *parent); + ~ScrollBar(); + + void setRange(int min, int max); + void setSingleStep(int val); + void setPageStep(int val); + void setValue(int val); + int value() const; + int minimum() const; + int maximum() const; + void setStyleSheet(const QString &stylesheet); + QString styleSheet(); + QScrollBar *nativeWidget() const; +}; + +}; diff --git a/widgets/tabbar.cpp b/widgets/tabbar.cpp index 5d9203ab8..2e79648b0 100644 --- a/widgets/tabbar.cpp +++ b/widgets/tabbar.cpp @@ -29,6 +29,8 @@ #include #include +#include +#include #include "private/nativetabbar_p.h" @@ -53,11 +55,13 @@ public: { } + void syncBorders(); void slidingCompleted(QGraphicsItem *item); void shapeChanged(const QTabBar::Shape shape); TabBar *q; NativeTabBar *tabBar; + PanelSvg *background; QList pages; QGraphicsLinearLayout *mainLayout; QGraphicsLinearLayout *tabBarLayout; @@ -69,6 +73,15 @@ public: int newPageAnimId; }; +void TabBarPrivate::syncBorders() +{ + //set margins from the normal element + qreal left, top, right, bottom; + + background->getMargins(left, top, right, bottom); + + q->setContentsMargins(left, top, right, bottom); +} void TabBarPrivate::slidingCompleted(QGraphicsItem *item) { @@ -132,6 +145,13 @@ TabBar::TabBar(QGraphicsWidget *parent) d->tabBarLayout->addItem(tabProxy); d->tabBarLayout->addStretch(); + //background painting stuff + d->background = new Plasma::PanelSvg(this); + d->background->setImagePath("widgets/frame"); + d->background->setElementPrefix("sunken"); + + connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders())); + connect(d->tabBar, SIGNAL(currentChanged(int)), this, SLOT(setCurrentIndex(int))); connect(d->tabBar, SIGNAL(shapeChanged(QTabBar::Shape)), this, SLOT(shapeChanged(QTabBar::Shape))); connect(Plasma::Animator::self(), SIGNAL(movementFinished(QGraphicsItem*)), this, SLOT(slidingCompleted(QGraphicsItem*))); @@ -316,6 +336,21 @@ QTabBar *TabBar::nativeWidget() const return d->tabBar; } +void TabBar::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget *widget) +{ + Q_UNUSED(option) + Q_UNUSED(widget) + + d->background->paintPanel(painter, QPoint(contentsRect().left(), contentsRect().top() + nativeWidget()->height()/1.5)); +} + +void TabBar::resizeEvent(QGraphicsSceneResizeEvent *event) +{ + d->background->resizePanel(event->newSize() - QSize(0, nativeWidget()->height()/2)); +} + void TabBar::wheelEvent(QGraphicsSceneWheelEvent * event) { //FIXME: probably this would make more sense in NativeTabBar, but it works only here diff --git a/widgets/tabbar.h b/widgets/tabbar.h index 5462b844f..673632b5b 100644 --- a/widgets/tabbar.h +++ b/widgets/tabbar.h @@ -187,11 +187,16 @@ Q_SIGNALS: void currentChanged(int index); protected: + void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget *widget); + void resizeEvent(QGraphicsSceneResizeEvent *event); void wheelEvent(QGraphicsSceneWheelEvent *event); private: TabBarPrivate * const d; + Q_PRIVATE_SLOT(d, void syncBorders()) Q_PRIVATE_SLOT(d, void slidingCompleted(QGraphicsItem *item)) Q_PRIVATE_SLOT(d, void shapeChanged(const QTabBar::Shape shape)) }; diff --git a/widgets/textedit.cpp b/widgets/textedit.cpp index 45d17bd61..417eeb8ed 100644 --- a/widgets/textedit.cpp +++ b/widgets/textedit.cpp @@ -21,11 +21,13 @@ #include #include +#include #include #include "theme.h" #include "svg.h" +#include "private/style.h" namespace Plasma { @@ -50,6 +52,9 @@ TextEdit::TextEdit(QGraphicsWidget *parent) connect(native, SIGNAL(textChanged()), this, SIGNAL(textChanged())); setWidget(native); native->setAttribute(Qt::WA_NoSystemBackground); + Plasma::Style *style = new Plasma::Style(); + native->verticalScrollBar()->setStyle(style); + native->horizontalScrollBar()->setStyle(style); } TextEdit::~TextEdit()