From f32e74c2a78ae2fea6c1f825900287202778bf59 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Tue, 12 Jan 2021 20:34:02 +0100 Subject: Allow extended selection in log list and implement copy. --- src/copyeventfilter.cpp | 6 +++--- src/copyeventfilter.h | 2 +- src/loglist.cpp | 19 ++++++++++++++++--- src/loglist.h | 4 ++++ src/mainwindow.cpp | 8 +------- src/mainwindow.h | 1 - src/mainwindow.ui | 3 +++ 7 files changed, 28 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/copyeventfilter.cpp b/src/copyeventfilter.cpp index 223561b0..bd61e04d 100644 --- a/src/copyeventfilter.cpp +++ b/src/copyeventfilter.cpp @@ -4,8 +4,8 @@ #include #include -CopyEventFilter::CopyEventFilter(QAbstractItemView* view, int role) : - CopyEventFilter(view, [=](auto& index) { return index.data(role).toString(); }) +CopyEventFilter::CopyEventFilter(QAbstractItemView* view, int column, int role) : + CopyEventFilter(view, [=](auto& index) { return index.sibling(index.row(), column).data(role).toString(); }) { } @@ -27,7 +27,7 @@ void CopyEventFilter::copySelection() const QModelIndexList selectedRows = m_view->selectionModel()->selectedRows(); std::sort(selectedRows.begin(), selectedRows.end(), [=](const auto& lidx, const auto& ridx) { return m_view->visualRect(lidx).top() < m_view->visualRect(ridx).top(); - }); + }); QStringList rows; for (auto& idx : selectedRows) { diff --git a/src/copyeventfilter.h b/src/copyeventfilter.h index a9d0a1c1..141ddae1 100644 --- a/src/copyeventfilter.h +++ b/src/copyeventfilter.h @@ -23,7 +23,7 @@ class CopyEventFilter : public QObject public: - CopyEventFilter(QAbstractItemView* view, int role = Qt::DisplayRole); + CopyEventFilter(QAbstractItemView* view, int column = 0, int role = Qt::DisplayRole); CopyEventFilter(QAbstractItemView* view, std::function format); // copy the selection of the view associated with this diff --git a/src/loglist.cpp b/src/loglist.cpp index 8df21111..7f8f05bd 100644 --- a/src/loglist.cpp +++ b/src/loglist.cpp @@ -19,6 +19,7 @@ along with Mod Organizer. If not, see . #include "loglist.h" #include "organizercore.h" +#include "copyeventfilter.h" using namespace MOBase; @@ -45,6 +46,14 @@ void LogModel::add(MOBase::log::Entry e) this, [this, e]{ onEntryAdded(std::move(e)); }, Qt::QueuedConnection); } +QString LogModel::formattedMessage(const QModelIndex& index) const +{ + if (!index.isValid()) { + return ""; + } + return QString::fromStdString(m_entries[index.row()].formattedMessage); +} + void LogModel::clear() { beginResetModel(); @@ -160,7 +169,9 @@ QVariant LogModel::headerData(int, Qt::Orientation, int) const LogList::LogList(QWidget* parent) - : QTreeView(parent), m_core(nullptr) + : QTreeView(parent), + m_core(nullptr), + m_copyFilter(this, [=](auto&& index) { return LogModel::instance().formattedMessage(index); }) { setModel(&LogModel::instance()); @@ -180,6 +191,8 @@ LogList::LogList(QWidget* parent) m_timer.setSingleShot(true); connect(&m_timer, &QTimer::timeout, [&]{ scrollToBottom(); }); + + installEventFilter(&m_copyFilter); } void LogList::onNewEntry() @@ -196,8 +209,7 @@ void LogList::copyToClipboard() { std::string s; - auto* m = static_cast(model()); - for (const auto& e : m->entries()) { + for (const auto& e : LogModel::instance().entries()) { s += e.formattedMessage + "\n"; } @@ -224,6 +236,7 @@ QMenu* LogList::createMenu(QWidget* parent) { auto* menu = new QMenu(parent); + menu->addAction(tr("Copy"), [&]{ m_copyFilter.copySelection(); }); menu->addAction(tr("&Copy all"), [&]{ copyToClipboard(); }); menu->addSeparator(); menu->addAction(tr("C&lear all"), [&]{ clear(); }); diff --git a/src/loglist.h b/src/loglist.h index 0745ed3e..d502f193 100644 --- a/src/loglist.h +++ b/src/loglist.h @@ -22,6 +22,7 @@ along with Mod Organizer. If not, see . #include "shared/appconfig.h" +#include "copyeventfilter.h" #include #include #include @@ -41,6 +42,8 @@ public: const std::deque& entries() const; + QString formattedMessage(const QModelIndex& index) const; + protected: QModelIndex index(int row, int column, const QModelIndex& parent) const override; QModelIndex parent(const QModelIndex &child) const override; @@ -77,6 +80,7 @@ public: private: OrganizerCore* m_core; QTimer m_timer; + CopyEventFilter m_copyFilter; void onContextMenu(const QPoint& pos); void onNewEntry(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index ce98772a..f7ff7b04 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2317,12 +2317,6 @@ void MainWindow::openInstanceFolder() shell::Explore(dataPath); } -void MainWindow::openLogsFolder() -{ - QString logsPath = qApp->property("dataPath").toString() + "/" + QString::fromStdWString(AppConfig::logPath()); - shell::Explore(logsPath); -} - void MainWindow::openInstallFolder() { shell::Explore(qApp->applicationDirPath()); @@ -2400,7 +2394,7 @@ QMenu *MainWindow::openFolderMenu() FolderMenu->addAction(tr("Open MO2 Install folder"), this, SLOT(openInstallFolder())); FolderMenu->addAction(tr("Open MO2 Plugins folder"), this, SLOT(openPluginsFolder())); FolderMenu->addAction(tr("Open MO2 Stylesheets folder"), this, SLOT(openStylesheetsFolder())); - FolderMenu->addAction(tr("Open MO2 Logs folder"), this, SLOT(openLogsFolder())); + FolderMenu->addAction(tr("Open MO2 Logs folder"), [=] { ui->logList->openLogsFolder(); }); return FolderMenu; } diff --git a/src/mainwindow.h b/src/mainwindow.h index 6ead7a77..1f521f38 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -348,7 +348,6 @@ private slots: bool shouldStartTutorial() const; void openInstanceFolder(); - void openLogsFolder(); void openInstallFolder(); void openPluginsFolder(); void openStylesheetsFolder(); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 14cbdda1..a0900542 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -1550,6 +1550,9 @@ p, li { white-space: pre-wrap; } true + + QAbstractItemView::ExtendedSelection + false -- cgit v1.3.1