blob: 185b2299a60f8af3628d602beb8116827512261d (
plain)
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
|
/*
ui/listview.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include "ui/listview.h"
namespace ui
{
ListView::ListView(Widget *parent) : Widget(parent)
{
listview_scroll = 0.0f;
listview_selecteditem = 0;
set_label("listview");
set_border(true);
set_background(true);
listview_scrollbar = new ScrollBar(this);
listview_scrollbar->set_background(false);
listview_scrollbar->set_border(false);
}
ListView::~ListView()
{
}
void ListView::set_scroll(float scroll)
{
listview_scroll = scroll;
if (listview_scroll < 0)
listview_scroll = 0;
// calculate maximal scroll size
float total_height = 0;
for (Children::iterator it = children().begin(); it != children().end(); it++) {
total_height += (*it)->height();
}
if (listview_scroll > total_height) listview_scroll = total_height;
}
void ListView::inc_scroll(float scroll)
{
set_scroll(listview_scroll + scroll);
}
void ListView::dec_scroll(float scroll)
{
set_scroll(listview_scroll - scroll);
}
void ListView::resize()
{
listview_scrollbar->set_size(font()->height(), height());
listview_scrollbar->set_location(width() - listview_scrollbar->width(), 0);
float total_height = 0;
// reposition all children within the container
for (Children::iterator it = children().begin(); it != children().end(); it++) {
if ((*it) != static_cast<Widget *>(listview_scrollbar)) {
(*it)->set_width(width() - listview_scrollbar->width());
if (total_height - listview_scroll < 0) {
// child widget is invisible
(*it)->hide();
} else if ((total_height - listview_scroll + (*it)->height()) > height()) {
// child widget is invisible
(*it)->hide();
} else {
(*it)->show();
(*it)->set_location(0, total_height - listview_scroll);
}
total_height += (*it)->height();
}
}
if (total_height > height()) {
listview_scrollbar->set_range(0.0f, total_height - height());
listview_scrollbar->set_value(listview_scroll);
listview_scrollbar->show();
} else {
for (Children::iterator it = children().begin(); it != children().end(); it++) {
if ((*it) != static_cast<Widget *>(listview_scrollbar)) {
(*it)->set_width(width());
}
}
listview_scrollbar->hide();
}
}
void ListView::deselect()
{
listview_selecteditem = 0;
}
void ListView::select(ListItem *item)
{
if (!item || is_child(item)) {
listview_selecteditem = item;
}
}
void ListView::clear()
{
listview_scroll = 0;
listview_selecteditem = 0;
remove_children();
listview_scrollbar = new ScrollBar(this);
listview_scrollbar->set_background(false);
listview_scrollbar->set_border(false);
listview_scrollbar->set_range(0.0f, 0.0f);
listview_scrollbar->set_value(listview_scroll);
}
bool ListView::on_emit(Widget *sender, const Event event, void *data)
{
if (sender == listview_scrollbar) {
if (event == Widget::EventScrollBarChanged) {
listview_scroll = listview_scrollbar->value();
resize();
}
return true;
} else if ((sender->parent() == this) && (event == Widget::EventListItemClicked)) {
listview_selecteditem = static_cast<ListItem *>(sender);
emit(EventListViewChanged);
return true;
}
return false;
}
bool compare_listitems(const Widget *first, const Widget *second)
{
const ListItem *firstitem = dynamic_cast<const ListItem *>(first);
const ListItem *seconditem = dynamic_cast<const ListItem *>(second);
if (!firstitem)
return true;
else if (!seconditem)
return false;
if (firstitem->sortkey() < seconditem->sortkey()) {
return true;
} else if (firstitem->sortkey() > seconditem->sortkey()) {
return false;
} else {
return (firstitem->sortkey().length() < seconditem->sortkey().length());
}
}
void ListView::sort()
{
children().sort(compare_listitems);
}
bool compare_listitems_reverse(const Widget *first, const Widget *second)
{
const ListItem *firstitem = dynamic_cast<const ListItem *>(first);
const ListItem *seconditem = dynamic_cast<const ListItem *>(second);
if (!firstitem)
return true;
else if (!seconditem)
return false;
if (firstitem->sortkey() < seconditem->sortkey()) {
return false;
} else if (firstitem->sortkey() > seconditem->sortkey()) {
return true;
} else {
return (firstitem->sortkey().length() > seconditem->sortkey().length());
}
}
void ListView::sort_reverse()
{
children().sort(compare_listitems_reverse);
}
}
|