summaryrefslogtreecommitdiff
path: root/src/fsview.cpp
blob: 3e9495ea263375610e62473785706e2a4884a539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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));
  }
}