File indexing completed on 2024-05-05 05:40:28

0001 /***************************************************************************
0002  *  Copyright (C) 2021 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "model/mediafilteredmodel.h"
0021 
0022 #include <QDebug>
0023 
0024 MediaFilteredModel::MediaFilteredModel()
0025 {
0026     setDynamicSortFilter(true);
0027     connect(this, &MediaFilteredModel::typeChanged, this, &MediaFilteredModel::invalidateFilter);
0028     connect(this, &MediaFilteredModel::patternChanged, this, &MediaFilteredModel::invalidateFilter);
0029 }
0030 
0031 Core::MediaType MediaFilteredModel::type() const
0032 {
0033     return m_type;
0034 }
0035 
0036 QString MediaFilteredModel::pattern() const
0037 {
0038     return m_pattern;
0039 }
0040 
0041 void MediaFilteredModel::setType(Core::MediaType type)
0042 {
0043     if(m_type == type)
0044         return;
0045     m_type= type;
0046     emit typeChanged();
0047 }
0048 
0049 void MediaFilteredModel::setPattern(const QString& pattern)
0050 {
0051     if(m_pattern == pattern)
0052         return;
0053     m_pattern= pattern;
0054     emit patternChanged();
0055 }
0056 
0057 bool MediaFilteredModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceparent) const
0058 {
0059     QModelIndex index0= sourceModel()->index(sourceRow, 0, sourceparent);
0060     QModelIndex index2= sourceModel()->index(sourceRow, 2, sourceparent);
0061     auto mediaType= sourceModel()->data(index2).value<Core::MediaType>();
0062     auto name= sourceModel()->data(index0).toString();
0063 
0064     QRegularExpression re(m_pattern);
0065     auto match= re.match(name);
0066 
0067     auto respectPattern= m_pattern.isEmpty() ? true : match.hasMatch();
0068 
0069     auto respectMediaType= m_type == Core::MediaType::Unknown ? true : mediaType == m_type;
0070 
0071     return respectPattern && respectMediaType;
0072 }