-
Notifications
You must be signed in to change notification settings - Fork 2
/
view.rs
192 lines (172 loc) · 5.7 KB
/
view.rs
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
use qml::*;
use lazysort::*;
use super::Package;
pub fn show(gathered: Vec<Package>) {
let mut engine = QmlEngine::new();
let list = form_list(&gathered);
let qvar: QVariant = list.get_qvar();
let mut repos = gathered.iter().map(|p| p.group.clone()).collect::<Vec<_>>();
repos.dedup();
let mut groups = gathered.iter()
.flat_map(|p| p.meta.iter())
.sorted()
.cloned()
.collect::<Vec<_>>();
groups.dedup();
let qrepos = repos.iter().map(|s| s.clone().into()).collect::<Vec<QVariant>>();
let qgroups = groups.iter()
.map(|s| {
if s == "" {
"(no group)".into()
} else {
s.as_str().into()
}
})
.collect::<Vec<QVariant>>();
let qpckgs = QPackages::new(Packages {
vec: gathered,
list: list,
repos: repos,
groups: groups,
chosen_repo: -1,
chosen_group: -1,
selected: SelectedPackages::new(),
});
engine.set_property("packages", &qvar);
engine.set_and_store_property("qpkgs", qpckgs.get_qobj());
engine.set_and_store_property("repos", qrepos);
engine.set_and_store_property("groups", qgroups);
engine.load_data(include_str!("view.qml"));
engine.exec();
}
Q_LISTMODEL!{
pub QPackageList {
name: String,
version: String,
repo: String,
group: String,
selected: bool
}
}
fn form_list(gathered: &[Package]) -> QPackageList {
let mut qalm = QPackageList::new();
qalm.set_data(filter_for_qml(gathered, &|_| true, None));
qalm
}
pub struct Packages {
vec: Vec<Package>,
list: QPackageList,
repos: Vec<String>,
groups: Vec<String>,
chosen_repo: i32,
chosen_group: i32,
selected: SelectedPackages,
}
pub struct SelectedPackages {
vec: Vec<Package>,
}
fn filter_for_qml(vec: &[Package],
filter: &Fn(&&Package) -> bool,
selecteds: Option<&SelectedPackages>)
-> Vec<(String, String, String, String, bool)> {
vec.into_iter()
.filter(filter)
.map(|pkg| {
let meta = match pkg.meta.first() {
Some(k) => k.clone(),
None => "".into(),
};
let selected = if let Some(selecteds) = selecteds {
selecteds.vec.contains(pkg)
} else {
false
};
(pkg.name.clone(), pkg.version.clone(), pkg.group.clone(), meta, selected)
})
.collect()
}
Q_OBJECT!(
pub Packages as QPackages {
signals:
fn notify_packages_changed(text: String);
slots:
fn request_update_repo(r: i32);
fn request_update_group(r: i32);
fn add_package(i: i32);
fn remove_package(i: i32);
properties:
}
);
impl QPackages {
fn request_update_repo(&mut self, r: i32) -> Option<&QVariant> {
self.chosen_repo = r;
self.decide_and_update();
None
}
fn request_update_group(&mut self, r: i32) -> Option<&QVariant> {
self.chosen_group = r;
self.decide_and_update();
None
}
fn decide_and_update(&mut self) {
let data = {
let s = &self;
let closure: Box<Fn(&&Package) -> bool> = match (self.chosen_repo, self.chosen_group) {
(-1, -1) => box |_| true,
(-1, group) => box move |pkg| pkg.meta.contains(&s.groups[group as usize]),
(repo, -1) => box move |pkg| pkg.group == s.repos[repo as usize],
(repo, group) => {
box move |pkg| {
pkg.group == s.repos[repo as usize] &&
pkg.meta.contains(&s.groups[group as usize])
}
}
};
let selected = Some(&self.selected);
filter_for_qml(&self.vec, closure.as_ref(), selected)
};
self.list.set_data(data);
}
fn add_package(&mut self, index: i32) -> Option<&QVariant> {
let pkg_in_list = &self.list.view_data()[index as usize];
self.list.change_line(index as usize,
pkg_in_list.0.clone(),
pkg_in_list.1.clone(),
pkg_in_list.2.clone(),
pkg_in_list.3.clone(),
true);
let pkg_name = &pkg_in_list.0;
let pkg = self.vec.iter().find(|pkg| pkg_name == &pkg.name).unwrap().clone();
self.selected.add_package(pkg);
self.notify_packages_changed(self.selected.get_text());
None
}
fn remove_package(&mut self, index: i32) -> Option<&QVariant> {
let pkg_in_list = &self.list.view_data()[index as usize];
self.list.change_line(index as usize,
pkg_in_list.0.clone(),
pkg_in_list.1.clone(),
pkg_in_list.2.clone(),
pkg_in_list.3.clone(),
false);
let pkg_name = &pkg_in_list.0;
let pkg = self.vec.iter().find(|pkg| pkg_name == &pkg.name).unwrap().clone();
self.selected.remove_package(pkg);
self.notify_packages_changed(self.selected.get_text());
None
}
}
impl SelectedPackages {
fn new() -> Self {
SelectedPackages { vec: Vec::new() }
}
fn add_package(&mut self, package: Package) {
self.vec.push(package);
}
fn remove_package(&mut self, package: Package) {
self.vec.retain(|p| p.name != package.name);
}
fn get_text(&self) -> String {
self.vec.iter().map(|p| &p.name as &str).collect::<Vec<&str>>().join(" ")
}
}