1 #include "./downloadview.h"
2 #include "./downloaditemdelegate.h"
3 #include "./helper.h"
4 
5 #include <syncthingconnector/syncthingdir.h>
6 #include <syncthingmodel/syncthingdownloadmodel.h>
7 
8 #include <QClipboard>
9 #include <QGuiApplication>
10 #include <QHeaderView>
11 #include <QMenu>
12 #include <QMouseEvent>
13 
14 using namespace Data;
15 
16 namespace QtGui {
17 
DownloadView(QWidget * parent)18 DownloadView::DownloadView(QWidget *parent)
19     : QTreeView(parent)
20 {
21     header()->setSectionResizeMode(QHeaderView::ResizeToContents);
22     header()->hide();
23     setItemDelegateForColumn(0, new DownloadItemDelegate(this));
24     setContextMenuPolicy(Qt::CustomContextMenu);
25     connect(this, &DownloadView::customContextMenuRequested, this, &DownloadView::showContextMenu);
26 }
27 
mouseReleaseEvent(QMouseEvent * event)28 void DownloadView::mouseReleaseEvent(QMouseEvent *event)
29 {
30     QTreeView::mouseReleaseEvent(event);
31     const auto *const dlModel = qobject_cast<const SyncthingDownloadModel *>(model());
32     if (!dlModel) {
33         return;
34     }
35     const QPoint pos(event->pos());
36     const QModelIndex clickedIndex(indexAt(event->pos()));
37     if (!clickedIndex.isValid() || clickedIndex.column() != 0) {
38         return;
39     }
40     const QRect itemRect(visualRect(clickedIndex));
41     if (pos.x() <= itemRect.right() - 17) {
42         return;
43     }
44     if (clickedIndex.parent().isValid()) {
45         if (pos.y() < itemRect.y() + itemRect.height() / 2) {
46             if (const SyncthingItemDownloadProgress *const progress = dlModel->progressInfo(clickedIndex)) {
47                 emit openItemDir(*progress);
48             }
49         }
50     } else if (const SyncthingDir *const dir = dlModel->dirInfo(clickedIndex)) {
51         emit openDir(*dir);
52     }
53 }
54 
showContextMenu(const QPoint & position)55 void DownloadView::showContextMenu(const QPoint &position)
56 {
57     const auto selectedRow = SelectedRow(this);
58     const auto &selectedIndex = selectedRow.index;
59     if (!selectedRow) {
60         return;
61     }
62     QMenu menu(this);
63     if (selectedIndex.parent().isValid()) {
64         connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
65                     tr("Copy value")),
66             &QAction::triggered, copyToClipboard(model()->data(model()->index(selectedIndex.row(), 1, selectedIndex.parent())).toString()));
67     } else {
68         const auto [dir, progress] = selectedRow.data;
69         connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
70                     tr("Copy label/ID")),
71             &QAction::triggered, copyToClipboard(dir->displayName()));
72         menu.addSeparator();
73         connect(menu.addAction(QIcon::fromTheme(QStringLiteral("folder"), QIcon(QStringLiteral(":/icons/hicolor/scalable/places/folder-open.svg"))),
74                     tr("Open in file browser")),
75             &QAction::triggered, triggerActionForSelectedRow(this, &DownloadView::emitOpenDir));
76     }
77     showViewMenu(position, *this, menu);
78 }
79 
emitOpenDir(QPair<const SyncthingDir *,const SyncthingItemDownloadProgress * > info)80 void DownloadView::emitOpenDir(QPair<const SyncthingDir *, const SyncthingItemDownloadProgress *> info)
81 {
82     if (info.second) {
83         emit openItemDir(*info.second);
84     } else {
85         emit openDir(*info.first);
86     }
87 }
88 
89 } // namespace QtGui
90