syncthingtray/tray/gui/devview.cpp

113 lines
3.8 KiB
C++
Raw Normal View History

2016-08-25 00:45:32 +02:00
#include "./devview.h"
2016-08-26 16:43:53 +02:00
#include "./devbuttonsitemdelegate.h"
2016-08-25 00:45:32 +02:00
#include "../../model/syncthingdevicemodel.h"
2016-09-21 21:09:12 +02:00
2017-05-01 03:34:43 +02:00
#include <QClipboard>
2016-08-25 00:45:32 +02:00
#include <QGuiApplication>
2017-05-01 03:34:43 +02:00
#include <QHeaderView>
#include <QMenu>
#include <QMouseEvent>
2016-08-25 00:45:32 +02:00
2016-09-21 21:09:12 +02:00
using namespace Data;
2016-08-25 00:45:32 +02:00
namespace QtGui {
2017-05-01 03:34:43 +02:00
DevView::DevView(QWidget *parent)
: QTreeView(parent)
2016-08-25 00:45:32 +02:00
{
header()->setSectionResizeMode(QHeaderView::ResizeToContents);
header()->hide();
2016-08-26 16:43:53 +02:00
setItemDelegateForColumn(1, new DevButtonsItemDelegate(this));
2016-08-25 00:45:32 +02:00
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &DevView::customContextMenuRequested, this, &DevView::showContextMenu);
}
2016-08-26 16:43:53 +02:00
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);
2016-08-26 16:43:53 +02:00
}
}
void DevView::showContextMenu(const QPoint &position)
2016-08-25 00:45:32 +02:00
{
if (!selectionModel() || selectionModel()->selectedRows(0).size() != 1) {
return;
}
QMenu menu(this);
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"))),
2017-05-01 03:34:43 +02:00
tr("Copy value")),
&QAction::triggered, this, &DevView::copySelectedItem);
} else {
connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
2017-05-01 03:34:43 +02:00
tr("Copy name")),
&QAction::triggered, this, &DevView::copySelectedItem);
connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
2017-05-01 03:34:43 +02:00
tr("Copy ID")),
&QAction::triggered, this, &DevView::copySelectedItemId);
2016-08-25 00:45:32 +02:00
}
// map the coordinates to top-level widget if it is a QMenu (not sure why this is required)
const auto *const topLevelWidget = this->topLevelWidget();
if (qobject_cast<const QMenu *>(topLevelWidget)) {
menu.exec(topLevelWidget->mapToGlobal(position));
} else {
menu.exec(viewport()->mapToGlobal(position));
}
2016-08-25 00:45:32 +02:00
}
void DevView::copySelectedItem()
{
if (!selectionModel() || selectionModel()->selectedRows(0).size() != 1) {
return;
}
const QModelIndex selectedIndex = selectionModel()->selectedRows(0).at(0);
QString text;
if (selectedIndex.parent().isValid()) {
// dev attribute
text = model()->data(model()->index(selectedIndex.row(), 1, selectedIndex.parent())).toString();
} else {
// dev name/id
text = model()->data(selectedIndex).toString();
}
if (!text.isEmpty()) {
QGuiApplication::clipboard()->setText(text);
2016-08-25 00:45:32 +02:00
}
}
2016-08-30 20:01:07 +02:00
void DevView::copySelectedItemId()
{
if (!selectionModel() || selectionModel()->selectedRows(0).size() != 1) {
return;
}
const QModelIndex selectedIndex = selectionModel()->selectedRows(0).at(0);
QString text;
if (selectedIndex.parent().isValid()) {
// dev attribute: should be handled by copySelectedItemId()
} else {
// dev name/id
text = model()->data(model()->index(0, 1, selectedIndex)).toString();
}
if (!text.isEmpty()) {
QGuiApplication::clipboard()->setText(text);
2016-08-30 20:01:07 +02:00
}
}
} // namespace QtGui