Skip to content

Commit

Permalink
Implement multiselect mode
Browse files Browse the repository at this point in the history
This can be triggered with the common Ctrl+Click keybinding.
  • Loading branch information
vimpostor committed Jul 27, 2024
1 parent 9a75663 commit 28426af
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/Models/path_model.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "path_model.hpp"

#include <ranges>

#include "backend.hpp"
#include "path_registry.hpp"
#include "settings.hpp"
Expand All @@ -25,6 +27,8 @@ QVariant PathModel::data(const QModelIndex &index, int role) const {
return QString::fromStdString(p.pretty_print());
case UsedRole:
return p.used;
case MultiselectRole:
return p.multiselect;
case IconRole:
return QString::fromStdString(p.iconName);
case ThumbnailRole:
Expand Down Expand Up @@ -52,8 +56,17 @@ void PathModel::taint_all_used() {
check_should_quit();
}

void PathModel::multiselect(int i) {
paths[i].multiselect = !paths[i].multiselect;
multiselected += (2 * paths[i].multiselect) - 1;
emit dataChanged(index(i, 0), index(i, 0));
refresh_folded_paths();
}

void PathModel::refresh_folded_paths() {
folded_uri_list = std::accumulate(paths.cbegin(), paths.cend(), QString(), [&](QString s, const auto p) { return s.append(QString::fromStdString(p.get_uri()) + "\n"); });
// only add multiselected items in multiselect mode
auto v = paths | std::views::filter([&](const auto &i) { return !multiselected || i.multiselect; });
folded_uri_list = std::accumulate(v.cbegin(), v.cend(), QString(), [](QString s, const auto p) { return s.append(QString::fromStdString(p.get_uri()) + "\n"); });
emit foldedUriListChanged(folded_uri_list);
}

Expand Down
8 changes: 6 additions & 2 deletions src/Models/path_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ class PathModel : public QAbstractListModel {
QML_SINGLETON

Q_PROPERTY(QString foldedUriList MEMBER folded_uri_list NOTIFY foldedUriListChanged)
Q_PROPERTY(int multiSelected MEMBER multiselected NOTIFY foldedUriListChanged)
public:
PathModel(QObject *parent = nullptr);
explicit PathModel(QObject *parent = nullptr);

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

Q_INVOKABLE void taint_used(int i);
Q_INVOKABLE void taint_all_used();
Q_INVOKABLE void multiselect(int i);
Q_INVOKABLE void refresh_folded_paths();
Q_INVOKABLE void open(int i) const;
Q_INVOKABLE void finish_init();
Expand All @@ -33,12 +35,14 @@ class PathModel : public QAbstractListModel {
UriRole,
PrettyRole,
UsedRole,
MultiselectRole,
IconRole,
ThumbnailRole,
ExistsRole,
};
QHash<int, QByteArray> role_names {{PathRole, "path"}, {UriRole, "uri"}, {PrettyRole, "pretty"}, {UsedRole, "used"}, {IconRole, "iconName"}, {ThumbnailRole, "thumbnail"}, {ExistsRole, "exists"}};
QHash<int, QByteArray> role_names {{PathRole, "path"}, {UriRole, "uri"}, {PrettyRole, "pretty"}, {UsedRole, "used"}, {MultiselectRole, "multiselect"}, {IconRole, "iconName"}, {ThumbnailRole, "thumbnail"}, {ExistsRole, "exists"}};
std::vector<Path> paths;
QString folded_uri_list;
int multiselected = 0;
void check_should_quit();
};
1 change: 1 addition & 0 deletions src/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Path {
QUrl thumbnail;
bool exists = false;
std::string iconName;
bool multiselect = false;

std::string get_uri() const;
QUrl get_url() const;
Expand Down
13 changes: 10 additions & 3 deletions src/qml/PathView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ListView {
Behavior on height { NumberAnimation { duration: 300; easing.type: Easing.InOutSine }}
visible: pathView.count > 1
highlighted: true
text: "Drag all " + pathView.count + " items"
text: "Drag all " + (PathModel.multiSelected ? PathModel.multiSelected : pathView.count) + " items"
Button {
id: dragallDummy
visible: false
Expand Down Expand Up @@ -93,8 +93,9 @@ ListView {
Rectangle {
id: usedIndicator
anchors { right: parent.right; top: parent.top; bottom: parent.bottom }
width: 8
width: 8 + multiselect * 40
color: used ? Material.primary : exists ? Material.color(Material.Grey) : Material.color(Material.Red)
Behavior on width { NumberAnimation { duration: 200; easing.type: Easing.InOutSine }}
Behavior on color { ColorAnimation { duration: 200; easing.type: Easing.InOutSine }}
}
}
Expand All @@ -105,7 +106,13 @@ ListView {
dragUri: uri
hoverEnabled: true
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: PathModel.open(index);
onClicked: (ev) => {
if (ev.modifiers & Qt.ControlModifier) {
PathModel.multiselect(index);
} else {
PathModel.open(index);
}
}
onDragStarted: {
Settings.alwaysOnBottom = true;
pathView.dragActive = true;
Expand Down

0 comments on commit 28426af

Please sign in to comment.