~neon/project-neon/kscreen

« back to all changes in this revision

Viewing changes to kcm/src/modesproxymodel.cpp

  • Committer: Dan Vrátil
  • Date: 2012-11-13 11:22:10 UTC
  • mfrom: (0.1.48)
  • Revision ID: git-v1:14fdbb224dc8a85b623b394662458a62675e977c
Merge branch 'kcm_import'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2012  Dan Vratil <dvratil@redhat.com>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Lesser General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2.1 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
    Lesser General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Lesser General Public
 
15
    License along with this library; if not, write to the Free Software
 
16
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
17
*/
 
18
 
 
19
#include "modesproxymodel.h"
 
20
#include "qmloutput.h"
 
21
 
 
22
#include <KComboBox>
 
23
#include <KDebug>
 
24
#include <KLocalizedString>
 
25
 
 
26
ModesProxyModel::ModesProxyModel(QObject* parent):
 
27
    QSortFilterProxyModel(parent),
 
28
    m_currentSourceRow(-1)
 
29
{
 
30
    setSortRole(QMLOutput::RefreshRateRole);
 
31
    setDynamicSortFilter(true);
 
32
}
 
33
 
 
34
ModesProxyModel::~ModesProxyModel()
 
35
{
 
36
 
 
37
}
 
38
 
 
39
int ModesProxyModel::columnCount(const QModelIndex& parent) const
 
40
{
 
41
    return 1;
 
42
}
 
43
 
 
44
int ModesProxyModel::rowCount(const QModelIndex& parent) const
 
45
{
 
46
    if (!sourceModel()) {
 
47
        return 0;
 
48
    }
 
49
 
 
50
    QModelIndex parentIndex = sourceModel()->index(m_currentSourceRow, 0);
 
51
    return sourceModel()->rowCount(parentIndex) + 1;
 
52
}
 
53
 
 
54
QVariant ModesProxyModel::data(const QModelIndex& index, int role) const
 
55
{
 
56
    if (!sourceModel() || !index.isValid()) {
 
57
        return QVariant();
 
58
    }
 
59
 
 
60
    if (index.row() == 0) {
 
61
        if (role == Qt::DisplayRole) {
 
62
            return i18n("Auto");
 
63
        } else if (role == QMLOutput::RefreshRateRole) {
 
64
            return -1;
 
65
        } else if (role == QMLOutput::ModeIdRole) {
 
66
            return -1;
 
67
        } else {
 
68
            return QVariant();
 
69
        }
 
70
    }
 
71
 
 
72
    QModelIndex parentIndex = sourceModel()->index(m_currentSourceRow, 0);
 
73
    QModelIndex realIndex = parentIndex.child(index.row() - 1, 0);
 
74
    return sourceModel()->data(realIndex, role);
 
75
}
 
76
 
 
77
QModelIndex ModesProxyModel::index(int row, int column, const QModelIndex& parent) const
 
78
{
 
79
    return createIndex(row, column, 0);
 
80
}
 
81
 
 
82
QModelIndex ModesProxyModel::parent(const QModelIndex& child) const
 
83
{
 
84
    /* Flatten the model */
 
85
    return QModelIndex();
 
86
}
 
87
 
 
88
QModelIndex ModesProxyModel::mapToSource(const QModelIndex& proxyIndex) const
 
89
{
 
90
    if (!sourceModel() || !proxyIndex.isValid()) {
 
91
        return QModelIndex();
 
92
    }
 
93
 
 
94
    QModelIndex realParentIndex = sourceModel()->index(m_currentSourceRow, 0);
 
95
 
 
96
    if (proxyIndex.row() == 0) {
 
97
      return realParentIndex.child(proxyIndex.row(), 0);
 
98
    }
 
99
 
 
100
    return realParentIndex.child(proxyIndex.row() - 1, 0);
 
101
}
 
102
 
 
103
bool ModesProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
 
104
{
 
105
    float rateA = left.data(QMLOutput::RefreshRateRole).toFloat();
 
106
    float rateB = right.data(QMLOutput::RefreshRateRole).toFloat();
 
107
 
 
108
    if (rateA == -1) {
 
109
        return true;
 
110
    }
 
111
 
 
112
    return (rateA < rateB);
 
113
}
 
114
 
 
115
void ModesProxyModel::setSourceModelCurrentRow(int index)
 
116
{
 
117
    m_currentSourceRow = index;
 
118
}