class StringEditorTextArea(Editor): def __init__(self, model=None, value=''): super(StringEditorTextArea, self).__init__(model, value) self._commitOnChange = LiveValue(True) def __getstate__(self): state = super(StringEditorTextArea, self).__getstate__() state['commitOnChange'] = self._commitOnChange.getValue() return state def __setstate__(self, state): super(StringEditorTextArea, self).__setstate__(state) self._commitOnChange = LiveValue(state.get('commitOnChange', True)) def _newModel(self, value): if not isinstance(value, str) and not isinstance(value, unicode): value = '' return Model(value) def _createSettingsPres(self): commitOnChange = Checkbox.checkboxWithLabel('Commit on change', self._commitOnChange) return commitOnChange def buildEditorPres(self, fragment, inheritedState): if self._commitOnChange.getValue(): return TextArea.textAreaCommitOnChange(self._model.liveValue) else: return TextArea(self._model.liveValue)
class Model (object): def __init__(self, value=None): self._live = LiveValue( value ) def __getValue(self): return self._live.getValue() def __setValue(self, value): self._live.setLiteralValue(value) value = property(__getValue, __setValue) @property def liveValue(self): return self._live def __getstate__(self): return { 'value' : self._live.getValue() } def __setstate__(self, state): value = state['value'] self._live = LiveValue( value ) def _addListener(self, listener): self._live.addListener( listener ) def _removeListener(self, listener): self._live.removeListener( listener ) def __present__(self, fragment, inheritedState): return ObjectBox( 'LarchTools.EmbeddedData.Model', self._live )
class StepperBreakpoint(object): def __init__(self): self.__change_history__ = None self._isCurrent = LiveValue(False) def __getstate__(self): return {None: None} def __setstate__(self, state): self.__change_history__ = None self._isCurrent = LiveValue(False) def __get_trackable_contents__(self): return [] def __py_exec__(self, globals, locals, codeGen): co = Coroutine.getCurrent() stepper = _getStepperForCoroutine(co) if stepper is not None: stateView = self._createStateView() sourceStepper = stepper._co.yieldToParent((self, stateView)) def _setAsNotCurrent(self): self._isCurrent.setLiteralValue(False) def _setAsCurrent(self): self._isCurrent.setLiteralValue(True) def _createStateView(self): print 'StepperBreakpoint._createStateView: not implemented' return None def __present__(self, fragment, inheritedState): current = self._isCurrent.getValue() label = _breakPointStyle(Label('BREAKPOINT')) if current: return _breakPointCurrentBorder.surround( Row([_breakPointArrow, Spacer( 3.0, 0.0, ), label]).alignVCentre()) else: return _breakPointBorder.surround(label)
class GUIFlowGrid(GUISequenceComponent): componentName = 'Flow grid' #targetNumColumns = IntEvalField() def __init__(self, targetNumColumns=None): super(GUIFlowGrid, self).__init__() self._targetNumColumns = LiveValue(targetNumColumns) def _presentSequenceContents(self, contents, fragment, inheritedState): return FlowGrid(contents) def __component_py_evalmodel__(self, codeGen): flowGrid = codeGen.embeddedValue(FlowGrid) targetNumColumns = self._targetNumColumns.getValue() args = [] if targetNumColumns is not None: args.append(Py.IntLiteral(value=repr(targetNumColumns))) args.append(self._py_evalmodel_forChildren(codeGen)) return Py.Call(target=flowGrid, args=args)
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))
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))