| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2013~2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "filelistmodel.h" |
| 9 |
#include <QAbstractListModel> |
| 10 |
#include <QObject> |
| 11 |
#include <Qt> |
| 12 |
#include <algorithm> |
| 13 |
#include <fcitx-utils/i18n.h> |
| 14 |
#include <fcitx-utils/standardpaths.h> |
| 15 |
#include <filesystem> |
| 16 |
#include <iterator> |
| 17 |
|
| 18 |
fcitx::FileListModel::FileListModel(QObject *parent) |
| 19 |
: QAbstractListModel(parent) {} |
| 20 |
|
| 21 |
fcitx::FileListModel::~FileListModel() {} |
| 22 |
|
| 23 |
int fcitx::FileListModel::rowCount(const QModelIndex &parent) const { |
| 24 |
return parent.isValid() ? 0 : fileList_.size(); |
| 25 |
} |
| 26 |
|
| 27 |
QVariant fcitx::FileListModel::data(const QModelIndex &index, int role) const { |
| 28 |
if (!index.isValid() || index.row() >= fileList_.size()) { |
| 29 |
return {}; |
| 30 |
} |
| 31 |
|
| 32 |
switch (role) { |
| 33 |
case Qt::DisplayRole: |
| 34 |
if (fileList_[index.row()] == QUICK_PHRASE_CONFIG_FILE) { |
| 35 |
return _("Default"); |
| 36 |
} else { |
| 37 |
return QString::fromStdString( |
| 38 |
fileList_[index.row()].stem().string()); |
| 39 |
} |
| 40 |
case Qt::UserRole: |
| 41 |
return QString::fromStdString(fileList_[index.row()].string()); |
| 42 |
default: |
| 43 |
break; |
| 44 |
} |
| 45 |
return {}; |
| 46 |
} |
| 47 |
|
| 48 |
void fcitx::FileListModel::loadFileList() { |
| 49 |
beginResetModel(); |
| 50 |
fileList_.clear(); |
| 51 |
fileList_.push_back(QUICK_PHRASE_CONFIG_FILE); |
| 52 |
auto files = StandardPaths::global().locate(StandardPathsType::PkgData, |
| 53 |
QUICK_PHRASE_CONFIG_DIR, |
| 54 |
pathfilter::extension(".mb")); |
| 55 |
|
| 56 |
for (auto &file : files) { |
| 57 |
fileList_.push_back(std::filesystem::path(QUICK_PHRASE_CONFIG_DIR) / |
| 58 |
file.first); |
| 59 |
} |
| 60 |
|
| 61 |
endResetModel(); |
| 62 |
} |
| 63 |
|
| 64 |
int fcitx::FileListModel::findFile(const QString &lastFileName) { |
| 65 |
auto iter = std::ranges::find( |
| 66 |
fileList_, std::filesystem::path(lastFileName.toStdString())); |
| 67 |
if (iter == fileList_.end()) { |
| 68 |
return 0; |
| 69 |
} |
| 70 |
return std::distance(fileList_.begin(), iter); |
| 71 |
} |