-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicelist.cpp
More file actions
164 lines (144 loc) · 5.33 KB
/
Copy pathdevicelist.cpp
File metadata and controls
164 lines (144 loc) · 5.33 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
#include "devicelist.h"
#include <QStringListModel>
#include <QFileInfoList>
#include <QFileInfo>
#include <QDir>
#include <QStandardItem>
#include <QList>
#include <QMouseEvent>
#include <QStyle>
#include <QStorageInfo>
#include <QFileSystemModel>
#include <QAbstractItemModel>
#include <QDebug>
#include <QKeyEvent>
#include <QItemSelection>
#include <QHeaderView>
deviceList::deviceList(QWidget *parent ) :
QTreeView(parent)
{
// Ensure pointer is initialized to avoid undefined behavior on first use
fileModel = nullptr;
// Configure once rather than on every expand
setSelectionMode(QAbstractItemView::ExtendedSelection);
// Interactive mode + explicit resizeColumnToContents() after loading:
// ResizeToContents would keep re-measuring every row on each change,
// which gets slow on trees with thousands of files.
if (auto *hdr = header()) {
hdr->setSectionResizeMode(QHeaderView::Interactive);
hdr->setStretchLastSection(false);
}
// resizeColumnToContents() only measures the rows that are visible, so
// re-measure whenever a branch is expanded — otherwise file names under
// a freshly opened hour group stay truncated.
connect(this, &QTreeView::expanded, this, [this](const QModelIndex &) {
const int cols = model() ? model()->columnCount() : 0;
for (int c = 0; c < cols; ++c)
resizeColumnToContents(c);
});
// Optional performance hint for tree views with consistent row heights
setUniformRowHeights(true);
}
void deviceList::setFiles(QList<QFileInfo> files)
{
qDebug() << "SetFiles" << files.count();
if (fileModel) {
// detach model from the view before deleting to avoid use-after-free
this->setModel(nullptr);
delete fileModel;
fileModel = nullptr;
}
fileModel = new FileInfoModel(files, this);
connect(fileModel, &FileInfoModel::dataChanged, this, &deviceList::updateSelectedCount);
// Bulk check changes are announced with layoutChanged (refreshChecks)
connect(fileModel, &FileInfoModel::layoutChanged, this, [this]() {
updateSelectedCount(QModelIndex(), QModelIndex(), {});
});
connect(fileModel, &FileInfoModel::treeBuildingFinished, this, &deviceList::expandTree);
this->setModel(fileModel);
// Ensure columns fit contents after (re)setting the model
const int cols = fileModel->columnCount();
for (int c = 0; c < cols; ++c) {
resizeColumnToContents(c);
}
}
void deviceList::expandTree()
{
if (!fileModel)
return;
// Expand only the relevant levels (year and month) and size the first column
resizeColumnToContents(0);
// Expand only year and month levels
for (int ri = 0; ri < fileModel->rowCount(); ++ri) {
QModelIndex rootIndex = fileModel->index(ri, 0);
this->expand(rootIndex);
for (int i = 0; i < fileModel->rowCount(rootIndex); ++i) {
QModelIndex yearIndex = fileModel->index(i, 0, rootIndex);
this->expand(yearIndex);
for (int j = 0; j < fileModel->rowCount(yearIndex); ++j) {
QModelIndex monthIndex = fileModel->index(j, 0, yearIndex);
this->expand(monthIndex);
for (int t = 0; t < fileModel->rowCount(monthIndex); ++t) {
QModelIndex dayIndex = fileModel->index(t, 0, monthIndex);
this->collapse(dayIndex);
}
}
}
}
// Get the current width of the column
// int currentWidth = columnWidth(0);
// Add 20px to the current width
// int newWidth = currentWidth;
//+30;
// Set the new width for the column
// setColumnWidth(0, newWidth);
if (fileModel->rowCount() > 0) {
const QModelIndex selectedFirst = fileModel->index(0, 0);
if (selectedFirst.isValid()) {
if (auto *node = static_cast<TreeNode *>(selectedFirst.internalPointer())) {
emit selectedNode(node);
}
}
}
// After expanding, ensure all columns are sized to fit current contents
if (fileModel) {
const int cols = fileModel->columnCount();
for (int c = 0; c < cols; ++c) {
resizeColumnToContents(c);
}
}
emit doneLoading();
}
void deviceList::updateSelectedCount(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) {
Q_UNUSED(topLeft);
Q_UNUSED(bottomRight);
Q_UNUSED(roles);
if (!fileModel) return;
int cnt = fileModel->countSelected();
qint64 size = fileModel->countSelectedSize();
emit selectedUpdated(cnt, size);
}
void deviceList::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
emit returnButtonPressed();
return;
}
if (event->key() == Qt::Key_Space) {
emit spaceButtonPressed();
return;
}
QTreeView::keyPressEvent(event);
}
void deviceList::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
if (!selected.isEmpty()) {
const QModelIndex selectedFirst = selected.at(0).topLeft();
if (selectedFirst.isValid()) {
if (auto *node = static_cast<TreeNode *>(selectedFirst.internalPointer())) {
emit selectedNode(node);
}
}
}
QTreeView::selectionChanged(selected, deselected);
}