syncthingtray/tray/gui/dirview.cpp

131 lines
4.4 KiB
C++
Raw Normal View History

2016-08-25 00:45:32 +02:00
#include "./dirview.h"
#include "./dirbuttonsitemdelegate.h"
2016-10-04 23:42:17 +02:00
#include "../../connector/syncthingconnection.h"
2017-05-01 03:34:43 +02:00
#include "../../model/syncthingdirectorymodel.h"
#include "../../widgets/misc/direrrorsdialog.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 <QCursor>
#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
DirView::DirView(QWidget *parent)
: QTreeView(parent)
2016-08-25 00:45:32 +02:00
{
header()->setSectionResizeMode(QHeaderView::ResizeToContents);
header()->hide();
setItemDelegateForColumn(1, new DirButtonsItemDelegate(this));
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &DirView::customContextMenuRequested, this, &DirView::showContextMenu);
}
void DirView::mouseReleaseEvent(QMouseEvent *event)
{
QTreeView::mouseReleaseEvent(event);
// get SyncthingDir object
auto *const dirModel = qobject_cast<SyncthingDirectoryModel *>(model());
if (!dirModel) {
return;
}
const QPoint pos(event->pos());
const QModelIndex clickedIndex(indexAt(event->pos()));
if (!clickedIndex.isValid() || clickedIndex.column() != 1) {
return;
}
const auto *const dir = dirModel->dirInfo(clickedIndex);
if (!dir) {
return;
}
if (!clickedIndex.parent().isValid()) {
// open/scan dir buttons
const QRect itemRect(visualRect(clickedIndex));
if (pos.x() <= itemRect.right() - 58) {
return;
}
if (pos.x() < itemRect.right() - 34) {
if (!dir->paused) {
emit scanDir(*dir);
2016-08-25 00:45:32 +02:00
}
} else if (pos.x() < itemRect.right() - 17) {
emit pauseResumeDir(*dir);
} else {
emit openDir(*dir);
2016-08-25 00:45:32 +02:00
}
} else if (clickedIndex.row() == 9 && dir->pullErrorCount) {
auto &connection(*dirModel->connection());
connection.requestDirPullErrors(dir->id);
auto *const textViewDlg = new DirectoryErrorsDialog(connection, *dir);
textViewDlg->setAttribute(Qt::WA_DeleteOnClose);
textViewDlg->show();
2016-08-25 00:45:32 +02:00
}
}
void DirView::showContextMenu(const QPoint &position)
2016-08-25 00:45:32 +02:00
{
2017-05-01 03:34:43 +02:00
if (selectionModel() && selectionModel()->selectedRows(0).size() == 1) {
2016-08-25 00:45:32 +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, &DirView::copySelectedItem);
2016-08-30 20:01:07 +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, &DirView::copySelectedItem);
connect(
menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/edit-copy.svg"))),
tr("Copy path")),
&QAction::triggered, this, &DirView::copySelectedItemPath);
2016-08-30 20:01:07 +02:00
}
menu.exec(mapToGlobal(position));
2016-08-25 00:45:32 +02:00
}
}
void DirView::copySelectedItem()
{
2017-05-01 03:34:43 +02:00
if (selectionModel() && selectionModel()->selectedRows(0).size() == 1) {
2016-08-25 00:45:32 +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-08-25 00:45:32 +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-08-25 00:45:32 +02:00
QGuiApplication::clipboard()->setText(text);
}
}
}
2016-08-30 20:01:07 +02:00
void DirView::copySelectedItemPath()
{
2017-05-01 03:34:43 +02:00
if (selectionModel() && selectionModel()->selectedRows(0).size() == 1) {
2016-08-30 20:01:07 +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-08-30 20:01:07 +02:00
// dev attribute: should be handled by copySelectedItem() only
} else {
// dev path
text = model()->data(model()->index(1, 1, selectedIndex)).toString();
}
2017-05-01 03:34:43 +02:00
if (!text.isEmpty()) {
2016-08-30 20:01:07 +02:00
QGuiApplication::clipboard()->setText(text);
}
}
}
} // namespace QtGui