Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor script dialog for pregression #13

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

add_definitions(-DEXAMPLES_PATH="${CMAKE_SOURCE_DIR}/examples")
add_definitions(-DTEST_DATA_PATH="${CMAKE_SOURCE_DIR}/test_data")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
Expand Down
33 changes: 21 additions & 12 deletions docs/API/script/scriptdialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ import Script 1.0
|-|-|
|QQmlPropertyMap|**[data](#data)**|
|bool|**[interactive](#interactive)**|
|bool|**[showProgress](#showProgress)**|
|int|**[stepCount](#stepCount)**|

## Methods

| | Name |
|-|-|
||**[firstStep](#firstStep)**(string firstStep)|
||**[nextStep](#nextStep)**(string title)|
||**[runSteps](#runSteps)**(function generator)|
||**[startProgress](#startProgress)**(string firstStep, int numSteps)|
||**[setStepCount](#setStepCount)**(int stepCount)|

## Signals

Expand Down Expand Up @@ -71,13 +72,19 @@ This read-only property contains all properties mapping the widgets.
If set to false, runSteps will not ask for user input, the entire script will be run at once.
This is especially useful for testing.

#### <a name="showProgress"></a>bool **showProgress**
#### <a name="stepCount"></a>int **stepCount**

If set to true, a progress dialog will be shown when the dialog is accepted.
This is useful for long-running scripts.
Number of steps to display in the progress bar.

## Method Documentation

#### <a name="firstStep"></a>**firstStep**(string firstStep)

Starts a progress bar with the given `firstStep` title.

The number of following `nextStep` calls (or yield calls if using runSteps) should be one less than the number of
steps set here.

#### <a name="nextStep"></a>**nextStep**(string title)

Indicate a new progress step.
Expand All @@ -98,15 +105,17 @@ This will behave the same as calling `nextStep`, but pauses the script, until th
script.
You can also mix and match between `yield` and `nextStep` calls.

For the best experience, we recommend to use `startProgress` and `nextStep` to indicate the remaining progress.
For the best experience, we recommend to use `setStepCount`, `firstStep` and `yield` to indicate the remaining
progress.

Example:
```javascript
function *conversionSteps() {
startProgress("Adding member", 2)
setStepCount(2) // <--- Initialize the number of steps
firstStep("Adding member") // <--- Start the first step
document.addMember("test", "int", CppDocument.Public)

yield "Inserting include" // <--- The user can check that the member was inserted correctly
yield "Inserting include" // <--- The user can check that the member was inserted correctly
document.insertInclude("<iostream>")
}

Expand All @@ -115,12 +124,12 @@ function convert() {
}
```

#### <a name="startProgress"></a>**startProgress**(string firstStep, int numSteps)
#### <a name="setStepCount"></a>**setStepCount**(int stepCount)

Start a progress bar with the given `firstStep` title and number of steps.
Sets the number of steps to show in the progress bar.

The number of following `nextStep` calls (or yield calls if using runSteps) should be one less than the number of
steps set here.
By default the value is 0, meaning there are no steps set. This will show an indeterminate progress bar. You can use
the `stepCount` property to set the number of steps too.

## Signal Documentation

Expand Down
50 changes: 50 additions & 0 deletions examples/ex_gui_interactive.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Example of an interactive GUI script

import Script 1.0

ScriptDialog {
id: root

// 1) Create a Javascript generator to run the different steps
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator
function *stepGeneratorFunc() {
// 2) Defines the number of steps in the script
// This can be done using the `stepCount` property of ScriptDialog
// Note: if you don't set the step count, a moving scrollbar will be displayed
setStepCount(5);

// 3) Initialize the first step using `firstStep` and run the different commands for this step
firstStep("Step 1...");
Message.log("Step 1 is in progress, cooking something good...");
Utils.sleep(1000);

// 4) Use `yield` to start the following steps
yield "Step 2...";
Message.log("Step 2 is in progress, cooking something good...");
Utils.sleep(1000);

yield "Step 3...";
Message.log("Step 3 is in progress, cooking something good...");
Utils.sleep(1000);

yield "Step 4...";
Message.log("Step 4 is in progress, cooking something good...");
Utils.sleep(1000);

yield "Step 5...";
Message.log("Step 5 is in progress, cooking something good...");
Utils.sleep(1000);
}

// 5) On Ok, start the generator to show the different steps
onAccepted: {
runSteps(stepGeneratorFunc());
}

// This is to test the script automatically, to be used with `knut --test ex-gui-interactive.qml`
// It will run the script automatically without interaction, even if it's the same method as `onAccepted`
function test() {
runSteps(stepGeneratorFunc());
close();
}
}
47 changes: 47 additions & 0 deletions examples/ex_gui_interactive.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>401</width>
<height>220</height>
</rect>
</property>
<property name="windowTitle">
<string>Example of interactive script</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textEdit">
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;meta charset=&quot;utf-8&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: &quot;\2610&quot;; }
li.checked::marker { content: &quot;\2612&quot;; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;This is an &lt;span style=&quot; font-weight:700;&quot;&gt;interactive&lt;/span&gt; script using a JS generator.&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;The script is running different steps, and the user can inspect the result after each steps (the script does nothing, only displaying steps).&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;A progress bar shows the current script progress.&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;It is using a Javascript generator to yield the different steps.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
48 changes: 48 additions & 0 deletions examples/ex_gui_progressbar.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Example of an interactive GUI script

import Script 1.0

ScriptDialog {
id: root

function showProgress() {
// 1) Defines the number of steps in the script
// This can be done using the `stepCount` property of ScriptDialog
// Note: if you don't set the step count, a moving scrollbar will be displayed
setStepCount(5);

// 2) Initialize the first step using `firstStep` and run the different commands for this step
firstStep("Step 1...");
Message.log("Step 1 is in progress, cooking something good...");
Utils.sleep(1000);

// 3) Use `nextStep` for the following steps
nextStep("Step 2...");
Message.log("Step 2 is in progress, cooking something good...");
Utils.sleep(1000);

nextStep("Step 3...");
Message.log("Step 3 is in progress, cooking something good...");
Utils.sleep(1000);

nextStep("Step 4...");
Message.log("Step 4 is in progress, cooking something good...");
Utils.sleep(1000);

nextStep("Step 5...");
Message.log("Step 5 is in progress, cooking something good...");
Utils.sleep(1000);
}

// Function called when the user click on the OK button
onAccepted: {
showProgress();
}

// This is to test the script automatically, to be used with `knut --test ex-gui-interactive.qml`
// It will run the script automatically without interaction
function test() {
showProgress();
close();
}
}
45 changes: 45 additions & 0 deletions examples/ex_gui_progressbar.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>401</width>
<height>220</height>
</rect>
</property>
<property name="windowTitle">
<string>Example of script with progressbar</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textEdit">
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;meta charset=&quot;utf-8&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: &quot;\2610&quot;; }
li.checked::marker { content: &quot;\2612&quot;; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;This is an gui script showing a progressbar for the customer.&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;The script is running different steps, a progress bar shows the current script progress.&lt;/p&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
3 changes: 2 additions & 1 deletion knut.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"span": "cpp",
"vector": "cpp",
"xstring": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"*.moc": "cpp"
}
},
"launch": {
Expand Down
6 changes: 3 additions & 3 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ set(PROJECT_SOURCES
codedocument.cpp
codedocument_p.h
codedocument_p.cpp
conversionprogressdialog.h
conversionprogressdialog.cpp
conversionprogressdialog.ui
cppdocument.h
cppdocument.cpp
cppdocument_p.h
Expand Down Expand Up @@ -79,6 +76,9 @@ set(PROJECT_SOURCES
scriptmanager.cpp
scriptmodel.h
scriptmodel.cpp
scriptprogressdialog.h
scriptprogressdialog.cpp
scriptprogressdialog.ui
scriptrunner.h
scriptrunner.cpp
settings.h
Expand Down
66 changes: 0 additions & 66 deletions src/core/conversionprogressdialog.cpp

This file was deleted.

Loading
Loading