syncthingtray/tray/gui/downloadview.cpp

87 lines
3.1 KiB
C++
Raw Normal View History

2016-09-21 21:09:12 +02:00
#include "./downloadview.h"
#include "./downloaditemdelegate.h"
#include "../../model/syncthingdownloadmodel.h"
2016-09-21 21:09:12 +02:00
2017-05-01 03:34:43 +02:00
#include <QClipboard>
2016-09-21 21:09:12 +02:00
#include <QCursor>
#include <QGuiApplication>
2017-05-01 03:34:43 +02:00
#include <QHeaderView>
#include <QMenu>
#include <QMouseEvent>
2016-09-21 21:09:12 +02:00
using namespace Data;
namespace QtGui {
2017-05-01 03:34:43 +02:00
DownloadView::DownloadView(QWidget *parent)
: QTreeView(parent)
2016-09-21 21:09:12 +02:00
{
header()->setSectionResizeMode(QHeaderView::ResizeToContents);
header()->hide();
2016-09-28 00:06:21 +02:00
setItemDelegateForColumn(0, new DownloadItemDelegate(this));
2016-09-21 21:09:12 +02:00
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &DownloadView::customContextMenuRequested, this, &DownloadView::showContextMenu);
}
void DownloadView::mouseReleaseEvent(QMouseEvent *event)
{
QTreeView::mouseReleaseEvent(event);
2017-05-01 03:34:43 +02:00
if (const SyncthingDownloadModel *dlModel = qobject_cast<SyncthingDownloadModel *>(model())) {
2016-09-21 21:09:12 +02:00
const QPoint pos(event->pos());
const QModelIndex clickedIndex(indexAt(event->pos()));
2017-05-01 03:34:43 +02:00
if (clickedIndex.isValid() && clickedIndex.column() == 0) {
2016-09-21 21:09:12 +02:00
const QRect itemRect(visualRect(clickedIndex));
2017-05-01 03:34:43 +02:00
if (pos.x() > itemRect.right() - 17) {
if (clickedIndex.parent().isValid()) {
if (pos.y() < itemRect.y() + itemRect.height() / 2) {
if (const SyncthingItemDownloadProgress *progress = dlModel->progressInfo(clickedIndex)) {
2016-09-28 00:06:21 +02:00
emit openItemDir(*progress);
}
2016-09-21 21:09:12 +02:00
}
2017-05-01 03:34:43 +02:00
} else if (const SyncthingDir *dir = dlModel->dirInfo(clickedIndex)) {
2016-09-21 21:09:12 +02:00
emit openDir(*dir);
}
}
}
}
}
void DownloadView::showContextMenu()
{
2017-05-01 03:34:43 +02:00
if (selectionModel() && selectionModel()->selectedRows(0).size() == 1) {
2016-09-21 21:09:12 +02:00
QMenu menu;
2017-05-01 03:34:43 +02:00
if (selectionModel()->selectedRows(0).at(0).parent().isValid()) {
connect(
menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
tr("Copy value")),
&QAction::triggered, this, &DownloadView::copySelectedItem);
2016-09-21 21:09:12 +02:00
} else {
2017-05-01 03:34:43 +02:00
connect(
menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
tr("Copy label/ID")),
&QAction::triggered, this, &DownloadView::copySelectedItem);
2016-09-21 21:09:12 +02:00
}
menu.exec(QCursor::pos());
}
}
void DownloadView::copySelectedItem()
{
2017-05-01 03:34:43 +02:00
if (selectionModel() && selectionModel()->selectedRows(0).size() == 1) {
2016-09-21 21:09:12 +02:00
const QModelIndex selectedIndex = selectionModel()->selectedRows(0).at(0);
QString text;
2017-05-01 03:34:43 +02:00
if (selectedIndex.parent().isValid()) {
2016-09-21 21:09:12 +02:00
// dev attribute
text = model()->data(model()->index(selectedIndex.row(), 1, selectedIndex.parent())).toString();
} else {
// dev label/id
text = model()->data(selectedIndex).toString();
}
2017-05-01 03:34:43 +02:00
if (!text.isEmpty()) {
2016-09-21 21:09:12 +02:00
QGuiApplication::clipboard()->setText(text);
}
}
}
} // namespace QtGui