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 //
0005 
0006 #include "SatellitesConfigModel.h"
0007 
0008 #include "SatellitesConfigLeafItem.h"
0009 #include "MarbleDebug.h"
0010 
0011 namespace Marble {
0012 
0013 SatellitesConfigModel::SatellitesConfigModel( QObject *parent )
0014     : QAbstractItemModel( parent ),
0015       m_rootItem(new SatellitesConfigNodeItem(QString()))
0016 {
0017 }
0018 
0019 SatellitesConfigModel::~SatellitesConfigModel()
0020 {
0021     delete m_rootItem;
0022 }
0023 
0024 QStringList SatellitesConfigModel::idList() const
0025 {
0026     return m_rootItem->data( 0, SatellitesConfigAbstractItem::IdListRole )
0027             .toStringList();
0028 }
0029 
0030 QStringList SatellitesConfigModel::fullIdList() const
0031 {
0032     return m_rootItem->data( 0, SatellitesConfigAbstractItem::FullIdListRole )
0033             .toStringList();
0034 }
0035 
0036 QStringList SatellitesConfigModel::urlList() const
0037 {
0038     return m_rootItem->data( 0, SatellitesConfigAbstractItem::UrlListRole )
0039             .toStringList();
0040 }
0041 
0042 void SatellitesConfigModel::loadSettings(const QHash<QString, QVariant> &settings)
0043 {
0044     m_rootItem->loadSettings( settings );
0045 }
0046 
0047 void SatellitesConfigModel::appendChild( SatellitesConfigAbstractItem *child )
0048 {
0049     m_rootItem->appendChild( child );
0050 }
0051 
0052 void SatellitesConfigModel::clear()
0053 {
0054     m_rootItem->clear();
0055 }
0056 
0057 QVariant SatellitesConfigModel::data( const QModelIndex &index, int role ) const
0058 {
0059     if ( !index.isValid() ) {
0060         return QVariant();
0061     }
0062 
0063     SatellitesConfigAbstractItem *item = 
0064         static_cast<SatellitesConfigAbstractItem *>( index.internalPointer() );
0065     return item->data( index.column(), role );
0066 }
0067 
0068 bool SatellitesConfigModel::setData( const QModelIndex &index,
0069                                      const QVariant &value,
0070                                      int role )
0071 {
0072     SatellitesConfigAbstractItem *item =
0073         static_cast<SatellitesConfigAbstractItem *>( index.internalPointer() );
0074 
0075     bool success = item->setData( index.column(), role, value );
0076 
0077     if ( success ) {
0078         QModelIndex parentCellIndex = this->index( index.parent().row(),
0079                                                    index.column(),
0080                                                    index.parent().parent() );
0081         emit dataChanged( parentCellIndex, parentCellIndex );
0082     }
0083 
0084     return success;
0085 }
0086 
0087 int SatellitesConfigModel::columnCount( const QModelIndex &parent ) const
0088 {
0089     Q_UNUSED( parent )
0090     //TODO: enable second column
0091     return 1;
0092 }
0093 
0094 int SatellitesConfigModel::rowCount( const QModelIndex &parent ) const
0095 {
0096     if ( parent.column() > 0 ) {
0097         return 0;
0098     }
0099 
0100     SatellitesConfigAbstractItem *parentItem = nullptr;
0101     if ( !parent.isValid() ) {
0102         parentItem = m_rootItem;
0103     } else {
0104         parentItem = static_cast<SatellitesConfigAbstractItem *>(
0105             parent.internalPointer() );
0106     }
0107 
0108     return parentItem->childrenCount();
0109 }
0110 
0111 QModelIndex SatellitesConfigModel::parent( const QModelIndex &child ) const
0112 {
0113     if ( !child.isValid() ) {
0114         return QModelIndex();
0115     }
0116 
0117     SatellitesConfigAbstractItem *childItem =
0118         static_cast<SatellitesConfigAbstractItem *>( child.internalPointer() );
0119     SatellitesConfigAbstractItem *parentItem = childItem->parent();
0120 
0121     if ( parentItem == m_rootItem ) {
0122         return QModelIndex();
0123     }
0124 
0125     return createIndex( parentItem->row(), 0, parentItem );
0126 }
0127 
0128 QModelIndex SatellitesConfigModel::index( int row, int column,
0129                                           const QModelIndex &parent ) const
0130 {
0131     if ( !hasIndex( row, column, parent ) ) {
0132         return QModelIndex();
0133     }
0134 
0135     SatellitesConfigAbstractItem *parentItem = nullptr;
0136     if ( !parent.isValid() ) {
0137         parentItem = m_rootItem;
0138     } else {
0139         parentItem = static_cast<SatellitesConfigAbstractItem *>(
0140             parent.internalPointer() );
0141     }
0142 
0143     SatellitesConfigAbstractItem *childItem = parentItem->childAt( row );
0144 
0145     if ( !childItem ) {
0146         return QModelIndex();
0147     }
0148 
0149     return createIndex( row, column, childItem );
0150 }
0151 
0152 QVariant SatellitesConfigModel::headerData( int section,
0153                                             Qt::Orientation orientation,
0154                                             int role ) const
0155 {
0156     if ( orientation != Qt::Horizontal || role != Qt::DisplayRole ) {
0157         return QVariant();
0158     }
0159 
0160     switch (section) {
0161         case 0: {
0162             return QVariant( tr( "Catalogs" ) );
0163         }
0164         default: {
0165             return QVariant();
0166         }
0167     }
0168 }
0169 
0170 Qt::ItemFlags SatellitesConfigModel::flags( const QModelIndex &index ) const
0171 {
0172     if ( !index.isValid() ) {
0173         return Qt::ItemFlags();
0174     }
0175 
0176     SatellitesConfigAbstractItem *item =
0177         static_cast<SatellitesConfigAbstractItem *>( index.internalPointer() );
0178     return item->flags();
0179 }
0180 
0181 SatellitesConfigNodeItem* SatellitesConfigModel::rootItem() const
0182 {
0183     return m_rootItem;
0184 }
0185 
0186 } // namespace Marble
0187 
0188 #include "moc_SatellitesConfigModel.cpp"
0189