From ded00dfde0e5524d3afc543e3905b5c57893cdf9 Mon Sep 17 00:00:00 2001 From: Philip Belemezov Date: Thu, 9 Jan 2014 11:14:02 +0200 Subject: [PATCH] Tooltip support for QMacNativeToolBar This patch adds support for setting/querying the tooltips of toolbar buttons on Mac. --- src/macextras/qmacnativetoolbar.mm | 4 ++-- src/macextras/qmacnativetoolbar_p.h | 2 +- src/macextras/qmactoolbardelegate.mm | 11 +++++++++++ src/macextras/qmactoolbardelegate_p.h | 1 + src/macextras/qmactoolbutton.mm | 10 ++++++++++ src/macextras/qmactoolbutton_p.h | 4 ++++ 6 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/macextras/qmacnativetoolbar.mm b/src/macextras/qmacnativetoolbar.mm index 22c6a6b..b81161b 100644 --- a/src/macextras/qmacnativetoolbar.mm +++ b/src/macextras/qmacnativetoolbar.mm @@ -438,9 +438,9 @@ QMacToolButton *QMacNativeToolBar::addAction(const QString &text) return [d->delegate addActionWithText:&text]; } -QMacToolButton *QMacNativeToolBar::addAction(const QIcon &icon, const QString &text) +QMacToolButton *QMacNativeToolBar::addAction(const QIcon &icon, const QString &text, const QString &toolTip/*=QString()*/) { - return [d->delegate addActionWithText:&text icon:&icon]; + return [d->delegate addActionWithText:&text icon:&icon toolTip:&toolTip]; } void QMacNativeToolBar::addSeparator() diff --git a/src/macextras/qmacnativetoolbar_p.h b/src/macextras/qmacnativetoolbar_p.h index fe15ea2..76b250f 100644 --- a/src/macextras/qmacnativetoolbar_p.h +++ b/src/macextras/qmacnativetoolbar_p.h @@ -89,7 +89,7 @@ public: // Add actions to the toolbar Q_INVOKABLE QMacToolButton *addAction(const QString &text); - Q_INVOKABLE QMacToolButton *addAction(const QIcon &icon, const QString &text); + Q_INVOKABLE QMacToolButton *addAction(const QIcon &icon, const QString &text, const QString &toolTip=QString()); Q_INVOKABLE void addSeparator(); Q_INVOKABLE QMacToolButton *addStandardItem(QMacToolButton::StandardItem standardItem); diff --git a/src/macextras/qmactoolbardelegate.mm b/src/macextras/qmactoolbardelegate.mm index 678423e..0f623b8 100644 --- a/src/macextras/qmactoolbardelegate.mm +++ b/src/macextras/qmactoolbardelegate.mm @@ -132,6 +132,10 @@ QString qt_strippedText(QString s) NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease]; [toolbarItem setLabel: QtMac::toNSString(toolButton->text())]; [toolbarItem setPaletteLabel :[toolbarItem label]]; + const QString toolTip = toolButton->toolTip(); + if (!toolTip.isEmpty()) { + [toolbarItem setToolTip:(QtMac::toNSString(toolTip))]; + } QPixmap icon = toolButton->icon().pixmap(64, 64); if (icon.isNull() == false) { @@ -152,9 +156,16 @@ QString qt_strippedText(QString s) - (QMacToolButton *)addActionWithText:(const QString *)text icon:(const QIcon *)icon { + QString nullToolTip; + return [self addActionWithText:text icon:icon toolTip:&nullToolTip]; +} + +- (QMacToolButton *)addActionWithText:(const QString *)text icon:(const QIcon *)icon toolTip:(const QString *)toolTip +{ QMacToolButton *button = new QMacToolButton(0); button->setText(*text); button->setIcon(*icon); + button->setToolTip(*toolTip); items.append(button); allowedItems.append(button); return button; diff --git a/src/macextras/qmactoolbardelegate_p.h b/src/macextras/qmactoolbardelegate_p.h index 8c71804..4d7920e 100644 --- a/src/macextras/qmactoolbardelegate_p.h +++ b/src/macextras/qmactoolbardelegate_p.h @@ -63,6 +63,7 @@ - (QMacToolButton *)addActionWithText:(const QString *)text; - (QMacToolButton *)addActionWithText:(const QString *)text icon:(const QIcon *)icon; +- (QMacToolButton *)addActionWithText:(const QString *)text icon:(const QIcon *)icon toolTip:(const QString *)toolTip; - (QMacToolButton *)addStandardItem:(QMacToolButton::StandardItem)standardItem; - (QMacToolButton *)addAllowedActionWithText:(const QString *)text; diff --git a/src/macextras/qmactoolbutton.mm b/src/macextras/qmactoolbutton.mm index ca41f2a..e5677fe 100644 --- a/src/macextras/qmactoolbutton.mm +++ b/src/macextras/qmactoolbutton.mm @@ -101,4 +101,14 @@ void QMacToolButton::setIcon(const QIcon &icon) m_icon = icon; } +QString QMacToolButton::toolTip() const +{ + return m_toolTip; +} + +void QMacToolButton::setToolTip(const QString &tip) +{ + m_toolTip = tip; +} + QT_END_NAMESPACE diff --git a/src/macextras/qmactoolbutton_p.h b/src/macextras/qmactoolbutton_p.h index 584b391..be6e867 100644 --- a/src/macextras/qmactoolbutton_p.h +++ b/src/macextras/qmactoolbutton_p.h @@ -89,6 +89,9 @@ public: QIcon icon() const; void setIcon(const QIcon &icon); + + QString toolTip() const; + void setToolTip(const QString &tip); Q_SIGNALS: void activated(); private: @@ -96,6 +99,7 @@ private: StandardItem m_standardItem; QString m_text; QIcon m_icon; + QString m_toolTip; public: // (not really public) void emitActivated() { Q_EMIT activated(); } -- 1.8.5.2