coding utf-8 from PyQt4 import QtCore QtGui from Track import Track fr

  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
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from Track import Track
from SQLCollection import Cached
class TreeItem:
parent = None
children = []
data = None
root = False
row = None
def __init__(self, parent, children, data, root = False):
self.parent = parent
self.children = children
self.data = data
self.root = root
def make_collection_tree():
db = Cached()
query = "select distinct artist from files order by artist"
artists = db.files._connection.queryAll(query)
root = TreeItem(None, [], None, True)
for artist in artists:
art = TreeItem(root, [], artist[0], False)
root.children.append(art)
#query = "select * from files where artist = '"+artist[0]+"'"
#tracks = db.files._connection.queryAll(query)
files = db.files
tracks = files.select(files.q.artist == artist[0])
for track in tracks:
trk = TreeItem(art, None, track.title, False)
art.children.append(trk)
return root
class CollectionModel(QtCore.QAbstractItemModel):
db = Cached()
root = make_collection_tree()
def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
if role != QtCore.Qt.DisplayRole :
return QtCore.QVariant()
if self.hasIndex(index.row(), index.column(), index):
if not index.parent().isValid():
return QtCore.QVariant(self.root.children[index.row()].data)
else:
return QtCore.QVariant()
return QtCore.QVariant(index.internalPointer().data)
def headerData(self, section, orientation, role):
if role != QtCore.Qt.DisplayRole:
return QtCore.QVariant()
if orientation == QtCore.Qt.Horizontal:
return QtCore.QVariant('Collection')
if orientation == QtCore.Qt.Vertical:
return QtCore.QVariant()
#done
def index(self, row, column, parent = QtCore.QModelIndex()):
if not self.hasIndex(row, column, parent):
return QtCore.QModelIndex()
if not parent.isValid():
return self.createIndex(row, column, self.root.children[row])
#query = "select distinct artist from files order by artist"
#item = self.db.files._connection.queryAll(query)[row]
#return self.createIndex(row, column, item)
else:
if parent.internalPointer().children:
return self.createIndex(row, column, parent.internalPointer().children[row])
return self.createIndex(row, column, "nothing")
def parent(self, index):
#return index.internalPointer()
if not index.isValid():
return QModelIndex()
else:
child = index.internalPointer()
parent = child.parent
if parent:
row = 0
if not parent.root:
row = parent.parent.children.index(parent)
return self.createIndex(row,0,parent)
#тут надо вернуть индекс исполнителя, если этот элемент трек
#или вернуть QtCore.QModelIndex() если это исполнитель
return QtCore.QModelIndex()
def rowCount(self, parent = QtCore.QModelIndex()):
if not parent.isValid():
return len(self.root.children)
#query = "select distinct artist from files order by artist"
#items = self.db.files._connection.queryAll(query)
#return len(items)#self.db.files.select().count()
else:
par = parent.internalPointer()
if par:
if par.children:
return len(par.children)
else: return 0
else: return 0
#artist = str(parent.data().toString())
#files = self.db.files
#return files.select(files.q.artist==artist).count()
#done
def columnCount(self, parent = QtCore.QModelIndex()):
return 1
#done
class Collec1tionModel:
def columnCount(self, *args):
return 1
def rowCount(self, parent = QtCore.QModelIndex()):
pass