modelfilter.cpp
Go to the documentation of this file.
1 #include "modelfilter.hpp"
2 #include "logs_table_model.hpp"
3 
4 
5 ModelFilter::ModelFilter(QObject *parent) :
6  QSortFilterProxyModel (parent)
7 {
8  _msg_filter_enabled = false;
9  _node_filter_enabled = false;
10  _source_filter_enabled = false;
11  _time_filter_enabled = true;
12 
13  _info_filter_enabled = false;
14  _error_filter_enabled = false;
15  _warn_filter_enabled = false;
16  _debug_filter_enabled = false;
17 }
18 
20 {
21  _msg_filter_enabled = enabled;
22  invalidateFilter();
23 }
24 
26 {
27  _node_filter_enabled = enabled;
28  invalidateFilter();
29 }
30 
32 {
33  _source_filter_enabled = enabled;
34  invalidateFilter();
35 }
36 
38 {
39  _time_filter_enabled = enabled;
40  invalidateFilter();
41 }
42 
44 {
45  _msg_mode = mode;
46  _msg_text = filter;
47 
48  if( mode == WILDCARDS){
49  QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::Wildcard );
50  _msg_validator.setRegExp(regexp);
51  }
52  else if( mode == REGEX){
53  QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::RegExp2 );
54  _msg_validator.setRegExp(regexp);
55  }
56 
57  invalidateFilter();
58 }
59 
61 {
62  _node_mode = mode;
63  _node_text = filter;
64 
65  if( mode == WILDCARDS){
66  QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::Wildcard );
67  _node_validator.setRegExp(regexp);
68  }
69  else if( mode == REGEX){
70  QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::RegExp2 );
71  _node_validator.setRegExp(regexp);
72  }
73  invalidateFilter();
74 }
75 
77 {
78  _source_mode = mode;
79  _source_text = filter;
80 
81  if( mode == WILDCARDS){
82  QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::Wildcard );
83  _source_validator.setRegExp(regexp);
84  }
85  else if( mode == REGEX){
86  QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::RegExp2 );
87  _source_validator.setRegExp(regexp);
88  }
89  invalidateFilter();
90 }
91 
93 {
94  _min = min;
95  _max = max;
96  invalidateFilter();
97 }
98 
100 {
101  _info_filter_enabled = enabled;
102  invalidateFilter();
103 }
104 
106 {
107  _debug_filter_enabled = enabled;
108  invalidateFilter();
109 }
110 
112 {
113  _error_filter_enabled = enabled;
114  invalidateFilter();
115 }
116 
118 {
119  _warn_filter_enabled = enabled;
120  invalidateFilter();
121 }
122 
123 bool ModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
124 {
125  QModelIndex index_time = sourceModel()->index(sourceRow, 1, sourceParent);
126  QModelIndex index_severity = sourceModel()->index(sourceRow, 2, sourceParent);
127  QModelIndex index_node = sourceModel()->index(sourceRow, 3, sourceParent);
128  QModelIndex index_message = sourceModel()->index(sourceRow, 4, sourceParent);
129  QModelIndex index_source = sourceModel()->index(sourceRow, 5, sourceParent);
130 
131  int severity = sourceModel()->data( index_severity, Qt::UserRole ).toInt();
132 
133  if( !_info_filter_enabled && severity == LogsTableModel::INFO) return false;
134  if( !_error_filter_enabled && severity == LogsTableModel::ERROR) return false;
135  if( !_warn_filter_enabled && severity == LogsTableModel::WARNINGS) return false;
136  if( !_debug_filter_enabled && severity == LogsTableModel::DEBUG) return false;
137 
139  {
140  int64_t usec = sourceModel()->data( index_time, Qt::UserRole ).toLongLong();
141  auto timestamp = TimePoint() + std::chrono::microseconds(usec);
142 
143  if( timestamp < _min || timestamp > _max ){
144  return false;
145  }
146  }
147 
148  if( _msg_filter_enabled ){
149  const QString& text = sourceModel()->data( index_message, Qt::UserRole ).toString();
150  bool ret = applyFilter( _msg_text, _msg_mode, text, &_msg_validator );
151  if( !ret ){
152  return false;
153  }
154  }
155 
157  const QString& text = sourceModel()->data( index_source, Qt::UserRole ).toString();
159  if( !ret ){
160  return false;
161  }
162  }
163 
164  if( _node_filter_enabled ){
165  const QString& text = sourceModel()->data( index_node, Qt::UserRole ).toString();
166  bool ret = applyFilter( _node_text, _node_mode, text, &_node_validator );
167  if( !ret ){
168  return false;
169  }
170  }
171 
172  return true;
173 }
174 
175 
176 bool ModelFilter::applyFilter(const QString& filter,
178  const QString& text_to_parse,
179  const QRegExpValidator* validator ) const
180 {
181 
182  // accept if no filter
183  if(filter.count() == 0 )
184  {
185  return true;
186  }
187 
188  assert( !( validator == nullptr && (mode == WILDCARDS || mode == REGEX) ) );
189 
190  if( mode == CONTAINS_ONE)
191  {
192  QStringList filter_words = filter.split(QRegExp("\\s"), QString::SkipEmptyParts);
193 
194  for (int i=0; i< filter_words.size(); i++){
195  if( text_to_parse.contains(filter_words[i], Qt::CaseSensitive) == true ){
196  return true;
197  }
198  }
199  return false;
200  }
201  //-----------------------
202  if( mode == WILDCARDS || mode == REGEX)
203  {
204  QString message = text_to_parse;
205  int pos = 0;
206  return validator->validate( message, pos ) == QValidator::Acceptable;
207  }
208  return false;
209 }
210 
QRegExpValidator _msg_validator
Definition: modelfilter.hpp:73
std::chrono::high_resolution_clock::time_point TimePoint
FilterMode _node_mode
Definition: modelfilter.hpp:64
bool _msg_filter_enabled
Definition: modelfilter.hpp:56
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
bool _error_filter_enabled
Definition: modelfilter.hpp:61
void timeMinMaxUpdated(TimePoint min, TimePoint max)
Definition: modelfilter.cpp:92
FilterMode _source_mode
Definition: modelfilter.hpp:66
bool _time_filter_enabled
Definition: modelfilter.hpp:57
QString _msg_text
Definition: modelfilter.hpp:69
bool _debug_filter_enabled
Definition: modelfilter.hpp:59
void setSourceFilterEnabled(bool enabled)
Definition: modelfilter.cpp:31
void sourceFilterUpdated(FilterMode mode, const QString &text)
Definition: modelfilter.cpp:76
FilterMode _msg_mode
Definition: modelfilter.hpp:65
QRegExpValidator _source_validator
Definition: modelfilter.hpp:74
void setNodeFilterEnabled(bool enabled)
Definition: modelfilter.cpp:25
void messageFilterUpdated(FilterMode mode, const QString &text)
Definition: modelfilter.cpp:43
void setSeverityInfoEnabled(bool enabled)
Definition: modelfilter.cpp:99
void setSeverityErrorEnabled(bool enabled)
bool _info_filter_enabled
Definition: modelfilter.hpp:60
bool _source_filter_enabled
Definition: modelfilter.hpp:55
void setTimeFilterEnabled(bool enabled)
Definition: modelfilter.cpp:37
QRegExpValidator _node_validator
Definition: modelfilter.hpp:72
void nodeFilterUpdated(FilterMode mode, const QString &text)
Definition: modelfilter.cpp:60
void setSeverityDebugEnabled(bool enabled)
void setSeverityWarningsEnabled(bool enabled)
TimePoint _max
Definition: modelfilter.hpp:52
int i
bool applyFilter(const QString &filter, ModelFilter::FilterMode mode, const QString &text_to_parse, const QRegExpValidator *validator) const
void setMessageFilterEnabled(bool enabled)
Definition: modelfilter.cpp:19
TimePoint _min
Definition: modelfilter.hpp:51
QString _node_text
Definition: modelfilter.hpp:68
bool _warn_filter_enabled
Definition: modelfilter.hpp:62
ModelFilter(QObject *parent=0)
Definition: modelfilter.cpp:5
bool _node_filter_enabled
Definition: modelfilter.hpp:54
QString _source_text
Definition: modelfilter.hpp:70


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17