| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
1 |
#include "pathcompletermodel.h" |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
2 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
3 |
#include <QFileInfo> |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
4 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
5 |
#include <algorithm> |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
6 |
#include <filesystem> |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
7 |
#include <vector> |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
8 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
9 |
namespace fs = std::filesystem; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
10 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
11 |
PathCompleterModel::PathCompleterModel(QObject *parent) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
12 |
: QAbstractListModel(parent) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
13 |
{ |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
14 |
} |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
15 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
16 |
int PathCompleterModel::rowCount(const QModelIndex &parent) const |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
17 |
{ |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
18 |
if (parent.isValid()) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
19 |
return 0; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
20 |
return static_cast<int>(m_entries.size()); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
21 |
} |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
22 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
23 |
QVariant PathCompleterModel::data(const QModelIndex &index, int role) const |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
24 |
{ |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
25 |
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size()) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
26 |
return {}; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
27 |
if (role == Qt::DisplayRole || role == Qt::EditRole) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
28 |
return m_entries.at(index.row()); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
29 |
return {}; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
30 |
} |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
31 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
32 |
void PathCompleterModel::setPrefix(const QString &text) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
33 |
{ |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
34 |
// Determine the directory whose children we should list. |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
35 |
QString parentDir; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
36 |
if (text.isEmpty()) { |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
37 |
parentDir = QStringLiteral("/"); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
38 |
} else if (text.endsWith(QLatin1Char('/'))) { |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
39 |
// Strip the trailing slash unless we're at filesystem root. |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
40 |
parentDir = (text.size() == 1) ? text : text.left(text.size() - 1); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
41 |
} else { |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
42 |
QFileInfo info(text); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
43 |
parentDir = info.path(); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
44 |
if (parentDir.isEmpty()) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
45 |
parentDir = QStringLiteral("/"); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
46 |
} |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
47 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
48 |
if (m_hasParent && parentDir == m_currentParent) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
49 |
return; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
50 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
51 |
QStringList newEntries; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
52 |
try { |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
53 |
const QByteArray parentNative = QFile::encodeName(parentDir); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
54 |
std::vector<std::pair<std::string, std::string>> entries; // {nativePath, filename} |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
55 |
for (const fs::directory_entry &entry : |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
56 |
fs::directory_iterator(parentNative.toStdString(), |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
57 |
fs::directory_options::skip_permission_denied)) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
58 |
{ |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
59 |
std::error_code ec; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
60 |
const std::string fname = entry.path().filename().native(); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
61 |
if (fname.empty() || fname[0] == '.') |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
62 |
continue; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
63 |
if (!entry.is_directory(ec)) |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
64 |
continue; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
65 |
entries.push_back({entry.path().native(), fname}); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
66 |
} |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
67 |
std::sort(entries.begin(), entries.end(), |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
68 |
[](const auto &a, const auto &b) { return a.second < b.second; }); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
69 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
70 |
newEntries.reserve(static_cast<qsizetype>(entries.size())); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
71 |
for (const auto &[npath, fname] : entries) { |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
72 |
newEntries.append(QFile::decodeName( |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
73 |
QByteArray::fromStdString(npath))); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
74 |
} |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
75 |
} catch (const fs::filesystem_error &) { |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
76 |
// Inaccessible parent — fall through with an empty list. |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
77 |
} |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
78 |
|
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
79 |
beginResetModel(); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
80 |
m_entries = std::move(newEntries); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
81 |
m_currentParent = parentDir; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
82 |
m_hasParent = true; |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
83 |
endResetModel(); |
| fd7f51cd |
Poul Sander |
2026-05-22 15:30 |
84 |
} |
|
|
1970-01-01 00:00 |
85 |
|