From c93742a3d7431385391529cc7898f1f78727ff5a Mon Sep 17 00:00:00 2001 From: Mathias Garbe Date: Wed, 21 Nov 2012 13:10:34 +0100 Subject: [PATCH] Adds caching of items, fixes layout in admin item view --- adminItemWindow.py | 18 +++++++++----- mainWindow.py | 60 +++++++++++++++++++--------------------------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/adminItemWindow.py b/adminItemWindow.py index 0e117fb..f63192b 100644 --- a/adminItemWindow.py +++ b/adminItemWindow.py @@ -46,13 +46,15 @@ def rebuildItems(self): self.items = CoffeeClient().getItems() font = QtGui.QFont() - font.setPointSize(12) - font.setWeight(75) + font.setPointSize(10) + font.setWeight(50) font.setBold(True) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) - + + itemPerRow = 2 + itemIndex = 0 for item in self.items: label = QtGui.QLabel(self) label.setFont(font) @@ -60,16 +62,20 @@ def rebuildItems(self): label.setSizePolicy(sizePolicy) label.setAlignment(QtCore.Qt.AlignBottom | QtCore.Qt.AlignHCenter) label.setStyleSheet("image: url(resource/items/" + str(item.id) + ".png);") - self.ui.itemLayout.addWidget(label, 0, item.id) + self.ui.itemLayout.addWidget(label, (itemIndex / itemPerRow) * 3 + 0, itemIndex % itemPerRow) enabledCheckbox = QtGui.QCheckBox(self) enabledCheckbox.setText("Enabled") + enabledCheckbox.setFont(font) enabledCheckbox.setChecked(item.enabled) - self.ui.itemLayout.addWidget(enabledCheckbox, 1, item.id) + self.ui.itemLayout.addWidget(enabledCheckbox, (itemIndex / itemPerRow) * 3 + 1, itemIndex % itemPerRow) item.enabledCheckbox = enabledCheckbox soldoutCheckbox = QtGui.QCheckBox(self) soldoutCheckbox.setText("Sold out") + soldoutCheckbox.setFont(font) soldoutCheckbox.setChecked(item.sold_out) - self.ui.itemLayout.addWidget(soldoutCheckbox, 2, item.id) + self.ui.itemLayout.addWidget(soldoutCheckbox, (itemIndex / itemPerRow) * 3 + 2, itemIndex % itemPerRow) item.soldoutCheckbox = soldoutCheckbox + + itemIndex += 1 diff --git a/mainWindow.py b/mainWindow.py index c517752..1d9a044 100644 --- a/mainWindow.py +++ b/mainWindow.py @@ -34,8 +34,6 @@ def __init__(self, cfg): # Message Window self.messageWindow = MessageWindow(self) - self.messageWindow.show("Just a moment...", 999999) - QtCore.QCoreApplication.processEvents() # Windows self.codeWindow = CodeWindow(self.messageWindow, self.redeemCode, self) @@ -61,9 +59,6 @@ def __init__(self, cfg): self.buttons = {} self.items = {} - # Close message window, we are done - self.messageWindow.close() - # Timer for screensaver timeouts self.screensaverTimer = QtCore.QTimer() QtCore.QObject.connect(self.screensaverTimer, QtCore.SIGNAL("timeout()"), self.screensaverTimeout) @@ -91,9 +86,9 @@ def rebuildItemTimeout(self): self.rebuildItems() def rebuildItems(self): - self.messageWindow.show("Items werden geupdated ...", 999999) - print "Rebuilding items...", - + QtCore.QCoreApplication.processEvents() + self.messageWindow.show("Items werden geupdated ...", 60) + print "Rebuilding items..." QtCore.QCoreApplication.processEvents() # Remove all buttons @@ -114,32 +109,31 @@ def rebuildItems(self): items = sorted(self.client.getItems(), cmp=Item_Sort) - noItems = 0 - - for item in items: - if item.enabled == True: - noItems += 1 - - # Three items per row - noRows = math.ceil(float(noItems) / 3.0) - - print noRows - print noItems - if items is None: items = [] - self.items = {} - columnIndex = 0 - rowIndex = 0 + itemPerRow = 2 + itemIndex = 0 for item in items: self.items[item.id] = item + print "Image for '" + str(item.id) + "...", + serverLastModified = float(self.client.getRequest("resource/item/lastModified/" + str(item.id))) + clientLastModified = 0 + try: + clientLastModified = os.path.getctime("resource/items/" + str(item.id) + ".png") + except: + pass + # Download the item icon - f = open("resource/items/" + str(item.id) + ".png", "w+") - f.write(self.client.getRequest("resource/item/" + str(item.id))) - f.close() + if serverLastModified > clientLastModified: + f = open("resource/items/" + str(item.id) + ".png", "w+") + f.write(self.client.getRequest("resource/item/image/" + str(item.id))) + f.close() + print "downloaded." + else: + print "cached." if item.enabled != True: continue @@ -158,16 +152,12 @@ def rebuildItems(self): button.setText(item.desc + " / " + str(item.price) + " Bits") button.setEnabled(True) - self.ui.dynamicButtonLayout.addWidget(button, rowIndex, columnIndex) - columnIndex += 1 - - if columnIndex >= noItems / noRows: - columnIndex = 0 - rowIndex += 1 + self.ui.dynamicButtonLayout.addWidget(button, itemIndex / itemPerRow, itemIndex % itemPerRow) + itemIndex += 1 button.clicked.connect(partial(self.pushItemClicked, item.id)) self.buttons[item.id] = button - QtCore.QCoreApplication.processEvents() +# QtCore.QCoreApplication.processEvents() # Charge button... self.chargeButton = QtGui.QPushButton(self.ui.centralwidget) @@ -176,7 +166,7 @@ def rebuildItems(self): self.chargeButton.setStyleSheet("image: url(resource/gold.png);") self.chargeButton.setObjectName("Aufladen") self.chargeButton.setText("Aufladen") - self.ui.dynamicButtonLayout.addWidget(self.chargeButton, 0, 1337, noRows, 1) + self.ui.dynamicButtonLayout.addWidget(self.chargeButton, 0, 1337, itemIndex / itemPerRow, 1) self.chargeButton.clicked.connect(self.pushChargeClicked) self.adminButton = QtGui.QPushButton(self.ui.centralwidget) @@ -186,7 +176,7 @@ def rebuildItems(self): self.adminButton.setObjectName("Admin") self.adminButton.setText("Admin") self.adminButton.setVisible(False) - self.ui.dynamicButtonLayout.addWidget(self.adminButton, 0, 1338, noRows, 1) + self.ui.dynamicButtonLayout.addWidget(self.adminButton, 0, 1338, itemIndex / itemPerRow, 1) self.adminButton.clicked.connect(self.pushAdminClicked) print "done"