/
RtListBox.cpp
executable file
·223 lines (191 loc) · 5.71 KB
/
RtListBox.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
#include <QTimer>
#include "RtListBox.h"
RtListBox::RtListBox(QWidget *parent,int iX, int iY, int iWidth, int iHeight)
:QWidget(parent),
m_HeightUsed(0),
m_iItems(0),
m_iItemSelect(-1),
m_iBoxHeight(0),
m_iPressY(0),
m_iLastScroll(0)
{
setGeometry(iX, iY, iWidth, iHeight);
for (int i=0; i<__MAX_ITEMS__; i++)
{
m_pButtonList[i] = NULL;
}
m_pQTimerScroll = new QTimer(this);
connect(m_pQTimerScroll, SIGNAL(timeout()), this, SLOT(AutoScroll()));
m_pQTimerReset = new QTimer(this);
connect(m_pQTimerReset, SIGNAL(timeout()), this, SLOT(ResetLastScroll()));
}
RtPushButton* RtListBox::AddButton(RtPushButton* pButtonNew)
{
if ( m_iItems >= __MAX_ITEMS__ )
{
return NULL;
}
m_iBoxHeight = pButtonNew->GetImageHeight();
pButtonNew->setCheckable(true);
connect(pButtonNew, SIGNAL(MousePress(int)), this, SLOT(MousePress(int)));
connect(pButtonNew, SIGNAL(MoveInterval(int)), this, SLOT(ScrollList(int)));
connect(pButtonNew, SIGNAL(MouseRelease()), this, SLOT(MouseRelease()));
connect(pButtonNew, SIGNAL(ButtonClick(int)), this, SLOT(ButtonClick(int)));
connect(this, SIGNAL(ButtonScroll()), pButtonNew, SLOT(ButtonScroll()));
//arrange item position
pButtonNew->move(0, m_HeightUsed);
m_HeightUsed += pButtonNew->height();
//resize width to let mouse have reaction in all listbox
pButtonNew->setMinimumWidth(this->width());
//Add into list
m_pButtonList[m_iItems] = pButtonNew;
pButtonNew->SetListIndex(m_iItems);
m_iItems++;
return pButtonNew;
}
bool RtListBox::DeleteButton(int iSelect)
{
// parameter check
if ( m_iItems <= iSelect || iSelect < 0 )
{
return false;
}
//Update the select status
if ( m_iItemSelect != -1 )
{
m_pButtonList[m_iItemSelect]->setChecked(false);
m_iItemSelect = -1;
}
QRect rectPrevious = m_pButtonList[iSelect]->frameGeometry();
QRect rectNew;
delete m_pButtonList[iSelect];
for (int i=iSelect; i<m_iItems-1; i++) // remaining items move up an item
{
m_pButtonList[i] = m_pButtonList[i+1];
rectNew = m_pButtonList[i]->frameGeometry();
m_pButtonList[i]->setGeometry(rectPrevious);
rectPrevious = rectNew;
m_pButtonList[i]->SetListIndex(i); // re-set, button click return value;
}
m_pButtonList[m_iItems-1] = NULL;
m_iItems --; // total item decrease 1
if ( m_iItems != 0 )
{
if ( (m_pButtonList[m_iItems-1]->y() + m_pButtonList[m_iItems-1]->height()) < height() )
{// if last item is too high
int iShouldScrollDown = height() - (m_pButtonList[m_iItems-1]->y() + m_pButtonList[m_iItems-1]->height());
if ( m_pButtonList[0]->y() < 0 )
{// if first item can scroll down
int iCanScrollDown = -(m_pButtonList[0]->y());
int iScrollDown = (iShouldScrollDown > iCanScrollDown)? iCanScrollDown : iShouldScrollDown;
scroll(0, iScrollDown);
}
}
}
return true;
}
void RtListBox::DeleteAllButton(void)
{
for (int i=0; i<m_iItems; i++)
{
delete m_pButtonList[i];
m_pButtonList[i] = NULL;
}
m_HeightUsed = 0;
m_iItems = 0;
m_iItemSelect = -1;
m_iBoxHeight = 0;
m_iPressY = 0;
m_iLastScroll = 0;
}
void RtListBox::MousePress(int iPressY)
{
m_pQTimerScroll->stop();
m_iLastScroll = 0;
m_iPressY = iPressY - y();
}
void RtListBox::ScrollList(int iDiffY)
{
m_pQTimerReset->start(200);
int iButtonTop = m_pButtonList[0]->y();
if ( iButtonTop + iDiffY >= 0 )
{
iDiffY = -iButtonTop;
}
else
{
int iButtonBottom = (m_pButtonList[m_iItems-1]->y() + m_pButtonList[m_iItems-1]->height());
int iCanScroll = height() - iButtonBottom;
if ( iCanScroll > 0 ) iCanScroll = 0;
if ( iCanScroll >= iDiffY )
{
iDiffY = iCanScroll;
}
}
if ( m_iItemSelect != -1 )
{
m_pButtonList[m_iItemSelect]->setChecked(false);
m_iItemSelect = -1;
}
scroll(0, iDiffY);
emit ButtonScroll();
m_iLastScroll = iDiffY;
}
void RtListBox::MouseRelease()
{
m_pQTimerReset->stop();
m_iLastScroll -= m_iLastScroll%__AutoScroll_INTERVAL__;
if( m_iLastScroll != 0 )
m_pQTimerScroll->start(100);
}
void RtListBox::AutoScroll()
{
if ( m_iLastScroll != 0 )
{
if ( m_iLastScroll > 0)
{
m_iLastScroll -= __AutoScroll_INTERVAL__;
}
else
{
m_iLastScroll += __AutoScroll_INTERVAL__;
}
ScrollList(m_iLastScroll);
m_iLastScroll -= m_iLastScroll%__AutoScroll_INTERVAL__;
}
else
{
m_pQTimerScroll->stop();
}
}
void RtListBox::ResetLastScroll()
{
m_pQTimerReset->stop();
m_iLastScroll = 0;
}
void RtListBox::ButtonClick(int iSelect)
{
if ( m_iItemSelect != -1 )
{
m_pButtonList[m_iItemSelect]->setChecked(false);
m_iItemSelect = -1;
}
m_iItemSelect = iSelect;
m_pButtonList[m_iItemSelect]->setChecked(true);
emit ButtonSelect(m_iItemSelect);
}
int RtListBox::count () const
{
return m_iItems;
}
RtPushButton* RtListBox::GetButton(int iIndex)
{
// parameter check
if ( m_iItems <= iIndex || iIndex < 0 )
{
return NULL;
}
if(m_pButtonList[iIndex] != NULL)
return m_pButtonList[iIndex];
return NULL;
}