-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUi.py
238 lines (158 loc) · 7.83 KB
/
Ui.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from configparser import ConfigParser, NoOptionError, NoSectionError
from PyQt5 import QtWidgets, QtGui, uic
import sys, re
import pathlib
from PIL import Image
from PIL.ImageQt import ImageQt
from Iconify import Iconify
from UserOptions import UserOptions
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
# load from the .ui file
uic.loadUi("main.ui", self)
self.initUI()
self.initFromConf()
def statusMessage(self, msg, color=""):
if(color != ""):
self.inputError_label.setStyleSheet(f"color: {color};")
else:
self.inputError_label.setStyleSheet("")
self.inputError_label.setText(msg)
def setPreviewIcon(self, path):
img = None
try: # check if the file is valid for PILLOW
img = Image.open(path) # load image
except OSError:
self.resetPreviewIcon()
raise ValueError("Incompatible image file.")
# need to use ImageQt to be able to open .ico files
qimg = ImageQt(img)
pixmap = QtGui.QPixmap.fromImage(qimg)
# hide the border
self.mediumIconPreview_label.setStyleSheet("")
self.smallIconPreview_label.setStyleSheet("")
# apply image to label
size = (self.mediumIconPreview_label.height(), self.mediumIconPreview_label.height())
self.mediumIconPreview_label.setPixmap(pixmap.scaled(*size))
size = (self.smallIconPreview_label.height(), self.smallIconPreview_label.height())
self.smallIconPreview_label.setPixmap(pixmap.scaled(*size))
def resetPreviewIcon(self):
self.smallIconPreview_label.setPixmap(QtGui.QPixmap())
self.mediumIconPreview_label.setPixmap(QtGui.QPixmap())
self.mediumIconPreview_label.setStyleSheet("border: 1px solid grey;")
self.smallIconPreview_label.setStyleSheet("border: 1px solid grey;")
def iconify_pushButton_clicked(self):
self.statusMessage("Loading...")
try:
userOptions = UserOptions( self.gameTitle_lineEdit.text().strip(),
self.gameURL_lineEdit.text().strip(),
self.iconLocation_lineEdit.text().strip(),
self.steamLocation_lineEdit.text().strip(),
self.customShortcutFolder_lineEdit.text().strip())
# if everything went through, do the magic iconify stuff
Iconify(userOptions)
# save the settings place in steam path and custom shortcut folder
configFileName = "config.ini"
config = ConfigParser()
config.read(configFileName)
config.set("Path", "steam_path", self.steamLocation_lineEdit.text())
config.set("Path", "custom_shortcut_folder", self.customShortcutFolder_lineEdit.text())
with open(configFileName, "w") as configFile:
config.write(configFile)
self.statusMessage("Success!", "green")
except ValueError as e:
self.statusMessage(e.args[0], "red")
def reset_pushButton_clicked(self):
self.gameTitle_lineEdit.clear()
self.gameURL_lineEdit.clear()
self.iconLocation_lineEdit.clear()
self.statusMessage("")
self.resetPreviewIcon()
def refresh_pushButton_clicked(self):
# get path entered
path = self.iconLocation_lineEdit.text()
#if path is empty, return
if(path == ""):
self.statusMessage("Icon location is empty.", "red")
return
# try to set the preview, if fails, show error
try:
self.setPreviewIcon(path)
except ValueError as e:
self.statusMessage(e.args[0], "red")
def openUrl_pushButton_clicked(self):
fileName = QtWidgets.QFileDialog.getOpenFileName(self, "Open Url", "", "Internet Shortcut (*.url)")
fileName = fileName[0] # getOpenFileName returns a tuple for some reason, I only need the path
if(not fileName == ""):
# get url from file
data = ""
with open(fileName, "r") as urlFile:
data = urlFile.read()
# match the url and write the result
re_steamURL = re.compile("(?<=URL=)steam://rungameid/[0-9]+")
match = re_steamURL.search(data)
if(match != None):
url = match[0]
self.gameURL_lineEdit.setText(url)
# set game title from the selected file
gameTitle = pathlib.Path(fileName).stem
self.gameTitle_lineEdit.setText(gameTitle)
# set icon path if availlable
if(self.iconLocation_lineEdit.text().strip() == ""): # if there is text already, don't change it
re_icoPath = re.compile("(?<=IconFile=).*\\.ico")
match = re_icoPath.search(data)
if(match != None):
path = match[0]
self.iconLocation_lineEdit.setText(path)
try:
self.setPreviewIcon(path)
except ValueError:
pass
else: # if cannot find the steam game url
self.statusMessage("Invalid game url", "red")
def iconLocation_toolButton_clicked(self):
fileDialogResult = QtWidgets.QFileDialog.getOpenFileName(self, "Open Image", "", "Image Files (*.png *.jpg *.bmp *.ico)")
path = fileDialogResult[0] # getOpenFileName returns a path and type of file, I only need the path
if(not path == ""):
self.iconLocation_lineEdit.setText(path)
try:
self.setPreviewIcon(path)
except ValueError as e:
self.statusMessage(e.args[0], "red")
def steamLocation_toolButton_clicked(self):
fileName = QtWidgets.QFileDialog.getOpenFileName(self, "Open Steam", "", "Executable (*.exe)")
fileName = fileName[0] # getOpenFileName returns a tuple for some reason, I only need the path
if(not fileName == ""):
self.steamLocation_lineEdit.setText(fileName)
def customShortcutFolder_toolButton_clicked(self):
folderName = QtWidgets.QFileDialog.getExistingDirectory(self, "Open Folder", "")
if(not folderName == ""):
self.customShortcutFolder_lineEdit.setText(folderName)
def initUI(self):
# apparence
self.statusMessage("")
self.setFixedSize(self.size())
# set window icon
self.setWindowIcon(QtGui.QIcon("steamIcon.ico"))
# connections
self.iconify_pushButton.clicked.connect(self.iconify_pushButton_clicked)
self.reset_pushButton.clicked.connect(self.reset_pushButton_clicked)
self.refresh_pushButton.clicked.connect(self.refresh_pushButton_clicked)
self.openUrl_pushButton.clicked.connect(self.openUrl_pushButton_clicked)
self.iconLocation_toolButton.clicked.connect(self.iconLocation_toolButton_clicked)
self.steamLocation_toolButton.clicked.connect(self.steamLocation_toolButton_clicked)
self.customShortcutFolder_toolButton.clicked.connect(self.customShortcutFolder_toolButton_clicked)
def initFromConf(self):
# read from config file
configFileName = "config.ini"
config = ConfigParser()
config.read(configFileName)
try: # try to get steam_path from config file
self.steamLocation_lineEdit.setText(config.get("Path", "steam_path"))
except (NoOptionError, NoSectionError):
pass
try: # try to get custom_shortcut_folder from config file
self.customShortcutFolder_lineEdit.setText(config.get("Path", "custom_shortcut_folder"))
except (NoOptionError, NoSectionError):
pass