File indexing completed on 2024-04-28 07:38:19

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2014 Levente Kurusa <levex@linux.com>
0004 
0005 #include "TreeViewDecoratorModel.h"
0006 #include "MarbleDebug.h"
0007 #include "GeoDataFolder.h"
0008 #include "GeoDataObject.h"
0009 #include "GeoDataContainer.h"
0010 #include "GeoDataStyle.h"
0011 #include "GeoDataListStyle.h"
0012 #include "GeoDataItemIcon.h"
0013 #include "GeoDataGeometry.h"
0014 #include "MarblePlacemarkModel.h"
0015 
0016 #include <QImage>
0017 
0018 namespace Marble
0019 {
0020 
0021 TreeViewDecoratorModel::TreeViewDecoratorModel( QObject *parent ) :
0022     QSortFilterProxyModel( parent )
0023 {
0024     // nothing to do
0025 }
0026 
0027 bool TreeViewDecoratorModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
0028 {
0029     QModelIndex rowIndex = sourceModel()->index( sourceRow, 0, sourceParent );
0030 
0031     const GeoDataObject* object = qvariant_cast<GeoDataObject*>( rowIndex.data( MarblePlacemarkModel::ObjectPointerRole ) );
0032     const GeoDataObject* parent = object->parent();
0033     if (const auto container = dynamic_cast<const GeoDataContainer *>(parent)) {
0034         if ( container->style()->listStyle().listItemType() == GeoDataListStyle::CheckHideChildren ) {
0035             return false;
0036         }
0037     }
0038 
0039     return QSortFilterProxyModel::filterAcceptsRow( sourceRow, sourceParent );
0040 }
0041 
0042 QVariant TreeViewDecoratorModel::data( const QModelIndex &proxyIndex, int role) const
0043 {
0044     if ( role != Qt::DecorationRole || proxyIndex.column() != 0 ) {
0045         if (proxyIndex.column() == 1) {
0046             return QSortFilterProxyModel::data(proxyIndex, role).toString().remove("GeoData");
0047         }
0048         else {
0049             return QSortFilterProxyModel::data(proxyIndex, role);
0050         }
0051     }
0052 
0053     GeoDataObject *object = qvariant_cast<GeoDataObject *>( QSortFilterProxyModel::data(proxyIndex, MarblePlacemarkModel::ObjectPointerRole));
0054     if ( !object ) {
0055         return QSortFilterProxyModel::data(proxyIndex, role);
0056     }
0057 
0058     GeoDataFolder *folder = dynamic_cast<GeoDataFolder *>( object );
0059 
0060     if (folder) {
0061         bool const expandedState = m_expandedRows.contains( QPersistentModelIndex( proxyIndex ) );
0062 
0063         for (GeoDataItemIcon *icon: folder->style()->listStyle().itemIconList()) {
0064             if ( ! expandedState ) {
0065                 if ( icon->state() == GeoDataItemIcon::Closed ) {
0066                     return icon->icon();
0067                 }
0068             } else {
0069                 if ( icon->state() == GeoDataItemIcon::Open ) {
0070                     return icon->icon();
0071                 }
0072             }
0073         }
0074     }
0075 
0076     return QSortFilterProxyModel::data(proxyIndex, role);
0077 }
0078 
0079 void TreeViewDecoratorModel::trackExpandedState( const QModelIndex &index )
0080 {
0081     m_expandedRows << QPersistentModelIndex( index );
0082 }
0083 
0084 void TreeViewDecoratorModel::trackCollapsedState( const QModelIndex &index )
0085 {
0086     m_expandedRows.removeAll( QPersistentModelIndex( index ));
0087 }
0088 
0089 }
0090 #include "moc_TreeViewDecoratorModel.cpp"
0091