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.
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
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
21
from PyQt4.QtCore import QAbstractListModel, QModelIndex, pyqtSlot
24
class CollectionsModel(QAbstractListModel):
26
ROLE_SOURCE = "_source"
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"
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)
40
ROLES = dict(enumerate(COLUMNS))
42
def __init__(self, collections):
43
QAbstractListModel.__init__(self)
44
self.setRoleNames(CollectionsModel.ROLES)
45
self._collections = collections
47
self._ignore_reload = False
48
collections.collectionAdded.connect(self.collection_added)
51
def _update_row(self, i):
52
self.dataChanged.emit(
53
self.createIndex(i, 0),
54
self.createIndex(i, 0))
57
def collection_added(self, i, source):
59
self.beginInsertRows(QModelIndex(), i, i)
63
def collection_changed(self, i, collection):
68
self.beginResetModel()
73
def update_collections(self):
74
print("update collections")
75
if self._ignore_reload:
76
self._ignore_reload = False
80
collections = self._collections.list()
81
self._count = len(collections)
82
self.beginInsertRows(QModelIndex(), 0, self.rowCount()-1)
89
self._ignore_reload = True
90
self.beginRemoveRows(QModelIndex(), i, i)
94
@pyqtSlot(str, str, result=bool)
95
def state_is(self, state, substate):
97
substates = str(substate).split("|")
103
# QAbstractListModel compliance code
104
def rowCount(self, parent=QModelIndex()):
107
def data(self, index, role, roles=ROLES):
108
if not index.isValid():
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:
137
elif collection.hidden:
139
return _("Not connected")