* Support triggering actions via the context menu in the regular tray like it is already possible in the Plasmoid * Support copying via the context menu in the Plasmoid like it is already possible in the regular tray * Reduce repetition of coding patterns using templates
88 lines
3.4 KiB
C++
88 lines
3.4 KiB
C++
#include "./devview.h"
|
|
#include "./devbuttonsitemdelegate.h"
|
|
#include "./helper.h"
|
|
|
|
#include "../../connector/syncthingdev.h"
|
|
#include "../../model/syncthingdevicemodel.h"
|
|
|
|
#include <QClipboard>
|
|
#include <QGuiApplication>
|
|
#include <QHeaderView>
|
|
#include <QMenu>
|
|
#include <QMouseEvent>
|
|
|
|
using namespace Data;
|
|
|
|
namespace QtGui {
|
|
|
|
DevView::DevView(QWidget *parent)
|
|
: QTreeView(parent)
|
|
{
|
|
header()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
|
header()->hide();
|
|
setItemDelegateForColumn(1, new DevButtonsItemDelegate(this));
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
connect(this, &DevView::customContextMenuRequested, this, &DevView::showContextMenu);
|
|
}
|
|
|
|
void DevView::mouseReleaseEvent(QMouseEvent *event)
|
|
{
|
|
QTreeView::mouseReleaseEvent(event);
|
|
const auto *const devModel = qobject_cast<const SyncthingDeviceModel *>(model());
|
|
if (!devModel) {
|
|
return;
|
|
}
|
|
const QPoint pos(event->pos());
|
|
const QModelIndex clickedIndex(indexAt(event->pos()));
|
|
if (!clickedIndex.isValid() || clickedIndex.column() != 1 || clickedIndex.parent().isValid()) {
|
|
return;
|
|
}
|
|
const SyncthingDev *const devInfo = devModel->devInfo(clickedIndex);
|
|
if (!devInfo) {
|
|
return;
|
|
}
|
|
const QRect itemRect(visualRect(clickedIndex));
|
|
if (pos.x() > itemRect.right() - 17) {
|
|
emit pauseResumeDev(*devInfo);
|
|
}
|
|
}
|
|
|
|
void DevView::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 dev = selectedRow.data;
|
|
connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
|
|
tr("Copy name")),
|
|
&QAction::triggered, copyToClipboard(dev->displayName()));
|
|
connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
|
|
tr("Copy ID")),
|
|
&QAction::triggered, copyToClipboard(dev->id));
|
|
menu.addSeparator();
|
|
if (dev->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, &DevView::pauseResumeDev));
|
|
} 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, &DevView::pauseResumeDev));
|
|
}
|
|
}
|
|
showViewMenu(position, *this, menu);
|
|
}
|
|
|
|
} // namespace QtGui
|