-
Notifications
You must be signed in to change notification settings - Fork 1
/
ListView.cpp
123 lines (102 loc) · 3.2 KB
/
ListView.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
//
// Created by zjh_3 on 2023/7/23.
//
#include "ListView.h"
Flare::ListView::ListView(QWidget *parent) :
Widget(parent),
WidgetList(new QList<QWidget *>()),
spacing(0), marginLeft(0), isScrollable(false),moveSpeed(20),topMargin(0) {
}
void Flare::ListView::append(QWidget *w) {
w->setParent(this);
WidgetList->push_back(w);
// 如果已经可见那就显示
if (isVisible()) {
setPosition();
}
}
void Flare::ListView::remove(const int &i) {
WidgetList->remove(i);
if (isVisible()) {
setPosition();
}
}
void Flare::ListView::clear() {
WidgetList->clear();
}
void Flare::ListView::setSpacing(const int &s) {
spacing = s;
}
void Flare::ListView::showEvent(QShowEvent *event) {
QWidget::showEvent(event);
for (const auto &i: *WidgetList)
i->show();
setPosition();
}
void Flare::ListView::setMarginLeft(const int &m) {
marginLeft = m;
}
void Flare::ListView::setPosition() {
if(WidgetList->empty())
return;
WidgetList->at(0)->move(MarginLeft(), TopMargin());
for (int i = 1; i != WidgetList->size(); ++i) {
WidgetList->at(i)->move(MarginLeft(),
WidgetList->at(i - 1)->y() + WidgetList->at(i - 1)->height() + Spacing());
}
}
int Flare::ListView::Spacing() const {
return spacing;
}
int Flare::ListView::MarginLeft() const {
return marginLeft;
}
void Flare::ListView::setScrollable(const bool &s) {
isScrollable = s;
}
bool Flare::ListView::IsScrollable() const {
return isScrollable;
}
void Flare::ListView::wheelEvent(QWheelEvent *event) {
QWidget::wheelEvent(event);
if (IsScrollable()) {
allWidgetMove(event->angleDelta().y());
}
//! 要记得在使用VS时将这个类继承于Flare::Widget然后加一层isAbove的判断
}
void Flare::ListView::setMoveSpeed(const int &s) {
moveSpeed = s;
}
int Flare::ListView::MoveSpeed() const {
return moveSpeed;
}
void Flare::ListView::allWidgetMove(const int &n) {
if (WidgetList->empty()) {
return;
}
if (n > 0) { // 向下
// 如果 如果列表的第一个元素移动后坐标大于等于0就不移动
if (WidgetList->first()->y() + MoveSpeed() < TopMargin()) {
for (const auto &item: *WidgetList)
item->move(item->x(), item->y() + MoveSpeed());
// 如果移动后的位置大于0但是移动后的位置小于移动的距离便默认为显示不正常重置视图
} else
setPosition();
} else if (n < 0) { // 向上
// 如果列表的最后一位在移动后他的底部小于高就不移动
if (WidgetList->back()->y() + WidgetList->back()->height() - MoveSpeed() > height()) {
for (const auto &item: *WidgetList)
item->move(item->x(), item->y() - MoveSpeed());
}
} else {
qDebug()
<< "呕吼,你发现了什么蜜汁错误,请联系该软件的开发者,或者去Github上的FlareUI储存库去提交issue,感谢你对本ui库做出的贡献";
return;
}
}
void Flare::ListView::setTopMargin(const int &i) {
topMargin = i;
}
int Flare::ListView::TopMargin() const {
return topMargin;
}