-
Notifications
You must be signed in to change notification settings - Fork 0
/
appdialog.cpp
181 lines (159 loc) · 4.29 KB
/
appdialog.cpp
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
#include "appdialog.h"
#include "ui_appdialog.h"
#include <QPushButton>
#include <QIcon>
#include <QtConcurrent>
#include <QFuture>
#include <QFutureWatcher>
namespace
{
struct IconLoaderResult
{
QTreeWidgetItem *item;
QIcon icon;
};
}
AppDialog::AppDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AppDialog)
{
ui->setupUi(this);
AppDef root = getApps();
for (auto &nameAppPair : root.subApps)
{
addApp(nameAppPair.first, nameAppPair.second, nullptr);
}
ui->appTree->hideColumn(1);
ui->appTree->setCurrentItem(nullptr);
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(false);
}
AppDialog::~AppDialog()
{
delete ui;
}
QString AppDialog::appPath() const
{
return mAppPath;
if (ui->appTree->currentItem())
return ui->appTree->currentItem()->text(1);
else
return "";
}
void AppDialog::addApp(const QString &name, const AppDef& app, QTreeWidgetItem *parent)
{
QTreeWidgetItem *item = new QTreeWidgetItem((QTreeWidget*)nullptr, {name, app.path});
if (parent)
{
parent->addChild(item);
}
else
{
ui->appTree->addTopLevelItem(item);
}
// Load icon concurrently
if (app.iconLoader)
{
std::function<QIcon()> iconLoader = app.iconLoader;
auto future = QtConcurrent::run([item, iconLoader](){
IconLoaderResult res;
res.item = item;
res.icon = iconLoader();
return res;
});
auto watcher = new QFutureWatcher<IconLoaderResult>(this);
connect(watcher, SIGNAL(finished()), this, SLOT(on_iconLoadedByFuture()));
connect(watcher, SIGNAL(finished()), watcher, SLOT(deleteLater()));
watcher->setFuture(future);
}
// add children
for (auto& nameAppPair : app.subApps)
{
addApp(nameAppPair.first, nameAppPair.second, item);
}
}
void AppDialog::on_iconLoadedByFuture()
{
auto *watcher = (QFutureWatcher<IconLoaderResult>*)sender();
IconLoaderResult res = watcher->result();
res.item->setIcon(0, res.icon);
}
void AppDialog::on_filterEdit_textChanged()
{
QString filter = ui->filterEdit->text();
if (filter.size() > 0)
{
for (QTreeWidgetItem *item : ui->appTree->findItems("", Qt::MatchContains | Qt::MatchRecursive, 0))
{
item->setHidden(true);
}
for (QTreeWidgetItem *item : ui->appTree->findItems(filter, Qt::MatchContains | Qt::MatchRecursive, 0))
{
if (item->text(1).length())// does it have exec?
{
item->setHidden(false);
QTreeWidgetItem *parent = item;
while ((parent = parent->parent()))
{
parent->setHidden(false);
}
}
}
}
else
{
for (QTreeWidgetItem *item : ui->appTree->findItems("", Qt::MatchContains | Qt::MatchRecursive, 0))
{
item->setHidden(false);
}
}
// expand items if not many
const int smallNum = 3;
int numVisibles = 0;
for (int i=0; i < ui->appTree->topLevelItemCount(); i++)
{
QTreeWidgetItem *item = ui->appTree->topLevelItem(i);
if (!item->isHidden())
{
numVisibles++;
}
}
if (numVisibles <= smallNum)
{
ui->appTree->expandAll();
}
else
{
ui->appTree->collapseAll();
}
if (ui->appTree->currentItem())
{
QTreeWidgetItem *parent = ui->appTree->currentItem();
while ((parent = parent->parent()))
{
ui->appTree->expandItem(ui->appTree->currentItem());
}
}
}
void AppDialog::on_appTree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(current != nullptr);
}
void AppDialog::on_appTree_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
if (ui->appTree->currentItem()->text(1).size())
{
ui->appTree->setCurrentItem(item);
mAppPath = ui->appTree->currentItem()->text(1);
close();
}
}
void AppDialog::on_buttonBox_accepted()
{
mAppPath = ui->appTree->currentItem()->text(1);
close();
}
void AppDialog::on_buttonBox_rejected()
{
mAppPath = "";
close();
}