Commits

Anonymous committed 17c7161 Draft

add text list, accordion and scrollview

  • Participants
  • Parent commits 0c85eff

Comments (0)

Files changed (3)

 
 <Editor>:
     content: content
-    canvas:
-        Color:
-            rgb: 1, 1, 1
-        Rectangle:
-            source: 'data/images/background.jpg'
-            size: self.size
 
     BoxLayout:
         orientation: 'vertical'
             spacing: 10
             size_hint: 1, None
             pos_hint: {'top': 1}
-            height: 44
-            Image:
-                size_hint: None, None
-                size: 24, 24
-                source: 'data/logo/kivy-icon-24.png'
-            Label:
-                height: 24
-                text_size: self.width, None
-                color: (1, 1, 1, .8)
-                text: 'Kivy %s - Add content couchdb via couchdbkit' % kivy.__version__
+            height: 34
 
         FloatLayout:
             id: content
 
-[Title@Label]
-    pos_hint: {'center_x': .5, 'y': .3}
-    text: ctx.text
-    font_size: 22
 
-[VSeparator@Widget]:
-	size_hint_x: None
-	width: 10
-	canvas:
-		Color:
-			rgba: .8, .8, .8, .3
-		Rectangle:
-			size: 1, self.height
-			pos: self.center_x, self.y
-
-[HSeparator@Label]:
-	size_hint_y: None
-	height: 45
-	text: ctx.text if 'text' in ctx else ''
-	text_size: self.size
-	valign: 'middle'
-	halign: 'center'
-	canvas.before:
-		Color:
-			rgba: .2, .2, .2, .8
-		Rectangle:
-			size: self.size
-			pos: self.pos
-
-<StandardWidgets>:
+<AddWidgets>:
     text_input_title: text_input_title
     text_input_paragraph: text_input_paragraph
 	BoxLayout:
 		BoxLayout:
 		    orientation: 'vertical'
 
-            HSeparator:
-                text: 'Add content'
-
 		    BoxLayout:
 		        orientation: 'vertical'
 		        padding: 2
 from kivy.properties import NumericProperty
 from kivy.properties import ObjectProperty
 from kivy.app import App
+from kivy.core.window import Window
+from kivy.core.text import LabelBase
+from kivy.uix.accordion import Accordion, AccordionItem
 from kivy.uix.boxlayout import BoxLayout
+from kivy.uix.scrollview import ScrollView
+from kivy.uix.gridlayout import GridLayout
 from kivy.uix.floatlayout import FloatLayout
 from kivy.uix.button import Button
 from kivy.uix.widget import Widget
+from kivy.uix.label import Label
+from kivy.graphics import Color, Rectangle
 from kivy.clock import Clock
+from kivy.lang import Builder
 from couchdbkit import Server
 import random
 import datetime
 import json
 
 
+""" Build list content
+"""
+Builder.load_string('''
+[EntriesItem@BoxLayout]:
+    pos_hint: {'x': 0, 'y': 0}
+    orientation: 'vertical'
+    BoxLayout:
+        orientation: 'vertical'
+        Label:
+            text: str(ctx.title)
+            font_size: 15
+            text_size: (700, None)
+        Label:
+            text: str(ctx.paragraph)
+            font_size: 12
+            text_size: (700, None)
+        Label:
+            text: ctx.published
+            font_size: 10
+            text_size: (700, None)
+''')
+
 class Editor(FloatLayout):
     pass
 
-
-class StandardWidgets(FloatLayout):
-
-    value = NumericProperty(0)
-    text_input_title = ObjectProperty(None)
-    text_input_paragraph = ObjectProperty(None)
-
-    def __init__(self, **kwargs):
-        super(StandardWidgets, self).__init__(**kwargs)
-        Clock.schedule_interval(self.increment_value, 1 / 30.)
-
-    def increment_value(self, dt):
-        self.value += dt
-
+class Connect():
+    """ Simple connect
+    """
     def connect_server(self):
+        """ Connect couchdb database
+        """
         server = Server('http://admin:sekrit@127.0.0.1:5984/')
         try:
             self.db = server.get_or_create_db("greeting-kivy")
 
         return self.db
 
+class ListWidgets(FloatLayout):
+    """ ListWidgets display database documents
+    """
+    value = NumericProperty(0)
+
+    def __init__(self, **kwargs):
+        super(ListWidgets, self).__init__(**kwargs)
+        Clock.schedule_interval(self.increment_value, 1 / 30.)
+        self.wid = ''
+        self.entries = []
+        self.title = []
+        self.paragraph = []
+        self.published = []
+
+    def increment_value(self, dt):
+        self.value += dt
+
+    def all_documents(self):
+        self.db = Connect().connect_server()
+        docs = self.db.all_docs()
+        if not docs:
+            now = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
+            ctx = { 'title': 'Not title', 'paragraph': 'No paragraph', 'published': now }
+            self.wid = Builder.template('EntriesItem', **ctx)
+        else:
+            count = len(docs)
+            for d in docs:
+                entry = self.db.get(d['id'])
+                title = entry['title']
+                paragraph = entry['paragraph']
+                published = entry['published']
+                self.title.append(title)
+                self.paragraph.append(paragraph)
+                self.published.append(published)
+                # Create Layout
+                layout = GridLayout(cols=1, spacing=32, size_hint=(None, None))
+                layout.bind(minimum_height=layout.setter('height'))
+                layout.bind(minimum_width=layout.setter('width'))
+                # Create Label for data
+                for i in range(count):
+                    for t, p, s in zip(self.title, self.paragraph, self.published):
+                        title = Label(text=str(t), size_hint_y=None, size=(34,64), height=40)
+                        paragraph = Label(text=str(p), size_hint_y=None, size=(34,64), height=40)
+                        published = Label(text=s, size_hint_y=None, size=(134,64), height=40)
+                        layout.add_widget(title)
+                        layout.add_widget(paragraph)
+                        layout.add_widget(published)
+                # Create ScrollView
+                scroll_view = ScrollView()
+                scroll_view.center = Window.center
+                scroll_view.add_widget(layout)
+
+        return scroll_view
+
+class AddWidgets(FloatLayout):
+    """ AddWidgets display form for insert document
+    """
+    value = NumericProperty(0)
+    text_input_title = ObjectProperty(None)
+    text_input_paragraph = ObjectProperty(None)
+
+    def __init__(self, **kwargs):
+        super(AddWidgets, self).__init__(**kwargs)
+        Clock.schedule_interval(self.increment_value, 1 / 30.)
+
+    def increment_value(self, dt):
+        self.value += dt
+
     def callback(self):
-        self.db = self.connect_server()
+        """ Call data since the form
+            Use a template kivy for that
+        """
+        self.db = Connect().connect_server()
         published = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
         greeting = {
             "title": self.text_input_title.text,
         print 'Data save'
 
 class EditorApp(App):
+    """ EditorApp build render application
+    """
+    def build(self):
+        root = Accordion(orientation='vertical')
 
-    def build(self):
-        root = BoxLayout(orientation='horizontal', padding=20, spacing=20)
+        # First layout
+        text_home = "Hello world,  I'm a editor example for Kivy.\nI run with couchdbkit on android plateform\n\nEnjoy!"
+        item = AccordionItem(title='Kivy - couchdb editor via couchdbkit')
+        item.add_widget(Label(text=text_home, font_size=22))
+        root.add_widget(item)
+
+        # Second layout
+        item = AccordionItem(title='List content')
+        item.add_widget(self.list_content())
+        root.add_widget(item)
+
+        # Third layout
+        item = AccordionItem(title='Add content')
+        item.add_widget(self.add_content())
+        root.add_widget(item)
+
+        return root
+
+    def list_content(self):
+        """ Display docs into the widgets
+        """
+        list_content = BoxLayout(orientation='horizontal')
 
         self.content = content = BoxLayout()
-        root.add_widget(content)
+        list_content.add_widget(content)
         sc = Editor()
-        sc.content.add_widget(root)
-        self.content.add_widget(StandardWidgets())
+        sc.content.add_widget(list_content)
+        self.content.add_widget(ListWidgets().all_documents())
+        return sc
+
+    def add_content(self):
+        """ Add form into the widgets
+        """
+        add_content = BoxLayout(orientation='horizontal')
+
+        self.content = content = BoxLayout()
+        add_content.add_widget(content)
+        sc = Editor()
+        sc.content.add_widget(add_content)
+        self.content.add_widget(AddWidgets())
         return sc
 
 if __name__ in ('__main__', '__android__'):
+# -*- coding: utf-8 -
+#
+
+version_info = (0, 2, 0)
+__version__ = ".".join(map(str, version_info))