示例#1
0
    def __init__(self, name, style, parent):
        """
        Create the StyleEditor.

        name - name of the style that is to be edited
        style - style object that is to be edited
        parent - StyleListDisplay object that called the editor
        """
        self.current_p = None
        self.current_name = None

        self.style = StyleSheet(style)
        self.parent = parent
        self.top = Glade(toplevel='editor')
        self.window = self.top.toplevel

        self.top.connect_signals({
            "on_save_style_clicked": self.on_save_style_clicked,
            "destroy_passed_object": self.__close,
            "on_ok_clicked": dummy_callback,
            "on_add_clicked": dummy_callback,
            "on_delete_clicked": dummy_callback,
            "on_button_press": dummy_callback,
            "on_edit_clicked": dummy_callback,
        })

        self.pname = self.top.get_object('pname')
        self.pdescription = self.top.get_object('pdescription')

        ManagedWindow.set_titles(self.window, self.top.get_object('title'),
                                 _('Style editor'))
        self.top.get_object("label6").set_text(_("point size|pt"))

        titles = [(_('Paragraph'), 0, 130)]
        self.plist = ListModel.ListModel(self.top.get_object("ptree"), titles,
                                         self.change_display)

        self.top.get_object('color').connect('color-set', self.fg_color_set)
        self.top.get_object('bgcolor').connect('color-set', self.bg_color_set)
        self.top.get_object("style_name").set_text(name)

        names = self.style.get_paragraph_style_names()
        names.reverse()
        for p_name in names:
            self.plist.add([p_name], self.style.get_paragraph_style(p_name))
        self.plist.select_row(0)

        if self.parent:
            self.window.set_transient_for(parent.window)
        self.window.run()
        self.window.destroy()
示例#2
0
def append_styles(selected_style, item):
    """
    Append the styles for a book item to the stylesheet.
    """
    # Set up default style
    default_style = StyleSheet()
    make_default_style = item.option_class.make_default_style
    make_default_style(default_style)

    # Read all style sheets available for this item
    style_file = item.option_class.handler.get_stylesheet_savefile()
    style_list = StyleSheetList(style_file, default_style)

    # Get the selected stylesheet
    style_name = item.get_style_name()
    style_sheet = style_list.get_style_sheet(style_name)

    for this_style_name in style_sheet.get_paragraph_style_names():
        selected_style.add_paragraph_style(
            this_style_name, style_sheet.get_paragraph_style(this_style_name))

    for this_style_name in style_sheet.get_draw_style_names():
        selected_style.add_draw_style(
            this_style_name, style_sheet.get_draw_style(this_style_name))

    for this_style_name in style_sheet.get_table_style_names():
        selected_style.add_table_style(
            this_style_name, style_sheet.get_table_style(this_style_name))

    for this_style_name in style_sheet.get_cell_style_names():
        selected_style.add_cell_style(
            this_style_name, style_sheet.get_cell_style(this_style_name))

    return selected_style
示例#3
0
    def setup_style_frame(self):
        """Set up the style frame of the dialog.  This function relies
        on other routines create the default style for this report,
        and to read in any user defined styles for this report.  It
        the builds a menu of all the available styles for the user to
        choose from."""
        # Build the default style set for this report.
        self.default_style = StyleSheet()
        self.options.make_default_style(self.default_style)

        if self.default_style.is_empty():
            # Don't display the option of no styles are used
            return

        # Styles Frame
        label = gtk.Label("%s:" % _("Style"))
        label.set_alignment(0.0, 0.5)

        self.style_menu = StyleComboBox()
        self.style_button = gtk.Button("%s..." % _("Style Editor"))
        self.style_button.connect('clicked', self.on_style_edit_clicked)

        self.tbl.attach(label, 1, 2, self.row, self.row+1, gtk.SHRINK|gtk.FILL)
        self.tbl.attach(self.style_menu, 2, 3, self.row, self.row+1,
                        yoptions=gtk.SHRINK)
        self.tbl.attach(self.style_button, 3, 4, self.row, self.row+1,
                        xoptions=gtk.SHRINK|gtk.FILL, yoptions=gtk.SHRINK)
        self.row += 1
        
        # Build the initial list of available styles sets.  This
        # includes the default style set and any style sets saved from
        # previous invocations of gramps.
        self.style_sheet_list = StyleSheetList(
                            self.options.handler.get_stylesheet_savefile(),
                            self.default_style)

        # Now build the actual menu.
        style = self.options.handler.get_default_stylesheet_name()
        self.build_style_menu(style)
示例#4
0
    def make_document(self):
        """Create a document of the type requested by the user."""
        user = gui.user.User()
        self.rptlist = []
        selected_style = StyleSheet()

        pstyle = self.paper_frame.get_paper_style()
        self.doc = self.format(None, pstyle)

        for item in self.book.get_item_list():
            item.option_class.set_document(self.doc)
            report_class = item.get_write_item()
            obj = write_book_item(self.database, report_class,
                                  item.option_class, user)
            self.rptlist.append(obj)
            append_styles(selected_style, item)

        self.doc.set_style_sheet(selected_style)
        self.doc.open(self.target_path)
示例#5
0
def cl_report(database, name, category, options_str_dict):

    clr = CommandLineReport(database, name, category, BookOptions,
                            options_str_dict)

    # Exit here if show option was given
    if clr.show:
        return

    if 'bookname' not in clr.options_dict or not clr.options_dict['bookname']:
        print _("Please specify a book name")
        return

    book_list = BookList('books.xml', database)
    book_name = clr.options_dict['bookname']
    if book_name:
        if book_name not in book_list.get_book_names():
            print _("No such book '%s'") % book_name
            return
        book = book_list.get_book(book_name)
    else:
        print _("Please specify a book name")
        return

    # write report
    doc = clr.format(
        None,
        PaperStyle(clr.paper, clr.orien, clr.marginl, clr.marginr, clr.margint,
                   clr.marginb))
    user = cli.user.User()
    rptlist = []
    selected_style = StyleSheet()
    for item in book.get_item_list():

        # The option values were loaded magically by the book parser.
        # But they still need to be applied to the menu options.
        opt_dict = item.option_class.options_dict
        menu = item.option_class.menu
        for optname in opt_dict:
            menu_option = menu.get_option_by_name(optname)
            if menu_option:
                menu_option.set_value(opt_dict[optname])

        item.option_class.set_document(doc)
        report_class = item.get_write_item()
        obj = write_book_item(database, report_class, item.option_class, user)
        if obj:
            append_styles(selected_style, item)
            rptlist.append(obj)

    doc.set_style_sheet(selected_style)
    doc.open(clr.option_class.get_output())
    doc.init()
    newpage = 0
    for rpt in rptlist:
        if newpage:
            doc.page_break()
        newpage = 1
        rpt.begin_report()
        rpt.write_report()
    doc.close()
示例#6
0
def make_basic_stylesheet(**kwargs):
    """
    Create the basic style sheet for the SimpleDoc class.

    kwargs - a dictionary of the form:
           item={method: value, ...}, ...

    Example:

    make_basic_stylesheet(Table={"set_width": 90})

    """
    sheet = StyleSheet()

    pstyle = ParagraphStyle()
    fstyle = pstyle.get_font()
    fstyle.set_type_face(FONT_SANS_SERIF)
    fstyle.set_size(14)
    fstyle.set_bold(True)
    pstyle.set_font(fstyle)
    pstyle.set_alignment(PARA_ALIGN_LEFT)
    # Handle args:
    if "Title" in kwargs:
        for method in kwargs["Title"]:
            value = kwargs["Title"][method]
            if value is not None:
                getattr(pstyle, method)(value)
    sheet.add_paragraph_style('Title', pstyle)

    pstyle = ParagraphStyle()
    fstyle = pstyle.get_font()
    fstyle.set_type_face(FONT_SANS_SERIF)
    fstyle.set_size(12)
    fstyle.set_bold(True)
    pstyle.set_font(fstyle)
    pstyle.set_alignment(PARA_ALIGN_LEFT)
    pstyle.set_tabs([4, 8, 12, 16])
    # Handle args:
    if "Header1" in kwargs:
        for method in kwargs["Header1"]:
            value = kwargs["Header1"][method]
            if value is not None:
                getattr(pstyle, method)(value)
    sheet.add_paragraph_style('Header1', pstyle)

    pstyle = ParagraphStyle()
    fstyle = pstyle.get_font()
    fstyle.set_type_face(FONT_SANS_SERIF)
    fstyle.set_size(10)
    fstyle.set_bold(True)
    pstyle.set_font(fstyle)
    pstyle.set_alignment(PARA_ALIGN_LEFT)
    pstyle.set_tabs([4, 8, 12, 16])
    # Handle args:
    if "Header2" in kwargs:
        for method in kwargs["Header2"]:
            value = kwargs["Header2"][method]
            if value is not None:
                getattr(pstyle, method)(value)
    sheet.add_paragraph_style('Header2', pstyle)

    pstyle = ParagraphStyle()
    fstyle = pstyle.get_font()
    fstyle.set_type_face(FONT_SANS_SERIF)
    fstyle.set_size(10)
    fstyle.set_bold(True)
    fstyle.set_italic(True)
    pstyle.set_font(fstyle)
    pstyle.set_alignment(PARA_ALIGN_LEFT)
    pstyle.set_tabs([4, 8, 12, 16])
    # Handle args:
    if "Header3" in kwargs:
        for method in kwargs["Header3"]:
            value = kwargs["Header3"][method]
            if value is not None:
                getattr(pstyle, method)(value)
    sheet.add_paragraph_style('Header3', pstyle)

    pstyle = ParagraphStyle()
    pstyle.set_tabs([4, 8, 12, 16])
    # Handle args:
    if "Normal" in kwargs:
        for method in kwargs["Normal"]:
            value = kwargs["Normal"][method]
            if value is not None:
                getattr(pstyle, method)(value)
    sheet.add_paragraph_style('Normal', pstyle)

    # Styles for tables:
    tbl = TableStyle()
    tbl.set_width(100)
    tbl.set_columns(2)
    tbl.set_column_width(0, 20)
    tbl.set_column_width(1, 80)
    # Handle args:
    if "Table" in kwargs:
        for method in kwargs["Table"]:
            value = kwargs["Table"][method]
            if value is not None:
                getattr(tbl, method)(value)
    sheet.add_table_style("Table", tbl)

    cell = TableCellStyle()
    cell.set_top_border(1)
    cell.set_bottom_border(1)
    # Handle args:
    if "TableHead" in kwargs:
        for method in kwargs["TableHead"]:
            value = kwargs["TableHead"][method]
            if value is not None:
                getattr(cell, method)(value)
    sheet.add_cell_style("TableHead", cell)

    cell = TableCellStyle()
    # Handle args:
    if "TableHeaderCell" in kwargs:
        for method in kwargs["TableHeaderCell"]:
            value = kwargs["TableHeaderCell"][method]
            if value is not None:
                getattr(cell, method)(value)
    sheet.add_cell_style("TableHeaderCell", cell)

    cell = TableCellStyle()
    cell.set_longlist(1)
    # Handle args:
    if "TableDataCell" in kwargs:
        for method in kwargs["TableDataCell"]:
            value = kwargs["TableDataCell"][method]
            if value is not None:
                getattr(cell, method)(value)
    sheet.add_cell_style("TableDataCell", cell)

    return sheet
示例#7
0
class ReportDialog(ManagedWindow.ManagedWindow):
    """
    The ReportDialog base class.  This is a base class for generating
    customized dialogs to solicit options for a report.  It cannot be
    used as is, but it can be easily sub-classed to create a functional
    dialog for a stand-alone report.
    """
    border_pad = 6

    def __init__(self, dbstate, uistate, option_class, name, trans_name,
                 track=[]):
        """Initialize a dialog to request that the user select options
        for a basic *stand-alone* report."""
        
        self.style_name = "default"
        self.firstpage_added = False
        self.raw_name = name
        self.dbstate = dbstate
        self.uistate = uistate
        self.db = dbstate.db
        self.report_name = trans_name
        
        ManagedWindow.ManagedWindow.__init__(self, uistate, track, self)

        self.init_options(option_class)
        self.init_interface()

    def close(self, *obj):
        """
        Close itself.
        cleanup things that can prevent garbage collection
        """
        if hasattr(self, 'widgets'): # handle pathlogical bug 4145
            totwidg = range(len(self.widgets))
            totwidg.reverse()
            for ind in totwidg:
                if hasattr(self.widgets[ind][1], 'clean_up'):
                    self.widgets[ind][1].clean_up()
                del self.widgets[ind]
            delattr(self, 'widgets')
        for name, fram in self.frames.iteritems():
            totwidg = range(len(fram))
            totwidg.reverse()
            for ind in totwidg:
                if hasattr(fram[ind][1], 'clean_up'):
                    fram[ind][1].clean_up()
                del fram[ind]
        self.frames.clear()
        self.frames = None
        ManagedWindow.ManagedWindow.close(self, *obj)

    def init_options(self, option_class):
        try:
            if (issubclass(option_class, object) or     # New-style class
                isinstance(option_class, ClassType)):   # Old-style class
                self.options = option_class(self.raw_name, self.db)
        except TypeError:
            self.options = option_class
        self.options.load_previous_values()

    def build_window_key(self, obj):
        key = self.raw_name
        return key

    def build_menu_names(self, obj):
        return (_("Configuration"), self.report_name)

    def init_interface(self):
        self.widgets = []
        self.frame_names = []
        self.frames = {}
        self.format_menu = None
        self.style_button = None

        self.style_name = self.options.handler.get_default_stylesheet_name()

        window = gtk.Dialog('Gramps')
        self.set_window(window, None, self.get_title())
        self.window.set_has_separator(False)
        self.window.set_modal(True)

        self.help = self.window.add_button(gtk.STOCK_HELP, gtk.RESPONSE_HELP)
        self.help.connect('clicked', self.on_help_clicked)

        self.cancel = self.window.add_button(gtk.STOCK_CANCEL,
                                             gtk.RESPONSE_CANCEL)
        self.cancel.connect('clicked', self.on_cancel)

        self.ok = self.window.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
        self.ok.connect('clicked', self.on_ok_clicked)

        self.window.set_default_size(600, -1)

        # Set up and run the dialog.  These calls are not in top down
        # order when looking at the dialog box as there is some
        # interaction between the various frames.

        self.setup_title()
        self.setup_header()
        self.tbl = gtk.Table(4, 4, False)
        self.tbl.set_col_spacings(12)
        self.tbl.set_row_spacings(6)
        self.tbl.set_border_width(6)
        self.row = 0

        # Build the list of widgets that are used to extend the Options
        # frame and to create other frames
        self.add_user_options()

        self.setup_main_options()
        self.setup_init()
        self.setup_format_frame()
        self.setup_target_frame()
        self.setup_style_frame()

        self.notebook = gtk.Notebook()
        self.notebook.set_scrollable(True)
        self.notebook.set_border_width(6)
        self.window.vbox.add(self.notebook)

        self.setup_report_options_frame()
        self.setup_other_frames()
        self.notebook.set_current_page(0)

        self.window.vbox.add(self.tbl)
        self.show()

    def get_title(self):
        """The window title for this dialog"""
        name = self.report_name
        category = standalone_categories[self.category]
        return "%s - %s - Gramps" % (name, category)
    
    #------------------------------------------------------------------------
    #
    # Functions related to extending the options
    #
    #------------------------------------------------------------------------
    def add_user_options(self):
        """Called to allow subclasses add widgets to the dialog form.
        It is called immediately before the window is displayed. All
        calls to add_option or add_frame_option should be called in
        this task."""
        add_gui_options(self)

    def parse_user_options(self):
        """Called to allow parsing of added widgets.
        It is called when OK is pressed in a dialog. 
        All custom widgets should provide a parsing code here."""
        try:
            self.options.parse_user_options()
        except:
            LOG.error("Failed to parse user options.", exc_info=True)
            
    def add_option(self, label_text, widget):
        """Takes a text string and a Gtk Widget, and stores them to be
        appended to the Options section of the dialog. The text string
        is used to create a label for the passed widget. This allows the
        subclass to extend the Options section with its own widgets. The
        subclass is responsible for all managing of the widgets, including
        extracting the final value before the report executes. This task
        should only be called in the add_user_options task."""
        self.widgets.append((label_text, widget))

    def add_frame_option(self, frame_name, label_text, widget):
        """Similar to add_option this method takes a frame_name, a
        text string and a Gtk Widget. When the interface is built,
        all widgets with the same frame_name are grouped into a
        GtkFrame. This allows the subclass to create its own sections,
        filling them with its own widgets. The subclass is responsible for
        all managing of the widgets, including extracting the final value
        before the report executes. This task should only be called in
        the add_user_options task."""
        
        if frame_name in self.frames:
            self.frames[frame_name].append((label_text, widget))
        else:
            self.frames[frame_name] = [(label_text, widget)]
            self.frame_names.append(frame_name)
        
    #------------------------------------------------------------------------
    #
    # Functions to create a default output style.
    #
    #------------------------------------------------------------------------

    def build_style_menu(self, default=None):
        """Build a menu of style sets that are available for use in
        this report.  This menu will always have a default style
        available, and will have any other style set name that the
        user has previously created for this report.  This menu is
        created here instead of inline with the rest of the style
        frame, because it must be recreated to reflect any changes
        whenever the user closes the style editor dialog."""
        
        if default is None:
            default = self.style_name
            
        style_sheet_map = self.style_sheet_list.get_style_sheet_map()
        self.style_menu.set(style_sheet_map, default)
        
    #------------------------------------------------------------------------
    #
    # Functions related to setting up the dialog window.
    #
    #------------------------------------------------------------------------
    def setup_title(self):
        """Set up the title bar of the dialog.  This function relies
        on the get_title() customization function for what the title
        should be."""
        self.window.set_title(self.get_title())

    def setup_header(self):
        """Set up the header line bar of the dialog."""
        label = gtk.Label('<span size="larger" weight="bold">%s</span>' % 
                          self.report_name)
        label.set_use_markup(True)
        self.window.vbox.pack_start(label, True, True, self.border_pad)
        
    def setup_style_frame(self):
        """Set up the style frame of the dialog.  This function relies
        on other routines create the default style for this report,
        and to read in any user defined styles for this report.  It
        the builds a menu of all the available styles for the user to
        choose from."""
        # Build the default style set for this report.
        self.default_style = StyleSheet()
        self.options.make_default_style(self.default_style)

        if self.default_style.is_empty():
            # Don't display the option of no styles are used
            return

        # Styles Frame
        label = gtk.Label("%s:" % _("Style"))
        label.set_alignment(0.0, 0.5)

        self.style_menu = StyleComboBox()
        self.style_button = gtk.Button("%s..." % _("Style Editor"))
        self.style_button.connect('clicked', self.on_style_edit_clicked)

        self.tbl.attach(label, 1, 2, self.row, self.row+1, gtk.SHRINK|gtk.FILL)
        self.tbl.attach(self.style_menu, 2, 3, self.row, self.row+1,
                        yoptions=gtk.SHRINK)
        self.tbl.attach(self.style_button, 3, 4, self.row, self.row+1,
                        xoptions=gtk.SHRINK|gtk.FILL, yoptions=gtk.SHRINK)
        self.row += 1
        
        # Build the initial list of available styles sets.  This
        # includes the default style set and any style sets saved from
        # previous invocations of gramps.
        self.style_sheet_list = StyleSheetList(
                            self.options.handler.get_stylesheet_savefile(),
                            self.default_style)

        # Now build the actual menu.
        style = self.options.handler.get_default_stylesheet_name()
        self.build_style_menu(style)

    def setup_report_options_frame(self):
        """Set up the report options frame of the dialog.  This
        function relies on several report_xxx() customization
        functions to determine which of the items should be present in
        this box.  *All* of these items are optional, although the
        generations fields is used in most
        (but not all) dialog boxes."""

        row = 0
        max_rows = len(self.widgets)

        if max_rows == 0:
            return

        table = gtk.Table(3, max_rows+1)
        table.set_col_spacings(12)
        table.set_row_spacings(6)
        
        label = gtk.Label("<b>%s</b>" % _("Report Options"))
        label.set_alignment(0.0, 0.5)
        label.set_use_markup(True)
        
        table.set_border_width(6)
        self.notebook.append_page(table, label)
        row += 1

        # Setup requested widgets
        for (text, widget) in self.widgets:
            if text:
                text_widget = gtk.Label("%s:" % text)
                text_widget.set_alignment(0.0, 0.0)
                table.attach(text_widget, 1, 2, row, row+1,
                             gtk.SHRINK|gtk.FILL, gtk.SHRINK)
                table.attach(widget, 2, 3, row, row+1,
                             yoptions=gtk.SHRINK)
            else:
                table.attach(widget, 2, 3, row, row+1,
                             yoptions=gtk.SHRINK)
            row += 1

    def setup_other_frames(self):
        for key in self.frame_names:
            if key == "":
                continue
            flist = self.frames[key]
            table = gtk.Table(3, len(flist))
            table.set_col_spacings(12)
            table.set_row_spacings(6)
            table.set_border_width(6)
            l = gtk.Label("<b>%s</b>" % _(key))
            l.set_use_markup(True)
            self.notebook.append_page(table, l)

            row = 0
            for (text, widget) in flist:
                if text:
                    text_widget = gtk.Label('%s:' % text)
                    text_widget.set_alignment(0.0, 0.5)
                    table.attach(text_widget, 1, 2, row, row+1,
                                 gtk.SHRINK|gtk.FILL, gtk.SHRINK)
                    table.attach(widget, 2, 3, row, row+1,
                                 yoptions=gtk.SHRINK)
                else:
                    table.attach(widget, 2, 3, row, row+1,
                                 yoptions=gtk.SHRINK)
                row += 1
                
    def setup_main_options(self):
        if "" in self.frames:
            flist = self.frames[""]
            for (text, widget) in flist:
                label = gtk.Label("<b>%s</b>" % text)
                label.set_use_markup(True)
                label.set_alignment(0.0, 0.5)
                
                self.tbl.set_border_width(12)
                self.tbl.attach(label, 0, 4, self.row, self.row+1)
                self.row += 1
    
                self.tbl.attach(widget, 2, 4, self.row, self.row+1)
                self.row += 1

    #------------------------------------------------------------------------
    #
    # Customization hooks for stand-alone reports (subclass ReportDialog)
    #
    #------------------------------------------------------------------------
    def setup_format_frame(self): 
        """Not used in bare report dialogs. Override in the subclass."""
        pass

    #------------------------------------------------------------------------
    #
    # Functions related getting/setting the default directory for a dialog.
    #
    #------------------------------------------------------------------------
    def get_default_directory(self):
        """Get the name of the directory to which the target dialog
        box should default.  This value can be set in the preferences
        panel."""
        return config.get('paths.report-directory')

    def set_default_directory(self, value):
        """Save the name of the current directory, so that any future
        reports will default to the most recently used directory.
        This also changes the directory name that will appear in the
        preferences panel, but does not change the preference in disk.
        This means that the last directory used will only be
        remembered for this session of gramps unless the user saves
        his/her preferences."""
        config.set('paths.report-directory', value)

    #------------------------------------------------------------------------
    #
    # Functions related to setting up the dialog window.
    #
    #------------------------------------------------------------------------
    def setup_init(self):
        # add any elements that we are going to need:
        hid = self.style_name
        if hid[-4:] == ".xml":
            hid = hid[0:-4]
        self.target_fileentry = FileEntry(hid, _("Save As"))
        spath = self.get_default_directory()
        self.target_fileentry.set_filename(spath)
        # need any labels at top:
        label = gtk.Label("<b>%s</b>" % _('Document Options'))
        label.set_use_markup(1)
        label.set_alignment(0.0, 0.5)
        self.tbl.set_border_width(12)
        self.tbl.attach(label, 0, 4, self.row, self.row+1, gtk.FILL)
        self.row += 1
        
    def setup_target_frame(self):
        """Set up the target frame of the dialog.  This function
        relies on several target_xxx() customization functions to
        determine whether the target is a directory or file, what the
        title of any browser window should be, and what default
        directory should be used."""

        # Save Frame
        self.doc_label = gtk.Label("%s:" % _("Filename"))
        self.doc_label.set_alignment(0.0, 0.5)

        self.tbl.attach(self.doc_label, 1, 2, self.row, self.row+1,
                        xoptions=gtk.SHRINK|gtk.FILL,yoptions=gtk.SHRINK)
        self.tbl.attach(self.target_fileentry, 2, 4, self.row, self.row+1,
                        xoptions=gtk.EXPAND|gtk.FILL,yoptions=gtk.SHRINK)
        self.row += 1
        
    #------------------------------------------------------------------------
    #
    # Functions related to retrieving data from the dialog window
    #
    #------------------------------------------------------------------------
    def parse_target_frame(self):
        """Parse the target frame of the dialog.  If the target
        filename is empty this routine returns a special value of None
        to tell the calling routine to give up.  This function also
        saves the current directory so that any future reports will
        default to the most recently used directory."""
        self.target_path = Utils.get_unicode_path_from_file_chooser(self.target_fileentry.get_full_path(0))
        if not self.target_path:
            return None

        # First we check whether the selected path exists
        if os.path.exists(self.target_path):

            # selected path is an existing dir and we need a dir
            if os.path.isdir(self.target_path):

                # check whether the dir has rwx permissions
                if not os.access(self.target_path, os.R_OK|os.W_OK|os.X_OK):
                    ErrorDialog(_('Permission problem'),
                                _("You do not have permission to write "
                                  "under the directory %s\n\n"
                                  "Please select another directory or correct "
                                  "the permissions.") % self.target_path 
                                )
                    return None

            # selected path is an existing file and we need a file
            if os.path.isfile(self.target_path):
                a = OptionDialog(_('File already exists'),
                                 _('You can choose to either overwrite the '
                                   'file, or change the selected filename.'),
                                 _('_Overwrite'), None,
                                 _('_Change filename'), None)
                             
                if a.get_response() == gtk.RESPONSE_YES:
                    return None

        # selected path does not exist yet
        else:
            # we will need to create the file/dir
            # need to make sure we can create in the parent dir
            parent_dir = os.path.dirname(os.path.normpath(self.target_path))
            if os.path.isdir(parent_dir):
                if not os.access(parent_dir, os.W_OK):
                    ErrorDialog(_('Permission problem'),
                                _("You do not have permission to create "
                                  "%s\n\n"
                                  "Please select another path or correct "
                                  "the permissions.") % self.target_path 
                                )
                    return None
            else:
                ErrorDialog(_('No directory'),
                            _('There is no directory %s.\n\n'
                              'Please select another directory '
                              'or create it.') % parent_dir )
                return None
        
        self.set_default_directory(os.path.dirname(self.target_path) + os.sep)
        self.options.handler.output = self.target_path
        return 1
    
    def parse_style_frame(self):
        """Parse the style frame of the dialog.  Save the user
        selected output style for later use.  Note that this routine
        retrieves a value whether or not the menu is displayed on the
        screen.  The subclass will know whether this menu was enabled.
        This is for simplicity of programming."""
        if not self.default_style.is_empty():
            (style_name, self.selected_style) = self.style_menu.get_value()
            self.options.handler.set_default_stylesheet_name(style_name)

    #------------------------------------------------------------------------
    #
    # Callback functions from the dialog
    #
    #------------------------------------------------------------------------
    def on_ok_clicked(self, obj):
        """The user is satisfied with the dialog choices.  Validate
        the output file name before doing anything else.  If there is
        a file name, gather the options and create the report."""

        # Is there a filename?  This should also test file permissions, etc.
        if not self.parse_target_frame():
            self.window.run()

        # Preparation
        self.parse_style_frame()
        self.parse_user_options()
        
        # Save options
        self.options.handler.save_options()
        
    def on_cancel(self, *obj):
        pass

    def on_help_clicked(self, *obj):
        import GrampsDisplay
        GrampsDisplay.help(URL_REPORT_PAGE, self.report_name.replace(" ", "_"))
        
    def on_style_edit_clicked(self, *obj):
        """The user has clicked on the 'Edit Styles' button.  Create a
        style sheet editor object and let them play.  When they are
        done, the previous routine will be called to update the dialog
        menu for selecting a style."""
        StyleListDisplay(self.style_sheet_list, self.build_style_menu,
                         self.window)
示例#8
0
class StyleEditor(object):
    """
    Edits the current style definition. Presents a dialog allowing the values
    of the paragraphs in the style to be altered.
    """
    def __init__(self, name, style, parent):
        """
        Create the StyleEditor.

        name - name of the style that is to be edited
        style - style object that is to be edited
        parent - StyleListDisplay object that called the editor
        """
        self.current_p = None
        self.current_name = None

        self.style = StyleSheet(style)
        self.parent = parent
        self.top = Glade(toplevel='editor')
        self.window = self.top.toplevel

        self.top.connect_signals({
            "on_save_style_clicked": self.on_save_style_clicked,
            "destroy_passed_object": self.__close,
            "on_ok_clicked": dummy_callback,
            "on_add_clicked": dummy_callback,
            "on_delete_clicked": dummy_callback,
            "on_button_press": dummy_callback,
            "on_edit_clicked": dummy_callback,
        })

        self.pname = self.top.get_object('pname')
        self.pdescription = self.top.get_object('pdescription')

        ManagedWindow.set_titles(self.window, self.top.get_object('title'),
                                 _('Style editor'))
        self.top.get_object("label6").set_text(_("point size|pt"))

        titles = [(_('Paragraph'), 0, 130)]
        self.plist = ListModel.ListModel(self.top.get_object("ptree"), titles,
                                         self.change_display)

        self.top.get_object('color').connect('color-set', self.fg_color_set)
        self.top.get_object('bgcolor').connect('color-set', self.bg_color_set)
        self.top.get_object("style_name").set_text(name)

        names = self.style.get_paragraph_style_names()
        names.reverse()
        for p_name in names:
            self.plist.add([p_name], self.style.get_paragraph_style(p_name))
        self.plist.select_row(0)

        if self.parent:
            self.window.set_transient_for(parent.window)
        self.window.run()
        self.window.destroy()

    def __close(self, obj):
        self.window.destroy()

    def draw(self):
        """Updates the display with the selected paragraph."""

        p = self.current_p
        self.pname.set_text('<span size="larger" weight="bold">%s</span>' %
                            self.current_name)
        self.pname.set_use_markup(True)

        descr = p.get_description()
        self.pdescription.set_text(descr or _("No description available"))

        font = p.get_font()
        self.top.get_object("size").set_value(font.get_size())
        if font.get_type_face() == FONT_SERIF:
            self.top.get_object("roman").set_active(1)
        else:
            self.top.get_object("swiss").set_active(1)
        self.top.get_object("bold").set_active(font.get_bold())
        self.top.get_object("italic").set_active(font.get_italic())
        self.top.get_object("underline").set_active(font.get_underline())
        if p.get_alignment() == PARA_ALIGN_LEFT:
            self.top.get_object("lalign").set_active(1)
        elif p.get_alignment() == PARA_ALIGN_RIGHT:
            self.top.get_object("ralign").set_active(1)
        elif p.get_alignment() == PARA_ALIGN_CENTER:
            self.top.get_object("calign").set_active(1)
        else:
            self.top.get_object("jalign").set_active(1)
        self.top.get_object("rmargin").set_value(p.get_right_margin())
        self.top.get_object("lmargin").set_value(p.get_left_margin())
        self.top.get_object("pad").set_value(p.get_padding())
        self.top.get_object("tmargin").set_value(p.get_top_margin())
        self.top.get_object("bmargin").set_value(p.get_bottom_margin())
        self.top.get_object("indent").set_value(p.get_first_indent())
        self.top.get_object("tborder").set_active(p.get_top_border())
        self.top.get_object("lborder").set_active(p.get_left_border())
        self.top.get_object("rborder").set_active(p.get_right_border())
        self.top.get_object("bborder").set_active(p.get_bottom_border())

        self.fg_color = font.get_color()
        c = Color(self.fg_color[0] << 8, self.fg_color[1] << 8,
                  self.fg_color[2] << 8)
        self.top.get_object("color").set_color(c)
        self.top.get_object('color_code').set_text("#%02X%02X%02X" %
                                                   self.fg_color)

        self.bg_color = p.get_background_color()
        c = Color(self.bg_color[0] << 8, self.bg_color[1] << 8,
                  self.bg_color[2] << 8)
        self.top.get_object("bgcolor").set_color(c)
        self.top.get_object('bgcolor_code').set_text("#%02X%02X%02X" %
                                                     self.bg_color)

    def bg_color_set(self, x):
        c = x.get_color()
        self.bg_color = (c.red >> 8, c.green >> 8, c.blue >> 8)
        self.top.get_object('bgcolor_code').set_text("#%02X%02X%02X" %
                                                     self.bg_color)

    def fg_color_set(self, x):
        c = x.get_color()
        self.fg_color = (c.red >> 8, c.green >> 8, c.blue >> 8)
        self.top.get_object('color_code').set_text("#%02X%02X%02X" %
                                                   self.fg_color)

    def save_paragraph(self):
        """Saves the current paragraph displayed on the dialog"""
        p = self.current_p
        font = p.get_font()
        font.set_size(self.top.get_object("size").get_value_as_int())

        if self.top.get_object("roman").get_active():
            font.set_type_face(FONT_SERIF)
        else:
            font.set_type_face(FONT_SANS_SERIF)

        font.set_bold(self.top.get_object("bold").get_active())
        font.set_italic(self.top.get_object("italic").get_active())
        font.set_underline(self.top.get_object("underline").get_active())
        if self.top.get_object("lalign").get_active():
            p.set_alignment(PARA_ALIGN_LEFT)
        elif self.top.get_object("ralign").get_active():
            p.set_alignment(PARA_ALIGN_RIGHT)
        elif self.top.get_object("calign").get_active():
            p.set_alignment(PARA_ALIGN_CENTER)
        else:
            p.set_alignment(PARA_ALIGN_JUSTIFY)

        p.set_right_margin(self.top.get_object("rmargin").get_value())
        p.set_left_margin(self.top.get_object("lmargin").get_value())
        p.set_top_margin(self.top.get_object("tmargin").get_value())
        p.set_bottom_margin(self.top.get_object("bmargin").get_value())
        p.set_padding(self.top.get_object("pad").get_value())
        p.set_first_indent(self.top.get_object("indent").get_value())
        p.set_top_border(self.top.get_object("tborder").get_active())
        p.set_left_border(self.top.get_object("lborder").get_active())
        p.set_right_border(self.top.get_object("rborder").get_active())
        p.set_bottom_border(self.top.get_object("bborder").get_active())

        font.set_color(self.fg_color)
        p.set_background_color(self.bg_color)

        self.style.add_paragraph_style(self.current_name, self.current_p)

    def on_save_style_clicked(self, obj):
        """
        Saves the current style sheet and causes the parent to be updated with
        the changes.
        """
        name = unicode(self.top.get_object("style_name").get_text())

        self.save_paragraph()
        self.style.set_name(name)
        self.parent.sheetlist.set_style_sheet(name, self.style)
        self.parent.redraw()
        self.window.destroy()

    def change_display(self, obj):
        """Called when the paragraph selection has been changed. Saves the
        old paragraph, then draws the newly selected paragraph"""
        # Don't save until current_name is defined
        # If it's defined, save under the current paragraph name
        if self.current_name:
            self.save_paragraph()
        # Then change to new paragraph
        objs = self.plist.get_selected_objects()
        store, node = self.plist.get_selected()
        self.current_name = store.get_value(node, 0)
        self.current_p = objs[0]
        self.draw()
示例#9
0
    def parse_options(self):
        """
        Load the options that the user has entered.
        """
        if not hasattr(self.option_class, "menu"):
            menu = None
        else:
            menu = self.option_class.menu
            menu_opt_names = menu.get_all_option_names()
        for opt in self.options_str_dict:
            if opt in self.options_dict:
                self.options_dict[opt] = \
                    _convert_str_to_match_type(self.options_str_dict[opt],
                                               self.options_dict[opt])

                self.option_class.handler.options_dict[opt] = \
                                                        self.options_dict[opt]

                if menu and opt in menu_opt_names:
                    option = menu.get_option_by_name(opt)
                    option.set_value(self.options_dict[opt])

            else:
                print(_("Ignoring unknown option: %s") % opt).encode(
                    sys.getfilesystemencoding())
                print(
                    _("   Valid options are:") +
                    ", ".join(self.options_dict.keys())).encode(
                        sys.getfilesystemencoding())
                print(
                    _("   Use '%(donottranslate)s' to see description "
                      "and acceptable values") % {
                          'donottranslate': "show=option"
                      }).encode(sys.getfilesystemencoding())

        self.option_class.handler.output = self.options_dict['of']

        self.css_filename = None
        _chosen_format = None
        if self.category == CATEGORY_TEXT:
            for plugin in self.__textdoc_plugins:
                if plugin.get_extension() == self.options_dict['off']:
                    self.format = plugin.get_basedoc()
            self.css_filename = self.options_dict['css']
            if self.format is None:
                # Pick the first one as the default.
                self.format = self.__textdoc_plugins[0].get_basedoc()
                _chosen_format = self.__textdoc_plugins[0].get_extension()
        elif self.category == CATEGORY_DRAW:
            for plugin in self.__drawdoc_plugins:
                if plugin.get_extension() == self.options_dict['off']:
                    self.format = plugin.get_basedoc()
            if self.format is None:
                # Pick the first one as the default.
                self.format = self.__drawdoc_plugins[0].get_basedoc()
                _chosen_format = self.__drawdoc_plugins[0].get_extension()
        elif self.category == CATEGORY_BOOK:
            for plugin in self.__bookdoc_plugins:
                if plugin.get_extension() == self.options_dict['off']:
                    self.format = plugin.get_basedoc()
            if self.format is None:
                # Pick the first one as the default.
                self.format = self.__bookdoc_plugins[0].get_basedoc()
                _chosen_format = self.__bookdoc_plugins[0].get_extension()
        elif self.category == CATEGORY_GRAPHVIZ:
            for graph_format in graphdoc.FORMATS:
                if graph_format['type'] == self.options_dict['off']:
                    if not self.format:  # choose the first one, not the last
                        self.format = graph_format["class"]
            if self.format is None:
                # Pick the first one as the default.
                self.format = graphdoc.FORMATS[0]["class"]
                _chosen_format = graphdoc.FORMATS[0]["type"]
        else:
            self.format = None
        if _chosen_format and self.options_str_dict.has_key('off'):
            print(
                _("Ignoring '%(notranslate1)s=%(notranslate2)s' "
                  "and using '%(notranslate1)s=%(notranslate3)s'.") % {
                      'notranslate1': "off",
                      'notranslate2': self.options_dict['off'],
                      'notranslate3': _chosen_format
                  }).encode(sys.getfilesystemencoding())
            print(
                _("Use '%(notranslate)s' to see valid values.") % {
                    'notranslate': "show=off"
                }).encode(sys.getfilesystemencoding())

        self.paper = paper_sizes[0]  # make sure one exists
        for paper in paper_sizes:
            if paper.get_name() == self.options_dict['papers']:
                self.paper = paper
        self.option_class.handler.set_paper(self.paper)

        self.orien = self.options_dict['papero']

        self.marginl = self.options_dict['paperml']
        self.marginr = self.options_dict['papermr']
        self.margint = self.options_dict['papermt']
        self.marginb = self.options_dict['papermb']

        if self.category in (CATEGORY_TEXT, CATEGORY_DRAW):
            default_style = StyleSheet()
            self.option_class.make_default_style(default_style)

            # Read all style sheets available for this item
            style_file = self.option_class.handler.get_stylesheet_savefile()
            self.style_list = StyleSheetList(style_file, default_style)

            # Get the selected stylesheet
            style_name = self.option_class.handler.get_default_stylesheet_name(
            )
            self.selected_style = self.style_list.get_style_sheet(style_name)
示例#10
0
    def init_standard_options(self, noopt):
        """
        Initialize the options that are hard-coded into the report system.
        """
        self.options_dict = {
            'of'        : self.option_class.handler.module_name,
            'off'       : self.option_class.handler.get_format_name(),
            'style'     : \
                    self.option_class.handler.get_default_stylesheet_name(),
            'papers'    : self.option_class.handler.get_paper_name(),
            'papero'    : self.option_class.handler.get_orientation(),
            'paperml'   : self.option_class.handler.get_margins()[0],
            'papermr'   : self.option_class.handler.get_margins()[1],
            'papermt'   : self.option_class.handler.get_margins()[2],
            'papermb'   : self.option_class.handler.get_margins()[3],
            'css'       : self.option_class.handler.get_css_filename(),
            }

        self.options_help = {
            'of': ["=filename", "Output file name. MANDATORY", ""],
            'off': ["=format", "Output file format.", []],
            'style': ["=name", "Style name.", ""],
            'papers': ["=name", "Paper size name.", ""],
            'papero': ["=num", "Paper orientation number.", ""],
            'paperml': ["=num", "Left paper margin", "Size in cm"],
            'papermr': ["=num", "Right paper margin", "Size in cm"],
            'papermt': ["=num", "Top paper margin", "Size in cm"],
            'papermb': ["=num", "Bottom paper margin", "Size in cm"],
            'css':
            ["=css filename", "CSS filename to use, html format"
             " only", ""],
        }

        if noopt:
            return

        self.options_help['of'][2] = os.path.join(const.USER_HOME,
                                                  "whatever_name")

        if self.category == CATEGORY_TEXT:
            for plugin in self.__textdoc_plugins:
                self.options_help['off'][2].append(plugin.get_extension() +
                                                   "\t" +
                                                   plugin.get_description())
        elif self.category == CATEGORY_DRAW:
            for plugin in self.__drawdoc_plugins:
                self.options_help['off'][2].append(plugin.get_extension() +
                                                   "\t" +
                                                   plugin.get_description())
        elif self.category == CATEGORY_BOOK:
            for plugin in self.__bookdoc_plugins:
                self.options_help['off'][2].append(plugin.get_extension() +
                                                   "\t" +
                                                   plugin.get_description())
        elif self.category == CATEGORY_GRAPHVIZ:
            for graph_format in graphdoc.FORMATS:
                self.options_help['off'][2].append(graph_format["type"] +
                                                   "\t" +
                                                   graph_format["descr"])
        else:
            self.options_help['off'][2] = "NA"

        self.options_help['papers'][2] = \
            [ paper.get_name() for paper in paper_sizes
                        if paper.get_name() != 'Custom Size' ]

        self.options_help['papero'][2] = [
            "%d\tPortrait" % PAPER_PORTRAIT,
            "%d\tLandscape" % PAPER_LANDSCAPE
        ]

        self.options_help['css'][2] = os.path.join(const.USER_HOME,
                                                   "whatever_name.css")

        if self.category in (CATEGORY_TEXT, CATEGORY_DRAW):
            default_style = StyleSheet()
            self.option_class.make_default_style(default_style)

            # Read all style sheets available for this item
            style_file = self.option_class.handler.get_stylesheet_savefile()
            self.style_list = StyleSheetList(style_file, default_style)

            self.options_help['style'][2] = self.style_list.get_style_names()