syncthingtray/tray/gui/devview.cpp

94 lines
3.4 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
2016-08-25 00:45:32 +02:00
#include <QHeaderView>
#include <QMouseEvent>
#include <QMenu>
#include <QCursor>
#include <QGuiApplication>
#include <QClipboard>
2016-09-21 21:09:12 +02:00
using namespace Data;
2016-08-25 00:45:32 +02:00
namespace QtGui {
DevView::DevView(QWidget *parent) :
QTreeView(parent)
{
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);
2016-09-21 21:09:12 +02:00
if(const auto *devModel = qobject_cast<SyncthingDeviceModel *>(model())) {
const QPoint pos(event->pos());
const QModelIndex clickedIndex(indexAt(event->pos()));
if(clickedIndex.isValid() && clickedIndex.column() == 1 && !clickedIndex.parent().isValid()) {
if(const SyncthingDev *devInfo = devModel->devInfo(clickedIndex)) {
const QRect itemRect(visualRect(clickedIndex));
if(pos.x() > itemRect.right() - 17) {
emit pauseResumeDev(*devInfo);
}
}
2016-08-26 16:43:53 +02:00
}
}
}
2016-08-25 00:45:32 +02:00
void DevView::showContextMenu()
{
if(selectionModel() && selectionModel()->selectedRows(0).size() == 1) {
QMenu menu;
2016-08-30 20:01:07 +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, &DevView::copySelectedItem);
} else {
connect(menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))), 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"))), tr("Copy ID")), &QAction::triggered, this, &DevView::copySelectedItemId);
}
2016-08-25 00:45:32 +02:00
menu.exec(QCursor::pos());
}
}
void DevView::copySelectedItem()
{
if(selectionModel() && selectionModel()->selectedRows(0).size() == 1) {
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-30 20:01:07 +02:00
void DevView::copySelectedItemId()
{
if(selectionModel() && selectionModel()->selectedRows(0).size() == 1) {
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-25 00:45:32 +02:00
}