-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappgui.py
119 lines (94 loc) · 4.17 KB
/
appgui.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
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.factory import Factory
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.config import Config
from desktop_file_dialogs import Desktop_FileDialog, Desktop_FolderDialog, FileGroup
from pathlib import Path
from pathvalidate import sanitize_filename
from schematic_generator import generate_schematic
import os
class ResultPopup(FloatLayout):
exit = ObjectProperty(None)
result = StringProperty(None)
class Root(FloatLayout):
def __init__(self, **kwargs):
super(Root, self).__init__(**kwargs)
self.selected_input_path = ''
self.output_filename = ''
self.selected_output_dir = os.path.join(os.environ.get('appdata'), '.minecraft', 'schematics')
self.schematic_author = 'Minecraft Noteblock Schematic Generator'
def dismiss_popup(self):
self.result_popup.dismiss()
def select_midi_file(self):
Desktop_FileDialog(
title = "Select Midi File",
initial_directory = os.path.dirname(self.selected_input_path),
on_cancel = self.on_cancel_file_or_dir,
on_accept = self.on_accept_file,
file_groups = [
FileGroup(name="Midi Files", extensions=["midi, mid"]),
],
).show()
def select_save_path(self):
Desktop_FolderDialog(
title = 'Select Output Directory',
initial_directory = self.selected_output_dir,
on_accept = self.on_accept_folder,
on_cancel = self.on_cancel_file_or_dir
).show()
def on_accept_file(self, path):
self.selected_input_path = path
self.ids.midi_file_label.text = path
if self.output_filename == '':
self.output_filename = os.path.splitext(os.path.basename(path))[0]
self.ids.output_filename_label.text = self.output_filename
print(f"Selected MIDI File: {path}")
def on_accept_folder(self, path):
self.selected_output_dir = path
self.ids.output_directory_label.text = path
print(f"Selected Folder: {path}")
def on_cancel_file_or_dir(self):
print('No File or Folder was selected')
def submit(self):
self.selected_input_path = self.ids.midi_file_label.text
self.selected_output_dir = self.ids.output_directory_label.text
self.output_filename = self.ids.output_filename_label.text
self.schematic_author = self.ids.schematic_author_label.text
if self.selected_input_path == '':
self.show_result('Input MIDI file missing')
return
elif self.output_filename == '':
self.show_result('Output Filename not given')
return
elif self.selected_output_dir == '':
self.show_result('Output Directory not given')
return
elif not os.path.isfile(self.selected_input_path):
self.show_result('Selected Input File not found')
return
self.output_filename = f'{self.output_filename}.litematic'
Path(self.selected_output_dir).mkdir(parents=True, exist_ok=True)
self.output_filename = sanitize_filename(self.output_filename)
out_filepath = os.path.join(self.selected_output_dir, self.output_filename)
generate_schematic(self.selected_input_path, save_path=out_filepath, author=self.schematic_author)
self.show_result(f'File {out_filepath} was successfully created!')
def show_result(self, result):
result_content = ResultPopup(exit=self.dismiss_popup, result=result)
# result_content.result = result
self.result_popup = Popup(title='Result', content=result_content, size_hint=(0.9, 0.9))
self.result_popup.open()
print(result)
class Editor(App):
icon = 'ico/icon.png'
def build(self):
self.title = 'Minecraft Noteblock Schematic Generator'
Window.minimum_width = 500
Window.minimum_height = 500
Factory.register('Root', cls=Root)
Factory.register('Result', cls=ResultPopup)
if __name__ == '__main__':
Editor().run()