9
9
import ttk
10
10
import tkMessageBox as messagebox
11
11
import tkFileDialog as filedialog
12
+ try :
13
+ import ConfigParser as configparser
14
+ except :
15
+ import configparser
16
+
17
+ from appdirs import AppDirs
12
18
import pygubu
13
19
14
20
15
21
FILE_PATH = os .path .dirname (os .path .abspath (__file__ ))
22
+ dirs = AppDirs ('pygubu-designer' )
23
+ CONFIG_FILE = os .path .join (dirs .user_data_dir , 'config' )
24
+
25
+ CUSTOM_WIDGETS = 'CUSTOM_WIDGETS'
26
+ config = configparser .SafeConfigParser ()
27
+ config .add_section (CUSTOM_WIDGETS )
28
+
29
+
30
+ def initialize_configfile ():
31
+ if not os .path .exists (dirs .user_data_dir ):
32
+ os .makedirs (dirs .user_data_dir )
33
+ if not os .path .exists (CONFIG_FILE ):
34
+ with open (CONFIG_FILE , 'w' ) as configfile :
35
+ config .write (configfile )
36
+
37
+ def save_configfile ():
38
+ with open (CONFIG_FILE , 'w' ) as configfile :
39
+ config .write (configfile )
40
+
41
+ def load_configfile ():
42
+ if not os .path .exists (CONFIG_FILE ):
43
+ initialize_configfile ()
44
+ else :
45
+ config .read (CONFIG_FILE )
46
+
47
+ def get_custom_widgets ():
48
+ paths = []
49
+ for k , p in config .items (CUSTOM_WIDGETS ):
50
+ paths .append (p )
51
+ return paths
52
+
53
+ # Get user configuration
54
+ load_configfile ()
16
55
17
56
18
57
class PreferencesUI (object ):
@@ -21,8 +60,8 @@ def __init__(self, master, translator=None):
21
60
self .master = master
22
61
self .translator = translator
23
62
self .dialog = None
24
- self .modulestv = None
25
63
self ._create_preferences_dialog ()
64
+ self ._load_options ()
26
65
27
66
def _create_preferences_dialog (self ):
28
67
builder = pygubu .Builder (self .translator )
@@ -31,21 +70,47 @@ def _create_preferences_dialog(self):
31
70
32
71
top = self .master .winfo_toplevel ()
33
72
self .dialog = dialog = builder .get_object ('preferences' , top )
34
- self .modulestv = builder .get_object ('modules ' )
35
-
73
+ self .cwtv = builder .get_object ('cwtv ' )
74
+ self . path_remove = builder . get_object ( 'path_remove' )
36
75
builder .connect_callbacks (self )
76
+
77
+ def _load_options (self ):
78
+ for k , p in config .items (CUSTOM_WIDGETS ):
79
+ self .cwtv .insert ('' , 'end' , text = p )
80
+ self ._configure_path_remove ()
81
+
82
+ def _save_options (self ):
83
+ config .remove_section (CUSTOM_WIDGETS )
84
+ config .add_section (CUSTOM_WIDGETS )
85
+ paths = []
86
+ for iid in self .cwtv .get_children ():
87
+ txt = self .cwtv .item (iid , 'text' )
88
+ paths .append (txt )
89
+ for j , p in enumerate (paths ):
90
+ config .set (CUSTOM_WIDGETS , 'w{0}' .format (j ), p )
91
+ save_configfile ()
37
92
38
- def on_btnclose_clicked (self ):
93
+ def _configure_path_remove (self ):
94
+ if len (self .cwtv .get_children ()):
95
+ self .path_remove .configure (state = 'normal' )
96
+ else :
97
+ self .path_remove .configure (state = 'disabled' )
98
+
99
+ def on_dialog_close (self , event = None ):
100
+ self ._save_options ()
39
101
self .dialog .close ()
40
102
41
103
def on_pathremove_clicked (self ):
42
- pass
104
+ items = self .cwtv .selection ()
105
+ self .cwtv .delete (* items )
106
+ self ._configure_path_remove ()
43
107
44
108
def on_pathadd_clicked (self ):
45
109
options = {
46
110
'defaultextension' : '.py' ,
47
111
'filetypes' : ((_ ('Python module' ), '*.py' ), (_ ('All' ), '*.*' ))}
48
112
fname = filedialog .askopenfilename (** options )
49
113
if fname :
50
- self .modulestv .insert ('' , tk .END , text = fname )
114
+ self .cwtv .insert ('' , tk .END , text = fname )
115
+ self ._configure_path_remove ()
51
116
0 commit comments