~ubuntu-branches/ubuntu/quantal/marble/quantal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2011 Guillaume Martres <smarter@ubuntu.com>
//

#include "SatellitesConfigModel.h"

#include "SatellitesConfigNodeItem.h"
#include "SatellitesConfigLeafItem.h"
#include "MarbleDebug.h"

using namespace Marble;

SatellitesConfigModel::SatellitesConfigModel( QObject *parent )
    : QAbstractItemModel( parent ),
      m_rootItem( new SatellitesConfigNodeItem( "" ) )
{
}

QStringList SatellitesConfigModel::tleList()
{
    return m_rootItem->data( 0, SatellitesConfigAbstractItem::UrlListRole ).toStringList();
}

void SatellitesConfigModel::loadSettings( QHash<QString, QVariant> settings)
{
    m_rootItem->loadSettings( settings );
}

void SatellitesConfigModel::appendChild( SatellitesConfigAbstractItem *child )
{
    m_rootItem->appendChild( child );
}

QVariant SatellitesConfigModel::data( const QModelIndex &index, int role ) const
{
    if ( !index.isValid() ) {
        return QVariant();
    }

    SatellitesConfigAbstractItem *item = static_cast<SatellitesConfigAbstractItem *>( index.internalPointer() );
    return item->data( index.column(), role );
}

bool SatellitesConfigModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
    SatellitesConfigAbstractItem *item = static_cast<SatellitesConfigAbstractItem *>( index.internalPointer() );

    bool success = item->setData( index.column(), role, value );

    if ( success ) {
        QModelIndex parentCellIndex = this->index( index.parent().row(), index.column(), index.parent().parent() );
        emit dataChanged( parentCellIndex, parentCellIndex );
    }

    return success;
}

int SatellitesConfigModel::columnCount( const QModelIndex &parent ) const
{
    Q_UNUSED( parent )
    //TODO: enable second column
    return 1;
}

int SatellitesConfigModel::rowCount( const QModelIndex &parent ) const
{
    if ( parent.column() > 0 ) {
        return 0;
    }

    SatellitesConfigAbstractItem *parentItem = 0;
    if ( !parent.isValid() ) {
        parentItem = m_rootItem;
    } else {
        parentItem = static_cast<SatellitesConfigAbstractItem *>( parent.internalPointer() );
    }

    return parentItem->childrenCount();
}

QModelIndex SatellitesConfigModel::parent( const QModelIndex &child ) const
{
    if ( !child.isValid() ) {
        return QModelIndex();
    }

    SatellitesConfigAbstractItem *childItem = static_cast<SatellitesConfigAbstractItem *>( child.internalPointer() );
    SatellitesConfigAbstractItem *parentItem = childItem->parent();
    if ( parentItem == m_rootItem ) {
        return QModelIndex();
    }

    return createIndex( parentItem->row(), 0, parentItem);
}

QModelIndex SatellitesConfigModel::index( int row, int column, const QModelIndex &parent ) const
{
    if ( !hasIndex( row, column, parent ) ) {
        return QModelIndex();
    }

    SatellitesConfigAbstractItem *parentItem = 0;
    if ( !parent.isValid() ) {
        parentItem = m_rootItem;
    } else {
        parentItem = static_cast<SatellitesConfigAbstractItem *>( parent.internalPointer() );
    }

    SatellitesConfigAbstractItem *childItem = parentItem->childAt( row );

    if ( !childItem ) {
        return QModelIndex();
    }

    return createIndex( row, column, childItem );    
}

QVariant SatellitesConfigModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
    if ( orientation != Qt::Horizontal || role != Qt::DisplayRole ) {
        return QVariant();
    }

    switch (section) {
    case 0: {
        return QVariant( tr( "Category" ) );
    }
    case 1: {
        return QVariant( tr( "Display orbit" ) );
    }
    default: {
        return QVariant();
    }
    }
}

Qt::ItemFlags SatellitesConfigModel::flags( const QModelIndex &index ) const
{
    if ( !index.isValid() ) {
        return 0;
    }

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
}