Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox
- from os import path
- from json import load
- from cache import Cache
- from widgets import Container, Dialog
- from menus import PopupMenu, MenuSeparator
- from copy import deepcopy
- from content.pages.home.config.setup import setup
- from time import strftime, localtime
- from tkinter import messagebox, TclError
- ABS_PATH = path.dirname(path.realpath(__file__))
- PAGES_PATH = path.join(f'{ABS_PATH}', 'plugins')
- IMAGES = path.join(f'{ABS_PATH}', 'images')
- CONFIG = path.join(f'{ABS_PATH}', 'config')
- popups = {
- 'default': {
- 'type': 'PopupMenu',
- 'config': [
- ('bd', 1),
- ('font', 'helvetica 10'),
- ('relief', 'raised'),
- ('tearoff', 0),
- ('foreground', '#000000'),
- ('background', '#eff0f1'),
- ('activeforeground', '#ffffff'),
- ('activebackground', '#00aaff'),
- ],
- 'cut': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Cut'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'copy': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Copy'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'paste': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Paste'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'sep1': {
- 'type': 'MenuSeparator',
- },
- 'select_all': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Select All'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- },
- 'treeview': {
- 'type': 'PopupMenu',
- 'config': [
- ('bd', 1),
- ('font', 'helvetica 10'),
- ('relief', 'raised'),
- ('tearoff', 0),
- ('foreground', '#000000'),
- ('background', '#eff0f1'),
- ('activeforeground', '#ffffff'),
- ('activebackground', '#00aaff'),
- ],
- 'add_item': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Add Item'),
- ('state', 'disabled'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'add_project': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Add Project'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'sep1': {
- 'type': 'MenuSeparator',
- },
- 'save': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Save'),
- ('state', 'disabled'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'save_all': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Save All'),
- ('state', 'disabled'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'sep2': {
- 'type': 'MenuSeparator',
- },
- 'cut': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Cut'),
- ('state', 'disabled'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'copy': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Copy'),
- ('state', 'disabled'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'paste': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Paste'),
- ('state', 'disabled'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- 'sep3': {
- 'type': 'MenuSeparator',
- },
- 'delete': {
- 'type': 'MenuItem',
- 'options': [
- ('label', 'Delete'),
- ('state', 'disabled'),
- ('image', 'drive-harddisk.png'),
- ('underline', 0),
- ('accelerator', 'Alt+D '),
- ],
- },
- },
- }
- dialogs = {
- 'add_item': {
- 'type': 'Dialog',
- 'title': 'Add Item',
- 'settings': [
- ('options', (
- ('width', 400),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'body': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('sticky', tk.NSEW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'section1': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('padx', 5),
- ('pady', (10, 0)),
- ('sticky', tk.EW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (1, {'weight': 1}),
- )),
- ],
- 'lbl_path1': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('pady', 5),
- ('sticky', tk.W),
- )),
- ('options', (
- ('text', 'Add a new item to'),
- )),
- ],
- },
- 'lbl_path2': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('row', 0),
- ('column', 1),
- ('pady', 5),
- ('sticky', tk.W),
- )),
- ('options', (
- ('text', ''),
- )),
- ],
- },
- },
- 'section2': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('padx', (0, 15)),
- ('sticky', tk.EW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (1, {'weight': 1}),
- )),
- ],
- 'lbl_name': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('row', 1),
- ('pady', (1, 0)),
- ('sticky', tk.E),
- )),
- ('options', (
- ('width', 8),
- ('anchor', tk.E),
- ('text', 'Name'),
- )),
- ],
- },
- 'ent_name': {
- 'type': 'Entry',
- 'settings': [
- ('grid', (
- ('row', 1),
- ('column', 1),
- ('padx', (5, 0)),
- ('sticky', tk.EW),
- )),
- ],
- },
- 'lbl_clear': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('row', 1),
- ('column', 991),
- ('pady', (2, 0)),
- ('sticky', tk.W),
- )),
- ('options', (
- ('text', 'clear'),
- ('cursor', 'hand1'),
- ('image', 'draw-eraser.png'),
- )),
- ],
- },
- },
- 'section3': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('pady', (10, 0)),
- ('sticky', tk.EW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'group1': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('sticky', tk.NSEW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (1, {'weight': 1}),
- )),
- ],
- 'lbl_type': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('row', 1),
- ('pady', (5, 0)),
- )),
- ('options', (
- ('width', 8),
- ('anchor', tk.E),
- ('text', 'Type'),
- )),
- ],
- },
- 'cbo_type': {
- 'type': 'Combobox',
- 'settings': [
- ('grid', (
- ('row', 1),
- ('column', 1),
- ('padx', (5, 0)),
- ('pady', (5, 0)),
- ('sticky', tk.EW),
- )),
- ('options', (
- ('value', 'Menu Item'),
- ('values', ('Menu Item', 'Check Box', 'Radio Button')),
- )),
- ],
- },
- 'lbl_default_value': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('row', 2),
- ('column', 0),
- ('pady', (10, 0)),
- )),
- ('options', (
- ('anchor', tk.E),
- ('width', 8),
- ('text', 'Value'),
- )),
- ],
- },
- 'wrapper1': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('row', 2),
- ('column', 1),
- ('padx', (5, 0)),
- ('pady', (10, 0)),
- ('columnspan', 2),
- ('sticky', tk.EW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (2, {'weight': 1}),
- )),
- ],
- 'rad_default_value': {
- 'type': 'Radiobutton',
- 'settings': [
- ('options', (
- ('value', 1),
- )),
- ],
- 'items': [
- ('item1', (
- ('grid', (
- ('row', 2),
- ('column', 1),
- ('padx', (5, 0)),
- ('sticky', tk.EW),
- )),
- ('options', (
- ('text', 'True'),
- ('value', 1),
- )),
- )),
- ('item2', (
- ('grid', (
- ('row', 2),
- ('column', 2),
- ('padx', (5, 0)),
- ('sticky', tk.EW),
- )),
- ('options', (
- ('text', 'False'),
- ('value', 0),
- )),
- )),
- ],
- },
- },
- },
- 'group2': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('row', 0),
- ('column', 1),
- ('padx', 10),
- ('pady', (5, 0)),
- ('sticky', tk.NSEW),
- )),
- ('rowconfigure', (
- (1, {'weight': 1}),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'ckb_update_navigator': {
- 'type': 'Checkbutton',
- 'settings': [
- ('grid', (
- ('columnspan', 990),
- ('padx', (5, 0)),
- ('sticky', tk.EW),
- )),
- ('options', (
- ('value', False),
- ('onvalue', True),
- ('offvalue', False),
- ('text', 'Set Focus in Navigator'),
- )),
- ],
- },
- },
- },
- 'section4': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('padx', 5),
- ('pady', (10, 5)),
- ('sticky', tk.SE),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'ok': {
- 'type': 'Button',
- 'settings': [
- ('grid', (
- ('sticky', tk.W),
- )),
- ('options', (
- ('text', 'Ok'),
- )),
- ],
- },
- 'cancel': {
- 'type': 'Button',
- 'settings': [
- ('grid', (
- ('row', 0),
- ('column', 1),
- ('padx', (5, 0)),
- )),
- ('options', (
- ('text', 'Cancel'),
- )),
- ],
- },
- },
- },
- },
- 'add_project': {
- 'type': 'Dialog',
- 'title': 'Add Project',
- 'settings': [
- ('options', (
- ('width', 400),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'body': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('sticky', tk.NSEW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'section1': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('padx', (0, 15)),
- ('pady', (20, 0)),
- ('sticky', tk.EW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (1, {'weight': 1}),
- )),
- ],
- 'lbl_name': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('row', 1),
- ('pady', (1, 0)),
- ('sticky', tk.E),
- )),
- ('options', (
- ('width', 7),
- ('anchor', tk.E),
- ('text', 'Name'),
- )),
- ],
- },
- 'ent_name': {
- 'type': 'Entry',
- 'settings': [
- ('grid', (
- ('row', 1),
- ('column', 1),
- ('padx', (5, 0)),
- ('sticky', tk.EW),
- )),
- ],
- },
- 'lbl_clear': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('row', 1),
- ('column', 991),
- ('pady', (2, 0)),
- ('sticky', tk.W),
- )),
- ('options', (
- ('text', 'clear'),
- ('cursor', 'hand1'),
- ('image', 'draw-eraser.png'),
- )),
- ],
- },
- },
- 'section2': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('pady', (10, 0)),
- ('sticky', tk.EW),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'group1': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('sticky', tk.EW),
- )),
- ('rowconfigure', (
- (1, {'weight': 1}),
- )),
- ('columnconfigure', (
- (1, {'weight': 1}),
- )),
- ],
- 'lbl_type': {
- 'type': 'Label',
- 'settings': [
- ('grid', (
- ('pady', (5, 0)),
- )),
- ('options', (
- ('width', 7),
- ('anchor', tk.E),
- ('text', 'Type'),
- )),
- ],
- },
- 'cbo_type': {
- 'type': 'Combobox',
- 'settings': [
- ('grid', (
- ('row', 0),
- ('column', 1),
- ('padx', (5, 0)),
- ('pady', (5, 0)),
- ('sticky', tk.EW),
- )),
- ('options', (
- ('value', 'Menubar'),
- ('values', ('Menubar', 'Popup Menu', 'Menu Button')),
- )),
- ],
- },
- },
- 'group2': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('row', 0),
- ('column', 1),
- ('padx', 10),
- ('pady', (5, 0)),
- ('sticky', tk.NSEW),
- )),
- ('rowconfigure', (
- (1, {'weight': 1}),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'ckb_update_navigator': {
- 'type': 'Checkbutton',
- 'settings': [
- ('grid', (
- ('columnspan', 990),
- ('padx', (5, 0)),
- ('sticky', tk.EW),
- )),
- ('options', (
- ('value', False),
- ('onvalue', True),
- ('offvalue', False),
- ('text', 'Set Focus in Navigator'),
- )),
- ],
- },
- },
- },
- 'section3': {
- 'type': 'Frame',
- 'settings': [
- ('grid', (
- ('padx', 5),
- ('pady', (15, 5)),
- ('sticky', tk.SE),
- )),
- ('rowconfigure', (
- (0, {'weight': 1}),
- )),
- ('columnconfigure', (
- (0, {'weight': 1}),
- )),
- ],
- 'ok': {
- 'type': 'Button',
- 'settings': [
- ('grid', (
- ('sticky', tk.W),
- )),
- ('options', (
- ('text', 'Ok'),
- )),
- ],
- },
- 'cancel': {
- 'type': 'Button',
- 'settings': [
- ('grid', (
- ('row', 0),
- ('column', 1),
- ('padx', (5, 0)),
- )),
- ('options', (
- ('text', 'Cancel'),
- )),
- ],
- },
- },
- },
- },
- }
- def uri2dict(uri, value):
- node = output = {}
- for _path in uri.split('/'):
- node[_path] = node = {}
- c = Cache(**output)
- c.set(uri, value)
- return c.get()
- def dump(data, indent=None):
- indent = indent if indent else '.'
- print('-------------------------------------------------------------------------------------------------------')
- if data:
- def walk(_data, count):
- count += 1
- for key, value in _data.items():
- if isinstance(value, dict):
- print(indent * count, key)
- walk(value, count)
- else:
- if isinstance(value, str):
- value = f'"{value}"'
- print(indent * count, key, f'value={value}')
- walk(data, 0)
- else:
- print(' (No Data)')
- print('-------------------------------------------------------------------------------------------------------')
- class Plugin(Container):
- def __init__(self, parent, key):
- Container.__init__(self, parent, key, **setup)
- self.data = \
- self.cache = None
- x = Cache(**{'aa/bb': 11})
- x.dump()
- exit()
- self.popups = {}
- self.parent = parent
- def execute(self):
- self.cache = Cache(**popups)
- def popup_defaults_cut():
- self.winfo_toplevel().focus_get().event_generate('<Control-x>')
- def popup_defaults_copy():
- self.winfo_toplevel().focus_get().event_generate('<Control-c>')
- def popup_defaults_paste():
- self.winfo_toplevel().focus_get().event_generate('<Control-v>')
- def popup_defaults_select_all():
- self.winfo_toplevel().focus_get().event_generate('<Control-a>')
- def dlg_add_project():
- def btn_ok(event):
- dlg.destroy()
- ent_name = dlg.element('body/section1/ent_name').get()
- cbo_type = dlg.element('body/section2/group1/cbo_type').get()
- ckb_set_focus = dlg.element('body/section2/group2/ckb_update_navigator').get()
- if not ent_name:
- tk.messagebox.showerror(
- title='Create Project',
- message='You have not entered a project name. Please enter a name and try again.'
- )
- return
- last_modification = strftime('%a, %d %b %Y %H:%M:%S', localtime())
- tv.append('', text=ent_name, iid=ent_name, values=[cbo_type, '', last_modification])
- pane.element('left/path/ent_path').set_value(f'/{ent_name}')
- pane.element('right/section1/line1/ent_name').set_value(ent_name)
- pane.element('right/section1/line1/ent_name').config(state=tk.NORMAL)
- pane.element('right/section2/group1/cbo_type').set_value(cbo_type)
- pane.element('right/section2').grid()
- pane.element('right/section2/group1').grid()
- if ckb_set_focus:
- tv.see(ent_name)
- tv.selection_set(ent_name)
- def btn_cancel(event):
- dlg.destroy()
- def ckb_navigator():
- val = navigator.get()
- settings = list(
- dialogs['add_project']['body']['section2']['group2']['ckb_update_navigator']['settings']
- )
- settings_dict = dict(settings)
- options = dict(settings_dict.get('options'))
- options['value'] = val
- settings_dict['options'] = tuple(options.items())
- dialogs['add_project']['body']['section2']['group2']['ckb_update_navigator'].update(
- **{'settings': tuple(settings_dict.items())}
- )
- dlg = Dialog(self.winfo_toplevel(), 'add_project', **dialogs['add_project'])
- dlg.element('body/section3/ok').bind('<Button-1>', btn_ok)
- dlg.element('body/section3/cancel').bind('<Button-1>', btn_cancel)
- navigator = dlg.element('body/section2/group2/ckb_update_navigator')
- navigator.config(command=ckb_navigator)
- dlg.grab_set()
- dlg.wait_window()
- def dlg_tv_add_item():
- def ok_item(event):
- dlg.destroy()
- section2 = self.element('body/pane/right/section2')
- section2.grid()
- ent_name = dlg.element('body/section2/ent_name').get()
- cbo_type = dlg.element('body/section3/group1/cbo_type').get()
- ckb_set_focus = dlg.element('body/section3/group2/ckb_update_navigator').get()
- if not ent_name:
- tk.messagebox.showerror(
- title='Add Item',
- message='You have not entered the item name. Please enter a name and try again.'
- )
- return
- tv = pane.element('left/treeview')
- parent_id = tv.selection()[0]
- child_id = f'{parent_id}_{ent_name}'
- if cbo_type:
- cbo = pane.element('right/section2/group2/cbo_type').set_value(cbo_type)
- last_modification = strftime('%a, %d %b %Y %H:%M:%S', localtime())
- tv.append(parent_id, iid=child_id, text=ent_name, values=[cbo_type, '', last_modification])
- pane.element('right/section2').grid()
- pane.element('right/section2/group2').grid()
- if ckb_set_focus:
- tv.see(child_id)
- tv.selection_set(child_id)
- if tv.item(parent_id,)['values'][0] != 'Menubar':
- values = tv.item(parent_id)['values']
- values[0] = 'Sub Menu'
- tv.item(parent_id, values=values)
- pane.element('left/path/ent_path').set_value(f'/{parent_id.replace("_", "/")}/{ent_name}')
- pane.element('right/section1/line1/ent_name').config(state='readonly')
- def cancel_item(event):
- dlg.destroy()
- def update_navigator():
- val = navigator.get()
- settings = list(dialogs['add_item']['body']['section3']['group2']['ckb_update_navigator']['settings'])
- settings_dict = dict(settings)
- options = dict(settings_dict.get('options'))
- options['value'] = val
- settings_dict['options'] = tuple(options.items())
- dialogs['add_item']['body']['section3']['group2']['ckb_update_navigator'].update(
- **{'settings': tuple(settings_dict.items())}
- )
- dlg = Dialog(self.winfo_toplevel(), 'add_item', **dialogs['add_item'])
- dlg.element('body/section4/ok').bind('<Button-1>', ok_item)
- dlg.element('body/section4/cancel').bind('<Button-1>', cancel_item)
- navigator = dlg.element('body/section3/group2/ckb_update_navigator')
- navigator.config(command=update_navigator)
- dlg.grab_set()
- dlg.grab_set()
- dlg.wait_window()
- def entry_delete(event):
- event.widget.target.clear()
- def validate_entry(value):
- if not value:
- return True
- char = value[0] if len(value) == 1 else value[-1]
- if char.isalnum() or char == '_':
- if len(value) >= 2 and value[-1] == '_' and value[-2] == '_':
- return False
- return True
- return False
- def tv_cut():
- tv.cut()
- def tv_copy():
- tv.copy()
- def tv_paste():
- tv.paste()
- def tv_remove():
- tv.remove()
- def tv_save():
- _cache = Cache()
- _cache.load(path.join(CONFIG, 'treeview.json'))
- project = tv.selection()[0]
- for _project in tv.get_children():
- if _project == project:
- _cache.set(project, tv.get_nodes(project).get(project))
- _cache.save(path.join(CONFIG, 'treeview.json'))
- break
- def tv_save_all():
- _cache = Cache(**tv.get_nodes())
- _cache.save(path.join(CONFIG, 'treeview.json'))
- def tv_selected(event=None):
- selection = tv.selection()[0]
- pane.element('right/section1/line1/ent_name').set_value(selection.split('_', 1)[0])
- section2 = pane.element('right/section2')
- section2.element('group1/cbo_type').set_value(tv.item(tv.selection()[0])['values'][0])
- pane.element('left/path/ent_path').set_value(f'/{selection.replace("_", "/")}')
- if tv.item(selection):
- if tv.item(selection)['values'][0] in ('Menubar', 'Popup Menu', 'Menu Button'):
- pane.element('right/section2/group2').grid_remove()
- pane.element('right/section2/group1').grid()
- pane.element('right/section2/group1/cbo_type').set_value(tv.item(selection)['values'][0])
- pane.element('right/section1/line1/ent_name').config(state=tk.NORMAL)
- else:
- pane.element('right/section2/group1').grid_remove()
- pane.element('right/section2/group2').grid()
- pane.element('right/section2/group2/cbo_type').set_value(tv.item(selection)['values'][0])
- pane.element('right/section1/line1/ent_name').config(state='readonly')
- def btn_update_project(event):
- prj_name = pane.element('right/section1/line1/ent_name').get()
- prj_type = pane.element('right/section2/group1/cbo_type').get()
- select = tv.selection()[0]
- _cache = Cache(**tv.get_nodes(select))
- index = tv.index(select)
- tv.delete(select)
- _cache.get(select)['text'] = prj_name
- values = _cache.get(select)['values']
- values[0] = prj_type
- _cache.get(select)['values'] = values
- tv.populate('', index, **{prj_name: _cache.get(select)})
- def pane_btn_update_project():
- if tv.selection():
- dlg = Dialog(self.winfo_toplevel(), 'add_item', **dialogs['add_item'])
- lbl_path2 = dlg.element('body/section1/lbl_path2')
- lbl_path2.set_value(f'/{tv.selection()[0]}')
- self.winfo_toplevel().wait_window(dlg)
- else:
- tk.messagebox.showerror(
- title='Create Projectssss',
- message='The project name you entered, already exists. Choose another name and try again.'
- )
- # name = dlg.element('body/section1/ent_name')
- # name.cfg = popups.get('default')
- pane = self.element('body/pane')
- tv = pane.element('left/treeview')
- file = path.join(CONFIG, 'treeview.json')
- if path.exists(file):
- with open(file) as f:
- tv.populate('', **load(f))
- images = ('left/path/lbl_clear', )
- callbacks = {
- 'left/path/lbl_clear': {
- 'target': 'left/path/ent_path',
- 'bindings': [
- ('<Button-1>', entry_delete),
- ],
- },
- 'right/section1/line1/ent_name': {
- 'config': [
- ('validate', 'key'),
- ('validatecommand', (self.parent.register(validate_entry), '%P')),
- ],
- 'bindings': [
- ('<Return>', btn_update_project),
- ('<KP_Enter>', btn_update_project),
- ],
- },
- 'right/footer/btn_update': {
- 'target': 'right/section1/ent_name',
- 'bindings': [
- ('<Button-1>', btn_update_project),
- ],
- },
- 'right/footer/btn_delete': {
- 'target': 'right/section1/ent_name',
- 'bindings': [
- ('<Button-1>', pane_btn_update_project),
- ],
- },
- 'left/treeview': {
- 'bindings': [
- ('<<TreeviewSelect>>', tv_selected),
- ],
- },
- }
- popup_defaults = dict((
- ('default/cut/options', ('command', popup_defaults_cut)),
- ('default/copy/options', ('command', popup_defaults_copy)),
- ('default/paste/options', ('command', popup_defaults_paste)),
- ('default/select_all/options', ('command', popup_defaults_select_all)),
- ))
- popup_commands = {
- 'path': {'widget': self.element('body/pane/left/path/ent_path'), 'commands': popup_defaults},
- 'ent_name': {'widget': self.element('body/pane/right/section1/line1/ent_name'), 'commands': popup_defaults},
- }
- tv_commands = dict((
- ('treeview/save/options', ('command', tv_save)),
- ('treeview/save_all/options', ('command', tv_save_all)),
- ('treeview/copy/options', ('command', tv_copy)),
- ('treeview/paste/options', ('command', tv_paste)),
- ('treeview/cut/options', ('command', tv_cut)),
- ('treeview/delete/options', ('command', tv_remove)),
- ('treeview/add_item/options', ('command', dlg_tv_add_item)),
- ('treeview/add_project/options', ('command', dlg_add_project)),
- ))
- self.create_images(images)
- self.configure_callbacks(callbacks)
- self.configure_popups(popup_commands)
- self.configure_treeview_popup(tv, tv_commands)
- return self
- @staticmethod
- def context_menu(event):
- try:
- key = event.widget.key
- event.widget.focus()
- PopupMenu(event.widget, key, **{key: event.widget.cfg}).tk_popup(event.x_root, event.y_root)
- except tk.TclError:
- pass
- def create_images(self, images, size=22):
- for uri in images:
- wdg = self.element('body/pane').element(uri)
- if wdg.image_name:
- file = path.join(IMAGES, str(size), 'png', wdg.image_name)
- wdg.options_args['image'] = tk.PhotoImage(file=file) if path.exists(file) else None
- wdg.config(image=wdg.options_args['image'])
- def configure_popups(self, data):
- def context_menu(event):
- try:
- _key = event.widget.key
- event.widget.focus()
- self.popups[_key] = PopupMenu(event.widget, _key, **{_key: event.widget.cfg}).tk_popup(event.x_root, event.y_root)
- except tk.TclError:
- pass
- for key, popup in data.items():
- widget = popup.get('widget')
- widget.commands = popup.get('commands')
- for _uri, command in widget.commands.items():
- opts = self.cache.get(_uri)
- if not opts:
- opts = self.cache.get(f'default/{_uri.split("/", 1)[1]}')
- if opts:
- opts.append(command)
- widget.cfg = popups.get(key, popups.get('default'))
- widget.bind(f'<Button-3>', context_menu)
- def configure_treeview_popup(self, parent, data):
- def test(event):
- key = event.widget.key
- parent.cfg = popups.get('treeview', ())
- self.update_idletasks()
- try:
- tv = self.element('body/pane/left/treeview')
- event.widget.focus()
- popup = PopupMenu(event.widget, key, **{key: event.widget.cfg})
- popup.tk_popup(event.x_root, event.y_root)
- if parent.selection():
- for k, p in popup.get().items():
- if not isinstance(p, MenuSeparator):
- popup.entryconfig(p.index, state='normal')
- else:
- popup.entryconfig(popup.get('add_project').index, state='normal')
- item = tv.identify('item', event.x, event.y)
- if item:
- tv.selection_set(item)
- except tk.TclError:
- pass
- for _uri, command in data.items():
- opts = self.cache.get(_uri)
- if opts:
- opts.append(command)
- parent.bind(f'<Button-3>', test)
- def configure_callbacks(self, callbacks):
- pane = self.element('body/pane')
- for uri, options in callbacks.items():
- wdg = pane.element(uri)
- for binding in options.get('bindings', ()):
- wdg.bind(binding[0], binding[1])
- wdg.config(**dict(options.get('config', ())))
- wdg.target = pane.element(options.get('target')) if 'target' in options else wdg
- def show(self):
- for k, n in self.parent.nodes.items():
- if self.key != k:
- n.grid_remove()
- self.grid(**self.grid_args)
- return self
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement