diff options
Diffstat (limited to 'src/fsview.cpp')
-rw-r--r-- | src/fsview.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/fsview.cpp b/src/fsview.cpp new file mode 100644 index 0000000..3e9495e --- /dev/null +++ b/src/fsview.cpp @@ -0,0 +1,50 @@ +/* ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2023 aqua <aqua@iserlohn-fortress.net> + * ============================================================ */ + +#include "fsview.hpp" +#include <QFileSystemModel> +#include <QMenu> + +FSView::FSView(QWidget *parent) : QTreeView(parent), model{new QFileSystemModel} +{ + setModel(model); + setRootPath(QDir::homePath()); + + connect(this, &QWidget::customContextMenuRequested, this, &FSView::contextMenu); + setContextMenuPolicy(Qt::CustomContextMenu); +} + +FSView::~FSView() { delete model; } + +void FSView::setRootPath(const QString &path) +{ + model->setRootPath(path); + setRootIndex(model->index(path)); +} + +void FSView::contextMenu(const QPoint &pos) +{ + const auto idx = indexAt(pos); + if (idx.isValid()) { + QMenu menu; + + if (model->isDir(idx)) { + menu.addAction(tr("Set as root"), this, [this, idx]() { + const auto path = model->filePath(idx); + qDebug() << "set root path" << path; + model->setRootPath(path); + setRootIndex(idx); + }); + } + else { + menu.addAction(tr("Add to Playlist"), this, [this, idx]() { + const auto url = QUrl::fromLocalFile(model->filePath(idx)); + emit addLocalFile(url); + }); + } + + menu.exec(mapToGlobal(pos)); + } +} |