File indexing completed on 2024-04-28 03:50:23

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Guillaume Martres <smarter@ubuntu.com>
0004 // SPDX-FileCopyrightText: 2012 Rene Kuettner <rene@bitkanal.net>
0005 //
0006 
0007 #include "SatellitesConfigNodeItem.h"
0008 
0009 #include <QStringList>
0010 #include <QVariant>
0011 
0012 #include "MarbleDebug.h"
0013 
0014 namespace Marble {
0015 
0016 SatellitesConfigNodeItem::SatellitesConfigNodeItem( const QString &name )
0017     : SatellitesConfigAbstractItem( name )
0018 {
0019 }
0020 
0021 SatellitesConfigNodeItem::~SatellitesConfigNodeItem()
0022 {
0023 }
0024 
0025 void SatellitesConfigNodeItem::loadSettings(const QHash<QString, QVariant> &settings)
0026 {
0027     for( SatellitesConfigAbstractItem *item: m_children ) {
0028         item->loadSettings( settings );
0029     }
0030 }
0031 
0032 QVariant SatellitesConfigNodeItem::data( int column, int role ) const
0033 {
0034     QVariant base = SatellitesConfigAbstractItem::data( column, role );
0035     if ( base.isValid() ) {
0036         return base;
0037     }
0038 
0039     switch ( role ) {
0040     case IdListRole:
0041     case UrlListRole: {
0042         QStringList list;
0043         for( SatellitesConfigAbstractItem *item: m_children ) {
0044             if ( item->data( column, Qt::CheckStateRole ).toInt() != Qt::Unchecked ) {
0045                 list.append( item->data( column, role).toStringList() );
0046             }
0047         }
0048         return list;
0049     }
0050     case FullIdListRole: {
0051         QStringList fullIdList;
0052         for( SatellitesConfigAbstractItem *item: m_children ) {
0053             fullIdList.append( item->data( column, role ).toStringList() );
0054         }
0055         return fullIdList;
0056     }
0057     case Qt::CheckStateRole: {
0058         bool oneChecked = false;
0059         bool oneUnchecked = false;
0060         for( SatellitesConfigAbstractItem *item: m_children ) {         
0061             switch ( item->data( column, Qt::CheckStateRole ).toInt() ) {
0062             case Qt::Checked:
0063                 oneChecked = true;
0064                 if ( oneUnchecked ) {
0065                     return Qt::PartiallyChecked;
0066                 }
0067                 break;
0068             case Qt::PartiallyChecked:
0069                 return Qt::PartiallyChecked;
0070             case Qt::Unchecked:
0071                 oneUnchecked = true;
0072                 if ( oneChecked ) {
0073                     return Qt::PartiallyChecked;
0074                 }
0075             }
0076         }
0077 
0078         return QVariant( oneChecked ? Qt::Checked : Qt::Unchecked );
0079     }
0080     }
0081 
0082     return QVariant();
0083 }
0084 
0085 bool SatellitesConfigNodeItem::setData( int column, int role, const QVariant &data )
0086 {
0087     if ( role == Qt::CheckStateRole ) {
0088         switch ( column ) {
0089         case 0:
0090             // fall through
0091         case 1:
0092             for( SatellitesConfigAbstractItem *item: m_children ) {
0093                 item->setData( column, role, data );
0094             }
0095             return true;
0096         }
0097     }
0098 
0099     return false;
0100 }
0101 
0102 bool SatellitesConfigNodeItem::isLeaf() const
0103 {
0104     return false;
0105 }
0106 
0107 SatellitesConfigAbstractItem *SatellitesConfigNodeItem::childAt( int row ) const
0108 {
0109     if ( m_children.size() <= row ) {
0110         return nullptr;
0111     }
0112 
0113     return m_children.at( row );
0114 }
0115 
0116 int SatellitesConfigNodeItem::indexOf( const SatellitesConfigAbstractItem *child ) const
0117 {
0118     //TODO: find out if we can avoid the const_cast
0119     return m_children.indexOf( const_cast<SatellitesConfigAbstractItem *>( child ) );
0120 }
0121 
0122 int SatellitesConfigNodeItem::childrenCount() const
0123 {
0124     return m_children.size();
0125 }
0126 
0127 void SatellitesConfigNodeItem::appendChild( SatellitesConfigAbstractItem *item )
0128 {
0129     item->setParent( this );
0130     m_children.append( item );
0131 }
0132 
0133 void SatellitesConfigNodeItem::clear()
0134 {
0135     for( int i = childrenCount(); i > 0; --i ) {
0136         SatellitesConfigAbstractItem *item = m_children.at( i - 1 );
0137         item->clear();
0138         m_children.remove( i - 1 );
0139         delete item;
0140     }
0141 }
0142 
0143 } // namespace Marble
0144