From 3ea42ee898fb239553544e5c92ff2ddbf7150def Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Date: Tue, 14 Jul 2015 10:53:58 +0200
Subject: [PATCH] Add debug proxy style to gallery example

Change-Id: I8e88e7500b1bf1998d12f05f7f26fdbf803e21ea
Task-number: QTBUG-47084
---
 .../quick/controls/gallery/debuggingproxystyle.h   | 132 +++++++++++++++++++++
 examples/quick/controls/gallery/gallery.pro        |   4 +-
 examples/quick/controls/gallery/main.cpp           |   2 +
 3 files changed, 137 insertions(+), 1 deletion(-)
 create mode 100644 examples/quick/controls/gallery/debuggingproxystyle.h

diff --git a/examples/quick/controls/gallery/debuggingproxystyle.h b/examples/quick/controls/gallery/debuggingproxystyle.h
new file mode 100644
index 0000000..386a78a
--- /dev/null
+++ b/examples/quick/controls/gallery/debuggingproxystyle.h
@@ -0,0 +1,132 @@
+#ifndef DEBUGGINGPROXYSTYLE_H
+#define DEBUGGINGPROXYSTYLE_H
+
+#include <QProxyStyle>
+#include <QStyleOptionComplex>
+#include <QDebug>
+#include <QApplication>
+
+class DebugStyle : public QProxyStyle {
+    Q_OBJECT
+public:
+    DebugStyle(QStyle *style) : QProxyStyle(style)
+    {
+#if QT_VERSION >= 0x050000
+          const qreal dpr = qApp->devicePixelRatio();
+#else
+          const qreal dpr = 1;
+#endif
+          qDebug() << __FUNCTION__
+                   << QT_VERSION_STR
+                   << QGuiApplication::platformName()
+                   << style->objectName() << "devicePixelRatio=" << dpr;
+    }
+
+    void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const
+    {
+        qDebug() << __FUNCTION__ << "element="<< element
+                 << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget);
+        QProxyStyle::drawPrimitive( element, option, painter, widget);
+    }
+
+    void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const
+    {
+        qDebug() << __FUNCTION__ << "element="<< element
+                 << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget);
+        QProxyStyle::drawControl(element, option, painter, widget);
+    }
+    void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = 0) const
+    {
+        qDebug() << __FUNCTION__ << "control="<< control
+                 << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget);
+        QProxyStyle::drawComplexControl(control, option, painter, widget);
+    }
+    void drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const
+    {
+        qDebug() << __FUNCTION__ << rect << "alignment=" << alignment << pixmap;
+        QProxyStyle::drawItemPixmap(painter, rect, alignment, pixmap);
+    }
+
+    QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const
+    {
+        QSize r= QProxyStyle::sizeFromContents(type, option, size, widget);
+        qDebug() << __FUNCTION__ << size << "type=" << type
+                 << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget) << "returns" << r;
+        return r;
+    }
+
+    QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
+    {
+        QRect r = QProxyStyle::subElementRect(element, option, widget);
+        qDebug() << __FUNCTION__ << "element=" << element
+                 << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget) << "returns" << r;
+        return r;
+
+    }
+    QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const
+    {
+        QRect r = QProxyStyle::subControlRect(cc, opt, sc, widget);
+        qDebug() << __FUNCTION__ << "cc=" << cc << "sc=" << sc
+                 <<  DebugStyle::debugQStyleOption(opt) << DebugStyle::debugWidget(widget) << "returns" << r;
+        return r;
+    }
+    QRect itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const
+    {
+        QRect re = QProxyStyle::itemTextRect(fm, r, flags, enabled, text);
+        qDebug() << __FUNCTION__ <<  r << "flags=" << flags  << "enabled=" << enabled << text << "returns " << re;
+        return re;
+    }
+
+    QRect itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const
+    {
+        QRect re = QProxyStyle::itemPixmapRect(r, flags, pixmap);
+        qDebug() << __FUNCTION__ << r << "flags=" << flags  << pixmap << "returns " << re;
+        return re;
+    }
+
+    int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const
+    {
+        int r = QProxyStyle::pixelMetric(metric, option, widget);
+        qDebug() << __FUNCTION__ << "metric="<< metric
+                 << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget) << "returns" << r;
+        return r;
+    }
+
+    QPixmap standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, const QWidget *widget = 0) const
+    {
+        QPixmap r = QProxyStyle::standardPixmap(standardPixmap, opt, widget);
+        qDebug() << __FUNCTION__ << "standardPixmap=" << standardPixmap << "returns " << r;
+        return r;
+    }
+
+    QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const
+    {
+        QPixmap r = QProxyStyle::generatedIconPixmap(iconMode, pixmap, opt);
+        qDebug() << __FUNCTION__ << "iconMode=" << iconMode << pixmap << "returns " << r;
+        return r;
+    }
+
+private:
+    static QString debugWidget(const QWidget *widget = 0)
+    {
+        if (!widget)
+            return QLatin1String("widget=NULL");
+        const QString on = widget->objectName();
+        if (!on.isEmpty())
+            return on;
+        return widget->metaObject()->className();
+    }
+    static QString debugQStyleOption(const QStyleOption *option)
+    {
+        QString result;
+        if (option)
+            QDebug(&result) << "optRec=" << option->rect;
+        else
+            result = QLatin1String("option=NULL");
+        return result;
+    }
+
+
+}; // DebugStyle
+
+#endif // DEBUGGINGPROXYSTYLE_H
diff --git a/examples/quick/controls/gallery/gallery.pro b/examples/quick/controls/gallery/gallery.pro
index f13f421..b20236f 100644
--- a/examples/quick/controls/gallery/gallery.pro
+++ b/examples/quick/controls/gallery/gallery.pro
@@ -1,6 +1,6 @@
 TEMPLATE = app
 TARGET = gallery
-
+CONFIG+=console
 SOURCES += \
     main.cpp
 
@@ -18,3 +18,5 @@ OTHER_FILES += \
     qml/+osx/UI.js
 
 include(../shared/shared.pri)
+
+HEADERS += debuggingproxystyle.h
diff --git a/examples/quick/controls/gallery/main.cpp b/examples/quick/controls/gallery/main.cpp
index 4c254a4..fd68f8b 100644
--- a/examples/quick/controls/gallery/main.cpp
+++ b/examples/quick/controls/gallery/main.cpp
@@ -39,12 +39,14 @@
 ****************************************************************************/
 
 #include "qtquickcontrolsapplication.h"
+#include "debuggingproxystyle.h"
 #include <QtQml/QQmlApplicationEngine>
 #include <QtGui/QSurfaceFormat>
 
 int main(int argc, char *argv[])
 {
     QtQuickControlsApplication app(argc, argv);
+    app.setStyle(new DebugStyle(app.style()));
     if (QCoreApplication::arguments().contains(QLatin1String("--coreprofile"))) {
         QSurfaceFormat fmt;
         fmt.setVersion(4, 4);
-- 
1.9.1

