~ubuntu-branches/ubuntu/natty/plasma-mobile/natty

« back to all changes in this revision

Viewing changes to containments/mobilelauncher/models/pagedproxymodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Belem
  • Date: 2011-01-01 16:58:27 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20110101165827-vij700zj247uxcdr
Tags: 0.0~svn20110101-0ubuntu1
* New svn snapshot
* Remove kubuntu_01_library_version.diff and
  debian-changes-0.0~svn20100830-0ubuntu4, they don't apply and aren't
  needed in the current snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2010 Marco Martin <notmart@gmail.com>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Library General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2 of the License, or (at your option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    Library General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to
 
16
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "pagedproxymodel.h"
 
21
 
 
22
#include <KDebug>
 
23
 
 
24
PagedProxyModel::PagedProxyModel(QObject *parent)
 
25
    : QProxyModel(parent),
 
26
      m_pageSize(16),
 
27
      m_currentPage(0)
 
28
{
 
29
}
 
30
 
 
31
PagedProxyModel::~PagedProxyModel()
 
32
{
 
33
}
 
34
 
 
35
int PagedProxyModel::totalPages()
 
36
{
 
37
    if (!model()) {
 
38
        return 0;
 
39
    }
 
40
 
 
41
    return model()->rowCount() / m_pageSize;
 
42
}
 
43
 
 
44
void PagedProxyModel::setCurrentPage(const int page)
 
45
{
 
46
    if (m_currentPage == page) {
 
47
        return;
 
48
    }
 
49
 
 
50
    m_currentPage = page;
 
51
    emit modelReset();
 
52
}
 
53
 
 
54
int PagedProxyModel::currentPage() const
 
55
{
 
56
    return m_currentPage;
 
57
}
 
58
 
 
59
void PagedProxyModel::setPageSize(const int size)
 
60
{
 
61
    if (m_pageSize == size) {
 
62
        return;
 
63
    }
 
64
 
 
65
    m_pageSize = size;
 
66
    emit modelReset();
 
67
}
 
68
 
 
69
int PagedProxyModel::pageSize() const
 
70
{
 
71
    return m_pageSize;
 
72
}
 
73
 
 
74
void PagedProxyModel::setSourceModel(QObject *source)
 
75
{
 
76
    QAbstractItemModel *model = qobject_cast<QAbstractItemModel *>(source);
 
77
    if (!model) {
 
78
        return;
 
79
    }
 
80
    setRoleNames(model->roleNames());
 
81
    setModel(model);
 
82
}
 
83
 
 
84
QObject *PagedProxyModel::sourceModel() const
 
85
{
 
86
    return model();
 
87
}
 
88
 
 
89
 
 
90
int PagedProxyModel::rowCount(const QModelIndex &parent) const
 
91
{
 
92
    return qMin(m_pageSize, (QProxyModel::rowCount(parent)-m_currentPage*m_pageSize));
 
93
}
 
94
 
 
95
QVariant PagedProxyModel::data(const QModelIndex &index, int role) const
 
96
{
 
97
    return QProxyModel::data(QProxyModel::index(index.row()+(m_pageSize*m_currentPage), index.column()), role);
 
98
}
 
99
 
 
100
#include "pagedproxymodel.moc"