forked from bnufree/Hq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qcontainerwidget.cpp
131 lines (116 loc) · 3.16 KB
/
qcontainerwidget.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
#include "qcontainerwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
QContainerWidget::QContainerWidget(LAYOUT_DIRECTION dir, QWidget *parent) : QWidget(parent),mDir(dir)
{
if(mDir == LAYOUT_HOR)
{
setLayout(new QHBoxLayout(this));
} else
{
setLayout(new QVBoxLayout(this));
}
this->layout()->setMargin(0);
this->layout()->setSpacing(0);
this->layout()->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding));
this->layout()->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding));
}
bool QContainerWidget::appendWidget(QWidget *w)
{
//计算是否有剩余空间来添加
int width = this->geometry().width();
int height = this->geometry().height();
int count = this->layout()->count();
int est_len = count > 0 ? count * this->layout()->spacing() : 0;
for(int i=0; i<count; i++)
{
QWidget* o = this->layout()->itemAt(i)->widget();
if(!o) continue;
est_len += (mDir == LAYOUT_HOR ? o->width() : o->height());
}
est_len += (mDir == LAYOUT_HOR ? w->width() : w->height());
bool insert = (mDir == LAYOUT_HOR ? est_len <= width : est_len <= height);
if(insert)
{
QBoxLayout *box = qobject_cast<QBoxLayout*> (this->layout());
if(box)
{
box->insertWidget(count-1, w);
return true;
}
}
return false;
}
bool QContainerWidget::removeWidget(QWidget *w)
{
int index = this->layout()->indexOf(w);
if(index >= 0) {
this->layout()->removeWidget(w);
return true;
}
return false;
}
QWidget* QContainerWidget::getLastWidget()
{
QWidget* w = NULL;
int count = this->layout()->count();
if(count > 2)
{
w = this->layout()->itemAt(count - 2)->widget();
}
return w;
}
QWidget* QContainerWidget::getFirstWidget()
{
QWidget* w = NULL;
int count = this->layout()->count();
if(count > 2)
{
w = this->layout()->itemAt(1)->widget();
}
return w;
}
QWidgetList QContainerWidget::getWidgetList()
{
QWidgetList list;
QWidget* w = NULL;
int count = this->layout()->count();
for(int i=1; i<count - 1; i++)
{
QLayoutItem *item = this->layout()->itemAt(i);
if(!item) continue;
w = item->widget();
if(w)
{
w->setParent(0);
list.append(w);
}
}
return list;
}
void QContainerWidget::removeAllWidget()
{
while (this->layout()->count() > 2) {
this->layout()->removeItem(this->layout()->itemAt(1));
}
}
int QContainerWidget::getWidgetCount()
{
return this->layout()->count() - 2;
}
int QContainerWidget::totalWidgetSize() const
{
int size = 0;
int count = this->layout()->count();
if(count > 2)
{
for(int i=1; i<count-1; i++)
{
QWidget* w = this->layout()->itemAt(i)->widget();
if(!w) continue;
size += (mDir == LAYOUT_HOR ? w->width() : w->height());
}
}
return size;
}