Skip to content

Commit 28426af

Browse files
committed
Implement multiselect mode
This can be triggered with the common Ctrl+Click keybinding.
1 parent 9a75663 commit 28426af

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/Models/path_model.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "path_model.hpp"
22

3+
#include <ranges>
4+
35
#include "backend.hpp"
46
#include "path_registry.hpp"
57
#include "settings.hpp"
@@ -25,6 +27,8 @@ QVariant PathModel::data(const QModelIndex &index, int role) const {
2527
return QString::fromStdString(p.pretty_print());
2628
case UsedRole:
2729
return p.used;
30+
case MultiselectRole:
31+
return p.multiselect;
2832
case IconRole:
2933
return QString::fromStdString(p.iconName);
3034
case ThumbnailRole:
@@ -52,8 +56,17 @@ void PathModel::taint_all_used() {
5256
check_should_quit();
5357
}
5458

59+
void PathModel::multiselect(int i) {
60+
paths[i].multiselect = !paths[i].multiselect;
61+
multiselected += (2 * paths[i].multiselect) - 1;
62+
emit dataChanged(index(i, 0), index(i, 0));
63+
refresh_folded_paths();
64+
}
65+
5566
void PathModel::refresh_folded_paths() {
56-
folded_uri_list = std::accumulate(paths.cbegin(), paths.cend(), QString(), [&](QString s, const auto p) { return s.append(QString::fromStdString(p.get_uri()) + "\n"); });
67+
// only add multiselected items in multiselect mode
68+
auto v = paths | std::views::filter([&](const auto &i) { return !multiselected || i.multiselect; });
69+
folded_uri_list = std::accumulate(v.cbegin(), v.cend(), QString(), [](QString s, const auto p) { return s.append(QString::fromStdString(p.get_uri()) + "\n"); });
5770
emit foldedUriListChanged(folded_uri_list);
5871
}
5972

src/Models/path_model.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ class PathModel : public QAbstractListModel {
1212
QML_SINGLETON
1313

1414
Q_PROPERTY(QString foldedUriList MEMBER folded_uri_list NOTIFY foldedUriListChanged)
15+
Q_PROPERTY(int multiSelected MEMBER multiselected NOTIFY foldedUriListChanged)
1516
public:
16-
PathModel(QObject *parent = nullptr);
17+
explicit PathModel(QObject *parent = nullptr);
1718

1819
virtual int rowCount(const QModelIndex &) const override;
1920
virtual QVariant data(const QModelIndex &index, int role) const override;
2021
virtual QHash<int, QByteArray> roleNames() const override;
2122

2223
Q_INVOKABLE void taint_used(int i);
2324
Q_INVOKABLE void taint_all_used();
25+
Q_INVOKABLE void multiselect(int i);
2426
Q_INVOKABLE void refresh_folded_paths();
2527
Q_INVOKABLE void open(int i) const;
2628
Q_INVOKABLE void finish_init();
@@ -33,12 +35,14 @@ class PathModel : public QAbstractListModel {
3335
UriRole,
3436
PrettyRole,
3537
UsedRole,
38+
MultiselectRole,
3639
IconRole,
3740
ThumbnailRole,
3841
ExistsRole,
3942
};
40-
QHash<int, QByteArray> role_names {{PathRole, "path"}, {UriRole, "uri"}, {PrettyRole, "pretty"}, {UsedRole, "used"}, {IconRole, "iconName"}, {ThumbnailRole, "thumbnail"}, {ExistsRole, "exists"}};
43+
QHash<int, QByteArray> role_names {{PathRole, "path"}, {UriRole, "uri"}, {PrettyRole, "pretty"}, {UsedRole, "used"}, {MultiselectRole, "multiselect"}, {IconRole, "iconName"}, {ThumbnailRole, "thumbnail"}, {ExistsRole, "exists"}};
4144
std::vector<Path> paths;
4245
QString folded_uri_list;
46+
int multiselected = 0;
4347
void check_should_quit();
4448
};

src/path.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Path {
1212
QUrl thumbnail;
1313
bool exists = false;
1414
std::string iconName;
15+
bool multiselect = false;
1516

1617
std::string get_uri() const;
1718
QUrl get_url() const;

src/qml/PathView.qml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ListView {
1717
Behavior on height { NumberAnimation { duration: 300; easing.type: Easing.InOutSine }}
1818
visible: pathView.count > 1
1919
highlighted: true
20-
text: "Drag all " + pathView.count + " items"
20+
text: "Drag all " + (PathModel.multiSelected ? PathModel.multiSelected : pathView.count) + " items"
2121
Button {
2222
id: dragallDummy
2323
visible: false
@@ -93,8 +93,9 @@ ListView {
9393
Rectangle {
9494
id: usedIndicator
9595
anchors { right: parent.right; top: parent.top; bottom: parent.bottom }
96-
width: 8
96+
width: 8 + multiselect * 40
9797
color: used ? Material.primary : exists ? Material.color(Material.Grey) : Material.color(Material.Red)
98+
Behavior on width { NumberAnimation { duration: 200; easing.type: Easing.InOutSine }}
9899
Behavior on color { ColorAnimation { duration: 200; easing.type: Easing.InOutSine }}
99100
}
100101
}
@@ -105,7 +106,13 @@ ListView {
105106
dragUri: uri
106107
hoverEnabled: true
107108
acceptedButtons: Qt.LeftButton | Qt.RightButton
108-
onClicked: PathModel.open(index);
109+
onClicked: (ev) => {
110+
if (ev.modifiers & Qt.ControlModifier) {
111+
PathModel.multiselect(index);
112+
} else {
113+
PathModel.open(index);
114+
}
115+
}
109116
onDragStarted: {
110117
Settings.alwaysOnBottom = true;
111118
pathView.dragActive = true;

0 commit comments

Comments
 (0)