Marble

BranchFilterProxyModel.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2010 Dennis Nienhüser <[email protected]>
4 // SPDX-FileCopyrightText: 2012 Thibaut Gridel <[email protected]>
5 
6 #include "BranchFilterProxyModel.h"
7 
8 #include "MarblePlacemarkModel.h"
9 #include "GeoDataContainer.h"
10 #include "GeoDataTreeModel.h"
11 
12 namespace Marble
13 {
14 
15 BranchFilterProxyModel::BranchFilterProxyModel( QObject *parent ) :
16  QSortFilterProxyModel( parent ), m_treeModel( nullptr )
17 {
18  // nothing to do
19 }
20 
21 /// sets the folder index for which we want to see bookmarks
22 void BranchFilterProxyModel::setBranchIndex( GeoDataTreeModel* treeModel, const QModelIndex &index )
23 {
24  Q_ASSERT( index.isValid() );
25  Q_ASSERT( index.model() == treeModel );
26  m_treeModel = treeModel;
27  m_branchIndex = index;
28  invalidateFilter();
29 }
30 
31 /// determines if such row should be filtered.
32 /// Beware, all parents of our folder must be accepted as well
33 bool BranchFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
34 {
35  if ( !m_treeModel || !m_branchIndex.isValid() ) {
36  return true;
37  }
38 
39  Q_ASSERT( m_treeModel == sourceModel() );
40  if ( sourceParent.isValid() ) {
41  Q_ASSERT( sourceParent.model() == m_treeModel );
42  }
43  QModelIndex rowIndex = sourceModel()->index( sourceRow, 0, sourceParent );
44  Q_ASSERT( rowIndex.isValid() );
45 
46  // return true for all non folder children of m_branchIndex
47  if( sourceParent == m_branchIndex ) {
48  GeoDataObject* obj = qvariant_cast<GeoDataObject*>( rowIndex.data( MarblePlacemarkModel::ObjectPointerRole ) );
49  return !dynamic_cast<const GeoDataContainer *>(obj);
50  }
51 
52  // return true if rowIndex is a parent of m_branchIndex
53  QModelIndex tmpIndex = m_branchIndex;
54  while ( tmpIndex.isValid() && tmpIndex != rowIndex ) {
55  tmpIndex = tmpIndex.parent();
56  }
57  return tmpIndex == rowIndex;
58 }
59 
60 }
@ ObjectPointerRole
The pointer to a specific object.
QVariant data(int role) const const
Binds a QML item to a specific geodetic location in screen coordinates.
bool isValid() const const
QModelIndex parent() const const
const QAbstractItemModel * model() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Oct 4 2023 04:09:41 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.