class Result object Search result class def __init__ self data self __

  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
class Result(object):
"""
Search result class
"""
def __init__(self, data):
self.__dict__.update(data)
# Adding DC++ user to path
if self.prot == "dchub":
self.path = "%s/%s" % (self.dcuser, self.path)
# Found domainname match
self.path = getattr(self, 'path', '/')
if getattr(self, 'ext', None):
self.ext = self.ext.strip().upper()
else:
# Item is a directory
self.ext = ''
self.name = self.path.split('/')[-1]
# No <name> like in /?q=123#n470
if not getattr(self, 'name', None):
# Trying to get name from fullpath
# FIXME: can there be an exc?
self.name = re.findall(
r'.+/(.+)\.',
self.full
)[0]
self.size = int(self.size) \
if getattr(self, 'size', None) else 0
class ResultContainer():
"""
Container for resutl list
"""
def __init__(self, resutl):
super(ResultContainer, self)__init__()
self.result = result
def saveData():
"""
Разделение результата для последующей
передачи в модель
"""
self.data = []
if result is None:
return []
else:
self.size = filesize.size(
result.size, system=filesize.alternative)
self.item = dict(full=result.full, name=result.name,
ext=result.ext, size=self.size)
self.data.append(self.item)
def resCount(self):
"""
Return a nuber of results
"""
return len(self.data)
def getData4Row(self, row):
"""
Getting data from list
"""
return self.data[row].values()
# def getData(self):
def iconSet(self, row):
"""
Set icon
"""
# Шаблон расширений
template = dict(
iso = ['iso', 'nrg', 'vcd'],
image = ['jpg', 'jpeg', 'bmp', 'png'],
archiv = ['rar', 'tar', 'zip', '7z', 'gz', 'arj']
)
#FIXME: Не совсем уверен что
#самый простой способ
for i in len(template.keys()):
for ext in template.values()[i]:
if self.data[row].values()[2].lower() == ext:
"""
Возвращает QIcon объект
"""
return QIcon('icons/search/%s.png' % ext)
class ResultModel(QAbstractItemModel):
"""
Model
"""
def __init__(self):
super(ResultModel, self).__init__()
self.cont = ResultContainer()
def data(self, index, role = Qt.DisplayRole):
if not index.isValid():
return QVariant()
col = index.column()
row = index.row()
if role == Qt.DisplayRole:
#Get data to row
item = self.cont.getData4Row(row)
# Icon set
if col == 0:
return self.cont.iconSet(row)
return QVariant(item[col])
return QVariant()
def rowCount(self, index=QModelIndex()):
return self.cont.resCount()
def columnCount(self, index=QModelIndex()):
return 5
class ResultView(QTreeView):
"""
Result output
"""
def __init__(self, parent = None):
QTreeView.__init__(self, parent)
self.setSelection(QItemSelectionModel.SelectRows)
self.setWordWrap(True)
self.model = ResultModel()
self.setModel(self.model)