11#include <QtDesigner/abstractformwindow.h>
12#include <QtDesigner/qextensionmanager.h>
13#include <QtDesigner/abstractformeditor.h>
14#include <QtDesigner/propertysheet.h>
15#include <QtDesigner/abstractwidgetfactory.h>
17#include <QtGui/qpainter.h>
18#include <QtWidgets/qboxlayout.h>
19#include <QtWidgets/qgridlayout.h>
20#include <QtWidgets/qformlayout.h>
21#include <QtWidgets/qapplication.h>
22#include <QtGui/qevent.h>
24#include <QtCore/qdebug.h>
25#include <QtCore/qalgorithms.h>
26#include <QtCore/qhash.h>
27#include <QtCore/qmap.h>
28#include <QtCore/qstack.h>
29#include <QtCore/qpair.h>
30#include <QtCore/qset.h>
45inline int gridRowCount(
const QGridLayout *gridLayout)
47 return gridLayout->rowCount();
50inline int gridColumnCount(
const QGridLayout *gridLayout)
52 return gridLayout->columnCount();
56inline void getGridItemPosition(QGridLayout *gridLayout,
int index,
57 int *row,
int *column,
int *rowspan,
int *colspan)
59 gridLayout->getItemPosition(index, row, column, rowspan, colspan);
62QRect gridItemInfo(QGridLayout *grid,
int index)
64 int row, column, rowSpan, columnSpan;
66 grid->getItemPosition(index, &row, &column, &rowSpan, &columnSpan);
67 return QRect(column, row, columnSpan, rowSpan);
70inline int gridRowCount(
const QFormLayout *formLayout) {
return formLayout->rowCount(); }
73inline void getGridItemPosition(QFormLayout *formLayout,
int index,
int *row,
int *column,
int *rowspan,
int *colspan)
81using namespace Qt::StringLiterals;
87
94 {
return Qt::Vertical | Qt::Horizontal; }
100 return new QSpacerItem(0, 0);
109template <
class GridLikeLayout>
112 const int count = gl.count();
113 str <<
"Grid: " << gl.objectName() << gridRowCount(&gl) <<
" rows x " << gridColumnCount(&gl)
114 <<
" cols " << count <<
" items\n";
115 for (
int i = 0; i < count; i++) {
116 QLayoutItem *item = gl.itemAt(i);
117 str <<
"Item " << i << item << item->widget() << gridItemInfo(
const_cast<GridLikeLayout *>(&gl), i) <<
" empty=" << qdesigner_internal::LayoutInfo::isEmptyItem(item) <<
"\n";
122static inline QDebug operator<<(QDebug str,
const QGridLayout &gl) {
return debugGridLikeLayout(str, gl); }
127 if (fl->itemAt(row, QFormLayout::SpanningRole))
129 return qdesigner_internal::LayoutInfo::isEmptyItem(fl->itemAt(row, QFormLayout::LabelRole)) && qdesigner_internal::LayoutInfo::isEmptyItem(fl->itemAt(row, QFormLayout::FieldRole));
137 const int bottomCheckRow = qMin(formLayout->rowCount(), restrictionArea.top() + restrictionArea.height());
138 for (
int r = restrictionArea.y(); r < bottomCheckRow; r++)
149 const qdesigner_internal::LayoutInfo::Type t = qdesigner_internal::LayoutInfo::layoutType(core, lt);
151 const int mask = properties.fromPropertySheet(core, lt, qdesigner_internal::LayoutProperties::AllProperties);
152 qdesigner_internal::LayoutInfo::deleteLayout(core, w);
153 QLayout *rc = core->widgetFactory()->createLayout(w,
nullptr, t);
154 properties.toPropertySheet(core, rc, mask,
true);
159template <
class GridLikeLayout>
162 Q_ASSERT(gridLayout);
163 const int count = gridLayout->count();
164 for (
int index = 0; index < count; index++) {
165 int row, column, rowspan, colspan;
166 getGridItemPosition(gridLayout, index, &row, &column, &rowspan, &colspan);
167 if (at_row >= row && at_row < (row + rowspan)
168 && at_column >= column && at_column < (column + colspan)) {
175template <
class GridLikeLayout>
180 QList<
int> indexesToBeRemoved;
181 indexesToBeRemoved.reserve(grid->count());
182 const int rightColumn = area.x() + area.width();
183 const int bottomRow = area.y() + area.height();
184 for (
int c = area.x(); c < rightColumn; c++)
185 for (
int r = area.y(); r < bottomRow; r++) {
186 const int index = findGridItemAt(grid, r ,c);
188 if (QLayoutItem *item = grid->itemAt(index)) {
189 if (qdesigner_internal::LayoutInfo::isEmptyItem(item)) {
190 if (indexesToBeRemoved.indexOf(index) == -1)
191 indexesToBeRemoved.push_back(index);
198 if (!indexesToBeRemoved.isEmpty()) {
199 std::stable_sort(indexesToBeRemoved.begin(), indexesToBeRemoved.end());
200 std::reverse(indexesToBeRemoved.begin(), indexesToBeRemoved.end());
201 for (
auto i : std::as_const(indexesToBeRemoved))
202 delete grid->takeAt(i);
267static bool intValueFromSheet(
const QDesignerPropertySheetExtension *sheet,
const QString &name,
int *value,
bool *changed)
269 const int sheetIndex = sheet->indexOf(name);
270 if (sheetIndex == -1)
272 *value = sheet->property(sheetIndex).toInt();
273 *changed = sheet->isChanged(sheetIndex);
278 QVariant *value,
bool *changed,
int *returnMask)
281 const int sIndex = sheet->indexOf(name);
283 *value = sheet->property(sIndex);
284 *changed = sheet->isChanged(sIndex);
329static bool intValueToSheet(QDesignerPropertySheetExtension *sheet,
const QString &name,
int value,
bool changed,
bool applyChanged)
333 const int sheetIndex = sheet->indexOf(name);
334 if (sheetIndex == -1) {
335 qWarning() <<
" LayoutProperties: Attempt to set property " << name <<
" that does not exist for the layout.";
338 sheet->setProperty(sheetIndex, QVariant(value));
340 sheet->setChanged(sheetIndex, changed);
344static void variantPropertyToSheet(
int mask,
int flag,
bool applyChanged, QDesignerPropertySheetExtension *sheet,
const QString &name,
345 const QVariant &value,
bool changed,
int *returnMask)
348 const int sIndex = sheet->indexOf(name);
350 sheet->setProperty(sIndex, value);
352 sheet->setChanged(sIndex, changed);
408 const int itemCount =
lt->
count();
419 qWarning() <<
"LayoutHelper::itemInfo: " <<
widget <<
" not in layout " <<
lt;
420 return QRect(0, 0, 1, 1);
438 bool canSimplify(
const QDesignerFormEditorInterface *,
const QWidget *,
const QRect &)
const override {
return false; }
439 void simplify(
const QDesignerFormEditorInterface *,
QWidget *,
const QRect &)
override {}
449 static BoxLayoutState state(
const QBoxLayout*lt);
451 QStack<BoxLayoutState> m_states;
452 const Qt::Orientation m_orientation;
457 return m_orientation == Qt::Horizontal ? QRect(index, 0, 1, 1) : QRect(0, index, 1, 1);
463 QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(lt);
465 boxLayout->insertWidget(m_orientation == Qt::Horizontal ? info.x() : info.y(), w);
470 QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(lt);
472 boxLayout->removeWidget(widget);
479 if (QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(lt)) {
480 const int index = boxLayout->indexOf(before);
482 const bool visible = before->isVisible();
483 delete boxLayout->takeAt(index);
486 before->setParent(
nullptr);
487 boxLayout->insertWidget(index, after);
492 qWarning() <<
"BoxLayoutHelper::replaceWidget : Unable to replace " << before <<
" by " << after <<
" in " << lt;
495 BoxLayoutHelper::BoxLayoutState
BoxLayoutHelper::state(
const QBoxLayout*lt)
498 if (
const int count = lt->count()) {
500 for (
int i = 0; i < count; i++)
501 if (
QWidget *w = lt->itemAt(i)->widget())
509 const QBoxLayout *boxLayout = qobject_cast<
const QBoxLayout *>(LayoutInfo::managedLayout(core, w));
511 m_states.push(state(boxLayout));
517 if (l->widget() == w)
526 const int count = lt->count();
528 return LayoutItemVector();
531 for (
int i = count - 1; i >= 0; i--)
532 rc.push_back(lt->takeAt(i));
538 QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(LayoutInfo::managedLayout(core, w));
540 const BoxLayoutState savedState = m_states.pop();
541 const BoxLayoutState currentState = state(boxLayout);
545 if (savedState == state(boxLayout))
548 Q_ASSERT(savedState.size() == currentState.size());
550 const LayoutItemVector items = disassembleLayout(boxLayout);
551 for (
auto *w : savedState) {
552 QLayoutItem *item = findItemOfWidget(items, w);
554 boxLayout->addItem(item);
604 <<
" cols " << gs.widgetItemMap.size() <<
" items\n";
606 const auto wcend = gs.widgetItemMap.constEnd();
607 for (
auto it = gs.widgetItemMap.constBegin(); it != wcend; ++it)
608 str <<
"Item " << it.key() << it.value() <<
'\n';
612 GridLayoutState::CellStates
GridLayoutState::cellStates(
const QList<QRect> &rects,
int numRows,
int numColumns)
614 CellStates rc = CellStates(numRows * numColumns, CellState(Free, Free));
615 for (
const auto &rect : rects) {
616 const int leftColumn = rect.x();
617 const int topRow = rect.y();
618 const int rightColumn = leftColumn + rect.width() - 1;
619 const int bottomRow = topRow + rect.height() - 1;
620 for (
int r = topRow; r <= bottomRow; r++)
621 for (
int c = leftColumn; c <= rightColumn; c++) {
622 const int flatIndex = r * numColumns + c;
624 DimensionCellState &horizState = rc[flatIndex].first;
625 if (c == leftColumn || c == rightColumn) {
626 horizState = Occupied;
628 if (horizState < Spanned)
629 horizState = Spanned;
632 DimensionCellState &vertState = rc[flatIndex].second;
633 if (r == topRow || r == bottomRow) {
634 vertState = Occupied;
636 if (vertState < Spanned)
642 qDebug() <<
"GridLayoutState::cellStates: " << numRows <<
" x " << numColumns;
643 for (
int r = 0; r < numRows; r++)
644 for (
int c = 0; c < numColumns; c++)
645 qDebug() <<
" Row: " << r <<
" column: " << c << rc[r * numColumns + c];
654 const int count = l->count();
655 for (
int i = 0; i < count; i++) {
656 QLayoutItem *item = l->itemAt(i);
657 if (!LayoutInfo::isEmptyItem(item)) {
658 widgetItemMap.insert(item->widget(), gridItemInfo(l, i));
659 if (item->alignment())
660 widgetAlignmentMap.insert(item->widget(), item->alignment());
667 QGridLayout *grid = qobject_cast<QGridLayout *>(LayoutInfo::managedLayout(core, w));
670 qDebug() <<
">GridLayoutState::applyToLayout" << *
this << *grid;
671 const bool shrink = grid->rowCount() >
rowCount || grid->columnCount() >
colCount;
673 QHash<QLayoutItem *, QRect> itemMap;
674 while (grid->count()) {
675 QLayoutItem *item = grid->takeAt(0);
676 if (!LayoutInfo::isEmptyItem(item)) {
677 QWidget *itemWidget = item->widget();
678 const auto it = widgetItemMap.constFind(itemWidget);
679 if (it == widgetItemMap.constEnd())
680 qFatal(
"GridLayoutState::applyToLayout: Attempt to apply to a layout that has a widget '%s'/'%s' added after saving the state.",
681 itemWidget->metaObject()->className(), itemWidget->objectName().toUtf8().constData());
682 itemMap.insert(item, it.value());
687 Q_ASSERT(itemMap.size() == widgetItemMap.size());
690 grid =
static_cast<QGridLayout*>(recreateManagedLayout(core, w, grid));
693 for (
auto it = itemMap.cbegin(), icend = itemMap.cend(); it != icend; ++it) {
694 const QRect info = it.value();
695 const Qt::Alignment alignment = widgetAlignmentMap.value(it.key()->widget(), {});
696 grid->addItem(it.key(), info.y(), info.x(), info.height(), info.width(), alignment);
699 const CellStates cs = cellStates(itemMap.values(), rowCount, colCount);
700 for (
int r = 0; r < rowCount; r++)
701 for (
int c = 0; c < colCount; c++)
702 if (needsSpacerItem(cs[r * colCount + c]))
703 grid->addItem(createGridSpacer(), r, c);
706 qDebug() <<
"<GridLayoutState::applyToLayout" << *grid;
712 for (
auto it = widgetItemMap.begin(), iend = widgetItemMap.end(); it != iend; ++it) {
713 const int topRow = it.value().y();
715 it.value().translate(0, 1);
717 const int rowSpan = it.value().height();
718 if (rowSpan > 1 && topRow + rowSpan > row)
719 it.value().setHeight(rowSpan + 1);
727 for (
auto it = widgetItemMap.begin(), iend = widgetItemMap.end(); it != iend; ++it) {
728 const int leftColumn = it.value().x();
729 if (leftColumn >= column) {
730 it.value().translate(1, 0);
732 const int colSpan = it.value().width();
733 if (colSpan > 1 && leftColumn + colSpan > column)
734 it.value().setWidth(colSpan + 1);
747 QList<
bool> occupiedRows(rowCount,
false);
748 QList<
bool> occupiedColumns(colCount,
false);
750 const int restrictionLeftColumn = r.x();
751 const int restrictionRightColumn = restrictionLeftColumn + r.width();
752 const int restrictionTopRow = r.y();
753 const int restrictionBottomRow = restrictionTopRow + r.height();
754 if (restrictionLeftColumn > 0 || restrictionRightColumn <
colCount ||
755 restrictionTopRow > 0 || restrictionBottomRow <
rowCount) {
756 for (
int r = 0; r < rowCount; r++)
757 if (r < restrictionTopRow || r >= restrictionBottomRow)
758 occupiedRows[r] =
true;
759 for (
int c = 0; c < colCount; c++)
760 if (c < restrictionLeftColumn || c >= restrictionRightColumn)
761 occupiedColumns[c] =
true;
764 const CellStates cs = cellStates(widgetItemMap.values(), rowCount, colCount);
766 for (
int c = 0; c <
colCount; c++) {
767 const CellState &state = cs[r *
colCount + c];
768 if (state.first == Occupied)
769 occupiedColumns[c] =
true;
770 if (state.second == Occupied)
771 occupiedRows[r] =
true;
774 if (occupiedRows.indexOf(
false) == -1 && occupiedColumns.indexOf(
false) == -1)
779 for (
int r =
rowCount - 1; r >= 0; r--)
780 if (!occupiedRows[r])
783 for (
int c =
colCount - 1; c >= 0; c--)
784 if (!occupiedColumns[c])
791 for (
auto it = widgetItemMap.begin(), iend = widgetItemMap.end(); it != iend; ++it) {
792 const int r = it.value().y();
793 Q_ASSERT(r != removeRow);
795 const int rowSpan = it.value().height();
797 const int bottomRow = r + rowSpan;
798 if (bottomRow > removeRow)
799 it.value().setHeight(rowSpan - 1);
803 it.value().translate(0, -1);
810 for (
auto it = widgetItemMap.begin(), iend = widgetItemMap.end(); it != iend; ++it) {
811 const int c = it.value().x();
812 Q_ASSERT(c != removeColumn);
813 if (c < removeColumn) {
814 const int colSpan = it.value().width();
816 const int rightColumn = c + colSpan;
817 if (rightColumn > removeColumn)
818 it.value().setWidth(colSpan - 1);
821 if (c > removeColumn)
822 it.value().translate(-1, 0);
837 void pushState(
const QDesignerFormEditorInterface *core,
const QWidget *widgetWithManagedLayout)
override;
838 void popState(
const QDesignerFormEditorInterface *core,
QWidget *widgetWithManagedLayout)
override;
840 bool canSimplify(
const QDesignerFormEditorInterface *core,
const QWidget *widgetWithManagedLayout,
const QRect &restrictionArea)
const override;
841 void simplify(
const QDesignerFormEditorInterface *core,
QWidget *widgetWithManagedLayout,
const QRect &restrictionArea)
override;
846 QStack<GridLayoutState> m_states;
854 QDesignerFormWindowInterface *fw = QDesignerFormWindowInterface::findFormWindow(grid);
860 QGridLayout *grid = qobject_cast<QGridLayout *>(lt);
862 return gridItemInfo(grid, index);
868 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(lt);
869 Q_ASSERT(gridLayout);
871 const int row = info.y();
872 int column = info.x();
873 int colSpan = info.width();
874 int rowSpan = info.height();
877 if (!removeEmptyCellsOnGrid(gridLayout, info)) {
879 colSpan = rowSpan = 1;
881 const int columnCount = gridLayout->columnCount();
882 for (
int c = column; c < columnCount; c++) {
883 const int idx = findGridItemAt(gridLayout, row, c);
884 if (idx != -1 && LayoutInfo::isEmptyItem(gridLayout->itemAt(idx))) {
889 if (freeColumn != -1) {
890 removeEmptyCellsOnGrid(gridLayout, QRect(freeColumn, row, 1, 1));
897 gridLayout->addWidget(w, row , column, rowSpan, colSpan);
902 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(lt);
903 Q_ASSERT(gridLayout);
904 const int index = gridLayout->indexOf(widget);
906 qWarning() <<
"GridLayoutHelper::removeWidget : Attempt to remove " << widget <<
" which is not in the layout.";
910 int row, column, rowspan, colspan;
911 gridLayout->getItemPosition(index, &row, &column, &rowspan, &colspan);
912 delete gridLayout->takeAt(index);
913 const int rightColumn = column + colspan;
914 const int bottomRow = row + rowspan;
915 for (
int c = column; c < rightColumn; c++)
916 for (
int r = row; r < bottomRow; r++)
917 gridLayout->addItem(createGridSpacer(), r, c);
924 if (QGridLayout *gridLayout = qobject_cast<QGridLayout *>(lt)) {
925 const int index = gridLayout->indexOf(before);
927 int row, column, rowSpan, columnSpan;
928 gridLayout->getItemPosition (index, &row, &column, &rowSpan, &columnSpan);
929 const bool visible = before->isVisible();
930 delete gridLayout->takeAt(index);
933 before->setParent(
nullptr);
934 gridLayout->addWidget(after, row, column, rowSpan, columnSpan);
939 qWarning() <<
"GridLayoutHelper::replaceWidget : Unable to replace " << before <<
" by " << after <<
" in " << lt;
944 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
945 Q_ASSERT(gridLayout);
953 Q_ASSERT(!m_states.isEmpty());
960 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
961 Q_ASSERT(gridLayout);
969 QGridLayout *gridLayout = qobject_cast<QGridLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
970 Q_ASSERT(gridLayout);
972 qDebug() <<
">GridLayoutHelper::simplify" << *gridLayout;
978 qDebug() <<
"<GridLayoutHelper::simplify" << *gridLayout;
993 void pushState(
const QDesignerFormEditorInterface *core,
const QWidget *widgetWithManagedLayout)
override;
994 void popState(
const QDesignerFormEditorInterface *core,
QWidget *widgetWithManagedLayout)
override;
996 bool canSimplify(
const QDesignerFormEditorInterface *core,
const QWidget *,
const QRect &)
const override;
997 void simplify(
const QDesignerFormEditorInterface *,
QWidget *,
const QRect &)
override;
1000 static FormLayoutState state(
const QFormLayout *lt);
1002 QStack<FormLayoutState> m_states;
1007 QFormLayout *form = qobject_cast<QFormLayout *>(lt);
1009 int row, column, colspan;
1010 getFormLayoutItemPosition(form, index, &row, &column,
nullptr, &colspan);
1011 return QRect(column, row, colspan, 1);
1017 qDebug() <<
"FormLayoutHelper::insertWidget:" << w << info;
1019 QFormLayout *formLayout = qobject_cast<QFormLayout *>(lt);
1020 Q_ASSERT(formLayout);
1023 const bool insert = !removeEmptyCellsOnGrid(formLayout, info);
1024 formLayoutAddWidget(formLayout, w, info, insert);
1025 QLayoutSupport::createEmptyCells(formLayout);
1030 QFormLayout *formLayout = qobject_cast<QFormLayout *>(lt);
1031 Q_ASSERT(formLayout);
1032 const int index = formLayout->indexOf(widget);
1034 qWarning() <<
"FormLayoutHelper::removeWidget : Attempt to remove " << widget <<
" which is not in the layout.";
1038 int row, column, colspan;
1039 getFormLayoutItemPosition(formLayout, index, &row, &column,
nullptr, &colspan);
1041 qDebug() <<
"FormLayoutHelper::removeWidget: #" << index << widget <<
" at " << row << column << colspan;
1042 delete formLayout->takeAt(index);
1043 if (colspan > 1 || column == 0)
1044 formLayout->setItem(row, QFormLayout::LabelRole, createFormSpacer());
1045 if (colspan > 1 || column == 1)
1046 formLayout->setItem(row, QFormLayout::FieldRole, createFormSpacer());
1053 if (QFormLayout *formLayout = qobject_cast<QFormLayout *>(lt)) {
1054 const int index = formLayout->indexOf(before);
1057 QFormLayout::ItemRole role;
1058 formLayout->getItemPosition (index, &row, &role);
1059 const bool visible = before->isVisible();
1060 delete formLayout->takeAt(index);
1063 before->setParent(
nullptr);
1064 formLayout->setWidget(row, role, after);
1069 qWarning() <<
"FormLayoutHelper::replaceWidget : Unable to replace " << before <<
" by " << after <<
" in " << lt;
1072 FormLayoutHelper::FormLayoutState
FormLayoutHelper::state(
const QFormLayout *lt)
1074 const int rowCount = lt->rowCount();
1076 return FormLayoutState();
1077 FormLayoutState rc(rowCount, {
nullptr,
nullptr});
1078 const int count = lt->count();
1079 int row, column, colspan;
1080 for (
int i = 0; i < count; i++) {
1081 QLayoutItem *item = lt->itemAt(i);
1082 if (!LayoutInfo::isEmptyItem(item)) {
1085 getFormLayoutItemPosition(lt, i, &row, &column,
nullptr, &colspan);
1086 if (colspan > 1 || column == 0)
1088 if (colspan > 1 || column == 1)
1093 qDebug() <<
"FormLayoutHelper::state: " << rowCount;
1094 for (
int r = 0; r < rowCount; r++)
1095 qDebug() <<
" Row: " << r << rc[r].first << rc[r].second;
1102 QFormLayout *formLayout = qobject_cast<QFormLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1103 Q_ASSERT(formLayout);
1104 m_states.push(state(formLayout));
1109 QFormLayout *formLayout = qobject_cast<QFormLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1110 Q_ASSERT(!m_states.isEmpty() && formLayout);
1112 const FormLayoutState storedState = m_states.pop();
1113 const FormLayoutState currentState = state(formLayout);
1114 if (currentState == storedState)
1116 const int rowCount = storedState.size();
1118 const BoxLayoutHelper::LayoutItemVector items = BoxLayoutHelper::disassembleLayout(formLayout);
1119 if (rowCount < formLayout->rowCount())
1120 formLayout =
static_cast<QFormLayout*>(recreateManagedLayout(core, widgetWithManagedLayout, formLayout ));
1121 for (
int r = 0; r < rowCount; r++) {
1123 const bool spanning = widgets[0] !=
nullptr && widgets[0] == widgets[1];
1125 formLayout->setWidget(r, QFormLayout::SpanningRole, widgets[0]);
1128 const QFormLayout::ItemRole role = c == 0 ? QFormLayout::LabelRole : QFormLayout::FieldRole;
1129 if (widgets[c] && BoxLayoutHelper::findItemOfWidget(items, widgets[c])) {
1130 formLayout->setWidget(r, role, widgets[c]);
1132 formLayout->setItem(r, role, createFormSpacer());
1141 const QFormLayout *formLayout = qobject_cast<QFormLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1142 Q_ASSERT(formLayout);
1148 using LayoutItemPair =
std::pair<QLayoutItem*, QLayoutItem*>;
1149 using LayoutItemPairs = QList<LayoutItemPair>;
1151 QFormLayout *formLayout = qobject_cast<QFormLayout *>(LayoutInfo::managedLayout(core, widgetWithManagedLayout));
1152 Q_ASSERT(formLayout);
1154 qDebug() <<
"FormLayoutHelper::simplify";
1156 const int rowCount = formLayout->rowCount();
1157 LayoutItemPairs pairs(rowCount, LayoutItemPair(0, 0));
1158 for (
int i = formLayout->count() - 1; i >= 0; i--) {
1159 int row, col,colspan;
1160 getFormLayoutItemPosition(formLayout, i, &row, &col,
nullptr, &colspan);
1162 pairs[row].first = pairs[row].second = formLayout->takeAt(i);
1165 pairs[row].first = formLayout->takeAt(i);
1167 pairs[row].second = formLayout->takeAt(i);
1171 const int bottomCheckRow = qMin(rowCount, restrictionArea.y() + restrictionArea.height());
1172 for (
int r = bottomCheckRow - 1; r >= restrictionArea.y(); r--)
1173 if (LayoutInfo::isEmptyItem(pairs[r].first) && LayoutInfo::isEmptyItem(pairs[r].second)) {
1174 delete pairs[r].first;
1175 delete pairs[r].second;
1178 const int simpleRowCount = pairs.size();
1179 if (simpleRowCount < rowCount)
1180 formLayout =
static_cast<QFormLayout *>(recreateManagedLayout(core, widgetWithManagedLayout, formLayout));
1182 for (
int r = 0; r < simpleRowCount; r++) {
1183 const bool spanning = pairs[r].first == pairs[r].second;
1185 formLayout->setItem(r, QFormLayout::SpanningRole, pairs[r].first);
1187 formLayout->setItem(r, QFormLayout::LabelRole, pairs[r].first);
1188 formLayout->setItem(r, QFormLayout::FieldRole, pairs[r].second);
1506class QBoxLayoutSupport:
public QLayoutSupport
1509 QBoxLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, Qt::Orientation orientation, QObject *parent =
nullptr);
1511 void insertWidget(
QWidget *widget,
const std::pair<
int,
int> &cell) override;
1512 void removeWidget(
QWidget *widget) override;
1513 void simplify() override {}
1514 void insertRow(
int ) override {}
1515 void insertColumn(
int ) override {}
1517 int findItemAt(
int ,
int )
const override {
return -1; }
1518 using QLayoutSupport::findItemAt;
1521 void setCurrentCellFromIndicatorOnEmptyCell(
int index) override;
1522 void setCurrentCellFromIndicator(Qt::Orientation indicatorOrientation,
int index,
int increment)
override;
1523 bool supportsIndicatorOrientation(Qt::Orientation indicatorOrientation)
const override;
1524 QRect extendedGeometry(
int index)
const override;
1526 const Qt::Orientation m_orientation;
1529void QBoxLayoutSupport::removeWidget(
QWidget *widget)
1531 QLayout *lt = layout();
1532 const int index = lt->indexOf(widget);
1537 std::pair<
int,
int> currCell = currentCell();
1538 switch (m_orientation) {
1539 case Qt::Horizontal:
1540 if (currCell.second > 0 && index < currCell.second ) {
1542 setCurrentCell(currCell);
1546 if (currCell.first > 0 && index < currCell.first) {
1548 setCurrentCell(currCell);
1552 helper()->removeWidget(lt, widget);
1555QBoxLayoutSupport::QBoxLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, Qt::Orientation orientation, QObject *parent) :
1556 QLayoutSupport(formWindow, widget,
new BoxLayoutHelper(orientation), parent),
1557 m_orientation(orientation)
1561void QBoxLayoutSupport::setCurrentCellFromIndicatorOnEmptyCell(
int index)
1563 qDebug() <<
"QBoxLayoutSupport::setCurrentCellFromIndicatorOnEmptyCell(): Warning: found a fake spacer inside a vbox layout at " << index;
1564 setCurrentCell({0, 0});
1567void QBoxLayoutSupport::insertWidget(
QWidget *widget,
const std::pair<
int,
int> &cell)
1569 switch (m_orientation) {
1570 case Qt::Horizontal:
1571 helper()->insertWidget(layout(), QRect(cell.second, 0, 1, 1), widget);
1574 helper()->insertWidget(layout(), QRect(0, cell.first, 1, 1), widget);
1579void QBoxLayoutSupport::setCurrentCellFromIndicator(Qt::Orientation indicatorOrientation,
int index,
int increment)
1581 if (m_orientation == Qt::Horizontal && indicatorOrientation == Qt::Vertical)
1582 setCurrentCell({0, index + increment});
1583 else if (m_orientation == Qt::Vertical && indicatorOrientation == Qt::Horizontal)
1584 setCurrentCell({index + increment, 0});
1587bool QBoxLayoutSupport::supportsIndicatorOrientation(Qt::Orientation indicatorOrientation)
const
1589 return m_orientation != indicatorOrientation;
1592QRect QBoxLayoutSupport::extendedGeometry(
int index)
const
1594 QLayoutItem *item = layout()->itemAt(index);
1596 QRect g = item->geometry();
1598 const QRect info = itemInfo(index);
1601 if (info.x() == 0) {
1602 QPoint topLeft = g.topLeft();
1603 topLeft.rx() = layout()->geometry().left();
1604 g.setTopLeft(topLeft);
1608 if (info.y() == 0) {
1609 QPoint topLeft = g.topLeft();
1610 topLeft.ry() = layout()->geometry().top();
1611 g.setTopLeft(topLeft);
1615 const QBoxLayout *box =
static_cast<
const QBoxLayout*>(layout());
1616 if (index < box->count() -1)
1620 QPoint bottomRight = g.bottomRight();
1621 switch (m_orientation) {
1623 bottomRight.ry() = layout()->geometry().bottom();
1625 case Qt::Horizontal:
1626 bottomRight.rx() = layout()->geometry().right();
1629 g.setBottomRight(bottomRight);
1634template <
class GridLikeLayout>
1635class GridLikeLayoutSupportBase:
public QLayoutSupport
1639 GridLikeLayoutSupportBase(QDesignerFormWindowInterface *formWindow,
QWidget *widget,
LayoutHelper *helper, QObject *parent =
nullptr) :
1640 QLayoutSupport(formWindow, widget, helper, parent) {}
1642 void insertWidget(
QWidget *widget,
const std::pair<
int,
int> &cell) override;
1643 void removeWidget(
QWidget *widget) override { helper()->removeWidget(layout(), widget); }
1644 int findItemAt(
int row,
int column)
const override;
1645 using QLayoutSupport::findItemAt;
1648 GridLikeLayout *gridLikeLayout()
const {
1649 return qobject_cast<GridLikeLayout*>(LayoutInfo::managedLayout(formWindow()->core(), widget()));
1654 void setCurrentCellFromIndicatorOnEmptyCell(
int index) override;
1655 void setCurrentCellFromIndicator(Qt::Orientation indicatorOrientation,
int index,
int increment)
override;
1656 bool supportsIndicatorOrientation(Qt::Orientation)
const override {
return true; }
1658 QRect extendedGeometry(
int index)
const override;
1661 virtual void checkCellForInsertion(
int * ,
int * )
const {}
1664template <
class GridLikeLayout>
1665void GridLikeLayoutSupportBase<GridLikeLayout>::setCurrentCellFromIndicatorOnEmptyCell(
int index)
1667 GridLikeLayout *grid = gridLikeLayout();
1670 setInsertMode(InsertWidgetMode);
1671 int row, column, rowspan, colspan;
1673 getGridItemPosition(grid, index, &row, &column, &rowspan, &colspan);
1674 setCurrentCell({row, column});
1677template <
class GridLikeLayout>
1678void GridLikeLayoutSupportBase<GridLikeLayout>::setCurrentCellFromIndicator(Qt::Orientation indicatorOrientation,
int index,
int increment) {
1679 const QRect info = itemInfo(index);
1680 switch (indicatorOrientation) {
1681 case Qt::Vertical: {
1682 setInsertMode(InsertColumnMode);
1683 int row = info.top();
1684 int column = increment ? info.right() + 1 : info.left();
1685 checkCellForInsertion(&row, &column);
1686 setCurrentCell({row, column});
1689 case Qt::Horizontal: {
1690 setInsertMode(InsertRowMode);
1691 int row = increment ? info.bottom() + 1 : info.top();
1692 int column = info.left();
1693 checkCellForInsertion(&row, &column);
1694 setCurrentCell({row, column});
1700template <
class GridLikeLayout>
1701void GridLikeLayoutSupportBase<GridLikeLayout>::insertWidget(
QWidget *widget,
const std::pair<
int,
int> &cell)
1703 helper()->insertWidget(layout(), QRect(cell.second, cell.first, 1, 1), widget);
1706template <
class GridLikeLayout>
1707int GridLikeLayoutSupportBase<GridLikeLayout>::findItemAt(
int at_row,
int at_column)
const
1709 GridLikeLayout *grid = gridLikeLayout();
1711 return findGridItemAt(grid, at_row, at_column);
1714template <
class GridLikeLayout>
1715QRect GridLikeLayoutSupportBase<GridLikeLayout>::extendedGeometry(
int index)
const
1717 QLayoutItem *item = layout()->itemAt(index);
1719 QRect g = item->geometry();
1721 const QRect info = itemInfo(index);
1724 if (info.x() == 0) {
1725 QPoint topLeft = g.topLeft();
1726 topLeft.rx() = layout()->geometry().left();
1727 g.setTopLeft(topLeft);
1731 if (info.y() == 0) {
1732 QPoint topLeft = g.topLeft();
1733 topLeft.ry() = layout()->geometry().top();
1734 g.setTopLeft(topLeft);
1736 const GridLikeLayout *grid = gridLikeLayout();
1740 QPoint bottomRight = g.bottomRight();
1741 if (gridRowCount(grid) == info.y())
1742 bottomRight.ry() = layout()->geometry().bottom();
1743 if (gridColumnCount(grid) == info.x())
1744 bottomRight.rx() = layout()->geometry().right();
1745 g.setBottomRight(bottomRight);
1750class QGridLayoutSupport:
public GridLikeLayoutSupportBase<QGridLayout>
1754 QGridLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, QObject *parent =
nullptr);
1756 void simplify() override;
1757 void insertRow(
int row) override;
1758 void insertColumn(
int column) override;
1763QGridLayoutSupport::QGridLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, QObject *parent) :
1764 GridLikeLayoutSupportBase<QGridLayout>(formWindow, widget,
new GridLayoutHelper, parent)
1768void QGridLayoutSupport::insertRow(
int row)
1770 QGridLayout *grid = gridLayout();
1775void QGridLayoutSupport::insertColumn(
int column)
1777 QGridLayout *grid = gridLayout();
1785void QGridLayoutSupport::simplify()
1787 QGridLayout *grid = gridLayout();
1792 const QRect fullArea = QRect(0, 0, state.colCount, state.rowCount);
1798class QFormLayoutSupport:
public GridLikeLayoutSupportBase<QFormLayout>
1801 QFormLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, QObject *parent =
nullptr);
1803 void simplify() override {}
1804 void insertRow(
int ) override {}
1805 void insertColumn(
int ) override {}
1808 void checkCellForInsertion(
int * row,
int *col)
const override;
1811QFormLayoutSupport::QFormLayoutSupport(QDesignerFormWindowInterface *formWindow,
QWidget *widget, QObject *parent) :
1812 GridLikeLayoutSupportBase<QFormLayout>(formWindow, widget,
new FormLayoutHelper, parent)
1816void QFormLayoutSupport::checkCellForInsertion(
int *row,
int *col)
const
1852QLayoutWidget::QLayoutWidget(QDesignerFormWindowInterface *formWindow, QWidget *parent)
1853 : QWidget(parent), m_formWindow(formWindow),
1854 m_leftMargin(0), m_topMargin(0), m_rightMargin(0), m_bottomMargin(0)
1858void QLayoutWidget::paintEvent(QPaintEvent*)
1860 if (m_formWindow->currentTool() != 0)
1867 QMap<
int, QMap<
int,
bool> > excludedRowsForColumn;
1868 QMap<
int, QMap<
int,
bool> > excludedColumnsForRow;
1870 QLayout *lt = layout();
1871 QGridLayout *grid = qobject_cast<QGridLayout *>(lt);
1873 if (
const int count = lt->count()) {
1874 p.setPen(QPen(QColor(255, 0, 0, 35), 1));
1875 for (
int i = 0; i < count; i++) {
1876 QLayoutItem *item = lt->itemAt(i);
1878 int row, column, rowSpan, columnSpan;
1879 grid->getItemPosition(i, &row, &column, &rowSpan, &columnSpan);
1880 QMap<
int,
bool> rows;
1881 QMap<
int,
bool> columns;
1882 for (
int i = rowSpan; i > 1; i--)
1883 rows[row + i - 2] =
true;
1884 for (
int i = columnSpan; i > 1; i--)
1885 columns[column + i - 2] =
true;
1887 while (rowSpan > 0) {
1888 excludedColumnsForRow[row + rowSpan - 1].insert(columns);
1891 while (columnSpan > 0) {
1892 excludedRowsForColumn[column + columnSpan - 1].insert(rows);
1896 if (item->spacerItem()) {
1897 const QRect geometry = item->geometry();
1898 if (!geometry.isNull())
1899 p.drawRect(geometry.adjusted(1, 1, -2, -2));
1905 p.setPen(QPen(QColor(0, 0x80, 0, 0x80), 1));
1906 const int rowCount = grid->rowCount();
1907 const int columnCount = grid->columnCount();
1908 for (
int i = 0; i < rowCount; i++) {
1909 for (
int j = 0; j < columnCount; j++) {
1910 const QRect cellRect = grid->cellRect(i, j);
1911 if (j < columnCount - 1 && !excludedColumnsForRow.value(i).value(j,
false)) {
1912 const double y0 = (i == 0)
1913 ? 0 : (grid->cellRect(i - 1, j).bottom() + cellRect.top()) / 2.0;
1914 const double y1 = (i == rowCount - 1)
1915 ? height() - 1 : (cellRect.bottom() + grid->cellRect(i + 1, j).top()) / 2.0;
1916 const double x = (cellRect.right() + grid->cellRect(i, j + 1).left()) / 2.0;
1917 p.drawLine(QPointF(x, y0), QPointF(x, y1));
1919 if (i < rowCount - 1 && !excludedRowsForColumn.value(j).value(i,
false)) {
1920 const double x0 = (j == 0)
1921 ? 0 : (grid->cellRect(i, j - 1).right() + cellRect.left()) / 2.0;
1922 const double x1 = (j == columnCount - 1)
1923 ? width() - 1 : (cellRect.right() + grid->cellRect(i, j + 1).left()) / 2.0;
1924 const double y = (cellRect.bottom() + grid->cellRect(i + 1, j).top()) / 2.0;
1925 p.drawLine(QPointF(x0, y), QPointF(x1, y));
1930 p.setPen(QPen(QColor(255, 0, 0, 128), 1));
1931 p.drawRect(0, 0, width() - 1, height() - 1);
1934bool QLayoutWidget::event(QEvent *e)
1936 switch (e->type()) {
1937 case QEvent::LayoutRequest: {
1938 (
void) QWidget::event(e);
1940 if (layout() && qdesigner_internal::LayoutInfo::layoutType(formWindow()->core(), parentWidget()) == qdesigner_internal::LayoutInfo::NoLayout) {
1941 resize(layout()->totalMinimumSize().expandedTo(size()));
1953 return QWidget::event(e);
1956int QLayoutWidget::layoutLeftMargin()
const
1958 if (m_leftMargin < 0 && layout()) {
1960 layout()->getContentsMargins(&margin,
nullptr,
nullptr,
nullptr);
1963 return m_leftMargin;
1966void QLayoutWidget::setLayoutLeftMargin(
int layoutMargin)
1968 m_leftMargin = layoutMargin;
1970 int newMargin = m_leftMargin;
1971 if (newMargin >= 0 && newMargin < ShiftValue)
1972 newMargin = ShiftValue;
1973 int left, top, right, bottom;
1974 layout()->getContentsMargins(&left, &top, &right, &bottom);
1975 layout()->setContentsMargins(newMargin, top, right, bottom);
1979int QLayoutWidget::layoutTopMargin()
const
1981 if (m_topMargin < 0 && layout()) {
1983 layout()->getContentsMargins(
nullptr, &margin,
nullptr,
nullptr);
1989void QLayoutWidget::setLayoutTopMargin(
int layoutMargin)
1991 m_topMargin = layoutMargin;
1993 int newMargin = m_topMargin;
1994 if (newMargin >= 0 && newMargin < ShiftValue)
1995 newMargin = ShiftValue;
1996 int left, top, right, bottom;
1997 layout()->getContentsMargins(&left, &top, &right, &bottom);
1998 layout()->setContentsMargins(left, newMargin, right, bottom);
2002int QLayoutWidget::layoutRightMargin()
const
2004 if (m_rightMargin < 0 && layout()) {
2006 layout()->getContentsMargins(
nullptr,
nullptr, &margin,
nullptr);
2009 return m_rightMargin;
2012void QLayoutWidget::setLayoutRightMargin(
int layoutMargin)
2014 m_rightMargin = layoutMargin;
2016 int newMargin = m_rightMargin;
2017 if (newMargin >= 0 && newMargin < ShiftValue)
2018 newMargin = ShiftValue;
2019 int left, top, right, bottom;
2020 layout()->getContentsMargins(&left, &top, &right, &bottom);
2021 layout()->setContentsMargins(left, top, newMargin, bottom);
2025int QLayoutWidget::layoutBottomMargin()
const
2027 if (m_bottomMargin < 0 && layout()) {
2029 layout()->getContentsMargins(
nullptr,
nullptr,
nullptr, &margin);
2032 return m_bottomMargin;
2035void QLayoutWidget::setLayoutBottomMargin(
int layoutMargin)
2037 m_bottomMargin = layoutMargin;
2039 int newMargin = m_bottomMargin;
2040 if (newMargin >= 0 && newMargin < ShiftValue)
2041 newMargin = ShiftValue;
2042 int left, top, right, bottom;
2043 layout()->getContentsMargins(&left, &top, &right, &bottom);
2044 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)
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