Simon Hausmann <hausmann@webkit.org>
authorhausmann@webkit.org <hausmann@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 21 Apr 2008 13:47:43 +0000 (13:47 +0000)
committerhausmann@webkit.org <hausmann@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 21 Apr 2008 13:47:43 +0000 (13:47 +0000)
Fix painting of text fields in the CleanLooks style.

The CleanLooks style calls painter->drawRect() and expects a painter
that has no brush set. This is a fairly common assumption and therefore
we now always set the brush to NoBrush before calling a style. This
is done by centralizing the painter setup code into StylePainter
and restore the old brush in the StylePainter destructor.

In addition for textfields it is not necessary anymore to draw
PE_FrameLineEdit as we set the lineWidth in the style option correctly
and it's the style responsibility (QCommonStyle usually) to draw
PE_FrameLineEdit.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@32312 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/platform/qt/RenderThemeQt.cpp
WebCore/platform/qt/RenderThemeQt.h

index 98461c3..8e8dd04 100644 (file)
@@ -1,3 +1,32 @@
+2008-04-21  Simon Hausmann  <hausmann@webkit.org>
+
+        Reviewed by Holger.
+
+        Fix painting of text fields in the CleanLooks style.
+
+        The CleanLooks style calls painter->drawRect() and expects a painter
+        that has no brush set. This is a fairly common assumption and therefore
+        we now always set the brush to NoBrush before calling a style. This
+        is done by centralizing the painter setup code into StylePainter
+        and restore the old brush in the StylePainter destructor.
+
+        In addition for textfields it is not necessary anymore to draw
+        PE_FrameLineEdit as we set the lineWidth in the style option correctly
+        and it's the style responsibility (QCommonStyle usually) to draw
+        PE_FrameLineEdit.
+
+        * platform/qt/RenderThemeQt.cpp:
+        (WebCore::RenderThemeQt::paintButton):
+        (WebCore::RenderThemeQt::paintTextField):
+        (WebCore::RenderThemeQt::paintMenuList):
+        (WebCore::StylePainter::StylePainter):
+        (WebCore::StylePainter::~StylePainter):
+        * platform/qt/RenderThemeQt.h:
+        (WebCore::StylePainter::isValid):
+        (WebCore::StylePainter::drawPrimitive):
+        (WebCore::StylePainter::drawControl):
+        (WebCore::StylePainter::drawComplexControl):
+
 2008-04-21  Olivier Goffart  <ogoffart@trolltech.com>
 
         Reviewed by Simon.
index 028ffba..9c984fe 100644 (file)
@@ -34,7 +34,6 @@
 #include <QApplication>
 #include <QColor>
 #include <QDebug>
-#include <QStyle>
 #include <QWidget>
 #include <QPainter>
 #include <QStyleOptionButton>
@@ -218,27 +217,24 @@ void RenderThemeQt::adjustButtonStyle(CSSStyleSelector* selector, RenderStyle* s
 
 bool RenderThemeQt::paintButton(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
 {
-    QStyle* style = 0;
-    QPainter* painter = 0;
-    QWidget* widget = 0;
-
-    if (!getStylePainterAndWidgetFromPaintInfo(i, style, painter, widget))
+    StylePainter p(i);
+    if (!p.isValid())
         return true;
 
     QStyleOptionButton option;
-    if (widget)
-        option.initFrom(widget);
+    if (p.widget)
+        option.initFrom(p.widget);
     option.rect = r;
 
     // Get the correct theme data for a button
     EAppearance appearance = applyTheme(option, o);
 
     if(appearance == PushButtonAppearance || appearance == ButtonAppearance)
-        style->drawControl(QStyle::CE_PushButton, &option, painter);
+        p.drawControl(QStyle::CE_PushButton, option);
     else if(appearance == RadioAppearance)
-        style->drawPrimitive(QStyle::PE_IndicatorRadioButton, &option, painter, widget);
+        p.drawPrimitive(QStyle::PE_IndicatorRadioButton, option);
     else if(appearance == CheckboxAppearance)
-        style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, widget);
+        p.drawPrimitive(QStyle::PE_IndicatorCheckBox, option);
 
     return false;
 }
@@ -250,18 +246,15 @@ void RenderThemeQt::setButtonSize(RenderStyle* style) const
 
 bool RenderThemeQt::paintTextField(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
 {
-    QStyle* style = 0;
-    QPainter* painter = 0;
-    QWidget* widget = 0;
-
-    if (!getStylePainterAndWidgetFromPaintInfo(i, style, painter, widget))
+    StylePainter p(i);
+    if (!p.isValid())
         return true;
 
     QStyleOptionFrameV2 panel;
-    if (widget)
-        panel.initFrom(widget);
+    if (p.widget)
+        panel.initFrom(p.widget);
     panel.rect = r;
-    panel.lineWidth = style->pixelMetric(QStyle::PM_DefaultFrameWidth, &panel, widget);
+    panel.lineWidth = p.style->pixelMetric(QStyle::PM_DefaultFrameWidth, &panel, p.widget);
     panel.state |= QStyle::State_Sunken;
     panel.features = QStyleOptionFrameV2::None;
 
@@ -270,9 +263,8 @@ bool RenderThemeQt::paintTextField(RenderObject* o, const RenderObject::PaintInf
     Q_ASSERT(appearance == TextFieldAppearance || appearance == SearchFieldAppearance);
 
     // Now paint the text field.
-    style->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, painter, widget);
-    style->drawPrimitive(QStyle::PE_FrameLineEdit, &panel, painter, widget);
-      
+    p.drawPrimitive(QStyle::PE_PanelLineEdit, panel);
+
     return false;
 }
 
@@ -303,26 +295,23 @@ void RenderThemeQt::adjustMenuListStyle(CSSStyleSelector*, RenderStyle* style, E
 
 bool RenderThemeQt::paintMenuList(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
 {
-    QStyle* style = 0;
-    QPainter* painter = 0;
-    QWidget* widget = 0;
-
-    if (!getStylePainterAndWidgetFromPaintInfo(i, style, painter, widget))
+    StylePainter p(i);
+    if (!p.isValid())
         return true;
 
     QStyleOptionComboBox opt;
-    if (widget)
-        opt.initFrom(widget);
+    if (p.widget)
+        opt.initFrom(p.widget);
     EAppearance appearance = applyTheme(opt, o);
     const QPoint topLeft = r.topLeft();
-    painter->translate(topLeft);
+    p.painter->translate(topLeft);
     opt.rect.moveTo(QPoint(0,0));
     opt.rect.setSize(r.size());
 
     opt.frame = false;
 
-    style->drawComplexControl(QStyle::CC_ComboBox, &opt, painter, widget);
-    painter->translate(-topLeft);
+    p.drawComplexControl(QStyle::CC_ComboBox, opt);
+    p.painter->translate(-topLeft);
     return false;
 }
 
@@ -426,21 +415,6 @@ bool RenderThemeQt::supportsFocus(EAppearance appearance) const
     }
 }
 
-bool RenderThemeQt::getStylePainterAndWidgetFromPaintInfo(const RenderObject::PaintInfo& i, QStyle*& style,
-                                                          QPainter*& painter, QWidget*& widget) const
-{
-    painter = (i.context ? static_cast<QPainter*>(i.context->platformContext()) : 0);
-    widget = 0;
-    QPaintDevice* dev = 0;
-    if (painter)
-        dev = painter->device();
-    if (dev && dev->devType() == QInternal::Widget)
-        widget = static_cast<QWidget*>(dev);
-    style = (widget ? widget->style() : QApplication::style());
-
-    return (painter && style);
-}
-
 EAppearance RenderThemeQt::applyTheme(QStyleOption& option, RenderObject* o) const
 {
     // Default bits: no focus, no mouse over
@@ -589,6 +563,31 @@ void RenderThemeQt::setPrimitiveSize(RenderStyle* style) const
     setSizeFromFont(style);
 }
 
+StylePainter::StylePainter(const RenderObject::PaintInfo& paintInfo)
+{
+    painter = (paintInfo.context ? static_cast<QPainter*>(paintInfo.context->platformContext()) : 0);
+    widget = 0;
+    QPaintDevice* dev = 0;
+    if (painter)
+        dev = painter->device();
+    if (dev && dev->devType() == QInternal::Widget)
+        widget = static_cast<QWidget*>(dev);
+    style = (widget ? widget->style() : QApplication::style());
+
+    if (painter) {
+        // the styles often assume being called with a pristine painter where no brush is set,
+        // so reset it manually
+        oldBrush = painter->brush();
+        painter->setBrush(Qt::NoBrush);
+    }
+}
+
+StylePainter::~StylePainter()
+{
+    if (painter)
+        painter->setBrush(oldBrush);
+}
+
 }
 
 // vim: ts=4 sw=4 et
index dc9ce0b..9e41d68 100644 (file)
 
 #include "RenderTheme.h"
 
+#include <QStyle>
+
 QT_BEGIN_NAMESPACE
-class QStyle;
 class QPainter;
 class QWidget;
-class QStyleOption;
 QT_END_NAMESPACE
 
 namespace WebCore {
@@ -105,7 +105,6 @@ protected:
 private:
     bool supportsFocus(EAppearance) const;
 
-    bool getStylePainterAndWidgetFromPaintInfo(const RenderObject::PaintInfo&, QStyle*&, QPainter*&, QWidget*&) const;
     EAppearance applyTheme(QStyleOption&, RenderObject*) const;
 
     void setSizeFromFont(RenderStyle*) const;
@@ -115,6 +114,31 @@ private:
     void setPrimitiveSize(RenderStyle*) const;
 };
 
+class StylePainter
+{
+public:
+    explicit StylePainter(const RenderObject::PaintInfo& paintInfo);
+    ~StylePainter();
+
+    bool isValid() const { return painter && style; }
+
+    QPainter* painter;
+    QWidget* widget;
+    QStyle* style;
+
+    void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption& opt)
+    { style->drawPrimitive(pe, &opt, painter, widget); }
+    void drawControl(QStyle::ControlElement ce, const QStyleOption& opt)
+    { style->drawControl(ce, &opt, painter, widget); }
+    void drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex& opt)
+    { style->drawComplexControl(cc, &opt, painter, widget); }
+
+private:
+    QBrush oldBrush;
+
+    Q_DISABLE_COPY(StylePainter)
+};
+
 }
 
 #endif