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)
27 Q_PROPERTY(bool filterRegexValid READ filterRegexValid NOTIFY filterRegexValidChanged)
28
29public:
30 explicit LogModel(QObject* parent = nullptr);
31
32 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
33
34 int totalCount() const { return static_cast<int>(_entries.size()); }
35
36 int maxEntries() const { return _maxEntries; }
37
38 void setMaxEntries(int max);
39
40 int filterLevel() const { return _filterLevel; }
41
42 void setFilterLevel(int level);
43
44 QString filterCategory() const { return _filterCategory; }
45
46 void setFilterCategory(const QString& category);
47
48 QString filterText() const { return _filterText; }
49
50 void setFilterText(const QString& text);
51
52 bool filterRegex() const { return _filterRegex; }
53
54 bool filterRegexValid() const { return !_filterRegex || _compiledRegex.isValid(); }
55
56 void setFilterRegex(bool enabled);
57
58 QStringList categoriesList() const;
59 Q_INVOKABLE void setFilterTextDeferred(const QString& text);
60 Q_INVOKABLE void clearFilters();
61 Q_INVOKABLE void clear();
62
63 void enqueue(LogEntry entry);
64
65 QList<LogEntry> allEntriesSnapshot() const { return QList<LogEntry>(_entries.begin(), _entries.end()); }
66
67 QList<LogEntry> filteredEntries() const;
68
69signals:
78
79protected:
80 const LogEntry* entryAt(int row) const override;
81
82private slots:
83 void _flushPending();
84
85private:
86 void _trimExcess();
87 void _rebuildFilteredIndices();
88 bool _passesFilter(const LogEntry& entry) const;
89 bool _hasActiveFilter() const;
90 void _recompileRegex();
91 void _appendToFiltered(int first, int last);
92 void _invalidateCategoryCache();
93
94 std::deque<LogEntry> _entries;
95 std::vector<LogEntry> _pendingEntries;
96 std::vector<int> _filteredIndices;
97 QChronoTimer _batchTimer{std::chrono::milliseconds{kBatchFlushMs}};
98 QTimer _filterTextDebounce;
99 QString _pendingFilterText;
100
101 int _maxEntries = 100000;
102 int _filterLevel = LogEntry::Debug;
103 QString _filterCategory;
104 QString _filterText;
105 bool _filterRegex = false;
106 bool _filterBypassed = true; // true when no active filter — avoids identity-map allocation
107 QRegularExpression _compiledRegex;
108
109 QSet<QString> _categoriesSet;
110 mutable QStringList _categoriesCache;
111 mutable bool _categoriesDirty = false;
112
113 static constexpr int kBatchFlushMs = 16;
114 static constexpr int kBatchMaxSize = 200;
115 static constexpr int kFilterDebounceMs = 200;
116};
void totalCountChanged()
void filterTextChanged()
const LogEntry * entryAt(int row) const override
Return entry at visible row, or nullptr if out of range.
Definition LogModel.cc:35
void categoriesChanged()
void filterCategoryChanged()
void maxEntriesChanged()
void filterRegexValidChanged()
void filterRegexChanged()
void filterLevelChanged()