/
qformlayout.cpp
2543 lines (2085 loc) · 77.6 KB
/
qformlayout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qapplication.h"
#include "qdebug.h"
#include "qformlayout.h"
#include "qlabel.h"
#include "qlayout_p.h"
#include "qlayoutengine_p.h"
#include "qlist.h"
#include "qrect.h"
#include "qwidget.h"
QT_BEGIN_NAMESPACE
namespace {
// Fixed column matrix, stores items as [i11, i12, i21, i22...],
// with FORTRAN-style index operator(r, c).
template <class T, int NumColumns>
class FixedColumnMatrix {
public:
typedef QList<T> Storage;
FixedColumnMatrix() { }
void clear() { m_storage.clear(); }
const T &operator()(int r, int c) const { return m_storage[r * NumColumns + c]; }
T &operator()(int r, int c) { return m_storage[r * NumColumns + c]; }
int rowCount() const { return m_storage.size() / NumColumns; }
void insertRow(int r, const T &value);
void removeRow(int r);
// Hmmpf.. Some things are faster that way.
const Storage &storage() const { return m_storage; }
static void storageIndexToPosition(int idx, int *rowPtr, int *colPtr);
private:
Storage m_storage;
};
template <class T, int NumColumns>
void FixedColumnMatrix<T, NumColumns>::insertRow(int r, const T &value)
{
typename Storage::iterator it = m_storage.begin();
it += r * NumColumns;
m_storage.insert(it, NumColumns, value);
}
template <class T, int NumColumns>
void FixedColumnMatrix<T, NumColumns>::removeRow(int r)
{
m_storage.remove(r * NumColumns, NumColumns);
}
template <class T, int NumColumns>
void FixedColumnMatrix<T, NumColumns>::storageIndexToPosition(int idx, int *rowPtr, int *colPtr)
{
*rowPtr = idx / NumColumns;
*colPtr = idx % NumColumns;
}
} // namespace
// special values for unset fields; must not clash with values of FieldGrowthPolicy or
// RowWrapPolicy
const uint DefaultFieldGrowthPolicy = 255;
const uint DefaultRowWrapPolicy = 255;
enum { ColumnCount = 2 };
// -- our data structure for our items
// This owns the QLayoutItem
struct QFormLayoutItem
{
QFormLayoutItem(QLayoutItem* i) : item(i) { }
~QFormLayoutItem() { delete item; }
// Wrappers
QWidget *widget() const { return item->widget(); }
QLayout *layout() const { return item->layout(); }
bool hasHeightForWidth() const { return item->hasHeightForWidth(); }
int heightForWidth(int width) const { return item->heightForWidth(width); }
int minimumHeightForWidth(int width) const { return item->minimumHeightForWidth(width); }
Qt::Orientations expandingDirections() const { return item->expandingDirections(); }
QSizePolicy::ControlTypes controlTypes() const { return item->controlTypes(); }
int vStretch() const { return widget() ? widget()->sizePolicy().verticalStretch() : 0; }
void setGeometry(const QRect& r) { item->setGeometry(r); }
QRect geometry() const { return item->geometry(); }
void setVisible(bool on);
bool isHidden() const { return !isVisible || (widget() && widget()->isHidden()); }
// For use with FixedColumnMatrix
bool operator==(const QFormLayoutItem& other) { return item == other.item; }
QLayoutItem *item = nullptr;
bool fullRow = false;
bool isVisible = true;
// set by updateSizes
bool isHfw = false;
QSize minSize;
QSize sizeHint;
QSize maxSize;
// also set by updateSizes
int sbsHSpace = -1; // only used for side by side, for the field item only (not label)
int vSpace = -1; // This is the spacing to the item in the row above
// set by setupVerticalLayoutData
bool sideBySide = false;
int vLayoutIndex = -1;
// set by setupHorizontalLayoutData
int layoutPos = -1;
int layoutWidth = -1;
};
static void hideOrShowWidgetsInLayout(QLayout *layout, bool on)
{
for (int i = 0; i < layout->count(); ++i) {
QLayoutItem *item = layout->itemAt(i);
if (QWidget *widget = item->widget())
widget->setVisible(on);
else if (item->layout())
hideOrShowWidgetsInLayout(item->layout(), on);
}
}
void QFormLayoutItem::setVisible(bool on)
{
isVisible = on;
// Explicitly hide the widget so that it loses focus and
// doesn't automatically get shown again when this layout
// hides and shows.
if (widget()) {
widget()->setVisible(on);
return;
}
// Layouts can't be hidden, so we have to traverse the widgets
// inside and hide all of them so that they also lose focus.
if (layout())
hideOrShowWidgetsInLayout(layout(), on);
}
class QFormLayoutPrivate : public QLayoutPrivate
{
Q_DECLARE_PUBLIC(QFormLayout)
public:
typedef FixedColumnMatrix<QFormLayoutItem *, ColumnCount> ItemMatrix;
QFormLayoutPrivate();
~QFormLayoutPrivate() { }
int insertRow(int row);
void insertRows(int row, int count);
void removeRow(int row);
bool setItem(int row, QFormLayout::ItemRole role, QLayoutItem *item);
void setLayout(int row, QFormLayout::ItemRole role, QLayout *layout);
void setWidget(int row, QFormLayout::ItemRole role, QWidget *widget);
void arrangeWidgets(const QList<QLayoutStruct> &layouts, QRect &rect);
void updateSizes();
void setupVerticalLayoutData(int width);
void setupHorizontalLayoutData(int width);
QStyle* getStyle() const;
inline bool haveHfwCached(int width) const
{
return (hfw_width == width) || (width == sh_width && hfw_sh_height >= 0);
}
void recalcHFW(int w);
void setupHfwLayoutData();
uint fieldGrowthPolicy : 8;
uint rowWrapPolicy : 8;
uint has_hfw : 2;
uint dirty : 2; // have we laid out yet?
uint sizesDirty : 2; // have we (not) gathered layout item sizes?
uint expandVertical : 1; // Do we expand vertically?
uint expandHorizontal : 1; // Do we expand horizonally?
Qt::Alignment labelAlignment;
Qt::Alignment formAlignment;
ItemMatrix m_matrix;
QList<QFormLayoutItem *> m_things;
int layoutWidth = -1; // the last width that we called setupVerticalLayoutData on (for vLayouts)
int hfw_width = -1; // the last width we calculated HFW for
int hfw_height = -1; // what that height was
int hfw_sh_height = -1; // the hfw for sh_width
int hfw_sh_minheight = -1; // the minhfw for sh_width
int min_width = -1; // the width that gets turned into minSize (from updateSizes)
int sh_width = -1; // the width that gets turned into prefSize (from updateSizes)
int thresh_width = QLAYOUTSIZE_MAX; // the width that we start splitting label/field pairs at (from updateSizes)
QSize minSize;
QSize prefSize;
int formMaxWidth;
void calcSizeHints();
QList<QLayoutStruct> vLayouts; // set by setupVerticalLayoutData;
int vLayoutCount; // Number of rows we calculated in setupVerticalLayoutData
int maxLabelWidth; // the label width we calculated in setupVerticalLayoutData
QList<QLayoutStruct> hfwLayouts;
int hSpacing = -1;
int vSpacing = -1;
QLayoutItem* replaceAt(int index, QLayoutItem*) override;
};
QFormLayoutPrivate::QFormLayoutPrivate()
: fieldGrowthPolicy(DefaultFieldGrowthPolicy),
rowWrapPolicy(DefaultRowWrapPolicy), has_hfw(false), dirty(true), sizesDirty(true),
expandVertical(0), expandHorizontal(0)
{
}
static Qt::Alignment fixedAlignment(Qt::Alignment alignment, Qt::LayoutDirection layoutDirection)
{
if (layoutDirection == Qt::RightToLeft && alignment & Qt::AlignAbsolute) {
// swap left and right, and eliminate absolute flag
return Qt::Alignment((alignment & ~(Qt::AlignLeft | Qt::AlignRight | Qt::AlignAbsolute))
| ((alignment & Qt::AlignRight) ? Qt::AlignLeft : 0)
| ((alignment & Qt::AlignLeft) ? Qt::AlignRight : 0));
} else {
return alignment & ~Qt::AlignAbsolute;
}
}
static int storageIndexFromLayoutItem(const QFormLayoutPrivate::ItemMatrix &m,
QFormLayoutItem *item)
{
if (item) {
return m.storage().indexOf(item);
} else {
return -1;
}
}
static void updateFormLayoutItem(QFormLayoutItem *item, int userVSpacing,
QFormLayout::FieldGrowthPolicy fieldGrowthPolicy,
bool fullRow)
{
item->minSize = item->item->minimumSize();
item->sizeHint = item->item->sizeHint();
item->maxSize = item->item->maximumSize();
if (!fullRow && (fieldGrowthPolicy == QFormLayout::FieldsStayAtSizeHint
|| (fieldGrowthPolicy == QFormLayout::ExpandingFieldsGrow
&& !(item->item->expandingDirections() & Qt::Horizontal))))
item->maxSize.setWidth(item->sizeHint.width());
item->isHfw = item->item->hasHeightForWidth();
item->vSpace = userVSpacing;
}
/*
Iterate over all the controls and gather their size information
(min, sizeHint and max). Also work out what the spacing between
pairs of controls should be, and figure out the min and sizeHint
widths.
*/
void QFormLayoutPrivate::updateSizes()
{
Q_Q(QFormLayout);
if (sizesDirty) {
QFormLayout::RowWrapPolicy wrapPolicy = q->rowWrapPolicy();
bool wrapAllRows = (wrapPolicy == QFormLayout::WrapAllRows);
bool dontWrapRows = (wrapPolicy == QFormLayout::DontWrapRows);
int rr = m_matrix.rowCount();
has_hfw = false;
// If any control can expand, so can this layout
// Wrapping doesn't affect expansion, though, just the minsize
bool expandH = false;
bool expandV = false;
QFormLayoutItem *prevLbl = nullptr;
QFormLayoutItem *prevFld = nullptr;
QWidget *parent = q->parentWidget();
QStyle *style = parent ? parent->style() : nullptr;
int userVSpacing = q->verticalSpacing();
int userHSpacing = wrapAllRows ? 0 : q->horizontalSpacing();
int maxMinLblWidth = 0;
int maxMinFldWidth = 0; // field with label
int maxMinIfldWidth = 0; // independent field
int maxShLblWidth = 0;
int maxShFldWidth = 0;
int maxShIfldWidth = 0;
for (int i = 0; i < rr; ++i) {
QFormLayoutItem *label = m_matrix(i, 0);
QFormLayoutItem *field = m_matrix(i, 1);
// Skip empty rows
if (!label && !field)
continue;
if (label) {
updateFormLayoutItem(label, userVSpacing, q->fieldGrowthPolicy(), false);
if (label->isHfw)
has_hfw = true;
Qt::Orientations o = label->expandingDirections();
if (o & Qt::Vertical)
expandV = true;
if (o & Qt::Horizontal)
expandH = true;
}
if (field) {
updateFormLayoutItem(field, userVSpacing, q->fieldGrowthPolicy(), !label && field->fullRow);
field->sbsHSpace = (!label && field->fullRow) ? 0 : userHSpacing;
if (field->isHfw)
has_hfw = true;
Qt::Orientations o = field->expandingDirections();
if (o & Qt::Vertical)
expandV = true;
if (o & Qt::Horizontal)
expandH = true;
}
// See if we need to calculate default spacings
if ((userHSpacing < 0 || userVSpacing < 0) && style) {
QSizePolicy::ControlTypes lbltypes =
QSizePolicy::ControlTypes(label ? label->controlTypes() : QSizePolicy::DefaultType);
QSizePolicy::ControlTypes fldtypes =
QSizePolicy::ControlTypes(field ? field->controlTypes() : QSizePolicy::DefaultType);
// VSpacing
if (userVSpacing < 0) {
if (wrapAllRows) {
// label spacing is to a previous item
QFormLayoutItem *lbltop = prevFld ? prevFld : prevLbl;
// field spacing is to the label (or a previous item)
QFormLayoutItem *fldtop = label ? label : lbltop;
QSizePolicy::ControlTypes lbltoptypes =
QSizePolicy::ControlTypes(lbltop ? lbltop->controlTypes() : QSizePolicy::DefaultType);
QSizePolicy::ControlTypes fldtoptypes =
QSizePolicy::ControlTypes(fldtop ? fldtop->controlTypes() : QSizePolicy::DefaultType);
if (label && lbltop)
label->vSpace = style->combinedLayoutSpacing(lbltoptypes, lbltypes, Qt::Vertical, nullptr, parent);
if (field && fldtop)
field->vSpace = style->combinedLayoutSpacing(fldtoptypes, fldtypes, Qt::Vertical, nullptr, parent);
} else {
// Side by side.. we have to also consider the spacings to empty cells, which can strangely be more than
// non empty cells..
QFormLayoutItem *lbltop = prevLbl ? prevLbl : prevFld;
QFormLayoutItem *fldtop = prevFld;
QSizePolicy::ControlTypes lbltoptypes =
QSizePolicy::ControlTypes(lbltop ? lbltop->controlTypes() : QSizePolicy::DefaultType);
QSizePolicy::ControlTypes fldtoptypes =
QSizePolicy::ControlTypes(fldtop ? fldtop->controlTypes() : QSizePolicy::DefaultType);
// To be compatible to QGridLayout, we have to compare solitary labels & fields with both predecessors
if (label && !label->isHidden()) {
if (!field) {
int lblspacing = style->combinedLayoutSpacing(lbltoptypes, lbltypes, Qt::Vertical, nullptr, parent);
int fldspacing = style->combinedLayoutSpacing(fldtoptypes, lbltypes, Qt::Vertical, nullptr, parent);
label->vSpace = qMax(lblspacing, fldspacing);
} else {
label->vSpace = style->combinedLayoutSpacing(lbltoptypes, lbltypes, Qt::Vertical, nullptr, parent);
}
}
if (field && !field->isHidden()) {
// check spacing against both the previous label and field
if (!label) {
int lblspacing = style->combinedLayoutSpacing(lbltoptypes, fldtypes, Qt::Vertical, nullptr, parent);
int fldspacing = style->combinedLayoutSpacing(fldtoptypes, fldtypes, Qt::Vertical, nullptr, parent);
field->vSpace = qMax(lblspacing, fldspacing);
} else {
field->vSpace = style->combinedLayoutSpacing(fldtoptypes, fldtypes, Qt::Vertical, nullptr, parent);
}
}
}
}
// HSpacing
// hard-coded the left and right control types so that all the rows have the same
// inter-column spacing (otherwise the right column isn't always left aligned)
if (userHSpacing < 0 && !wrapAllRows && (label || !field->fullRow) && field)
field->sbsHSpace = style->combinedLayoutSpacing(QSizePolicy::Label, QSizePolicy::LineEdit, Qt::Horizontal, nullptr, parent);
}
// Now update our min/sizehint widths
// We choose to put the spacing in the field side in sbs, so
// the right edge of the labels will align, but fields may
// be a little ragged.. since different controls may have
// different appearances, a slight raggedness in the left
// edges of fields can be tolerated.
// (Note - field->sbsHSpace is 0 for WrapAllRows mode)
if (label) {
maxMinLblWidth = qMax(maxMinLblWidth, label->minSize.width());
maxShLblWidth = qMax(maxShLblWidth, label->sizeHint.width());
}
if (field) {
if (field->fullRow) {
maxMinIfldWidth = qMax(maxMinIfldWidth, field->minSize.width());
maxShIfldWidth = qMax(maxShIfldWidth, field->sizeHint.width());
} else {
maxMinFldWidth = qMax(maxMinFldWidth, field->minSize.width() + field->sbsHSpace);
maxShFldWidth = qMax(maxShFldWidth, field->sizeHint.width() + field->sbsHSpace);
}
}
prevLbl = label;
prevFld = field;
}
// Now, finally update the min/sizeHint widths
if (wrapAllRows) {
sh_width = qMax(maxShLblWidth, qMax(maxShIfldWidth, maxShFldWidth));
min_width = qMax(maxMinLblWidth, qMax(maxMinIfldWidth, maxMinFldWidth));
// in two line, we don't care as much about the threshold width
thresh_width = 0;
} else if (dontWrapRows) {
// This is just the max widths glommed together
sh_width = qMax(maxShLblWidth + maxShFldWidth, maxShIfldWidth);
min_width = qMax(maxMinLblWidth + maxMinFldWidth, maxMinIfldWidth);
thresh_width = QWIDGETSIZE_MAX;
} else {
// This is just the max widths glommed together
sh_width = qMax(maxShLblWidth + maxShFldWidth, maxShIfldWidth);
// min width needs to be the min when everything is wrapped,
// otherwise we'll never get set with a width that causes wrapping
min_width = qMax(maxMinLblWidth, qMax(maxMinIfldWidth, maxMinFldWidth));
// We split a pair at label sh + field min (### for now..)
thresh_width = maxShLblWidth + maxMinFldWidth;
}
// Update the expansions
expandVertical = expandV;
expandHorizontal = expandH;
}
sizesDirty = false;
}
void QFormLayoutPrivate::recalcHFW(int w)
{
setupHfwLayoutData();
int h = 0;
int mh = 0;
for (int r = 0; r < vLayoutCount; ++r) {
int spacing = hfwLayouts.at(r).spacing;
h += hfwLayouts.at(r).sizeHint + spacing;
mh += hfwLayouts.at(r).minimumSize + spacing;
}
if (sh_width > 0 && sh_width == w) {
hfw_sh_height = qMin(QLAYOUTSIZE_MAX, h);
hfw_sh_minheight = qMin(QLAYOUTSIZE_MAX, mh);
} else {
hfw_width = w;
hfw_height = qMin(QLAYOUTSIZE_MAX, h);
}
}
void QFormLayoutPrivate::setupHfwLayoutData()
{
// setupVerticalLayoutData must be called before this
// setupHorizontalLayoutData must also be called before this
// copies non hfw data into hfw
// then updates size and min
// Note: QGridLayout doesn't call minimumHeightForWidth,
// but instead uses heightForWidth for both min and sizeHint.
// For the common case where minimumHeightForWidth just calls
// heightForWidth, we do the calculation twice, which can be
// very expensive for word wrapped QLabels/QTextEdits, for example.
// So we just use heightForWidth as well.
int i;
int rr = m_matrix.rowCount();
hfwLayouts.clear();
hfwLayouts.resize(vLayoutCount);
for (i = 0; i < vLayoutCount; ++i)
hfwLayouts[i] = vLayouts.at(i);
for (i = 0; i < rr; ++i) {
QFormLayoutItem *label = m_matrix(i, 0);
QFormLayoutItem *field = m_matrix(i, 1);
if (label && label->vLayoutIndex > -1) {
if (label->isHfw) {
// We don't check sideBySide here, since a label is only
// ever side by side with its field
int hfw = label->heightForWidth(label->layoutWidth);
hfwLayouts[label->vLayoutIndex].minimumSize = hfw;
hfwLayouts[label->vLayoutIndex].sizeHint = hfw;
} else {
// Reset these here, so the field can do a qMax below (the previous value may have
// been the fields non-hfw values, which are often larger than hfw)
hfwLayouts[label->vLayoutIndex].sizeHint = label->sizeHint.height();
hfwLayouts[label->vLayoutIndex].minimumSize = label->minSize.height();
}
}
if (field && field->vLayoutIndex > -1) {
int hfw = field->isHfw ? field->heightForWidth(field->layoutWidth) : 0;
int h = field->isHfw ? hfw : field->sizeHint.height();
int mh = field->isHfw ? hfw : field->minSize.height();
if (field->sideBySide) {
int oh = hfwLayouts.at(field->vLayoutIndex).sizeHint;
int omh = hfwLayouts.at(field->vLayoutIndex).minimumSize;
hfwLayouts[field->vLayoutIndex].sizeHint = qMax(h, oh);
hfwLayouts[field->vLayoutIndex].minimumSize = qMax(mh, omh);
} else {
hfwLayouts[field->vLayoutIndex].sizeHint = h;
hfwLayouts[field->vLayoutIndex].minimumSize = mh;
}
}
}
}
/*
Given up to four items involved in a vertical spacing calculation
(two rows * two columns), return the max vertical spacing for the
row containing item1 (which may also include item2)
We assume parent and item1 are not null.
If a particular row is split, then the spacings for that row and
the following row are affected, and this function should be
called with recalculate = true for both rows (note: only rows with both
a label and a field can be split).
In particular:
1) the split label's row vspace needs to be changed to qMax(label/prevLabel, label/prevField)
[call with item1 = label, item2 = null, prevItem1 & prevItem2 as before]
2) the split field's row vspace needs to be changed to the label/field spacing
[call with item1 = field, item2 = null, prevItem1 = label, prevItem2 = null]
[if the next row has one item, 'item']
3a) the following row's vspace needs to be changed to item/field spacing (would
previously been the qMax(item/label, item/field) spacings)
[call with item1 = item, item2 = null, prevItem1 = field, prevItem2 = null]
[if the next row has two items, 'label2' and 'field2']
3b) the following row's vspace needs to be changed to be qMax(field/label2, field/field2) spacing
[call with item1 = label2, item2 = field2, prevItem1 = field, prevItem2 = null]
In the (common) non split case, we can just use the precalculated vspace (possibly qMaxed between
label and field).
If recalculate is true, we expect:
- parent != null
- item1 != null
- item2 can be null
- prevItem1 can be null
- if item2 is not null, prevItem2 will be null (e.g. steps 1 or 3 above)
- if prevItem1 is null, prevItem2 will be null
*/
static inline int spacingHelper(QWidget* parent, QStyle *style, int userVSpacing, bool recalculate, QFormLayoutItem* item1, QFormLayoutItem* item2, QFormLayoutItem* prevItem1, QFormLayoutItem *prevItem2)
{
int spacing = userVSpacing;
if (spacing < 0) {
if (!recalculate) {
if (item1)
spacing = item1->vSpace;
if (item2)
spacing = qMax(spacing, item2->vSpace);
} else {
if (style && prevItem1) {
QSizePolicy::ControlTypes itemtypes =
QSizePolicy::ControlTypes(item1 ? item1->controlTypes() : QSizePolicy::DefaultType);
int spacing2 = 0;
spacing = style->combinedLayoutSpacing(itemtypes, prevItem1->controlTypes(), Qt::Vertical, nullptr, parent);
// At most of one of item2 and prevItem2 will be nonnull
if (item2)
spacing2 = style->combinedLayoutSpacing(item2->controlTypes(), prevItem1->controlTypes(), Qt::Vertical, nullptr, parent);
else if (prevItem2)
spacing2 = style->combinedLayoutSpacing(itemtypes, prevItem2->controlTypes(), Qt::Vertical, nullptr, parent);
spacing = qMax(spacing, spacing2);
}
}
} else {
if (prevItem1) {
QWidget *wid = prevItem1->item->widget();
if (wid)
spacing = qMax(spacing, prevItem1->geometry().top() - wid->geometry().top() );
}
if (prevItem2) {
QWidget *wid = prevItem2->item->widget();
if (wid)
spacing = qMax(spacing, prevItem2->geometry().top() - wid->geometry().top() );
}
}
return qMax(spacing, 0);
}
static inline void initLayoutStruct(QLayoutStruct& sl, QFormLayoutItem* item)
{
sl.init(item->vStretch(), item->minSize.height());
sl.sizeHint = item->sizeHint.height();
sl.maximumSize = item->maxSize.height();
sl.expansive = (item->expandingDirections() & Qt::Vertical);
sl.empty = false;
}
void QFormLayoutPrivate::setupVerticalLayoutData(int width)
{
Q_Q(QFormLayout);
// Early out if we have no changes that would cause a change in vertical layout
if ((width == layoutWidth || (width >= thresh_width && layoutWidth >= thresh_width)) && !dirty && !sizesDirty)
return;
layoutWidth = width;
int rr = m_matrix.rowCount();
int vidx = 1;
QFormLayout::RowWrapPolicy rowWrapPolicy = q->rowWrapPolicy();
bool wrapAllRows = (rowWrapPolicy == QFormLayout::WrapAllRows);
bool addTopBottomStretch = true;
vLayouts.clear();
vLayouts.resize((2 * rr) + 2); // a max, some may be unused
QStyle *style = nullptr;
int userVSpacing = q->verticalSpacing();
if (userVSpacing < 0) {
if (QWidget *widget = q->parentWidget())
style = widget->style();
}
// make sure our sizes are up to date
updateSizes();
// Grab the widest label width here
// This might be different from the value computed during
// sizeHint/minSize, since we don't count label/field pairs that
// are split.
maxLabelWidth = 0;
if (!wrapAllRows) {
for (int i = 0; i < rr; ++i) {
const QFormLayoutItem *label = m_matrix(i, 0);
const QFormLayoutItem *field = m_matrix(i, 1);
if (label && (label->sizeHint.width() + (field ? field->minSize.width() : 0) <= width))
maxLabelWidth = qMax(maxLabelWidth, label->sizeHint.width());
}
} else {
maxLabelWidth = width;
}
QFormLayoutItem *prevItem1 = nullptr;
QFormLayoutItem *prevItem2 = nullptr;
bool prevRowSplit = false;
for (int i = 0; i < rr; ++i) {
QFormLayoutItem *label = m_matrix(i, 0);
QFormLayoutItem *field = m_matrix(i, 1);
// Totally ignore empty rows or rows with only hidden items
if (!q->isRowVisible(i))
continue;
QSize min1;
QSize min2;
QSize sh1;
QSize sh2;
if (label) {
min1 = label->minSize;
sh1 = label->sizeHint;
}
if (field) {
min2 = field->minSize;
sh2 = field->sizeHint;
}
// In separate lines, we make a vLayout for everything that isn't null
// in side by side, we only separate label/field if we're going to wrap it
bool splitSideBySide = (rowWrapPolicy == QFormLayout::WrapLongRows)
&& ((maxLabelWidth < sh1.width()) || (width < (maxLabelWidth + min2.width())));
if (wrapAllRows || splitSideBySide) {
if (label) {
initLayoutStruct(vLayouts[vidx], label);
if (vidx > 1)
vLayouts[vidx - 1].spacing = spacingHelper(q->parentWidget(), style, userVSpacing, splitSideBySide || prevRowSplit, label, nullptr, prevItem1, prevItem2);
label->vLayoutIndex = vidx;
label->sideBySide = false;
prevItem1 = label;
prevItem2 = nullptr;
if (vLayouts[vidx].stretch > 0)
addTopBottomStretch = false;
++vidx;
}
if (field) {
initLayoutStruct(vLayouts[vidx], field);
if (vidx > 1)
vLayouts[vidx - 1].spacing = spacingHelper(q->parentWidget(), style, userVSpacing, splitSideBySide || prevRowSplit, field, nullptr, prevItem1, prevItem2);
field->vLayoutIndex = vidx;
field->sideBySide = false;
prevItem1 = field;
prevItem2 = nullptr;
if (vLayouts[vidx].stretch > 0)
addTopBottomStretch = false;
++vidx;
}
prevRowSplit = splitSideBySide;
} else {
// we're in side by side mode, and we have enough space to do that
QSize max1(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
QSize max2(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
int stretch1 = 0;
int stretch2 = 0;
bool expanding = false;
if (label) {
max1 = label->maxSize;
if (label->expandingDirections() & Qt::Vertical)
expanding = true;
label->sideBySide = (field != nullptr);
label->vLayoutIndex = vidx;
stretch1 = label->vStretch();
}
if (field) {
max2 = field->maxSize;
if (field->expandingDirections() & Qt::Vertical)
expanding = true;
field->sideBySide = (label || !field->fullRow);
field->vLayoutIndex = vidx;
stretch2 = field->vStretch();
}
vLayouts[vidx].init(qMax(stretch1, stretch2), qMax(min1.height(), min2.height()));
vLayouts[vidx].sizeHint = qMax(sh1.height(), sh2.height());
vLayouts[vidx].maximumSize = qMin(max1.height(), max2.height());
vLayouts[vidx].expansive = expanding || (vLayouts[vidx].stretch > 0);
vLayouts[vidx].empty = false;
if (vLayouts[vidx].expansive)
addTopBottomStretch = false;
if (vidx > 1)
vLayouts[vidx - 1].spacing = spacingHelper(q->parentWidget(), style, userVSpacing, prevRowSplit, label, field, prevItem1, prevItem2);
if (label) {
prevItem1 = label;
prevItem2 = field;
} else {
prevItem1 = field;
prevItem2 = nullptr;
}
prevRowSplit = false;
++vidx;
}
}
if (addTopBottomStretch) {
Qt::Alignment formAlignment = q->formAlignment();
if (!(formAlignment & Qt::AlignBottom)) {
// AlignTop (default if unspecified) or AlignVCenter: We add a stretch at the bottom
vLayouts[vidx].init(1, 0);
vLayouts[vidx].expansive = true;
++vidx;
}
if (formAlignment & (Qt::AlignVCenter | Qt::AlignBottom)) {
// AlignVCenter or AlignBottom: We add a stretch at the top
vLayouts[0].init(1, 0);
vLayouts[0].expansive = true;
} else {
vLayouts[0].init(0, 0);
}
} else {
vLayouts[0].init(0, 0);
}
vLayoutCount = vidx;
dirty = false;
}
void QFormLayoutPrivate::setupHorizontalLayoutData(int width)
{
Q_Q(QFormLayout);
// requires setupVerticalLayoutData to be called first
int fieldMaxWidth = 0;
int rr = m_matrix.rowCount();
bool wrapAllRows = (q->rowWrapPolicy() == QFormLayout::WrapAllRows);
for (int i = 0; i < rr; ++i) {
QFormLayoutItem *label = m_matrix(i, 0);
QFormLayoutItem *field = m_matrix(i, 1);
// Totally ignore empty rows...
if (!label && !field)
continue;
if (label) {
// if there is a field, and we're side by side, we use maxLabelWidth
// otherwise we just use the sizehint
label->layoutWidth = (field && label->sideBySide) ? maxLabelWidth : label->sizeHint.width();
label->layoutPos = 0;
}
if (field) {
// This is the default amount allotted to fields in sbs
int fldwidth = width - maxLabelWidth - field->sbsHSpace;
// If we've split a row, we still decide to align
// the field with all the other field if it will fit
// Fields in sbs mode get the remnants of the maxLabelWidth
if (!field->sideBySide) {
if (wrapAllRows || (!label && field->fullRow) || field->sizeHint.width() > fldwidth) {
field->layoutWidth = width;
field->layoutPos = 0;
} else {
field->layoutWidth = fldwidth;
field->layoutPos = width - fldwidth;
}
} else {
// We're sbs, so we should have a label
field->layoutWidth = fldwidth;
field->layoutPos = width - fldwidth;
}
fieldMaxWidth = qMax(fieldMaxWidth, field->maxSize.width());
}
}
formMaxWidth = maxLabelWidth + fieldMaxWidth;
}
void QFormLayoutPrivate::calcSizeHints()
{
Q_Q(QFormLayout);
int leftMargin, topMargin, rightMargin, bottomMargin;
q->getContentsMargins(&leftMargin, &topMargin, &rightMargin, &bottomMargin);
updateSizes();
setupVerticalLayoutData(QLAYOUTSIZE_MAX);
// Don't need to call setupHorizontal here
int h = topMargin + bottomMargin;
int mh = topMargin + bottomMargin;
// The following are set in updateSizes
int w = sh_width + leftMargin + rightMargin;
int mw = min_width + leftMargin + rightMargin;
for (int i = 0; i < vLayoutCount; ++i) {
int spacing = vLayouts.at(i).spacing;
h += vLayouts.at(i).sizeHint + spacing;
mh += vLayouts.at(i).minimumSize + spacing;
}
minSize.rwidth() = qMin(mw, QLAYOUTSIZE_MAX);
minSize.rheight() = qMin(mh, QLAYOUTSIZE_MAX);
prefSize.rwidth() = qMin(w, QLAYOUTSIZE_MAX);
prefSize.rheight() = qMin(h, QLAYOUTSIZE_MAX);
}
int QFormLayoutPrivate::insertRow(int row)
{
int rowCnt = m_matrix.rowCount();
if (uint(row) > uint(rowCnt))
row = rowCnt;
insertRows(row, 1);
return row;
}
void QFormLayoutPrivate::insertRows(int row, int count)
{
while (count > 0) {
m_matrix.insertRow(row, 0);
--count;
}
}
void QFormLayoutPrivate::removeRow(int row)
{
if (uint(row) < uint(m_matrix.rowCount()))
m_matrix.removeRow(row);
}
bool QFormLayoutPrivate::setItem(int row, QFormLayout::ItemRole role, QLayoutItem *item)
{
const bool fullRow = role == QFormLayout::SpanningRole;
const int column = role == QFormLayout::SpanningRole ? 1 : static_cast<int>(role);
if (Q_UNLIKELY(uint(row) >= uint(m_matrix.rowCount()) || uint(column) > 1U)) {
qWarning("QFormLayoutPrivate::setItem: Invalid cell (%d, %d)", row, column);
return false;
}
if (!item)
return false;
if (Q_UNLIKELY(m_matrix(row, column))) {
qWarning("QFormLayoutPrivate::setItem: Cell (%d, %d) already occupied", row, column);
return false;
}
QFormLayoutItem *i = new QFormLayoutItem(item);
i->fullRow = fullRow;
m_matrix(row, column) = i;
m_things.append(i);
return true;
}
void QFormLayoutPrivate::setLayout(int row, QFormLayout::ItemRole role, QLayout *layout)
{
if (layout) {
Q_Q(QFormLayout);
if (q->adoptLayout(layout))
setItem(row, role, layout);
}
}