-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui.py
169 lines (136 loc) · 5.01 KB
/
gui.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
#!/usr/bin/env python3
try:
import requests
except ImportError:
print("It looks like requests is not installed.")
print("Try this: pip3 install requests")
exit(1)
import sys
import subprocess
import os.path
from PyQt5 import QtCore, uic
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import mod_manager
from mod_manager.folders import mod_folder
Ui_MainWindow, QtBaseClass = uic.loadUiType("gui.ui")
def error(arg):
"""Display an error message."""
# TODO: show a graphical window
print(arg)
class App(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
# Mod stuff
self.mod_manager = mod_manager.ModManager()
self.cache = mod_manager.cache.Cache()
# UI elements
# Buttons
self.add_pack_button.clicked.connect(self.add_pack)
self.install_pack_button.clicked.connect(self.install_pack)
# Mods list
self.mod_list.itemClicked.connect(self.load_mod)
# Menubar items
# File
self.save_button.triggered.connect(self.save)
self.save_as_button.triggered.connect(self.save_as)
self.refresh_button.triggered.connect(self.load_packs)
# Folder
self.open_mods_button.triggered.connect(self.mods)
self.open_modpacks_button.triggered.connect(self.modpacks)
self.open_cache_button.triggered.connect(self.open_cache)
# Pack
self.add_empty_pack_button.triggered.connect(self.add_pack)
self.add_string_pack_button.triggered.connect(self.add_string_pack)
self.install_pack_button_2.triggered.connect(self.install_pack)
self.get_pack_string_button.triggered.connect(self.pack_string)
# Load packs initially
self.load_packs()
# Actions
def save(self):
if not self.get_selected_pack():
self.save_as()
# Get current editor text and transfer it into the currently selected pack file.
self.get_selected_pack().edit(self.mod_text_edit.toPlainText().replace("\r\n", "\n").split("\n"))
self.get_selected_pack().save()
def save_as(self):
# Get current editor text and open a dialog to name the new modpack that has the following mods.
self.add_pack()
self.save()
def load_mod(self):
"""Fill editor with currently selected modpack."""
pack = self.get_selected_pack()
if pack is None:
# No pack selected
return
self.mod_text_edit.setPlainText("\n".join(pack.lines))
def mods(self):
"""Open mods folder."""
self.open_folder(mod_folder.path)
def modpacks(self):
# open modpacks folder
self.open_folder(get_absolute_path("modpacks"))
def open_cache(self):
# open cache folder
self.open_folder(self.cache.check_folder())
def load_packs(self):
self.mod_list.clear()
for pack in self.mod_manager.modpacks:
tmp = QListWidgetItem()
tmp.setText(pack.name)
self.mod_list.addItem(tmp)
self.mod_list.sortItems()
def add_pack(self):
name = self.get_string_popup("Pack name")
if name:
self.mod_manager.get_pack(name).save()
tmp = QListWidgetItem()
tmp.setText(name)
self.mod_list.addItem(tmp)
self.mod_list.setCurrentRow(self.mod_list.count()-1)
self.mod_list.sortItems()
self.load_mod()
def add_string_pack(self):
name = self.get_string_popup("Digest")
if name:
mod_manager.modpack.ModPack.decompress(args[0]).save()
self.load_packs()
def install_pack(self):
pack = self.get_selected_pack()
if pack is None:
error("No pack selected")
return
self.mod_manager.install_packs([pack])
# TODO: show progress steps
# TODO: show message
def pack_string(self):
if self.get_selected_pack() is None:
error("No pack selected")
return
print(self.get_selected_pack())
self.mod_text_edit.setPlainText("# Digest string of " + self.get_selected_pack() + "\n\n" + self.get_selected_pack().compress())
self.mod_list.clearSelection()
def get_selected_pack(self):
current_item = self.mod_list.currentItem()
if current_item is None:
return None
return self.mod_manager.get_pack(current_item.data(0))
# Helpers
def open_folder(self, path):
if platform.system() == "Windows":
os.startfile(path)
elif platform.system() == "Darwin":
subprocess.Popen(["open", path])
else:
subprocess.Popen(["xdg-open", path])
def get_string_popup(self, name):
text, ok = QInputDialog.getText(self, 'Input Dialog', name+':')
if ok:
return str(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = App()
window.show()
sys.exit(app.exec_())