diff options
author | Friedemann Kleint <Friedemann.Kleint@nokia.com> | 2010-10-22 14:10:44 (GMT) |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@nokia.com> | 2010-10-22 14:10:44 (GMT) |
commit | 2231e4f03c621459e1c261b2ed224a49877d2ddf (patch) | |
tree | 93abd9fbf65a69c052b53170c0628d6dd9345034 /tools/designer/src/lib/uilib | |
parent | 03c1445ed4be734a82cea59d107c51a4be43c5f4 (diff) | |
download | Qt-2231e4f03c621459e1c261b2ed224a49877d2ddf.zip Qt-2231e4f03c621459e1c261b2ed224a49877d2ddf.tar.gz Qt-2231e4f03c621459e1c261b2ed224a49877d2ddf.tar.bz2 |
Designer: Support alignment in box and grid layouts.
Extend ui-format by alignment-attribute for QLayoutItem,
handle it in uic and formbuilder.
Support in Designer by context menu.
Reviewed-by: Jarek Kobus <jaroslaw.kobus@nokia.com>
Task-number: QTBUG-3120
Diffstat (limited to 'tools/designer/src/lib/uilib')
-rw-r--r-- | tools/designer/src/lib/uilib/abstractformbuilder.cpp | 82 | ||||
-rw-r--r-- | tools/designer/src/lib/uilib/ui4.cpp | 11 | ||||
-rw-r--r-- | tools/designer/src/lib/uilib/ui4_p.h | 8 |
3 files changed, 96 insertions, 5 deletions
diff --git a/tools/designer/src/lib/uilib/abstractformbuilder.cpp b/tools/designer/src/lib/uilib/abstractformbuilder.cpp index 3f40d81..ae6fac9 100644 --- a/tools/designer/src/lib/uilib/abstractformbuilder.cpp +++ b/tools/designer/src/lib/uilib/abstractformbuilder.cpp @@ -794,6 +794,69 @@ static inline QFormLayout::ItemRole formLayoutRole(int column, int colspan) } #endif +static inline QString alignmentValue(Qt::Alignment a) +{ + QString h,v; + switch (a & Qt::AlignHorizontal_Mask) { + case Qt::AlignLeft: + h = QLatin1String("Qt::AlignLeft"); + break; + case Qt::AlignRight: + h = QLatin1String("Qt::AlignRight"); + break; + case Qt::AlignHCenter: + h = QLatin1String("Qt::AlignHCenter"); + break; + case Qt::AlignJustify: + h = QLatin1String("Qt::AlignJustify"); + break; + } + switch (a & Qt::AlignVertical_Mask) { + case Qt::AlignTop: + v = QLatin1String("Qt::AlignTop"); + break; + case Qt::AlignBottom: + v = QLatin1String("Qt::AlignBottom"); + break; + case Qt::AlignVCenter: + v = QLatin1String("Qt::AlignVCenter"); + break; + } + if (h.isEmpty() && v.isEmpty()) + return QString(); + if (!v.isEmpty()) { + if (!h.isEmpty()) + h += QLatin1Char('|'); + h += v; + } + return h; +} + +static inline Qt::Alignment alignmentFromDom(const QString &in) +{ + Qt::Alignment rc = 0; + if (!in.isEmpty()) { + foreach (const QString &f, in.split(QLatin1Char('|'))) { + if (f == QLatin1String("Qt::AlignLeft")) { + rc |= Qt::AlignLeft; + } else if (f == QLatin1String("Qt::AlignRight")) { + rc |= Qt::AlignRight; + } else if (f == QLatin1String("Qt::AlignHCenter")) { + rc |= Qt::AlignHCenter; + } else if (f == QLatin1String("Qt::AlignJustify")) { + rc |= Qt::AlignJustify; + } else if (f == QLatin1String("Qt::AlignTop")) { + rc |= Qt::AlignTop; + } else if (f == QLatin1String("Qt::AlignBottom")) { + rc |= Qt::AlignBottom; + } else if (f == QLatin1String("Qt::AlignVCenter")) { + rc |= Qt::AlignVCenter; + } + } + } + return rc; +} + /*! \internal */ @@ -838,12 +901,15 @@ QLayoutItem *QAbstractFormBuilder::create(DomLayoutItem *ui_layoutItem, QLayout { switch (ui_layoutItem->kind()) { case DomLayoutItem::Widget: { - if (QWidget *w = create(ui_layoutItem->elementWidget(), parentWidget)) + if (QWidget *w = create(ui_layoutItem->elementWidget(), parentWidget)) { #ifdef QFORMINTERNAL_NAMESPACE // uilib - return new QWidgetItemV2(w); + QWidgetItem *item = new QWidgetItemV2(w); #else // Within Designer: Use factory method that returns special items that refuse to shrink to 0,0 - return QLayoutPrivate::createWidgetItem(layout, w); + QWidgetItem *item = QLayoutPrivate::createWidgetItem(layout, w); #endif + item->setAlignment(alignmentFromDom(ui_layoutItem->attributeAlignment())); + return item; + } qWarning() << QCoreApplication::translate("QAbstractFormBuilder", "Empty widget item in %1 '%2'.").arg(QString::fromUtf8(layout->metaObject()->className()), layout->objectName()); return 0; } @@ -1384,13 +1450,14 @@ DomActionRef *QAbstractFormBuilder::createActionRefDom(QAction *action) // Struct to store layout item parameters for saving layout items struct FormBuilderSaveLayoutEntry { explicit FormBuilderSaveLayoutEntry(QLayoutItem *li = 0) : - item(li), row(-1), column(-1), rowSpan(0), columnSpan(0) {} + item(li), row(-1), column(-1), rowSpan(0), columnSpan(0), alignment(0) {} QLayoutItem *item; int row; int column; int rowSpan; int columnSpan; + Qt::Alignment alignment; }; // Create list from standard box layout @@ -1401,7 +1468,9 @@ static QList<FormBuilderSaveLayoutEntry> saveLayoutEntries(const QLayout *layout rc.reserve(count); for (int idx = 0; idx < count; ++idx) { QLayoutItem *item = layout->itemAt(idx); - rc.append(FormBuilderSaveLayoutEntry(item)); + FormBuilderSaveLayoutEntry entry(item); + entry.alignment = item->alignment(); + rc.append(entry); } } return rc; @@ -1417,6 +1486,7 @@ static QList<FormBuilderSaveLayoutEntry> saveGridLayoutEntries(QGridLayout *grid QLayoutItem *item = gridLayout->itemAt(idx); FormBuilderSaveLayoutEntry entry(item); gridLayout->getItemPosition(idx, &entry.row, &entry.column, &entry.rowSpan,&entry.columnSpan); + entry.alignment = item->alignment(); rc.append(entry); } } @@ -1490,6 +1560,8 @@ DomLayout *QAbstractFormBuilder::createDom(QLayout *layout, DomLayout *ui_layout ui_item->setAttributeRowSpan(item.rowSpan); if (item.columnSpan > 1) ui_item->setAttributeColSpan(item.columnSpan); + if (item.alignment) + ui_item->setAttributeAlignment(alignmentValue(item.alignment)); ui_items.append(ui_item); } } diff --git a/tools/designer/src/lib/uilib/ui4.cpp b/tools/designer/src/lib/uilib/ui4.cpp index 98974e6..2b5fe8b 100644 --- a/tools/designer/src/lib/uilib/ui4.cpp +++ b/tools/designer/src/lib/uilib/ui4.cpp @@ -3673,6 +3673,7 @@ void DomLayoutItem::clear(bool clear_all) m_attr_rowSpan = 0; m_has_attr_colSpan = false; m_attr_colSpan = 0; + m_has_attr_alignment = false; } m_kind = Unknown; @@ -3694,6 +3695,7 @@ DomLayoutItem::DomLayoutItem() m_attr_rowSpan = 0; m_has_attr_colSpan = false; m_attr_colSpan = 0; + m_has_attr_alignment = false; m_widget = 0; m_layout = 0; m_spacer = 0; @@ -3727,6 +3729,10 @@ void DomLayoutItem::read(QXmlStreamReader &reader) setAttributeColSpan(attribute.value().toString().toInt()); continue; } + if (name == QLatin1String("alignment")) { + setAttributeAlignment(attribute.value().toString()); + continue; + } reader.raiseError(QLatin1String("Unexpected attribute ") + name.toString()); } @@ -3779,6 +3785,8 @@ void DomLayoutItem::read(const QDomElement &node) setAttributeRowSpan(node.attribute(QLatin1String("rowspan")).toInt()); if (node.hasAttribute(QLatin1String("colspan"))) setAttributeColSpan(node.attribute(QLatin1String("colspan")).toInt()); + if (node.hasAttribute(QLatin1String("alignment"))) + setAttributeAlignment(node.attribute(QLatin1String("alignment"))); for (QDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling()) { if (!n.isElement()) @@ -3828,6 +3836,9 @@ void DomLayoutItem::write(QXmlStreamWriter &writer, const QString &tagName) cons if (hasAttributeColSpan()) writer.writeAttribute(QLatin1String("colspan"), QString::number(attributeColSpan())); + if (hasAttributeAlignment()) + writer.writeAttribute(QLatin1String("alignment"), attributeAlignment()); + switch (kind()) { case Widget: { DomWidget* v = elementWidget(); diff --git a/tools/designer/src/lib/uilib/ui4_p.h b/tools/designer/src/lib/uilib/ui4_p.h index a464a89..836175e 100644 --- a/tools/designer/src/lib/uilib/ui4_p.h +++ b/tools/designer/src/lib/uilib/ui4_p.h @@ -1427,6 +1427,11 @@ public: inline void setAttributeColSpan(int a) { m_attr_colSpan = a; m_has_attr_colSpan = true; } inline void clearAttributeColSpan() { m_has_attr_colSpan = false; } + inline bool hasAttributeAlignment() const { return m_has_attr_alignment; } + inline QString attributeAlignment() const { return m_attr_alignment; } + inline void setAttributeAlignment(const QString& a) { m_attr_alignment = a; m_has_attr_alignment = true; } + inline void clearAttributeAlignment() { m_has_attr_alignment = false; } + // child element accessors enum Kind { Unknown = 0, Widget, Layout, Spacer }; inline Kind kind() const { return m_kind; } @@ -1460,6 +1465,9 @@ private: int m_attr_colSpan; bool m_has_attr_colSpan; + QString m_attr_alignment; + bool m_has_attr_alignment; + // child element data Kind m_kind; DomWidget* m_widget; |