From 2231e4f03c621459e1c261b2ed224a49877d2ddf Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 22 Oct 2010 16:10:44 +0200 Subject: 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 Task-number: QTBUG-3120 --- src/tools/uic/cpp/cppwriteinitialization.cpp | 4 + src/tools/uic/ui4.cpp | 11 ++ src/tools/uic/ui4.h | 8 ++ tools/designer/data/ui4.xsd | 1 + .../designer/src/lib/shared/qdesigner_command2.cpp | 62 +++++++++ .../designer/src/lib/shared/qdesigner_command2_p.h | 22 ++++ .../designer/src/lib/shared/qdesigner_taskmenu.cpp | 139 ++++++++++++++++++++- .../designer/src/lib/shared/qdesigner_taskmenu_p.h | 1 + tools/designer/src/lib/shared/qlayout_widget.cpp | 14 ++- .../designer/src/lib/uilib/abstractformbuilder.cpp | 82 +++++++++++- tools/designer/src/lib/uilib/ui4.cpp | 11 ++ tools/designer/src/lib/uilib/ui4_p.h | 8 ++ 12 files changed, 353 insertions(+), 10 deletions(-) diff --git a/src/tools/uic/cpp/cppwriteinitialization.cpp b/src/tools/uic/cpp/cppwriteinitialization.cpp index a2bc89d..e6fcd46 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.cpp +++ b/src/tools/uic/cpp/cppwriteinitialization.cpp @@ -1096,6 +1096,8 @@ void WriteInitialization::acceptLayoutItem(DomLayoutItem *node) const int colSpan = node->hasAttributeColSpan() ? node->attributeColSpan() : 1; addArgs = QString::fromLatin1("%1, %2, %3, %4, %5").arg(itemName).arg(row).arg(col).arg(rowSpan).arg(colSpan); + if (!node->attributeAlignment().isEmpty()) + addArgs += QLatin1String(", ") + node->attributeAlignment(); } else { if (layout->attributeClass() == QLatin1String("QFormLayout")) { methodPrefix = QLatin1String("set"); @@ -1105,6 +1107,8 @@ void WriteInitialization::acceptLayoutItem(DomLayoutItem *node) addArgs = QString::fromLatin1("%1, %2, %3").arg(row).arg(role).arg(itemName); } else { addArgs = itemName; + if (layout->attributeClass().contains(QLatin1String("Box")) && !node->attributeAlignment().isEmpty()) + addArgs += QLatin1String(", 0, ") + node->attributeAlignment(); } } diff --git a/src/tools/uic/ui4.cpp b/src/tools/uic/ui4.cpp index 1988696..e094eaf 100644 --- a/src/tools/uic/ui4.cpp +++ b/src/tools/uic/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/src/tools/uic/ui4.h b/src/tools/uic/ui4.h index a464a89..836175e 100644 --- a/src/tools/uic/ui4.h +++ b/src/tools/uic/ui4.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; diff --git a/tools/designer/data/ui4.xsd b/tools/designer/data/ui4.xsd index fc9c120..53bae62 100644 --- a/tools/designer/data/ui4.xsd +++ b/tools/designer/data/ui4.xsd @@ -207,6 +207,7 @@ + diff --git a/tools/designer/src/lib/shared/qdesigner_command2.cpp b/tools/designer/src/lib/shared/qdesigner_command2.cpp index c16bf40..ce2128a 100644 --- a/tools/designer/src/lib/shared/qdesigner_command2.cpp +++ b/tools/designer/src/lib/shared/qdesigner_command2.cpp @@ -154,6 +154,68 @@ QString MorphLayoutCommand::formatDescription(QDesignerFormEditorInterface * /* return QApplication::translate("Command", "Change layout of '%1' from %2 to %3").arg(widgetName, oldName, newName); } +LayoutAlignmentCommand::LayoutAlignmentCommand(QDesignerFormWindowInterface *formWindow) : + QDesignerFormWindowCommand(QApplication::translate("Command", "Change layout alignment"), formWindow), + m_newAlignment(0), m_oldAlignment(0), m_widget(0) +{ +} + +bool LayoutAlignmentCommand::init(QWidget *w, Qt::Alignment alignment) +{ + bool enabled; + m_newAlignment = alignment; + m_oldAlignment = LayoutAlignmentCommand::alignmentOf(core(), w, &enabled); + m_widget = w; + return enabled; +} + +void LayoutAlignmentCommand::redo() +{ + LayoutAlignmentCommand::applyAlignment(core(), m_widget, m_newAlignment); +} + +void LayoutAlignmentCommand::undo() +{ + LayoutAlignmentCommand::applyAlignment(core(), m_widget, m_oldAlignment); +} + +// Find out alignment and return whether command is enabled. +Qt::Alignment LayoutAlignmentCommand::alignmentOf(const QDesignerFormEditorInterface *core, QWidget *w, bool *enabledIn) +{ + bool managed; + QLayout *layout; + + if (enabledIn) + *enabledIn = false; + // Can only work on a managed layout + const LayoutInfo::Type type = LayoutInfo::laidoutWidgetType(core, w, &managed, &layout); + const bool enabled = layout && managed && + (type == LayoutInfo::HBox || type == LayoutInfo::VBox + || type == LayoutInfo::Grid); + if (!enabled) + return Qt::Alignment(0); + // Get alignment + const int index = layout->indexOf(w); + Q_ASSERT(index >= 0); + if (enabledIn) + *enabledIn = true; + return layout->itemAt(index)->alignment(); +} + +void LayoutAlignmentCommand::applyAlignment(const QDesignerFormEditorInterface *core, QWidget *w, Qt::Alignment a) +{ + // Find layout and apply to item + QLayout *layout; + LayoutInfo::laidoutWidgetType(core, w, 0, &layout); + if (layout) { + const int index = layout->indexOf(w); + if (index >= 0) { + layout->itemAt(index)->setAlignment(a); + layout->update(); + } + } +} + } // namespace qdesigner_internal QT_END_NAMESPACE diff --git a/tools/designer/src/lib/shared/qdesigner_command2_p.h b/tools/designer/src/lib/shared/qdesigner_command2_p.h index 5c7b9d6..4e9c95a 100644 --- a/tools/designer/src/lib/shared/qdesigner_command2_p.h +++ b/tools/designer/src/lib/shared/qdesigner_command2_p.h @@ -94,6 +94,28 @@ private: QWidget *m_layoutBase; }; +// Change the alignment of a widget in a managed grid/box layout cell. +class LayoutAlignmentCommand : public QDesignerFormWindowCommand { + Q_DISABLE_COPY(LayoutAlignmentCommand) +public: + explicit LayoutAlignmentCommand(QDesignerFormWindowInterface *formWindow); + + bool init(QWidget *w, Qt::Alignment alignment); + + virtual void redo(); + virtual void undo(); + + // Find out alignment and return whether command is enabled. + static Qt::Alignment alignmentOf(const QDesignerFormEditorInterface *core, QWidget *w, bool *enabled = 0); + +private: + static void applyAlignment(const QDesignerFormEditorInterface *core, QWidget *w, Qt::Alignment a); + + Qt::Alignment m_newAlignment; + Qt::Alignment m_oldAlignment; + QWidget *m_widget; +}; + } // namespace qdesigner_internal QT_END_NAMESPACE diff --git a/tools/designer/src/lib/shared/qdesigner_taskmenu.cpp b/tools/designer/src/lib/shared/qdesigner_taskmenu.cpp index a607f35..cdb7d15 100644 --- a/tools/designer/src/lib/shared/qdesigner_taskmenu.cpp +++ b/tools/designer/src/lib/shared/qdesigner_taskmenu.cpp @@ -41,6 +41,7 @@ #include "qdesigner_taskmenu_p.h" #include "qdesigner_command_p.h" +#include "qdesigner_command2_p.h" #include "richtexteditor_p.h" #include "plaintexteditor_p.h" #include "stylesheeteditor_p.h" @@ -180,10 +181,128 @@ QString ObjectNameDialog::newObjectName() const { return m_editor->text(); } +} // namespace +namespace qdesigner_internal { + +// Sub menu displaying the alignment options of a widget in a managed +// grid/box layout cell. +class LayoutAlignmentMenu { +public: + explicit LayoutAlignmentMenu(QObject *parent); + + QAction *subMenuAction() const { return m_subMenuAction; } + + void connect(QObject *receiver, const char *aSlot); + + // Set up enabled state and checked actions according to widget (managed box/grid) + bool setAlignment(const QDesignerFormEditorInterface *core, QWidget *w); + + // Return the currently checked alignment + Qt::Alignment alignment() const; + +private: + enum Actions { HorizNone, Left, HorizCenter, Right, VerticalNone, Top, VerticalCenter, Bottom }; + static QAction *createAction(const QString &text, int data, QMenu *menu, QActionGroup *ag); + + QAction *m_subMenuAction; + QActionGroup *m_horizGroup; + QActionGroup *m_verticalGroup; + QAction *m_actions[Bottom + 1]; +}; + +QAction *LayoutAlignmentMenu::createAction(const QString &text, int data, QMenu *menu, QActionGroup *ag) +{ + QAction * a = new QAction(text, 0); + a->setCheckable(true); + a->setData(QVariant(data)); + menu->addAction(a); + ag->addAction(a); + return a; +} + +LayoutAlignmentMenu::LayoutAlignmentMenu(QObject *parent) : + m_subMenuAction(new QAction(QDesignerTaskMenu::tr("Layout Alignment"), parent)), + m_horizGroup(new QActionGroup(parent)), + m_verticalGroup(new QActionGroup(parent)) +{ + m_horizGroup->setExclusive(true); + m_verticalGroup->setExclusive(true); + + QMenu *menu = new QMenu; + m_subMenuAction->setMenu(menu); + + m_actions[HorizNone] = createAction(QDesignerTaskMenu::tr("No Horizontal Alignment"), 0, menu, m_horizGroup); + m_actions[Left] = createAction(QDesignerTaskMenu::tr("Left"), Qt::AlignLeft, menu, m_horizGroup); + m_actions[HorizCenter] = createAction(QDesignerTaskMenu::tr("Center Horizontally"), Qt::AlignHCenter, menu, m_horizGroup); + m_actions[Right] = createAction(QDesignerTaskMenu::tr("Right"), Qt::AlignRight, menu, m_horizGroup); + menu->addSeparator(); + m_actions[VerticalNone] = createAction(QDesignerTaskMenu::tr("No Vertical Alignment"), 0, menu, m_verticalGroup); + m_actions[Top] = createAction(QDesignerTaskMenu::tr("Top"), Qt::AlignTop, menu, m_verticalGroup); + m_actions[VerticalCenter] = createAction(QDesignerTaskMenu::tr("Center Vertically"), Qt::AlignVCenter, menu, m_verticalGroup); + m_actions[Bottom] = createAction(QDesignerTaskMenu::tr("Bottom"), Qt::AlignBottom, menu, m_verticalGroup); +} + +void LayoutAlignmentMenu::connect(QObject *receiver, const char *aSlot) +{ + QObject::connect(m_horizGroup, SIGNAL(triggered(QAction*)), receiver, aSlot); + QObject::connect(m_verticalGroup, SIGNAL(triggered(QAction*)), receiver, aSlot); +} + +bool LayoutAlignmentMenu::setAlignment(const QDesignerFormEditorInterface *core, QWidget *w) +{ + bool enabled; + const Qt::Alignment alignment = LayoutAlignmentCommand::alignmentOf(core, w, &enabled); + if (!enabled) { + m_subMenuAction->setEnabled(false); + m_actions[HorizNone]->setChecked(true); + m_actions[VerticalNone]->setChecked(true); + return false; + } + // Get alignment + switch (alignment & Qt::AlignHorizontal_Mask) { + case Qt::AlignLeft: + m_actions[Left]->setChecked(true); + break; + case Qt::AlignHCenter: + m_actions[HorizCenter]->setChecked(true); + break; + case Qt::AlignRight: + m_actions[Right]->setChecked(true); + break; + default: + m_actions[HorizNone]->setChecked(true); + break; + } + switch (alignment & Qt::AlignVertical_Mask) { + case Qt::AlignTop: + m_actions[Top]->setChecked(true); + break; + case Qt::AlignVCenter: + m_actions[VerticalCenter]->setChecked(true); + break; + case Qt::AlignBottom: + m_actions[Bottom]->setChecked(true); + break; + default: + m_actions[VerticalNone]->setChecked(true); + break; + } + return true; +} + +Qt::Alignment LayoutAlignmentMenu::alignment() const +{ + Qt::Alignment alignment = 0; + if (const QAction *horizAction = m_horizGroup->checkedAction()) + if (const int horizAlign = horizAction->data().toInt()) + alignment |= static_cast(horizAlign); + if (const QAction *vertAction = m_verticalGroup->checkedAction()) + if (const int vertAlign = vertAction->data().toInt()) + alignment |= static_cast(vertAlign); + return alignment; } -namespace qdesigner_internal { // -------------- QDesignerTaskMenuPrivate class QDesignerTaskMenuPrivate { public: @@ -214,6 +333,7 @@ public: QAction *m_navigateToSlot; PromotionTaskMenu* m_promotionTaskMenu; QActionGroup *m_sizeActionGroup; + LayoutAlignmentMenu m_layoutAlignmentMenu; QAction *m_sizeActionsSubMenu; }; @@ -242,6 +362,7 @@ QDesignerTaskMenuPrivate::QDesignerTaskMenuPrivate(QWidget *widget, QObject *par m_navigateToSlot(new QAction(QDesignerTaskMenu::tr("Go to slot..."), parent)), m_promotionTaskMenu(new PromotionTaskMenu(widget, PromotionTaskMenu::ModeManagedMultiSelection, parent)), m_sizeActionGroup(new QActionGroup(parent)), + m_layoutAlignmentMenu(parent), m_sizeActionsSubMenu(new QAction(QDesignerTaskMenu::tr("Size Constraints"), parent)) { QMenu *sizeMenu = new QMenu; @@ -293,6 +414,7 @@ QDesignerTaskMenu::QDesignerTaskMenu(QWidget *widget, QObject *parent) : connect(d->m_containerFakeMethods, SIGNAL(triggered()), this, SLOT(containerFakeMethods())); connect(d->m_navigateToSlot, SIGNAL(triggered()), this, SLOT(slotNavigateToSlot())); connect(d->m_sizeActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(applySize(QAction*))); + d->m_layoutAlignmentMenu.connect(this, SLOT(slotLayoutAlignment())); } QDesignerTaskMenu::~QDesignerTaskMenu() @@ -410,6 +532,9 @@ QList QDesignerTaskMenu::taskActions() const actions.append(d->m_changeStyleSheet); actions.append(d->m_separator6); actions.append(d->m_sizeActionsSubMenu); + if (d->m_layoutAlignmentMenu.setAlignment(formWindow->core(), d->m_widget)) + actions.append(d->m_layoutAlignmentMenu.subMenuAction()); + d->m_promotionTaskMenu->setMode(formWindow->isManaged(d->m_widget) ? PromotionTaskMenu::ModeManagedMultiSelection : PromotionTaskMenu::ModeUnmanagedMultiSelection); d->m_promotionTaskMenu->addActions(formWindow, PromotionTaskMenu::LeadingSeparator, actions); @@ -771,7 +896,17 @@ void QDesignerTaskMenu::setProperty(QDesignerFormWindowInterface *fw, PropertyM } } - +void QDesignerTaskMenu::slotLayoutAlignment() +{ + QDesignerFormWindowInterface *fw = formWindow(); + const Qt::Alignment newAlignment = d->m_layoutAlignmentMenu.alignment(); + LayoutAlignmentCommand *cmd = new LayoutAlignmentCommand(fw); + if (cmd->init(d->m_widget, newAlignment)) { + fw->commandHistory()->push(cmd); + } else { + delete cmd; + } +} } // namespace qdesigner_internal QT_END_NAMESPACE diff --git a/tools/designer/src/lib/shared/qdesigner_taskmenu_p.h b/tools/designer/src/lib/shared/qdesigner_taskmenu_p.h index da7f572..23586f7 100644 --- a/tools/designer/src/lib/shared/qdesigner_taskmenu_p.h +++ b/tools/designer/src/lib/shared/qdesigner_taskmenu_p.h @@ -118,6 +118,7 @@ private slots: void containerFakeMethods(); void slotNavigateToSlot(); void applySize(QAction *a); + void slotLayoutAlignment(); private: QDesignerTaskMenuPrivate *d; diff --git a/tools/designer/src/lib/shared/qlayout_widget.cpp b/tools/designer/src/lib/shared/qlayout_widget.cpp index acec815..012f605 100644 --- a/tools/designer/src/lib/shared/qlayout_widget.cpp +++ b/tools/designer/src/lib/shared/qlayout_widget.cpp @@ -601,7 +601,7 @@ QRect LayoutHelper::itemInfo(QLayout *lt, const QWidget *widget) const } } - // Grid Layout state. Datatypically store the state of a GridLayout as a map of + // Grid Layout state. Datatype storing the state of a GridLayout as a map of // widgets to QRect(columns, rows) and size. Used to store the state for undo operations // that do not change the widgets within the layout; also provides some manipulation // functions and ability to apply the state to a layout provided its widgets haven't changed. @@ -634,7 +634,11 @@ QRect LayoutHelper::itemInfo(QLayout *lt, const QWidget *widget) const static CellStates cellStates(const QList &rects, int numRows, int numColumns); typedef QMap WidgetItemMap; + typedef QMap WidgetAlignmentMap; + WidgetItemMap widgetItemMap; + WidgetAlignmentMap widgetAlignmentMap; + int rowCount; int colCount; }; @@ -706,8 +710,11 @@ QRect LayoutHelper::itemInfo(QLayout *lt, const QWidget *widget) const const int count = l->count(); for (int i = 0; i < count; i++) { QLayoutItem *item = l->itemAt(i); - if (!LayoutInfo::isEmptyItem(item)) + if (!LayoutInfo::isEmptyItem(item)) { widgetItemMap.insert(item->widget(), gridItemInfo(l, i)); + if (item->alignment()) + widgetAlignmentMap.insert(item->widget(), item->alignment()); + } } } @@ -743,7 +750,8 @@ QRect LayoutHelper::itemInfo(QLayout *lt, const QWidget *widget) const const LayoutItemRectMap::const_iterator icend = itemMap.constEnd(); for (LayoutItemRectMap::const_iterator it = itemMap.constBegin(); it != icend; ++it) { const QRect info = it.value(); - grid->addItem(it.key(), info.y(), info.x(), info.height(), info.width()); + const Qt::Alignment alignment = widgetAlignmentMap.value(it.key()->widget(), Qt::Alignment(0)); + grid->addItem(it.key(), info.y(), info.x(), info.height(), info.width(), alignment); } // create spacers const CellStates cs = cellStates(itemMap.values(), rowCount, colCount); 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 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 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; -- cgit v0.12 From 0e47bb7df6f2cd0ad61e3769d98c1ddf1cc6d30c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 22 Oct 2010 16:16:51 +0200 Subject: uic: Add autotests for layout alignment Task-number: QTBUG-3120 --- tests/auto/uic/baseline/gridalignment.ui | 49 ++++++++++++++++++ tests/auto/uic/baseline/gridalignment.ui.h | 83 ++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 tests/auto/uic/baseline/gridalignment.ui create mode 100644 tests/auto/uic/baseline/gridalignment.ui.h diff --git a/tests/auto/uic/baseline/gridalignment.ui b/tests/auto/uic/baseline/gridalignment.ui new file mode 100644 index 0000000..11c28b1 --- /dev/null +++ b/tests/auto/uic/baseline/gridalignment.ui @@ -0,0 +1,49 @@ + + + Form + + + + 0 + 0 + 279 + 163 + + + + Form + + + + + + Left + + + + + + + Top + + + + + + + Right + + + + + + + Bottom + + + + + + + + diff --git a/tests/auto/uic/baseline/gridalignment.ui.h b/tests/auto/uic/baseline/gridalignment.ui.h new file mode 100644 index 0000000..8386190 --- /dev/null +++ b/tests/auto/uic/baseline/gridalignment.ui.h @@ -0,0 +1,83 @@ +/******************************************************************************** +** Form generated from reading UI file 'gridalignment.ui' +** +** Created: Fri Oct 22 14:33:59 2010 +** by: Qt User Interface Compiler version 4.8.0 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef GRIDALIGNMENT_H +#define GRIDALIGNMENT_H + +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_Form +{ +public: + QGridLayout *gridLayout; + QPushButton *pushButton; + QPushButton *pushButton_3; + QPushButton *pushButton_2; + QPushButton *pushButton_4; + + void setupUi(QWidget *Form) + { + if (Form->objectName().isEmpty()) + Form->setObjectName(QString::fromUtf8("Form")); + Form->resize(279, 163); + gridLayout = new QGridLayout(Form); + gridLayout->setObjectName(QString::fromUtf8("gridLayout")); + pushButton = new QPushButton(Form); + pushButton->setObjectName(QString::fromUtf8("pushButton")); + + gridLayout->addWidget(pushButton, 0, 0, 1, 1, Qt::AlignLeft); + + pushButton_3 = new QPushButton(Form); + pushButton_3->setObjectName(QString::fromUtf8("pushButton_3")); + + gridLayout->addWidget(pushButton_3, 0, 1, 1, 1, Qt::AlignTop); + + pushButton_2 = new QPushButton(Form); + pushButton_2->setObjectName(QString::fromUtf8("pushButton_2")); + + gridLayout->addWidget(pushButton_2, 1, 0, 1, 1, Qt::AlignRight); + + pushButton_4 = new QPushButton(Form); + pushButton_4->setObjectName(QString::fromUtf8("pushButton_4")); + + gridLayout->addWidget(pushButton_4, 1, 1, 1, 1, Qt::AlignBottom); + + + retranslateUi(Form); + + QMetaObject::connectSlotsByName(Form); + } // setupUi + + void retranslateUi(QWidget *Form) + { + Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8)); + pushButton->setText(QApplication::translate("Form", "Left", 0, QApplication::UnicodeUTF8)); + pushButton_3->setText(QApplication::translate("Form", "Top", 0, QApplication::UnicodeUTF8)); + pushButton_2->setText(QApplication::translate("Form", "Right", 0, QApplication::UnicodeUTF8)); + pushButton_4->setText(QApplication::translate("Form", "Bottom", 0, QApplication::UnicodeUTF8)); + } // retranslateUi + +}; + +namespace Ui { + class Form: public Ui_Form {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // GRIDALIGNMENT_H -- cgit v0.12 From 59a6ea0e4bde610dd55f735367a1097b79cc4383 Mon Sep 17 00:00:00 2001 From: Arvid Ephraim Picciani Date: Fri, 22 Oct 2010 17:10:35 +0200 Subject: Fix compiler warnings in qelfparser from magical conversion to signed int in arithmetics Reviewed-by: Friedemann Kleint --- src/corelib/plugin/qelfparser_p.cpp | 4 ++-- src/corelib/plugin/qlibrary.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/corelib/plugin/qelfparser_p.cpp b/src/corelib/plugin/qelfparser_p.cpp index 2e77ae7..4ae7f85 100644 --- a/src/corelib/plugin/qelfparser_p.cpp +++ b/src/corelib/plugin/qelfparser_p.cpp @@ -139,7 +139,7 @@ int QElfParser::parse(const char *dataStart, ulong fdlen, const QString &library qelfhalf_t e_shtrndx = read (data); data += sizeof(qelfhalf_t); // e_shtrndx - if ((e_shnum * e_shentsize) > fdlen) { + if ((quint32)(e_shnum * e_shentsize) > fdlen) { if (lib) lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)").arg(library) .arg(QLatin1String("announced %2 sections, each %3 bytes, exceed file size")) @@ -165,7 +165,7 @@ int QElfParser::parse(const char *dataStart, ulong fdlen, const QString &library parseSectionHeader(dataStart + soff, &strtab); m_stringTableFileOffset = strtab.offset; - if ((m_stringTableFileOffset + e_shentsize) >= fdlen || m_stringTableFileOffset == 0) { + if ((quint32)(m_stringTableFileOffset + e_shentsize) >= fdlen || m_stringTableFileOffset == 0) { if (lib) lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)").arg(library) .arg(QLatin1String("string table seems to be at %1")) diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp index 5683c66..fc8721c 100644 --- a/src/corelib/plugin/qlibrary.cpp +++ b/src/corelib/plugin/qlibrary.cpp @@ -1,3 +1,4 @@ + /**************************************************************************** ** ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). @@ -385,7 +386,7 @@ static bool qt_unix_query(const QString &library, uint *version, bool *debug, QB } } else if (r != QElfParser::Ok) { if (lib && qt_debug_component()) { - qWarning(qPrintable(lib->errorString)); + qWarning("QElfParser: %s",qPrintable(lib->errorString)); } return false; } -- cgit v0.12