kivy/kivy — pull request #738 by Titousensei (2012-10-14 06:31:27+00:00)
Title: [Experimental] auto-configuration of kv widgets

This is a new feature for Kivy to automatically save, load and default-configure kv widgets as defined by the application developer, using the Config object. Since this is a completely new feature, I want to explain in details why I implemented it.

My use case is the following. My applications are generally using sliders, checkboxes and what not to change the user's settings under which the app will run. (For example: a timer with different sliders, and a "run" button.) I don't want the user to have to remember and set his/her preferences each time he/she starts the application. I want the user to find the app settings in the same configuration he/she used the last time.

My first implementation was obviously very custom, with a list of widget ids and mapping code to and from the config object. But I didn't want to have to repeat the code to first load the widgets in their last state when starting, then save when exiting, because to do that I had to declare the same widgets in different places in the code.

So I started coding the whole init and save in my app class, using build_config, on_start and on_stop. That was much better, and that's when I started calling app.autoconf(self, ...) in the kv file. But to support all the different classes of widgets, I had to add a number of ugly elif isinstance(class) to set to correct property depending on the class.

So I decided to try and implement it inside Kivy, because I needed a way to call unified "get state" and "set state" method regardless of the widget, where the internal state serialization and deserialization would be hidden from the calling app. Doing that also allowed me to initialize the config loader only when needed, and keep on_start and on_stop for other things.

So here's how it works: in the kv file, you call "autoconf" for the widgets you want to be persistent, with the line "_: app.autoconf(self,

,
, )".

The property "_" is special in that it will be evaluated, but the result will not create an actual property named "_" for the widget.

and
are the section name and option name used to save the config for this widget.

The widget will be initialized with its last recorded state, or with the first time. The config is saved automatically when exiting the app, and loaded when starting the app (but it's also possible to call app.save_autoconf() at any time.)

Example:
```
Slider:
id: s1
range: (0,20)
step: 2
_: app.autoconf(self, "slider_panel", "increment_value", 10)
```

Most widgets support autoconf, including togglebutton, checkbox, accordion (will open the last open accordionitem) and textinput (saves the text). There is a test app in examples/kv/app_autoconf.kv

This is my first time adding a feature to Kivy and I'm not familiar with the internal code, so there might be better ways to implement part of the code. Please feel free to comment on the feature itself or the implementation.

Thanks,
EG