Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import sys
- import os
- import subprocess
- from PyQt4 import QtCore, QtGui, uic
- import sqlite3 as lite
- from xml.etree import ElementTree as ET
- import resources
- class Init(QtGui.QMainWindow):
- def __init__(self):
- super(Init, self).__init__()
- self.initUI()
- def initUI(self):
- uic.loadUi(os.path.join(os.getcwd(), "ui/lory.ui"), self)
- self.progressbar = QtGui.QProgressDialog("Generating Mame game list...\nThis process may take a few minutes to complete.", "Abort", 0, 0, self);
- self.progressbar.setWindowTitle("Lorena")
- self.progressbar.setWindowModality(QtCore.Qt.WindowModal);
- self.progressbar.setAutoReset(True)
- self.progressbar.setAutoClose(True)
- #self.show()
- self.buildGamelist()
- #self.animateMng("nrallyx.mng")
- self.show()
- def buildGamelist(self):
- if not os.path.isfile('lory.db'):
- con = self.dbConnection()
- item = 0
- with con:
- # Create a library of games table
- cur_libname = con.cursor()
- cur_libname.execute("CREATE TABLE libraryname (id INTEGER PRIMARY KEY ASC, name TEXT)")
- cur_libname.execute("INSERT INTO libraryname (name) VALUES ('Recently Played')")
- cur_libname.execute("INSERT INTO libraryname (name) VALUES ('Top Rated')")
- cur_libgame = con.cursor()
- cur_libgame.execute("CREATE TABLE library(id INTEGER PRIMARY KEY ASC, idgame INTEGER, idlibname INTEGER)")
- # Create a games list table
- cur_game = con.cursor()
- cur_game.execute("CREATE TABLE game (id INTEGER PRIMARY KEY ASC, name TEXT, sourcefile TEXT, cloneof TEXT, romof TEXT, description TEXT, year TEXT, manufacturer TEXT, picture TEXT, video TEXT, rating INTEGER, lastplayed DATE, playcount INTEGER, status INTEGER)")
- total = self.mameExec('-ll | wc -l')
- self.progressbar.setMinimum(1)
- self.progressbar.setMaximum(int(total))
- games = self.mameExec('-ll | awk \'{print $1}\'')
- for i in games.splitlines()[1:]:
- xmllist = self.mameExec('-lx -norc ', i)
- xmlresult = self.xmlParse(xmllist)
- item += 1
- self.progressbar.setValue(item)
- if self.progressbar.wasCanceled():
- break
- sql = """INSERT INTO game (name, sourcefile, cloneof, romof, description, year, manufacturer, picture, video, playcount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""
- cur_game.execute(sql, [xmlresult[0], xmlresult[1], xmlresult[2], xmlresult[3], xmlresult[4], xmlresult[5], xmlresult[6], xmlresult[0] + ".png", xmlresult[0] + ".mng", 0])
- con.commit()
- self.progressbar.cancel()
- self.populateLibrary()
- self.populateGame()
- def populateLibrary(self):
- # populate category
- self.trLibraries.clear()
- querylib = self.queryLibrary()
- for l in querylib:
- item = QtGui.QTreeWidgetItem(self.trLibraries)
- item.setText(0, l[1])
- def populateGame(self):
- # populate game
- self.gamebox = []
- self.scene = QtGui.QGraphicsScene()
- self.tableWidget.setColumnCount(7)
- self.tableWidget.setHorizontalHeaderLabels(["Game", "Name", "Year", "Manufacturer", "Last Played", "Play Count", "Rating"])
- hitem = QtGui.QTableWidgetItem("Game")
- hitem.setTextAlignment(QtCore.Qt.AlignLeft)
- self.tableWidget.setHorizontalHeaderItem(0, hitem)
- hitem = QtGui.QTableWidgetItem("Name")
- hitem.setTextAlignment(QtCore.Qt.AlignLeft)
- self.tableWidget.setHorizontalHeaderItem(1, hitem)
- hitem = QtGui.QTableWidgetItem("Manufacturer")
- hitem.setTextAlignment(QtCore.Qt.AlignLeft)
- self.tableWidget.setHorizontalHeaderItem(3, hitem)
- self.tableWidget.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows);
- self.tableWidget.setSelectionMode(QtGui.QAbstractItemView.SingleSelection);
- querygame, querygametotal = self.queryGame()
- self.statusBar().showMessage("Total Games: " + str(querygametotal[0]))
- col = 0
- lin = 0
- tableline = 0
- for l in querygame:
- gameid = l[0]
- gamename = l[1]
- gamedescription = l[5]
- gameyear = l[6]
- gamemanufaturer = l[7]
- gamepicture = l[8]
- gamevideo = l[9]
- gamerating = l[10]
- gamelastplay = l[11]
- gameplaycount = l[12]
- gamestdout = self.mameExec("-verifyroms", gamename)
- errcheck = gamestdout.find("is good") >= 0
- if errcheck:
- gamestatus = True
- else:
- gamestatus = False
- self.tableWidget.setRowCount(querygametotal[0])
- item = QtGui.QTableWidgetItem(tableline)
- if gamestatus:
- item.setIcon(QtGui.QIcon(QtGui.QPixmap("ui/pixmaps/ok.png")))
- item.setText(str(gamedescription))
- self.tableWidget.setItem(tableline, 0, item)
- else:
- item.setIcon(QtGui.QIcon(QtGui.QPixmap("ui/pixmaps/error.png")))
- item.setText(str(gamedescription))
- self.tableWidget.setItem(tableline, 0, item)
- item = QtGui.QTableWidgetItem(str(gamename))
- item.setTextAlignment(QtCore.Qt.AlignLeft)
- self.tableWidget.setItem(tableline, 1, item)
- item = QtGui.QTableWidgetItem(str(gameyear))
- item.setTextAlignment(QtCore.Qt.AlignHCenter)
- self.tableWidget.setItem(tableline, 2, item)
- item = QtGui.QTableWidgetItem(str(gamemanufaturer))
- item.setTextAlignment(QtCore.Qt.AlignLeft)
- self.tableWidget.setItem(tableline, 3, item)
- item = QtGui.QTableWidgetItem(str(gamerating))
- item.setTextAlignment(QtCore.Qt.AlignHCenter)
- self.tableWidget.setItem(tableline, 4, item)
- item = QtGui.QTableWidgetItem(str(gamelastplay))
- item.setTextAlignment(QtCore.Qt.AlignHCenter)
- self.tableWidget.setItem(tableline, 5, item)
- item = QtGui.QTableWidgetItem(str(gameplaycount))
- item.setTextAlignment(QtCore.Qt.AlignHCenter)
- self.tableWidget.setItem(tableline, 6, item)
- self.tableWidget.resizeColumnsToContents()
- tableline += 1
- picture = "/home/junix/snap/" + gamepicture
- if os.path.isfile(picture) and gamestatus:
- scaledpicture = QtGui.QPixmap(picture).scaled(238, 174, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.FastTransformation)
- picitem = QtGui.QGraphicsPixmapItem(scaledpicture)
- picitem.setPos((260 * col), lin)
- self.scene.addItem(picitem)
- col += 1
- if col >= 6:
- lin += 220
- col = 0
- self.graphicsView.setScene(self.scene)
- #self.gamebox.append(item)
- def dbConnection(self):
- try:
- con = lite.connect("lory.db")
- except lite.Error, e:
- print "Error %s:" % e.args[0]
- sys.exit(1)
- return con
- def queryLibrary(self):
- # Generate a library
- con = self.dbConnection()
- cur_lib = con.cursor()
- cur_lib.execute("SELECT * FROM libraryname")
- data_lib = cur_lib.fetchall()
- con.close()
- return data_lib
- def queryGame(self):
- # Generate a games list
- con = self.dbConnection()
- cur_game = con.cursor()
- cur_game.execute("SELECT * FROM game")
- data_game = cur_game.fetchall()
- cur_game.execute("SELECT COUNT(*) FROM game")
- data_game_count = cur_game.fetchone()
- con.close()
- return data_game, data_game_count
- def mameExec(self, parameter1, parameter2):
- mamebin = os.popen("/usr/bin/which mame").read()
- if not mamebin:
- QMessageBox.about(self, "Lorena", "Error on execute shell command.", "")
- return False
- command = subprocess.Popen([mamebin.strip(), parameter1, parameter2], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
- (standardout, standarderr) = command.communicate()
- return standardout
- def getNodetext(self, elem,node_name,default_value=None):
- node = elem.find(node_name)
- if node == None:
- return default_value
- return node.text
- def xmlParse(self, xmllist):
- doc = ET.ElementTree(ET.fromstring(xmllist))
- game_nodes = doc.getroot().findall("game")
- for game_node in game_nodes:
- name = game_node.attrib.get("name",None)
- sourcefile = game_node.attrib.get("sourcefile",None)
- cloneof = game_node.attrib.get("cloneof",None)
- romof = game_node.attrib.get("romof",None)
- description = self.getNodetext(game_node,"description","")
- year = self.getNodetext(game_node,"year","")
- manufacturer = self.getNodetext(game_node,"manufacturer","")
- xmlresult = [name, sourcefile, cloneof, romof, description, year, manufacturer]
- return xmlresult
- def animateMng(self, gamename):
- movie = QtGui.QMovie(os.path.join(os.getcwd(), gamename), parent=self.lblMovie)
- movie.setCacheMode(QtGui.QMovie.CacheAll)
- self.lblMovie.setMovie(movie)
- movie.start()
- def mousePressEvent(self, event):
- if (event.button() != QtCore.Qt.LeftButton):
- return
- x = event.globalX()
- y = event.globalY()
- print x, y
- self.selectItem(x, y)
- def selectItem(self, x, y):
- selected = self.scene.itemAt(x,y)
- print selected
- if selected:
- selected.setSelected(True)
- def main():
- app = QtGui.QApplication(sys.argv)
- ex = Init()
- sys.exit(app.exec_())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement