syncthingtray/tray/gui/dirview.cpp

119 lines
4.5 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/textviewdialog.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);
if (const SyncthingDirectoryModel *dirModel = qobject_cast<const SyncthingDirectoryModel *>(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() == 1) {
if (const SyncthingDir *dir = dirModel->dirInfo(clickedIndex)) {
if (!clickedIndex.parent().isValid()) {
2016-10-04 23:42:17 +02:00
// open/scan dir buttons
const QRect itemRect(visualRect(clickedIndex));
2017-05-01 03:34:43 +02:00
if (pos.x() > itemRect.right() - 58) {
if (pos.x() < itemRect.right() - 34) {
if (!dir->paused) {
emit scanDir(*dir);
}
2017-05-01 03:34:43 +02:00
} else if (pos.x() < itemRect.right() - 17) {
emit pauseResumeDir(*dir);
2017-02-23 15:49:58 +01:00
} else {
emit openDir(*dir);
2016-10-04 23:42:17 +02:00
}
2016-09-21 21:09:12 +02:00
}
2017-08-31 19:49:23 +02:00
} else if (clickedIndex.row() == 9 && !dir->itemErrors.empty()) {
auto *const textViewDlg = TextViewDialog::forDirectoryErrors(*dir);
textViewDlg->setAttribute(Qt::WA_DeleteOnClose);
2016-10-04 23:42:17 +02:00
textViewDlg->show();
2016-09-21 21:09:12 +02:00
}
2016-08-25 00:45:32 +02:00
}
}
}
}
void DirView::showContextMenu()
{
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
}
2016-08-25 00:45:32 +02:00
menu.exec(QCursor::pos());
}
}
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