summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorCarlos Manuel Duclos Vergara <carlos.duclos@nokia.com>2010-05-20 10:17:58 (GMT)
committerCarlos Manuel Duclos Vergara <carlos.duclos@nokia.com>2010-05-20 10:22:36 (GMT)
commit65a673f7ab1955e277246e4c88bec46493265cf3 (patch)
treeb5fe59da87a422682b2f7eaec2c66a6b5d605877 /src
parent16f44ee07db46ad362a464afc2c6e6567c933870 (diff)
downloadQt-65a673f7ab1955e277246e4c88bec46493265cf3.zip
Qt-65a673f7ab1955e277246e4c88bec46493265cf3.tar.gz
Qt-65a673f7ab1955e277246e4c88bec46493265cf3.tar.bz2
QPushButton's click area exceeds the button area by far on Mac OS X
The problem is the "rounding" of buttons in OSX. To achieve this we add some padding to the buttons, therefore creating an inner rect for the widget. The common hitButton(...) method found in QAbstractButton just checks the normal rect of the widget. What this patch does is to reimplement hitButton(...) in QPushButton, but only for the Mac case. In this reimplemented method I calculate the inner rect and check if the hit point is inside that rect or not. Task-number: QTBUG-10401 Reviewed-by: Richard Moe Gustavsen
Diffstat (limited to 'src')
-rw-r--r--src/gui/styles/qmacstyle_mac.h11
-rw-r--r--src/gui/styles/qmacstyle_mac.mm33
-rw-r--r--src/gui/widgets/qpushbutton.cpp34
-rw-r--r--src/gui/widgets/qpushbutton.h3
-rw-r--r--src/gui/widgets/qpushbutton_p.h3
5 files changed, 68 insertions, 16 deletions
diff --git a/src/gui/styles/qmacstyle_mac.h b/src/gui/styles/qmacstyle_mac.h
index bcebb1d..e594793 100644
--- a/src/gui/styles/qmacstyle_mac.h
+++ b/src/gui/styles/qmacstyle_mac.h
@@ -120,6 +120,17 @@ public:
bool event(QEvent *e);
+ // Ideally these wouldn't exist, but since they already exist we need some accessors.
+ static const int PushButtonLeftOffset;
+ static const int PushButtonTopOffset;
+ static const int PushButtonRightOffset;
+ static const int PushButtonBottomOffset;
+ static const int MiniButtonH;
+ static const int SmallButtonH;
+ static const int BevelButtonW;
+ static const int BevelButtonH;
+ static const int PushButtonContentPadding;
+
protected Q_SLOTS:
QIcon standardIconImplementation(StandardPixmap standardIcon, const QStyleOption *opt = 0,
const QWidget *widget = 0) const;
diff --git a/src/gui/styles/qmacstyle_mac.mm b/src/gui/styles/qmacstyle_mac.mm
index e065bcc..0f01bd5 100644
--- a/src/gui/styles/qmacstyle_mac.mm
+++ b/src/gui/styles/qmacstyle_mac.mm
@@ -108,15 +108,15 @@ extern QRegion qt_mac_convert_mac_region(RgnHandle); //qregion_mac.cpp
// The following constants are used for adjusting the size
// of push buttons so that they are drawn inside their bounds.
-static const int PushButtonLeftOffset = 6;
-static const int PushButtonTopOffset = 4;
-static const int PushButtonRightOffset = 12;
-static const int PushButtonBottomOffset = 12;
-static const int MiniButtonH = 26;
-static const int SmallButtonH = 30;
-static const int BevelButtonW = 50;
-static const int BevelButtonH = 22;
-static const int PushButtonContentPadding = 6;
+const int QMacStyle::PushButtonLeftOffset = 6;
+const int QMacStyle::PushButtonTopOffset = 4;
+const int QMacStyle::PushButtonRightOffset = 12;
+const int QMacStyle::PushButtonBottomOffset = 12;
+const int QMacStyle::MiniButtonH = 26;
+const int QMacStyle::SmallButtonH = 30;
+const int QMacStyle::BevelButtonW = 50;
+const int QMacStyle::BevelButtonH = 22;
+const int QMacStyle::PushButtonContentPadding = 6;
// These colors specify the titlebar gradient colors on
// Leopard. Ideally we should get them from the system.
@@ -1055,10 +1055,10 @@ HIRect QMacStylePrivate::pushButtonContentBounds(const QStyleOptionButton *btn,
// Adjust the bounds to correct for
// carbon not calculating the content bounds fully correct
if (bdi->kind == kThemePushButton || bdi->kind == kThemePushButtonSmall){
- outerBounds.origin.y += PushButtonTopOffset;
- outerBounds.size.height -= PushButtonBottomOffset;
+ outerBounds.origin.y += QMacStyle::PushButtonTopOffset;
+ outerBounds.size.height -= QMacStyle::PushButtonBottomOffset;
} else if (bdi->kind == kThemePushButtonMini) {
- outerBounds.origin.y += PushButtonTopOffset;
+ outerBounds.origin.y += QMacStyle::PushButtonTopOffset;
}
HIRect contentBounds;
@@ -1074,7 +1074,7 @@ QSize QMacStylePrivate::pushButtonSizeFromContents(const QStyleOptionButton *btn
{
QSize csz(0, 0);
QSize iconSize = btn->icon.isNull() ? QSize(0, 0)
- : (btn->iconSize + QSize(PushButtonContentPadding, 0));
+ : (btn->iconSize + QSize(QMacStyle::PushButtonContentPadding, 0));
QRect textRect = btn->text.isEmpty() ? QRect(0, 0, 1, 1)
: btn->fontMetrics.boundingRect(QRect(), Qt::AlignCenter, btn->text);
csz.setWidth(iconSize.width() + textRect.width()
@@ -1149,12 +1149,12 @@ void QMacStylePrivate::initHIThemePushButton(const QStyleOptionButton *btn,
// Choose the button kind that closest match the button rect, but at the
// same time displays the button contents without clipping.
bdi->kind = kThemeBevelButton;
- if (btn->rect.width() >= BevelButtonW && btn->rect.height() >= BevelButtonH){
+ if (btn->rect.width() >= QMacStyle::BevelButtonW && btn->rect.height() >= QMacStyle::BevelButtonH){
if (widget && widget->testAttribute(Qt::WA_MacVariableSize)) {
- if (btn->rect.height() <= MiniButtonH){
+ if (btn->rect.height() <= QMacStyle::MiniButtonH){
if (contentFitsInPushButton(btn, bdi, kThemePushButtonMini))
bdi->kind = kThemePushButtonMini;
- } else if (btn->rect.height() <= SmallButtonH){
+ } else if (btn->rect.height() <= QMacStyle::SmallButtonH){
if (contentFitsInPushButton(btn, bdi, kThemePushButtonSmall))
bdi->kind = kThemePushButtonSmall;
} else if (contentFitsInPushButton(btn, bdi, kThemePushButton)) {
@@ -3470,6 +3470,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
QCommonStyle::drawControl(ce, opt, p, w);
break;
case CE_PushButtonBevel:
+ qDebug() << "here";
if (const QStyleOptionButton *btn = ::qstyleoption_cast<const QStyleOptionButton *>(opt)) {
if (!(btn->state & (State_Raised | State_Sunken | State_On)))
break;
diff --git a/src/gui/widgets/qpushbutton.cpp b/src/gui/widgets/qpushbutton.cpp
index 1a9adcc..7b8c0db 100644
--- a/src/gui/widgets/qpushbutton.cpp
+++ b/src/gui/widgets/qpushbutton.cpp
@@ -58,6 +58,9 @@
#include "qdebug.h"
#include "qlayoutitem.h"
#include "qdialogbuttonbox.h"
+#ifdef Q_WS_MAC
+#include "qmacstyle_mac.h"
+#endif // Q_WS_MAC
#ifndef QT_NO_ACCESSIBILITY
#include "qaccessible.h"
@@ -679,6 +682,37 @@ bool QPushButton::event(QEvent *e)
return QAbstractButton::event(e);
}
+#ifdef Q_WS_MAC
+/*! \reimp */
+bool QPushButton::hitButton(const QPoint &pos) const
+{
+ // This is only required if we are using the native style, so check that first.
+ QMacStyle *macStyle = qobject_cast<QMacStyle *>(style());
+ // If this is a flat button we just bail out.
+ if(isFlat() || (0 == macStyle))
+ return QAbstractButton::hitButton(pos);
+ // Now that we know we are using the native style, let's proceed.
+ Q_D(const QPushButton);
+ QPushButtonPrivate *nonConst = const_cast<QPushButtonPrivate *>(d);
+ // In OSX buttons are round, which causes the hit method to be special.
+ // We cannot simply relay on detecting if something is inside the rect or not,
+ // we need to check if it is inside the "rounded area" or not. A point might
+ // be inside the rect but not inside the rounded area.
+ // Notice this method is only reimplemented for OSX.
+ return nonConst->hitButton(pos);
+}
+
+bool QPushButtonPrivate::hitButton(const QPoint &pos)
+{
+ Q_Q(QPushButton);
+ QRect roundedRect(q->rect().left() + QMacStyle::PushButtonLeftOffset,
+ q->rect().top() + QMacStyle::PushButtonContentPadding,
+ q->rect().width() - QMacStyle::PushButtonRightOffset,
+ q->rect().height() - QMacStyle::PushButtonBottomOffset);
+ return roundedRect.contains(pos);
+}
+#endif // Q_WS_MAC
+
#ifdef QT3_SUPPORT
/*!
Use one of the constructors that doesn't take the \a name
diff --git a/src/gui/widgets/qpushbutton.h b/src/gui/widgets/qpushbutton.h
index 2a4823d..cf28753 100644
--- a/src/gui/widgets/qpushbutton.h
+++ b/src/gui/widgets/qpushbutton.h
@@ -91,6 +91,9 @@ public Q_SLOTS:
protected:
bool event(QEvent *e);
+#ifdef Q_WS_MAC
+ bool hitButton(const QPoint &pos) const;
+#endif // Q_WS_MAC
void paintEvent(QPaintEvent *);
void keyPressEvent(QKeyEvent *);
void focusInEvent(QFocusEvent *);
diff --git a/src/gui/widgets/qpushbutton_p.h b/src/gui/widgets/qpushbutton_p.h
index f2ee09d..6feb726 100644
--- a/src/gui/widgets/qpushbutton_p.h
+++ b/src/gui/widgets/qpushbutton_p.h
@@ -69,6 +69,9 @@ public:
inline void init() { resetLayoutItemMargins(); }
static QPushButtonPrivate* get(QPushButton *b) { return b->d_func(); }
+#ifdef Q_WS_MAC
+ bool hitButton(const QPoint &pos);
+#endif
#ifndef QT_NO_MENU
QPoint adjustedMenuPosition();
#endif