File indexing completed on 2024-04-21 03:49:29

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2010 Dennis Nienhüser <nienhueser@kde.org>
0004 // SPDX-FileCopyrightText: 2012 Thibaut Gridel <tgridel@free.fr>
0005 
0006 #include "BranchFilterProxyModel.h"
0007 
0008 #include "MarblePlacemarkModel.h"
0009 #include "GeoDataContainer.h"
0010 #include "GeoDataTreeModel.h"
0011 
0012 namespace Marble
0013 {
0014 
0015 BranchFilterProxyModel::BranchFilterProxyModel( QObject *parent ) :
0016     QSortFilterProxyModel( parent ), m_treeModel( nullptr )
0017 {
0018     // nothing to do
0019 }
0020 
0021 /// sets the folder index for which we want to see bookmarks
0022 void BranchFilterProxyModel::setBranchIndex( GeoDataTreeModel* treeModel, const QModelIndex &index )
0023 {
0024     Q_ASSERT( index.isValid() );
0025     Q_ASSERT( index.model() == treeModel );
0026     m_treeModel = treeModel;
0027     m_branchIndex = index;
0028     invalidateFilter();
0029 }
0030 
0031 /// determines if such row should be filtered.
0032 /// Beware, all parents of our folder must be accepted as well
0033 bool BranchFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
0034 {
0035     if ( !m_treeModel || !m_branchIndex.isValid() ) {
0036         return true;
0037     }
0038 
0039     Q_ASSERT( m_treeModel == sourceModel() );
0040     if ( sourceParent.isValid() ) {
0041         Q_ASSERT( sourceParent.model() == m_treeModel );
0042     }
0043     QModelIndex rowIndex = sourceModel()->index( sourceRow, 0, sourceParent );
0044     Q_ASSERT( rowIndex.isValid() );
0045 
0046     // return true for all non folder children of m_branchIndex
0047     if( sourceParent == m_branchIndex ) {
0048         GeoDataObject* obj = qvariant_cast<GeoDataObject*>( rowIndex.data( MarblePlacemarkModel::ObjectPointerRole ) );
0049         return !dynamic_cast<const GeoDataContainer *>(obj);
0050     }
0051 
0052     // return true if rowIndex is a parent of m_branchIndex
0053     QModelIndex tmpIndex = m_branchIndex;
0054     while ( tmpIndex.isValid() && tmpIndex != rowIndex ) {
0055         tmpIndex = tmpIndex.parent();
0056     }
0057     return tmpIndex == rowIndex;
0058 }
0059 
0060 }