aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquicktabbar.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quicktemplates2/qquicktabbar.cpp')
-rw-r--r--src/quicktemplates2/qquicktabbar.cpp161
1 files changed, 62 insertions, 99 deletions
diff --git a/src/quicktemplates2/qquicktabbar.cpp b/src/quicktemplates2/qquicktabbar.cpp
index 2d732152..745023c3 100644
--- a/src/quicktemplates2/qquicktabbar.cpp
+++ b/src/quicktemplates2/qquicktabbar.cpp
@@ -104,15 +104,14 @@ public:
void updateCurrentIndex();
void updateLayout();
+ qreal getContentWidth() const override;
+ qreal getContentHeight() const override;
+
void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff) override;
void itemImplicitWidthChanged(QQuickItem *item) override;
void itemImplicitHeightChanged(QQuickItem *item) override;
bool updatingLayout = false;
- bool hasContentWidth = false;
- bool hasContentHeight = false;
- qreal contentWidth = 0;
- qreal contentHeight = 0;
QQuickTabBar::Position position = QQuickTabBar::Header;
};
@@ -154,8 +153,6 @@ void QQuickTabBarPrivate::updateLayout()
if (count <= 0 || !contentItem)
return;
- qreal maxHeight = 0;
- qreal totalWidth = 0;
qreal reservedWidth = 0;
int resizableCount = 0;
@@ -166,21 +163,15 @@ void QQuickTabBarPrivate::updateLayout()
QQuickItem *item = q->itemAt(i);
if (item) {
QQuickItemPrivate *p = QQuickItemPrivate::get(item);
- if (!p->widthValid) {
+ if (!p->widthValid)
++resizableCount;
- totalWidth += item->implicitWidth();
- } else {
+ else
reservedWidth += item->width();
- totalWidth += item->width();
- }
- maxHeight = qMax(maxHeight, item->implicitHeight());
allItems += item;
}
}
const qreal totalSpacing = qMax(0, count - 1) * spacing;
- totalWidth += totalSpacing;
-
const qreal itemWidth = (contentItem->width() - reservedWidth - totalSpacing) / qMax(1, resizableCount);
updatingLayout = true;
@@ -191,48 +182,68 @@ void QQuickTabBarPrivate::updateLayout()
p->widthValid = false;
}
if (!p->heightValid) {
- item->setHeight(hasContentHeight ? contentHeight : maxHeight);
+ item->setHeight(contentHeight);
p->heightValid = false;
} else {
- item->setY((maxHeight - item->height()) / 2);
+ item->setY((contentHeight - item->height()) / 2);
}
}
updatingLayout = false;
+}
- bool contentWidthChange = false;
- if (!hasContentWidth && !qFuzzyCompare(contentWidth, totalWidth)) {
- contentWidth = totalWidth;
- contentWidthChange = true;
+qreal QQuickTabBarPrivate::getContentWidth() const
+{
+ Q_Q(const QQuickTabBar);
+ const int count = contentModel->count();
+ qreal totalWidth = qMax(0, count - 1) * spacing;
+ for (int i = 0; i < count; ++i) {
+ QQuickItem *item = q->itemAt(i);
+ if (item) {
+ QQuickItemPrivate *p = QQuickItemPrivate::get(item);
+ if (!p->widthValid)
+ totalWidth += item->implicitWidth();
+ else
+ totalWidth += item->width();
+ }
}
+ return totalWidth;
+}
- bool contentHeightChange = false;
- if (!hasContentHeight && !qFuzzyCompare(contentHeight, maxHeight)) {
- contentHeight = maxHeight;
- contentHeightChange = true;
+qreal QQuickTabBarPrivate::getContentHeight() const
+{
+ Q_Q(const QQuickTabBar);
+ const int count = contentModel->count();
+ qreal maxHeight = 0;
+ for (int i = 0; i < count; ++i) {
+ QQuickItem *item = q->itemAt(i);
+ if (item)
+ maxHeight = qMax(maxHeight, item->implicitHeight());
}
-
- if (contentWidthChange)
- emit q->contentWidthChanged();
- if (contentHeightChange)
- emit q->contentHeightChanged();
+ return maxHeight;
}
-void QQuickTabBarPrivate::itemGeometryChanged(QQuickItem *, QQuickGeometryChange, const QRectF &)
+void QQuickTabBarPrivate::itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &diff)
{
- if (!updatingLayout)
+ QQuickContainerPrivate::itemGeometryChanged(item, change, diff);
+ if (!updatingLayout) {
+ if (change.sizeChange())
+ updateImplicitContentSize();
updateLayout();
+ }
}
-void QQuickTabBarPrivate::itemImplicitWidthChanged(QQuickItem *)
+void QQuickTabBarPrivate::itemImplicitWidthChanged(QQuickItem *item)
{
- if (!updatingLayout && !hasContentWidth)
- updateLayout();
+ QQuickContainerPrivate::itemImplicitWidthChanged(item);
+ if (item != contentItem)
+ updateImplicitContentWidth();
}
-void QQuickTabBarPrivate::itemImplicitHeightChanged(QQuickItem *)
+void QQuickTabBarPrivate::itemImplicitHeightChanged(QQuickItem *item)
{
- if (!updatingLayout && !hasContentHeight)
- updateLayout();
+ QQuickContainerPrivate::itemImplicitHeightChanged(item);
+ if (item != contentItem)
+ updateImplicitContentHeight();
}
QQuickTabBar::QQuickTabBar(QQuickItem *parent)
@@ -283,39 +294,11 @@ void QQuickTabBar::setPosition(Position position)
This property holds the content width. It is used for calculating the total
implicit width of the tab bar.
- Unless explicitly overridden, the content width is automatically calculated
- based on the total implicit width of the tabs and the \l {Control::}{spacing}
- of the tab bar.
+ \note This property is available in TabBar since QtQuick.Controls 2.2 (Qt 5.9),
+ but it was promoted to the Container base type in QtQuick.Controls 2.5 (Qt 5.12).
- \sa contentHeight
+ \sa Container::contentWidth
*/
-qreal QQuickTabBar::contentWidth() const
-{
- Q_D(const QQuickTabBar);
- return d->contentWidth;
-}
-
-void QQuickTabBar::setContentWidth(qreal width)
-{
- Q_D(QQuickTabBar);
- d->hasContentWidth = true;
- if (qFuzzyCompare(d->contentWidth, width))
- return;
-
- d->contentWidth = width;
- emit contentWidthChanged();
-}
-
-void QQuickTabBar::resetContentWidth()
-{
- Q_D(QQuickTabBar);
- if (!d->hasContentWidth)
- return;
-
- d->hasContentWidth = false;
- if (isComponentComplete())
- d->updateLayout();
-}
/*!
\since QtQuick.Controls 2.2 (Qt 5.9)
@@ -324,38 +307,11 @@ void QQuickTabBar::resetContentWidth()
This property holds the content height. It is used for calculating the total
implicit height of the tab bar.
- Unless explicitly overridden, the content height is automatically calculated
- based on the maximum implicit height of the tabs.
+ \note This property is available in TabBar since QtQuick.Controls 2.2 (Qt 5.9),
+ but it was promoted to the Container base type in QtQuick.Controls 2.5 (Qt 5.12).
- \sa contentWidth
+ \sa Container::contentHeight
*/
-qreal QQuickTabBar::contentHeight() const
-{
- Q_D(const QQuickTabBar);
- return d->contentHeight;
-}
-
-void QQuickTabBar::setContentHeight(qreal height)
-{
- Q_D(QQuickTabBar);
- d->hasContentHeight = true;
- if (qFuzzyCompare(d->contentHeight, height))
- return;
-
- d->contentHeight = height;
- emit contentHeightChanged();
-}
-
-void QQuickTabBar::resetContentHeight()
-{
- Q_D(QQuickTabBar);
- if (!d->hasContentHeight)
- return;
-
- d->hasContentHeight = false;
- if (isComponentComplete())
- d->updateLayout();
-}
QQuickTabBarAttached *QQuickTabBar::qmlAttachedProperties(QObject *object)
{
@@ -399,6 +355,7 @@ void QQuickTabBar::itemAdded(int index, QQuickItem *item)
QQuickTabBarAttached *attached = qobject_cast<QQuickTabBarAttached *>(qmlAttachedPropertiesObject<QQuickTabBar>(item));
if (attached)
QQuickTabBarAttachedPrivate::get(attached)->update(this, index);
+ d->updateImplicitContentSize();
if (isComponentComplete())
polish();
}
@@ -419,13 +376,19 @@ void QQuickTabBar::itemRemoved(int index, QQuickItem *item)
QQuickTabBarAttached *attached = qobject_cast<QQuickTabBarAttached *>(qmlAttachedPropertiesObject<QQuickTabBar>(item));
if (attached)
QQuickTabBarAttachedPrivate::get(attached)->update(nullptr, -1);
+ d->updateImplicitContentSize();
if (isComponentComplete())
polish();
}
+QFont QQuickTabBar::defaultFont() const
+{
+ return QQuickTheme::font(QQuickTheme::TabBar);
+}
+
QPalette QQuickTabBar::defaultPalette() const
{
- return QQuickControlPrivate::themePalette(QPlatformTheme::TabBarPalette);
+ return QQuickTheme::palette(QQuickTheme::TabBar);
}
#if QT_CONFIG(accessibility)