00001 #include "modelfilter.hpp"
00002 #include "logs_table_model.hpp"
00003
00004
00005 ModelFilter::ModelFilter(QObject *parent) :
00006 QSortFilterProxyModel (parent)
00007 {
00008 _msg_filter_enabled = false;
00009 _node_filter_enabled = false;
00010 _source_filter_enabled = false;
00011 _time_filter_enabled = true;
00012
00013 _info_filter_enabled = false;
00014 _error_filter_enabled = false;
00015 _warn_filter_enabled = false;
00016 _debug_filter_enabled = false;
00017 }
00018
00019 void ModelFilter::setMessageFilterEnabled(bool enabled)
00020 {
00021 _msg_filter_enabled = enabled;
00022 invalidateFilter();
00023 }
00024
00025 void ModelFilter::setNodeFilterEnabled(bool enabled)
00026 {
00027 _node_filter_enabled = enabled;
00028 invalidateFilter();
00029 }
00030
00031 void ModelFilter::setSourceFilterEnabled(bool enabled)
00032 {
00033 _source_filter_enabled = enabled;
00034 invalidateFilter();
00035 }
00036
00037 void ModelFilter::setTimeFilterEnabled(bool enabled)
00038 {
00039 _time_filter_enabled = enabled;
00040 invalidateFilter();
00041 }
00042
00043 void ModelFilter::messageFilterUpdated(ModelFilter::FilterMode mode, const QString &filter)
00044 {
00045 _msg_mode = mode;
00046 _msg_text = filter;
00047
00048 if( mode == WILDCARDS){
00049 QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::Wildcard );
00050 _msg_validator.setRegExp(regexp);
00051 }
00052 else if( mode == REGEX){
00053 QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::RegExp2 );
00054 _msg_validator.setRegExp(regexp);
00055 }
00056
00057 invalidateFilter();
00058 }
00059
00060 void ModelFilter::nodeFilterUpdated(ModelFilter::FilterMode mode, const QString &filter)
00061 {
00062 _node_mode = mode;
00063 _node_text = filter;
00064
00065 if( mode == WILDCARDS){
00066 QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::Wildcard );
00067 _node_validator.setRegExp(regexp);
00068 }
00069 else if( mode == REGEX){
00070 QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::RegExp2 );
00071 _node_validator.setRegExp(regexp);
00072 }
00073 invalidateFilter();
00074 }
00075
00076 void ModelFilter::sourceFilterUpdated(ModelFilter::FilterMode mode, const QString &filter)
00077 {
00078 _source_mode = mode;
00079 _source_text = filter;
00080
00081 if( mode == WILDCARDS){
00082 QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::Wildcard );
00083 _source_validator.setRegExp(regexp);
00084 }
00085 else if( mode == REGEX){
00086 QRegExp regexp( filter, Qt::CaseSensitive, QRegExp::RegExp2 );
00087 _source_validator.setRegExp(regexp);
00088 }
00089 invalidateFilter();
00090 }
00091
00092 void ModelFilter::timeMinMaxUpdated(TimePoint min, TimePoint max)
00093 {
00094 _min = min;
00095 _max = max;
00096 invalidateFilter();
00097 }
00098
00099 void ModelFilter::setSeverityInfoEnabled(bool enabled)
00100 {
00101 _info_filter_enabled = enabled;
00102 invalidateFilter();
00103 }
00104
00105 void ModelFilter::setSeverityDebugEnabled(bool enabled)
00106 {
00107 _debug_filter_enabled = enabled;
00108 invalidateFilter();
00109 }
00110
00111 void ModelFilter::setSeverityErrorEnabled(bool enabled)
00112 {
00113 _error_filter_enabled = enabled;
00114 invalidateFilter();
00115 }
00116
00117 void ModelFilter::setSeverityWarningsEnabled(bool enabled)
00118 {
00119 _warn_filter_enabled = enabled;
00120 invalidateFilter();
00121 }
00122
00123 bool ModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
00124 {
00125 QModelIndex index_time = sourceModel()->index(sourceRow, 1, sourceParent);
00126 QModelIndex index_severity = sourceModel()->index(sourceRow, 2, sourceParent);
00127 QModelIndex index_node = sourceModel()->index(sourceRow, 3, sourceParent);
00128 QModelIndex index_message = sourceModel()->index(sourceRow, 4, sourceParent);
00129 QModelIndex index_source = sourceModel()->index(sourceRow, 5, sourceParent);
00130
00131 int severity = sourceModel()->data( index_severity, Qt::UserRole ).toInt();
00132
00133 if( !_info_filter_enabled && severity == LogsTableModel::INFO) return false;
00134 if( !_error_filter_enabled && severity == LogsTableModel::ERROR) return false;
00135 if( !_warn_filter_enabled && severity == LogsTableModel::WARNINGS) return false;
00136 if( !_debug_filter_enabled && severity == LogsTableModel::DEBUG) return false;
00137
00138 if( _time_filter_enabled )
00139 {
00140 int64_t usec = sourceModel()->data( index_time, Qt::UserRole ).toLongLong();
00141 auto timestamp = TimePoint() + std::chrono::microseconds(usec);
00142
00143 if( timestamp < _min || timestamp > _max ){
00144 return false;
00145 }
00146 }
00147
00148 if( _msg_filter_enabled ){
00149 const QString& text = sourceModel()->data( index_message, Qt::UserRole ).toString();
00150 bool ret = applyFilter( _msg_text, _msg_mode, text, &_msg_validator );
00151 if( !ret ){
00152 return false;
00153 }
00154 }
00155
00156 if( _source_filter_enabled ){
00157 const QString& text = sourceModel()->data( index_source, Qt::UserRole ).toString();
00158 bool ret = applyFilter( _source_text, _source_mode, text, &_source_validator );
00159 if( !ret ){
00160 return false;
00161 }
00162 }
00163
00164 if( _node_filter_enabled ){
00165 const QString& text = sourceModel()->data( index_node, Qt::UserRole ).toString();
00166 bool ret = applyFilter( _node_text, _node_mode, text, &_node_validator );
00167 if( !ret ){
00168 return false;
00169 }
00170 }
00171
00172 return true;
00173 }
00174
00175
00176 bool ModelFilter::applyFilter(const QString& filter,
00177 ModelFilter::FilterMode mode,
00178 const QString& text_to_parse,
00179 const QRegExpValidator* validator ) const
00180 {
00181
00182
00183 if(filter.count() == 0 )
00184 {
00185 return true;
00186 }
00187
00188 assert( !( validator == nullptr && (mode == WILDCARDS || mode == REGEX) ) );
00189
00190 if( mode == CONTAINS_ONE)
00191 {
00192 QStringList filter_words = filter.split(QRegExp("\\s"), QString::SkipEmptyParts);
00193
00194 for (int i=0; i< filter_words.size(); i++){
00195 if( text_to_parse.contains(filter_words[i], Qt::CaseSensitive) == true ){
00196 return true;
00197 }
00198 }
00199 return false;
00200 }
00201
00202 if( mode == WILDCARDS || mode == REGEX)
00203 {
00204 QString message = text_to_parse;
00205 int pos = 0;
00206 return validator->validate( message, pos ) == QValidator::Acceptable;
00207 }
00208 return false;
00209 }
00210