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. ''' @classmethod def run_dialog_for_model(cls, widget, model, label, syntax): text, image_file = cls(widget, label, model.generator, model.image_file, model.get_text(), syntax).run() if text is not None: model.set_from_generator(text, image_file) model.generator.cleanup() def __init__(self, widget, label, generator, image_file=None, text='', syntax=None): title = _( 'Edit %s' ) % label # T: dialog title, %s is the object name like "Equation" Dialog.__init__(self, widget, title, defaultwindowsize=(450, 300)) self.generator = generator self.log_file = None self.image_file = image_file self.result = None, None self.vpane = VPaned() self.vpane.set_position(150) self.vbox.pack_start(self.vpane, True, True, 0) self.imageview = ImageView(bgcolor='#FFF') swin = ScrolledWindow(self.imageview) swin.set_size_request(200, 50) self.vpane.pack1(swin, 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, True, 0) self.previewbutton = Gtk.Button.new_with_mnemonic(_('_Preview')) # T: button in e.g. equation editor dialog self.previewbutton.set_sensitive(False) self.previewbutton.connect('clicked', lambda o: self.update_image()) hbox.pack_start(self.previewbutton, False, True, 0) self.textview.get_buffer().connect( 'modified-changed', lambda b: self.previewbutton.set_sensitive(b.get_modified())) self.logbutton = Gtk.Button.new_with_mnemonic(_('View _Log')) # T: button in e.g. equation editor dialog self.logbutton.set_sensitive(False) self.logbutton.connect('clicked', lambda o: self.show_log()) hbox.pack_start(self.logbutton, False, True, 0) self.set_text(text) self.imageview.set_file(self.image_file) # if None sets broken image self.textview.grab_focus() 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() start, end = buffer.get_bounds() text = start.get_text(end) return self.generator.check_user_input(text) def update_image(self): text = self.get_text() try: self.image_file, self.log_file = self.generator.generate_image( text) except Error as error: self.image_file, self.log_file = None, None show_error(error) self.textview.get_buffer().set_modified(False) self.imageview.set_file(self.image_file) # if None sets broken image self.logbutton.set_sensitive(self.log_file is not None) def show_log(self): assert self.logfile, 'BUG: no logfile set (yet)' LogFileDialog(self, self.logfile).run() def do_response_ok(self): buffer = self.textview.get_buffer() if buffer.get_modified(): self.update_image() if not (self.image_file and self.image_file.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 self.result = (self.get_text(), self.image_file) return True
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)