summaryrefslogtreecommitdiff
path: root/src/nepomuk/utils/simpleresourcemodel.cpp
blob: f500f648f7d46bf85a19066d78e4973622cb712c (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
   This file is part of the Nepomuk KDE project.
   Copyright (C) 2008-2010 Sebastian Trueg <trueg@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) version 3, or any
   later version accepted by the membership of KDE e.V. (or its
   successor approved by the membership of KDE e.V.), which shall
   act as a proxy defined in Section 6 of version 3 of the license.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "simpleresourcemodel.h"

#include <QtCore/QUrl>
#include <QtCore/QList>

#include <Nepomuk2/Resource>
#include <Nepomuk2/Query/Result>

#include "kdebug.h"
#include "kurl.h"


class Nepomuk2::Utils::SimpleResourceModel::Private
{
public:
    QList<Nepomuk2::Resource> resources;
};


Nepomuk2::Utils::SimpleResourceModel::SimpleResourceModel( QObject* parent )
    : ResourceModel( parent ),
      d( new Private() )
{
}


Nepomuk2::Utils::SimpleResourceModel::~SimpleResourceModel()
{
    delete d;
}


QModelIndex Nepomuk2::Utils::SimpleResourceModel::indexForResource( const Resource& res ) const
{
    Q_ASSERT( res.isValid() );
    // FIXME: performance
    int i = 0;
    QList<Nepomuk2::Resource>::const_iterator end = d->resources.constEnd();
    for ( QList<Nepomuk2::Resource>::const_iterator it = d->resources.constBegin(); it != end; ++it ) {
        if ( *it == res ) {
            return index( i, 0 );
        }
        ++i;
    }

    return QModelIndex();
}


Nepomuk2::Resource Nepomuk2::Utils::SimpleResourceModel::resourceForIndex( const QModelIndex& index ) const
{
    if ( index.isValid() && index.row() < d->resources.count() ) {
        return d->resources[index.row()];
    }
    else {
        return Resource();
    }
}


int Nepomuk2::Utils::SimpleResourceModel::rowCount( const QModelIndex& parent ) const
{
    if ( parent.isValid() ) {
        return 0;
    }
    else {
        return d->resources.count();
    }
}


QModelIndex Nepomuk2::Utils::SimpleResourceModel::index( int row, int column, const QModelIndex& parent ) const
{
    if ( !parent.isValid() && row < d->resources.count() ) {
        return createIndex( row, column, 0 );
    }
    else {
        return QModelIndex();
    }
}


bool Nepomuk2::Utils::SimpleResourceModel::removeRows(int row, int count, const QModelIndex& parent)
{
    if( count < 1 || row < 0 || (row + count) > d->resources.size() || parent.isValid() )
        return false;

    beginRemoveRows( parent, row, row + count -1 );

    QList<Resource>::iterator begin, end;
    begin = end = d->resources.begin();
    begin += row;
    end += row + count;
    d->resources.erase( begin, end );

    endRemoveRows();
    return true;
}


void Nepomuk2::Utils::SimpleResourceModel::setResources( const QList<Nepomuk2::Resource>& resources )
{
    d->resources = resources;
    reset();
}


void Nepomuk2::Utils::SimpleResourceModel::addResources( const QList<Nepomuk2::Resource>& resources )
{
    if(!resources.isEmpty()) {
        beginInsertRows( QModelIndex(), d->resources.count(), d->resources.count() + resources.count() - 1 );
        d->resources << resources;
        endInsertRows();
    }
}


void Nepomuk2::Utils::SimpleResourceModel::addResource( const Nepomuk2::Resource& resource )
{
    addResources( QList<Resource>() << resource );
}


void Nepomuk2::Utils::SimpleResourceModel::setResults( const QList<Nepomuk2::Query::Result>& results)
{
    clear();
    addResults( results );
}

void Nepomuk2::Utils::SimpleResourceModel::addResults( const QList<Nepomuk2::Query::Result>& results )
{
    Q_FOREACH( const Query::Result& result, results ) {
        addResource( result.resource() );
    }
}

void Nepomuk2::Utils::SimpleResourceModel::addResult( const Nepomuk2::Query::Result result )
{
    addResource( result.resource() );
}


void Nepomuk2::Utils::SimpleResourceModel::clear()
{
    d->resources.clear();
    reset();
}

#include "simpleresourcemodel.moc"