From 4c968e83624b037837636f6a0064874a3dbeb0b3 Mon Sep 17 00:00:00 2001 From: Martin Sivak Date: Mon, 30 Aug 2010 14:32:55 +0200 Subject: Standardize option passing in Questions and add Back & Abort buttons to Config question. Also add radio button mode to Config question. --- frontend/frontend_gtk.py | 29 +++++++++++++++++++----- frontend/gtk-list.xml | 55 ++++++++++++++++++++++++++++++++++++++++------ pyfirstaidkit/reporting.py | 48 +++++++++++++++++++++------------------- 3 files changed, 96 insertions(+), 36 deletions(-) diff --git a/frontend/frontend_gtk.py b/frontend/frontend_gtk.py index 0eab401..665f830 100644 --- a/frontend/frontend_gtk.py +++ b/frontend/frontend_gtk.py @@ -25,6 +25,7 @@ import gobject #we need gobject.idle_add import copy import logging from pyfirstaidkit import reporting +from pyfirstaidkit.returns import * import pprint import os.path import thread @@ -317,12 +318,14 @@ class CallbacksFlagList(object): return True class ListDialog(object): - def __init__(self, title, description, items, dir="", mode = 0): + def __init__(self, title, description, items, dir="", options = {}): gtkb = gtk.Builder() gtkb.add_from_file(os.path.join(dir, "gtk-list.xml")) self._dialog = gtkb.get_object("listdialog") self._dialog.set_title(title) + mode = options.get("mode", 0) + # text if mode == 0: self._store = gtk.ListStore(gobject.TYPE_STRING, @@ -339,7 +342,7 @@ class ListDialog(object): # checkboxes - elif mode == 1: + elif mode == 1 or mode == 3: self._store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_BOOLEAN, @@ -349,6 +352,7 @@ class ListDialog(object): gobject.TYPE_NONE) rend_text_check = gtk.CellRendererToggle() + rend_text_check.set_radio(mode == 3) rend_text_check.connect('toggled', self.toggled_cb, self._store) rend_text_check.set_property('activatable', True) @@ -397,7 +401,7 @@ class ListDialog(object): # value column if mode == 0: col_1 = gtk.TreeViewColumn('Value', rend_text_edit, text = 2) - elif mode == 1: + elif mode == 1 or mode == 3: col_1 = gtk.TreeViewColumn('Value', rend_text_check) col_1.add_attribute(rend_text_check, "active", 2) elif mode == 2: @@ -430,7 +434,14 @@ class ListDialog(object): err.destroy() def toggled_cb(self, cell, path, store): - store[path][2] = not store[path][2] + new = not store[path][2] + if cell.get_radio(): + i = store.get_iter_first() + while i: + store[i][2] = False + i = store.iter_next(i) + + store[path][2] = new def changed_cb(self, combo, path, new, store): store[path][2] = store[path][6][new] @@ -788,7 +799,7 @@ class MainWindow(object): vbox = glade.get_widget("choice_question_vbox") radio_map = {} group = None - for (value, name) in question.options: + for (value, name) in question.choices: r = gtk.RadioButton(group, name, False) radio_map[r] = value r.show() @@ -864,7 +875,7 @@ class MainWindow(object): description = question.description, items = question.items, dir = os.path.dirname(self._glade.relative_file(".")), - mode = question.mode + options = question.options ) res = dlg.run() @@ -873,6 +884,12 @@ class MainWindow(object): question.send_answer(message, dlg.items(), origin = self) elif res==gtk.RESPONSE_CANCEL: question.send_answer(message, [], origin = self) + elif res==gtk.RESPONSE_DELETE_EVENT: + question.send_answer(message, [], origin = self) + elif res==2: + question.send_answer(message, ReturnBack, origin = self) + elif res==1: + question.send_answer(message, ReturnAbort, origin = self) else: raise Exception("Unknown value %s" % (res,)) diff --git a/frontend/gtk-list.xml b/frontend/gtk-list.xml index 0a71aee..22b5674 100644 --- a/frontend/gtk-list.xml +++ b/frontend/gtk-list.xml @@ -65,13 +65,12 @@ True end - - Save and continue + + gtk-go-back True True True - - + True False @@ -79,9 +78,34 @@ 0 + + + gtk-stop + True + True + True + True + + + False + False + 1 + + + + + True + vertical + + + False + False + 2 + + - Use defaults and continue + Use defaults True True True @@ -91,7 +115,22 @@ False False - 1 + 3 + + + + + OK + True + True + True + + + + + False + False + 4 @@ -104,8 +143,10 @@ - ok_button + button1 + button2 cancel_button + ok_button diff --git a/pyfirstaidkit/reporting.py b/pyfirstaidkit/reporting.py index 88a7114..ec14b13 100644 --- a/pyfirstaidkit/reporting.py +++ b/pyfirstaidkit/reporting.py @@ -66,8 +66,9 @@ class Question(object): Object identity is used to match questions and replies.""" - def __init__(self, prompt): + def __init__(self, prompt, options = {}): self.prompt = prompt + self.options = options def send_answer(self, question_message, answer, origin = None): assert question_message["message"] is self @@ -82,15 +83,15 @@ class ConfigQuestion(Question): Each item is a tuple (id, title, value, tooltip, regexp_validator, validator_error_msg)""" - def __init__(self, title, description, items, mode = 0): - super(ConfigQuestion, self).__init__(title) + def __init__(self, title, description, items, options = {}): + super(ConfigQuestion, self).__init__(title, options) assert len(items) > 0 self.title = title self.description = description - self.mode = mode + self.mode = options.get("mode", 1) def _fillrow(x): - if mode == 2: + if self.mode == 2: model = x[6] else: model = None @@ -105,10 +106,10 @@ class ChoiceQuestion(Question): Each option is a tuple of (return value, description).""" - def __init__(self, prompt, options): - super(ChoiceQuestion, self).__init__(prompt) - assert len(options) > 0 - self.options = options + def __init__(self, prompt, choices, options = {}): + super(ChoiceQuestion, self).__init__(prompt, options) + assert len(choices) > 0 + self.choices = choices class TextQuestion(Question): """A question that asks for a string.""" @@ -121,8 +122,8 @@ class FilenameQuestion(TextQuestion): class PasswordQuestion(Question): """A question that asks for a password.""" - def __init__(self, prompt, confirm): - super(PasswordQuestion, self).__init__(prompt) + def __init__(self, prompt, confirm, options = {}): + super(PasswordQuestion, self).__init__(prompt, options) self.confirm = confirm class Reports(object): @@ -314,9 +315,9 @@ class Reports(object): mb.closeMailbox() return answer - def choice_question(self, reply_mb, prompt, options, origin, level = PLUGIN, - importance = logging.ERROR): - q = ChoiceQuestion(prompt, options) + def choice_question(self, reply_mb, prompt, choices, origin, level = PLUGIN, + importance = logging.ERROR, options = {}): + q = ChoiceQuestion(prompt, choices, options) self.put(q, origin, level, CHOICE_QUESTION, importance = importance, reply = reply_mb) return q @@ -325,8 +326,8 @@ class Reports(object): return self.__blocking_question(self.choice_question, args, kwargs) def text_question(self, reply_mb, prompt, origin, level = PLUGIN, - importance = logging.ERROR): - q = TextQuestion(prompt) + importance = logging.ERROR, options = {}): + q = TextQuestion(prompt, options) self.put(q, origin, level, TEXT_QUESTION, importance = importance, reply = reply_mb) return q @@ -335,8 +336,8 @@ class Reports(object): return self.__blocking_question(self.text_question, args, kwargs) def password_question(self, reply_mb, prompt, origin, level = PLUGIN, - importance = logging.ERROR, confirm = False): - q = PasswordQuestion(prompt, confirm) + importance = logging.ERROR, confirm = False, options = {}): + q = PasswordQuestion(prompt, confirm, options) self.put(q, origin, level, PASSWORD_QUESTION, importance = importance, reply = reply_mb) return q @@ -345,8 +346,8 @@ class Reports(object): return self.__blocking_question(self.password_question, args, kwargs) def filename_question(self, reply_mb, prompt, origin, level = PLUGIN, - importance = logging.ERROR): - q = FilenameQuestion(prompt) + importance = logging.ERROR, options = {}): + q = FilenameQuestion(prompt, options) self.put(q, origin, level, FILENAME_QUESTION, importance = importance, reply = reply_mb) return q @@ -354,9 +355,10 @@ class Reports(object): def filename_question_wait(self, *args, **kwargs): return self.__blocking_question(self.filename_question, args, kwargs) - def config_question(self, reply_mb, title, description, items, origin, level = PLUGIN, - importance = logging.ERROR, mode = 0): - q = ConfigQuestion(title, description, items, mode = mode) + def config_question(self, reply_mb, title, description, + items, origin, level = PLUGIN, + importance = logging.ERROR, options = {}): + q = ConfigQuestion(title, description, items, options = options) self.put(q, origin, level, CONFIG_QUESTION, importance = importance, reply = reply_mb) return q -- cgit