Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 32d86dd625ba2263056541242ee9d7ec3e227965 (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
/*
   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)
{
	set_label("listview");
	set_border(true);
	
	listview_scroll = 0.0f;
	listview_selecteditem = 0;
}

ListView::~ListView()
{
}

void ListView::set_scroll(float scroll)
{
	listview_scroll = scroll;
	if (listview_scroll < 0)
		listview_scroll = 0;
	
	// calculate maximal scroll size
	float top = 0;
	for (Children::iterator it = children().begin(); it != children().end(); it++) {		
		top += (*it)->height();
	}
	if (listview_scroll > top) listview_scroll = top;
}

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()
{
	float content_top = 0;
	
	// reposition all children within the container
	for (Children::iterator it = children().begin(); it != children().end(); it++) {
		(*it)->set_width(width());
		if (content_top - listview_scroll < 0) {
			// child widget is invisible
			(*it)->hide();
		} else if ((content_top - listview_scroll) >= height()) {
			// child widget is invisible
			(*it)->hide();
		} else {
			(*it)->show();
			(*it)->set_location(0, content_top - listview_scroll);
		}
		
		content_top += (*it)->height();
	}
}

void ListView::deselect()
{
	listview_selecteditem = 0;
}

void ListView::select(ListItem *item)
{
	if (!item || is_child(item)) {
		listview_selecteditem = item;
	}
}

void ListView::clear()
{
	listview_selecteditem = 0;
	remove_children();
}

bool ListView::on_emit(Widget *sender, const Event event, void *data)
{
	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);
	assert(firstitem && seconditem);
	
	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);
}

}