File: dirview.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (117 lines) | stat: -rw-r--r-- 5,382 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "./dirview.h"
#include "./dirbuttonsitemdelegate.h"

#include <syncthingconnector/syncthingconnection.h>
#include <syncthingmodel/syncthingdirectorymodel.h>
#include <syncthingmodel/syncthingsortfiltermodel.h>
#include <syncthingwidgets/misc/direrrorsdialog.h>
#include <syncthingwidgets/settings/settings.h>

#include <QClipboard>
#include <QGuiApplication>
#include <QHeaderView>
#include <QMenu>
#include <QMouseEvent>

using namespace Data;

namespace QtGui {

DirView::DirView(QWidget *parent)
    : BasicTreeView(parent)
{
    header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    header()->hide();
    setItemDelegate(new DirButtonsItemDelegate(this));
    connect(this, &BasicTreeView::customContextMenuRequested, this, &DirView::showContextMenu);
}

void DirView::mouseReleaseEvent(QMouseEvent *event)
{
    BasicTreeView::mouseReleaseEvent(event);
    if (event->button() != Qt::LeftButton) {
        return;
    }

    const auto pos = event->pos();
    const auto clickedRow = ClickedRow(this, pos);
    if (!clickedRow) {
        return;
    }

    if (!clickedRow.index.parent().isValid()) {
        // open/scan dir buttons
        const QRect itemRect = visualRect(clickedRow.proxyIndex);
        if (pos.x() <= itemRect.right() - (listItemIconsSize(2) + listItemSpacing / 2)) {
            return;
        }
        if (pos.x() < itemRect.right() - (listItemIconsSize(1) + listItemIconSpacing / 2)) {
            if (!clickedRow.data->paused) {
                emit scanDir(*clickedRow.data);
            }
        } else if (pos.x() < itemRect.right() - (listItemIconsSize(0) + listItemIconSpacing / 2)) {
            emit pauseResumeDir(*clickedRow.data);
        } else {
            emit openDir(*clickedRow.data);
        }
    } else if (clickedRow.index.row() == 10 && clickedRow.data->pullErrorCount) {
        auto &connection(*clickedRow.model->connection());
        connection.requestDirPullErrors(clickedRow.data->id);

        auto *const textViewDlg = new DirectoryErrorsDialog(connection, *clickedRow.data);
        textViewDlg->setAttribute(Qt::WA_DeleteOnClose);
        textViewDlg->show();
    }
}

void DirView::showContextMenu(const QPoint &position)
{
    const auto selectedRow = SelectedRow(this);
    const auto &selectedIndex = selectedRow.index;
    if (!selectedRow) {
        return;
    }
    QMenu menu(this);
    if (selectedIndex.parent().isValid()) {
        connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
                    tr("Copy value")),
            &QAction::triggered,
            copyToClipboard(selectedRow.model->data(selectedRow.model->index(selectedIndex.row(), 1, selectedIndex.parent())).toString()));
    } else {
        const auto *const dir = selectedRow.data;
        connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
                    tr("Copy label/ID")),
            &QAction::triggered, copyToClipboard(dir->displayName()));
        connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
                    tr("Copy path")),
            &QAction::triggered, copyToClipboard(dir->path));
        menu.addSeparator();
        connect(menu.addAction(
                    QIcon::fromTheme(QStringLiteral("view-refresh"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/view-refresh.svg"))),
                    tr("Rescan")),
            &QAction::triggered, triggerActionForSelectedRow(this, &DirView::scanDir));
        if (dir->paused) {
            connect(menu.addAction(QIcon::fromTheme(QStringLiteral("media-playback-start"),
                                       QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/media-playback-start.svg"))),
                        tr("Resume")),
                &QAction::triggered, triggerActionForSelectedRow(this, &DirView::pauseResumeDir));
        } else {
            connect(menu.addAction(QIcon::fromTheme(QStringLiteral("media-playback-pause"),
                                       QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/media-playback-pause.svg"))),
                        tr("Pause")),
                &QAction::triggered, triggerActionForSelectedRow(this, &DirView::pauseResumeDir));
        }
        connect(menu.addAction(QIcon::fromTheme(QStringLiteral("folder"), QIcon(QStringLiteral(":/icons/hicolor/scalable/places/folder-open.svg"))),
                    tr("Open in file browser")),
            &QAction::triggered, triggerActionForSelectedRow(this, &DirView::openDir));
        connect(menu.addAction(QIcon::fromTheme(QStringLiteral("document-open-remote"),
                                   QIcon(QStringLiteral(":/icons/hicolor/scalable/places/document-open-remote.svg"))),
                    tr("Browse remote files")),
            &QAction::triggered, triggerActionForSelectedRow(this, &DirView::browseRemoteFiles));
        connect(menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), tr("Show/edit ignore patterns")), &QAction::triggered,
            triggerActionForSelectedRow(this, &DirView::showIgnorePatterns));
    }
    showViewMenu(position, *this, menu);
}

} // namespace QtGui