Exemplo n.º 1
0
    def _on_print(self, _view, print_op):
        """
        To print the PDF without a dialog, we would need to set the
        "Print to File" printer name. While we can set the printer by
        localized name, this obviously only works if the two
        translations match, which is brittle. If they don't match,
        calling `print_op.print_()` exits without an error, but does
        nothing. We therefore set the localized printer name as a hint,
        but don't depend on it. Instead, we display the print dialog and
        let the user make adjustments.

        see gtk/modules/printbackends/file/gtkprintbackendfile.c shows
        that the non-translated printer name is "Print to File".

        """
        print_settings = Gtk.PrintSettings()
        print_settings.set_paper_size(self._paper_size)
        print_settings.set_printer(_('Print to File'))
        print_settings.set(
            Gtk.PRINT_SETTINGS_OUTPUT_URI,
            filesystem.get_local_url(os.path.abspath(self.outfile)))
        print_settings.set(Gtk.PRINT_SETTINGS_OUTPUT_FILE_FORMAT, 'pdf')

        print_op.set_page_setup(Gtk.PageSetup())
        print_op.set_print_settings(print_settings)
        print_op.connect('finished', self._on_end_print)

        logging.info('Exporting PDF...')

        # Show print dialog.
        return False
Exemplo n.º 2
0
 def _convert_uri(uri):
     path = uri[len('file://'):] if uri.startswith('file://') else uri
     # Check if relative file exists and convert it if it does.
     if (not any(uri.startswith(proto) for proto in filesystem.REMOTE_PROTOCOLS) and
             not os.path.isabs(path)):
         path = os.path.join(data_dir, path)
         assert os.path.isabs(path), path
         if os.path.exists(path):
             uri = filesystem.get_local_url(path)
     return uri
Exemplo n.º 3
0
 def _convert_uri(uri):
     path = uri[len('file://'):] if uri.startswith('file://') else uri
     # Check if relative file exists and convert it if it does.
     if (not any(uri.startswith(proto) for proto in filesystem.REMOTE_PROTOCOLS) and
             not os.path.isabs(path)):
         path = os.path.join(data_dir, path)
         assert os.path.isabs(path), path
         if os.path.exists(path):
             uri = filesystem.get_local_url(path)
     return uri
Exemplo n.º 4
0
    def on_insert_file(self, sel_text):
        dirs = self.main_window.journal.dirs
        file_chooser = self.main_window.builder.get_object('file_chooser')
        file_chooser.set_current_folder(dirs.last_file_dir)

        response = file_chooser.run()
        file_chooser.hide()

        if response == gtk.RESPONSE_OK:
            folder = file_chooser.get_current_folder()
            # Folder is None if the file was chosen from the "recently used" section.
            if folder:
                dirs.last_file_dir = folder.decode('utf-8')
            filename = file_chooser.get_filename().decode('utf-8')
            filename = filesystem.get_local_url(filename)
            sel_text = self.main_window.day_text_field.get_selected_text()
            head, tail = os.path.split(filename)
            # It is always safer to add the "file://" protocol and the ""s
            return '[%s ""%s""]' % (sel_text or tail, filename)
Exemplo n.º 5
0
    def on_insert_file(self, sel_text):
        dirs = self.main_window.journal.dirs
        file_chooser = self.main_window.builder.get_object('file_chooser')
        file_chooser.set_current_folder(dirs.last_file_dir)

        response = file_chooser.run()
        file_chooser.hide()

        if response == Gtk.ResponseType.OK:
            folder = file_chooser.get_current_folder()
            # Folder is None if the file was chosen from the "recently used" section.
            if folder:
                dirs.last_file_dir = folder
            filename = file_chooser.get_filename()
            filename = filesystem.get_local_url(filename)
            sel_text = self.main_window.day_text_field.get_selected_text()
            _, tail = os.path.split(filename)
            # It is always safer to add the "file://" protocol and the ""s
            return '[%s ""%s""]' % (sel_text or tail, filename)
Exemplo n.º 6
0
    def on_insert_pic(self, sel_text):
        dirs = self.main_window.journal.dirs
        picture_chooser = self.main_window.builder.get_object('picture_chooser')
        picture_chooser.set_current_folder(dirs.last_pic_dir)

        # if no text is selected, we can support inserting multiple images
        picture_chooser.set_select_multiple(not sel_text)

        filter = gtk.FileFilter()
        filter.set_name("Images")
        filter.add_mime_type("image/png")
        filter.add_mime_type("image/jpeg")
        filter.add_mime_type("image/gif")
        filter.add_pattern("*.png")
        filter.add_pattern("*.jpg")
        filter.add_pattern("*.jpeg")
        filter.add_pattern("*.gif")
        filter.add_pattern("*.bmp")

        picture_chooser.add_filter(filter)

        # Add box for inserting image width.
        box = gtk.HBox()
        box.set_spacing(2)
        label = gtk.Label(_('Width (optional):'))
        width_entry = gtk.Entry(max=6)
        width_entry.set_width_chars(6)
        box.pack_start(label, False)
        box.pack_start(width_entry, False)
        box.pack_start(gtk.Label(_('pixels')), False)
        box.show_all()
        picture_chooser.set_extra_widget(box)

        response = picture_chooser.run()
        picture_chooser.hide()

        if response == gtk.RESPONSE_OK:
            folder = picture_chooser.get_current_folder()
            # Folder is None if the file was chosen from the "recently used" section.
            if folder:
                dirs.last_pic_dir = folder.decode('utf-8')

            # get requested width of image
            width_text = ''
            width = width_entry.get_text().decode('utf-8')
            if width:
                try:
                    width = int(width)
                except ValueError:
                    self.main_window.journal.show_message(_('Width must be an integer.'), error=True)
                    return
                width_text = '?%d' % width

            if sel_text:
                sel_text += ' '

            # iterate through all selected images
            lines = []
            for filename in picture_chooser.get_filenames():
                base, ext = os.path.splitext(filename.decode('utf-8'))

                # On windows firefox accepts absolute filenames only
                # with the file:// prefix
                base = filesystem.get_local_url(base)

                lines.append('[%s""%s""%s%s]' % (sel_text, base, ext, width_text))

            return '\n'.join(lines)
Exemplo n.º 7
0
    def on_insert_pic(self, sel_text):
        dirs = self.main_window.journal.dirs
        picture_chooser = self.main_window.builder.get_object(
            'picture_chooser')
        picture_chooser.set_current_folder(dirs.last_pic_dir)

        # if no text is selected, we can support inserting multiple images
        picture_chooser.set_select_multiple(not sel_text)

        filter = Gtk.FileFilter()
        filter.set_name("Images")
        filter.add_mime_type("image/bmp")
        filter.add_mime_type("image/gif")
        filter.add_mime_type("image/jpeg")
        filter.add_mime_type("image/png")
        # SVG images aren't found by MIME type on Windows.
        filter.add_pattern("*.svg")

        # File filter hides all files on MacOS.
        if not filesystem.IS_MAC:
            picture_chooser.add_filter(filter)

        # Add box for inserting image width.
        box = Gtk.HBox()
        box.set_spacing(2)
        label = Gtk.Label(label=_('Width (optional):'))
        width_entry = Gtk.Entry()
        width_entry.set_max_length(6)
        width_entry.set_width_chars(6)
        box.pack_start(label, False, False, 0)
        box.pack_start(width_entry, False, False, 0)
        box.pack_start(Gtk.Label(_('pixels')), True, True, 0)
        box.show_all()
        picture_chooser.set_extra_widget(box)

        response = picture_chooser.run()
        picture_chooser.hide()

        if response == Gtk.ResponseType.OK:
            folder = picture_chooser.get_current_folder()
            # Folder is None if the file was chosen from the "recently used" section.
            if folder:
                dirs.last_pic_dir = folder

            # get requested width of image
            width_text = ''
            width = width_entry.get_text()
            if width:
                try:
                    width = int(width)
                except ValueError:
                    self.main_window.journal.show_message(
                        _('Width must be an integer.'), error=True)
                    return
                width_text = '?%d' % width

            if sel_text:
                sel_text += ' '

            # iterate through all selected images
            lines = []
            for filename in picture_chooser.get_filenames():
                base, ext = os.path.splitext(filename)

                # On windows firefox accepts absolute filenames only
                # with the file:// prefix
                base = filesystem.get_local_url(base)

                lines.append('[%s""%s""%s%s]' %
                             (sel_text, base, ext, width_text))

            return '\n'.join(lines)