aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickpopup.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quicktemplates2/qquickpopup.cpp')
-rw-r--r--src/quicktemplates2/qquickpopup.cpp419
1 files changed, 378 insertions, 41 deletions
diff --git a/src/quicktemplates2/qquickpopup.cpp b/src/quicktemplates2/qquickpopup.cpp
index b69dab64..d5e2c940 100644
--- a/src/quicktemplates2/qquickpopup.cpp
+++ b/src/quicktemplates2/qquickpopup.cpp
@@ -36,6 +36,7 @@
#include "qquickpopup_p.h"
#include "qquickpopup_p_p.h"
+#include "qquickpopupanchors_p.h"
#include "qquickpopupitem_p_p.h"
#include "qquickpopuppositioner_p_p.h"
#include "qquickapplicationwindow_p.h"
@@ -109,14 +110,42 @@ QT_BEGIN_NAMESPACE
\image qtquickcontrols2-popup.png
The \l implicitWidth and \l implicitHeight of a popup are typically based
- on the implicit sizes of the background and the content item plus any
- \l padding. These properties determine how large the popup will be when no
+ on the implicit sizes of the background and the content item plus any insets
+ and paddings. These properties determine how large the popup will be when no
explicit \l width or \l height is specified.
+ The geometry of the \l contentItem is determined by the padding. The following
+ example reserves 10px padding between the boundaries of the popup and its content:
+
+ \code
+ Popup {
+ padding: 10
+
+ contentItem: Text {
+ text: "Content"
+ }
+ }
+ \endcode
+
The \l background item fills the entire width and height of the popup,
- unless an explicit size has been given for it.
+ unless insets or an explicit size have been given for it.
+
+ Negative insets can be used to make the background larger than the popup.
+ The following example uses negative insets to place a shadow outside the
+ popup's boundaries:
- The geometry of the \l contentItem is determined by the \l padding.
+ \code
+ Popup {
+ topInset: -2
+ leftInset: -2
+ rightInset: -6
+ bottomInset: -6
+
+ background: BorderImage {
+ source: ":/images/shadowed-background.png"
+ }
+ }
+ \endcode
\section1 Popup Sizing
@@ -178,6 +207,11 @@ QT_BEGIN_NAMESPACE
\include qquickoverlay-popup-parent.qdocinc
+ Another way to center a popup in the window regardless of its parent item
+ is to use \l {anchors.centerIn}:
+
+ \snippet qtquickcontrols2-popup.qml centerIn
+
\sa {Popup Controls}, {Customizing Popup}, ApplicationWindow
*/
@@ -230,7 +264,10 @@ void QQuickPopupPrivate::init()
QObject::connect(popupItem, &QQuickControl::paddingChanged, q, &QQuickPopup::paddingChanged);
QObject::connect(popupItem, &QQuickControl::backgroundChanged, q, &QQuickPopup::backgroundChanged);
QObject::connect(popupItem, &QQuickControl::contentItemChanged, q, &QQuickPopup::contentItemChanged);
- positioner = new QQuickPopupPositioner(q);
+ QObject::connect(popupItem, &QQuickControl::implicitContentWidthChanged, q, &QQuickPopup::implicitContentWidthChanged);
+ QObject::connect(popupItem, &QQuickControl::implicitContentHeightChanged, q, &QQuickPopup::implicitContentHeightChanged);
+ QObject::connect(popupItem, &QQuickControl::implicitBackgroundWidthChanged, q, &QQuickPopup::implicitBackgroundWidthChanged);
+ QObject::connect(popupItem, &QQuickControl::implicitBackgroundHeightChanged, q, &QQuickPopup::implicitBackgroundHeightChanged);
}
void QQuickPopupPrivate::closeOrReject()
@@ -400,7 +437,7 @@ bool QQuickPopupPrivate::prepareEnterTransition()
visible = true;
transitionState = EnterTransition;
popupItem->setVisible(true);
- positioner->setParentItem(parentItem);
+ getPositioner()->setParentItem(parentItem);
emit q->visibleChanged();
}
return true;
@@ -439,7 +476,7 @@ void QQuickPopupPrivate::finalizeEnterTransition()
void QQuickPopupPrivate::finalizeExitTransition()
{
Q_Q(QQuickPopup);
- positioner->setParentItem(nullptr);
+ getPositioner()->setParentItem(nullptr);
popupItem->setParentItem(nullptr);
popupItem->setVisible(false);
destroyOverlay();
@@ -525,6 +562,55 @@ void QQuickPopupPrivate::setBottomMargin(qreal value, bool reset)
}
}
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlpropertygroup QtQuick.Controls::Popup::anchors
+ \qmlproperty Object QtQuick.Controls::Popup::anchors.centerIn
+
+ Anchors provide a way to position an item by specifying its
+ relationship with other items.
+
+ A common use case is to center a popup within its parent. One way to do
+ this is with the \l {Item::}{x} and \l {Item::}{y} properties. Anchors offer
+ a more convenient approach:
+
+ \qml
+ Pane {
+ // ...
+
+ Popup {
+ anchors.centerIn: parent
+ }
+ }
+ \endqml
+
+ It is also possible to center the popup in the window by using \l Overlay:
+
+ \snippet qtquickcontrols2-popup.qml centerIn
+
+ This makes it easy to center a popup in the window from any component.
+
+ \note Popups can only be centered within their immediate parent or
+ the window overlay; trying to center in other items will produce a warning.
+
+ \sa {Popup Positioning}, {Item::anchors}
+*/
+QQuickPopupAnchors *QQuickPopupPrivate::getAnchors()
+{
+ Q_Q(QQuickPopup);
+ if (!anchors)
+ anchors = new QQuickPopupAnchors(q);
+ return anchors;
+}
+
+QQuickPopupPositioner *QQuickPopupPrivate::getPositioner()
+{
+ Q_Q(QQuickPopup);
+ if (!positioner)
+ positioner = new QQuickPopupPositioner(q);
+ return positioner;
+}
+
void QQuickPopupPrivate::setWindow(QQuickWindow *newWindow)
{
Q_Q(QQuickPopup);
@@ -566,7 +652,7 @@ void QQuickPopupPrivate::itemDestroyed(QQuickItem *item)
void QQuickPopupPrivate::reposition()
{
- positioner->reposition();
+ getPositioner()->reposition();
}
static QQuickItem *createDimmer(QQmlComponent *component, QQuickPopup *popup, QQuickItem *parent)
@@ -955,17 +1041,13 @@ void QQuickPopup::setImplicitHeight(qreal height)
qreal QQuickPopup::contentWidth() const
{
Q_D(const QQuickPopup);
- return d->contentWidth;
+ return d->popupItem->contentWidth();
}
void QQuickPopup::setContentWidth(qreal width)
{
Q_D(QQuickPopup);
- if (qFuzzyCompare(d->contentWidth, width))
- return;
-
- d->contentWidth = width;
- emit contentWidthChanged();
+ d->popupItem->setContentWidth(width);
}
/*!
@@ -981,17 +1063,13 @@ void QQuickPopup::setContentWidth(qreal width)
qreal QQuickPopup::contentHeight() const
{
Q_D(const QQuickPopup);
- return d->contentHeight;
+ return d->popupItem->contentHeight();
}
void QQuickPopup::setContentHeight(qreal height)
{
Q_D(QQuickPopup);
- if (qFuzzyCompare(d->contentHeight, height))
- return;
-
- d->contentHeight = height;
- emit contentHeightChanged();
+ d->popupItem->setContentHeight(height);
}
/*!
@@ -1249,11 +1327,12 @@ void QQuickPopup::resetPadding()
/*!
\qmlproperty real QtQuick.Controls::Popup::topPadding
- This property holds the top padding.
+ This property holds the top padding. Unless explicitly set, the value
+ is equal to \c verticalPadding.
\include qquickpopup-padding.qdocinc
- \sa padding, bottomPadding, availableHeight
+ \sa padding, bottomPadding, verticalPadding, availableHeight
*/
qreal QQuickPopup::topPadding() const
{
@@ -1276,11 +1355,12 @@ void QQuickPopup::resetTopPadding()
/*!
\qmlproperty real QtQuick.Controls::Popup::leftPadding
- This property holds the left padding.
+ This property holds the left padding. Unless explicitly set, the value
+ is equal to \c horizontalPadding.
\include qquickpopup-padding.qdocinc
- \sa padding, rightPadding, availableWidth
+ \sa padding, rightPadding, horizontalPadding, availableWidth
*/
qreal QQuickPopup::leftPadding() const
{
@@ -1303,11 +1383,12 @@ void QQuickPopup::resetLeftPadding()
/*!
\qmlproperty real QtQuick.Controls::Popup::rightPadding
- This property holds the right padding.
+ This property holds the right padding. Unless explicitly set, the value
+ is equal to \c horizontalPadding.
\include qquickpopup-padding.qdocinc
- \sa padding, leftPadding, availableWidth
+ \sa padding, leftPadding, horizontalPadding, availableWidth
*/
qreal QQuickPopup::rightPadding() const
{
@@ -1330,11 +1411,12 @@ void QQuickPopup::resetRightPadding()
/*!
\qmlproperty real QtQuick.Controls::Popup::bottomPadding
- This property holds the bottom padding.
+ This property holds the bottom padding. Unless explicitly set, the value
+ is equal to \c verticalPadding.
\include qquickpopup-padding.qdocinc
- \sa padding, topPadding, availableHeight
+ \sa padding, topPadding, verticalPadding, availableHeight
*/
qreal QQuickPopup::bottomPadding() const
{
@@ -1524,8 +1606,9 @@ void QQuickPopup::setParentItem(QQuickItem *parent)
QQuickItemPrivate::get(d->parentItem)->removeItemChangeListener(d, QQuickItemPrivate::Destroyed);
}
d->parentItem = parent;
- if (d->positioner->parentItem())
- d->positioner->setParentItem(parent);
+ QQuickPopupPositioner *positioner = d->getPositioner();
+ if (positioner->parentItem())
+ positioner->setParentItem(parent);
if (parent) {
QObjectPrivate::connect(parent, &QQuickItem::windowChanged, d, &QQuickPopupPrivate::setWindow);
QQuickItemPrivate::get(d->parentItem)->addItemChangeListener(d, QQuickItemPrivate::Destroyed);
@@ -1620,13 +1703,12 @@ void QQuickPopup::setContentItem(QQuickItem *item)
\sa Item::data, contentChildren
*/
-QQmlListProperty<QObject> QQuickPopup::contentData()
+QQmlListProperty<QObject> QQuickPopupPrivate::contentData()
{
- Q_D(QQuickPopup);
- QQuickControlPrivate *p = QQuickControlPrivate::get(d->popupItem);
+ QQuickControlPrivate *p = QQuickControlPrivate::get(popupItem);
if (!p->contentItem)
p->executeContentItem();
- return QQmlListProperty<QObject>(d->popupItem->contentItem(), nullptr,
+ return QQmlListProperty<QObject>(popupItem->contentItem(), nullptr,
QQuickItemPrivate::data_append,
QQuickItemPrivate::data_count,
QQuickItemPrivate::data_at,
@@ -1646,10 +1728,9 @@ QQmlListProperty<QObject> QQuickPopup::contentData()
\sa Item::children, contentData
*/
-QQmlListProperty<QQuickItem> QQuickPopup::contentChildren()
+QQmlListProperty<QQuickItem> QQuickPopupPrivate::contentChildren()
{
- Q_D(QQuickPopup);
- return QQmlListProperty<QQuickItem>(d->popupItem->contentItem(), nullptr,
+ return QQmlListProperty<QQuickItem>(popupItem->contentItem(), nullptr,
QQuickItemPrivate::children_append,
QQuickItemPrivate::children_count,
QQuickItemPrivate::children_at,
@@ -2042,6 +2123,236 @@ void QQuickPopup::setExit(QQuickTransition *transition)
emit exitChanged();
}
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::horizontalPadding
+
+ This property holds the horizontal padding. Unless explicitly set, the value
+ is equal to \c padding.
+
+ \include qquickpopup-padding.qdocinc
+
+ \sa padding, leftPadding, rightPadding, verticalPadding
+*/
+qreal QQuickPopup::horizontalPadding() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->horizontalPadding();
+}
+
+void QQuickPopup::setHorizontalPadding(qreal padding)
+{
+ Q_D(QQuickPopup);
+ d->popupItem->setHorizontalPadding(padding);
+}
+
+void QQuickPopup::resetHorizontalPadding()
+{
+ Q_D(QQuickPopup);
+ d->popupItem->resetHorizontalPadding();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::verticalPadding
+
+ This property holds the vertical padding. Unless explicitly set, the value
+ is equal to \c padding.
+
+ \include qquickpopup-padding.qdocinc
+
+ \sa padding, topPadding, bottomPadding, horizontalPadding
+*/
+qreal QQuickPopup::verticalPadding() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->verticalPadding();
+}
+
+void QQuickPopup::setVerticalPadding(qreal padding)
+{
+ Q_D(QQuickPopup);
+ d->popupItem->setVerticalPadding(padding);
+}
+
+void QQuickPopup::resetVerticalPadding()
+{
+ Q_D(QQuickPopup);
+ d->popupItem->resetVerticalPadding();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::implicitContentWidth
+ \readonly
+
+ This property holds the implicit content width.
+
+ The value is calculated based on the content children.
+
+ \sa implicitContentHeight, implicitBackgroundWidth
+*/
+qreal QQuickPopup::implicitContentWidth() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->implicitContentWidth();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::implicitContentHeight
+ \readonly
+
+ This property holds the implicit content height.
+
+ The value is calculated based on the content children.
+
+ \sa implicitContentWidth, implicitBackgroundHeight
+*/
+qreal QQuickPopup::implicitContentHeight() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->implicitContentHeight();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::implicitBackgroundWidth
+ \readonly
+
+ This property holds the implicit background width.
+
+ The value is equal to \c {background ? background.implicitWidth : 0}.
+
+ \sa implicitBackgroundHeight, implicitContentWidth
+*/
+qreal QQuickPopup::implicitBackgroundWidth() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->implicitBackgroundWidth();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::implicitBackgroundHeight
+ \readonly
+
+ This property holds the implicit background height.
+
+ The value is equal to \c {background ? background.implicitHeight : 0}.
+
+ \sa implicitBackgroundWidth, implicitContentHeight
+*/
+qreal QQuickPopup::implicitBackgroundHeight() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->implicitBackgroundHeight();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::topInset
+
+ This property holds the top inset for the background.
+
+ \sa {Popup Layout}, bottomInset
+*/
+qreal QQuickPopup::topInset() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->topInset();
+}
+
+void QQuickPopup::setTopInset(qreal inset)
+{
+ Q_D(QQuickPopup);
+ d->popupItem->setTopInset(inset);
+}
+
+void QQuickPopup::resetTopInset()
+{
+ Q_D(QQuickPopup);
+ d->popupItem->resetTopInset();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::leftInset
+
+ This property holds the left inset for the background.
+
+ \sa {Popup Layout}, rightInset
+*/
+qreal QQuickPopup::leftInset() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->leftInset();
+}
+
+void QQuickPopup::setLeftInset(qreal inset)
+{
+ Q_D(QQuickPopup);
+ d->popupItem->setLeftInset(inset);
+}
+
+void QQuickPopup::resetLeftInset()
+{
+ Q_D(QQuickPopup);
+ d->popupItem->resetLeftInset();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::rightInset
+
+ This property holds the right inset for the background.
+
+ \sa {Popup Layout}, leftInset
+*/
+qreal QQuickPopup::rightInset() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->rightInset();
+}
+
+void QQuickPopup::setRightInset(qreal inset)
+{
+ Q_D(QQuickPopup);
+ d->popupItem->setRightInset(inset);
+}
+
+void QQuickPopup::resetRightInset()
+{
+ Q_D(QQuickPopup);
+ d->popupItem->resetRightInset();
+}
+
+/*!
+ \since QtQuick.Controls 2.5 (Qt 5.12)
+ \qmlproperty real QtQuick.Controls::Popup::bottomInset
+
+ This property holds the bottom inset for the background.
+
+ \sa {Popup Layout}, topInset
+*/
+qreal QQuickPopup::bottomInset() const
+{
+ Q_D(const QQuickPopup);
+ return d->popupItem->bottomInset();
+}
+
+void QQuickPopup::setBottomInset(qreal inset)
+{
+ Q_D(QQuickPopup);
+ d->popupItem->setBottomInset(inset);
+}
+
+void QQuickPopup::resetBottomInset()
+{
+ Q_D(QQuickPopup);
+ d->popupItem->resetBottomInset();
+}
+
bool QQuickPopup::filtersChildMouseEvents() const
{
Q_D(const QQuickPopup);
@@ -2217,6 +2528,14 @@ void QQuickPopup::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
Q_UNUSED(oldItem);
}
+void QQuickPopup::contentSizeChange(const QSizeF &newSize, const QSizeF &oldSize)
+{
+ if (!qFuzzyCompare(newSize.width(), oldSize.width()))
+ emit contentWidthChanged();
+ if (!qFuzzyCompare(newSize.height(), oldSize.height()))
+ emit contentHeightChanged();
+}
+
void QQuickPopup::fontChange(const QFont &newFont, const QFont &oldFont)
{
Q_UNUSED(newFont);
@@ -2292,10 +2611,14 @@ void QQuickPopup::paddingChange(const QMarginsF &newPadding, const QMarginsF &ol
if (bp)
emit bottomPaddingChanged();
- if (lp || rp)
+ if (lp || rp) {
+ emit horizontalPaddingChanged();
emit availableWidthChanged();
- if (tp || bp)
+ }
+ if (tp || bp) {
+ emit verticalPaddingChanged();
emit availableHeightChanged();
+ }
}
void QQuickPopup::paletteChange(const QPalette &newPalette, const QPalette &oldPalette)
@@ -2312,14 +2635,26 @@ void QQuickPopup::spacingChange(qreal newSpacing, qreal oldSpacing)
emit spacingChanged();
}
+void QQuickPopup::insetChange(const QMarginsF &newInset, const QMarginsF &oldInset)
+{
+ if (!qFuzzyCompare(newInset.top(), oldInset.top()))
+ emit topInsetChanged();
+ if (!qFuzzyCompare(newInset.left(), oldInset.left()))
+ emit leftInsetChanged();
+ if (!qFuzzyCompare(newInset.right(), oldInset.right()))
+ emit rightInsetChanged();
+ if (!qFuzzyCompare(newInset.bottom(), oldInset.bottom()))
+ emit bottomInsetChanged();
+}
+
QFont QQuickPopup::defaultFont() const
{
- return QQuickControlPrivate::themeFont(QPlatformTheme::SystemFont);
+ return QQuickTheme::font(QQuickTheme::System);
}
QPalette QQuickPopup::defaultPalette() const
{
- return QQuickControlPrivate::themePalette(QPlatformTheme::SystemPalette);
+ return QQuickTheme::palette(QQuickTheme::System);
}
#if QT_CONFIG(accessibility)
@@ -2359,3 +2694,5 @@ bool QQuickPopup::setAccessibleProperty(const char *propertyName, const QVariant
}
QT_END_NAMESPACE
+
+#include "moc_qquickpopup_p.cpp"