~mmcg069/totallywired/trunk

« back to all changes in this revision

Viewing changes to totallywired/models/indexes.py

  • Committer: Matthew McGowan
  • Date: 2013-04-15 22:32:54 UTC
  • Revision ID: matthew.joseph.mcgowan@gmail.com-20130415223254-0cpu3j3jl7ldlbm8
slight refactoring of some code

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Authors:
3
 
#  Michael Vogt
4
 
#  Ollivier Tilloy
5
 
#  Matthew McGowan
6
 
#
7
 
# This program is free software; you can redistribute it and/or modify it under
8
 
# the terms of the GNU General Public License as published by the Free Software
9
 
# Foundation; version 3.
10
 
#
11
 
# This program is distributed in the hope that it will be useful, but WITHOUT
12
 
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14
 
# details.
15
 
#
16
 
# You should have received a copy of the GNU General Public License along with
17
 
# this program; if not, write to the Free Software Foundation, Inc.,
18
 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 
 
20
 
 
21
 
from PyQt4.QtCore import QAbstractListModel, QModelIndex, pyqtSlot
22
 
 
23
 
 
24
 
class IndexModel(QAbstractListModel):
25
 
 
26
 
    ROLE_SOURCE = "_source"
27
 
    ROLE_INDEX = "_index"
28
 
    ROLE_STATE = "_state"
29
 
    ROLE_PROGRESS = "_progress"
30
 
    ROLE_SRC_TYPE = "_srctype"
31
 
    ROLE_SRC_NAME = "_srcname"
32
 
    ROLE_SRC_ICON = "_srcicon"
33
 
    ROLE_DOCCOUNT = "_doccount"
34
 
    ROLE_DISPL_TEXT = "_displ_text"
35
 
 
36
 
    COLUMNS = (ROLE_SOURCE, ROLE_INDEX, ROLE_STATE, ROLE_PROGRESS,
37
 
               ROLE_DOCCOUNT, ROLE_SRC_TYPE, ROLE_SRC_NAME,
38
 
               ROLE_SRC_ICON, ROLE_DISPL_TEXT)
39
 
 
40
 
    ROLES = dict(enumerate(COLUMNS))
41
 
 
42
 
    def __init__(self, collections):
43
 
        QAbstractListModel.__init__(self)
44
 
        self.setRoleNames(IndexModel.ROLES)
45
 
        self.collections = collections
46
 
        self._count = 0
47
 
        self._ignore_reload = False
48
 
        self.update_collections()
49
 
        collections.collectionAdded.connect(self.collection_added)
50
 
        return
51
 
 
52
 
    def _update_row(self, i):
53
 
        self.dataChanged.emit(
54
 
            self.createIndex(i, 0),
55
 
            self.createIndex(i, 0))
56
 
        return
57
 
 
58
 
    def collection_added(self, i, source):
59
 
        self._count += 1
60
 
        self.beginInsertRows(QModelIndex(), i, i)
61
 
        self.endInsertRows()
62
 
        return
63
 
 
64
 
    def collection_changed(self, i, collection):
65
 
        self._update_row(i)
66
 
        return
67
 
 
68
 
    def clear(self):
69
 
        self.beginResetModel()
70
 
        self._count = 0
71
 
        self.endResetModel()
72
 
        return
73
 
 
74
 
    def update_collections(self):
75
 
        if self._ignore_reload:
76
 
            self._ignore_reload = False
77
 
            return
78
 
        self.clear()
79
 
        indexes = self.collections.list()
80
 
        self._count = len(indexes)
81
 
        self.beginInsertRows(QModelIndex(), 0, self.rowCount()-1)
82
 
        self.endInsertRows()
83
 
        return
84
 
 
85
 
    @pyqtSlot(int)
86
 
    def remove_index(self, i):
87
 
        self._count -= 1
88
 
        self._ignore_reload = True
89
 
        self.beginRemoveRows(QModelIndex(), i, i)
90
 
        self.endRemoveRows()
91
 
        return
92
 
 
93
 
    @pyqtSlot(str, str, result=bool)
94
 
    def state_is(self, state, substate):
95
 
        state = str(state)
96
 
        substates = str(substate).split("|")
97
 
        for s in substates:
98
 
            if s in state:
99
 
                return True
100
 
        return False
101
 
 
102
 
    # QAbstractListModel compliance code
103
 
    def rowCount(self, parent=QModelIndex()):
104
 
        return self._count
105
 
 
106
 
    def data(self, index, role, roles=ROLES):
107
 
        if not index.isValid():
108
 
            return None
109
 
 
110
 
        rolev = self.ROLES[role]
111
 
        collection = self.collections.list()[index.row()]
112
 
 
113
 
        if rolev == self.ROLE_SOURCE:
114
 
            if not collection.source:
115
 
                return _("Not inserted")
116
 
            return collection.source
117
 
        elif rolev == self.ROLE_INDEX:
118
 
            return collection.xpath
119
 
        elif rolev == self.ROLE_STATE:
120
 
            return collection.state
121
 
        elif rolev == self.ROLE_PROGRESS:
122
 
            return collection.progress
123
 
        elif rolev == self.ROLE_SRC_TYPE:
124
 
            if collection.removable:
125
 
                return _("Removable Sources")
126
 
            return _("Home Sources")
127
 
        elif rolev == self.ROLE_SRC_NAME:
128
 
            return collection.title
129
 
        elif rolev == self.ROLE_SRC_ICON:
130
 
            return collection.icon_source
131
 
        elif rolev == self.ROLE_DOCCOUNT:
132
 
            return collection.doccount
133
 
        elif rolev == self.ROLE_DISPL_TEXT:
134
 
            if collection.progress != -1:
135
 
                return "%s %.2f%%" % (_("Working..."), round(collection.progress * 100, 2))
136
 
            elif collection.live:
137
 
                return _("Active")
138
 
            elif collection.hidden:
139
 
                return _("Hidden")
140
 
            return _("Not connected")
141
 
        return None