-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathadvancedtoolbox.cpp
More file actions
1376 lines (1228 loc) · 38.5 KB
/
advancedtoolbox.cpp
File metadata and controls
1376 lines (1228 loc) · 38.5 KB
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 "advancedtoolbox.h"
#include <QApplication>
#include <QDebug>
#include <QDrag>
#include <QEvent>
#include <QLayoutItem>
#include <QMenu>
#include <QMimeData>
#include <QMouseEvent>
#include <QPainter>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QAbstractButton>
#include <QRubberBand>
#include <QStyleOption>
#include <QtMath>
#include <functional>
class ToolBoxPageContainer : public QWidget
{
public:
using QWidget::QWidget;
bool event(QEvent *e)
{
if(e->type() == QEvent::LayoutRequest)
updateGeometry();
return QWidget::event(e);
}
};
class ToolBoxTitle : public QAbstractButton
{
Q_OBJECT
signals:
void titleClicked(int index);
void titleContextMenuRequest(int index, const QPoint &pos);
public:
explicit ToolBoxTitle(const QString &label, const QIcon &icon, AdvancedToolBox *parent);
void setIndex(int index)
{
tabIndex = index;
}
QSize sizeHint() const;
QSize minimumSizeHint() const
{
return sizeHint();
}
void setExpanded(bool expanded)
{
if(this->expanded != expanded)
{
this->expanded = expanded;
update();
}
}
protected:
bool event(QEvent *e);
void initStyleOption(QStyleOptionToolBox *option) const;
void paintEvent(QPaintEvent *);
void changeEvent(QEvent *e)
{
_sizeHint = QSize();
updateGeometry();
QWidget::changeEvent(e);
}
private:
QPoint pressedPos;
bool pressed = false;
bool expanded = true;
mutable QSize _sizeHint;
bool hoverBranch = false;
int tabIndex = -1;
};
class AdvancedToolBoxPrivate : public QObject
{
Q_DECLARE_PUBLIC(AdvancedToolBox)
public:
class ToolBoxItem;
public:
AdvancedToolBoxPrivate(AdvancedToolBox *p)
: QObject()
, q_ptr(p)
{
QStyle *style = q_ptr->style();
handleWidth = style->pixelMetric(QStyle::PM_DockWidgetSeparatorExtent, nullptr, p);
indent = style->pixelMetric(QStyle::PM_TreeViewIndentation, nullptr, p);
}
~AdvancedToolBoxPrivate()
{
qDeleteAll(items);
}
QWidget *takeIndex(int index);
QWidget *widget(int index);
void setIndexExpand(int index, bool expand = true);
void setIndexVisible(int index, bool visible = true);
void styleChangedEvent();
void setIndentation(int i);
void insertWidgetToList(int index, QWidget *widget, const QString &label, const QIcon &icon = QIcon());
void doLayout();
void expandStateChanged(int index, bool expand);
void moveHandle(int index, int distance);
void updateGeometries(bool animate = false);
void resetPages();
ToolBoxSplitterHandle *createHandle();
ToolBoxTitle *createTitle(const QString &label, const QIcon &icon);
void showTitleMenu(int index, const QPoint &pos);
void setDragRubberVisible(bool visible, const QRect &rect = QRect());
void updateTitleIndent();
void resetManualSize();
void widgetDestroyed(QObject *o);
void resetSizeHint();
protected:
int indent = 10;
int handleWidth = 5;
QSize minSizeHint;
QSize sizeHint;
int boxSpacing = 0;
QList<ToolBoxItem *> items;
QRubberBand *dragRubber = nullptr;
bool isAnimationState = false;
bool nextIsAnimation = false;
bool dragSortEnable = true;
bool animationEnable = true;
AdvancedToolBox *q_ptr = nullptr;
friend class AdvancedToolBox;
friend class ToolBoxSplitterHandle;
};
class AdvancedToolBoxPrivate::ToolBoxItem
{
QWidget *widget = nullptr;
ToolBoxSplitterHandle *handle = nullptr; // 移动handle
ToolBoxTitle *tabTitle = nullptr; // 标题栏文字、图标等
QWidget *tabContainer = nullptr; // 容器,方便做折叠动画
int layoutHeight = 0; // 实际布局高度
QSize sizeHint; // 建议高度
QSize minSize; // 最小高度
QSize maxSize; // 最大高度
int manualHeight = 0; // 手动高度,用于在调整高度或者展开折叠后做一次缓存,避免resize时抖动
bool freezeTarget = false;
bool isExpanded = true;
inline bool expanded() { return isExpanded; }
void calItemSize()
{
QWidgetItem wi(widget);
sizeHint = wi.sizeHint();
minSize = wi.minimumSize();
maxSize = wi.maximumSize();
if(sizeHint.height() < 0)
sizeHint.rheight() = 100;
}
int preferHeight()
{
int prefer = 0;
if(manualHeight > 0)
prefer = manualHeight;
else if(sizeHint.height() > 0)
prefer = sizeHint.height();
return qMin(qMax(minSize.height(), prefer), maxSize.height());
}
inline bool canResize() const
{
return !widget->isHidden() && isExpanded;
}
inline bool isHidden() const
{
return widget->isHidden();
}
inline QString title() const
{
return tabTitle->text();
}
friend class AdvancedToolBox;
friend class AdvancedToolBoxPrivate;
};
using AdToolBoxItem = AdvancedToolBoxPrivate::ToolBoxItem;
AdvancedToolBox::AdvancedToolBox(QWidget *parent)
: QWidget(parent)
, d_ptr(new AdvancedToolBoxPrivate(this))
{
setAcceptDrops(true);
}
AdvancedToolBox::~AdvancedToolBox()
{
}
QSize AdvancedToolBox::sizeHint() const
{
Q_D(const AdvancedToolBox);
return d->sizeHint;
}
QSize AdvancedToolBox::minimumSizeHint() const
{
Q_D(const AdvancedToolBox);
return d->minSizeHint;
}
void AdvancedToolBox::addWidget(QWidget *widget, const QString &label, const QIcon &icon)
{
Q_D(AdvancedToolBox);
Q_ASSERT(widget);
int n = d->items.count();
d->insertWidgetToList(n, widget, label, icon);
}
int AdvancedToolBox::indexOf(QWidget *widget)
{
Q_D(AdvancedToolBox);
auto &items = d->items;
for(int i = 0; i < items.size(); i++)
{
QWidget *w = items.at(i)->widget;
if(w == widget)
return i;
}
return -1;
}
QWidget *AdvancedToolBox::takeIndex(int index)
{
Q_D(AdvancedToolBox);
return d->takeIndex(index);
}
QWidget *AdvancedToolBox::widget(int index)
{
Q_D(AdvancedToolBox);
auto item = d->items.value(index);
if(item)
return item->widget;
return nullptr;
}
void AdvancedToolBox::setItemExpand(int index, bool expand)
{
Q_D(AdvancedToolBox);
d->setIndexExpand(index, expand);
}
void AdvancedToolBox::setItemVisible(int index, bool visible)
{
Q_D(AdvancedToolBox);
d->setIndexVisible(index, visible);
}
void AdvancedToolBox::setItemText(int index, const QString &text)
{
Q_D(AdvancedToolBox);
if(auto item = d->items.value(index))
item->tabTitle->setText(text);
}
void AdvancedToolBox::setItemIcon(int index, const QIcon &icon)
{
Q_D(AdvancedToolBox);
if(auto item = d->items.value(index))
item->tabTitle->setIcon(icon);
}
QString AdvancedToolBox::itemText(int index)
{
Q_D(AdvancedToolBox);
if(auto item = d->items.value(index))
return item->title();
return QString();
}
QIcon AdvancedToolBox::itemIcon(int index)
{
Q_D(AdvancedToolBox);
if(auto item = d->items.value(index))
return item->tabTitle->icon();
return QIcon();
}
int AdvancedToolBox::textIndentation()
{
Q_D(AdvancedToolBox);
return d->indent;
}
void AdvancedToolBox::resetTextIndentation(int indent)
{
Q_D(AdvancedToolBox);
d->setIndentation(indent);
}
void AdvancedToolBox::setDragSortEnable(bool enable)
{
Q_D(AdvancedToolBox);
d->dragSortEnable = enable;
}
void AdvancedToolBox::setAnimationEnable(bool enable)
{
Q_D(AdvancedToolBox);
d->animationEnable = enable;
}
bool AdvancedToolBox::event(QEvent *e)
{
bool ret = QWidget::event(e);
switch(e->type())
{
case QEvent::LayoutRequest:
{
Q_D(AdvancedToolBox);
for(auto item : d->items)
item->calItemSize();
d->resetSizeHint();
d->doLayout();
}
break;
case QEvent::StyleChange:
{
Q_D(AdvancedToolBox);
int old = d->handleWidth;
d->styleChangedEvent();
d->updateTitleIndent();
if(old != d->handleWidth)
{
d->resetSizeHint();
d->doLayout();
}
}
break;
case QEvent::Resize:
{
Q_D(AdvancedToolBox);
d->doLayout();
}
break;
default:
break;
}
return ret;
}
void AdvancedToolBox::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
Q_D(AdvancedToolBox);
const int hw = d->handleWidth;
QStyleOption opt(0);
opt.state = QStyle::State_None;
opt.state |= this->isEnabled() ? QStyle::State_Enabled : QStyle::State_None;
opt.state |= QStyle::State_Horizontal;
opt.palette = this->palette();
bool first = true;
QPainter painter(this);
for(auto item : d->items)
{
if(item->widget->isHidden())
continue;
if(!first)
{
int top = item->tabTitle->pos().y();
opt.rect = QRect(0, top - hw, this->width(), hw);
this->style()->drawPrimitive(QStyle::PE_IndicatorDockWidgetResizeHandle, &opt, &painter, this);
}
first = false;
}
}
void AdvancedToolBox::dragEnterEvent(QDragEnterEvent *event)
{
const QMimeData *data = event->mimeData();
if(data->hasFormat("advanced-toolbox-drag-index") && event->source() == this)
{
event->acceptProposedAction();
return;
}
return QWidget::dragEnterEvent(event);
}
void AdvancedToolBox::dragMoveEvent(QDragMoveEvent *event)
{
Q_D(AdvancedToolBox);
const QMimeData *data = event->mimeData();
bool ok = false;
int drag_index = data->data("advanced-toolbox-drag-index").toInt(&ok);
if(!ok)
{
event->ignore();
return;
}
auto &items = d->items;
const int y = event->pos().y();
int hover = -1;
QRect rubber_rect;
for(int i = 0; i < items.count(); i++)
{
auto *item = items.at(i);
if(item->isHidden())
continue;
if(item->expanded())
{
QRect cr = item->tabContainer->geometry();
if(cr.bottom() >= y)
{
hover = i;
QRect tr = item->tabTitle->geometry();
int mid = (tr.top() + cr.bottom() + 1) / 2;
int top = mid < y ? mid : tr.top();
rubber_rect = QRect(tr.x(), top, tr.width(), 0);
rubber_rect.setBottom(mid < y ? cr.bottom() : mid);
break;
}
}
else
{
QRect r = item->tabTitle->geometry();
if(r.bottom() >= y)
{
hover = i;
int mid = r.center().y();
int top = mid < y ? r.bottom() + 1 : r.top() - d->handleWidth;
rubber_rect = QRect(r.x(), top, r.width(), d->handleWidth);
if(d->handleWidth <= 1)
rubber_rect.adjust(0, -2, 0, 2);
break;
}
}
}
if(hover >= 0 && hover != drag_index)
{
event->acceptProposedAction();
d->setDragRubberVisible(true, rubber_rect);
}
else
{
d->setDragRubberVisible(false);
event->ignore();
}
}
void AdvancedToolBox::dropEvent(QDropEvent *event)
{
Q_D(AdvancedToolBox);
d->setDragRubberVisible(false);
const QMimeData *data = event->mimeData();
bool ok = false;
int drag_index = data->data("advanced-toolbox-drag-index").toInt(&ok);
if(!ok)
{
event->ignore();
return;
}
auto &items = d->items;
const int y = event->pos().y();
int target = -1;
for(int i = 0; i < items.count(); i++)
{
auto *item = items.at(i);
if(!item->isHidden())
{
if(item->expanded())
{
QRect cr = item->tabContainer->geometry();
if(cr.bottom() >= y)
{
QRect tr = item->tabTitle->geometry();
int mid = (tr.top() + cr.bottom() + 1) / 2;
target = i + (mid < y ? 1 : 0);
target -= target > drag_index ? 1 : 0;
break;
}
}
else
{
QRect r = item->tabTitle->geometry();
if(r.bottom() >= y)
{
target = i + (r.center().y() < y ? 1 : 0);
target -= target > drag_index ? 1 : 0;
break;
}
}
}
}
if(target >= 0 && target != drag_index)
{
event->acceptProposedAction();
d->items.move(drag_index, target);
d->resetPages();
d->updateGeometries();
}
else
{
event->ignore();
}
}
void AdvancedToolBox::dragLeaveEvent(QDragLeaveEvent *event)
{
Q_D(AdvancedToolBox);
d->setDragRubberVisible(false);
event->ignore();
}
void AdvancedToolBox::startDrag(int index, const QPoint &gpos)
{
Q_D(AdvancedToolBox);
if(d->dragSortEnable && index >= 0)
{
QDrag drag(this);
QMimeData *data = new QMimeData();
data->setData("advanced-toolbox-drag-index", QByteArray::number(index));
drag.setMimeData(data);
Q_D(AdvancedToolBox);
auto item = d->items.at(index);
QPixmap pix = item->tabTitle->grab();
QPoint pos = item->tabTitle->mapFromGlobal(gpos);
drag.setHotSpot(pos);
drag.setPixmap(pix);
drag.exec(Qt::MoveAction);
}
}
QWidget *AdvancedToolBoxPrivate::takeIndex(int index)
{
Q_Q(AdvancedToolBox);
ToolBoxItem *item = items.value(index);
if(item)
{
QWidget *ret = item->widget;
if(ret)
{
ret->setVisible(false);
ret->setParent(q);
}
item->tabTitle->deleteLater();
item->tabContainer->deleteLater();
item->handle->deleteLater();
delete item;
doLayout();
return ret;
}
return nullptr;
}
void AdvancedToolBoxPrivate::setIndexExpand(int index, bool expand)
{
auto item = items.value(index);
if(item && item->expanded() != expand)
{
item->isExpanded = expand;
expandStateChanged(index, expand);
}
}
void AdvancedToolBoxPrivate::setIndexVisible(int index, bool visible)
{
auto item = items.value(index);
if(!item)
return;
if((item->widget->isHidden() && !visible) || !item->widget->isHidden() && visible)
return;
item->widget->setVisible(visible);
item->tabTitle->setVisible(visible);
item->tabContainer->setVisible(visible);
if(!visible && item->isExpanded)
item->manualHeight = item->layoutHeight;
resetSizeHint();
if(visible)
resetManualSize();
// 这里选择重新布局,可以考虑尽可能维持该页面后布局,提升体验
doLayout();
}
void AdvancedToolBoxPrivate::styleChangedEvent()
{
QStyle *style = q_ptr->style();
handleWidth = style->pixelMetric(QStyle::PM_DockWidgetSeparatorExtent, nullptr, q_ptr);
indent = style->pixelMetric(QStyle::PM_TreeViewIndentation, nullptr, q_ptr);
}
void AdvancedToolBoxPrivate::setIndentation(int i)
{
int old = indent;
if(i < 0)
indent = q_ptr->style()->pixelMetric(QStyle::PM_TreeViewIndentation, nullptr, q_ptr);
else
indent = i;
if(old != indent)
{
updateTitleIndent();
}
}
void AdvancedToolBoxPrivate::insertWidgetToList(int index, QWidget *widget, const QString &label, const QIcon &icon)
{
Q_Q(AdvancedToolBox);
int count = items.count();
if(index < 0 || index > count)
index = count;
int old_index = q->indexOf(widget);
if(old_index >= 0) // just move
{
if(index != old_index)
{
items.move(old_index, index);
resetPages();
updateGeometries();
}
}
else
{
bool show = !(widget->isHidden() && widget->testAttribute(Qt::WA_WState_ExplicitShowHide));
ToolBoxItem *item = new ToolBoxItem();
item->tabContainer = new ToolBoxPageContainer(q);
widget->setParent(item->tabContainer);
widget->move(QPoint(0, 0));
item->widget = widget;
item->tabTitle = createTitle(label, icon);
item->handle = createHandle();
item->isExpanded = true;
items.insert(index, item);
if(show)
widget->show();
item->calItemSize();
resetSizeHint();
connect(widget, &QWidget::destroyed, this, &AdvancedToolBoxPrivate::widgetDestroyed);
doLayout();
}
}
void AdvancedToolBoxPrivate::doLayout()
{
Q_Q(AdvancedToolBox);
if(!q->testAttribute(Qt::WA_Resized))
return;
resetPages();
int space = 0;
QVector<ToolBoxItem *> expandedItems;
{
int totalSize = 0;
for(auto item : items)
{
if(item->widget->isHidden())
continue;
if(item->expanded())
expandedItems.append(item);
item->layoutHeight = item->expanded() ? item->preferHeight() : 0;
totalSize += item->tabTitle->sizeHint().height(); // title height
totalSize += item->layoutHeight;
totalSize += handleWidth;
}
// handle 要少一个
totalSize -= (totalSize > 0 ? handleWidth : 0);
space = q->rect().height() - totalSize;
}
if(space != 0)
{
int space2 = space;
int viewsSize = 0;
auto it = expandedItems.begin();
while(it != expandedItems.end())
{
ToolBoxItem *item = *it;
if((space > 0 && item->layoutHeight >= item->maxSize.height()) ||
(space < 0 && item->layoutHeight <= item->minSize.height()))
{
// remove fix size
it = expandedItems.erase(it);
}
else
{
viewsSize += item->layoutHeight;
it++;
}
}
// 目前默认按照初始窗口当前尺寸来按比例分配空间
// viewsSize变为0, 则认为layoutHeight都因为某些原因为0(不存在布局等)
// 此时平均分配
auto calcAddSize = [&viewsSize, &expandedItems](int curr, int space){
if(viewsSize <= 0)
return qCeil(qreal(space) / expandedItems.count());
else
return qCeil(qreal(space) * curr / viewsSize);
};
// 空间不足或者有空余时,调整可伸缩的窗口,按照上一次手动调整后的比例,分配或者压缩空间
// 先尝试按比例分配空间,超过最大、最小的先处理掉。
bool done = false;
while(!expandedItems.isEmpty() && !done)
{
done = true;
auto it = expandedItems.begin();
while(it != expandedItems.end())
{
ToolBoxItem *item = *it;
int prefer = item->layoutHeight + calcAddSize(item->layoutHeight, space2);
if((space > 0 && prefer > item->maxSize.height()) || (space < 0 && prefer < item->minSize.height()))
{
int threshold = space > 0 ? item->maxSize.height() : item->minSize.height();
space2 -= (threshold - item->layoutHeight);
viewsSize -= item->layoutHeight;
item->layoutHeight = threshold;
expandedItems.erase(it);
done = false;
break;
}
it++;
}
}
for(ToolBoxItem *item : expandedItems)
{
int add = calcAddSize(item->layoutHeight, space2);
viewsSize -= item->layoutHeight;
space2 -= add;
item->layoutHeight += add;
}
}
updateGeometries();
}
void AdvancedToolBoxPrivate::expandStateChanged(int index, bool expand)
{
ToolBoxItem *curr = items.value(index);
if(!curr)
return;
curr->tabTitle->setExpanded(expand);
if(expand)
{
int target = curr->preferHeight();
int space = boxSpacing - target;
if(space >= 0)
{
target += space;
curr->layoutHeight = qMin(target, curr->maxSize.height());
}
else
{
space = -space;
const int count = items.count();
for(int i = count - 1; i >= 0 && space > 0; i--)
{
auto item = items.at(i);
if(item->canResize() && i != index) // 暂时忽略当前page
{
int diff = qMin(item->layoutHeight - item->minSize.height(), space);
item->layoutHeight -= diff;
space -= diff;
}
}
curr->layoutHeight = target;
if(space > 0)
{
int diff = qMin(curr->layoutHeight - curr->minSize.height(), space);
curr->layoutHeight -= diff;
}
}
}
else
{
int space = curr->manualHeight = curr->layoutHeight;
curr->layoutHeight = 0;
space += boxSpacing;
if(space > 0)
{
const int count = items.count();
for(int i = count - 1; i >= 0 && space > 0; i--)
{
auto item = items.at(i);
if(item->canResize())
{
int diff = qMin(item->maxSize.height() - item->layoutHeight, space);
item->layoutHeight += diff;
space -= diff;
}
}
}
}
curr->freezeTarget = true;
updateGeometries(animationEnable);
resetManualSize();
}
void AdvancedToolBoxPrivate::moveHandle(int index, int distance)
{
if(distance == 0)
return;
// adjectHandle入口在鼠标移动事件,当鼠标按下时,resetManualSize将当前布局存储
QVector<ToolBoxItem *> shrink_part, expand_part;
for(int i = index; i < items.count(); i++)
{
auto item = items.at(i);
if(item->canResize())
expand_part.append(item);
}
for(int i = index - 1; i >= 0; i--)
{
auto item = items.at(i);
if(item->canResize())
shrink_part.append(item);
}
if(distance > 0) // 交换一下
std::swap(shrink_part, expand_part);
int expand = 0, shrink = 0;
for(ToolBoxItem *item : shrink_part)
shrink += item->manualHeight - item->minSize.height();
for(ToolBoxItem *item : expand_part)
expand += item->maxSize.height() - item->manualHeight;
int min_dis = qMin(qMin(shrink, expand), abs(distance));
if(min_dis == 0)
return;
//调整
int space = min_dis;
for(int i = 0; i < shrink_part.count(); i++)
{
ToolBoxItem *item = shrink_part.at(i);
int diff = qMin(item->manualHeight - item->minSize.height(), space);
item->layoutHeight = item->manualHeight - diff;
space -= diff;
}
space = min_dis;
for(int i = 0; i < expand_part.count(); i++)
{
ToolBoxItem *item = expand_part.at(i);
int diff = qMin(item->maxSize.height() - item->manualHeight, space);
item->layoutHeight = item->manualHeight + diff;
space -= diff;
}
updateGeometries();
}
// 根据计算好的布局,设置窗口位置等
void AdvancedToolBoxPrivate::updateGeometries(bool animate)
{
Q_Q(AdvancedToolBox);
animate = animate && q->isVisible();
QParallelAnimationGroup *group = nullptr;
if(isAnimationState)
{
nextIsAnimation = animate;
return;
}
if(animate)
{
isAnimationState = true;
group = new QParallelAnimationGroup();
QObject::connect(group, &QParallelAnimationGroup::finished, this,
[this](){
isAnimationState = false;
updateGeometries(nextIsAnimation);
});
}
const int hw = handleWidth;
QRect cr = q->rect();
int x = cr.left(), offset = cr.top(), width = cr.width();
bool first = true;
for(auto item : items)
{
if(item->isHidden())
continue;
int th = item->tabTitle->sizeHint().height();
offset += (first ? 0 : hw);
offset += th;
int h = item->layoutHeight;
QRect start = item->tabContainer->geometry();
QRect end(x, offset, width, h);
bool freezeSize = item->freezeTarget;
auto resizeTo = [q, item, th, hw, freezeSize](const QVariant &val)
{
QRect rect = val.toRect();
item->tabContainer->setGeometry(rect);
if(!freezeSize)
{
item->widget->setGeometry(QRect(QPoint(0, 0), rect.size()));
}
rect = QRect(rect.left(), rect.top() - th, rect.width(), th);
item->tabTitle->setGeometry(rect);
if(item->handle->isVisibleTo(q))
{
rect = QRect(rect.left(), rect.top() - hw, rect.width(), hw);
if(hw <= 1)
rect.adjust(0, -2, 0, 2);
item->handle->setGeometry(rect);
}
};
if(freezeSize && item->expanded())
{
item->widget->resize(end.size());
}
if(animate && start != end)
{
QVariantAnimation *animation = new QVariantAnimation(group);
animation->setDuration(100);
animation->setStartValue(start);
animation->setEndValue(end);
connect(animation, &QVariantAnimation::valueChanged, this, resizeTo);
group->addAnimation(animation);
}
else
{
resizeTo(end);
}
item->freezeTarget = false;
offset += h;
first = false;
}
boxSpacing = cr.bottom() - (offset - 1);
if(group)
{
connect(group, &QVariantAnimation::finished, this, &AdvancedToolBoxPrivate::resetSizeHint);
group->start(QAbstractAnimation::DeleteWhenStopped);
}
else
{
resetSizeHint();
}
nextIsAnimation = false;
}
// 更新handle顺序以及重新设置隐藏和显示
void AdvancedToolBoxPrivate::resetPages()
{
int count = items.count();
bool visible = false;
for(int i = 0; i < count; i++)
{
auto item = items.at(i);
item->tabTitle->setIndex(i);
item->handle->setIndex(i);
if(item->isHidden())