-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmainwindow.cc
More file actions
329 lines (274 loc) · 12.1 KB
/
mainwindow.cc
File metadata and controls
329 lines (274 loc) · 12.1 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "mainwindow.hpp"
#include "checkabletreeitem.hpp"
#include <QtWidgets>
namespace {
void buildFileSystemTreeImpl(QStandardItemModel *model,
QTreeView *treeView,
QProgressBar *progressBar,
QLabel *statusLabel,
QLabel *countLabel,
QVBoxLayout *mainLayout,
QWidget *centralWidget,
std::function<int(QStandardItem *)> countTreeItems,
std::function<int(QStandardItem *)> countCheckedItems)
{
// 显示加载状态
QLabel *loadingLabel = new QLabel("Scanning file system, please wait...", centralWidget);
loadingLabel->setAlignment(Qt::AlignCenter);
mainLayout->insertWidget(2, loadingLabel); // 插入到树视图上方
// 使用单次定时器延迟构建,确保UI先显示
QMetaObject::invokeMethod(
qApp,
[model,
treeView,
progressBar,
statusLabel,
countLabel,
loadingLabel,
countTreeItems,
countCheckedItems]() {
loadingLabel->setText("Building root directory...");
// 获取根目录路径(跨平台)
QList<QPair<QString, QString>> rootPaths;
#ifdef Q_OS_WIN
// Windows系统:获取所有驱动器
QFileInfoList drives = QDir::drives();
for (const QFileInfo &drive : drives) {
rootPaths.append(qMakePair(drive.absoluteFilePath(), drive.absoluteFilePath()));
}
#else
// Unix/Linux/Mac系统:使用用户主目录和常见系统目录
rootPaths.append(qMakePair(QDir::homePath(), "Home Directory"));
rootPaths.append(qMakePair("/", "Root Directory"));
rootPaths.append(qMakePair("/etc", "System Configuration"));
rootPaths.append(qMakePair("/usr", "User Programs"));
#endif
progressBar->setVisible(true);
statusLabel->setVisible(true);
int totalPaths = rootPaths.size();
int processed = 0;
// 清空模型
model->clear();
// 构建根节点
CheckableTreeItem *rootItem = new CheckableTreeItem("Local File System");
model->appendRow(rootItem);
// 递归构建文件树函数
std::function<void(CheckableTreeItem *, const QString &, int)> buildTree;
buildTree = [&](CheckableTreeItem *parentItem, const QString &path, int depth) {
if (depth >= 5) { // 限制最大层级为5层
return;
}
QDir dir(path);
if (!dir.exists()) {
return;
}
// 设置过滤器:只显示目录,不显示隐藏文件和系统文件
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
dir.setSorting(QDir::Name | QDir::IgnoreCase);
QFileInfoList entries;
try {
entries = dir.entryInfoList();
} catch (...) {
// 跳过无权限访问的目录
return;
}
// 限制每个目录下最多显示10个子项,避免过多
int maxChildren = 10;
int count = 0;
for (const QFileInfo &entry : entries) {
if (count >= maxChildren)
break;
QString entryName = entry.fileName();
QString fullPath = entry.absoluteFilePath();
// 跳过一些系统目录和无关目录
if (entryName.startsWith(".") || entryName == "System32"
|| entryName == "Windows" || entryName == "proc" || entryName == "sys"
|| entryName == "dev") {
continue;
}
CheckableTreeItem *item = new CheckableTreeItem(entryName);
item->setToolTip(fullPath);
parentItem->appendRow(item);
// 递归构建子目录(深度+1)
buildTree(item, fullPath, depth + 1);
count++;
}
};
// 为每个根路径构建子树
for (const auto &rootPair : rootPaths) {
QString path = rootPair.first;
QString displayName = rootPair.second;
statusLabel->setText(QString("Scanning: %1").arg(path));
qApp->processEvents(); // 处理事件,更新UI
CheckableTreeItem *driveItem = new CheckableTreeItem(displayName);
driveItem->setToolTip(path);
rootItem->appendRow(driveItem);
buildTree(driveItem, path, 1);
processed++;
progressBar->setValue((processed * 100) / totalPaths);
qApp->processEvents(); // 处理事件,更新UI
}
// 完成构建,隐藏进度条和加载标签
progressBar->setVisible(false);
statusLabel->setVisible(false);
loadingLabel->setVisible(false);
loadingLabel->deleteLater();
// 展开根节点
treeView->expand(rootItem->index());
// 显示统计信息
int totalItems = countTreeItems(rootItem);
statusLabel->setText(QString("Loaded %1 directory items").arg(totalItems));
statusLabel->setVisible(true);
// 初始计数
int checkedCount = countCheckedItems(rootItem);
countLabel->setText(QString("Checked Items: %1").arg(checkedCount));
},
Qt::QueuedConnection);
}
} // namespace
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("File System Checkbox Tree");
resize(700, 650);
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
// 创建主布局
QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
// 添加标题
QLabel *titleLabel = new QLabel("Local File System Hierarchical Checkboxes", this);
mainLayout->addWidget(titleLabel);
// 创建说明标签
QLabel *descriptionLabel
= new QLabel("This example uses local real file paths to build a tree structure, "
"demonstrating recursive checkbox functionality:\n"
"- Maximum depth limited to 5 levels to prevent excessive data\n"
"- Supports real directory structure of file system\n"
"- Automatically skips directories without access permissions",
this);
descriptionLabel->setWordWrap(true);
mainLayout->addWidget(descriptionLabel);
// 创建树状视图和模型
QStandardItemModel *model = new QStandardItemModel(this);
QTreeView *treeView = new QTreeView(this);
treeView->setModel(model);
treeView->setHeaderHidden(true);
// 创建进度条和状态标签
QProgressBar *progressBar = new QProgressBar(this);
progressBar->setRange(0, 100);
progressBar->setValue(0);
progressBar->setVisible(false);
QLabel *statusLabel = new QLabel("Building file tree...", this);
statusLabel->setAlignment(Qt::AlignCenter);
statusLabel->setFrameStyle(QFrame::Box);
statusLabel->setVisible(false);
// 创建操作按钮组
QGroupBox *controlGroup = new QGroupBox("Operation Control", this);
QHBoxLayout *controlLayout = new QHBoxLayout(controlGroup);
QPushButton *selectAllBtn = new QPushButton("Select All", this);
QPushButton *deselectAllBtn = new QPushButton("Deselect All", this);
QPushButton *expandAllBtn = new QPushButton("Expand All", this);
QPushButton *collapseAllBtn = new QPushButton("Collapse All", this);
QPushButton *refreshBtn = new QPushButton("Refresh", this);
controlLayout->addWidget(selectAllBtn);
controlLayout->addWidget(deselectAllBtn);
controlLayout->addWidget(expandAllBtn);
controlLayout->addWidget(collapseAllBtn);
controlLayout->addWidget(refreshBtn);
// 统计显示
QLabel *countLabel = new QLabel("Checked Items: 0", this);
// 将组件添加到主布局
mainLayout->addWidget(treeView);
mainLayout->addWidget(progressBar);
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(controlGroup);
mainLayout->addWidget(countLabel);
// Helper function: count tree node items
auto countTreeItems = [](QStandardItem *root) -> int {
std::function<int(QStandardItem *)> countFunc;
countFunc = [&](QStandardItem *item) -> int {
int count = 1; // 当前节点
for (int i = 0; i < item->rowCount(); i++) {
count += countFunc(item->child(i));
}
return count;
};
return countFunc(root);
};
// 辅助函数:统计选中节点数量
auto countCheckedItems = [](QStandardItem *root) -> int {
std::function<int(QStandardItem *)> countFunc;
countFunc = [&](QStandardItem *item) -> int {
int count = 0;
if (item->isCheckable() && item->checkState() == Qt::Checked) {
count = 1;
}
for (int i = 0; i < item->rowCount(); i++) {
count += countFunc(item->child(i));
}
return count;
};
return countFunc(root);
};
// 构建文件系统树的函数
auto buildFileSystemTree = [model,
treeView,
progressBar,
statusLabel,
countLabel,
mainLayout,
centralWidget,
countTreeItems,
countCheckedItems]() {
return buildFileSystemTreeImpl(model,
treeView,
progressBar,
statusLabel,
countLabel,
mainLayout,
centralWidget,
countTreeItems,
countCheckedItems);
};
// 初始构建文件系统树
buildFileSystemTree();
// 连接按钮信号
connect(selectAllBtn, &QPushButton::clicked, [model, countLabel, countCheckedItems]() {
for (int i = 0; i < model->rowCount(); i++) {
QStandardItem *rootItem = model->item(i);
if (rootItem && rootItem->isCheckable()) {
rootItem->setData(Qt::Checked, Qt::CheckStateRole);
}
}
// 更新计数
int checkedCount = countCheckedItems(model->item(0));
countLabel->setText(QString("Checked Items: %1").arg(checkedCount));
});
connect(deselectAllBtn, &QPushButton::clicked, [model, countLabel]() {
for (int i = 0; i < model->rowCount(); i++) {
QStandardItem *rootItem = model->item(i);
if (rootItem && rootItem->isCheckable()) {
rootItem->setData(Qt::Unchecked, Qt::CheckStateRole);
}
}
// 更新计数
countLabel->setText("Checked Items: 0");
});
connect(expandAllBtn, &QPushButton::clicked, this, [treeView]() { treeView->expandAll(); });
connect(collapseAllBtn, &QPushButton::clicked, this, [treeView]() { treeView->collapseAll(); });
connect(refreshBtn, &QPushButton::clicked, this, [buildFileSystemTree]() {
buildFileSystemTree();
});
// 连接模型数据变化信号来更新计数
connect(model,
&QStandardItemModel::itemChanged,
this,
[model, countLabel, countCheckedItems](QStandardItem *item) {
Q_UNUSED(item)
if (model->rowCount() > 0) {
int checkedCount = countCheckedItems(model->item(0));
countLabel->setText(QString("Checked Items: %1").arg(checkedCount));
}
});
}
MainWindow::~MainWindow() {}