zaz-favorites/source/pythonpath/main.py

193 lines
4.6 KiB
Python
Raw Normal View History

2020-12-24 21:26:05 -06:00
#!/usr/bin/env python3
import easymacro as app
ID_EXTENSION = ''
_ = None
2020-12-26 22:21:23 -06:00
PREFIX = 'fav'
2020-12-24 21:26:05 -06:00
class Controllers(object):
2020-12-25 19:39:51 -06:00
TITLE = ''
2020-12-24 21:26:05 -06:00
def __init__(self, dlg):
self.d = dlg
self.paths = []
def button_add_action(self, event):
2020-12-24 21:42:48 -06:00
path = app.paths.get_file()
2020-12-25 19:39:51 -06:00
if not path:
return
if path in self.paths:
msg = _('Path previously added')
app.msgbox(msg, self.TITLE)
return
self.paths.append(path)
2020-12-25 20:07:26 -06:00
p = app.paths(path)
2020-12-25 20:50:41 -06:00
path_img = app.paths.join(self.d.path, 'images/delete.svg')
image = app.paths.image(path_img)
self.d.grid.add_row((p.file_name, image, path))
self.d.grid.set_cell_tooltip(0, self.d.grid.row_count-1, p.path)
self.d.grid.sort(0)
2020-12-24 21:26:05 -06:00
return
def button_save_action(self, event):
2020-12-26 22:21:23 -06:00
msg = _('Want you save your favorites files?')
2020-12-24 21:26:05 -06:00
if not app.question(msg, self.TITLE):
return
2020-12-26 22:21:23 -06:00
base = f'service:{ID_EXTENSION}?{{}}'
2020-12-24 21:26:05 -06:00
submenus = []
2020-12-27 19:29:09 -06:00
for i in range(self.d.grid.row_count):
name = self.d.grid[0, i]
path = self.d.grid[2, i]
label = f'{i+1}. {name}'
2020-12-27 19:25:32 -06:00
url = app.paths.to_url(path)
sm = {'Label': label, 'CommandURL': base.format(url)}
submenus.append(sm)
sm = {'Label': '-'}
submenus.append(sm)
sm = {'Label': _('Favorites...'), 'CommandURL': base.format('config')}
submenus.append(sm)
command = 'menu.zaz.favorites'
data = {
'Label': 'Favorites',
'CommandURL': command,
'After': '.uno:RecentFileList',
'Submenu': submenus,
}
for application in ('main', 'calc', 'writer'):
app.menus[application].insert('file', data)
2020-12-26 22:21:23 -06:00
app.set_config('paths', self.paths, PREFIX)
2020-12-24 21:26:05 -06:00
self.d.close(1)
msg = _('Favorites saved correctly')
app.msgbox(msg, self.TITLE)
return
def grid_click(self, event):
2020-12-25 20:50:41 -06:00
if not self.d.grid.is_valid:
return
2020-12-24 21:26:05 -06:00
col = self.d.grid.column
row = self.d.grid.row
if col != 1:
return
2020-12-25 20:50:41 -06:00
file_name = self.d.grid[0, row]
path = self.d.grid[2, row]
msg = _('Want you delete this file?\n\n')
if app.question(msg + file_name, self.TITLE):
2020-12-24 21:26:05 -06:00
self.paths.remove(path)
self.d.grid.remove_row(row)
return
def grid_double_click(self, event):
2020-12-25 20:50:41 -06:00
if not self.d.grid.is_valid:
return
2020-12-24 21:26:05 -06:00
col = self.d.grid.column
row = self.d.grid.row
2020-12-25 20:50:41 -06:00
if col != 0:
2020-12-24 21:26:05 -06:00
return
app.msgbox(self.d.grid.get_cell_tooltip(col, row))
return
def _config():
dlg = _create_dialog()
2020-12-26 22:21:23 -06:00
path_img = app.paths.join(dlg.path, 'images/delete.svg')
image = app.paths.image(path_img)
2020-12-24 21:52:58 -06:00
2020-12-26 22:21:23 -06:00
paths = app.get_config('paths', prefix=PREFIX)
for i, path in enumerate(paths):
p = app.paths(path)
dlg.grid.add_row((p.file_name, image, path))
dlg.grid.set_cell_tooltip(0, i, p.path)
2020-12-24 21:52:58 -06:00
2020-12-26 22:21:23 -06:00
if paths:
dlg.grid.sort(0)
dlg.events.paths = paths
2020-12-24 21:26:05 -06:00
dlg.open()
return
def _create_dialog():
TITLE = _('ZAZ Favorites')
args = {
'Name': 'dialog',
'Title': TITLE,
'Width': 160,
2020-12-24 21:52:58 -06:00
'Height': 170,
2020-12-24 21:26:05 -06:00
}
dlg = app.create_dialog(args)
2020-12-24 21:42:48 -06:00
dlg.id = ID_EXTENSION
2020-12-24 21:26:05 -06:00
dlg.events = Controllers
2020-12-25 19:39:51 -06:00
dlg.events.TITLE = TITLE
2020-12-24 21:26:05 -06:00
2020-12-24 21:42:48 -06:00
args = {
'Type': 'Button',
'Name': 'button_add',
'Label': _('~Add'),
'ImageURL': 'add.svg',
'ImagePosition': 1,
'Width': 60,
'Height': 20,
'Y': 5,
}
dlg.add_control(args)
2020-12-24 21:52:58 -06:00
columns = (
{'Title': _('File Name'), 'MaxWidth': 120},
2020-12-25 20:23:45 -06:00
{'Title': '-', 'HorizontalAlign': 1, 'Resizeable': False, 'ColumnWidth': 10},
2020-12-24 21:52:58 -06:00
{'Title': '', 'Resizeable': False, 'ColumnWidth': 0},
)
args = {
'Type': 'Grid',
'Name': 'grid',
'Width': 140,
'Height': 110,
'ShowRowHeader': True,
}
grid = dlg.add_control(args)
grid.columns = columns
2020-12-24 21:42:48 -06:00
args = {
'Type': 'Button',
'Name': 'button_save',
'Label': _('~Save'),
'ImageURL': 'save.svg',
'ImagePosition': 1,
'Width': 60,
'Height': 20,
}
dlg.add_control(args)
dlg.button_add.center()
2020-12-24 21:52:58 -06:00
dlg.grid.move(dlg.button_add)
dlg.button_save.move(dlg.grid)
dlg.grid.center()
2020-12-24 21:42:48 -06:00
dlg.button_save.center()
2020-12-24 21:26:05 -06:00
return dlg
2020-12-27 19:25:32 -06:00
2020-12-24 21:26:05 -06:00
def run(args, path_locales):
global _
_ = app.install_locales(path_locales)
if args == 'config':
_config()
return
app.docs.open(args)
return