Make ToolButton work with Macstyle for components
authorJens Bache-Wiig <jens.bache-wiig@digia.com>
Thu, 1 Nov 2012 10:47:37 +0000 (11:47 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 1 Nov 2012 19:17:57 +0000 (20:17 +0100)
We needed a new helper function for this. The widget cast was
breaking desktop components. By using accessible role, we can make
it work for both use cases without depending on the widget.

Change-Id: Ic854dc45a4e5b7a50c5be701e903d58a4a914ee5
Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com>
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
src/widgets/styles/qmacstyle_mac.mm
src/widgets/styles/qstylehelper.cpp
src/widgets/styles/qstylehelper_p.h

index b0a43c5..83895b6 100644 (file)
@@ -5434,7 +5434,8 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex
     case CC_ToolButton:
         if (const QStyleOptionToolButton *tb
                 = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
-            if (widget && qobject_cast<QToolBar *>(widget->parentWidget())) {
+
+            if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) {
                 if (tb->subControls & SC_ToolButtonMenu) {
                     QStyleOption arrowOpt(0);
                     arrowOpt.rect = proxy()->subControlRect(cc, tb, SC_ToolButtonMenu, widget);
@@ -6030,7 +6031,7 @@ QRect QMacStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *op
         break;
     case CC_ToolButton:
         ret = QCommonStyle::subControlRect(cc, opt, sc, widget);
-        if (sc == SC_ToolButtonMenu && widget && !qobject_cast<QToolBar*>(widget->parentWidget())) {
+        if (sc == SC_ToolButtonMenu && !QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar)) {
             ret.adjust(-1, 0, 0, 0);
         }
         break;
index e23db2b..8d67683 100644 (file)
@@ -86,6 +86,23 @@ qreal dpiScaled(qreal value)
 #endif
 }
 
+// Searches for an ancestor of a particular accessible role
+bool hasAncestor(QObject *obj, QAccessible::Role role)
+{
+    bool found = false;
+#ifndef QT_NO_ACCESSIBILITY
+    QObject *parent = obj ? obj->parent() : 0;
+    while (parent && !found) {
+        QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(parent);
+        if (iface && iface->role() == role)
+            found = true;
+        delete iface;
+        parent = parent->parent();
+    }
+#endif // QT_NO_ACCESSIBILITY
+    return found;
+}
+
 
 #ifndef QT_NO_DIAL
 
index e47722b..ab6a97e 100644 (file)
@@ -44,6 +44,7 @@
 #include <QtCore/qstring.h>
 #include <QtGui/qpolygon.h>
 #include <QtCore/qstringbuilder.h>
+#include <QtGui/qaccessible.h>
 
 #ifndef QSTYLEHELPER_P_H
 #define QSTYLEHELPER_P_H
@@ -81,6 +82,7 @@ namespace QStyleHelper
     void drawBorderPixmap(const QPixmap &pixmap, QPainter *painter, const QRect &rect,
                      int left = 0, int top = 0, int right = 0,
                      int bottom = 0);
+    bool hasAncestor(QObject *obj, QAccessible::Role role);
 }