Switch to a screen manager for better use of screen real estate

This commit is contained in:
Dusty Phillips 2012-10-17 21:48:34 -06:00
parent 4a4adfeaac
commit 4715beff36
3 changed files with 140 additions and 181 deletions

View File

@ -1,4 +1,6 @@
#:kivy 1.4
Label:
text: "Paste your Kivy code in here and render!"
text_size: self.width-60, self.height-60
valign: "middle"
text: "The Kivy Catalog is an interactive showcase of Kivy Widgets defined in the Kivy language. For each widget you see, you can directly edit the .kv language syntax to see what effects your changes have on the widget. Click 'Render' or hit 'Ctrl-S' to view your changes.\n\nThere is also a playground on this tab where you can test your Kivy language code directly. This is beta software. The basics seem to work, but some widgets are missing or don't have the ideal .kv representation. Not all widgets are represented yet. It is trivial to add a new .kv file to the interface.\n\nPull requests are welcome."

View File

@ -1,153 +1,112 @@
#:kivy 1.4
[ContainerToggle@ToggleButton]:
group: "container_toggle"
text: ctx.text
on_press: root.parent.parent.parent.show_kv(*args)
state: ctx.state if hasattr(ctx, "state") else "normal"
<Catalog>:
language_box: language_box
screen_manager: screen_manager
BoxLayout:
spacing: 5
TabbedPanel:
size_hint: .6, 1
tab_pos: "left_top"
default_tab_text: "Introduction"
default_tab_content: introduction_tab
on_default_tab: root.show_kv(*args)
Accordion:
id: introduction_tab
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Playground"
kv_container: playground
PlaygroundContainer:
id: playground
AccordionItem:
title: "Welcome"
Label:
text_size: self.width-60, self.height-60
valign: "middle"
text: "The Kivy Catalog is an interactive showcase of Kivy Widgets defined in the Kivy language. For each widget you see, you can directly edit the .kv language syntax to see what effects your changes have on the widget. Click 'Render' or hit 'Ctrl-S' to view your changes.\n\nThere is also a playground on this tab where you can test your Kivy language code directly. This is beta software. The basics seem to work, but some widgets are missing or don't have the ideal .kv representation. Not all widgets are represented yet. It is trivial to add a new .kv file to the interface.\n\nPull requests are welcome."
TabbedPanelHeader:
text: 'Layouts'
content: layout_tab
on_state: root.show_kv(*args)
TabbedPanelHeader:
text: 'UX Widgets'
content: basic_widgets
on_state: root.show_kv(*args)
TabbedPanelHeader:
text: 'Complex'
content: complex_widgets
on_state: root.show_kv(*args)
Accordion:
id: layout_tab
AccordionItem:
on_collapse: root.show_kv(*args)
title: "FloatLayout"
kv_container: floatlayoutcontainer
FloatLayoutContainer:
id: floatlayoutcontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "BoxLayout"
kv_container: boxlayoutcontainer
BoxLayoutContainer:
id: boxlayoutcontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "AnchorLayout"
kv_container: anchorlayoutcontainer
AnchorLayoutContainer:
id: anchorlayoutcontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "GridLayout"
kv_container: gridlayoutcontainer
GridLayoutContainer:
id: gridlayoutcontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "StackLayout"
kv_container: stacklayoutcontainer
StackLayoutContainer:
id: stacklayoutcontainer
Accordion:
id: basic_widgets
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Buttons"
kv_container: buttoncontainer
ButtonContainer:
id: buttoncontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Labels"
kv_container: labelcontainer
LabelContainer:
id: labelcontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Booleans"
kv_container: checkboxcontainer
CheckBoxContainer:
id: checkboxcontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Progress Bar"
kv_container: progressbarcontainer
ProgressBarContainer:
id: progressbarcontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Media"
kv_container: mediacontainer
MediaContainer:
id: mediacontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Text"
kv_container: textcontainer
TextContainer:
id: textcontainer
Accordion:
id: complex_widgets
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Popups"
kv_container: popupcontainer
PopupContainer:
id: popupcontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Selectors"
kv_container: selectorscontainer
SelectorsContainer:
id: selectorscontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "FileChoosers"
kv_container: filechoosercontainer
FileChooserContainer:
id: filechoosercontainer
AccordionItem:
on_collapse: root.show_kv(*args)
title: "Scatter"
kv_container: scatter
ScatterContainer:
id: scatter
AccordionItem:
on_collapse: root.show_kv(*args)
title: "ReST"
kv_container: rest
RestContainer:
id: rest
BoxLayout:
size_hint: None, 1
orientation: "vertical"
ContainerToggle:
text: "Welcome"
state: "down"
ContainerToggle:
text: "Float Layout"
ContainerToggle:
text: "Box Layout"
ContainerToggle:
text: "Anchor Layout"
ContainerToggle:
text: "Grid Layout"
ContainerToggle:
text: "Stack Layout"
ContainerToggle:
text: "Buttons"
ContainerToggle:
text: "Labels"
ContainerToggle:
text: "Booleans"
ContainerToggle:
text: "Progress Bar"
ContainerToggle:
text: "Media"
ContainerToggle:
text: "Text"
ContainerToggle:
text: "Popups"
ContainerToggle:
text: "Selectors"
ContainerToggle:
text: "File Choosers"
ContainerToggle:
text: "Scatter"
ContainerToggle:
text: "ReST"
ScreenManager:
id: screen_manager
size_hint: 1, 1
Screen:
name: "Welcome"
PlaygroundContainer:
Screen:
name: "Float Layout"
FloatLayoutContainer
Screen:
name: "Box Layout"
BoxLayoutContainer:
Screen:
name: "Anchor Layout"
AnchorLayoutContainer:
Screen:
name: "Grid Layout"
GridLayoutContainer:
Screen:
name: "Stack Layout"
StackLayoutContainer:
Screen:
name: "Buttons"
ButtonContainer:
Screen:
name: "Labels"
LabelContainer:
Screen:
name: "Booleans"
CheckBoxContainer:
Screen:
name: "Progress Bar"
ProgressBarContainer:
Screen:
name: "Media"
MediaContainer:
Screen:
name: "Text"
TextContainer:
Screen:
name: "Popups"
PopupContainer:
Screen:
name: "Selectors"
SelectorsContainer:
Screen:
name: "File Choosers"
FileChooserContainer:
Screen:
name: "Scatter"
ScatterContainer:
Screen:
name: "ReST"
RestContainer:
BoxLayout:
id: bl
orientation: "vertical"
size_hint: .4, 1
size_hint: 1, 1
KivyRenderTextInput:
text_size: self.width-20, self.height-20
font_name: "DroidSansMono.ttf"
@ -158,4 +117,4 @@
size_hint: 1, None
height: 50
text: "Render"
on_press: root.change_kv(*args)
on_press: root.change_kv(*args)

View File

@ -94,58 +94,56 @@ class Catalog(BoxLayout):
You do not need to edit any python code, just .kv language files!
'''
language_box = ObjectProperty()
screen_manager = ObjectProperty()
def __init__(self, **kwargs):
super(Catalog, self).__init__(**kwargs)
self.kv_container = None
from kivy.clock import Clock
Clock.schedule_once(self.show_kv)
self.show_kv(None)
def show_kv(self, object, collapsed=None):
def show_kv(self, object):
'''Called when an accordionitem is collapsed or expanded. If it
was expanded, we need to show the .kv language file associated with
the newly revealed container.'''
if collapsed == "down": # a tabbed panel was clicked, not an accordion
object = object.content.children[0]
collapsed = False
if collapsed is False and hasattr(object, "kv_container"):
with open(object.kv_container.kv_file) as file:
self.language_box.background_color = (1, 1, 1, 1)
self.language_box.readonly = False
self.language_box.text = file.read()
self.kv_container = object.kv_container
else:
self.language_box.background_color = (1, 1, 1, .5)
self.language_box.text = ""
self.language_box.readonly = True
self.kv_container = None
# if object is not passed, it's initialization, we just need to load
# the file
if object:
# one button must always be pressed, even if user presses it again
if object.state == "normal":
object.state = "down"
self.screen_manager.current = object.text
with open(self.screen_manager.current_screen.content.children[
0].kv_file) as file:
self.language_box.text = file.read()
def change_kv(self, button):
'''Called when the update button is clicked. Needs to update the
interface for the currently active kv widget, if there is one based
on the kv file the user entered. If there is an error in their kv
syntax, show a nice popup.'''
if self.kv_container:
try:
parser = Parser(content=self.language_box.text.encode('utf8'))
self.kv_container.clear_widgets()
widget = Factory.get(parser.root.name)()
Builder._apply_rule(widget, parser.root, parser.root)
self.kv_container.add_widget(widget)
except (SyntaxError, ParserException) as e:
content = Label(text=str(e), text_size=(350, None))
popup = Popup(title="Parse Error in Kivy Language Markup",
content=content, text_size=(350, None),
size_hint=(None, None), size=(400, 400))
popup.open()
except:
import traceback
traceback.print_exc()
popup = Popup(title="Boom",
content=Label(text="Something horrible happened while parsing your Kivy Language", text_size=(350, None)),
text_size=(350, None),
size_hint=(None, None), size=(400, 400))
popup.open()
kv_container = self.screen_manager.current_screen.content.children[0]
try:
parser = Parser(content=self.language_box.text.encode('utf8'))
kv_container.clear_widgets()
widget = Factory.get(parser.root.name)()
Builder._apply_rule(widget, parser.root, parser.root)
kv_container.add_widget(widget)
except (SyntaxError, ParserException) as e:
content = Label(text=str(e), text_size=(350, None))
popup = Popup(title="Parse Error in Kivy Language Markup",
content=content, text_size=(350, None),
size_hint=(None, None), size=(400, 400))
popup.open()
except:
import traceback
traceback.print_exc()
popup = Popup(title="Boom",
content=Label(text="Something horrible happened while parsing your Kivy Language", text_size=(350, None)),
text_size=(350, None),
size_hint=(None, None), size=(400, 400))
popup.open()
class KivyCatalogApp(App):