~ubuntu-branches/ubuntu/saucy/plasma-nm/saucy-proposed

« back to all changes in this revision

Viewing changes to declarative-plugins/model/sortmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-08-16 19:07:09 UTC
  • Revision ID: package-import@ubuntu.com-20130816190709-ef9ydm9skigmg15l
Tags: upstream-0.0~git20130816
ImportĀ upstreamĀ versionĀ 0.0~git20130816

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2013 Jan Grulich <jgrulich@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) version 3, or any
 
8
    later version accepted by the membership of KDE e.V. (or its
 
9
    successor approved by the membership of KDE e.V.), which shall
 
10
    act as a proxy defined in Section 6 of version 3 of the license.
 
11
 
 
12
    This library is distributed in the hope that it will be useful,
 
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
    Lesser General Public License for more details.
 
16
 
 
17
    You should have received a copy of the GNU Lesser General Public
 
18
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
19
*/
 
20
 
 
21
#include "sortmodel.h"
 
22
#include "model.h"
 
23
 
 
24
SortModel::SortedConnectionType SortModel::connectionTypeToSortedType(NetworkManager::ConnectionSettings::ConnectionType type)
 
25
{
 
26
    switch (type) {
 
27
        case NetworkManager::ConnectionSettings::Unknown:
 
28
            return SortModel::SortModel::Unknown;
 
29
            break;
 
30
        case NetworkManager::ConnectionSettings::Adsl:
 
31
            return SortModel::SortModel::Adsl;
 
32
            break;
 
33
        case NetworkManager::ConnectionSettings::Bluetooth:
 
34
            return SortModel::Bluetooth;
 
35
            break;
 
36
        case NetworkManager::ConnectionSettings::Bond:
 
37
            return SortModel::Bond;
 
38
            break;
 
39
        case NetworkManager::ConnectionSettings::Bridge:
 
40
            return SortModel::Bridge;
 
41
            break;
 
42
        case NetworkManager::ConnectionSettings::Cdma:
 
43
            return SortModel::Cdma;
 
44
            break;
 
45
        case NetworkManager::ConnectionSettings::Gsm:
 
46
            return SortModel::Gsm;
 
47
            break;
 
48
        case NetworkManager::ConnectionSettings::Infiniband:
 
49
            return SortModel::Infiniband;
 
50
            break;
 
51
        case NetworkManager::ConnectionSettings::OLPCMesh:
 
52
            return SortModel::OLPCMesh;
 
53
            break;
 
54
        case NetworkManager::ConnectionSettings::Pppoe:
 
55
            return SortModel::Pppoe;
 
56
            break;
 
57
        case NetworkManager::ConnectionSettings::Vlan:
 
58
            return SortModel::Vlan;
 
59
            break;
 
60
        case NetworkManager::ConnectionSettings::Vpn:
 
61
            return SortModel::Vpn;
 
62
            break;
 
63
        case NetworkManager::ConnectionSettings::Wimax:
 
64
            return SortModel::Wimax;
 
65
            break;
 
66
        case NetworkManager::ConnectionSettings::Wired:
 
67
            return SortModel::Wired;
 
68
            break;
 
69
        case NetworkManager::ConnectionSettings::Wireless:
 
70
            return SortModel::Wireless;
 
71
            break;
 
72
        default:
 
73
            return SortModel::Unknown;
 
74
            break;
 
75
    }
 
76
}
 
77
 
 
78
SortModel::SortModel(QObject* parent):
 
79
    QSortFilterProxyModel(parent)
 
80
{
 
81
    setDynamicSortFilter(true);
 
82
    sort(0, Qt::DescendingOrder);
 
83
}
 
84
 
 
85
SortModel::~SortModel()
 
86
{
 
87
}
 
88
 
 
89
void SortModel::setSourceModel(QAbstractItemModel* sourceModel)
 
90
{
 
91
    QSortFilterProxyModel::setSourceModel(sourceModel);
 
92
}
 
93
 
 
94
QAbstractItemModel* SortModel::sourceModel() const
 
95
{
 
96
    return QSortFilterProxyModel::sourceModel();
 
97
}
 
98
 
 
99
bool SortModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
 
100
{
 
101
    const bool leftConnected = sourceModel()->data(left, Model::ConnectedRole).toBool();
 
102
    const SortedConnectionType leftType = connectionTypeToSortedType((NetworkManager::ConnectionSettings::ConnectionType) sourceModel()->data(left, Model::TypeRole).toUInt());
 
103
    const QString leftUuid = sourceModel()->data(left, Model::UuidRole).toString();
 
104
    const int leftSignal = sourceModel()->data(left, Model::SignalRole).toInt();
 
105
 
 
106
    const bool rightConnected = sourceModel()->data(right, Model::ConnectedRole).toBool();
 
107
    const SortedConnectionType rightType = connectionTypeToSortedType((NetworkManager::ConnectionSettings::ConnectionType) sourceModel()->data(right, Model::TypeRole).toUInt());
 
108
    const QString rightUuid = sourceModel()->data(right, Model::UuidRole).toString();
 
109
    const int rightSignal = sourceModel()->data(right, Model::SignalRole).toInt();
 
110
 
 
111
    if (leftConnected < rightConnected) {
 
112
        return true;
 
113
    } else if (leftConnected > rightConnected) {
 
114
        return false;
 
115
    }
 
116
 
 
117
    if (leftUuid.isEmpty() && !rightUuid.isEmpty()) {
 
118
        return true;
 
119
    } else if (!leftUuid.isEmpty() && rightUuid.isEmpty()) {
 
120
        return false;
 
121
    }
 
122
 
 
123
    if (leftType < rightType) {
 
124
        return false;
 
125
    } else if (leftType > rightType) {
 
126
        return true;
 
127
    }
 
128
 
 
129
    if (leftSignal < rightSignal) {
 
130
        return true;
 
131
    } else {
 
132
        return false;
 
133
    }
 
134
 
 
135
    return false;
 
136
}