[PATCH] KStyle changes V2

Rafael Fernández López ereslibre at gmail.com
Wed May 9 20:44:01 BST 2007


Hi all,

I would like to know what you think about the attached patch. It will let us
do something like what you see in these screenshots:

http://media.ereslibre.es/2007/04/categorization5.png<http://media.ereslibre.es>
http://media.ereslibre.es/2007/04/klistview.png
http://media.ereslibre.es/2007/05/kpluginselector3.png

If you have suggestions to improve the code, please let me know...


Bye and thanks,
Rafael Fernández López.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070509/afb2a261/attachment.htm>
-------------- next part --------------
Index: kstyle.cpp
===================================================================
--- kstyle.cpp	(revisi��n: 662969)
+++ kstyle.cpp	(copia de trabajo)
@@ -199,6 +199,10 @@ KStyle::KStyle() : clickedLabel(0)
     setWidgetLayoutProp(WT_Window, Window::ButtonWidth, 16);
     setWidgetLayoutProp(WT_Window, Window::ButtonSpace, 2);
     setWidgetLayoutProp(WT_Window, Window::ButtonToTextSpace, 3);
+
+    setWidgetLayoutProp(WT_Category, Category::ContentsMargin, 5);
+    setWidgetLayoutProp(WT_Category, Category::CategoryDataSeparator, 5);
+    setWidgetLayoutProp(WT_Category, Category::CategorySeparatorHeight, 2);
 }
 
 KStyle::~KStyle()
@@ -435,6 +439,35 @@ void KStyle::drawKStylePrimitive(WidgetT
             break;
         }
 
+        case WT_Category:
+        {
+            if (KStyle::TextOption *textOpt = extractOption<KStyle::TextOption*>(kOpt))
+            {
+                QFont painterFont = p->font();
+                painterFont.setWeight(QFont::Bold);
+                QFontMetrics metrics(painterFont);
+                p->setFont(painterFont);
+
+                QPainterPath path;
+                path.addRect(opt->rect.left(),
+                             opt->rect.top() + metrics.height() + widgetLayoutProp(WT_Category, Category::CategoryDataSeparator),
+                             opt->rect.width(),
+                             2);
+
+                QLinearGradient gradient(opt->rect.topLeft(),
+                                        opt->rect.bottomRight());
+                gradient.setColorAt(0, pal.color(QPalette::Button));
+                gradient.setColorAt(1, pal.color(QPalette::Light));
+
+                p->setBrush(gradient);
+                p->fillPath(path, gradient);
+
+                p->drawText(opt->rect, textOpt->hAlign, textOpt->text);
+            }
+
+            break;
+        }
+
         default:
             break;
     }
@@ -823,7 +856,7 @@ void KStyle::drawPrimitive(PrimitiveElem
     QCommonStyle::drawPrimitive(elem, option, painter, widget);
 }
 
-void KStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* p, const QWidget* widget) const
+void KStyle::drawControl(QStyle::ControlElement element, const QStyleOption* option, QPainter* p, const QWidget* widget) const
 {
     //Extract the stuff we need out of the option
     State flags = option->state;
@@ -1913,7 +1946,6 @@ void KStyle::drawControl(ControlElement 
     QCommonStyle::drawControl(element, option, p, widget);
 }
 
-
 int KStyle::styleHint (StyleHint hint, const QStyleOption* option, const QWidget* widget, QStyleHintReturn* returnData) const
 {
     switch (hint)
@@ -3328,7 +3360,7 @@ QStyle::SubControl KStyle::hitTestComple
 }
 
 
-QSize KStyle::sizeFromContents(ContentsType type, const QStyleOption* option, const QSize& contentsSize, const QWidget* widget) const
+QSize KStyle::sizeFromContents(QStyle::ContentsType type, const QStyleOption* option, const QSize& contentsSize, const QWidget* widget) const
 {
     switch (type)
     {
@@ -3548,11 +3580,12 @@ QSize KStyle::sizeFromContents(ContentsT
                 return expandDim(QSize(w, h), WT_Header, Header::ContentsMargin, option, widget);
             }
         }
+
         default:
             break;
     }
 
-    return QCommonStyle::sizeFromContents(type, option, contentsSize, widget);
+    return QCommonStyle::sizeFromContents((QStyle::ContentsType) type, option, contentsSize, widget);
 }
 
 bool KStyle::eventFilter(QObject *obj, QEvent *ev)
@@ -3613,5 +3646,50 @@ bool KStyle::eventFilter(QObject *obj, Q
     return false;
 }
 
+void KStyle::drawControl (KStyle::ControlElement elem, const QStyleOption* option, QPainter* p, const QWidget* widget) const
+{
+    //Extract the stuff we need out of the option
+    State flags = option->state;
+    QRect      r     = option->rect;
+    QPalette   pal   = option->palette;
+
+    switch (elem)
+    {
+        case CE_Category:
+        {
+            if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton*>(option))
+            {
+                TextOption textOpt;
+                textOpt.hAlign = Qt::AlignLeft;
+                textOpt.text = button->text;
+
+                drawKStylePrimitive(WT_Category, Category::Title, option, r, pal, flags, p, widget, &textOpt);
+            }
+            return;
+        }
+
+        default:
+            break;
+    }
+}
+
+QSize KStyle::sizeFromContents (KStyle::ContentsType type, const QStyleOption* opt,
+                                const QSize& contentsSize, const QWidget* w) const
+{
+    switch (type)
+    {
+        case CT_Category:
+        {
+            QSize size(contentsSize);
+
+            size.setHeight(size.height() + widgetLayoutProp(WT_Category, Category::CategoryDataSeparator) + widgetLayoutProp(WT_Category, Category::CategorySeparatorHeight));
+
+            return expandDim(size, WT_Category, Category::ContentsMargin, opt, w);
+        }
+        default:
+            break;
+    }
+}
+
 
 // kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;
Index: kstyle.h
===================================================================
--- kstyle.h	(revisi��n: 662969)
+++ kstyle.h	(copia de trabajo)
@@ -88,6 +88,15 @@ public:
     KStyle();
     ~KStyle();
 
+
+    enum ControlElement {
+        CE_Category = 0xf0000001
+    };
+
+    enum ContentsType {
+        CT_Category = 0xf0000001
+    };
+
 protected:
     /** @name Helper Methods
     * These are methods helping with QRect handling, for example.
@@ -396,10 +405,10 @@ protected:
         WT_ToolButton,      ///< @sa ToolButton
         WT_ToolBoxTab,      ///< @sa ToolBoxTab
         WT_Window,          ///< @sa Window
+        WT_Category,        ///< @sa Category
         WT_Limit = 0xFFFF ///< For enum extensibility
     };
 
-                
     /**
      These constants describe how to access various fields of a margin property.
      For example, to set an additional top margin of 2 pixels, use
@@ -1422,6 +1431,32 @@ protected:
             ButtonHelp      ///< context help button, passes TitleButtonOption
         };
     };
+
+    /**
+     * @brief Describes categories in KListView.
+     *
+     * @sa WT_Category
+     */
+    struct Category
+    {
+        /**
+         * @sa setWidgetLayoutProp()
+         */
+        enum LayoutProps
+        {
+            ContentsMargin,          ///< space between the text and the margin
+            CategoryDataSeparator,   ///< space between the category and the data
+            CategorySeparatorHeight  ///< the category separator height
+        };
+
+        /**
+         * @sa drawKStylePrimitive()
+         */
+        enum Primitive
+        {
+            Title,       ///< category title
+        };
+    };
 //@}
 
     ///Interface for the style to configure various metrics that KStyle has customizable.
@@ -1533,17 +1568,17 @@ public:
  * the implementation from QCommonStyle.
  */
 //@{
-    void drawControl      (ControlElement   elem, const QStyleOption* opt, QPainter* p, const QWidget* w) const;
+    void drawControl (QStyle::ControlElement elem, const QStyleOption* opt, QPainter* p, const QWidget* w) const;
     void drawPrimitive    (PrimitiveElement elem, const QStyleOption* opt, QPainter* p, const QWidget* w) const;
     int  pixelMetric      (PixelMetric    metric, const QStyleOption* opt = 0, const QWidget* w = 0) const;
     QRect subElementRect  (SubElement    subRect, const QStyleOption* opt, const QWidget* w) const;
-    QSize sizeFromContents(ContentsType     type, const QStyleOption* opt,
+    QSize sizeFromContents (QStyle::ContentsType type, const QStyleOption* opt,
                                                 const QSize& contentsSize, const QWidget* w) const;
     int   styleHint       (StyleHint        hint, const QStyleOption* opt, const QWidget* w,
                                                                QStyleHintReturn* returnData) const;
     QRect subControlRect (ComplexControl control, const QStyleOptionComplex* opt,
                                                     SubControl subControl, const QWidget* w) const;
-    SubControl hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex* opt,
+    SubControl hitTestComplexControl (ComplexControl cc, const QStyleOptionComplex* opt,
                                              const QPoint& pt, const QWidget* w) const;
     void       drawComplexControl   (ComplexControl cc, const QStyleOptionComplex* opt,
                                              QPainter *p,      const QWidget* w) const;
@@ -1569,6 +1604,15 @@ public:
                                    const QStyleOption *opt) const;
     bool eventFilter(QObject *, QEvent *);
 //@}
+
+public:
+/** @name Adapted Methods
+ * These are methods similar to the QStyle's ones, but extended to be able to
+ * accept more elements.
+ */
+    virtual void drawControl (KStyle::ControlElement elem, const QStyleOption* opt, QPainter* p, const QWidget* w) const;
+    QSize sizeFromContents (KStyle::ContentsType type, const QStyleOption* opt,
+                            const QSize& contentsSize, const QWidget* w) const;
 };
 
 template<typename T>


More information about the kde-core-devel mailing list