Repository navigation
Expand file tree
/
Copy pathAccounts.py
More file actions
213 lines (182 loc) · 6.75 KB
/
Copy pathAccounts.py
File metadata and controls
213 lines (182 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
@author: mkaay
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from time import strftime, gmtime
class AccountModel(QAbstractItemModel):
"""
model for account view
"""
def __init__(self, view, connector):
QAbstractItemModel.__init__(self)
self.connector = connector
self.view = view
self._data = []
self.cols = 4
self.mutex = QMutex()
def reloadData(self, force=False):
"""
reload account list
"""
accounts = self.connector.proxy.getAccounts(False)
if self._data == accounts:
return
if len(self._data) > 0:
self.beginRemoveRows(QModelIndex(), 0, len(self._data)-1)
self._data = []
self.endRemoveRows()
if len(accounts) > 0:
self.beginInsertRows(QModelIndex(), 0, len(accounts)-1)
self._data = accounts
self.endInsertRows()
def toData(self, index):
"""
return index pointer
"""
return index.internalPointer()
def data(self, index, role=Qt.DisplayRole):
"""
return cell data
"""
if not index.isValid():
return QVariant()
if role == Qt.DisplayRole:
if index.column() == 0:
return QVariant(self.toData(index).type)
elif index.column() == 1:
return QVariant(self.toData(index).login)
elif index.column() == 2:
if not self.toData(index).valid:
return QVariant(_("not valid"))
if not self.toData(index).validuntil:
return QVariant(_("n/a"))
until = int(self.toData(index).validuntil)
if until > 0:
fmtime = strftime(_("%a, %d %b %Y %H:%M"), gmtime(until))
return QVariant(fmtime)
else:
return QVariant(_("unlimited"))
#elif role == Qt.EditRole:
# if index.column() == 0:
# return QVariant(index.internalPointer().data["name"])
return QVariant()
def index(self, row, column, parent=QModelIndex()):
"""
create index with data pointer
"""
if parent == QModelIndex() and len(self._data) > row:
pointer = self._data[row]
index = self.createIndex(row, column, pointer)
elif parent.isValid():
pointer = parent.internalPointer().children[row]
index = self.createIndex(row, column, pointer)
else:
index = QModelIndex()
return index
def parent(self, index):
"""
no parents, everything on top level
"""
return QModelIndex()
def rowCount(self, parent=QModelIndex()):
"""
account count
"""
if parent == QModelIndex():
return len(self._data)
return 0
def columnCount(self, parent=QModelIndex()):
return self.cols
def hasChildren(self, parent=QModelIndex()):
"""
everything on top level
"""
if parent == QModelIndex():
return True
else:
return False
def canFetchMore(self, parent):
return False
def headerData(self, section, orientation, role=Qt.DisplayRole):
"""
returns column heading
"""
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
if section == 0:
return QVariant(_("Type"))
elif section == 1:
return QVariant(_("Login"))
elif section == 2:
return QVariant(_("Valid until"))
elif section == 3:
return QVariant(_("Traffic left"))
return QVariant()
def flags(self, index):
"""
cell flags
"""
return Qt.ItemIsSelectable | Qt.ItemIsEnabled
class AccountView(QTreeView):
"""
view component for accounts
"""
def __init__(self, connector):
QTreeView.__init__(self)
self.setModel(AccountModel(self, connector))
self.setColumnWidth(0, 150)
self.setColumnWidth(1, 150)
self.setColumnWidth(2, 150)
self.setColumnWidth(3, 150)
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.delegate = AccountDelegate(self, self.model())
self.setItemDelegateForColumn(3, self.delegate)
class AccountDelegate(QItemDelegate):
"""
used to display a progressbar for the traffic in the traffic cell
"""
def __init__(self, parent, model):
QItemDelegate.__init__(self, parent)
self.model = model
def paint(self, painter, option, index):
"""
paint the progressbar
"""
if not index.isValid():
return
if index.column() == 3:
data = self.model.toData(index)
opts = QStyleOptionProgressBarV2()
opts.minimum = 0
if data.trafficleft:
if data.trafficleft == -1 or data.trafficleft is None:
opts.maximum = opts.progress = 1
else:
opts.maximum = opts.progress = data.trafficleft
if data.maxtraffic:
opts.maximum = data.maxtraffic
opts.rect = option.rect
opts.rect.setRight(option.rect.right()-1)
opts.rect.setHeight(option.rect.height()-1)
opts.textVisible = True
opts.textAlignment = Qt.AlignCenter
if data.trafficleft and data.trafficleft == -1:
opts.text = QString(_("unlimited"))
elif data.trafficleft is None:
opts.text = QString(_("n/a"))
else:
opts.text = QString.number(round(float(opts.progress)/1024/1024, 2)) + " GB"
QApplication.style().drawControl(QStyle.CE_ProgressBar, opts, painter)
return
QItemDelegate.paint(self, painter, option, index)