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
|
#include "./downloadview.h"
#include "./downloaditemdelegate.h"
#include <syncthingconnector/syncthingdir.h>
#include <syncthingmodel/syncthingdownloadmodel.h>
#include <QClipboard>
#include <QGuiApplication>
#include <QHeaderView>
#include <QMenu>
#include <QMouseEvent>
using namespace Data;
namespace QtGui {
DownloadView::DownloadView(QWidget *parent)
: BasicTreeView(parent)
{
header()->setSectionResizeMode(QHeaderView::ResizeToContents);
header()->hide();
setItemDelegateForColumn(0, new DownloadItemDelegate(this));
connect(this, &BasicTreeView::customContextMenuRequested, this, &DownloadView::showContextMenu);
}
void DownloadView::mouseReleaseEvent(QMouseEvent *event)
{
BasicTreeView::mouseReleaseEvent(event);
if (event->button() != Qt::LeftButton) {
return;
}
const auto *const dlModel = qobject_cast<const SyncthingDownloadModel *>(model());
if (!dlModel) {
return;
}
const auto pos = event->pos();
const auto clickedIndex(indexAt(pos));
if (!clickedIndex.isValid() || clickedIndex.column() != 0) {
return;
}
const QRect itemRect(visualRect(clickedIndex));
if (pos.x() <= itemRect.right() - 17) {
return;
}
if (clickedIndex.parent().isValid()) {
if (pos.y() < itemRect.y() + itemRect.height() / 2) {
if (const SyncthingItemDownloadProgress *const progress = dlModel->progressInfo(clickedIndex)) {
emit openItemDir(*progress);
}
}
} else if (const SyncthingDir *const dir = dlModel->dirInfo(clickedIndex)) {
emit openDir(*dir);
}
}
void DownloadView::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(model()->data(model()->index(selectedIndex.row(), 1, selectedIndex.parent())).toString()));
} else {
const auto [dir, progress] = 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()));
menu.addSeparator();
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, &DownloadView::emitOpenDir));
}
showViewMenu(position, *this, menu);
}
void DownloadView::emitOpenDir(QPair<const SyncthingDir *, const SyncthingItemDownloadProgress *> info)
{
if (info.second) {
emit openItemDir(*info.second);
} else {
emit openDir(*info.first);
}
}
} // namespace QtGui
|