-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_item.py
114 lines (88 loc) · 3.38 KB
/
game_item.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
import os
import utils
import ntpath
import platform
import win32gui
import pywintypes
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWinExtras import QtWin
class Game:
def __init__(self, name, path, parameters):
if not name:
_, name = ntpath.split(path)
self.name = name
self.path = path
self.parameters = parameters
def is_valid_path(self):
return os.path.isfile(self.path)
def title(self):
return self.name if self.name else self.path
def serialize(self):
return self.__dict__
def get_dir(self):
return ntpath.split(self.path)[0]
def get_file(self):
return ntpath.split(self.path)[1]
class GameItemWidget(QtWidgets.QWidget):
def __init__(self):
super(GameItemWidget, self).__init__(None)
# Set up labels
self.nameLabel = QtWidgets.QLabel()
self.iconLabel = QtWidgets.QLabel()
# Set up layout
self.boxLayout = QtWidgets.QHBoxLayout()
self.boxLayout.addWidget(self.iconLabel)
self.boxLayout.addWidget(self.nameLabel, 1)
self.setLayout(self.boxLayout)
# Set up design
self.nameLabel.setStyleSheet("font: 15px;")
class GameListItem(QtWidgets.QListWidgetItem):
def __init__(self, game_list, game: Game):
super(GameListItem, self).__init__(game_list)
self.game = game
self.game_list: QtWidgets.QListWidget = game_list
self.game_item_widget = GameItemWidget()
# Set details
self.update()
def update(self):
self.set_name(self.game.name if self.game.name else self.game.path)
self.set_icon(self.game.path)
self.setSizeHint(self.game_item_widget.sizeHint())
def set_name(self, name):
self.game_item_widget.nameLabel.setText(name)
def set_icon(self, path):
if platform.system() == "Windows":
# Get the icons in different sizes from the binary
try:
large, small = win32gui.ExtractIconEx(path, 0, 10)
except pywintypes.error:
return
# Convert it into a pixmap
if large:
pixmap: QPixmap = QtWin.fromHICON(large[0])
self.game_item_widget.iconLabel.setPixmap(QPixmap.scaledToHeight(pixmap, 32))
tuple(map(win32gui.DestroyIcon, small + large))
def add_to_list(self):
self.game_list.addItem(self)
self.game_list.setItemWidget(self, self.game_item_widget)
def move_up(self):
try:
current_row = self.game_list.currentRow()
self.game_list.removeItemWidget(self)
self.game_list.takeItem(current_row)
self.setSizeHint(self.game_item_widget.sizeHint())
self.game_list.insertItem(current_row - 1, self)
# self.game_list.setItemWidget(self, self.game_item_widget)
except Exception as e:
utils.show_error(text=e)
def move_down(self):
try:
current_row = self.game_list.currentRow()
self.game_list.removeItemWidget(self)
self.game_list.takeItem(current_row)
self.setSizeHint(self.game_item_widget.sizeHint())
self.game_list.insertItem(current_row + 1, self)
# self.game_list.setItemWidget(self, self.game_item_widget)
except Exception as e:
utils.show_error(text=e)