Skip to content

Commit

Permalink
Add alternate view for widget list. fixes #106
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroautalan committed Jan 30, 2018
1 parent 6d7fb11 commit 362563d
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 112 deletions.
100 changes: 92 additions & 8 deletions pygubudesigner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
from .i18n import translator
from pygubu.widgets.accordionframe import AccordionFrame
from pygubu.widgets.autoarrangeframe import AutoArrangeFrame
from pygubu.widgets.scrollbarhelper import ScrollbarHelper
import pygubu.widgets.simpletooltip as tooltip
import pygubudesigner
from pygubudesigner.preferences import PreferencesUI, get_custom_widgets
from pygubudesigner.preferences import PreferencesUI, get_custom_widgets, get_option


#Initialize logger
Expand Down Expand Up @@ -252,6 +253,9 @@ def clear_key_pressed(event, newevent):
self.tree_editor.treeview.bind(
'<Alt-KeyPress-l>',
lambda e: self.on_edit_menuitem_clicked('grid_right'))

# On preferences save binding
self.master.bind('<<PygubuDesignerPreferencesSaved>>', self.on_preferences_saved)

#
# Setup tkk styles
Expand Down Expand Up @@ -301,11 +305,7 @@ def handler(style=s, theme=name):
menu.add_radiobutton(label=name, value=name,
variable=self.__theme_var, command=handler)

def configure_widget_list(self):
acf = AccordionFrame(self.widgetlist)
acf.grid(sticky=tk.NSEW)
acf.bind('<<AccordionGroupToggle>>', self.on_widgetlist_group_toogle)

def create_treelist(self):
root_tagset = set(('tk', 'ttk'))

#create unique tag set
Expand All @@ -330,6 +330,79 @@ def configure_widget_list(self):
def by_label(t):
return "{0}{1}".format(t[0], t[1].label)
treelist.sort(key=by_label)
return treelist

def configure_widget_list(self):
treelist = self.create_treelist()
if get_option('widget_palette') == 'accordion':
self.create_accordion_widget_list(treelist)
else:
self.create_treeview_widget_list(treelist)

def create_treeview_widget_list(self, treelist):
# sbhelper widget
sbhelper = ScrollbarHelper(self.widgetlist, scrolltype='both')

# widgetlisttv widget
widgetlisttv = ttk.Treeview(sbhelper)
widgetlisttv.configure(selectmode='browse', show='tree')
widgetlisttv.grid(column='0', row='0', sticky='nsew')

sbhelper.add_child(widgetlisttv)
sbhelper.configure(usemousewheel='true')
sbhelper.grid(column='0', row='0', sticky='nsew')

#Default widget image:
default_image = ''
try:
default_image = StockImage.get('22x22-tk.default')
except StockImageException as e:
pass

#Start building widget tree selector
roots = {}
sections = {}
for key, wc in treelist:
root, section = key.split('>')
#insert root
if root not in roots:
roots[root] = widgetlisttv.insert('', 'end', text=root)
#insert section
if key not in sections:
sections[key] = widgetlisttv.insert(roots[root], 'end', text=section)
#insert widget
w_image = default_image
try:
w_image = StockImage.get('22x22-{0}'.format(wc.classname))
except StockImageException as e:
pass

widgetlisttv.insert(sections[key], 'end', text=wc.label,
image=w_image, tags='widget', values=(wc.classname,))
widgetlisttv.tag_bind('widget', '<Double-1>', self.on_widgetlist_dclick)

#Expand prefered widget set
hidews = 'tk'
prefws = get_option('widget_set')
if hidews == prefws:
hidews = 'ttk'
widgetlisttv.item(roots[hidews], open=False)
widgetlisttv.item(roots[prefws], open=True)
for child in widgetlisttv.get_children(roots[prefws]):
widgetlisttv.item(child, open=True)

def on_widgetlist_dclick(self, event):
tv = event.widget
sel = tv.selection()
if sel:
item = sel[0]
classname = tv.item(item, 'values')[0]
self.on_add_widget_event(classname)

def create_accordion_widget_list(self, treelist):
acf = AccordionFrame(self.widgetlist)
acf.grid(sticky=tk.NSEW)
acf.bind('<<AccordionGroupToggle>>', self.on_widgetlist_group_toogle)

#Default widget image:
default_image = ''
Expand Down Expand Up @@ -372,8 +445,12 @@ def create_cb(cname):
tooltip.create(b, wc.classname)
b.grid()

#hide tk widget by default
acf.group_toogle('tk')
#Expand prefered widget set
hidews = 'tk'
prefws = get_option('widget_set')
if hidews == prefws:
hidews = 'ttk'
acf.group_toogle(hidews)
self.widgetlist_sf.reposition()

def on_widgetlist_group_toogle(self, event=None):
Expand All @@ -387,6 +464,13 @@ def on_add_widget_event(self, classname):
self.tree_editor.add_widget(classname)
self.tree_editor.treeview.focus_set()

def on_preferences_saved(self, event=None):
children = self.widgetlist.winfo_children()
if children:
child = self.widgetlist.nametowidget(children[0])
child.destroy()
self.configure_widget_list()

def on_close_execute(self):
quit = True
if self.is_changed:
Expand Down
50 changes: 42 additions & 8 deletions pygubudesigner/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
dirs = AppDirs('pygubu-designer')
CONFIG_FILE = os.path.join(dirs.user_data_dir, 'config')

CUSTOM_WIDGETS = 'CUSTOM_WIDGETS'
options = {
'widget_set': {'values': '["tk", "ttk"]', 'default':'ttk'},
'widget_palette': {'values': '["accordion", "treeview"]', 'default':'accordion'}
}

SEC_GENERAL = 'GENERAL'
SEC_CUSTOM_WIDGETS = 'CUSTOM_WIDGETS'
config = configparser.SafeConfigParser()
config.add_section(CUSTOM_WIDGETS)
config.add_section(SEC_CUSTOM_WIDGETS)
config.add_section(SEC_GENERAL)


def initialize_configfile():
Expand All @@ -40,16 +47,24 @@ def save_configfile():
config.write(configfile)

def load_configfile():
defaults = {}
for k in options:
defaults[k] = options[k]['default']
config[SEC_GENERAL] = defaults

if not os.path.exists(CONFIG_FILE):
initialize_configfile()
else:
config.read(CONFIG_FILE)

def get_custom_widgets():
paths = []
for k, p in config.items(CUSTOM_WIDGETS):
for k, p in config.items(SEC_CUSTOM_WIDGETS):
paths.append(p)
return paths

def get_option(key):
return config.get(SEC_GENERAL, key)

# Get user configuration
load_configfile()
Expand All @@ -61,35 +76,54 @@ def __init__(self, master, translator=None):
self.master = master
self.translator = translator
self.dialog = None
self.builder = None
self._create_preferences_dialog()
self._load_options()

def _create_preferences_dialog(self):
builder = pygubu.Builder(self.translator)
self.builder = builder = pygubu.Builder(self.translator)
uifile = os.path.join(FILE_PATH, "ui/preferences_dialog.ui")
builder.add_from_file(uifile)

top = self.master.winfo_toplevel()
self.dialog = dialog = builder.get_object('preferences', top)

#General
for key in ('widget_set', 'widget_palette'):
cbox = builder.get_object(key)
cbox.configure(values=options[key]['values'])

#Custom widgets
self.cwtv = builder.get_object('cwtv')
self.path_remove = builder.get_object('path_remove')
builder.connect_callbacks(self)

def _load_options(self):
for k, p in config.items(CUSTOM_WIDGETS):
# General
for key in options:
var = self.builder.get_variable(key)
var.set(get_option(key))
# Custom widgets
for k, p in config.items(SEC_CUSTOM_WIDGETS):
self.cwtv.insert('', 'end', text=p)
self._configure_path_remove()

def _save_options(self):
config.remove_section(CUSTOM_WIDGETS)
config.add_section(CUSTOM_WIDGETS)
# General
for key in options:
var = self.builder.get_variable(key)
config.set(SEC_GENERAL, key, var.get())
# Custom Widgets
config.remove_section(SEC_CUSTOM_WIDGETS)
config.add_section(SEC_CUSTOM_WIDGETS)
paths = []
for iid in self.cwtv.get_children():
txt = self.cwtv.item(iid, 'text')
paths.append(txt)
for j, p in enumerate(paths):
config.set(CUSTOM_WIDGETS, 'w{0}'.format(j), p)
config.set(SEC_CUSTOM_WIDGETS, 'w{0}'.format(j), p)
save_configfile()
self.master.event_generate('<<PygubuDesignerPreferencesSaved>>')

def _configure_path_remove(self):
if len(self.cwtv.get_children()):
Expand Down
90 changes: 74 additions & 16 deletions pygubudesigner/ui/preferences_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
<property name="propagate">True</property>
<property name="row">0</property>
<property name="sticky">nsew</property>
<columns>
<column id="0">
<property name="weight">1</property>
</column>
</columns>
<rows>
<row id="0">
<property name="weight">1</property>
</row>
</rows>
<columns>
<column id="0">
<property name="weight">1</property>
</column>
</columns>
</layout>
<child>
<object class="ttk.Notebook" id="Notebook_1">
Expand All @@ -40,7 +40,65 @@
<property name="sticky">nsew</property>
</layout>
<child>
<object class="ttk.Notebook.Tab" id="Tab_1">
<object class="ttk.Notebook.Tab" id="Tab_general">
<property name="sticky">nsew</property>
<property name="text" translatable="yes">General</property>
<child>
<object class="ttk.Frame" id="fgeneral">
<property name="height">200</property>
<property name="padding">5</property>
<property name="width">200</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">0</property>
</layout>
<child>
<object class="ttk.Label" id="lblpt">
<property name="text" translatable="yes">Prefered widget set:</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="pygubu.builder.widgets.combobox" id="widget_set">
<property name="keyvariable">string:widget_set</property>
<layout>
<property name="column">1</property>
<property name="propagate">True</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="Label_3">
<property name="text" translatable="yes">Widget Palette:</property>
<layout>
<property name="column">0</property>
<property name="propagate">True</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="pygubu.builder.widgets.combobox" id="widget_palette">
<property name="keyvariable">string:widget_palette</property>
<layout>
<property name="column">1</property>
<property name="propagate">True</property>
<property name="row">1</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="ttk.Notebook.Tab" id="Tab_cwidgets">
<property name="sticky">nsew</property>
<property name="text" translatable="yes">Custom Widgets</property>
<child>
Expand All @@ -53,16 +111,16 @@
<property name="propagate">True</property>
<property name="row">1</property>
<property name="sticky">nsew</property>
<columns>
<column id="0">
<property name="weight">1</property>
</column>
</columns>
<rows>
<row id="1">
<property name="weight">1</property>
</row>
</rows>
<columns>
<column id="0">
<property name="weight">1</property>
</column>
</columns>
</layout>
<child>
<object class="ttk.Label" id="Label_1">
Expand All @@ -86,11 +144,6 @@
<property name="propagate">True</property>
<property name="row">1</property>
<property name="sticky">nswe</property>
<columns>
<column id="0">
<property name="weight">1</property>
</column>
</columns>
<rows>
<row id="0">
<property name="weight">0</property>
Expand All @@ -99,6 +152,11 @@
<property name="weight">1</property>
</row>
</rows>
<columns>
<column id="0">
<property name="weight">1</property>
</column>
</columns>
</layout>
<child>
<object class="ttk.Label" id="Label_2">
Expand Down
Loading

0 comments on commit 362563d

Please sign in to comment.