From 89223c8c573bddc335e8c641ea9cb2b0c33bdc5f Mon Sep 17 00:00:00 2001 From: Lamisator Date: Sun, 6 Aug 2017 03:56:50 +0200 Subject: [PATCH] Added/updated test-only files --- src/dialogs.py | 112 +++++++++++++++++++++++++++++++++++ src/fileChoserDialog.py | 30 ++++++++++ src/nobj_fileChoserDialog.py | 30 ++++++++++ src/nobj_selectionDialog.py | 40 +++++++++++++ src/popup.py | 30 ++++++++++ 5 files changed, 242 insertions(+) create mode 100644 src/dialogs.py create mode 100644 src/fileChoserDialog.py create mode 100644 src/nobj_fileChoserDialog.py create mode 100644 src/nobj_selectionDialog.py create mode 100644 src/popup.py diff --git a/src/dialogs.py b/src/dialogs.py new file mode 100644 index 0000000..3226d99 --- /dev/null +++ b/src/dialogs.py @@ -0,0 +1,112 @@ +import sys + +import urwid + + +class DialogExit(Exception): + pass + + +class DialogDisplay: + palette = [ + ('body','black','light gray', 'standout'), + ('border','black','dark blue'), + ('shadow','white','black'), + ('selectable','black', 'dark cyan'), + ('focus','white','dark blue','bold'), + ('focustext','light gray','dark blue'), + ] + + def __init__(self, text, height, width, body=None): + width = int(width) + if width <= 0: + width = ('relative', 80) + height = int(height) + if height <= 0: + height = ('relative', 80) + + self.body = body + if body is None: + # fill space with nothing + body = urwid.Filler(urwid.Divider(),'top') + + self.frame = urwid.Frame( body, focus_part='footer') + if text is not None: + self.frame.header = urwid.Pile( [urwid.Text(text), + urwid.Divider()] ) + w = self.frame + + # pad area around listbox + w = urwid.Padding(w, ('fixed left',2), ('fixed right',2)) + w = urwid.Filler(w, ('fixed top',1), ('fixed bottom',1)) + w = urwid.AttrWrap(w, 'body') + + # "shadow" effect + w = urwid.Columns( [w,('fixed', 2, urwid.AttrWrap( + urwid.Filler(urwid.Text(('border',' ')), "top") + ,'shadow'))]) + w = urwid.Frame( w, footer = + urwid.AttrWrap(urwid.Text(('border',' ')),'shadow')) + + # outermost border area + w = urwid.Padding(w, 'center', width ) + w = urwid.Filler(w, 'middle', height ) + w = urwid.AttrWrap( w, 'border' ) + + self.view = w + + + def add_buttons(self, buttons): + l = [] + for name, exitcode in buttons: + b = urwid.Button( name, self.button_press ) + b.exitcode = exitcode + b = urwid.AttrWrap( b, 'selectable','focus' ) + l.append( b ) + self.buttons = urwid.GridFlow(l, 10, 3, 1, 'center') + self.frame.footer = urwid.Pile( [ urwid.Divider(), + self.buttons ], focus_item = 1) + + def button_press(self, button): + raise DialogExit(button.exitcode) + + def main(self): + self.loop = urwid.MainLoop(self.view, self.palette) + try: + self.loop.run() + except DialogExit: + return self.on_exit( e.args[0] ) + + def on_exit(self, exitcode): + return exitcode, "" + + + + +class InputDialogDisplay(DialogDisplay): + def __init__(self, text, height, width): + self.edit = urwid.Edit() + body = urwid.ListBox([self.edit]) + body = urwid.AttrWrap(body, 'selectable','focustext') + + DialogDisplay.__init__(self, text, height, width, body) + + self.frame.set_focus('body') + + def unhandled_key(self, size, k): + if k in ('up','page up'): + self.frame.set_focus('body') + if k in ('down','page down'): + self.frame.set_focus('footer') + if k == 'enter': + # pass enter to the "ok" button + self.frame.set_focus('footer') + self.view.keypress( size, k ) + + def on_exit(self, exitcode): + return exitcode, self.edit.get_edit_text() + +def do_inputbox(text, height, width): + d = InputDialogDisplay( text, height, width ) + d.add_buttons([ ("Exit", 0) ]) + return d diff --git a/src/fileChoserDialog.py b/src/fileChoserDialog.py new file mode 100644 index 0000000..f6f63ae --- /dev/null +++ b/src/fileChoserDialog.py @@ -0,0 +1,30 @@ +from selectionDialog import SelectionDialog +import os + + +def openFileChoserDialog(title, path, preview = True): + choices = list() + file_list = list() + + for filename in os.listdir(path): + fullpath = path + "/" + filename + file_list.append(fullpath) + with open(fullpath, "r") as fhandle: + appendstring = "" + if preview: + line = fhandle.readline() + line = line.rstrip() + if len(line) > 20: + line = line[:17] + line += "..." + appendstring += line + "\n(" + + appendstring += filename + + if preview: + appendstring += ")" + + choices.append(appendstring) + + choice = SelectionDialog(title, choices, 20).start() + print(file_list[choice]) diff --git a/src/nobj_fileChoserDialog.py b/src/nobj_fileChoserDialog.py new file mode 100644 index 0000000..f6f63ae --- /dev/null +++ b/src/nobj_fileChoserDialog.py @@ -0,0 +1,30 @@ +from selectionDialog import SelectionDialog +import os + + +def openFileChoserDialog(title, path, preview = True): + choices = list() + file_list = list() + + for filename in os.listdir(path): + fullpath = path + "/" + filename + file_list.append(fullpath) + with open(fullpath, "r") as fhandle: + appendstring = "" + if preview: + line = fhandle.readline() + line = line.rstrip() + if len(line) > 20: + line = line[:17] + line += "..." + appendstring += line + "\n(" + + appendstring += filename + + if preview: + appendstring += ")" + + choices.append(appendstring) + + choice = SelectionDialog(title, choices, 20).start() + print(file_list[choice]) diff --git a/src/nobj_selectionDialog.py b/src/nobj_selectionDialog.py new file mode 100644 index 0000000..df8653d --- /dev/null +++ b/src/nobj_selectionDialog.py @@ -0,0 +1,40 @@ +import urwid +import sys + +class SelectionDialog(object): + +def menu(title, choices): + body = [ urwid.Text(title, align = "center"), urwid.Divider()] + body.append(headline) + for c in choices: + button = urwid.Button(c) + urwid.connect_signal(button, 'click', self.item_chosen, c) + body.append(urwid.AttrMap(button, None, focus_map='reversed')) + body.append(urwid.Text("------")) + return urwid.ListBox(urwid.SimpleFocusListWalker(body)) + +def item_chosen(self, button, choice): + self.choice = choice + self.exit_program() + +def exit_program(self): + raise urwid.ExitMainLoop() + +def __init__ (self, title, choices, width = 60, height = 60): + self.choices = choices + self.mainpd = urwid.Padding(self.menu(title, choices), left=1, right=1) + self.top = urwid.Overlay(self.mainpd, urwid.SolidFill(u'\N{MEDIUM SHADE}'), + align='center', width=('relative', 60), + valign='middle', height=('relative', 60), + min_width=20, min_height=9) + + bt = urwid.BigText("Notology.", urwid.font.HalfBlock5x4Font()) + bt = urwid.Padding(bt, "center", width = "clip") + #bt = urwid.Filler(bt, "bottom") + bt = urwid.BoxAdapter(bt, height = 5) + self.main_frame = urwid.Frame(self.top, header = bt) + +def start(self): + urwid.MainLoop(self.main_frame, palette=[('reversed', 'standout', '')]).run() + return self.choices.index(self.choice) + diff --git a/src/popup.py b/src/popup.py new file mode 100644 index 0000000..dd4df53 --- /dev/null +++ b/src/popup.py @@ -0,0 +1,30 @@ +import urwid + +class PopUpDialog(urwid.WidgetWrap): + """A dialog that appears with nothing but a close button """ + signals = ['close'] + def __init__(self): + close_button = urwid.Button("that's pretty cool") + urwid.connect_signal(close_button, 'click', + lambda button:self._emit("close")) + pile = urwid.Pile([urwid.Text( + "^^ I'm attached to the widget that opened me. " + "Try resizing the window!\n"), close_button]) + fill = urwid.Filler(pile) + self.__super.__init__(urwid.AttrWrap(fill, 'popbg')) + + +class Popup(urwid.PopUpLauncher): + def __init__(self): + self.__super.__init__(urwid.Button("click-me")) + urwid.connect_signal(self.original_widget, 'click', + lambda button: self.open_pop_up()) + + def create_pop_up(self): + pop_up = PopUpDialog() + urwid.connect_signal(pop_up, 'close', + lambda button: self.close_pop_up()) + return pop_up + + def get_pop_up_parameters(self): + return {'left':0, 'top':1, 'overlay_width':32, 'overlay_height':7}