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 IndexModel(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(IndexModel.ROLES)
45
self.collections = collections
47
self._ignore_reload = False
48
self.update_collections()
49
collections.collectionAdded.connect(self.collection_added)
52
def _update_row(self, i):
53
self.dataChanged.emit(
54
self.createIndex(i, 0),
55
self.createIndex(i, 0))
58
def collection_added(self, i, source):
60
self.beginInsertRows(QModelIndex(), i, i)
64
def collection_changed(self, i, collection):
69
self.beginResetModel()
74
def update_collections(self):
75
if self._ignore_reload:
76
self._ignore_reload = False
79
indexes = self.collections.list()
80
self._count = len(indexes)
81
self.beginInsertRows(QModelIndex(), 0, self.rowCount()-1)
86
def remove_index(self, i):
88
self._ignore_reload = True
89
self.beginRemoveRows(QModelIndex(), i, i)
93
@pyqtSlot(str, str, result=bool)
94
def state_is(self, state, substate):
96
substates = str(substate).split("|")
102
# QAbstractListModel compliance code
103
def rowCount(self, parent=QModelIndex()):
106
def data(self, index, role, roles=ROLES):
107
if not index.isValid():
110
rolev = self.ROLES[role]
111
collection = self.collections.list()[index.row()]
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:
138
elif collection.hidden:
140
return _("Not connected")