Koreader: [question]: desktop settings viewer: now I want an editor.

Created on 11 Feb 2020  路  5Comments  路  Source: koreader/koreader

Yesterday I wrote a little tool to view KOReader settings from the computer.

Basically is a window with some tabs that are populated from reading koreader.settings.lua. It looks like:

Sin nombre

As you can see I choosed some nested tables that fit into a single key/value pair of strings to showcase and wrote a simple model to represent them on a table.

The code that populates the table model is:

    void loadStringPairs(lua_State *L, QList<QString> &k, QList<QString> &v, const char *t)
    {
        if (lua::check(L, luaL_dofile(L, "lua/settingsWrapper.lua"))) {
            lua_getglobal(L, t);
            if (lua_istable(L, -1)) {
                lua_pushnil(L);
                while (lua_next(L, -2) != 0) {
                    k.append(lua_tostring(L, -2));
                    v.append(lua_tostring(L, -1));
                    lua_pop(L, 1);
                }
            }
            lua_pop(L, 1);
            lua::printStack(L);
        }
    }

So I can repurpose the same code for different kind of settings, from different tables, with:

    KeyValueForm *gesture_fm = new KeyValueForm(tr("gestures"), tr("actions"), "gesture_fm", this);
    KeyValueForm *gesture_reader = new KeyValueForm(tr("gestures"), tr("actions"), "gesture_reader", this);
    KeyValueForm *footer = new KeyValueForm(tr("key"), tr("value"), "footer", this);

Now I'm wondering if there is a simple way to save edited data back to the lua file. Both C++ and lua options are ok but I don't want to reinvent the wheel here.

Instead of writting each new change just after the edition I was thinking about creating a buffer of changes and increment the counter after each edition. Then a single flush would commit changes, one by one. As models are indexed it is really easy to know which settings was changed on each case (and even to validate the change, which is useful for gestures, I think)

Any ideas?.

question

Most helpful comment

(But don't you already have a GUI for these settings, a black & white app written in Lua with a GUI were you can tweak these settings in various (hidden or not) places ? :)

All 5 comments

Now I'm wondering if there is a simple way to save edited data back to the lua file. Both C++ and lua options are ok but I don't want to reinvent the wheel here.

Use the same Lua C API (Lua_newtable/lua_pushstring/lua_settable) to create a Lua object, then require/use our frontend/dump.lua to serialize it to a string?
(For the C API, some sample usage in cre.cpp, or xtext.cpp where I added some comments, which might help understanding how that Lua stack passing work, which was quite tedious to get into and to get right :)

Also, beware the type of the values (you convert all of them to strings, you might need to save some of them to a number or KOReader may crash if it gets a string where it expects a number).

Now I'm wondering if there is a simple way to save edited data back to the lua file. Both C++ and lua options are ok but I don't want to reinvent the wheel here.

Well, if you have the right types and such perhaps you could just reuse https://github.com/koreader/koreader/blob/24f1f435deb556694846752651c890fcf279d69a/frontend/dump.lua

Thank you!, I will play a bit with dump.lua. I do like the Lua C API. It feels a bit old school and is robust as hell once you wrote the right code :smile:

I don't like to prototype gui stuff (qt, wxwidgets, fltk or whatever) so I guess I'll start playing with a cli tool until I have a serious idea of how the gui should look.

(But don't you already have a GUI for these settings, a black & white app written in Lua with a GUI were you can tweak these settings in various (hidden or not) places ? :)

I played a bit with the dump functions and they work great. I'm going to close this issue but probably I will ask questions here again if I find something problematic.

Thanks again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

WolfBackGR picture WolfBackGR  路  5Comments

Kwuik picture Kwuik  路  4Comments

Monirzadeh picture Monirzadeh  路  4Comments

AlanSP1 picture AlanSP1  路  3Comments

hpdung99 picture hpdung99  路  4Comments