kivyblocks/kivyblocks/boxViewer.py

154 lines
4.1 KiB
Python
Raw Normal View History

2019-12-19 11:13:47 +08:00
"""
BoxViewer options:
{
"dataloader":{
"url",
"method",
"params"
"filter":{
}
}
"boxwidth",
"boxheight",
"viewer"
"toolbar":{
}
}
"""
2020-04-24 18:35:30 +08:00
from traceback import print_exc
from functools import partial
2019-12-19 11:13:47 +08:00
from kivy.app import App
2020-04-24 18:35:30 +08:00
from kivy.factory import Factory
2019-12-19 11:13:47 +08:00
from kivy.utils import platform
from kivy.uix.boxlayout import BoxLayout
from .responsivelayout import VResponsiveLayout
from .toolbar import Toolbar
from .paging import Paging, RelatedLoader
from .utils import CSize
class BoxViewer(BoxLayout):
def __init__(self, **options):
self.toolbar = None
self.parenturl = None
self.dataloader = None
self.initflag = False
BoxLayout.__init__(self, orientation='vertical')
self.selected_data = None
self.options = options
self.box_width = CSize(options['boxwidth'])
self.box_height = CSize(options['boxheight'])
self.viewContainer = VResponsiveLayout(cols=2,box_width=self.box_width)
if options.get('toolbar'):
self.toolbar = Toolbar(options['toolbar'])
lopts = options['dataloader'].copy()
2020-04-24 18:35:30 +08:00
self.dataloader = RelatedLoader(target=self,**lopts['options'])
self.dataloader.bind(on_deletepage=self.deleteWidgets)
self.dataloader.bind(on_pageloaded=self.addPageWidgets)
self.dataloader.bind(on_newbegin=self.deleteAllWidgets)
2020-04-29 10:43:38 +08:00
self.params = self.options['dataloader']['options'].get('params',{})
2020-04-24 18:35:30 +08:00
2019-12-19 11:13:47 +08:00
if self.toolbar:
self.add_widget(self.toolbar)
if self.dataloader.widget:
self.add_widget(self.dataloader.widget)
2020-04-29 10:43:38 +08:00
self.dataloader.bind(on_submit=self.getParams)
2019-12-19 11:13:47 +08:00
self.add_widget(self.viewContainer)
self.register_event_type('on_selected')
self.viewContainer.bind(size=self.resetCols,
pos=self.resetCols)
self.viewContainer.bind(on_scroll_stop=self.on_scroll_stop)
2020-04-29 10:43:38 +08:00
def getParams(self,o,p):
self.params = p
2020-04-24 18:35:30 +08:00
def deleteAllWidgets(self,o):
self.viewContainer.clear_widgets()
def addPageWidgets(self,o,data):
widgets = []
recs = data['data']
dir = data['dir']
idx = 0
if dir == 'up':
recs.reverse()
idx = -1
print('addPageWidgets(),begin')
for r in recs:
self.showObject(widgets, r, index=idx)
print('addPageWidgets(),widgets=',len(widgets))
self.dataloader.bufferObjects(widgets)
x = self.dataloader.getLocater()
self.locater(x)
def deleteWidgets(self,o,data):
for w in data:
self.viewContainer.remove_widget(w)
2019-12-19 11:13:47 +08:00
def on_selected(self, o, v=None):
print('BoxViewer(): on_selected fired....')
def locater(self, pos):
self.viewContainer.scroll_y = pos
def resetCols(self,o=None, v=None):
h = 0
if self.toolbar:
h += self.toolbar.height
if self.dataloader.widget:
h += self.dataloader.widget.height
self.viewContainer.height = self.height - h
self.viewContainer.setCols()
if not self.initflag:
self.dataloader.loadPage(1)
self.initflag = True
2020-04-24 18:35:30 +08:00
def showObject(self, holders, rec,index=0):
def doit(self,holders,idx,o,w):
print('showObject()...doit(),w=',w,o)
w.bind(on_press=self.select_record)
self.viewContainer.add_widget(w,index=idx)
holders.append(w)
def doerr(o,e):
print_exc()
print('showObject(),error=',e)
raise e
2019-12-19 11:13:47 +08:00
options = self.options['viewer'].copy()
options['options']['record'] = rec
options['options']['ancestor'] = self
options['options']['size_hint'] = None,None
options['options']['width'] = self.box_width
options['options']['height'] = self.box_height
2020-04-24 18:35:30 +08:00
blocks = Factory.Blocks()
blocks.bind(on_built=partial(doit,self,holders,index))
blocks.bind(on_failed=doerr)
blocks.widgetBuild(options, ancestor=self)
2019-12-19 11:13:47 +08:00
def on_scroll_stop(self,o,v=None):
if o.scroll_y <= 0.001:
self.dataloader.loadNextPage()
if o.scroll_y >= 0.999:
self.dataloader.loadPreviousPage()
def select_record(self,o,v=None):
self.selected_data = o.getRecord()
2020-04-29 10:43:38 +08:00
d = {
"page_rows":1,
"page":self.selected_data['__posInSet__'],
"dataurl":self.options['dataloader']['options']['dataurl'],
"params":self.params
}
self.dispatch('on_selected',d)
2019-12-19 11:13:47 +08:00
def getData(self):
2020-04-29 10:43:38 +08:00
d = {
"caller":self,
"page_rows":1,
"page":self.selected_data['__posInSet__'],
"dataurl":self.options['dataloader']['options']['dataurl'],
"params":self.params
}
return d