-
Notifications
You must be signed in to change notification settings - Fork 2
/
tabviewpagerframe.cpp
159 lines (130 loc) · 4.07 KB
/
tabviewpagerframe.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
#include "tabviewpagerframe.h"
#include <QLabel>
#include <QDebug>
#define PAGECONTROLID 10000
TabViewPagerFrame::TabViewPagerFrame(QWidget *parent) :
QFrame(parent),
_curPage(0)
{
init();
}
TabViewPagerFrame::~TabViewPagerFrame()
{
}
void TabViewPagerFrame::init() {
_layout = new GridLayoutEx(1, this);
//默认为1列,实际需要根据具体的来设置
_layout->addWidget(new POSLabelEx(QObject::tr("页码控制器"), _layout));
}
void TabViewPagerFrame::setPager(int total, int pagesize) {
this->_total = total;
this->_pageSize = pagesize;
int count = total / pagesize;
if( total % pagesize != 0) {
count += 1;
}
if( _layout != nullptr ) {
_layout->clean();
delete _layout;
_layout = nullptr;
}
/**
//如果 page count > 5 ,则中间用省略号代替
_____ [ prev page ] [1] [2] [...] [ pagecount - 2] [ pagecount - 1] [next page]
//如果小于 5,则直接显示页码
_________________ [1] [2] [3] [4] [5]
*/
QRect rect = this->geometry();
//只有一行,10列
_layout = new GridLayoutEx(10, this);
_layout->setGeometry(0, 0, rect.width(), rect.height());
// _layout->setStyleSheet("background-color: rgb(0,0,0)");
_layout->setRowHeight( rect.height() );
if(count < 5) {
{
_layout->addWidget( new QLabel(_layout), 10 - count);
}
for(int i = 0; i < count; i++) {
VKeyItemButton* _kb = new VKeyItemButton( QString::number(i + 1), this, _layout);
_layout->addWidget(_kb);
}
} else {
{
_layout->addWidget( new QLabel(_layout), 3);
}
//
{
VKeyItemButton* _kb = new VKeyItemButton( "prevPage", this, _layout);
_kb->setText(tr("上一页"));
_layout->addWidget(_kb);
}
{
VKeyItemButton* _kb = new VKeyItemButton( QString::number(1), this, _layout);
_layout->addWidget(_kb, 1, 1, PAGECONTROLID);
}
{
VKeyItemButton* _kb = new VKeyItemButton( QString::number(2), this, _layout);
_layout->addWidget(_kb, 1, 1, PAGECONTROLID + 1);
}
{
VKeyItemButton* _kb = new VKeyItemButton( "...", this, _layout);
_layout->addWidget(_kb, 1, 1, PAGECONTROLID + 2);
}
{
VKeyItemButton* _kb = new VKeyItemButton( QString::number(count - 2), this, _layout);
_layout->addWidget(_kb, 1, 1, PAGECONTROLID + 3);
}
{
VKeyItemButton* _kb = new VKeyItemButton( QString::number(count - 1), this, _layout);
_layout->addWidget(_kb, 1, 1, PAGECONTROLID + 4);
}
{
VKeyItemButton* _kb = new VKeyItemButton( "nextPage", this, _layout);
_kb->setText(tr("下一页"));
_layout->addWidget(_kb);
}
}
_layout->show();
}
void TabViewPagerFrame::resizeEvent(QResizeEvent* e) {
QFrame::resizeEvent(e);
if( _layout == nullptr ) {
return;
}
QRect rect = this->geometry();
_layout->setGeometry(0, 0, rect.width(), rect.height());
}
void TabViewPagerFrame::showEvent(QShowEvent* e) {
QFrame::showEvent(e);
if(_layout != nullptr) {
QRect rect = this->geometry();
_layout->setGeometry(0, 0, rect.width(), rect.height());
qDebug() << "layout : " << _layout->geometry();
_layout->show();
}
}
void TabViewPagerFrame::onKeyDown(QString value) {
//下一页
int count = this->_total / this->_pageSize;
if( this->_total / this->_pageSize != 0) {
count += 1;
}
if( "prevPage" == value ) {
//上一页
if( _curPage > 0 ) {
_curPage -= 1;
}
emit(toPage(_curPage));
} else if( "nextPage" == value ) {
if( _curPage < count ) {
_curPage += 1;
}
emit(toPage(_curPage));
} else if( "..." == value ) {
//不可操作
} else if( 0 < value.toInt()) {
//具体的页码
_curPage = value.toInt() - 1;
emit(toPage(_curPage));
}
}