class ExecutionStyle(object):
    pythonExecution = AttributeNamespace('pythonExecution')

    labelStyle = InheritedAttributeNonNull(
        pythonExecution, 'labelStyle', StyleSheet,
        StyleSheet.style(Primitive.fontSize(10)))

    stdOutStyle = InheritedAttributeNonNull(
        pythonExecution, 'stdOutStyle', StyleSheet,
        StyleSheet.style(
            Primitive.border(
                SolidBorder(1.0, 3.0, 7.0, 7.0, Color(0.5, 1.0, 0.5),
                            Color(0.9, 1.0, 0.9))),
            Primitive.foreground(Color(0.0, 0.5, 0.0))))
    stdErrStyle = InheritedAttributeNonNull(
        pythonExecution, 'stdErrStyle', StyleSheet,
        StyleSheet.style(
            Primitive.border(
                SolidBorder(1.0, 3.0, 7.0, 7.0, Color(1.0, 0.75, 0.5),
                            Color(1.0, 0.95, 0.9))),
            Primitive.foreground(Color(0.75, 0.375, 0.0))))
    exceptionBorderStyle = InheritedAttributeNonNull(
        pythonExecution, 'exceptionBorderStyle', StyleSheet,
        StyleSheet.style(
            Primitive.border(
                SolidBorder(1.0, 3.0, 7.0, 7.0, Color(0.8, 0.0, 0.0),
                            Color(1.0, 0.9, 0.9)))))
    resultBorderStyle = InheritedAttributeNonNull(
        pythonExecution, 'resultBorderStyle', StyleSheet,
        StyleSheet.style(
            Primitive.border(
                SolidBorder(1.0, 3.0, 10.0, 10.0, Color(0.0, 0.0, 0.8),
                            Color.WHITE))))

    resultBoxStyle = InheritedAttributeNonNull(
        pythonExecution, 'resultSpacing', StyleSheet,
        StyleSheet.style(Primitive.columnSpacing(5.0)))

    @PyDerivedValueTable(pythonExecution)
    def _resultBoxStyle(style):
        resultSpacing = style.get(ExecutionStyle.resultSpacing)
        return style.withValues(Primitive.columnSpacing(resultSpacing))
    def PythonCode(self, fragment, inheritedState, node):
        if node.isVisible():
            if node.isCodeVisible():
                codeView = Python2.python2EditorPerspective.applyTo(
                    Pres.coerce(node.getCode()))
                if node.isCodeEditable():
                    codeView = StyleSheet.style(
                        Primitive.editable(True)).applyTo(codeView)
            else:
                codeView = None

            executionResultView = None
            executionResult = node.getResult()
            if executionResult is not None:
                if not node.isResultVisible():
                    executionResult = executionResult.suppressStdOut(
                    ).suppressResult()
                if node.isMinimal():
                    executionResultView = executionResult.minimalView()
                else:
                    executionResultView = executionResult.view()

            if node.isMinimal():
                return executionResultView.alignHExpand(
                ) if executionResultView is not None else Blank()
            else:
                boxContents = []
                if node.isCodeVisible():
                    boxContents.append(
                        _pythonCodeBorderStyle.applyTo(
                            Border(codeView.alignHExpand()).alignHExpand()))
                if node.isResultVisible() and executionResultView is not None:
                    boxContents.append(executionResultView.alignHExpand())
                box = StyleSheet.style(Primitive.columnSpacing(5.0)).applyTo(
                    Column(boxContents))

                return _pythonCodeEditorBorderStyle.applyTo(
                    Border(box.alignHExpand()).alignHExpand())
        else:
            return Blank()
Example #3
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))
Example #4
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))
 def _resultBoxStyle(style):
     resultSpacing = style.get(ExecutionStyle.resultSpacing)
     return style.withValues(Primitive.columnSpacing(resultSpacing))
_executeShortcut = Shortcut( KeyEvent.VK_ENTER, Modifier.CTRL )
_executeNoEvalShortcut = Shortcut( KeyEvent.VK_ENTER, Modifier.CTRL | Modifier.SHIFT )
_historyPreviousShortcut = Shortcut( KeyEvent.VK_UP, Modifier.ALT )
_historyNextShortcut = Shortcut( KeyEvent.VK_DOWN, Modifier.ALT )

_bannerTextStyle = StyleSheet.style( Primitive.fontFace( 'Serif' ), Primitive.fontSmallCaps( True ), Primitive.editable( False ) )
_bannerHelpKeyTextStyle = StyleSheet.style( Primitive.fontFace( 'Serif' ), Primitive.fontSmallCaps( True ), Primitive.fontItalic( True ), Primitive.foreground( Color( 0.25, 0.25, 0.25 ) ) )
_bannerHelpTextStyle = StyleSheet.style( Primitive.fontFace( 'Serif' ), Primitive.fontItalic( True ), Primitive.foreground( Color( 0.25, 0.25, 0.25 ) ) )
_bannerBorder = SolidBorder( 2.0, 5.0, 8.0, 8.0, Color( 0.3, 0.5, 0.3 ), Color( 0.875, 0.9, 0.875 ) )


_labelStyle = StyleSheet.style( Primitive.fontSize( 10 ) )

#_blockStyle = StyleSheet.style( Primitive.columnSpacing( 2.0 ), Primitive.border( SolidBorder( 1.0, 5.0, 15.0, 15.0, Color( 0.25, 0.25, 0.25 ), Color( 0.8, 0.8, 0.8 ) ) ) )
_blockStyle = StyleSheet.style( Primitive.columnSpacing( 3.0 ), Primitive.border( SolidBorder( 1.0, 3.0, 13.0, 13.0, Color( 0.6, 0.6, 0.6 ), Color( 0.9, 0.9, 0.9 ) ) ) )
_blockOutputStyle = StyleSheet.style( Primitive.columnSpacing( 2.0 ) )

_pythonModuleBorderStyle = StyleSheet.style( Primitive.border( SolidBorder( 1.5, 5.0, 10.0, 10.0, Color( 0.65, 0.65, 0.65 ), Color.WHITE ) ) )
_dropPromptStyle = StyleSheet.style( Primitive.border( SolidBorder( 1.0, 3.0, 10.0, 10.0, Color( 0.0, 0.8, 0.0 ), None ) ) )

_varAssignVarNameStyle = StyleSheet.style( Primitive.fontItalic( True ), Primitive.foreground( Color( 0.0, 0.0, 0.5 ) ) )
_varAssignTypeNameStyle = StyleSheet.style( Primitive.foreground( Color( 0.3, 0.0, 0.3 ) ) )
_varAssignJavaKindStyle = StyleSheet.style( Primitive.foreground( Color( 0.0, 0.0, 0.4 ) ), Primitive.fontItalic( True ) )
_varAssignPythonKindStyle = StyleSheet.style( Primitive.foreground( Color( 0.0, 0.4, 0.0 ) ), Primitive.fontItalic( True ) )
_varAssignDocModelKindStyle = StyleSheet.style( Primitive.foreground( Color( 0.4, 0.4, 0.4 ) ), Primitive.fontItalic( True ) )
_varAssignMsgStyle = StyleSheet.style( Primitive.foreground( Color( 0.0, 0.125, 0.0 ) ) )

_consoleBlockListStyle = StyleSheet.style( Primitive.columnSpacing( 5.0 ) )
_consoleStyle = StyleSheet.style( Primitive.columnSpacing( 9.0 ) )