KDAB/knut

Add a way to initialize script dialog from command line

Closed this issue · 1 comments

Nowadays, we can run or test a script from the command line using –run or –test.

The one missing piece would be to be able to initialize the dialog from the command line, particularly useful for testing.

Script Dialog use widget name to access data from QML, for example a QLineEdit named fileName will be accessed using data.fileName from QML. We could use a similar mechanism to initialize data. I'm thinking of something like that:

knut --test Script.qml --data `{ fileName:"myfilename"; checkBox: true }`

ie passing either a json fileName or a string that can be interpreted as a json file.

That would mean:

  • adding a new data option in KnutCore
  • loading the json file or interpreting the string (using nlohman/json
  • saving that to initialize the script (most likely needs to be saved in ScriptRunner)
  • create method ScriptDialogItem::initialize(json) to initialize the dialog from a json
  • in ScriptRunner::runQml if it's a ScriptDialog, pass the json to it

Finally, once everything is done, add a test:

  • create a small dialog script with some different widgets (lineedit, checkbox, spinbox, combo...) - see #28
  • create a test method that check the data values are not the ones initialized in the designer
  • create a test in tst_knut and pass the data so it works fine

A good test case would be the mfc-migrate-custom-widgets.qml, which has that:

    function test_script() {
        // TODO: remove once https://github.com/KDAB/knut/issues/32 is fixed
        if (!Settings.hasValue("qtui")){
            data.extend = "QSlider"
            data.includeFile = '"mysliderctrl.h"'
        }
        else if (data.memberName === "m_HSliderEcho") {
            data.extend = "QLabel"
            data.includeFile = '"myktext.h"'
        }
        else if (data.memberName === "m_HSliderEcho2") {
            data.extend = "QLabel"
            data.includeFile = '"myktextnew.h"'
        }
        // END TODO
        ...
     }

With --data, you can create a C++ script that is going to call 3 times the script, each time with different data:

knut --script mfc-migrate-custom-widgets --data { extend : QSlider, includeFile :'"mysliderctrl.h" }
knut --script mfc-migrate-custom-widgets --data { extend : QLabel, includeFile :'"myktext.h" }
knut --script mfc-migrate-custom-widgets --data { extend : QLabel, includeFile :'"myktextnew.h" }
...