File indexing completed on 2024-04-21 05:47:46

0001 /*******************************************************************************
0002  * Copyright (C) 2016 Ragnar Thomsen <rthomsen6@gmail.com>                     *
0003  *                                                                             *
0004  * This program is free software: you can redistribute it and/or modify it     *
0005  * under the terms of the GNU General Public License as published by the Free  *
0006  * Software Foundation, either version 2 of the License, or (at your option)   *
0007  * any later version.                                                          *
0008  *                                                                             *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT *
0010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
0011  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
0012  * more details.                                                               *
0013  *                                                                             *
0014  * You should have received a copy of the GNU General Public License along     *
0015  * with this program. If not, see <http://www.gnu.org/licenses/>.              *
0016  *******************************************************************************/
0017 
0018 #include "sortfilterunitmodel.h"
0019 
0020 #include <QDebug>
0021 #include <QRegularExpression>
0022 
0023 SortFilterUnitModel::SortFilterUnitModel(QObject *parent)
0024     : QSortFilterProxyModel(parent)
0025 {
0026 }
0027 
0028 void SortFilterUnitModel::initFilterMap(const QMap<filterType, QString> &map)
0029 {
0030     filtersMap.clear();
0031 
0032     for(QMap<filterType, QString>::const_iterator iter = map.constBegin(); iter != map.constEnd(); ++iter)
0033     {
0034         filtersMap[iter.key()] = iter.value();
0035     }
0036 
0037 }
0038 
0039 void SortFilterUnitModel::addFilterRegExp(filterType type, const QString &pattern)
0040 {
0041     if (!filtersMap.contains(type)) {
0042         return;
0043     }
0044 
0045     filtersMap[type] = pattern;
0046 }
0047 
0048 bool SortFilterUnitModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0049 {
0050     if(filtersMap.isEmpty()) {
0051         return true;
0052     }
0053 
0054     bool ret = false;
0055 
0056     for(QMap<filterType, QString>::const_iterator iter = filtersMap.constBegin(); iter != filtersMap.constEnd(); ++iter)
0057     {
0058         QModelIndex indexActiveState = sourceModel()->index(sourceRow, 2, sourceParent);
0059         QModelIndex indexUnitName = sourceModel()->index(sourceRow, 0, sourceParent);
0060 
0061         if (iter.key() == activeState) {
0062             ret = (indexActiveState.data().toString().contains(QRegularExpression(iter.value())));
0063         } else if (iter.key() == unitType) {
0064             ret = (indexUnitName.data().toString().contains(QRegularExpression(iter.value())));
0065         } else if (iter.key() == unitName) {
0066             ret = (indexUnitName.data().toString().contains(QRegularExpression(iter.value(), QRegularExpression::CaseInsensitiveOption)));
0067         }
0068 
0069         if(!ret) {
0070             return ret;
0071         }
0072     }
0073 
0074     return ret;
0075 }
0076