From d1b4dec8ad1635738ada3dfbde5907e7f0df3448 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Tue, 16 Jul 2019 05:58:51 -0400 Subject: moved setup to new LogList class --- src/loglist.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 7 deletions(-) (limited to 'src/loglist.cpp') diff --git a/src/loglist.cpp b/src/loglist.cpp index cb927272..9e876d37 100644 --- a/src/loglist.cpp +++ b/src/loglist.cpp @@ -52,21 +52,26 @@ void LogModel::add(MOBase::log::Entry e) emit entryAdded(std::move(e)); } +const std::deque& LogModel::entries() const +{ + return m_entries; +} + void LogModel::onEntryAdded(MOBase::log::Entry e) { bool full = false; - if (m_messages.size() > MaxLines) { - m_messages.pop_front(); + if (m_entries.size() > MaxLines) { + m_entries.pop_front(); full = true; } - const int row = static_cast(m_messages.size()); + const int row = static_cast(m_entries.size()); if (!full) { beginInsertRows(QModelIndex(), row, row + 1); } - m_messages.emplace_back(std::move(e)); + m_entries.emplace_back(std::move(e)); if (!full) { endInsertRows(); @@ -92,7 +97,7 @@ int LogModel::rowCount(const QModelIndex& parent) const if (parent.isValid()) return 0; else - return static_cast(m_messages.size()); + return static_cast(m_entries.size()); } int LogModel::columnCount(const QModelIndex&) const @@ -105,11 +110,11 @@ QVariant LogModel::data(const QModelIndex& index, int role) const using namespace std::chrono; const auto row = static_cast(index.row()); - if (row >= m_messages.size()) { + if (row >= m_entries.size()) { return {}; } - const auto& e = m_messages[row]; + const auto& e = m_entries[row]; if (role == Qt::DisplayRole) { if (index.column() == 1) { @@ -153,6 +158,48 @@ QVariant LogModel::headerData(int, Qt::Orientation, int) const return {}; } + +LogList::LogList(QWidget* parent) + : QTreeView(parent) +{ + setModel(&LogModel::instance()); + + const int timestampWidth = QFontMetrics(font()).width("00:00:00.000"); + + header()->setMinimumSectionSize(0); + header()->resizeSection(0, 20); + header()->resizeSection(1, timestampWidth + 8); + + setAutoScroll(true); + scrollToBottom(); + + connect( + model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), + this, SLOT(scrollToBottom())); + + connect( + model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), + this, SLOT(scrollToBottom())); +} + +void LogList::copyToClipboard() +{ + std::string s; + + auto* m = static_cast(model()); + for (const auto& e : m->entries()) { + s += e.formattedMessage + "\n"; + } + + if (!s.empty()) { + // last newline + s.pop_back(); + } + + QApplication::clipboard()->setText(QString::fromStdString(s)); +} + + void vlog(const char *format, ...) { va_list argList; -- cgit v1.3.1