forked from foprea/Jeopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerTableWidget.py
182 lines (142 loc) · 6.21 KB
/
PlayerTableWidget.py
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
"""
PlayerTable class contains the table displayed at various times throughout Jeopy.
One of these tables is displayed in the PlayerAdminDialog and the others in the Gui (both Player and Admin) and finally it supports colors when drawing the plot at the end.
QTableWidgets are not very cooperative. They need to be specifically forced to a
certain size and the table needs to know that its holding ints, otherwise when
enabling sorting it will do a lexicographic sort, which makes no sense for numbers.
"""
"""
When adding and updating players from a PlayerTable with add() or update(),
the table expects a tuple, that resembles the one used in the GameServer's player
dictionary. Instead of the first element of the tuple being _uri_, it is _name_.
AdminGui:
(name, ip, status, score)
[0] [1] [2] [3]
PlayerGui:
(name, score)
[0] [1]
Invariant:
If our tuple is called player, the sorting is always done after
player[len(player) - 1] which is always the score and contains an int!
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class PlayerTable(QWidget):
playersMuted = pyqtSignal(list)
playersUnmuted = pyqtSignal(list)
"""
There are two basic types of table - the ones displayed in the AdminGui which also contain buttons for muting and unmuting and the ones used in all other cases which don't have any buttons.
"""
def __init__(self, labels, buttonText, parent = None):
super(PlayerTable, self).__init__(parent)
self.setupGui(labels, buttonText)
if buttonText != '':
self.layout().itemAtPosition(1, 0).widget().clicked.connect(self.mutePlayers)
# this is legacy! banning is no longer permitted during login
# actually working to fix this
if buttonText != 'ban':
self.layout().itemAtPosition(1, 1).widget().clicked.connect(self.unmutePlayers)
def addPlayer(self, player):
table = self.getTable()
table.insertRow(0)
for i in range(len(player)):
if i != len(player)-1:
table.setItem(0, i, QTableWidgetItem(str(player[i])))
else:
newItem=QTableWidgetItem()
newItem.setData(0, (player[i]))
table.setItem(0, i, newItem)
table.sortItems(len(player) - 1, Qt.DescendingOrder)
self.updateTableWidth()
table.setEditTriggers(QAbstractItemView.NoEditTriggers)
def updatePlayer(self, player):
table = self.getTable()
name = player[0]
print 'admintable updating', player
table.setSortingEnabled(False)
for r in range(table.rowCount()):
#print table.itemAt(r, 0).text()
if table.item(r, 0).text() == name:
for i in range(1, len(player)):
if i != len(player)-1:
table.setItem(r, i, QTableWidgetItem(str(player[i])))
else:
newItem=QTableWidgetItem()
newItem.setData(0, (player[i]))
table.setItem(r , i, newItem)
break
table.setSortingEnabled(True)
table.sortItems(len(player) - 1, Qt.DescendingOrder)
self.updateTableWidth()
#table.setEditTriggers(QAbstractItemView.NoEditTriggers)
def setupGui(self, labels, buttonText):
layout = QGridLayout()
self.setLayout(layout)
table = QTableWidget()
# starting at line 0 and column 0, taking up 1 line and 2 columns
# this way we make sure that buttons (if there are any are properly aligned)
layout.addWidget(table, 0, 0, 1, 2)
table.setColumnCount(len(labels))
table.setHorizontalHeaderLabels(labels)
table.setSortingEnabled(True)
table.setShowGrid(False)
table.setEditTriggers(QAbstractItemView.NoEditTriggers)
table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
table.horizontalHeader().setSortIndicatorShown(True)
self.updateTableWidth()
if buttonText != '':
if buttonText != 'ban':
layout.addWidget(QPushButton(buttonText.title()), 1, 0)
layout.addWidget(QPushButton('Un' + buttonText), 1, 1)
else:
layout.addWidget(QPushButton(buttonText.title()), 1, 0, 1, 2)
def getTable(self):
return self.layout().itemAt(0).widget()
def getTableWidth(self):
"""
set the appropriate table width! needs to work properly!
currently works but doesn't display sort indicator... maybe I'll come up with
something better when I stop hating it.
"""
table = self.getTable()
width = 0
table.resizeColumnsToContents()
for c in range(table.columnCount()):
if table.horizontalHeader().isSectionHidden(c):
continue
width += max(table.columnWidth(c),
table.horizontalHeader().sectionSize(c))
print table.columnWidth(c), table.horizontalHeader().sectionSize(c)
return width
def updateTableWidth(self):
self.getTable().setFixedWidth(self.getTableWidth())
def getSelected(self):
return [str(item.text()) for item in self.getTable().selectedItems()]
def mutePlayers(self):
self.playersMuted.emit(self.getSelected())
def unmutePlayers(self):
self.playersUnmuted.emit(self.getSelected())
def hideButtons(self):
self.layout().itemAt(1).widget().hide()
self.layout().itemAt(2).widget().hide()
def showColors(self, colors):
"""
add a column for colors computed according to the colors array.
@param colors: each tuple contains red green blue alpha information
@type colors: dictionary of names to tuples
"""
table = self.getTable()
table.setColumnCount(table.columnCount() + 1)
c = table.columnCount() - 1
table.setHorizontalHeaderItem(c, QTableWidgetItem('Color'))
if c == 4:
table.horizontalHeader().hideSection(1)
table.horizontalHeader().hideSection(2)
color = QColor()
for row in range(table.rowCount()):
(r, g, b, a) = colors[str(table.item(row, 0).text())]
color.setRgbF(r, g, b, a)
item = QTableWidgetItem()
item.setBackground(QBrush(QColor(color)))
table.setItem(row, c, item)
self.updateTableWidth()