12#include <QtDesigner/abstractformwindow.h>
13#include <QtDesigner/qextensionmanager.h>
14#include <QtDesigner/abstractformeditor.h>
15#include <QtDesigner/propertysheet.h>
16#include <QtDesigner/abstractwidgetfactory.h>
18#include <QtGui/qpainter.h>
19#include <QtWidgets/qboxlayout.h>
20#include <QtWidgets/qgridlayout.h>
21#include <QtWidgets/qformlayout.h>
22#include <QtWidgets/qapplication.h>
23#include <QtGui/qevent.h>
25#include <QtCore/qdebug.h>
26#include <QtCore/qalgorithms.h>
27#include <QtCore/qhash.h>
28#include <QtCore/qmap.h>
29#include <QtCore/qstack.h>
30#include <QtCore/qset.h>
47inline int gridRowCount(
const QGridLayout *gridLayout)
49 return gridLayout->rowCount();
52inline int gridColumnCount(
const QGridLayout *gridLayout)
54 return gridLayout->columnCount();
58inline void getGridItemPosition(QGridLayout *gridLayout,
int index,
59 int *row,
int *column,
int *rowspan,
int *colspan)
61 gridLayout->getItemPosition(index, row, column, rowspan, colspan);
64QRect gridItemInfo(QGridLayout *grid,
int index)
66 int row, column, rowSpan, columnSpan;
68 grid->getItemPosition(index, &row, &column, &rowSpan, &columnSpan);
69 return QRect(column, row, columnSpan, rowSpan);
72inline int gridRowCount(
const QFormLayout *formLayout) {
return formLayout->rowCount(); }
75inline void getGridItemPosition(QFormLayout *formLayout,
int index,
int *row,
int *column,
int *rowspan,
int *colspan)
83using namespace Qt::StringLiterals;
86#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
87static constexpr auto sizeConstraintC =
"sizeConstraint"_L1;
94
101 {
return Qt::Vertical | Qt::Horizontal; }
107 return new QSpacerItem(0, 0);
116template <
class GridLikeLayout>
119 const int count = gl.count();
120 str <<
"Grid: " << gl.objectName() << gridRowCount(&gl) <<
" rows x " << gridColumnCount(&gl)
121 <<
" cols " << count <<
" items\n";
122 for (
int i = 0; i < count; i++) {
123 QLayoutItem *item = gl.itemAt(i);
124 str <<
"Item " << i << item << item->widget() << gridItemInfo(
const_cast<GridLikeLayout *>(&gl), i) <<
" empty=" << qdesigner_internal::LayoutInfo::isEmptyItem(item) <<
"\n";
129static inline QDebug operator<<(QDebug str,
const QGridLayout &gl) {
return debugGridLikeLayout(str, gl); }
134 if (fl->itemAt(row, QFormLayout::SpanningRole))
136 return qdesigner_internal::LayoutInfo::isEmptyItem(fl->itemAt(row, QFormLayout::LabelRole)) && qdesigner_internal::LayoutInfo::isEmptyItem(fl->itemAt(row, QFormLayout::FieldRole));
144 const int bottomCheckRow = qMin(formLayout->rowCount(), restrictionArea.top() + restrictionArea.height());
145 for (
int r = restrictionArea.y(); r < bottomCheckRow; r++)
156 const qdesigner_internal::LayoutInfo::Type t = qdesigner_internal::LayoutInfo::layoutType(core, lt);
158 const int mask = properties.fromPropertySheet(core, lt, qdesigner_internal::LayoutProperties::AllProperties);
159 qdesigner_internal::LayoutInfo::deleteLayout(core, w);
160 QLayout *rc = core->widgetFactory()->createLayout(w,
nullptr, t);
161 properties.toPropertySheet(core, rc, mask,
true);
166template <
class GridLikeLayout>
169 Q_ASSERT(gridLayout);
170 const int count = gridLayout->count();
171 for (
int index = 0; index < count; index++) {
172 int row, column, rowspan, colspan;
173 getGridItemPosition(gridLayout, index, &row, &column, &rowspan, &colspan);
174 if (at_row >= row && at_row < (row + rowspan)
175 && at_column >= column && at_column < (column + colspan)) {
182template <
class GridLikeLayout>
187 QList<
int> indexesToBeRemoved;
188 indexesToBeRemoved.reserve(grid->count());
189 const int rightColumn = area.x() + area.width();
190 const int bottomRow = area.y() + area.height();
191 for (
int c = area.x(); c < rightColumn; c++)
192 for (
int r = area.y(); r < bottomRow; r++) {
193 const int index = findGridItemAt(grid, r ,c);
195 if (QLayoutItem *item = grid->itemAt(index)) {
196 if (qdesigner_internal::LayoutInfo::isEmptyItem(item)) {
197 if (indexesToBeRemoved.indexOf(index) == -1)
198 indexesToBeRemoved.push_back(index);
205 if (!indexesToBeRemoved.isEmpty()) {
206 std::stable_sort(indexesToBeRemoved.begin(), indexesToBeRemoved.end());
207 std::reverse(indexesToBeRemoved.begin(), indexesToBeRemoved.end());
208 for (
auto i : std::as_const(indexesToBeRemoved))
209 delete grid->takeAt(i);
231#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
252#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
282static bool intValueFromSheet(
const QDesignerPropertySheetExtension *sheet,
const QString &name,
int *value,
bool *changed)
284 const int sheetIndex = sheet->indexOf(name);
285 if (sheetIndex == -1)
287 *value = sheet->property(sheetIndex).toInt();
288 *changed = sheet->isChanged(sheetIndex);
293 QVariant *value,
bool *changed,
int *returnMask)
296 const int sIndex = sheet->indexOf(name);
298 *value = sheet->property(sIndex);
299 *changed = sheet->isChanged(sIndex);
331#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
352static bool intValueToSheet(QDesignerPropertySheetExtension *sheet,
const QString &name,
int value,
bool changed,
bool applyChanged)
356 const int sheetIndex = sheet->indexOf(name);
357 if (sheetIndex == -1) {
358 qWarning() <<
" LayoutProperties: Attempt to set property " << name <<
" that does not exist for the layout.";
361 sheet->setProperty(sheetIndex, QVariant(value));
363 sheet->setChanged(sheetIndex, changed);
367static void variantPropertyToSheet(
int mask,
int flag,
bool applyChanged, QDesignerPropertySheetExtension *sheet,
const QString &name,
368 const QVariant &value,
bool changed,
int *returnMask)
371 const int sIndex = sheet->indexOf(name);
373 sheet->setProperty(sIndex, value);
375 sheet->setChanged(sIndex, changed);
408#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
439 const int itemCount =
lt->
count();
448 const int index = indexOf(lt, widget);
450 qWarning() <<
"LayoutHelper::itemInfo: " <<
widget <<
" not in layout " <<
lt;
451 return QRect(0, 0, 1, 1);
469 bool canSimplify(
const QDesignerFormEditorInterface *,
const QWidget *,
const QRect &)
const override {
return false; }
470 void simplify(
const QDesignerFormEditorInterface *,
QWidget *,
const QRect &)
override {}
480 static BoxLayoutState state(
const QBoxLayout*lt);
482 QStack<BoxLayoutState> m_states;
483 const Qt::Orientation m_orientation;
488 return m_orientation == Qt::Horizontal ? QRect(index, 0, 1, 1) : QRect(0, index, 1, 1);
494 QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(lt);
496 boxLayout->insertWidget(m_orientation == Qt::Horizontal ? info.x() : info.y(), w);
501 QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(lt);
503 boxLayout->removeWidget(widget);
510 if (QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(lt)) {
511 const int index = boxLayout->indexOf(before);
513 const bool visible = before->isVisible();
514 delete boxLayout->takeAt(index);
517 before->setParent(
nullptr);
518 boxLayout->insertWidget(index, after);
523 qWarning() <<
"BoxLayoutHelper::replaceWidget : Unable to replace " << before <<
" by " << after <<
" in " << lt;
526 BoxLayoutHelper::BoxLayoutState
BoxLayoutHelper::state(
const QBoxLayout*lt)
529 if (
const int count = lt->count()) {
531 for (
int i = 0; i < count; i++)
532 if (
QWidget *w = lt->itemAt(i)->widget())
540 const QBoxLayout *boxLayout = qobject_cast<
const QBoxLayout *>(LayoutInfo::managedLayout(core, w));
542 m_states.push(state(boxLayout));
548 if (l->widget() == w)
557 const int count = lt->count();
559 return LayoutItemVector();
562 for (
int i = count - 1; i >= 0; i--)
563 rc.push_back(lt->takeAt(i));
569 QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(LayoutInfo::managedLayout(core, w));
571 const BoxLayoutState savedState = m_states.pop();
572 const BoxLayoutState currentState = state(boxLayout);
576 if (savedState == state(boxLayout))
579 Q_ASSERT(savedState.size() == currentState.size());
581 const LayoutItemVector items = disassembleLayout(boxLayout);
582 for (
auto *w : savedState) {
583 QLayoutItem *item = findItemOfWidget(items, w);
585 boxLayout->addItem(item);
635 <<
" cols " << gs.widgetItemMap.size() <<
" items\n";
637 const auto wcend = gs.widgetItemMap.constEnd();
638 for (
auto it = gs.widgetItemMap.constBegin(); it != wcend; ++it)
639 str <<
"Item " << it.key() << it.value() <<
'\n';
643 GridLayoutState::CellStates
GridLayoutState::cellStates(
const QList<QRect> &rects,
int numRows,
int numColumns)
645 CellStates rc = CellStates(numRows * numColumns, CellState(
Free,
Free));
646 for (
const auto &rect : rects) {
647 const int leftColumn = rect.x();
648 const int topRow = rect.y();
649 const int rightColumn = leftColumn + rect.width() - 1;
650 const int bottomRow = topRow + rect.height() - 1;
651 for (
int r = topRow; r <= bottomRow; r++)
652 for (
int c = leftColumn; c <= rightColumn; c++) {
653 const int flatIndex = r * numColumns + c;
655 DimensionCellState &horizState = rc[flatIndex].first;
656 if (c == leftColumn || c == rightColumn) {
657 horizState = Occupied;
659 if (horizState < Spanned)
660 horizState = Spanned;
663 DimensionCellState &vertState = rc[flatIndex].second;
664 if (r == topRow || r == bottomRow) {
665 vertState = Occupied;
667 if (vertState < Spanned)
673 qDebug() <<
"GridLayoutState::cellStates: " << numRows <<
" x " << numColumns;
674 for (
int r = 0; r < numRows; r++)
675 for (
int c = 0; c < numColumns; c++)
676 qDebug() <<
" Row: " << r <<
" column: " << c << rc[r * numColumns + c];
685 const int count = l->count();
686 for (
int i = 0; i < count; i++) {
687 QLayoutItem *item = l->itemAt(i);
688 if (!LayoutInfo::isEmptyItem(item)) {
689 widgetItemMap.insert(item->widget(), gridItemInfo(l, i));
690 if (item->alignment())
691 widgetAlignmentMap.insert(item->widget(), item->alignment());
698 QGridLayout *grid = qobject_cast<QGridLayout *>(LayoutInfo::managedLayout(core, w));
701 qDebug() <<
">GridLayoutState::applyToLayout" << *
this << *grid;
702 const bool shrink = grid->rowCount() >
rowCount || grid->columnCount() >
colCount;
704 QHash<QLayoutItem *, QRect> itemMap;
705 while (grid->count()) {
706 QLayoutItem *item = grid->takeAt(0);
707 if (!LayoutInfo::isEmptyItem(item)) {
708 QWidget *itemWidget = item->widget();
709 const auto it = widgetItemMap.constFind(itemWidget);
710 if (it == widgetItemMap.constEnd())
711 qFatal(
"GridLayoutState::applyToLayout: Attempt to apply to a layout that has a widget '%s'/'%s' added after saving the state.",
712 itemWidget->metaObject()->className(), itemWidget->objectName().toUtf8().constData());
713 itemMap.insert(item, it.value());
718 Q_ASSERT(itemMap.size() == widgetItemMap.size());
721 grid =
static_cast<QGridLayout*>(recreateManagedLayout(core, w, grid));
724 for (
auto it = itemMap.cbegin(), icend = itemMap.cend(); it != icend; ++it) {
725 const QRect info = it.value();
726 const Qt::Alignment alignment = widgetAlignmentMap.value(it.key()->widget(), {});
727 grid->addItem(it.key(), info.y(), info.x(), info.height(), info.width(), alignment);
730 const CellStates cs = cellStates(itemMap.values(), rowCount, colCount);
733 if (needsSpacerItem(cs[r *
colCount + c]))
734 grid->addItem(createGridSpacer(), r, c);
737 qDebug() <<
"<GridLayoutState::applyToLayout" << *grid;
743 for (
auto it = widgetItemMap.begin(), iend = widgetItemMap.end(); it != iend; ++it) {
744 const int topRow = it.value().y();
746 it.value().translate(0, 1);
748 const int rowSpan = it.value().height();
749 if (rowSpan > 1 && topRow + rowSpan > row)
750 it.value().setHeight(rowSpan + 1);
758 for (
auto it = widgetItemMap.begin(), iend = widgetItemMap.end(); it != iend; ++it) {
759 const int leftColumn = it.value().x();
760 if (leftColumn >= column) {
761 it.value().translate(1, 0);
763 const int colSpan = it.value().width();
764 if (colSpan > 1 && leftColumn + colSpan > column)
765 it.value().setWidth(colSpan + 1);
778 QList<
bool> occupiedRows(rowCount,
false);
779 QList<
bool> occupiedColumns(colCount,
false);
781 const int restrictionLeftColumn = r.x();
782 const int restrictionRightColumn = restrictionLeftColumn + r.width();
783 const int restrictionTopRow = r.y();
784 const int restrictionBottomRow = restrictionTopRow + r.height();
785 if (restrictionLeftColumn > 0 || restrictionRightColumn <
colCount ||
786 restrictionTopRow > 0 || restrictionBottomRow <
rowCount) {
787 for (
int r = 0; r < rowCount; r++)
788 if (r < restrictionTopRow || r >= restrictionBottomRow)
789 occupiedRows[r] =
true;
790 for (
int c = 0; c < colCount; c++)
791 if (c < restrictionLeftColumn || c >= restrictionRightColumn)
792 occupiedColumns[c] =
true;
795 const CellStates cs = cellStates(widgetItemMap.values(), rowCount, colCount);
797 for (
int c = 0; c <
colCount; c++) {
799 if (state.first == Occupied)
800 occupiedColumns[c] =
true;
801 if (state.second == Occupied)
802 occupiedRows[r] =
true;
805 if (occupiedRows.indexOf(
false) == -1 && occupiedColumns.indexOf(
false) == -1)
810 for (
int r =
rowCount - 1; r >= 0; r--)
811 if (!occupiedRows[r])
814 for (
int c =
colCount - 1; c >= 0; c--)
815 if (!occupiedColumns[c])
822 for (
auto it = widgetItemMap.begin(), iend = widgetItemMap.end(); it != iend; ++it) {
823 const int r = it.value().y();
824 Q_ASSERT(r != removeRow);
826 const int rowSpan = it.value().height();
828 const int bottomRow = r + rowSpan;
829 if (bottomRow > removeRow)
830 it.value().setHeight(rowSpan - 1);
834 it.value().translate(0, -1);
841 for (
auto it = widgetItemMap.begin(), iend = widgetItemMap.end(); it != iend; ++it) {
842 const int c = it.value().x();
843 Q_ASSERT(c != removeColumn);
844 if (c < removeColumn) {
845 const int colSpan = it.value().width();
847 const int rightColumn = c + colSpan;
848 if (rightColumn > removeColumn)
849 it.value().setWidth(colSpan - 1);
852 if (c > removeColumn)
853 it.value().translate(-1, 0);
868 void pushState(
const QDesignerFormEditorInterface *core,
const QWidget *widgetWithManagedLayout)
override;
869 void popState(
const QDesignerFormEditorInterface *core,
QWidget *widgetWithManagedLayout)
override;
871 bool canSimplify(
const QDesignerFormEditorInterface *core,
const QWidget *widgetWithManagedLayout,
const QRect &restrictionArea)
const override;
872 void simplify(
const QDesignerFormEditorInterface *core,
QWidget *widgetWithManagedLayout,
const QRect &restrictionArea)
override;
877 QStack<GridLayoutState> m_states;
885 QDesignerFormWindowInterface *fw = QDesignerFormWindowInterface::findFormWindow(grid);
891 QGridLayout *grid = qobject_cast<QGridLayout *>(lt);
893 return gridItemInfo(grid, index);
899 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(lt);
900 Q_ASSERT(gridLayout);
902 const int row = info.y();
903 int column = info.x();
904 int colSpan = info.width();
905 int rowSpan = info.height();
908 if (!removeEmptyCellsOnGrid(gridLayout, info)) {
910 colSpan = rowSpan = 1;
912 const int columnCount = gridLayout->columnCount();
913 for (
int c = column; c < columnCount; c++) {
914 const int idx = findGridItemAt(gridLayout, row, c);
915 if (idx != -1 && LayoutInfo::isEmptyItem(gridLayout->itemAt(idx))) {
920 if (freeColumn != -1) {
921 removeEmptyCellsOnGrid(gridLayout, QRect(freeColumn, row, 1, 1));
928 gridLayout->addWidget(w, row , column, rowSpan, colSpan);
933 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(lt);
934 Q_ASSERT(gridLayout);
935 const int index = gridLayout->indexOf(widget);
937 qWarning() <<
"GridLayoutHelper::removeWidget : Attempt to remove " << widget <<
" which is not in the layout.";
941 int row, column, rowspan, colspan;
942 gridLayout->getItemPosition(index, &row, &column, &rowspan, &colspan);
943 delete gridLayout->takeAt(index);
944 const int rightColumn = column + colspan;
945 const int bottomRow = row + rowspan;
946 for (
int c = column; c < rightColumn; c++)
947 for (
int r = row; r < bottomRow; r++)
948 gridLayout->addItem(createGridSpacer(), r, c);
955 if (QGridLayout *gridLayout = qobject_cast<QGridLayout *>(lt)) {
956 const int index = gridLayout->indexOf(before);
958 int row, column, rowSpan, columnSpan;
959 gridLayout->getItemPosition (index, &row, &column, &rowSpan, &columnSpan);
960 const bool visible = before->isVisible();
961 delete gridLayout->takeAt(index);
964 before->setParent(
nullptr);
965 gridLayout->addWidget(after, row, column, rowSpan, columnSpan);
970 qWarning() <<
"GridLayoutHelper::replaceWidget : Unable to replace " << before <<
" by " << after <<
" in " << lt;
975 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
976 Q_ASSERT(gridLayout);
984 Q_ASSERT(!m_states.isEmpty());
991 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
992 Q_ASSERT(gridLayout);
1000 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1001 Q_ASSERT(gridLayout);
1003 qDebug() <<
">GridLayoutHelper::simplify" << *gridLayout;
1009 qDebug() <<
"<GridLayoutHelper::simplify" << *gridLayout;
1024 void pushState(
const QDesignerFormEditorInterface *core,
const QWidget *widgetWithManagedLayout)
override;
1025 void popState(
const QDesignerFormEditorInterface *core,
QWidget *widgetWithManagedLayout)
override;
1027 bool canSimplify(
const QDesignerFormEditorInterface *core,
const QWidget *,
const QRect &)
const override;
1031 static FormLayoutState state(
const QFormLayout *lt);
1033 QStack<FormLayoutState> m_states;
1038 QFormLayout *form = qobject_cast<QFormLayout *>(lt);
1040 int row, column, colspan;
1041 getFormLayoutItemPosition(form, index, &row, &column,
nullptr, &colspan);
1042 return QRect(column, row, colspan, 1);
1048 qDebug() <<
"FormLayoutHelper::insertWidget:" << w << info;
1050 QFormLayout *formLayout = qobject_cast<QFormLayout *>(lt);
1051 Q_ASSERT(formLayout);
1054 const bool insert = !removeEmptyCellsOnGrid(formLayout, info);
1055 formLayoutAddWidget(formLayout, w, info, insert);
1056 QLayoutSupport::createEmptyCells(formLayout);
1061 QFormLayout *formLayout = qobject_cast<QFormLayout *>(lt);
1062 Q_ASSERT(formLayout);
1063 const int index = formLayout->indexOf(widget);
1065 qWarning() <<
"FormLayoutHelper::removeWidget : Attempt to remove " << widget <<
" which is not in the layout.";
1069 int row, column, colspan;
1070 getFormLayoutItemPosition(formLayout, index, &row, &column,
nullptr, &colspan);
1072 qDebug() <<
"FormLayoutHelper::removeWidget: #" << index << widget <<
" at " << row << column << colspan;
1073 delete formLayout->takeAt(index);
1074 if (colspan > 1 || column == 0)
1075 formLayout->setItem(row, QFormLayout::LabelRole, createFormSpacer());
1076 if (colspan > 1 || column == 1)
1077 formLayout->setItem(row, QFormLayout::FieldRole, createFormSpacer());
1084 if (QFormLayout *formLayout = qobject_cast<QFormLayout *>(lt)) {
1085 const int index = formLayout->indexOf(before);
1088 QFormLayout::ItemRole role;
1089 formLayout->getItemPosition (index, &row, &role);
1090 const bool visible = before->isVisible();
1091 delete formLayout->takeAt(index);
1094 before->setParent(
nullptr);
1095 formLayout->setWidget(row, role, after);
1100 qWarning() <<
"FormLayoutHelper::replaceWidget : Unable to replace " << before <<
" by " << after <<
" in " << lt;
1103 FormLayoutHelper::FormLayoutState
FormLayoutHelper::state(
const QFormLayout *lt)
1105 const int rowCount = lt->rowCount();
1107 return FormLayoutState();
1108 FormLayoutState rc(rowCount, {
nullptr,
nullptr});
1109 const int count = lt->count();
1110 int row, column, colspan;
1111 for (
int i = 0; i < count; i++) {
1112 QLayoutItem *item = lt->itemAt(i);
1113 if (!LayoutInfo::isEmptyItem(item)) {
1116 getFormLayoutItemPosition(lt, i, &row, &column,
nullptr, &colspan);
1117 if (colspan > 1 || column == 0)
1119 if (colspan > 1 || column == 1)
1124 qDebug() <<
"FormLayoutHelper::state: " << rowCount;
1125 for (
int r = 0; r < rowCount; r++)
1126 qDebug() <<
" Row: " << r << rc[r].first << rc[r].second;
1133 QFormLayout *formLayout = qobject_cast<QFormLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1134 Q_ASSERT(formLayout);
1135 m_states.push(state(formLayout));
1140 QFormLayout *formLayout = qobject_cast<QFormLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1141 Q_ASSERT(!m_states.isEmpty() && formLayout);
1143 const FormLayoutState storedState = m_states.pop();
1144 const FormLayoutState currentState = state(formLayout);
1145 if (currentState == storedState)
1147 const int rowCount = storedState.size();
1149 const BoxLayoutHelper::LayoutItemVector items = BoxLayoutHelper::disassembleLayout(formLayout);
1150 if (rowCount < formLayout->rowCount())
1151 formLayout =
static_cast<QFormLayout*>(recreateManagedLayout(core, widgetWithManagedLayout, formLayout ));
1152 for (
int r = 0; r < rowCount; r++) {
1154 const bool spanning = widgets[0] !=
nullptr && widgets[0] == widgets[1];
1156 formLayout->setWidget(r, QFormLayout::SpanningRole, widgets[0]);
1159 const QFormLayout::ItemRole role = c == 0 ? QFormLayout::LabelRole : QFormLayout::FieldRole;
1160 if (widgets[c] && BoxLayoutHelper::findItemOfWidget(items, widgets[c])) {
1161 formLayout->setWidget(r, role, widgets[c]);
1163 formLayout->setItem(r, role, createFormSpacer());
1172 const QFormLayout *formLayout = qobject_cast<QFormLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1173 Q_ASSERT(formLayout);
1179 using LayoutItemPair =
std::pair<QLayoutItem*, QLayoutItem*>;
1180 using LayoutItemPairs = QList<LayoutItemPair>;
1182 QFormLayout *formLayout = qobject_cast<QFormLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1183 Q_ASSERT(formLayout);
1185 qDebug() <<
"FormLayoutHelper::simplify";
1187 const int rowCount = formLayout->rowCount();
1188 LayoutItemPairs pairs(rowCount, LayoutItemPair(0, 0));
1189 for (
int i = formLayout->count() - 1; i >= 0; i--) {
1190 int row, col,colspan;
1191 getFormLayoutItemPosition(formLayout, i, &row, &col,
nullptr, &colspan);
1193 pairs[row].first = pairs[row].second = formLayout->takeAt(i);
1196 pairs[row].first = formLayout->takeAt(i);
1198 pairs[row].second = formLayout->takeAt(i);
1202 const int bottomCheckRow = qMin(rowCount, restrictionArea.y() + restrictionArea.height());
1203 for (
int r = bottomCheckRow - 1; r >= restrictionArea.y(); r--)
1204 if (LayoutInfo::isEmptyItem(pairs[r].first) && LayoutInfo::isEmptyItem(pairs[r].second)) {
1205 delete pairs[r].first;
1206 delete pairs[r].second;
1209 const int simpleRowCount = pairs.size();
1210 if (simpleRowCount < rowCount)
1211 formLayout =
static_cast<QFormLayout *>(recreateManagedLayout(core, widgetWithManagedLayout, formLayout));
1213 for (
int r = 0; r < simpleRowCount; r++) {
1214 const bool spanning = pairs[r].first == pairs[r].second;
1216 formLayout->setItem(r, QFormLayout::SpanningRole, pairs[r].first);
1218 formLayout->setItem(r, QFormLayout::LabelRole, pairs[r].first);
1219 formLayout->setItem(r, QFormLayout::FieldRole, pairs[r].second);
1537class QBoxLayoutSupport:
public QLayoutSupport
1540 QBoxLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, Qt::Orientation orientation, QObject *parent =
nullptr);
1542 void insertWidget(
QWidget *widget,
const std::pair<
int,
int> &cell) override;
1543 void removeWidget(
QWidget *widget) override;
1544 void simplify() override {}
1545 void insertRow(
int ) override {}
1546 void insertColumn(
int ) override {}
1548 int findItemAt(
int ,
int )
const override {
return -1; }
1549 using QLayoutSupport::findItemAt;
1552 void setCurrentCellFromIndicatorOnEmptyCell(
int index) override;
1553 void setCurrentCellFromIndicator(Qt::Orientation indicatorOrientation,
int index,
int increment)
override;
1554 bool supportsIndicatorOrientation(Qt::Orientation indicatorOrientation)
const override;
1555 QRect extendedGeometry(
int index)
const override;
1557 const Qt::Orientation m_orientation;
1560void QBoxLayoutSupport::removeWidget(
QWidget *widget)
1562 QLayout *lt = layout();
1563 const int index = lt->indexOf(widget);
1568 std::pair<
int,
int> currCell = currentCell();
1569 switch (m_orientation) {
1570 case Qt::Horizontal:
1571 if (currCell.second > 0 && index < currCell.second ) {
1573 setCurrentCell(currCell);
1577 if (currCell.first > 0 && index < currCell.first) {
1579 setCurrentCell(currCell);
1583 helper()->removeWidget(lt, widget);
1586QBoxLayoutSupport::QBoxLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, Qt::Orientation orientation, QObject *parent) :
1587 QLayoutSupport(formWindow, widget,
new BoxLayoutHelper(orientation), parent),
1588 m_orientation(orientation)
1592void QBoxLayoutSupport::setCurrentCellFromIndicatorOnEmptyCell(
int index)
1594 qDebug() <<
"QBoxLayoutSupport::setCurrentCellFromIndicatorOnEmptyCell(): Warning: found a fake spacer inside a vbox layout at " << index;
1595 setCurrentCell({0, 0});
1598void QBoxLayoutSupport::insertWidget(
QWidget *widget,
const std::pair<
int,
int> &cell)
1600 switch (m_orientation) {
1601 case Qt::Horizontal:
1602 helper()->insertWidget(layout(), QRect(cell.second, 0, 1, 1), widget);
1605 helper()->insertWidget(layout(), QRect(0, cell.first, 1, 1), widget);
1610void QBoxLayoutSupport::setCurrentCellFromIndicator(Qt::Orientation indicatorOrientation,
int index,
int increment)
1612 if (m_orientation == Qt::Horizontal && indicatorOrientation == Qt::Vertical)
1613 setCurrentCell({0, index + increment});
1614 else if (m_orientation == Qt::Vertical && indicatorOrientation == Qt::Horizontal)
1615 setCurrentCell({index + increment, 0});
1618bool QBoxLayoutSupport::supportsIndicatorOrientation(Qt::Orientation indicatorOrientation)
const
1620 return m_orientation != indicatorOrientation;
1623QRect QBoxLayoutSupport::extendedGeometry(
int index)
const
1625 QLayoutItem *item = layout()->itemAt(index);
1627 QRect g = item->geometry();
1629 const QRect info = itemInfo(index);
1632 if (info.x() == 0) {
1633 QPoint topLeft = g.topLeft();
1634 topLeft.rx() = layout()->geometry().left();
1635 g.setTopLeft(topLeft);
1639 if (info.y() == 0) {
1640 QPoint topLeft = g.topLeft();
1641 topLeft.ry() = layout()->geometry().top();
1642 g.setTopLeft(topLeft);
1646 const QBoxLayout *box =
static_cast<
const QBoxLayout*>(layout());
1647 if (index < box->count() -1)
1651 QPoint bottomRight = g.bottomRight();
1652 switch (m_orientation) {
1654 bottomRight.ry() = layout()->geometry().bottom();
1656 case Qt::Horizontal:
1657 bottomRight.rx() = layout()->geometry().right();
1660 g.setBottomRight(bottomRight);
1665template <
class GridLikeLayout>
1666class GridLikeLayoutSupportBase:
public QLayoutSupport
1670 GridLikeLayoutSupportBase(QDesignerFormWindowInterface *formWindow,
QWidget *widget,
LayoutHelper *helper, QObject *parent =
nullptr) :
1671 QLayoutSupport(formWindow, widget, helper, parent) {}
1673 void insertWidget(
QWidget *widget,
const std::pair<
int,
int> &cell) override;
1674 void removeWidget(
QWidget *widget) override { helper()->removeWidget(layout(), widget); }
1675 int findItemAt(
int row,
int column)
const override;
1676 using QLayoutSupport::findItemAt;
1679 GridLikeLayout *gridLikeLayout()
const {
1680 return qobject_cast<GridLikeLayout*>(LayoutInfo::managedLayout(formWindow()->core(), widget()));
1685 void setCurrentCellFromIndicatorOnEmptyCell(
int index) override;
1686 void setCurrentCellFromIndicator(Qt::Orientation indicatorOrientation,
int index,
int increment)
override;
1687 bool supportsIndicatorOrientation(Qt::Orientation)
const override {
return true; }
1689 QRect extendedGeometry(
int index)
const override;
1692 virtual void checkCellForInsertion(
int * ,
int * )
const {}
1695template <
class GridLikeLayout>
1696void GridLikeLayoutSupportBase<GridLikeLayout>::setCurrentCellFromIndicatorOnEmptyCell(
int index)
1698 GridLikeLayout *grid = gridLikeLayout();
1701 setInsertMode(InsertWidgetMode);
1702 int row, column, rowspan, colspan;
1704 getGridItemPosition(grid, index, &row, &column, &rowspan, &colspan);
1705 setCurrentCell({row, column});
1708template <
class GridLikeLayout>
1709void GridLikeLayoutSupportBase<GridLikeLayout>::setCurrentCellFromIndicator(Qt::Orientation indicatorOrientation,
int index,
int increment) {
1710 const QRect info = itemInfo(index);
1711 switch (indicatorOrientation) {
1712 case Qt::Vertical: {
1713 setInsertMode(InsertColumnMode);
1714 int row = info.top();
1715 int column = increment ? info.right() + 1 : info.left();
1716 checkCellForInsertion(&row, &column);
1717 setCurrentCell({row, column});
1720 case Qt::Horizontal: {
1721 setInsertMode(InsertRowMode);
1722 int row = increment ? info.bottom() + 1 : info.top();
1723 int column = info.left();
1724 checkCellForInsertion(&row, &column);
1725 setCurrentCell({row, column});
1731template <
class GridLikeLayout>
1732void GridLikeLayoutSupportBase<GridLikeLayout>::insertWidget(
QWidget *widget,
const std::pair<
int,
int> &cell)
1734 helper()->insertWidget(layout(), QRect(cell.second, cell.first, 1, 1), widget);
1737template <
class GridLikeLayout>
1738int GridLikeLayoutSupportBase<GridLikeLayout>::findItemAt(
int at_row,
int at_column)
const
1740 GridLikeLayout *grid = gridLikeLayout();
1742 return findGridItemAt(grid, at_row, at_column);
1745template <
class GridLikeLayout>
1746QRect GridLikeLayoutSupportBase<GridLikeLayout>::extendedGeometry(
int index)
const
1748 QLayoutItem *item = layout()->itemAt(index);
1750 QRect g = item->geometry();
1752 const QRect info = itemInfo(index);
1755 if (info.x() == 0) {
1756 QPoint topLeft = g.topLeft();
1757 topLeft.rx() = layout()->geometry().left();
1758 g.setTopLeft(topLeft);
1762 if (info.y() == 0) {
1763 QPoint topLeft = g.topLeft();
1764 topLeft.ry() = layout()->geometry().top();
1765 g.setTopLeft(topLeft);
1767 const GridLikeLayout *grid = gridLikeLayout();
1771 QPoint bottomRight = g.bottomRight();
1772 if (gridRowCount(grid) == info.y())
1773 bottomRight.ry() = layout()->geometry().bottom();
1774 if (gridColumnCount(grid) == info.x())
1775 bottomRight.rx() = layout()->geometry().right();
1776 g.setBottomRight(bottomRight);
1781class QGridLayoutSupport:
public GridLikeLayoutSupportBase<QGridLayout>
1785 QGridLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, QObject *parent =
nullptr);
1787 void simplify() override;
1788 void insertRow(
int row) override;
1789 void insertColumn(
int column) override;
1794QGridLayoutSupport::QGridLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, QObject *parent) :
1795 GridLikeLayoutSupportBase<QGridLayout>(formWindow, widget,
new GridLayoutHelper, parent)
1799void QGridLayoutSupport::insertRow(
int row)
1801 QGridLayout *grid = gridLayout();
1806void QGridLayoutSupport::insertColumn(
int column)
1808 QGridLayout *grid = gridLayout();
1816void QGridLayoutSupport::simplify()
1818 QGridLayout *grid = gridLayout();
1823 const QRect fullArea = QRect(0, 0, state.colCount, state.rowCount);
1829class QFormLayoutSupport:
public GridLikeLayoutSupportBase<QFormLayout>
1832 QFormLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, QObject *parent =
nullptr);
1834 void simplify() override {}
1835 void insertRow(
int ) override {}
1836 void insertColumn(
int ) override {}
1839 void checkCellForInsertion(
int * row,
int *col)
const override;
1842QFormLayoutSupport::QFormLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, QObject *parent) :
1843 GridLikeLayoutSupportBase<QFormLayout>(formWindow, widget,
new FormLayoutHelper, parent)
1847void QFormLayoutSupport::checkCellForInsertion(
int *row,
int *col)
const
1883QLayoutWidget::QLayoutWidget(QDesignerFormWindowInterface *formWindow, QWidget *parent)
1884 : QWidget(parent), m_formWindow(formWindow),
1885 m_leftMargin(0), m_topMargin(0), m_rightMargin(0), m_bottomMargin(0)
1889void QLayoutWidget::paintEvent(QPaintEvent*)
1891 if (m_formWindow->currentTool() != 0)
1898 QMap<
int, QMap<
int,
bool> > excludedRowsForColumn;
1899 QMap<
int, QMap<
int,
bool> > excludedColumnsForRow;
1901 QLayout *lt = layout();
1902 QGridLayout *grid = qobject_cast<QGridLayout *>(lt);
1904 if (
const int count = lt->count()) {
1905 p.setPen(QPen(QColor(255, 0, 0, 35), 1));
1906 for (
int i = 0; i < count; i++) {
1907 QLayoutItem *item = lt->itemAt(i);
1909 int row, column, rowSpan, columnSpan;
1910 grid->getItemPosition(i, &row, &column, &rowSpan, &columnSpan);
1911 QMap<
int,
bool> rows;
1912 QMap<
int,
bool> columns;
1913 for (
int i = rowSpan; i > 1; i--)
1914 rows[row + i - 2] =
true;
1915 for (
int i = columnSpan; i > 1; i--)
1916 columns[column + i - 2] =
true;
1918 while (rowSpan > 0) {
1919 excludedColumnsForRow[row + rowSpan - 1].insert(columns);
1922 while (columnSpan > 0) {
1923 excludedRowsForColumn[column + columnSpan - 1].insert(rows);
1927 if (item->spacerItem()) {
1928 const QRect geometry = item->geometry();
1929 if (!geometry.isNull())
1930 p.drawRect(geometry.adjusted(1, 1, -2, -2));
1936 p.setPen(QPen(QColor(0, 0x80, 0, 0x80), 1));
1937 const int rowCount = grid->rowCount();
1938 const int columnCount = grid->columnCount();
1939 for (
int i = 0; i < rowCount; i++) {
1940 for (
int j = 0; j < columnCount; j++) {
1941 const QRect cellRect = grid->cellRect(i, j);
1942 if (j < columnCount - 1 && !excludedColumnsForRow.value(i).value(j,
false)) {
1943 const double y0 = (i == 0)
1944 ? 0 : (grid->cellRect(i - 1, j).bottom() + cellRect.top()) / 2.0;
1945 const double y1 = (i == rowCount - 1)
1946 ? height() - 1 : (cellRect.bottom() + grid->cellRect(i + 1, j).top()) / 2.0;
1947 const double x = (cellRect.right() + grid->cellRect(i, j + 1).left()) / 2.0;
1948 p.drawLine(QPointF(x, y0), QPointF(x, y1));
1950 if (i < rowCount - 1 && !excludedRowsForColumn.value(j).value(i,
false)) {
1951 const double x0 = (j == 0)
1952 ? 0 : (grid->cellRect(i, j - 1).right() + cellRect.left()) / 2.0;
1953 const double x1 = (j == columnCount - 1)
1954 ? width() - 1 : (cellRect.right() + grid->cellRect(i, j + 1).left()) / 2.0;
1955 const double y = (cellRect.bottom() + grid->cellRect(i + 1, j).top()) / 2.0;
1956 p.drawLine(QPointF(x0, y), QPointF(x1, y));
1961 p.setPen(QPen(QColor(255, 0, 0, 128), 1));
1962 p.drawRect(0, 0, width() - 1, height() - 1);
1965bool QLayoutWidget::event(QEvent *e)
1967 switch (e->type()) {
1968 case QEvent::LayoutRequest: {
1969 (
void) QWidget::event(e);
1971 if (layout() && qdesigner_internal::LayoutInfo::layoutType(formWindow()->core(), parentWidget()) == qdesigner_internal::LayoutInfo::NoLayout) {
1972 resize(layout()->totalMinimumSize().expandedTo(size()));
1984 return QWidget::event(e);
1987int QLayoutWidget::layoutLeftMargin()
const
1989 if (m_leftMargin < 0 && layout()) {
1991 layout()->getContentsMargins(&margin,
nullptr,
nullptr,
nullptr);
1994 return m_leftMargin;
1997void QLayoutWidget::setLayoutLeftMargin(
int layoutMargin)
1999 m_leftMargin = layoutMargin;
2001 int newMargin = m_leftMargin;
2002 if (newMargin >= 0 && newMargin < ShiftValue)
2003 newMargin = ShiftValue;
2004 int left, top, right, bottom;
2005 layout()->getContentsMargins(&left, &top, &right, &bottom);
2006 layout()->setContentsMargins(newMargin, top, right, bottom);
2010int QLayoutWidget::layoutTopMargin()
const
2012 if (m_topMargin < 0 && layout()) {
2014 layout()->getContentsMargins(
nullptr, &margin,
nullptr,
nullptr);
2020void QLayoutWidget::setLayoutTopMargin(
int layoutMargin)
2022 m_topMargin = layoutMargin;
2024 int newMargin = m_topMargin;
2025 if (newMargin >= 0 && newMargin < ShiftValue)
2026 newMargin = ShiftValue;
2027 int left, top, right, bottom;
2028 layout()->getContentsMargins(&left, &top, &right, &bottom);
2029 layout()->setContentsMargins(left, newMargin, right, bottom);
2033int QLayoutWidget::layoutRightMargin()
const
2035 if (m_rightMargin < 0 && layout()) {
2037 layout()->getContentsMargins(
nullptr,
nullptr, &margin,
nullptr);
2040 return m_rightMargin;
2043void QLayoutWidget::setLayoutRightMargin(
int layoutMargin)
2045 m_rightMargin = layoutMargin;
2047 int newMargin = m_rightMargin;
2048 if (newMargin >= 0 && newMargin < ShiftValue)
2049 newMargin = ShiftValue;
2050 int left, top, right, bottom;
2051 layout()->getContentsMargins(&left, &top, &right, &bottom);
2052 layout()->setContentsMargins(left, top, newMargin, bottom);
2056int QLayoutWidget::layoutBottomMargin()
const
2058 if (m_bottomMargin < 0 && layout()) {
2060 layout()->getContentsMargins(
nullptr,
nullptr,
nullptr, &margin);
2063 return m_bottomMargin;
2066void QLayoutWidget::setLayoutBottomMargin(
int layoutMargin)
2068 m_bottomMargin = layoutMargin;
2070 int newMargin = m_bottomMargin;
2071 if (newMargin >= 0 && newMargin < ShiftValue)
2072 newMargin = ShiftValue;
2073 int left, top, right, bottom;
2074 layout()->getContentsMargins(&left, &top, &right, &bottom);
2075 layout()->setContentsMargins(left, top, right, newMargin);
BoxLayoutHelper(const Qt::Orientation orientation)
void simplify(const QDesignerFormEditorInterface *, QWidget *, const QRect &) override
void replaceWidget(QLayout *lt, QWidget *before, QWidget *after) override
void insertWidget(QLayout *lt, const QRect &info, QWidget *w) override
QRect itemInfo(QLayout *lt, int index) const override
static QLayoutItem * findItemOfWidget(const LayoutItemVector &lv, QWidget *w)
void popState(const QDesignerFormEditorInterface *, QWidget *) override
void removeWidget(QLayout *lt, QWidget *widget) override
bool canSimplify(const QDesignerFormEditorInterface *, const QWidget *, const QRect &) const override
static LayoutItemVector disassembleLayout(QLayout *lt)
void pushState(const QDesignerFormEditorInterface *, const QWidget *) override
void simplify(const QDesignerFormEditorInterface *core, QWidget *widgetWithManagedLayout, const QRect &restrictionArea) override
QRect itemInfo(QLayout *lt, int index) const override
void replaceWidget(QLayout *lt, QWidget *before, QWidget *after) override
void popState(const QDesignerFormEditorInterface *core, QWidget *widgetWithManagedLayout) override
void insertWidget(QLayout *lt, const QRect &info, QWidget *w) override
bool canSimplify(const QDesignerFormEditorInterface *core, const QWidget *widgetWithManagedLayout, const QRect &restrictionArea) const override
GridLayoutHelper()=default
static void insertRow(QGridLayout *grid, int row)
void pushState(const QDesignerFormEditorInterface *core, const QWidget *widgetWithManagedLayout) override
void removeWidget(QLayout *lt, QWidget *widget) override
Qt::Orientations expandingDirections() const override
Returns whether this layout item can make use of more space than sizeHint().
Auxiliary methods to store/retrieve settings.
static const char * marginPropertyNamesC[]
static constexpr auto labelAlignmentPropertyC
static bool intValueFromSheet(const QDesignerPropertySheetExtension *sheet, const QString &name, int *value, bool *changed)
static constexpr auto boxStretchPropertyC
static constexpr auto gridColumnStretchPropertyC
static constexpr auto rowWrapPolicyPropertyC
QDESIGNER_SHARED_EXPORT void getFormLayoutItemPosition(const QFormLayout *formLayout, int index, int *rowPtr, int *columnPtr=nullptr, int *rowspanPtr=nullptr, int *colspanPtr=nullptr)
static bool intValueToSheet(QDesignerPropertySheetExtension *sheet, const QString &name, int value, bool changed, bool applyChanged)
static bool needsSpacerItem(const GridLayoutState::CellState &cs)
static void variantPropertyToSheet(int mask, int flag, bool applyChanged, QDesignerPropertySheetExtension *sheet, const QString &name, const QVariant &value, bool changed, int *returnMask)
static const char * spacingPropertyNamesC[]
static constexpr auto gridRowMinimumHeightPropertyC
static constexpr auto fieldGrowthPolicyPropertyC
static void variantPropertyFromSheet(int mask, int flag, const QDesignerPropertySheetExtension *sheet, const QString &name, QVariant *value, bool *changed, int *returnMask)
static constexpr auto gridColumnMinimumWidthPropertyC
static constexpr auto formAlignmentPropertyC
static constexpr auto gridRowStretchPropertyC
static CellStates cellStates(const QList< QRect > &rects, int numRows, int numColumns)
void removeFreeColumn(int column)
void insertColumn(int column)
std::pair< DimensionCellState, DimensionCellState > CellState
void applyToLayout(const QDesignerFormEditorInterface *core, QWidget *w) const
bool simplify(const QRect &r, bool testOnly)
void removeFreeRow(int row)
GridLayoutState()=default
QHash< QWidget *, Qt::Alignment > widgetAlignmentMap
void fromLayout(QGridLayout *l)
QHash< QWidget *, QRect > widgetItemMap