~mmcg069/totallywired/trunk

« back to all changes in this revision

Viewing changes to totallywired/models/collections.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 CollectionsModel(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(CollectionsModel.ROLES)
 
45
        self._collections = collections
 
46
        self._count = 0
 
47
        self._ignore_reload = False
 
48
        collections.collectionAdded.connect(self.collection_added)
 
49
        return
 
50
 
 
51
    def _update_row(self, i):
 
52
        self.dataChanged.emit(
 
53
            self.createIndex(i, 0),
 
54
            self.createIndex(i, 0))
 
55
        return
 
56
 
 
57
    def collection_added(self, i, source):
 
58
        self._count += 1
 
59
        self.beginInsertRows(QModelIndex(), i, i)
 
60
        self.endInsertRows()
 
61
        return
 
62
 
 
63
    def collection_changed(self, i, collection):
 
64
        self._update_row(i)
 
65
        return
 
66
 
 
67
    def clear(self):
 
68
        self.beginResetModel()
 
69
        self._count = 0
 
70
        self.endResetModel()
 
71
        return
 
72
 
 
73
    def update_collections(self):
 
74
        print("update collections")
 
75
        if self._ignore_reload:
 
76
            self._ignore_reload = False
 
77
            return
 
78
        print("not ignored")
 
79
        self.clear()
 
80
        collections = self._collections.list()
 
81
        self._count = len(collections)
 
82
        self.beginInsertRows(QModelIndex(), 0, self.rowCount()-1)
 
83
        self.endInsertRows()
 
84
        return
 
85
 
 
86
    @pyqtSlot(int)
 
87
    def remove(self, i):
 
88
        self._count -= 1
 
89
        self._ignore_reload = True
 
90
        self.beginRemoveRows(QModelIndex(), i, i)
 
91
        self.endRemoveRows()
 
92
        return
 
93
 
 
94
    @pyqtSlot(str, str, result=bool)
 
95
    def state_is(self, state, substate):
 
96
        state = str(state)
 
97
        substates = str(substate).split("|")
 
98
        for s in substates:
 
99
            if s in state:
 
100
                return True
 
101
        return False
 
102
 
 
103
    # QAbstractListModel compliance code
 
104
    def rowCount(self, parent=QModelIndex()):
 
105
        return self._count
 
106
 
 
107
    def data(self, index, role, roles=ROLES):
 
108
        if not index.isValid():
 
109
            return None
 
110
        rolev = self.ROLES[role]
 
111
        collection = self._collections.list()[index.row()]
 
112
        if rolev == self.ROLE_SOURCE:
 
113
            if not collection.source:
 
114
                return _("Not inserted")
 
115
            return collection.source
 
116
        elif rolev == self.ROLE_INDEX:
 
117
            return collection.xpath
 
118
        elif rolev == self.ROLE_STATE:
 
119
            return collection.state
 
120
        elif rolev == self.ROLE_PROGRESS:
 
121
            return collection.progress
 
122
        elif rolev == self.ROLE_SRC_TYPE:
 
123
            if collection.removable:
 
124
                return _("Removable Sources")
 
125
            return _("Home Sources")
 
126
        elif rolev == self.ROLE_SRC_NAME:
 
127
            return collection.title
 
128
        elif rolev == self.ROLE_SRC_ICON:
 
129
            return collection.icon_source
 
130
        elif rolev == self.ROLE_DOCCOUNT:
 
131
            return collection.doccount
 
132
        elif rolev == self.ROLE_DISPL_TEXT:
 
133
            if collection.progress != -1:
 
134
                return "%s %.2f%%" % (_("Working..."), round(collection.progress * 100, 2))
 
135
            elif collection.live:
 
136
                return _("Active")
 
137
            elif collection.hidden:
 
138
                return _("Hidden")
 
139
            return _("Not connected")
 
140
        return None