Skip to content

Commit c95e492

Browse files
committed
Hide extract/copy path context menu actions where it doesn't make sense
It doesn't make sense to extract folders, so that's hidden. And same for unknown files, there's no path to copy so we shouldn't think the user that it could be possible.
1 parent 206313b commit c95e492

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

apps/sagasu/include/filetreemodel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class FileTreeModel : public QAbstractItemModel
2828
public:
2929
explicit FileTreeModel(HashDatabase &database, bool showUnknown, const QString &gamePath, SqPackResource *data, QObject *parent = nullptr);
3030

31+
enum CustomRoles {
32+
PathRole = Qt::UserRole,
33+
IsUnknown,
34+
IsFolder,
35+
};
36+
3137
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
3238
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
3339

apps/sagasu/src/filetreemodel.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ QVariant FileTreeModel::data(const QModelIndex &index, int role) const
104104
return {};
105105

106106
auto item = static_cast<TreeInformation *>(index.internalPointer());
107-
if (role == Qt::UserRole) {
107+
if (role == PathRole) {
108108
if (item->type != TreeType::File || item->name.isEmpty()) {
109109
return {};
110110
}
@@ -122,6 +122,10 @@ QVariant FileTreeModel::data(const QModelIndex &index, int role) const
122122
}
123123

124124
return path;
125+
} else if (role == IsUnknown) {
126+
return item->name.isEmpty(); // unknown files/folders have no name (obviously, we don't know what its named!)
127+
} else if (role == IsFolder) {
128+
return item->type == TreeType::Folder;
125129
} else if (role == Qt::DisplayRole) {
126130
if (item->type == TreeType::Folder) {
127131
if (item->name.isEmpty()) {

apps/sagasu/src/filetreewindow.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,38 @@ FileTreeWindow::FileTreeWindow(HashDatabase &database, const QString &gamePath,
6363
auto index = treeWidget->indexAt(pos);
6464

6565
if (index.isValid()) {
66-
auto path = m_searchModel->data(index, Qt::UserRole).toString();
66+
const auto path = m_searchModel->data(index, FileTreeModel::CustomRoles::PathRole).toString();
67+
const auto isUnknown = m_searchModel->data(index, FileTreeModel::CustomRoles::IsUnknown).toBool();
68+
const auto isFolder = m_searchModel->data(index, FileTreeModel::CustomRoles::IsFolder).toBool();
6769

6870
auto menu = new QMenu();
6971

70-
auto extractAction = menu->addAction(i18nc("@action:inmenu", "Extract…"));
71-
extractAction->setIcon(QIcon::fromTheme(QStringLiteral("archive-extract-symbolic")));
72-
connect(extractAction, &QAction::triggered, this, [this, path] {
73-
Q_EMIT extractFile(path);
74-
});
75-
76-
auto copyFilePathAction = menu->addAction(i18nc("@action:inmenu", "Copy file path"));
77-
copyFilePathAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-symbolic")));
78-
connect(copyFilePathAction, &QAction::triggered, this, [this, path] {
79-
QClipboard *clipboard = QGuiApplication::clipboard();
80-
clipboard->setText(path);
81-
});
72+
// It doesn't make sense to extract folders
73+
if (!isFolder) {
74+
auto extractAction = menu->addAction(i18nc("@action:inmenu", "Extract…"));
75+
extractAction->setIcon(QIcon::fromTheme(QStringLiteral("archive-extract-symbolic")));
76+
connect(extractAction, &QAction::triggered, this, [this, path] {
77+
Q_EMIT extractFile(path);
78+
});
79+
}
80+
81+
// It doesn't make sense to copy file paths for... a file or folder that doesn't have a path.
82+
if (!isUnknown) {
83+
auto copyFilePathAction = menu->addAction(i18nc("@action:inmenu", "Copy file path"));
84+
copyFilePathAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-symbolic")));
85+
connect(copyFilePathAction, &QAction::triggered, this, [this, path] {
86+
QClipboard *clipboard = QGuiApplication::clipboard();
87+
clipboard->setText(path);
88+
});
89+
}
8290

8391
menu->exec(treeWidget->mapToGlobal(pos));
8492
}
8593
});
8694

8795
connect(treeWidget, &QTreeView::clicked, [this, treeWidget](const QModelIndex &item) {
8896
if (item.isValid()) {
89-
auto path = m_searchModel->data(item, Qt::UserRole).toString();
97+
auto path = m_searchModel->data(item, FileTreeModel::CustomRoles::PathRole).toString();
9098
Q_EMIT pathSelected(path);
9199
}
92100
});

0 commit comments

Comments
 (0)