Commit a6be1a3f authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Add FormContainer and StackContainer from v2.0

parent 1756d7c3
Pipeline #987 passed with stage
in 17 seconds
#include "QtQmlTricks.h"
#include "QQmlContainerEnums.h"
#include "QQmlFastObjectListModel.h"
#include "QQmlObjectListModel.h"
#include "QQuickColumnContainer.h"
#include "QQuickContainerAttachedObject.h"
#include "QQuickExtraAnchors.h"
#include "QQuickFastObjectListView.h"
#include "QQuickFormContainer.h"
#include "QQuickGridContainer.h"
#include "QQuickRowContainer.h"
#include "QQuickFastObjectListView.h"
#include "QQmlFastObjectListModel.h"
#include "QQmlContainerEnums.h"
#include "QQmlObjectListModel.h"
#include "QQuickStackContainer.h"
#include <qqml.h>
void QtQmlTricks::registerComponents (void) {
static const QString ERR_ENUM_CLASS { QStringLiteral ("Enum-class !") };
static const QString ERR_ATTACHED_OBJ { QStringLiteral ("Attached-object class !") };
static const QString ERR_ABSTRACT_BASE { QStringLiteral ("Abstract base class !") };
qmlRegisterType<QQuickColumnContainer> ("QtQmlTricks", 3, 0, "ColumnContainer");
qmlRegisterType<QQuickGridContainer> ("QtQmlTricks", 3, 0, "GridContainer");
qmlRegisterType<QQuickStackContainer> ("QtQmlTricks", 3, 0, "StackContainer");
qmlRegisterType<QQuickFormContainer> ("QtQmlTricks", 3, 0, "FormContainer");
qmlRegisterType<QQuickRowContainer> ("QtQmlTricks", 3, 0, "RowContainer");
qmlRegisterType<QQuickFastObjectListView> ("QtQmlTricks", 3, 0, "FastObjectListView");
qmlRegisterUncreatableType<VerticalDirections> ("QtQmlTricks", 3, 0, "VerticalDirections", "Enum-class !");
qmlRegisterUncreatableType<HorizontalDirections> ("QtQmlTricks", 3, 0, "HorizontalDirections", "Enum-class !");
qmlRegisterUncreatableType<FlowDirections> ("QtQmlTricks", 3, 0, "FlowDirections", "Enum-class !");
qmlRegisterUncreatableType<QQuickExtraAnchors> ("QtQmlTricks", 3, 0, "ExtraAnchors", "Attached-object class !");
qmlRegisterUncreatableType<QQuickContainerAttachedObject> ("QtQmlTricks", 3, 0, "Container", "Attached-object class !");
qmlRegisterUncreatableType<QQmlObjectListModelBase> ("QtQmlTricks", 3, 0, "ObjectListModel", "Abstract base class !");
qmlRegisterUncreatableType<QQmlFastObjectListModelBase> ("QtQmlTricks", 3, 0, "FastObjectListModel", "Abstract base class !");
qmlRegisterUncreatableType<VerticalDirections> ("QtQmlTricks", 3, 0, "VerticalDirections", ERR_ENUM_CLASS);
qmlRegisterUncreatableType<HorizontalDirections> ("QtQmlTricks", 3, 0, "HorizontalDirections", ERR_ENUM_CLASS);
qmlRegisterUncreatableType<FlowDirections> ("QtQmlTricks", 3, 0, "FlowDirections", ERR_ENUM_CLASS);
qmlRegisterUncreatableType<QQuickExtraAnchors> ("QtQmlTricks", 3, 0, "ExtraAnchors", ERR_ATTACHED_OBJ);
qmlRegisterUncreatableType<QQuickContainerAttachedObject> ("QtQmlTricks", 3, 0, "Container", ERR_ATTACHED_OBJ);
qmlRegisterUncreatableType<QQmlObjectListModelBase> ("QtQmlTricks", 3, 0, "ObjectListModel", ERR_ABSTRACT_BASE);
qmlRegisterUncreatableType<QQmlFastObjectListModelBase> ("QtQmlTricks", 3, 0, "FastObjectListModel", ERR_ABSTRACT_BASE);
}
#include "QQuickFormContainer.h"
#include "QQuickContainerAttachedObject.h"
#include <QtMath>
struct Line {
QQuickItem * label { Q_NULLPTR };
QQuickItem * field { Q_NULLPTR };
int forcedW { 0 };
int forcedH { 0 };
int computedH { 0 };
bool stretched { false };
};
QQuickFormContainer::QQuickFormContainer (QQuickItem * parent)
: QQuickAbstractContainerBase { parent }
, m_verticalSpacing { 0 }
, m_horizontalSpacing { 0 }
{ }
void QQuickFormContainer::setupHandlers (void) {
connect (this, &QQuickFormContainer::verticalSpacingChanged, this, &QQuickFormContainer::doLayout);
connect (this, &QQuickFormContainer::horizontalSpacingChanged, this, &QQuickFormContainer::doLayout);
}
void QQuickFormContainer::relayout (void) {
const QList<QQuickItem *> childItemsList { childItems () };
int itemIdx { 0 };
int totalItemsHeight { 0 };
int maxLeftImplicitWidth { 0 };
int maxRightImplicitWidth { 0 };
QVector<Line> layoutLines { };
layoutLines.reserve (childItemsList.count ());
for (QQuickItem * childItem : childItemsList) {
if (!childItem->inherits (REPEATER_CLASSNAME)) {
const QQuickContainerAttachedObject * attached { getContainerAttachedObject (childItem) };
const bool ignored { (attached != Q_NULLPTR && attached->get_ignored ()) };
if (!ignored && childItem->isVisible ()) {
if (itemIdx % 2 == 0) {
layoutLines.append (Line { });
layoutLines.last ().label = childItem;
if (maxLeftImplicitWidth < qCeil (childItem->implicitWidth ())) {
maxLeftImplicitWidth = qCeil (childItem->implicitWidth ());
}
}
else {
layoutLines.last ().field = childItem;
layoutLines.last ().stretched = (attached != Q_NULLPTR && attached->get_horizontalStretch () > 0);
layoutLines.last ().forcedW = (attached != Q_NULLPTR ? attached->get_forcedWidth () : 0);
layoutLines.last ().forcedH = (attached != Q_NULLPTR ? attached->get_forcedHeight () : 0);
layoutLines.last ().computedH = qMax (qCeil (layoutLines.last ().label->implicitHeight ()),
qCeil (layoutLines.last ().field->implicitHeight ()));
totalItemsHeight += layoutLines.last ().computedH;
if (maxRightImplicitWidth < qCeil (childItem->implicitWidth ())) {
maxRightImplicitWidth = qCeil (childItem->implicitWidth ());
}
}
++itemIdx;
}
}
}
if (!layoutLines.isEmpty () && layoutLines.last ().field != Q_NULLPTR) {
const int totalVerticalSpacing { (layoutLines.count () > 1 ? ((layoutLines.count () -1) * m_verticalSpacing) : 0) };
setImplicitWidth (maxLeftImplicitWidth + maxRightImplicitWidth + m_horizontalSpacing);
setImplicitHeight (totalItemsHeight + totalVerticalSpacing);
const int strechedRightItemsWidth { (qFloor (width ()) - maxLeftImplicitWidth - m_horizontalSpacing) };
int currentY { 0 };
for (const Line & line : layoutLines) {
line.label->setX (0);
line.label->setY (currentY);
line.label->setWidth (maxLeftImplicitWidth);
line.label->setHeight (line.computedH);
line.field->setX (maxLeftImplicitWidth + m_horizontalSpacing);
line.field->setY (currentY);
line.field->setHeight (line.computedH);
if (line.stretched) {
line.field->setWidth (strechedRightItemsWidth);
}
else if (line.forcedW > 0) {
line.field->setWidth (line.forcedW);
}
else {
line.field->setWidth (line.field->implicitWidth ());
}
currentY += line.computedH;
currentY += m_verticalSpacing;
}
set_layoutItemsCount (layoutLines.count () * 2);
}
else {
setImplicitWidth (0);
setImplicitHeight (0);
set_layoutItemsCount (0);
}
}
#ifndef QQUICKFORMCONTAINER_H
#define QQUICKFORMCONTAINER_H
#include "QQuickAbstractContainerBase.h"
class QQuickFormContainer : public QQuickAbstractContainerBase {
Q_OBJECT
QML_WRITABLE_VAR_PROPERTY (verticalSpacing, int)
QML_WRITABLE_VAR_PROPERTY (horizontalSpacing, int)
// COMPATIBILITY ALIASES
Q_PROPERTY (int colSpacing READ get_horizontalSpacing WRITE set_horizontalSpacing NOTIFY horizontalSpacingChanged)
Q_PROPERTY (int rowSpacing READ get_verticalSpacing WRITE set_verticalSpacing NOTIFY verticalSpacingChanged)
public:
explicit QQuickFormContainer (QQuickItem * parent = Q_NULLPTR);
protected:
void setupHandlers (void) Q_DECL_FINAL;
void relayout (void) Q_DECL_FINAL;
};
#endif // QQUICKFORMCONTAINER_H
......@@ -38,7 +38,7 @@ void QQuickGridContainer::relayout (void) {
if (!childItem->inherits (REPEATER_CLASSNAME)) {
if (childItem->isVisible () || !m_fillEmpty) {
const QQuickContainerAttachedObject * attached { getContainerAttachedObject (childItem) };
const bool ignored { (attached && attached->get_ignored ()) };
const bool ignored { (attached != Q_NULLPTR && attached->get_ignored ()) };
if (!ignored) {
layoutItemsList.append (childItem);
if (qCeil (childItem->implicitWidth ()) > maxChildWidth) {
......
#include "QQuickStackContainer.h"
#include <QtMath>
QQuickStackContainer::QQuickStackContainer (QQuickItem * parent)
: QQuickItem { parent }
{ }
void QQuickStackContainer::classBegin (void) {
QQuickItem::classBegin ();
}
void QQuickStackContainer::componentComplete (void) {
QQuickItem::componentComplete ();
connect (this, &QQuickStackContainer::visibleChanged, this, &QQuickStackContainer::polish);
connect (this, &QQuickStackContainer::widthChanged, this, &QQuickStackContainer::polish);
connect (this, &QQuickStackContainer::heightChanged, this, &QQuickStackContainer::polish);
polish ();
}
void QQuickStackContainer::updatePolish (void) {
QQuickItem::updatePolish ();
int maxChildWidth { 0 }, maxChildHeight { 0 };
const QList<QQuickItem *> childrenList { childItems () };
for (QQuickItem * childItem : childrenList) {
if (!childItem->inherits ("QQuickRepeater")) {
if (qCeil (childItem->implicitWidth ()) > maxChildWidth) {
maxChildWidth = qCeil (childItem->implicitWidth ());
}
if (qCeil (childItem->implicitHeight ()) > maxChildHeight) {
maxChildHeight = qCeil (childItem->implicitHeight ());
}
}
}
setImplicitWidth (maxChildWidth);
setImplicitHeight (maxChildHeight);
}
void QQuickStackContainer::itemChange (QQuickItem::ItemChange change, const QQuickItem::ItemChangeData & value) {
QQuickItem::itemChange (change, value);
switch (int (change)) {
case ItemChildAddedChange: {
if (QQuickItem * child { value.item }) {
connect (child, &QQuickItem::visibleChanged, this, &QQuickStackContainer::polish, Qt::UniqueConnection);
connect (child, &QQuickItem::implicitWidthChanged, this, &QQuickStackContainer::polish, Qt::UniqueConnection);
connect (child, &QQuickItem::implicitHeightChanged, this, &QQuickStackContainer::polish, Qt::UniqueConnection);
polish ();
}
break;
}
case ItemChildRemovedChange: {
if (QQuickItem * child { value.item }) {
disconnect (child, Q_NULLPTR, this, Q_NULLPTR);
polish ();
}
break;
}
}
}
#ifndef QQUICKSTACKCONTAINER_H
#define QQUICKSTACKCONTAINER_H
#include <QQuickItem>
#include "QmlPropertyHelpers.h"
class QQuickStackContainer : public QQuickItem {
Q_OBJECT
public:
explicit QQuickStackContainer (QQuickItem * parent = Q_NULLPTR);
protected:
void classBegin (void) Q_DECL_FINAL;
void componentComplete (void) Q_DECL_FINAL;
void updatePolish (void) Q_DECL_FINAL;
void itemChange (ItemChange change, const ItemChangeData & value) Q_DECL_FINAL;
};
#endif // QQUICKSTACKCONTAINER_H
QT += core gui qml quick
QT += core gui qml quick svg
INCLUDEPATH += \
$$PWD \
......@@ -21,7 +21,9 @@ HEADERS += \
$$PWD/gui/containers/QQuickGridContainer.h \
$$PWD/gui/containers/QQuickRowContainer.h \
$$PWD/gui/helpers/QQuickExtraAnchors.h \
$$PWD/QtQmlTricks.h
$$PWD/QtQmlTricks.h \
$$PWD/gui/containers/QQuickFormContainer.h \
$$PWD/gui/containers/QQuickStackContainer.h
SOURCES += \
$$PWD/gui/containers/QQuickAbstractContainerBase.cpp \
......@@ -31,4 +33,6 @@ SOURCES += \
$$PWD/gui/containers/QQuickGridContainer.cpp \
$$PWD/gui/containers/QQuickRowContainer.cpp \
$$PWD/gui/helpers/QQuickExtraAnchors.cpp \
$$PWD/QtQmlTricks.cpp
$$PWD/QtQmlTricks.cpp \
$$PWD/gui/containers/QQuickFormContainer.cpp \
$$PWD/gui/containers/QQuickStackContainer.cpp
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment