Exemple #1
0
class FontConfiguration(object):
    def __init__(self,
                 generic=Primitive.genericFontName,
                 normal=Primitive.normalTextFontName,
                 heading=Primitive.headingFontName,
                 title=Primitive.titleFontName,
                 uiHeading=Primitive.lightFontName):
        self._generic = LiveValue(generic)
        self._normal = LiveValue(normal)
        self._heading = LiveValue(heading)
        self._title = LiveValue(title)
        self._uiHeading = LiveValue(uiHeading)

    def __getstate__(self):
        state = {}
        state['generic'] = self._generic.getStaticValue()
        state['normal'] = self._normal.getStaticValue()
        state['heading'] = self._heading.getStaticValue()
        state['title'] = self._title.getStaticValue()
        state['uiHeading'] = self._uiHeading.getStaticValue()
        return state

    def __setstate__(self, state):
        self._generic = LiveValue(state.get('generic', 'SansSerif'))
        self._normal = LiveValue(state.get('normal', 'SansSerif'))
        self._heading = LiveValue(state.get('heading', 'Serif'))
        self._title = LiveValue(state.get('title', 'Serif'))
        self._uiHeading = LiveValue(state.get('uiHeading', 'SansSerif'))

    def copyFrom(self, config):
        self._generic.setLiteralValue(config._generic.getStaticValue())
        self._normal.setLiteralValue(config._normal.getStaticValue())
        self._heading.setLiteralValue(config._heading.getStaticValue())
        self._title.setLiteralValue(config._title.getStaticValue())
        self._uiHeading.setLiteralValue(config._uiHeading.getStaticValue())

    def _createStyleSheet(self):
        def suffix(fontName, defaultFont):
            if defaultFont in [f.strip() for f in fontName.split(';')]:
                return fontName
            elif fontName.strip() == '':
                return defaultFont
            else:
                return fontName + '; ' + defaultFont

        style = StyleSheet.style(
            Primitive.fontFace(suffix(self._generic.getValue(), 'SansSerif')),
            RichText.titleTextAttrs(
                RichText.titleTextAttrs.defaultValue.withValues(
                    Primitive.fontFace(suffix(self._title.getValue(),
                                              'Serif')))),
            RichText.headingTextAttrs(
                RichText.headingTextAttrs.defaultValue.withValues(
                    Primitive.fontFace(
                        suffix(self._heading.getValue(), 'Serif')))),
            RichText.normalTextAttrs(
                RichText.normalTextAttrs.defaultValue.withValues(
                    Primitive.fontFace(
                        suffix(self._normal.getValue(), 'SansSerif')))),
            UI.uiTextAttrs(
                UI.uiTextAttrs.defaultValue.withValues(
                    Primitive.fontFace(
                        suffix(self._uiHeading.getValue(), 'SansSerif')))))
        return style

    def apply(self):
        StyleValues.setRootStyleSheet(self._createStyleSheet())

    def __present__(self, fragment, inheritedState):
        helpText = NormalText(
            'Click to open the font choosers. Click on a font sample to choose it. Ctrl-click to select additional fonts.'
        )

        def titleSample(fontName, text='The quick brown fox'):
            style = StyleSheet.style(
                RichText.titleTextAttrs(
                    RichText.titleTextAttrs.defaultValue.withValues(
                        Primitive.fontFace(fontName))))
            return style(TitleBar(text))

        titleChooser = _collapsibleFontChooser('Titles', self._title,
                                               titleSample)

        def headingSample(fontName,
                          text='The quick brown fox jumps over the lazy dog'):
            style = StyleSheet.style(
                RichText.headingTextAttrs(
                    RichText.headingTextAttrs.defaultValue.withValues(
                        Primitive.fontFace(fontName))))
            return style(Heading1(text))

        headingChooser = _collapsibleFontChooser('Headings', self._heading,
                                                 headingSample)

        def normalSample(fontName,
                         text='The quick brown fox jumps over the lazy dog'):
            style = StyleSheet.style(
                RichText.normalTextAttrs(
                    RichText.normalTextAttrs.defaultValue.withValues(
                        Primitive.fontFace(fontName))))
            return style(NormalText(text))

        normalChooser = _collapsibleFontChooser('Normal text', self._normal,
                                                normalSample)

        def uiHeadingSample(fontName,
                            text='The quick brown fox jumps over the lazy dog'
                            ):
            style = StyleSheet.style(
                UI.uiTextAttrs(
                    UI.uiTextAttrs.defaultValue.withValues(
                        Primitive.fontFace(fontName))))
            return style(SectionHeading1(text))

        uiHeadingChooser = _collapsibleFontChooser('UI Headings',
                                                   self._uiHeading,
                                                   uiHeadingSample)

        def genericSample(fontName,
                          text='The quick brown fox jumps over the lazy dog'):
            style = StyleSheet.style(Primitive.fontFace(fontName))
            return style(Label(text))

        genericChooser = _collapsibleFontChooser('Generic text', self._generic,
                                                 genericSample)

        chooserColumn = self._chooserColumnStyle(
            Column([
                titleChooser, headingChooser, normalChooser, uiHeadingChooser,
                genericChooser
            ]))

        # Sample page
        @LiveFunction
        def samplePage():
            label = Label('Sample page:')

            title = titleSample(self._title.getValue(), 'Example Page Title')
            heading = headingSample(self._heading.getValue(), 'Main heading')
            normal1 = normalSample(self._normal.getValue(),
                                   'Normal text will appear like this.')
            normal2 = normalSample(
                self._normal.getValue(),
                'Paragraphs of normal text are used for standard content.')
            ui1 = uiHeadingSample(self._uiHeading.getValue(), 'UI heading')
            genericLabel = Label(
                'Generic text (within controls, code, etc) will appear like this.'
            )
            buttons = self._buttonRowStyle(
                Row([
                    Button.buttonWithLabel('Button %d' % (i, ), None)
                    for i in xrange(0, 5)
                ]))
            ui = Section(ui1, Column([genericLabel,
                                      Spacer(0.0, 7.0), buttons]))
            page = Page([title, Body([heading, normal1, normal2, ui])])

            return Column([label, Spacer(0.0, 15.0), page])

        return self._mainColumnStyle(
            Column([helpText, chooserColumn, samplePage]))

    _mainColumnStyle = StyleSheet.style(Primitive.columnSpacing(30.0))
    _chooserColumnStyle = StyleSheet.style(Primitive.columnSpacing(5.0))
    _buttonRowStyle = StyleSheet.style(Primitive.rowSpacing(10.0))
Exemple #2
0
class FontConfigurationPage(ConfigurationPage):
    def __init__(self):
        super(FontConfigurationPage, self).__init__()
        self._config = None
        self._userConfig = LiveValue(self._config)

    def __getstate__(self):
        state = super(FontConfigurationPage, self).__getstate__()
        state['config'] = self._config
        return state

    def __setstate__(self, state):
        super(FontConfigurationPage, self).__setstate__(state)
        self._config = state.get('config')
        self._userConfig = LiveValue(self._config)

    def getSubjectTitle(self):
        return '[CFG] Fonts'

    def getTitleText(self):
        return 'Font Configuration'

    def getLinkText(self):
        return 'Fonts'

    def apply(self):
        config = _fontConfigForName(self._config)
        config.apply()

    def __present_contents__(self, fragment, inheritedState):
        def _onApply(button, event):
            self._config = self._userConfig.getStaticValue()
            self.apply()

        def _onRevert(button, event):
            self._userConfig.setLiteralValue(self._config)

        choices = [Label(c[1]) for c in _fontConfigChoices]
        choices.append(Label('Custom'))

        @LiveFunction
        def indexOfChoice():
            config = self._userConfig.getValue()
            if isinstance(config, FontConfiguration):
                return len(choices) - 1
            else:
                try:
                    return [c[0] for c in _fontConfigChoices
                            ].index(self._userConfig.getValue())
                except ValueError:
                    return 0

        @LiveFunction
        def footer():
            config = self._userConfig.getValue()
            if isinstance(config, FontConfiguration):
                return config
            else:
                return Blank()

        def _onChoice(menu, prevChoice, choice):
            if choice == len(choices) - 1:
                # Custom
                if prevChoice != choice:
                    prevConfig = _fontConfigChoices[prevChoice][0]
                    prevConfig = _fontConfigForName(prevConfig)
                    config = FontConfiguration()
                    config.copyFrom(prevConfig)
                    self._userConfig.setLiteralValue(config)
            else:
                self._userConfig.setLiteralValue(_fontConfigChoices[choice][0])

        applyButton = Button.buttonWithLabel('Apply', _onApply)
        revertButton = Button.buttonWithLabel('Revert', _onRevert)
        buttons = Row([applyButton,
                       Spacer(15.0, 0.0), revertButton]).alignHPack()

        configMenu = OptionMenu(choices, indexOfChoice, _onChoice)

        chooserHeader = Row([
            Label('Choose a font configuration:'),
            Spacer(25.0, 0.0), configMenu
        ]).alignHPack()

        return self._pageColumnStyle(Column([buttons, chooserHeader, footer]))

    _pageColumnStyle = StyleSheet.style(Primitive.columnSpacing(25.0))