aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-08-30 11:28:17 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2017-08-31 11:01:46 +0000
commit19e7fb3911c2032d2bdf05e2d37da0a0967d419a (patch)
treeb523b77a1750b60cb2ffb949f92674ec87e18d82
parent4a7365cd6c451e5f56fa145b134f7fa9b6d2199a (diff)
Add ApplicationWindow::menuBar
A follow-up commit to 66faa149. Change-Id: I94c92752d54ae0ca4878da72915b3d83461a4124 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--src/quicktemplates2/qquickapplicationwindow.cpp135
-rw-r--r--src/quicktemplates2/qquickapplicationwindow_p.h8
-rw-r--r--tests/auto/applicationwindow/data/layout.qml3
-rw-r--r--tests/auto/applicationwindow/tst_applicationwindow.cpp13
4 files changed, 131 insertions, 28 deletions
diff --git a/src/quicktemplates2/qquickapplicationwindow.cpp b/src/quicktemplates2/qquickapplicationwindow.cpp
index b39a0c35..872783bf 100644
--- a/src/quicktemplates2/qquickapplicationwindow.cpp
+++ b/src/quicktemplates2/qquickapplicationwindow.cpp
@@ -60,7 +60,7 @@ QT_BEGIN_NAMESPACE
\brief Styled top-level window with support for a header and footer.
ApplicationWindow is a \l Window which makes it convenient to add
- a \l header and \l footer item to the window.
+ a \l {menuBar}{menu bar}, \l header and \l footer item to the window.
You can declare ApplicationWindow as the root item of your application,
and run it by using \l QQmlApplicationEngine. In this way you can control
@@ -69,11 +69,15 @@ QT_BEGIN_NAMESPACE
\image qtquickcontrols2-applicationwindow-wireframe.png
\qml
- import QtQuick.Controls 2.1
+ import QtQuick.Controls 2.3
ApplicationWindow {
visible: true
+ menuBar: MenuBar {
+ // ...
+ }
+
header: ToolBar {
// ...
}
@@ -119,6 +123,7 @@ public:
: complete(true),
background(nullptr),
contentItem(nullptr),
+ menuBar(nullptr),
header(nullptr),
footer(nullptr),
overlay(nullptr),
@@ -162,6 +167,7 @@ public:
bool complete;
QQuickItem *background;
QQuickItem *contentItem;
+ QQuickItem *menuBar;
QQuickItem *header;
QQuickItem *footer;
QQuickOverlay *overlay;
@@ -172,34 +178,34 @@ public:
QQuickApplicationWindow *q_ptr;
};
+static void layoutItem(QQuickItem *item, qreal y, qreal width)
+{
+ if (!item)
+ return;
+
+ item->setY(y);
+ QQuickItemPrivate *p = QQuickItemPrivate::get(item);
+ if (!p->widthValid) {
+ item->setWidth(width);
+ p->widthValid = false;
+ }
+}
+
void QQuickApplicationWindowPrivate::relayout()
{
Q_Q(QQuickApplicationWindow);
QQuickItem *content = q->contentItem();
qreal hh = header && header->isVisible() ? header->height() : 0;
qreal fh = footer && footer->isVisible() ? footer->height() : 0;
+ qreal mbh = menuBar && menuBar->isVisible() ? menuBar->height() : 0;
- content->setY(hh);
+ content->setY(mbh + hh);
content->setWidth(q->width());
- content->setHeight(q->height() - hh - fh);
+ content->setHeight(q->height() - mbh - hh - fh);
- if (header) {
- header->setY(-hh);
- QQuickItemPrivate *p = QQuickItemPrivate::get(header);
- if (!p->widthValid) {
- header->setWidth(q->width());
- p->widthValid = false;
- }
- }
-
- if (footer) {
- footer->setY(content->height());
- QQuickItemPrivate *p = QQuickItemPrivate::get(footer);
- if (!p->widthValid) {
- footer->setWidth(q->width());
- p->widthValid = false;
- }
- }
+ layoutItem(menuBar, -mbh - hh, q->width());
+ layoutItem(header, -hh, q->width());
+ layoutItem(footer, content->height(), q->width());
if (background) {
QQuickItemPrivate *p = QQuickItemPrivate::get(background);
@@ -331,6 +337,8 @@ QQuickApplicationWindow::~QQuickApplicationWindow()
Q_D(QQuickApplicationWindow);
d->setActiveFocusControl(nullptr);
disconnect(this, SIGNAL(activeFocusItemChanged()), this, SLOT(_q_updateActiveFocus()));
+ if (d->menuBar)
+ QQuickItemPrivate::get(d->menuBar)->removeItemChangeListener(d, ItemChanges);
if (d->header)
QQuickItemPrivate::get(d->header)->removeItemChangeListener(d, ItemChanges);
if (d->footer)
@@ -383,8 +391,9 @@ void QQuickApplicationWindow::setBackground(QQuickItem *background)
/*!
\qmlproperty Item QtQuick.Controls::ApplicationWindow::header
- This property holds the window header item. The header item is positioned to
- the top, and resized to the width of the window. The default value is \c null.
+ This property holds the window header item. The header item is positioned at the
+ top of the window, below the menu bar, and resized to the width of the window.
+ The default value is \c null.
\code
ApplicationWindow {
@@ -398,7 +407,7 @@ void QQuickApplicationWindow::setBackground(QQuickItem *background)
automatically sets the respective \l ToolBar::position, \l TabBar::position,
or \l DialogButtonBox::position property to \c Header.
- \sa footer, Page::header
+ \sa menuBar, footer, Page::header
*/
QQuickItem *QQuickApplicationWindow::header() const
{
@@ -453,7 +462,7 @@ void QQuickApplicationWindow::setHeader(QQuickItem *header)
automatically sets the respective \l ToolBar::position, \l TabBar::position,
or \l DialogButtonBox::position property to \c Footer.
- \sa header, Page::footer
+ \sa menuBar, header, Page::footer
*/
QQuickItem *QQuickApplicationWindow::footer() const
{
@@ -526,9 +535,9 @@ QQmlListProperty<QObject> QQuickApplicationWindow::contentData()
This property holds the window content item.
The content item is stacked above the \l background item, and under the
- \l header, and \l footer items.
+ \l menuBar, \l header, and \l footer items.
- \sa background, header, footer
+ \sa background, menuBar, header, footer
*/
QQuickItem *QQuickApplicationWindow::contentItem() const
{
@@ -729,6 +738,53 @@ void QQuickApplicationWindow::resetPalette()
setPalette(QPalette());
}
+/*!
+ \since QtQuick.Controls 2.3 (Qt 5.10)
+ \qmlproperty Item QtQuick.Controls::ApplicationWindow::menuBar
+
+ This property holds the window menu bar. The menu bar is positioned at the
+ top of the window, above the header, and resized to the width of the window.
+ The default value is \c null.
+
+ \code
+ ApplicationWindow {
+ menuBar: MenuBar {
+ // ...
+ }
+ }
+ \endcode
+
+ \sa header, footer, MenuBar
+*/
+QQuickItem *QQuickApplicationWindow::menuBar() const
+{
+ Q_D(const QQuickApplicationWindow);
+ return d->menuBar;
+}
+
+void QQuickApplicationWindow::setMenuBar(QQuickItem *menuBar)
+{
+ Q_D(QQuickApplicationWindow);
+ if (d->menuBar == menuBar)
+ return;
+
+ if (d->menuBar) {
+ QQuickItemPrivate::get(d->menuBar)->removeItemChangeListener(d, ItemChanges);
+ d->menuBar->setParentItem(nullptr);
+ }
+ d->menuBar = menuBar;
+ if (menuBar) {
+ menuBar->setParentItem(contentItem());
+ QQuickItemPrivate *p = QQuickItemPrivate::get(menuBar);
+ p->addItemChangeListener(d, ItemChanges);
+ if (qFuzzyIsNull(menuBar->z()))
+ menuBar->setZ(2);
+ }
+ if (isComponentComplete())
+ d->relayout();
+ emit menuBarChanged();
+}
+
QQuickApplicationWindowAttached *QQuickApplicationWindow::qmlAttachedProperties(QObject *object)
{
return new QQuickApplicationWindowAttached(object);
@@ -794,6 +850,8 @@ void QQuickApplicationWindowAttachedPrivate::windowChange(QQuickWindow *wnd)
if (oldWindow) {
disconnect(oldWindow, &QQuickApplicationWindow::activeFocusControlChanged,
this, &QQuickApplicationWindowAttachedPrivate::activeFocusChange);
+ QObject::disconnect(oldWindow, &QQuickApplicationWindow::menuBarChanged,
+ q, &QQuickApplicationWindowAttached::menuBarChanged);
QObject::disconnect(oldWindow, &QQuickApplicationWindow::headerChanged,
q, &QQuickApplicationWindowAttached::headerChanged);
QObject::disconnect(oldWindow, &QQuickApplicationWindow::footerChanged,
@@ -807,6 +865,8 @@ void QQuickApplicationWindowAttachedPrivate::windowChange(QQuickWindow *wnd)
if (newWindow) {
connect(newWindow, &QQuickApplicationWindow::activeFocusControlChanged,
this, &QQuickApplicationWindowAttachedPrivate::activeFocusChange);
+ QObject::connect(newWindow, &QQuickApplicationWindow::menuBarChanged,
+ q, &QQuickApplicationWindowAttached::menuBarChanged);
QObject::connect(newWindow, &QQuickApplicationWindow::headerChanged,
q, &QQuickApplicationWindowAttached::headerChanged);
QObject::connect(newWindow, &QQuickApplicationWindow::footerChanged,
@@ -822,6 +882,8 @@ void QQuickApplicationWindowAttachedPrivate::windowChange(QQuickWindow *wnd)
emit q->overlayChanged();
activeFocusChange();
+ if ((oldWindow && oldWindow->menuBar()) || (newWindow && newWindow->menuBar()))
+ emit q->menuBarChanged();
if ((oldWindow && oldWindow->header()) || (newWindow && newWindow->header()))
emit q->headerChanged();
if ((oldWindow && oldWindow->footer()) || (newWindow && newWindow->footer()))
@@ -968,6 +1030,25 @@ QQuickOverlay *QQuickApplicationWindowAttached::overlay() const
return QQuickOverlay::overlay(d->window);
}
+/*!
+ \since QtQuick.Controls 2.3 (Qt 5.10)
+ \qmlattachedproperty Item QtQuick.Controls::ApplicationWindow::menuBar
+ \readonly
+
+ This attached property holds the window menu bar. The property can be attached
+ to any item. The value is \c null if the item is not in an ApplicationWindow, or
+ the window has no menu bar.
+
+ \sa {Attached ApplicationWindow Properties}
+*/
+QQuickItem *QQuickApplicationWindowAttached::menuBar() const
+{
+ Q_D(const QQuickApplicationWindowAttached);
+ if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(d->window))
+ return window->menuBar();
+ return nullptr;
+}
+
QT_END_NAMESPACE
#include "moc_qquickapplicationwindow_p.cpp"
diff --git a/src/quicktemplates2/qquickapplicationwindow_p.h b/src/quicktemplates2/qquickapplicationwindow_p.h
index 8747e70f..ceab70e6 100644
--- a/src/quicktemplates2/qquickapplicationwindow_p.h
+++ b/src/quicktemplates2/qquickapplicationwindow_p.h
@@ -74,6 +74,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickApplicationWindow : public QQuickWi
Q_PROPERTY(QFont font READ font WRITE setFont RESET resetFont NOTIFY fontChanged FINAL)
Q_PROPERTY(QLocale locale READ locale WRITE setLocale RESET resetLocale NOTIFY localeChanged FINAL)
Q_PROPERTY(QPalette palette READ palette WRITE setPalette RESET resetPalette NOTIFY paletteChanged FINAL REVISION 3)
+ Q_PROPERTY(QQuickItem *menuBar READ menuBar WRITE setMenuBar NOTIFY menuBarChanged FINAL REVISION 3)
Q_CLASSINFO("DefaultProperty", "contentData")
public:
@@ -108,6 +109,9 @@ public:
void setPalette(const QPalette &palette);
void resetPalette();
+ QQuickItem *menuBar() const;
+ void setMenuBar(QQuickItem *menuBar);
+
static QQuickApplicationWindowAttached *qmlAttachedProperties(QObject *object);
Q_SIGNALS:
@@ -118,6 +122,7 @@ Q_SIGNALS:
void fontChanged();
void localeChanged();
Q_REVISION(3) void paletteChanged();
+ Q_REVISION(3) void menuBarChanged();
protected:
bool isComponentComplete() const;
@@ -141,6 +146,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickApplicationWindowAttached : public
Q_PROPERTY(QQuickItem *header READ header NOTIFY headerChanged FINAL)
Q_PROPERTY(QQuickItem *footer READ footer NOTIFY footerChanged FINAL)
Q_PROPERTY(QQuickOverlay *overlay READ overlay NOTIFY overlayChanged FINAL)
+ Q_PROPERTY(QQuickItem *menuBar READ menuBar NOTIFY menuBarChanged FINAL /*REVISION 3*/)
public:
explicit QQuickApplicationWindowAttached(QObject *parent = nullptr);
@@ -151,6 +157,7 @@ public:
QQuickItem *header() const;
QQuickItem *footer() const;
QQuickOverlay *overlay() const;
+ QQuickItem *menuBar() const;
Q_SIGNALS:
void windowChanged();
@@ -159,6 +166,7 @@ Q_SIGNALS:
void headerChanged();
void footerChanged();
void overlayChanged();
+ /*Q_REVISION(3)*/ void menuBarChanged();
private:
Q_DISABLE_COPY(QQuickApplicationWindowAttached)
diff --git a/tests/auto/applicationwindow/data/layout.qml b/tests/auto/applicationwindow/data/layout.qml
index a80b2d48..5bcffdf1 100644
--- a/tests/auto/applicationwindow/data/layout.qml
+++ b/tests/auto/applicationwindow/data/layout.qml
@@ -49,13 +49,14 @@
****************************************************************************/
import QtQuick 2.6
-import QtQuick.Controls 2.0
+import QtQuick.Controls 2.3
ApplicationWindow {
width: 200
height: 200
visible: true
+ menuBar: MenuBar { }
header: ToolBar { }
footer: ToolBar { }
}
diff --git a/tests/auto/applicationwindow/tst_applicationwindow.cpp b/tests/auto/applicationwindow/tst_applicationwindow.cpp
index dd50caf0..81cd9886 100644
--- a/tests/auto/applicationwindow/tst_applicationwindow.cpp
+++ b/tests/auto/applicationwindow/tst_applicationwindow.cpp
@@ -827,11 +827,18 @@ void tst_applicationwindow::layout()
QQuickItem *content = window->contentItem();
QVERIFY(content);
+ QQuickItem *menuBar = window->menuBar();
+ QVERIFY(menuBar);
QQuickItem *header = window->header();
QVERIFY(header);
QQuickItem *footer = window->footer();
QVERIFY(footer);
+ QCOMPARE(menuBar->x(), 0.0);
+ QCOMPARE(menuBar->y(), -menuBar->height() - header->height());
+ QCOMPARE(header->width(), qreal(window->width()));
+ QVERIFY(menuBar->height() > 0);
+
QCOMPARE(header->x(), 0.0);
QCOMPARE(header->y(), -header->height());
QCOMPARE(header->width(), qreal(window->width()));
@@ -843,6 +850,12 @@ void tst_applicationwindow::layout()
QVERIFY(footer->height() > 0.0);
QCOMPARE(content->x(), 0.0);
+ QCOMPARE(content->y(), menuBar->height() + header->height());
+ QCOMPARE(content->width(), qreal(window->width()));
+ QCOMPARE(content->height(), window->height() - menuBar->height() - header->height() - footer->height());
+
+ menuBar->setVisible(false);
+ QCOMPARE(content->x(), 0.0);
QCOMPARE(content->y(), header->height());
QCOMPARE(content->width(), qreal(window->width()));
QCOMPARE(content->height(), window->height() - header->height() - footer->height());