QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
LogModel.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QChronoTimer>
4#include <QtCore/QRegularExpression>
5#include <QtCore/QSet>
6#include <QtCore/QStringList>
7#include <QtQmlIntegration/QtQmlIntegration>
8#include <chrono>
9#include <deque>
10#include <vector>
11
12#include "LogEntryTableModel.h"
13
15{
16 Q_OBJECT
17 QML_ELEMENT
18 QML_UNCREATABLE("")
19
20 Q_PROPERTY(int totalCount READ totalCount NOTIFY totalCountChanged)
21 Q_PROPERTY(int maxEntries READ maxEntries WRITE setMaxEntries NOTIFY maxEntriesChanged)
22 Q_PROPERTY(QStringList categoriesList READ categoriesList NOTIFY categoriesChanged)
23 Q_PROPERTY(int filterLevel READ filterLevel WRITE setFilterLevel NOTIFY filterLevelChanged)
24 Q_PROPERTY(QString filterCategory READ filterCategory WRITE setFilterCategory NOTIFY filterCategoryChanged)
25 Q_PROPERTY(QString filterText READ filterText WRITE setFilterText NOTIFY filterTextChanged)
26 Q_PROPERTY(bool filterRegex READ filterRegex WRITE setFilterRegex NOTIFY filterRegexChanged)
28
29public:
30 explicit LogModel(QObject* parent = nullptr);
31
32 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
33 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
34 void multiData(const QModelIndex& index, QModelRoleDataSpan roleDataSpan) const override;
35
36 int totalCount() const { return static_cast<int>(_entries.size()); }
37
38 int maxEntries() const { return _maxEntries; }
39
40 void setMaxEntries(int max);
41
42 int filterLevel() const { return _filterLevel; }
43
44 void setFilterLevel(int level);
45
46 QString filterCategory() const { return _filterCategory; }
47
48 void setFilterCategory(const QString& category);
49
50 QString filterText() const { return _filterText; }
51
52 void setFilterText(const QString& text);
53
54 bool filterRegex() const { return _filterRegex; }
55
56 bool filterRegexValid() const { return !_filterRegex || _compiledRegex.isValid(); }
57
58 void setFilterRegex(bool enabled);
59
60 QStringList categoriesList() const;
61 Q_INVOKABLE void setFilterTextDeferred(const QString& text);
62 Q_INVOKABLE void clearFilters();
63 Q_INVOKABLE void clear();
64
65 void enqueue(LogEntry entry);
66
67 QList<LogEntry> allEntriesSnapshot() const { return QList<LogEntry>(_entries.begin(), _entries.end()); }
68
69 QList<LogEntry> filteredEntries() const;
70
71signals:
80
81protected:
82 const LogEntry* entryAt(int row) const override;
83
84private slots:
85 void _flushPending();
86
87private:
88 void _trimExcess();
89 void _rebuildFilteredIndices();
90 bool _passesFilter(const LogEntry& entry) const;
91
92 bool _hasActiveFilter() const;
93 void _recompileRegex();
94 void _appendToFiltered(int first, int last);
95 void _invalidateCategoryCache();
96
97 std::deque<LogEntry> _entries;
98 std::vector<LogEntry> _pendingEntries;
99 std::vector<int> _filteredIndices;
100 QChronoTimer _batchTimer{std::chrono::milliseconds{kBatchFlushMs}};
101 QTimer _filterTextDebounce;
102 QString _pendingFilterText;
103
104 int _maxEntries = 100000;
105 int _filterLevel = LogEntry::Debug;
106 QString _filterCategory;
107 QString _filterText;
108 bool _filterRegex = false;
109 bool _filterBypassed = true; // true when no active filter — avoids identity-map allocation
110 QRegularExpression _compiledRegex;
111
112 QSet<QString> _categoriesSet;
113 mutable QStringList _categoriesCache;
114 mutable bool _categoriesDirty = false;
115
116 static constexpr int kBatchFlushMs = 16;
117 static constexpr int kBatchMaxSize = 200;
118 static constexpr int kFilterDebounceMs = 200;
119};
Base class for table models that display LogEntry data.
int maxEntries() const
Definition LogModel.h:38
bool filterRegex() const
Definition LogModel.h:54
int filterLevel() const
Definition LogModel.h:42
void totalCountChanged()
int totalCount() const
Definition LogModel.h:36
void filterTextChanged()
const LogEntry * entryAt(int row) const override
Return entry at visible row, or nullptr if out of range.
Definition LogModel.cc:73
void enqueue(LogEntry entry)
Definition LogModel.cc:220
void categoriesChanged()
QString filterText() const
Definition LogModel.h:50
void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override
Definition LogModel.cc:51
void setFilterText(const QString &text)
Definition LogModel.cc:119
void setFilterRegex(bool enabled)
Definition LogModel.cc:141
bool filterRegexValid() const
Definition LogModel.h:56
Q_INVOKABLE void clearFilters()
Definition LogModel.cc:155
void filterCategoryChanged()
void setFilterCategory(const QString &category)
Definition LogModel.cc:110
void setMaxEntries(int max)
Definition LogModel.cc:92
QList< LogEntry > allEntriesSnapshot() const
Definition LogModel.h:67
void maxEntriesChanged()
Q_INVOKABLE void clear()
Definition LogModel.cc:355
void filterRegexValidChanged()
QList< LogEntry > filteredEntries() const
Definition LogModel.cc:388
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition LogModel.cc:37
Q_INVOKABLE void setFilterTextDeferred(const QString &text)
Definition LogModel.cc:133
void setFilterLevel(int level)
Definition LogModel.cc:101
void filterRegexChanged()
QStringList categoriesList() const
Definition LogModel.cc:377
QString filterCategory() const
Definition LogModel.h:46
void filterLevelChanged()
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition LogModel.cc:28