-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlanSetListModel.vala
executable file
·204 lines (144 loc) · 3.82 KB
/
PlanSetListModel.vala
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
// $Id: PlanSetListModel.vala 43 2010-11-13 09:32:38Z mitrandir $
public class PlanSetListModel: GLib.Object, Gtk.TreeModel
{
int stamp;
PlanSet plan_set = null;
private enum Column
{
NAME = 0,
PLAN,
TOTAL
}
public PlanSetListModel(PlanSet plan_set)
{
stamp = (int)Random.next_int();
this.plan_set = plan_set;
this.plan_set.plan_added.connect(on_plan_added);
this.plan_set.plan_removing.connect(on_plan_removing);
}
public unowned Plan plan_from_path(Gtk.TreePath path)
{
Gtk.TreeIter iter;
get_iter(out iter, path);
return (Plan)iter.user_data;
}
// Private
void plan_iter(Plan plan, out Gtk.TreeIter iter)
{
iter.stamp = stamp;
iter.user_data = plan;
iter.user_data2 = null;
iter.user_data3 = null;
}
void on_plan_added(Plan plan)
{
Gtk.TreeIter iter;
plan_iter(plan, out iter);
row_inserted(get_path(iter), iter);
}
void on_plan_removing(Plan plan)
{
Gtk.TreeIter iter;
plan_iter(plan, out iter);
row_deleted(get_path(iter));
}
// TreeModel implementatioin
public Type get_column_type(int index)
{
switch (index)
{
case Column.NAME: return typeof(string);
case Column.PLAN: return typeof(Plan);
default: return typeof(int);
}
}
public Gtk.TreeModelFlags get_flags()
{
return 0;
}
public bool get_iter(out Gtk.TreeIter iter, Gtk.TreePath path)
{
var i = path.get_indices()[0];
if (0 <= i && i < plan_set.plans.length())
{
plan_iter(plan_set.plans.nth_data(i), out iter);
return true;
}
return false;
}
public int get_n_columns()
{
return Column.TOTAL;
}
public Gtk.TreePath get_path(Gtk.TreeIter iter)
{
var path = new Gtk.TreePath();
path.append_index(plan_set.plans.index((Plan)iter.user_data));
return path;
}
public void get_value(Gtk.TreeIter iter, int column, out Value value)
{
var plan = (Plan)iter.user_data;
switch (column)
{
case Column.NAME: value = plan.name; break;
case Column.PLAN: value = plan; break;
default: value = 0; break;
}
}
public bool iter_children(out Gtk.TreeIter iter, Gtk.TreeIter? parent)
{
if (parent == null)
{
if (plan_set.plans.length() > 0)
{
plan_iter(plan_set.plans.first().data, out iter);
return true;
}
}
return false;
}
public bool iter_has_child(Gtk.TreeIter iter)
{
return false;
}
public int iter_n_children(Gtk.TreeIter? iter)
{
if (iter == null)
return (int)plan_set.plans.length();
else
return 0;
}
public bool iter_next(ref Gtk.TreeIter iter)
{
var plan = (Plan)iter.user_data;
int i = plan_set.plans.index(plan);
if (i+1 < plan_set.plans.length())
{
plan_iter(plan_set.plans.nth_data(i+1), out iter);
return true;
}
return false;
}
public bool iter_nth_child(out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n)
{
if (parent != null)
return false;
if (0 <= n && n < plan_set.plans.length())
{
plan_iter(plan_set.plans.nth_data(n), out iter);
return true;
}
return false;
}
public bool iter_parent(out Gtk.TreeIter iter, Gtk.TreeIter child)
{
return false;
}
public void ref_node(Gtk.TreeIter iter)
{
}
public void unref_node(Gtk.TreeIter iter)
{
}
}