-
Notifications
You must be signed in to change notification settings - Fork 345
/
mythuibuttonlist.cpp
3667 lines (3036 loc) · 98.3 KB
/
mythuibuttonlist.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
#include "mythuibuttonlist.h"
#include <math.h>
// QT headers
#include <QCoreApplication>
#include <QDomDocument>
#include <QKeyEvent>
// libmyth headers
#include "mythlogging.h"
// mythui headers
#include "mythmainwindow.h"
#include "mythuiscrollbar.h"
#include "mythuistatetype.h"
#include "lcddevice.h"
#include "mythuibutton.h"
#include "mythuitext.h"
#include "mythuitextedit.h"
#define LOC QString("MythUIButtonList(%1): ").arg(objectName())
MythUIButtonList::MythUIButtonList(MythUIType *parent, const QString &name)
: MythUIType(parent, name)
{
m_showArrow = true;
m_showScrollBar = true;
Const();
}
MythUIButtonList::MythUIButtonList(MythUIType *parent, const QString &name,
const QRect &area, bool showArrow,
bool showScrollArrows, bool showScrollBar)
: MythUIType(parent, name)
{
m_Area = area;
m_showArrow = showArrow;
m_showScrollBar = showScrollBar;
Const();
}
void MythUIButtonList::Const(void)
{
m_contentsRect = MythRect(0, 0, 0, 0);
m_layout = LayoutVertical;
m_arrange = ArrangeFixed;
m_alignment = Qt::AlignLeft | Qt::AlignTop;
m_scrollStyle = ScrollFree;
m_wrapStyle = WrapNone;
m_active = false;
m_drawFromBottom = false;
m_selPosition = 0;
m_topPosition = 0;
m_itemCount = 0;
m_keepSelAtBottom = false;
m_initialized = false;
m_needsUpdate = false;
m_clearing = false;
m_itemHorizSpacing = 0;
m_itemVertSpacing = 0;
m_itemHeight = 0;
m_itemWidth = 0;
m_itemsVisible = 0;
m_maxVisible = 0;
m_columns = 0;
m_rows = 0;
m_leftColumns = 0;
m_rightColumns = 0;
m_topRows = 0;
m_bottomRows = 0;
m_lcdTitle = "";
m_searchPosition = MythPoint(-2, -2);
m_searchFields = "**ALL**";
m_searchStartsWith = false;
m_upArrow = m_downArrow = NULL;
m_scrollBar = NULL;
m_buttontemplate = NULL;
m_nextItemLoaded = 0;
SetCanTakeFocus(true);
connect(this, SIGNAL(TakingFocus()), this, SLOT(Select()));
connect(this, SIGNAL(LosingFocus()), this, SLOT(Deselect()));
}
MythUIButtonList::~MythUIButtonList()
{
m_ButtonToItem.clear();
m_clearing = true;
while (!m_itemList.isEmpty())
delete m_itemList.takeFirst();
}
void MythUIButtonList::Select()
{
MythUIButtonListItem *item = GetItemCurrent();
if (item)
emit itemSelected(item);
SetActive(true);
}
void MythUIButtonList::Deselect()
{
SetActive(false);
}
void MythUIButtonList::SetDrawFromBottom(bool draw)
{
m_drawFromBottom = draw;
}
void MythUIButtonList::SetActive(bool active)
{
if (m_active == active)
return;
m_active = active;
if (m_initialized)
Update();
}
/*!
* \copydoc MythUIType::Reset()
*/
void MythUIButtonList::Reset()
{
m_ButtonToItem.clear();
if (m_itemList.isEmpty())
return;
m_clearing = true;
while (!m_itemList.isEmpty())
delete m_itemList.takeFirst();
m_clearing = false;
m_selPosition = 0;
m_topPosition = 0;
m_itemCount = 0;
StopLoad();
Update();
MythUIType::Reset();
}
void MythUIButtonList::Update()
{
m_needsUpdate = true;
SetRedraw();
}
/*
* The "width" of a button determines it relative position when using
* Dynamic-Layout.
*
* If a button has a negative offset, that offset needs accounted for
* to position the button in proper releation to the surrounding buttons.
*/
int MythUIButtonList::minButtonWidth(const MythRect &area)
{
int width = area.width();
if (area.x() < 0)
{
/*
* Assume if an overlap is allowed on the left, the same overlap
* is on the right
*/
width += (area.x() * 2 - 1); // x is negative
while (width < 0)
width -= area.x(); // Oops
}
else if (m_layout == LayoutHorizontal)
width -= area.x(); // Get rid of any "space" betwen the buttons
return width;
}
/*
* The "height" of a button determines it relative position when using
* Dynamic-Layout.
*
* If a button has a negative offset, that offset needs accounted for
* to position the button in proper releation to the surrounding buttons.
*/
int MythUIButtonList::minButtonHeight(const MythRect &area)
{
int height = area.height();
if (area.y() < 0)
{
/*
* Assume if an overlap is allowed on the top, the same overlap
* is on the bottom
*/
height += (area.y() * 2 - 1);
while (height < 0)
height -= area.y(); // Oops
}
else if (m_layout == LayoutVertical)
height -= area.y(); // Get rid of any "space" betwen the buttons
return height;
}
/*
* For Dynamic-Layout, buttons are allocated as needed. If the list is
* being redrawn, re-use any previously allocated buttons.
*/
MythUIGroup *MythUIButtonList::PrepareButton(int buttonIdx, int itemIdx,
int &selectedIdx,
int &button_shift)
{
MythUIStateType *realButton;
MythUIGroup *buttonstate;
MythUIButtonListItem *buttonItem = m_itemList[itemIdx];
buttonIdx += button_shift;
if (buttonIdx < 0 || buttonIdx + 1 > m_maxVisible)
{
QString name = QString("buttonlist button %1").arg(m_maxVisible);
MythUIStateType *button = new MythUIStateType(this, name);
button->CopyFrom(m_buttontemplate);
button->ConnectDependants(true);
if (buttonIdx < 0)
{
/*
* If a new button is needed in the front of the list, previously
* existing buttons need shifted to the right.
*/
m_ButtonList.prepend(button);
buttonIdx = 0;
++button_shift;
if (selectedIdx >= 0)
++selectedIdx;
}
else
m_ButtonList.append(button);
++m_maxVisible;
}
realButton = m_ButtonList[buttonIdx];
m_ButtonToItem[buttonIdx] = buttonItem;
buttonItem->SetToRealButton(realButton, itemIdx == m_selPosition);
buttonstate = dynamic_cast<MythUIGroup *>(realButton->GetCurrentState());
if (itemIdx == m_selPosition)
selectedIdx = buttonIdx;
return buttonstate;
}
/*
* Dynamically layout the buttons on a row.
*/
bool MythUIButtonList::DistributeRow(int &first_button, int &last_button,
int &first_item, int &last_item,
int &selected_column, int &skip_cols,
bool grow_left, bool grow_right,
int **col_widths, int &row_height,
int total_height, int split_height,
int &col_cnt, bool &wrapped)
{
MythUIGroup *buttonstate;
int initial_first_button, initial_last_button;
int initial_first_item, initial_last_item;
int col_idx, left_cnt = 0;
int width, height;
int max_width, max_height;
int left_width, right_width;
int begin, end;
bool underflow = false; // keep from being uninitialized
bool added;
bool hsplit, vsplit;
int selectedIdx;
int button_shift;
selectedIdx = -1;
button_shift = 0;
col_cnt = 1;
skip_cols = 0;
if (last_item + 1 > m_itemCount || last_item < 0 || first_item < 0)
return false;
/*
* Allocate a button on the row. With a vertical layout, there is
* only one button per row, and this would be it.
*/
if (grow_right)
buttonstate = PrepareButton(last_button, last_item,
selectedIdx, button_shift);
else
buttonstate = PrepareButton(first_button, first_item,
selectedIdx, button_shift);
if (buttonstate == NULL)
{
LOG(VB_GENERAL, LOG_ERR, QString("Failed to query buttonlist state: %1")
.arg(last_button));
return false;
}
// Note size of initial button.
max_width = m_contentsRect.width();
max_height = m_contentsRect.height();
row_height = minButtonHeight(buttonstate->GetArea());
width = minButtonWidth(buttonstate->GetArea());
/*
* If the selected button should be centered, don't allow new buttons
* to take up more than half the allowed area.
*/
vsplit = (m_scrollStyle == ScrollCenter);
hsplit = vsplit && grow_left && grow_right;
if (hsplit)
{
max_width /= 2;
left_width = right_width = (width / 2);
}
else
{
if (grow_right)
{
left_width = 0;
right_width = width;
}
else
{
left_width = width;
right_width = 0;
}
}
if (vsplit)
max_height /= 2;
/*
* If total_height == 0, then this is the first row, so allow any height.
* Otherwise, If adding a button to a column would exceed the
* parent height, abort
*/
if (total_height > 0 &&
((vsplit ? split_height : total_height) +
m_itemVertSpacing + row_height > max_height))
{
LOG(VB_GUI, LOG_DEBUG,
QString("%1 Height exceeded %2 + (%3) + %4 = %5 which is > %6")
.arg(vsplit ? "Centering" : "Total")
.arg(split_height).arg(m_itemVertSpacing).arg(row_height)
.arg(split_height + m_itemVertSpacing + row_height)
.arg(max_height));
first_button += button_shift;
last_button += button_shift;
return false;
}
LOG(VB_GUI, LOG_DEBUG, QString("Added button item %1 width %2 height %3")
.arg(grow_right ? last_item : first_item)
.arg(width).arg(row_height));
initial_first_button = first_button;
initial_last_button = last_button;
initial_first_item = first_item;
initial_last_item = last_item;
/*
* if col_widths is not NULL, then grow_left & grow_right
* are mutually exclusive. So, col_idx can be anchored from
* the left or right.
*/
if (grow_right)
col_idx = 0;
else
col_idx = m_columns - 1;
// Add butons until no more fit.
added = (m_layout != LayoutVertical);
while (added)
{
added = false;
// If a grid, maintain same number of columns on each row.
if (grow_right && col_cnt < m_columns)
{
if (wrapped)
end = first_item;
else
{
// Are we allowed to wrap when we run out of items?
if (m_wrapStyle == WrapItems &&
(hsplit || m_scrollStyle != ScrollFree) &&
last_item + 1 == m_itemCount)
{
last_item = -1;
wrapped = true;
end = first_item;
}
else
end = m_itemCount;
}
if (last_item + 1 < end)
{
// Allocate next button to the right.
buttonstate = PrepareButton(last_button + 1, last_item + 1,
selectedIdx, button_shift);
if (buttonstate == NULL)
continue;
width = minButtonWidth(buttonstate->GetArea());
// For grids, use the widest button in a column
if (*col_widths && width < (*col_widths)[col_idx])
width = (*col_widths)[col_idx];
// Does the button fit?
if ((hsplit ? right_width : left_width + right_width) +
m_itemHorizSpacing + width > max_width)
{
int total = hsplit ? right_width : left_width + right_width;
LOG(VB_GUI, LOG_DEBUG,
QString("button on right would exceed width: "
"%1+(%2)+%3 == %4 which is > %5")
.arg(total).arg(m_itemHorizSpacing).arg(width)
.arg(total + m_itemHorizSpacing + width)
.arg(max_width));
}
else
{
added = true;
++col_cnt;
++last_button;
++last_item;
++col_idx;
right_width += m_itemHorizSpacing + width;
height = minButtonHeight(buttonstate->GetArea());
if (row_height < height)
row_height = height;
LOG(VB_GUI, LOG_DEBUG,
QString("Added button item %1 "
"R.width %2 height %3 total width %4+%5"
" (max %6)")
.arg(last_item).arg(width).arg(height)
.arg(left_width).arg(right_width).arg(max_width));
}
}
else
underflow = true;
}
// If a grid, maintain same number of columns on each row.
if (grow_left && col_cnt < m_columns)
{
if (wrapped)
end = last_item + 1;
else
{
// Are we allowed to wrap when we run out of items?
if (m_wrapStyle == WrapItems &&
(hsplit || m_scrollStyle != ScrollFree) &&
first_item == 0)
{
first_item = m_itemCount;
wrapped = true;
end = last_item + 1;
}
else
end = 0;
}
if (first_item > end)
{
buttonstate = PrepareButton(first_button - 1, first_item - 1,
selectedIdx, button_shift);
if (buttonstate == NULL)
continue;
width = minButtonWidth(buttonstate->GetArea());
// For grids, use the widest button in a column
if (*col_widths && width < (*col_widths)[col_idx])
width = (*col_widths)[col_idx];
// Does the button fit?
if ((hsplit ? left_width : left_width + right_width) +
m_itemHorizSpacing + width > max_width)
{
int total = hsplit ? left_width : left_width + right_width;
LOG(VB_GUI, LOG_DEBUG,
QString("button on left would exceed width: "
"%1+(%2)+%3 == %4 which is > %5")
.arg(total).arg(m_itemHorizSpacing).arg(width)
.arg(total + m_itemHorizSpacing + width)
.arg(max_width));
}
else
{
added = true;
--first_button;
--first_item;
--col_idx;
++col_cnt;
++left_cnt;
left_width += m_itemHorizSpacing + width;
height = minButtonHeight(buttonstate->GetArea());
if (row_height < height)
row_height = height;
LOG(VB_GUI, LOG_DEBUG,
QString("Added button item %1 "
"L.width %2 height %3 total width %4+%5"
" (max %6)")
.arg(first_item).arg(width).arg(height)
.arg(left_width).arg(right_width).arg(max_width));
}
}
else
{
underflow = true;
if (m_layout == LayoutGrid)
skip_cols = m_columns - col_cnt;
}
}
}
/*
* If total_height == 0, then this is the first row, so allow any height.
* Otherwise, If adding a button to a column would exceed the
* parent height, abort
*/
if (total_height > 0 &&
((vsplit ? split_height : total_height) +
m_itemVertSpacing + row_height > max_height))
{
LOG(VB_GUI, LOG_DEBUG,
QString("%1 Height exceeded %2 + (%3) + %4 = %5 which is > %6")
.arg(vsplit ? "Centering" : "Total")
.arg(split_height).arg(m_itemVertSpacing).arg(row_height)
.arg(split_height + m_itemVertSpacing + row_height)
.arg(max_height));
first_button = initial_first_button + button_shift;
last_button = initial_last_button + button_shift;
first_item = initial_first_item;
last_item = initial_last_item;
return false;
}
if (*col_widths == 0)
{
/*
* Allocate array to hold columns widths, now that we know
* how many columns there are.
*/
*col_widths = new int[col_cnt];
for (col_idx = 0; col_idx < col_cnt; ++col_idx)
(*col_widths)[col_idx] = 0;
}
// Adjust for insertions on the front.
first_button += button_shift;
last_button += button_shift;
// It fits, so so note max column widths
MythUIStateType *realButton;
int buttonIdx;
if (grow_left)
{
begin = first_button;
end = first_button + col_cnt;
}
else
{
end = last_button + 1;
begin = end - col_cnt;
}
for (buttonIdx = begin, col_idx = 0;
buttonIdx < end; ++buttonIdx, ++col_idx)
{
realButton = m_ButtonList[buttonIdx];
buttonstate = dynamic_cast<MythUIGroup *>
(realButton->GetCurrentState());
if (!buttonstate)
break;
width = minButtonWidth(buttonstate->GetArea());
if ((*col_widths)[col_idx] < width)
(*col_widths)[col_idx] = width;
// Make note of which column has the selected button
if (selectedIdx == buttonIdx)
selected_column = col_idx;
}
/*
An underflow indicates we ran out of items, not that the
buttons did not fit on the row.
*/
if (total_height && underflow && col_cnt < m_columns)
col_cnt = m_columns;
return true;
}
/*
* Dynamically layout columns
*/
bool MythUIButtonList::DistributeCols(int &first_button, int &last_button,
int &first_item, int &last_item,
int &selected_column, int &selected_row,
int &skip_cols, int **col_widths,
QList<int> & row_heights,
int &top_height, int &bottom_height,
bool &wrapped)
{
int col_cnt;
int height;
int row_cnt = 1;
int end;
bool added;
do
{
added = false;
if (wrapped)
end = first_item;
else
{
// Are we allowed to wrap when we run out of items?
if (m_wrapStyle == WrapItems &&
(m_scrollStyle == ScrollCenter ||
m_scrollStyle == ScrollGroupCenter) &&
last_item + 1 == m_itemCount)
{
last_item = -1;
wrapped = true;
end = first_item;
}
else
end = m_itemCount;
}
if (last_item + 1 < end)
{
// Does another row fit?
if (DistributeRow(first_button, ++last_button,
first_item, ++last_item, selected_column,
skip_cols, false, true, col_widths, height,
top_height + bottom_height, bottom_height,
col_cnt, wrapped))
{
if (col_cnt < m_columns)
return false; // Need to try again with fewer cols
if (selected_row == -1 && selected_column != -1)
selected_row = row_heights.size();
++row_cnt;
row_heights.push_back(height);
bottom_height += (height + m_itemVertSpacing);
added = true;
}
else
{
--last_button;
--last_item;
}
}
if (wrapped)
end = last_item + 1;
else
{
// Are we allowed to wrap when we run out of items?
if (m_wrapStyle == WrapItems &&
(m_scrollStyle == ScrollCenter ||
m_scrollStyle == ScrollGroupCenter) &&
first_item == 0)
{
first_item = m_itemCount;
wrapped = true;
end = last_item + 1;
}
else
end = 0;
}
if (first_item > end)
{
// Can we insert another row?
if (DistributeRow(--first_button, last_button,
--first_item, last_item, selected_column,
skip_cols, true, false, col_widths, height,
top_height + bottom_height, top_height,
col_cnt, wrapped))
{
if (col_cnt < m_columns)
return false; // Need to try again with fewer cols
if (selected_row == -1 && selected_column != -1)
selected_row = row_heights.size();
else if (selected_row != -1)
++selected_row;
++row_cnt;
row_heights.push_front(height);
top_height += (height + m_itemVertSpacing);
added = true;
}
else
{
++first_button;
++first_item;
}
}
}
while (added);
return true;
}
/*
* Dynamically layout as many buttons as will fit in the area.
*/
bool MythUIButtonList::DistributeButtons(void)
{
int first_button, last_button, start_button, start_item;
int first_item, last_item, skip_cols = 0;
int *col_widths;
int col_cnt;
int selected_column, selected_row;
bool wrapped = false;
bool grow_left = true;
QList<int> row_heights;
QList<int>::iterator Iheight;
int height, top_height, bottom_height;
start_item = m_selPosition;
selected_column = selected_row = -1;
top_height = bottom_height = 0;
col_widths = 0;
first_button = last_button = start_button = 0;
LOG(VB_GUI, LOG_DEBUG, QString("DistributeButtons: "
"selected item %1 total items %2")
.arg(start_item).arg(m_itemCount));
/*
* Try fewer and fewer columns until each row can fit the same
* number of columns.
*/
for (m_columns = m_itemCount; m_columns > 0;)
{
first_item = last_item = start_item;
/*
* Drawing starts at start_button, and radiates from there.
* Attempt to pick a start_button which will minimize the need for new
* button allocations.
*/
switch (m_scrollStyle)
{
case ScrollCenter:
case ScrollGroupCenter:
start_button = qMax((m_maxVisible / 2) - 1, 0);
break;
case ScrollFree:
if (m_layout == LayoutGrid)
{
start_button = 0;
first_item = last_item = 0;
grow_left = false;
}
else if (!m_ButtonList.empty())
{
if (m_itemCount - m_selPosition - 1 <
static_cast<int>(m_ButtonList.size()) / 2)
start_button = m_ButtonList.size() -
(m_itemCount - m_selPosition) + 1;
else if (m_selPosition >
static_cast<int>(m_ButtonList.size()) / 2)
start_button = (m_ButtonList.size() / 2);
else
start_button = m_selPosition;
}
else
start_button = 0;
break;
}
first_button = last_button = start_button;
row_heights.clear();
// Process row with selected button, and set starting val for m_columns.
if (!DistributeRow(first_button, last_button,
first_item, last_item, selected_column,
skip_cols, grow_left, true, &col_widths,
height, 0, 0, col_cnt, wrapped))
{
delete[] col_widths;
return false;
}
m_columns = col_cnt;
if (m_layout == LayoutGrid && m_scrollStyle == ScrollFree)
{
/*
* Now that we know how many columns there are, we can start
* the grid layout for real.
*/
start_item = (m_selPosition / m_columns) * m_columns;
first_item = last_item = start_item;
/*
* Attempt to pick a start_button which will minimize the need
* for new button allocations.
*/
start_button = qMax((int)m_ButtonList.size() / 2, 0);
start_button = (start_button / qMax(m_columns, 1)) * m_columns;
if (start_button < m_itemCount / 2 &&
m_itemCount - m_selPosition - 1 < (int)m_ButtonList.size() / 2)
start_button += m_columns;
first_button = last_button = start_button;
// Now do initial row layout again with our new knowledge
selected_column = selected_row = -1;
if (!DistributeRow(first_button, last_button,
first_item, last_item, selected_column,
skip_cols, grow_left, true, &col_widths,
height, 0, 0, col_cnt, wrapped))
{
delete[] col_widths;
return false;
}
}
if (selected_column != -1)
selected_row = 0;
row_heights.push_back(height);
if (m_scrollStyle == ScrollCenter)
top_height = bottom_height = (height / 2);
else
bottom_height = height;
if (m_layout == LayoutHorizontal)
break;
// As as many columns as will fit.
if (DistributeCols(first_button, last_button,
first_item, last_item,
selected_column, selected_row,
skip_cols, &col_widths, row_heights,
top_height, bottom_height, wrapped))
break; // Buttons fit on each row, so done
delete[] col_widths;
col_widths = 0;
--m_columns;
start_item = m_selPosition;
}
m_rows = row_heights.size();
LOG(VB_GUI, LOG_DEBUG,
QString("%1 rows, %2 columns fit inside parent area %3x%4")
.arg(m_rows).arg(m_columns).arg(m_contentsRect.width())
.arg(m_contentsRect.height()));
if (col_widths == 0)
return false;
int total, row, col;
int left_spacing, right_spacing, top_spacing, bottom_spacing;
int x, y, x_init, x_adj, y_adj;
QString status_msg;
/*
* Calculate heights of buttons on each side of selected button
*/
top_height = bottom_height = m_topRows = m_bottomRows = 0;
status_msg = "Row heights: ";
for (row = 0; row < m_rows; ++row)
{
if (row != 0)
status_msg += ", ";
if (row == selected_row)
{
status_msg += '[';
top_height += (row_heights[row] / 2);
bottom_height += ((row_heights[row] / 2) + (row_heights[row] % 2));
}
else
{
if (bottom_height)
{
bottom_height += m_itemVertSpacing + row_heights[row];
++m_bottomRows;
}
else
{
top_height += row_heights[row] + m_itemVertSpacing;
++m_topRows;
}
}
status_msg += QString("%1").arg(row_heights[row]);
if (row == selected_row)
status_msg += ']';
}
/*
* How much extra space should there be between buttons?
*/
if (m_arrange == ArrangeStack || m_layout == LayoutHorizontal)
{
// None
top_spacing = bottom_spacing = 0;
}
else
{
if (m_rows < 2)
{
// Equal space on both sides of single row
top_spacing = bottom_spacing =
(m_contentsRect.height() - top_height) / 2;
}
else
{
if (m_scrollStyle == ScrollCenter)
{
// Selected button needs to end up in the middle of area
top_spacing = m_topRows ? (m_contentsRect.height() / 2 -
top_height) / m_topRows : 0;
bottom_spacing = m_bottomRows ? (m_contentsRect.height() / 2 -
bottom_height) / m_bottomRows : 0;
if (m_arrange == ArrangeSpread)
{
// Use same spacing on both sides of selected button
if (!m_topRows || top_spacing > bottom_spacing)
top_spacing = bottom_spacing;
else
bottom_spacing = top_spacing;
}
}
else
{
// Buttons will be evenly spread out to fill entire area
top_spacing = bottom_spacing = (m_contentsRect.height() -
(top_height + bottom_height)) /
(m_topRows + m_bottomRows);