File indexing completed on 2024-03-24 15:14:47

0001 /*************************************************************************************
0002  *  Copyright (C) 2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com>      *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 //Analitza includes
0020 #include <analitzaplot/plotsmodel.h>
0021 #include <analitzaplot/plotitem.h>
0022 
0023 //local includes
0024 #include "spaceplotsproxymodel.h"
0025 #include "spaceitem.h"
0026 #include "spacesmodel.h"
0027 #include "datastore.h"
0028 
0029 using namespace Analitza;
0030 
0031 SpacesFilterProxyModel::SpacesFilterProxyModel(QObject *parent)
0032     : QSortFilterProxyModel(parent)
0033 {
0034     m_dimension = DimAll;
0035     setDynamicSortFilter(true);
0036 }
0037 
0038 void SpacesFilterProxyModel::setFilterDimension(Dimensions dimension)
0039 {
0040     m_dimension = dimension;
0041     invalidateFilter();
0042 }
0043 
0044 void SpacesFilterProxyModel::setFilterText(const QString& text)
0045 {
0046     m_filterText=text;
0047     invalidateFilter();
0048 }
0049 
0050 bool SpacesFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0051 {
0052     if (!sourceModel() || sourceParent.isValid())
0053         return false;
0054     SpaceItem *spaceItem = static_cast<SpacesModel*>(sourceModel())->space(sourceRow);
0055     if (!spaceItem)
0056         return false;
0057     if (m_dimension != DimAll && spaceItem->dimension() != m_dimension)
0058         return false;
0059     if(!spaceItem->name().contains(m_filterText,Qt::CaseInsensitive)
0060             && !spaceItem->description().contains(m_filterText,Qt::CaseInsensitive))
0061         return false;
0062     return true;
0063 }
0064 
0065 
0066 PlotsProxyModel::PlotsProxyModel(QObject *parent)
0067     : QSortFilterProxyModel(parent)
0068     , m_dimension(DimAll)
0069 {
0070      setDynamicSortFilter(true);
0071 }
0072 
0073 PlotsProxyModel::~PlotsProxyModel()
0074 {
0075 }
0076 
0077 void PlotsProxyModel::setFilterSpaceDimension(Dimensions dimension)
0078 {
0079     m_dimension = dimension;
0080     invalidateFilter();
0081 }
0082 
0083 bool PlotsProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0084 {
0085     Q_ASSERT(sourceModel());
0086     if(sourceParent.isValid())
0087         return false;
0088     
0089     QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
0090     return m_dimension & idx.data(PlotsModel::DimensionRole).toInt();
0091 }
0092 
0093 bool PlotsProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
0094 {
0095     return QString::localeAwareCompare(left.data().toString(), right.data().toString())>=0;
0096 }
0097 
0098 SpacePlotsFilterProxyModel::SpacePlotsFilterProxyModel(DataStore *ds, QObject* parent)
0099     : PlotsProxyModel(parent)
0100     , m_dataStore(ds)
0101 {}
0102 
0103 SpacePlotsFilterProxyModel::~SpacePlotsFilterProxyModel()
0104 {}
0105 
0106 void SpacePlotsFilterProxyModel::setFilterSpace(SpaceItem* space)
0107 {
0108     Q_ASSERT(space);
0109     m_space = space; 
0110     invalidateFilter();
0111 }
0112 
0113 bool SpacePlotsFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
0114 {
0115     PlotItem* item = sourceModel()->index(sourceRow, 0, sourceParent).data(PlotsModel::PlotRole).value<PlotItem*>();
0116     return m_dataStore->isMapped(m_space, item);
0117 }
0118 
0119 #include "moc_spaceplotsproxymodel.cpp"