1+ #include " AppRemoval.hpp"
2+ #include " Model/AppListDelegate.hpp"
3+
4+ #include < Shared.hpp>
5+ #include < iostream>
6+
7+ std::vector<Application> Ui::appsForRemoval;
8+
9+ AppListModel *modelAppList0;
10+ AppListDelegate *delegateAppList0;
11+
12+ std::vector<QString> rSelectedApps;
13+ std::vector<Application> rFilteredApps, rApps;
14+
15+ void Ui_AppRemovalDialog::performLogic () const {
16+ self->setFixedSize (self->size ());
17+
18+ modelAppList0 = new AppListModel (appListView);
19+ delegateAppList0 = new AppListDelegate (appListView);
20+
21+ self->setFixedSize (self->size ());
22+
23+ appListView->setModel (modelAppList0);
24+ appListView->setItemDelegate (delegateAppList0);
25+ appListView->setViewMode (QListView::ListMode);
26+ appListView->setUniformItemSizes (true );
27+ appListView->setSpacing (5 );
28+ appListView->setSelectionMode (QAbstractItemView::MultiSelection);
29+ appListView->setEditTriggers (QAbstractItemView::NoEditTriggers);
30+ appListView->setFocusPolicy (Qt::StrongFocus);
31+
32+ rApps = SharedCode::GetAppList ();
33+ for (const auto &app: rApps)
34+ modelAppList0->addItem (app);
35+
36+ QObject::connect (filterBox, &QLineEdit::textChanged, self, [&](const QString &text) {
37+ QItemSelectionModel *selectionModel = appListView->selectionModel ();
38+
39+ rFilteredApps.clear ();
40+ for (const auto &app: rApps) {
41+ if (app.name .contains (text, Qt::CaseInsensitive))
42+ rFilteredApps.push_back (app);
43+ }
44+
45+ modelAppList0->clear ();
46+ for (const auto &app: rFilteredApps)
47+ modelAppList0->addItem (app);
48+
49+ // restore selections
50+ for (int i = 0 ; i < modelAppList0->rowCount (); i++) {
51+ QModelIndex index = modelAppList0->index (i, 0 );
52+ QString appName = modelAppList0->data (index, Qt::DisplayRole).toString ();
53+ if (std::find (rSelectedApps.begin (), rSelectedApps.end (), appName) != rSelectedApps.end ())
54+ selectionModel->select (index, QItemSelectionModel::Select);
55+ }
56+ });
57+
58+ QObject::connect (appListView->selectionModel (), &QItemSelectionModel::selectionChanged, self,
59+ [&](const QItemSelection &selected, const QItemSelection &deselected) {
60+ for (const QModelIndex &index: selected.indexes ()) {
61+ QString selectedName = modelAppList0->data (index, Qt::DisplayRole).toString ();
62+
63+ auto it = std::find_if (rSelectedApps.begin (), rSelectedApps.end (),
64+ [&](const QString &appName) {
65+ return appName == selectedName;
66+ });
67+
68+ if (it == rSelectedApps.end ()) {
69+ auto appIt = std::find_if (rApps.begin (), rApps.end (), [&](const Application &app) {
70+ return app.name == selectedName;
71+ });
72+
73+ if (appIt != rApps.end ())
74+ rSelectedApps.push_back (selectedName);
75+ }
76+ }
77+
78+ for (const QModelIndex &index: deselected.indexes ()) {
79+ QString deselectedName = modelAppList0->data (index, Qt::DisplayRole).toString ();
80+
81+ rSelectedApps.erase (std::remove_if (rSelectedApps.begin (), rSelectedApps.end (),
82+ [&](const QString &appName) {
83+ return appName == deselectedName;
84+ }), rSelectedApps.end ());
85+ }
86+ });
87+
88+ QObject::connect (buttonBox, &QDialogButtonBox::clicked, self, [&](QAbstractButton *button) {
89+ if (buttonBox->standardButton (button) != QDialogButtonBox::Apply)
90+ return ;
91+
92+ Ui::appsForRemoval.clear ();
93+
94+ for (const QString &index: rSelectedApps) {
95+ auto it = std::find_if (rApps.begin (), rApps.end (), [&](const Application &app) {
96+ return app.name == index;
97+ });
98+
99+ if (it != rApps.end ()) {
100+ Ui::appsForRemoval.push_back (*it);
101+ }
102+ }
103+
104+ appListView->selectionModel ()->clearSelection ();
105+ self->accept ();
106+ });
107+
108+ QObject::connect (buttonBox, &QDialogButtonBox::rejected, self, [&]() {
109+ self->reject ();
110+ });
111+ }
0 commit comments