def getGameData(self, row): data = dict() data[COLUMN_NAME] = self.item(row, headers.index(COLUMN_NAME)).text() data[COLUMN_SYSTEM] = self.item(row,headers.index(COLUMN_SYSTEM)).text() data[COLUMN_YEAR] = self.item(row,headers.index(COLUMN_YEAR)).text() data[COLUMN_RATING] = self.item(row,headers.index(COLUMN_RATING)).text() data[COLUMN_VOTES] = self.item(row,headers.index(COLUMN_VOTES)).text() data[COLUMN_WEIGHTED] = self.item(row,headers.index(COLUMN_WEIGHTED)).text() data[COLUMN_STATUS] = self.item(row, headers.index(COLUMN_STATUS)).text() data[COLUMN_LABELS] = self.cellWidget(row,headers.index(COLUMN_LABELS)).labelsToString() data[COLUMN_NOTES] = self.item(row,headers.index(COLUMN_NOTES)).text() data[COLUMN_URL] = self.item(row,headers.index(COLUMN_URL)).text() return data
def reload_scores(self): rows = self.rowCount() progress = QProgressDialog("Updating scores", "", 0, rows, self) progress.setWindowTitle('Reload scores') progress.setCancelButton(None) progress.setWindowModality(Qt.WindowModal) try: for i in range(0,rows): url = self.item(i,headers.index(COLUMN_URL)).text() req = urllib2.Request(str(url), headers={'User-Agent' : "Magic Browser"}) response = urllib2.urlopen(req) html = response.read().decode('ascii','ignore') doc = fromstring(html) # Updating the name, in case it changed el = doc.xpath("//h1[@class='page-title']") name = el[0].findtext('a') self.item(i,headers.index(COLUMN_NAME)).setText(name) # Updating the score el = doc.xpath("//fieldset[@id='js_mygames_rate']") if len(el)>0: rating_str = el[0].getchildren()[0].getchildren()[0].getchildren()[1].findtext('a') if rating_str == None: rating = '0.00' votes = '0' else: rating = rating_str.split(' / ')[0] votes_str = el[0].getchildren()[0].getchildren()[0].getchildren()[2].text votes = votes_str.split(' ')[0] else: rating = '0.00' votes = '0' self.item(i,headers.index(COLUMN_RATING)).setText(rating) self.item(i,headers.index(COLUMN_VOTES)).setText(votes) progress.setValue(i+1) self.compute_final_rating() self.changed = True except urllib2.URLError as e: print e.reason errorMessage=QErrorMessage(self) errorMessage.showMessage('Incorrect URL or not Internet connection') except urllib2.HTTPError as e: print e.code print e.read() errorMessage=QErrorMessage(self) errorMessage.showMessage('Connection error: ' + e.code + ' ' + e.read())
def cellClicked(self, tableItem): row = tableItem.row() column = tableItem.column() if column == headers.index(COLUMN_YEAR): sdc = SelectDateController(self.item(row, column).text(), self) sdc.exec_() date = sdc.getDate() if date != None: self.item(row, column).setText(date) elif column == headers.index(COLUMN_STATUS): ssc = SelectStatusController(self.item(row, column).text(), self) ssc.exec_() status = ssc.getStatus() if status != None: self.item(row, column).setText(status) self.item(row, column).setTextColor(self.status_model.getColor(status)) self.status_list_model.add(status) self.status_list_model.remove(ssc.getPreviousStatus()) self.hide_rows()
def hide_rows(self): none = self.label_list_model.get_filtered(LABEL_NONE) for row in range(0,self.rowCount()): labels_row = self.cellWidget(row,headers.index(COLUMN_LABELS)).getLabels() filtered_out = none and len(labels_row) == 0 i = 0 while not filtered_out and i<len(labels_row): filtered_out = self.label_list_model.get_filtered(labels_row[i]) i = i + 1 filtered_out = filtered_out or self.system_list_model.get_filtered(self.item(row, headers.index(COLUMN_SYSTEM)).text()) filtered_out = filtered_out or self.status_list_model.get_filtered(self.item(row, headers.index(COLUMN_STATUS)).text()) self.setRowHidden(row, filtered_out)
def addGame(self, url, html): #try: doc = fromstring(html) data = dict() # Game's name el = doc.xpath("//h1[@class='page-title']") data[COLUMN_NAME] = el[0].findtext('a') # Game's system el = doc.xpath("//title") system = el[0].text system = system.split(data[COLUMN_NAME] + ' for ')[1] system = system.split(' - GameFAQs')[0] data[COLUMN_SYSTEM] = system # Year el = doc.xpath("//div[@class='pod pod_gameinfo']") year = el[0].getchildren()[1].getchildren()[0].getchildren()[3].findtext('a') data[COLUMN_YEAR] = re.search('[0-9][0-9][0-9][0-9]|Canceled|TBA', year).group() # Rating, votes and final rating el = doc.xpath("//fieldset[@id='js_mygames_rate']") if len(el)>0: rating_str = el[0].getchildren()[0].getchildren()[0].getchildren()[1].findtext('a') if rating_str == None: data[COLUMN_RATING] = '0.00' data[COLUMN_VOTES] = '0' else: data[COLUMN_RATING] = rating_str.split(' / ')[0] votes_str = el[0].getchildren()[0].getchildren()[0].getchildren()[2].text data[COLUMN_VOTES] = votes_str.split(' ')[0] else: data[COLUMN_RATING] = '0.00' data[COLUMN_VOTES] = '0' # Checking that the game is not already in the database rows = self.rowCount() found = False pos = 0 while not found and pos < rows: if self.item(pos,headers.index(COLUMN_URL)).text() == url: found = True pos = pos + 1 if found: errorMessage=QErrorMessage(self) errorMessage.showMessage(data[COLUMN_NAME] + ' (' + data[COLUMN_SYSTEM] + ') is already in the database') else: data[COLUMN_WEIGHTED] = '' data[COLUMN_STATUS] = 'unplayed' data[COLUMN_LABELS] = '' data[COLUMN_NOTES] = '' data[COLUMN_URL] = url self.addGameRow(data) # And recomputing weighted ratins self.compute_final_rating()
def compute_final_rating(self): rows = self.rowCount() # Computing the mean ratings_i = [] votes_i = [] for i in range(0,rows): ratings_i.append(float(self.item(i,headers.index(COLUMN_RATING)).text())) votes_i.append(float(self.item(i,headers.index(COLUMN_VOTES)).text())) ratings_i = np.array(ratings_i) votes_i = np.array(votes_i) non_zeros = np.where(votes_i != 0)[0] mean = np.mean(ratings_i[non_zeros]) wr = np.zeros((rows)) wr[non_zeros] = (votes_i[non_zeros]/(votes_i[non_zeros] + self.minimum))*ratings_i[non_zeros] wr[non_zeros] = wr[non_zeros] + (self.minimum / (votes_i[non_zeros] + self.minimum))*mean wr_str = np.zeros((rows),dtype='S4') wr_str[non_zeros] = ["%.2f" % x for x in wr[non_zeros]] # Computing the weighted rating for all the games again for i in range(0,rows): self.item(i, headers.index(COLUMN_WEIGHTED)).setText(wr_str[i])
def remove_game_clicked(self): indexes = self.table.selectionModel().selectedRows() if len(indexes) > 0: actual_indexes = [] for index in sorted(indexes): actual_indexes.append(index.row()) names = [] systems = [] for i in actual_indexes: names.append(self.table.item(i,0).text()) systems.append(self.table.item(i,1).text()) delete_msg = "Are you sure you want to delete the following entries?\n" for i in range(0,min(len(names),10)): delete_msg = delete_msg + '\n' + names[i] + ' (' + systems[i] + ')' if len(names) > 10: delete_msg = delete_msg + '\n...' reply = QtGui.QMessageBox.question(self, 'Confirm game removal', delete_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: for i in range(len(actual_indexes) - 1, -1, -1): system = self.table.getGameData(actual_indexes[i])[COLUMN_SYSTEM] status = self.table.getGameData(actual_indexes[i])[COLUMN_STATUS] labels = self.table.cellWidget(actual_indexes[i],headers.index(COLUMN_LABELS)).getLabels() self.table.system_list_model.remove(system) self.table.status_list_model.remove(status) for label in labels: self.table.label_list_model.remove(label) self.table.removeRow(actual_indexes[i]) self.table.changed = True else: error = QErrorMessage() error.showMessage('No games were selected') error.setWindowTitle('Remove game') error.exec_()
def hide_rows_search(self, text): for row in range(0,self.rowCount()): item_text = str(self.item(row, headers.index(COLUMN_NAME)).text()).lower() self.setRowHidden(row, not text in item_text)
def addGameRow(self, data, row=None): # Adding the row, and disabling some of the fields, so # they can not be edited # name if row == None: rows = self.rowCount() self.setRowCount(rows + 1) else: rows = row item = QtGui.QTableWidgetItem(data[COLUMN_NAME]) item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_NAME), item) # system item = QtGui.QTableWidgetItem(data[COLUMN_SYSTEM]) item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_SYSTEM), item) self.system_list_model.add(data[COLUMN_SYSTEM]) # date item = QtGui.QTableWidgetItem(data[COLUMN_YEAR]) item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_YEAR), item) # rating item = QtGui.QTableWidgetItem(data[COLUMN_RATING]) item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_RATING), item) # votes item = NumericWidgetItem(data[COLUMN_VOTES]) item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_VOTES), item) # Weighted rating item = QtGui.QTableWidgetItem(data[COLUMN_WEIGHTED]) item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_WEIGHTED), item) # Status item = QtGui.QTableWidgetItem(data[COLUMN_STATUS]) item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_STATUS), item) item.setTextColor(self.status_model.getColor(data[COLUMN_STATUS])) self.status_list_model.add(data[COLUMN_STATUS]) # labels item = QtGui.QTableWidgetItem('') item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_LABELS), item) widget = LabelWidget(item, self) widget.stringToLabels(data[COLUMN_LABELS]) self.setCellWidget(rows, headers.index(COLUMN_LABELS), widget) new_labels = widget.getLabels() for label in new_labels: self.label_list_model.add(label) # Notes item = QtGui.QTableWidgetItem(data[COLUMN_NOTES]) self.setItem(rows, headers.index(COLUMN_NOTES), item) # Url item = QtGui.QTableWidgetItem(data[COLUMN_URL]) item.setFlags(QtCore.Qt.ItemIsEnabled) self.setItem(rows, headers.index(COLUMN_URL), item) self.changed = True