Introduce QAbstractPlatformMenuBar

Merge-request: 1254
Reviewed-by: Gabriel de Dietrich <gabriel.dietrich-de@nokia.com>
(cherry picked from commit 9aeadca09ab4e27ebf299873f90490d585b4fb7f)

Change-Id: Ib0f035ac15ad0cc6be52807e8101f5dcfb5b041b
Reviewed-on: http://codereview.qt.nokia.com/3932
Reviewed-by: Gabriel de Dietrich <gabriel.dietrich-de@nokia.com>
This commit is contained in:
Aurélien Gâteau 2010-11-10 23:37:56 +01:00 committed by Qt by Nokia
parent b7832ce2f8
commit 6c97b066a9
6 changed files with 457 additions and 11 deletions

View File

@ -0,0 +1,95 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QABSTRACTPLATFORMMENUBAR_P_H
#define QABSTRACTPLATFORMMENUBAR_P_H
#include <qglobal.h>
#ifndef QT_NO_MENUBAR
QT_BEGIN_NAMESPACE
class QAction;
class QActionEvent;
class QEvent;
class QMenuBar;
class QObject;
class QWidget;
/*!
The platform-specific implementation of a menubar
*/
class QAbstractPlatformMenuBar
{
public:
virtual ~QAbstractPlatformMenuBar() {}
virtual void init(QMenuBar *) = 0;
virtual void setVisible(bool visible) = 0;
virtual void actionEvent(QActionEvent *) = 0;
virtual void handleReparent(QWidget *oldParent, QWidget *newParent, QWidget *oldWindow, QWidget *newWindow) = 0;
virtual bool allowCornerWidgets() const = 0;
virtual void popupAction(QAction *) = 0;
virtual void setNativeMenuBar(bool) = 0;
virtual bool isNativeMenuBar() const = 0;
/*!
Return true if the native menubar is capable of listening to the
shortcut keys. If false is returned, QMenuBar will trigger actions on
shortcut itself.
*/
virtual bool shortcutsHandledByNativeMenuBar() const = 0;
virtual bool menuBarEventFilter(QObject *, QEvent *event) = 0;
};
QT_END_NAMESPACE
#endif // QT_NO_MENUBAR
#endif // QABSTRACTPLATFORMMENUBAR_P_H

View File

@ -66,6 +66,9 @@
#include "qmenu_p.h"
#include "qmenubar_p.h"
#include "qdebug.h"
#ifdef Q_WS_X11
#include "qmenubar_x11_p.h"
#endif
#ifdef Q_WS_WINCE
extern bool qt_wince_is_mobile(); //defined in qguifunctions_wce.cpp
@ -173,7 +176,11 @@ void QMenuBarPrivate::updateGeometries()
return;
int q_width = q->width()-(q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q)*2);
int q_start = -1;
if(leftWidget || rightWidget) {
if(
#ifdef Q_WS_X11
platformMenuBar->allowCornerWidgets() &&
#endif
(leftWidget || rightWidget)) {
int vmargin = q->style()->pixelMetric(QStyle::PM_MenuBarVMargin, 0, q)
+ q->style()->pixelMetric(QStyle::PM_MenuBarPanelWidth, 0, q);
int hmargin = q->style()->pixelMetric(QStyle::PM_MenuBarHMargin, 0, q)
@ -204,13 +211,23 @@ void QMenuBarPrivate::updateGeometries()
calcActionRects(q_width, q_start);
currentAction = 0;
#ifndef QT_NO_SHORTCUT
if(itemsDirty) {
if(
#ifdef Q_WS_X11
!platformMenuBar->shortcutsHandledByNativeMenuBar() &&
#endif
itemsDirty) {
for(int j = 0; j < shortcutIndexMap.size(); ++j)
q->releaseShortcut(shortcutIndexMap.value(j));
shortcutIndexMap.resize(0); // faster than clear
for(int i = 0; i < actions.count(); i++)
shortcutIndexMap.append(q->grabShortcut(QKeySequence::mnemonic(actions.at(i)->text())));
}
#endif
#ifdef Q_WS_X11
if(q->isNativeMenuBar()) {//nothing to see here folks, move along..
itemsDirty = false;
return;
}
#endif
itemsDirty = false;
@ -743,6 +760,11 @@ void QMenuBarPrivate::init()
QApplication::setAttribute(Qt::AA_DontUseNativeMenuBar, true);
}
#endif
#ifdef Q_WS_X11
platformMenuBar = new QX11MenuBar;
platformMenuBar->init(q);
#endif
q->setBackgroundRole(QPalette::Button);
oldWindow = oldParent = 0;
#ifdef QT3_SUPPORT
@ -750,6 +772,10 @@ void QMenuBarPrivate::init()
#endif
#ifdef QT_SOFTKEYS_ENABLED
menuBarAction = 0;
#endif
#ifdef Q_WS_X11
cornerWidgetToolBar = 0;
cornerWidgetContainer = 0;
#endif
handleReparent();
q->setMouseTracking(q->style()->styleHint(QStyle::SH_MenuBar_MouseTracking, 0, q));
@ -821,6 +847,10 @@ QMenuBar::~QMenuBar()
Q_D(QMenuBar);
d->symbianDestroyMenuBar();
#endif
#ifdef Q_WS_X11
Q_D(QMenuBar);
delete d->cornerWidgetToolBar;
#endif
}
/*!
@ -1072,6 +1102,10 @@ void QMenuBar::paintEvent(QPaintEvent *e)
*/
void QMenuBar::setVisible(bool visible)
{
#ifdef Q_WS_X11
Q_D(QMenuBar);
d->platformMenuBar->setVisible(visible);
#else
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE) || defined(Q_WS_S60)
if (isNativeMenuBar()) {
if (!visible)
@ -1080,6 +1114,7 @@ void QMenuBar::setVisible(bool visible)
}
#endif
QWidget::setVisible(visible);
#endif // Q_WS_X11
}
/*!
@ -1275,6 +1310,9 @@ void QMenuBar::actionEvent(QActionEvent *e)
{
Q_D(QMenuBar);
d->itemsDirty = true;
#ifdef Q_WS_X11
d->platformMenuBar->actionEvent(e);
#endif
#if defined (Q_WS_MAC) || defined(Q_OS_WINCE) || defined(Q_WS_S60)
if (isNativeMenuBar()) {
#ifdef Q_WS_MAC
@ -1369,6 +1407,10 @@ void QMenuBarPrivate::handleReparent()
newWindow->installEventFilter(q);
}
#ifdef Q_WS_X11
platformMenuBar->handleReparent(oldParent, newParent, oldWindow, newWindow);
#endif
oldParent = newParent;
oldWindow = newWindow;
@ -1566,6 +1608,11 @@ bool QMenuBar::event(QEvent *e)
bool QMenuBar::eventFilter(QObject *object, QEvent *event)
{
Q_D(QMenuBar);
#ifdef Q_WS_X11
if (d->platformMenuBar->menuBarEventFilter(object, event)) {
return true;
}
#endif
if (object == parent() && object) {
#ifdef QT3_SUPPORT
if (d->doAutoResize && event->type() == QEvent::Resize) {
@ -1659,7 +1706,7 @@ QRect QMenuBar::actionGeometry(QAction *act) const
QSize QMenuBar::minimumSizeHint() const
{
Q_D(const QMenuBar);
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60) || defined(Q_WS_X11)
const bool as_gui_menubar = !isNativeMenuBar();
#else
const bool as_gui_menubar = true;
@ -1682,6 +1729,9 @@ QSize QMenuBar::minimumSizeHint() const
ret += QSize(2*fw + hmargin, 2*fw + vmargin);
}
int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
#ifdef Q_WS_X11
if (d->platformMenuBar->allowCornerWidgets()) {
#endif
if(d->leftWidget) {
QSize sz = d->leftWidget->minimumSizeHint();
ret.setWidth(ret.width() + sz.width());
@ -1694,6 +1744,9 @@ QSize QMenuBar::minimumSizeHint() const
if(sz.height() + margin > ret.height())
ret.setHeight(sz.height() + margin);
}
#ifdef Q_WS_X11
}
#endif
if(as_gui_menubar) {
QStyleOptionMenuItem opt;
opt.rect = rect();
@ -1715,7 +1768,7 @@ QSize QMenuBar::minimumSizeHint() const
QSize QMenuBar::sizeHint() const
{
Q_D(const QMenuBar);
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60) || defined(Q_WS_X11)
const bool as_gui_menubar = !isNativeMenuBar();
#else
const bool as_gui_menubar = true;
@ -1741,6 +1794,9 @@ QSize QMenuBar::sizeHint() const
ret += QSize(fw + hmargin, fw + vmargin);
}
int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
#ifdef Q_WS_X11
if(d->platformMenuBar->allowCornerWidgets()) {
#endif
if(d->leftWidget) {
QSize sz = d->leftWidget->sizeHint();
ret.setWidth(ret.width() + sz.width());
@ -1753,6 +1809,9 @@ QSize QMenuBar::sizeHint() const
if(sz.height() + margin > ret.height())
ret.setHeight(sz.height() + margin);
}
#ifdef Q_WS_X11
}
#endif
if(as_gui_menubar) {
QStyleOptionMenuItem opt;
opt.rect = rect();
@ -1774,7 +1833,7 @@ QSize QMenuBar::sizeHint() const
int QMenuBar::heightForWidth(int) const
{
Q_D(const QMenuBar);
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60)
#if defined(Q_WS_MAC) || defined(Q_WS_WINCE) || defined(Q_WS_S60) || defined(Q_WS_X11)
const bool as_gui_menubar = !isNativeMenuBar();
#else
const bool as_gui_menubar = true;
@ -1794,10 +1853,16 @@ int QMenuBar::heightForWidth(int) const
height += 2*vmargin;
}
int margin = 2*vmargin + 2*fw + spaceBelowMenuBar;
#ifdef Q_WS_X11
if(d->platformMenuBar->allowCornerWidgets()) {
#endif
if(d->leftWidget)
height = qMax(d->leftWidget->sizeHint().height() + margin, height);
if(d->rightWidget)
height = qMax(d->rightWidget->sizeHint().height() + margin, height);
#ifdef Q_WS_X11
}
#endif
if(as_gui_menubar) {
QStyleOptionMenuItem opt;
opt.init(this);
@ -1817,7 +1882,15 @@ void QMenuBarPrivate::_q_internalShortcutActivated(int id)
{
Q_Q(QMenuBar);
QAction *act = actions.at(id);
setCurrentAction(act, true, true);
#ifdef Q_WS_X11
if (q->isNativeMenuBar()) {
platformMenuBar->popupAction(act);
} else {
#endif
setCurrentAction(act, true, true);
#ifdef Q_WS_X11
}
#endif
if (act && !act->menu()) {
activateAction(act, QAction::Trigger);
//100 is the same as the default value in QPushButton::animateClick
@ -1838,6 +1911,39 @@ void QMenuBarPrivate::_q_updateLayout()
}
}
#ifdef Q_WS_X11
void QMenuBarPrivate::updateCornerWidgetToolBar()
{
Q_Q(QMenuBar);
if (!cornerWidgetToolBar) {
QMainWindow *window = qobject_cast<QMainWindow *>(q->window());
if (!window) {
qWarning() << "Menubar parent is not a QMainWindow, not showing corner widgets";
return;
}
cornerWidgetToolBar = window->addToolBar(QApplication::translate("QMenuBar", "Corner Toolbar"));
cornerWidgetToolBar->setObjectName(QLatin1String("CornerToolBar"));
cornerWidgetContainer = new QWidget;
cornerWidgetToolBar->addWidget(cornerWidgetContainer);
new QHBoxLayout(cornerWidgetContainer);
} else {
QLayout *layout = cornerWidgetContainer->layout();
while (layout->count() > 0) {
layout->takeAt(0);
}
}
if (leftWidget) {
leftWidget->setParent(cornerWidgetContainer);
cornerWidgetContainer->layout()->addWidget(leftWidget);
}
if (rightWidget) {
rightWidget->setParent(cornerWidgetContainer);
cornerWidgetContainer->layout()->addWidget(rightWidget);
}
}
#endif
/*!
\fn void QMenuBar::setCornerWidget(QWidget *widget, Qt::Corner corner)
@ -1870,10 +1976,18 @@ void QMenuBar::setCornerWidget(QWidget *w, Qt::Corner corner)
return;
}
#ifdef Q_WS_X11
if(!d->platformMenuBar->allowCornerWidgets()) {
d->updateCornerWidgetToolBar();
} else {
#endif
if (w) {
w->setParent(this);
w->installEventFilter(this);
}
#ifdef Q_WS_X11
}
#endif
d->_q_updateLayout();
}
@ -1923,6 +2037,9 @@ QWidget *QMenuBar::cornerWidget(Qt::Corner corner) const
void QMenuBar::setNativeMenuBar(bool nativeMenuBar)
{
Q_D(QMenuBar);
#ifdef Q_WS_X11
d->platformMenuBar->setNativeMenuBar(nativeMenuBar);
#else
if (d->nativeMenuBar == -1 || (nativeMenuBar != bool(d->nativeMenuBar))) {
d->nativeMenuBar = nativeMenuBar;
#ifdef Q_WS_MAC
@ -1947,15 +2064,20 @@ void QMenuBar::setNativeMenuBar(bool nativeMenuBar)
setVisible(true);
#endif
}
#endif // Q_WS_X11
}
bool QMenuBar::isNativeMenuBar() const
{
Q_D(const QMenuBar);
#ifdef Q_WS_X11
return d->platformMenuBar->isNativeMenuBar();
#else
if (d->nativeMenuBar == -1) {
return !QApplication::instance()->testAttribute(Qt::AA_DontUseNativeMenuBar);
}
return d->nativeMenuBar;
#endif
}
/*!

View File

@ -47,7 +47,7 @@
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// platformMenuBarementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
@ -61,6 +61,10 @@
#include "qguifunctions_wince.h"
#endif
#ifdef Q_WS_X11
#include "qabstractplatformmenubar_p.h"
#endif
#ifndef QT_NO_MENUBAR
#ifdef Q_WS_S60
class CCoeControl;
@ -71,21 +75,27 @@ class CEikMenuBar;
QT_BEGIN_NAMESPACE
#ifndef QT_NO_MENUBAR
class QToolBar;
class QMenuBarExtension;
class QMenuBarPrivate : public QWidgetPrivate
{
Q_DECLARE_PUBLIC(QMenuBar)
public:
QMenuBarPrivate() : itemsDirty(0), currentAction(0), mouseDown(0),
closePopupMode(0), defaultPopDown(1), popupState(0), keyboardState(0), altPressed(0),
nativeMenuBar(-1), doChildEffects(false)
closePopupMode(0), defaultPopDown(1), popupState(0), keyboardState(0), altPressed(0)
#ifndef Q_WS_X11
, nativeMenuBar(-1)
#endif
, doChildEffects(false)
#ifdef QT3_SUPPORT
, doAutoResize(false)
#endif
#ifdef Q_WS_MAC
, mac_menubar(0)
#endif
#ifdef Q_WS_X11
, platformMenuBar(0)
#endif
#ifdef Q_WS_WINCE
, wce_menubar(0), wceClassicMenu(false)
#endif
@ -96,6 +106,9 @@ public:
{ }
~QMenuBarPrivate()
{
#ifdef Q_WS_X11
delete platformMenuBar;
#endif
#ifdef Q_WS_MAC
delete mac_menubar;
#endif
@ -136,8 +149,9 @@ public:
uint keyboardState : 1, altPressed : 1;
QPointer<QWidget> keyboardFocusWidget;
#ifndef Q_WS_X11
int nativeMenuBar : 3; // Only has values -1, 0, and 1
#endif
//firing of events
void activateAction(QAction *, QAction::ActionEvent);
@ -173,6 +187,9 @@ public:
#ifdef QT3_SUPPORT
bool doAutoResize;
#endif
#ifdef Q_WS_X11
QAbstractPlatformMenuBar *platformMenuBar;
#endif
#ifdef Q_WS_MAC
//mac menubar binding
struct QMacMenuBarPrivate {
@ -273,6 +290,12 @@ public:
#ifdef QT_SOFTKEYS_ENABLED
QAction *menuBarAction;
#endif
#ifdef Q_WS_X11
void updateCornerWidgetToolBar();
QToolBar *cornerWidgetToolBar;
QWidget *cornerWidgetContainer;
#endif
};
#endif

View File

@ -0,0 +1,114 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qmenubar_x11_p.h"
#ifndef QT_NO_MENUBAR
#include "qapplication.h"
#include "qdebug.h"
#include "qevent.h"
#include "qmenu.h"
#include "qmenubar.h"
QT_BEGIN_NAMESPACE
QX11MenuBar::~QX11MenuBar()
{
}
void QX11MenuBar::init(QMenuBar *_menuBar)
{
nativeMenuBar = -1;
menuBar = _menuBar;
}
void QX11MenuBar::setVisible(bool visible)
{
menuBar->QWidget::setVisible(visible);
}
void QX11MenuBar::actionEvent(QActionEvent *e)
{
Q_UNUSED(e);
}
void QX11MenuBar::handleReparent(QWidget *oldParent, QWidget *newParent, QWidget *oldWindow, QWidget *newWindow)
{
Q_UNUSED(oldParent)
Q_UNUSED(newParent)
Q_UNUSED(oldWindow)
Q_UNUSED(newWindow)
}
bool QX11MenuBar::allowCornerWidgets() const
{
return true;
}
void QX11MenuBar::popupAction(QAction *)
{
}
void QX11MenuBar::setNativeMenuBar(bool value)
{
if (nativeMenuBar == -1 || (value != bool(nativeMenuBar))) {
nativeMenuBar = value;
}
}
bool QX11MenuBar::isNativeMenuBar() const
{
return false;
}
bool QX11MenuBar::shortcutsHandledByNativeMenuBar() const
{
return false;
}
bool QX11MenuBar::menuBarEventFilter(QObject *, QEvent *)
{
return false;
}
QT_END_NAMESPACE
#endif // QT_NO_MENUBAR

View File

@ -0,0 +1,84 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QX11MENUBAR_P_H
#define QX11MENUBAR_P_H
#ifndef QT_NO_MENUBAR
#include "qabstractplatformmenubar_p.h"
QT_BEGIN_NAMESPACE
class QMenuBar;
class QX11MenuBar : public QAbstractPlatformMenuBar
{
public:
~QX11MenuBar();
virtual void init(QMenuBar *);
virtual void setVisible(bool visible);
virtual void actionEvent(QActionEvent *e);
virtual void handleReparent(QWidget *oldParent, QWidget *newParent, QWidget *oldWindow, QWidget *newWindow);
virtual bool allowCornerWidgets() const;
virtual void popupAction(QAction*);
virtual void setNativeMenuBar(bool);
virtual bool isNativeMenuBar() const;
virtual bool shortcutsHandledByNativeMenuBar() const;
virtual bool menuBarEventFilter(QObject *, QEvent *event);
private:
QMenuBar *menuBar;
int nativeMenuBar : 3; // Only has values -1, 0, and 1
};
QT_END_NAMESPACE
#endif // QT_NO_MENUBAR
#endif /* QX11MENUBAR_P_H */

View File

@ -144,6 +144,14 @@ SOURCES += \
widgets/qplaintextedit.cpp \
widgets/qprintpreviewwidget.cpp
x11: {
HEADERS += \
widgets/qabstractplatformmenubar_p.h
SOURCES += \
widgets/qmenubar_x11.cpp
}
!embedded:!qpa:mac {
HEADERS += widgets/qmacnativewidget_mac.h \
widgets/qmaccocoaviewcontainer_mac.h