!I integrate from //ce/main...
[CRYENGINE.git] / Tools / CryVersionSelector / backup_project_gui.py
blob8e8d83dfe49ec6845ac6003f085bf10a14fb3387
1 #!/usr/bin/env python3
2 """
3 This handles the TkInter GUI for making backups of projects.
4 It also handles it properly if the current machine isn't
5 capable of running TkInter.
6 """
8 import sys
9 import os.path
11 HAS_TK = True
12 try:
13 import tkinter as tk
14 from tkinter import filedialog
15 except ImportError:
16 print("Skipping importing tkinter, because it's not installed.")
17 HAS_TK = False
20 def configure_backup(export_path):
21 """
22 Opens a GUI in which the user can select where the backup is saved.
23 """
24 # Return the default export_path if no GUI can be made.
25 if not HAS_TK:
26 return export_path
28 iconfile = "editor_icon16.ico"
29 if not hasattr(sys, "frozen"):
30 iconfile = os.path.join(os.path.dirname(__file__), iconfile)
31 else:
32 iconfile = os.path.join(sys.prefix, iconfile)
34 root = tk.Tk()
35 root.iconbitmap(iconfile)
36 app = CryConfigureBackup(export_path=export_path, master=root)
38 app.mainloop()
39 path = app.export_path
41 return path
44 def center_window(win):
45 """
46 Centers the window.
47 """
48 win.update_idletasks()
49 width = win.winfo_width()
50 height = win.winfo_height()
51 position_x = (win.winfo_screenwidth() // 2) - (width // 2)
52 position_y = (win.winfo_screenheight() // 2) - (height // 2)
53 win.geometry('{}x{}+{}+{}'.format(width, height, position_x, position_y))
56 if HAS_TK:
57 class CryConfigureBackup(tk.Frame):
58 """
59 TK class that handles the window to configure the backup.
60 """
61 export_path = None
63 def __init__(self, export_path=None, master=None):
64 super().__init__(master)
65 self.parent = master
66 self.parent.title("CRYENGINE backup project")
67 self.parent.minsize(400, 100)
68 self.pack(expand=True, fill=tk.BOTH)
69 center_window(self.parent)
70 self.create_widgets(export_path)
72 def create_widgets(self, export_path):
73 """
74 Sets up the main window.
75 """
76 # Export path browse dialog
77 self.browse_frame = tk.Frame(self)
78 self.browse_frame.pack(
79 side='top', fill=tk.BOTH, expand=True, padx=5, pady=5)
81 tk.Label(self.browse_frame, text="Backup location:").pack()
83 self.browse_button = tk.Button(
84 self.browse_frame, text="...", width=3,
85 command=self.browse_cmd)
86 self.browse_button.pack(side="right", padx=2)
88 self.path_value = tk.StringVar()
89 self.path_value.set(export_path)
90 self.dir_box = tk.Entry(
91 self.browse_frame, textvariable=self.path_value)
92 self.dir_box.pack(fill=tk.X, expand=True, side="right", padx=2)
94 # Confirm button
95 self.confirm = tk.Button(self)
96 self.confirm["text"] = "Confirm"
97 self.confirm["command"] = self.confirm_cmd
98 self.confirm.pack(padx=5, pady=5)
100 def confirm_cmd(self):
102 Called when the select button is pressed.
104 self.export_path = self.path_value.get()
105 self.exit()
107 def browse_cmd(self):
109 Called when the browse button is pressed.
111 folder = filedialog.askdirectory(mustexist=False)
112 if folder:
113 self.path_value.set(folder)
115 def exit(self):
117 Closes this window
119 self.parent.destroy()