def __init__(self, parent, logging_context, output): self.logging_context = logging_context self.output = output n_error = logging_context.handler.n_error n_warning = logging_context.handler.n_warning if n_error and n_warning: text = _('%(n_error)i errors and %(n_warning)i warnings occurred, see log') % {'n_error': n_error, 'n_warning': n_warning} # T: label in export dialog elif n_error: text = _('%i errors occurred, see log') % n_error # T: label in export dialog elif n_warning: text = _('%i warnings occurred, see log') % n_warning # T: label in export dialog else: text = None MessageDialog.__init__(self, parent, (_('Export completed'), text)) # T: label in export dialog log_button = Button(_('View _Log'), stock='gtk-file') # T: button in export dialog log_button.set_sensitive(logging_context.file.exists()) log_button.connect_object( 'clicked', self.__class__.on_show_log, self) #~ open_button = #~ self.add_extra_button(open_button) self.add_extra_button(log_button)
def __init__(self, parent, logging_context, output): self.logging_context = logging_context self.output = output n_error = logging_context.handler.n_error n_warning = logging_context.handler.n_warning if n_error and n_warning: text = _( '%(n_error)i errors and %(n_warning)i warnings occurred, see log' ) % { 'n_error': n_error, 'n_warning': n_warning } # T: label in export dialog elif n_error: text = _('%i errors occurred, see log') % n_error # T: label in export dialog elif n_warning: text = _('%i warnings occurred, see log') % n_warning # T: label in export dialog else: text = None MessageDialog.__init__(self, parent, (_('Export completed'), text)) # T: label in export dialog log_button = Button(_('View _Log'), stock='gtk-file') # T: button in export dialog log_button.set_sensitive(logging_context.file.exists()) log_button.connect_object('clicked', self.__class__.on_show_log, self) #~ open_button = #~ self.add_extra_button(open_button) self.add_extra_button(log_button)
class ImageGeneratorDialog(Dialog): '''Base class for use by plugins that generate and insert an image based on textual user input. This is used e.g. by the equation editor and similar plugins. The dialog provides text input and an image view for showing previews. ''' # TODO: use uistate to remember pane position def __init__(self, ui, title, generator, image=None, syntax=None, **opt): '''Constructor @param ui: L{GtkInterface} object or parent window @param title: the dialog title @param generator: an L{ImageGeneratorClass} object @param image: image data for an image in the L{TextBuffer<zim.gui.pageview.TextBuffer>} @param syntax: optional syntax name (as understood by gtksourceview) @param opt: any other arguments to pass to the L{Dialog} constructor ''' if ui_environment['platform'] == 'maemo': defaultsize = (450,480) # Use maximum available vertical space because decorations take # too much real state else: defaultsize = (450,300) Dialog.__init__(self, ui, title, defaultwindowsize=defaultsize, **opt) if ui_environment['platform'] == 'maemo': self.resize(450,480) # Force maximum dialog size under maemo, otherwise # we'll end with a too small dialog and no way to resize it self.generator = generator self.imagefile = None self.logfile = None self.vpane = VPaned() self.vpane.set_position(150) self.vbox.add(self.vpane) self.imageview = ImageView(bgcolor='#FFF', checkerboard=False) self.vpane.pack1(self.imageview, resize=True) # TODO scrolled window and option to zoom in / real size window, textview = ScrolledSourceView(syntax=syntax) self.textview = textview self.textview.set_editable(True) self.vpane.pack2(window, resize=False) hbox = gtk.HBox(spacing=5) self.vbox.pack_start(hbox, False) self.previewbutton = Button(_('_Preview'), stock='gtk-refresh') # T: button in e.g. equation editor dialog self.previewbutton.set_sensitive(False) self.previewbutton.connect_object( 'clicked', self.__class__.preview, self) hbox.pack_start(self.previewbutton, False) self.textview.get_buffer().connect('modified-changed', lambda b: self.previewbutton.set_sensitive(b.get_modified())) self.logbutton = Button(_('View _Log'), stock='gtk-file') # T: button in e.g. equation editor dialog self.logbutton.set_sensitive(False) self.logbutton.connect_object( 'clicked', self.__class__.show_log, self) if generator.uses_log_file: hbox.pack_start(self.logbutton, False) # else keep hidden self._existing_file = None if image: file = image['_src_file'] # FIXME ? textfile = self._stitch_fileextension(file, self.generator.scriptname) self._existing_file = textfile self.imageview.set_file(file) self.set_text(self.generator.filter_input(textfile.read())) self.textview.grab_focus() def _stitch_fileextension(self, file, basename): '''Stitches the file extension from 'basename' to the path of 'file' and returns a File object. ''' i = basename.rfind('.') j = file.path.rfind('.') return File(file.path[:j] + basename[i:]) def set_text(self, text): '''Set text in the buffer''' buffer = self.textview.get_buffer() buffer.set_text(text) buffer.set_modified(False) def get_text(self): '''Get the text from the buffer @returns: text as string ''' buffer = self.textview.get_buffer() bounds = buffer.get_bounds() return buffer.get_text(*bounds) def generate_image(self): '''Update the image based on the text in the text buffer''' self.imagefile = None self.logfile = None text = self.get_text() text = self.generator.process_input(text) try: imagefile, logfile = self.generator.generate_image(text) except: logger.exception('Could not generate image') # TODO set "error" image instead of "broken" image # TODO set exception text as log message else: self.imagefile = imagefile self.logfile = logfile self.textview.get_buffer().set_modified(False) def preview(self): '''Action for the "Preview" button''' self.generate_image() self.imageview.set_file(self.imagefile) # if None sets broken image self.logbutton.set_sensitive(not self.logfile is None) def show_log(self): '''Action for the "View Log" button''' assert self.logfile, 'BUG: no logfile set (yet)' LogFileDialog(self, self.logfile).run() def do_response_ok(self): if not self.imagefile \ or self.textview.get_buffer().get_modified(): self.generate_image() if not (self.imagefile and self.imagefile.exists()): dialog = QuestionDialog(self, _('An error occurred while generating the image.\nDo you want to save the source text anyway?')) # T: Question prompt when e.g. equation editor encountered an error generating the image to insert if not dialog.run(): return False if self._existing_file: textfile = self._existing_file else: page = self.ui.page dir = self.ui.notebook.get_attachments_dir(page) textfile = dir.new_file(self.generator.scriptname) textfile.write( self.generator.process_input(self.get_text()) ) imgfile = self._stitch_fileextension(textfile, self.generator.imagename) if self.imagefile and self.imagefile.exists(): self.imagefile.rename(imgfile) elif imgfile.exists(): imgfile.remove() if self._existing_file: self.ui.reload_page() else: pageview = self.ui.mainwindow.pageview # XXX pageview.insert_image(imgfile, type=self.generator.object_type, interactive=False, force=True) if self.logfile and self.logfile.exists(): self.logfile.remove() return True def destroy(self): self.generator.cleanup() Dialog.destroy(self)
class ImageGeneratorDialog(Dialog): '''Dialog that provides text input and an image view for showing previews for an L{ImageGeneratorClass} implementation. ''' # TODO: use uistate to remember pane position def __init__(self, window, title, generator, image=None, syntax=None, **opt): '''Constructor @param window: the L{MainWindow} @param title: the dialog title @param generator: an L{ImageGeneratorClass} object @param image: image data for an image in the L{TextBuffer<zim.gui.pageview.TextBuffer>} @param syntax: optional syntax name (as understood by gtksourceview) @param opt: any other arguments to pass to the L{Dialog} constructor ''' Dialog.__init__(self, window, title, defaultwindowsize=(450, 300), **opt) self.app_window = window self.generator = generator self.imagefile = None self.logfile = None self.vpane = VPaned() self.vpane.set_position(150) self.vbox.add(self.vpane) self.imageview = ImageView(bgcolor='#FFF', checkerboard=False) self.vpane.pack1(self.imageview, resize=True) # TODO scrolled window and option to zoom in / real size window, textview = ScrolledSourceView(syntax=syntax) self.textview = textview self.textview.set_editable(True) self.vpane.pack2(window, resize=False) hbox = gtk.HBox(spacing=5) self.vbox.pack_start(hbox, False) self.previewbutton = Button(_('_Preview'), stock='gtk-refresh') # T: button in e.g. equation editor dialog self.previewbutton.set_sensitive(False) self.previewbutton.connect_object('clicked', self.__class__.preview, self) hbox.pack_start(self.previewbutton, False) self.textview.get_buffer().connect( 'modified-changed', lambda b: self.previewbutton.set_sensitive(b.get_modified())) self.logbutton = Button(_('View _Log'), stock='gtk-file') # T: button in e.g. equation editor dialog self.logbutton.set_sensitive(False) self.logbutton.connect_object('clicked', self.__class__.show_log, self) if generator.uses_log_file: hbox.pack_start(self.logbutton, False) # else keep hidden if image: file = image['_src_file'] # FIXME ? textfile = self._stitch_fileextension(file, self.generator.scriptname) self._existing_file = textfile self.imageview.set_file(file) self.set_text(self.generator.filter_input(textfile.read())) else: self._existing_file = None self.set_text( self.generator.filter_input(self.generator.get_default_text())) self.textview.grab_focus() def _stitch_fileextension(self, file, basename): '''Stitches the file extension from 'basename' to the path of 'file' and returns a File object. ''' i = basename.rfind('.') j = file.path.rfind('.') return File(file.path[:j] + basename[i:]) def set_text(self, text): '''Set text in the buffer''' buffer = self.textview.get_buffer() buffer.set_text(text) buffer.set_modified(False) def get_text(self): '''Get the text from the buffer @returns: text as string ''' buffer = self.textview.get_buffer() bounds = buffer.get_bounds() return buffer.get_text(*bounds) def generate_image(self): '''Update the image based on the text in the text buffer''' self.imagefile = None self.logfile = None text = self.get_text() text = self.generator.process_input(text) try: imagefile, logfile = self.generator.generate_image(text) except: logger.exception('Could not generate image') # TODO set "error" image instead of "broken" image # TODO set exception text as log message else: self.imagefile = imagefile self.logfile = logfile self.textview.get_buffer().set_modified(False) def preview(self): '''Action for the "Preview" button''' self.generate_image() self.imageview.set_file(self.imagefile) # if None sets broken image self.logbutton.set_sensitive(not self.logfile is None) def show_log(self): '''Action for the "View Log" button''' assert self.logfile, 'BUG: no logfile set (yet)' LogFileDialog(self, self.logfile).run() def do_response_ok(self): if not self.imagefile \ or self.textview.get_buffer().get_modified(): self.generate_image() if not (self.imagefile and self.imagefile.exists()): dialog = QuestionDialog( self, _('An error occurred while generating the image.\nDo you want to save the source text anyway?' )) # T: Question prompt when e.g. equation editor encountered an error generating the image to insert if not dialog.run(): return False if self._existing_file: textfile = self._existing_file else: page = self.app_window.ui.page # XXX dir = self.app_window.ui.notebook.get_attachments_dir(page) # XXX textfile = dir.new_file(self.generator.scriptname) textfile.write(self.generator.process_input(self.get_text())) imgfile = self._stitch_fileextension(textfile, self.generator.imagename) if self.imagefile and self.imagefile.exists(): self.imagefile.rename(imgfile) elif imgfile.exists(): imgfile.remove() if self._existing_file: self.app_window.ui.reload_page() # XXX else: pageview = self.app_window.pageview pageview.insert_image(imgfile, type=self.generator.object_type, interactive=False, force=True) if self.logfile and self.logfile.exists(): self.logfile.remove() return True def destroy(self): self.generator.cleanup() Dialog.destroy(self)
class ImageGeneratorDialog(Dialog): '''Base class for use by plugins that generate and insert an image based on textual user input. This is used e.g. by the equation editor and similar plugins. The dialog provides text input and an image view for showing previes. ''' # TODO: use uistate to remember pane position def __init__(self, ui, title, generator, image=None, **opt): Dialog.__init__(self, ui, title, defaultwindowsize=(450, 300), **opt) self.generator = generator self.imagefile = None self.logfile = None self._existing_file = None self.vpane = gtk.VPaned() self.vpane.set_position(100) self.vbox.add(self.vpane) self.imageview = ImageView(bgcolor='#FFF', checkboard=False) # TODO scrolled window self.vpane.add1(self.imageview) self.textview = gtk.TextView() # TODO scrolled window # TODO supply at least an Undo stack for this textview # or optionally subclass from gtksourceview self.textview.set_left_margin(5) self.textview.set_right_margin(5) self.vpane.add2(self.textview) hbox = gtk.HBox(spacing=5) self.vbox.pack_start(hbox, False) self.previewbutton = Button(_('_Preview'), stock='gtk-refresh') # T: button in e.g. equation editor dialog self.previewbutton.set_sensitive(False) self.previewbutton.connect_object( 'clicked', self.__class__.preview, self) hbox.pack_start(self.previewbutton, False) self.textview.get_buffer().connect('modified-changed', lambda b: self.previewbutton.set_sensitive(b.get_modified())) self.logbutton = Button(_('View _Log'), stock='gtk-file') # T: button in e.g. equation editor dialog self.logbutton.set_sensitive(False) self.logbutton.connect_object( 'clicked', self.__class__.show_log, self) hbox.pack_start(self.logbutton, False) if image: file = image['_src_file'] # FIXME ? textfile = self._stitch_fileextension(file, self.generator.basename) if file.exists() and textfile.exists(): self._existing_file = textfile self.imageview.set_file(file) self.set_text(textfile.read()) self.textview.grab_focus() def _stitch_fileextension(self, file, basename): '''Stitches the file extension from 'basename' to the path of 'file' and returns a File object. ''' i = basename.rfind('.') j = file.path.rfind('.') return File(file.path[:j] + basename[i:]) def set_text(self, text): buffer = self.textview.get_buffer() buffer.set_text(text) buffer.set_modified(False) def get_text(self): buffer = self.textview.get_buffer() bounds = buffer.get_bounds() return buffer.get_text(*bounds) def generate_image(self): self.imagefile = None self.logfile = None text = self.get_text() try: imagefile, logfile = self.generator.generate_image(text) except: logger.exception('Could not generate image') # TODO set "error" image instead of "broken" image # TODO set exception text as log message else: self.imagefile = imagefile self.logfile = logfile self.textview.get_buffer().set_modified(False) def preview(self): self.generate_image() self.imageview.set_file(self.imagefile) # if None sets broken image self.logbutton.set_sensitive(not self.logfile is None) def show_log(self): assert self.logfile, 'BUG: no logfile set (yet)' LogFileDialog(self, self.logfile).run() def do_response_ok(self): if not self.imagefile \ or self.textview.get_buffer().get_modified(): self.generate_image() if not (self.imagefile and self.imagefile.exists()): dialog = QuestionDialog(self, _('An error occured while generating the image.\nDo you want to save the source text anyway?')) # T: Question prompt when e.g. equation editor encountered an error genrating the image to insert if not dialog.run(): return False if self._existing_file: textfile = self._existing_file else: page = self.ui.page dir = self.ui.notebook.get_attachments_dir(page) textfile = dir.new_file(self.generator.basename) imgfile = self._stitch_fileextension(textfile, self.imagefile.basename) textfile.write( self.get_text() ) self.imagefile.rename(imgfile) if self._existing_file: self.ui.reload_page() else: pageview = self.ui.mainwindow.pageview pageview.insert_image(imgfile, type=self.generator.type, interactive=False) if self.logfile and self.logfile.exists(): self.logfile.remove() return True def destroy(self): self.generator.cleanup() Dialog.destroy(self)