Git commit e3d520a142b5cc004a44850636981627031c6c76 by Benson Muite, on behalf of Malak Elsayed. Committed on 25/09/2026 at 17:23. Pushed by kbkmde into branch 'master'.
Add a speed slider control to run the program instead of discrete speed actions M +1 -1 doc/using-kturtle.docbook M +0 -8 src/kturtleui.rc M +36 -82 src/mainwindow.cpp M +12 -28 src/mainwindow.h https://invent.kde.org/education/kturtle/-/commit/e3d520a142b5cc004a44850636981627031c6c76 diff --git a/doc/using-kturtle.docbook b/doc/using-kturtle.docbook index ebce1c0..7b97d33 100644 --- a/doc/using-kturtle.docbook +++ b/doc/using-kturtle.docbook @@ -404,7 +404,7 @@ <guimenu>Run</guimenu> <guisubmenu>Run Speed</guisubmenu> </menuchoice></term> [suppressed due to size limit] + <listitem><para>Presents two execution modes: First, a <guilabel>Speed control slider</guilabel> to choose from a range of 1%–100% with a label to represent the current percentage. 1% indicates the slowest speed and 100% (default) indicates the fastest speed. Highlighting and inspector are enabled for any speed. Second Mode: <guimenuitem>Step-by-Step</guimenuitem> will execute one command at a time.</para></listitem> </varlistentry> </variablelist> </sect2> diff --git a/src/kturtleui.rc b/src/kturtleui.rc index 4b64e7a..3e9c787 100644 --- a/src/kturtleui.rc +++ b/src/kturtleui.rc @@ -48,14 +48,6 @@ <Action name="run" /> <Action name="pause" /> <Action name="abort" /> - <Menu name="speed"><text>&Run Speed</text> - <Action name="dedicated_speed" /> - <Action name="full_speed" /> - <Action name="slow_speed" /> - <Action name="slower_speed" /> - <Action name="slowest_speed" /> - <Action name="step_speed" /> - </Menu> </Menu> <Menu name="tools"><text>&Tools</text> <Action name="direction_chooser" /> diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f7e9c04..aa3e25c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -19,10 +19,13 @@ #include <QPrintDialog> #include <QPrinter> #include <QSaveFile> +#include <QSlider> #include <QStackedWidget> #include <QStandardPaths> #include <QStatusBar> #include <QTimer> +#include <QWidgetAction> +#include <cmath> #include "interpreter/errormsg.h" #include "interpreter/translator.h" @@ -59,7 +62,7 @@ MainWindow::MainWindow() statusBar()->showMessage(i18nc("@info:status the application is ready for commands", "Ready")); updateContentName(); // also sets the window caption to 'untitled' - setRunSpeed(1); // full speed with highlighting + setRunSpeed(100); abort(); // sets the run-states for the actions right setupGUI(); @@ -147,25 +150,14 @@ void MainWindow::setRunSpeed(int speed) { switch (speed) { case 0: - dedicatedSpeedAct->setChecked(true); - break; - case 1: - fullSpeedAct->setChecked(true); - break; - case 2: - slowSpeedAct->setChecked(true); - break; - case 3: - slowerSpeedAct->setChecked(true); - break; - case 4: - slowestSpeedAct->setChecked(true); - break; - case 5: stepSpeedAct->setChecked(true); break; + default: + speedSlider->setEnabled(true); + stepSpeedAct->setChecked(false); + speedSliderLabel->setText(QStringLiteral("%1%").arg(speed)); + break; } - // TODO runOptionBox->setCurrentIndex(speed); runSpeed = speed; } @@ -421,9 +413,6 @@ void MainWindow::setupActions() connect(executeConsoleAct, &QAction::triggered, console, &Console::executeActionTriggered); executeConsoleAct->setWhatsThis(i18n("Execute: Executes the current line in the console")); - // The run speed action group - QActionGroup *runSpeedGroup = new QActionGroup(this); - // The run action collection, this is used in the toolbar to create a dropdown menu on the run button QAction *runSpeedAction = new QAction(QIcon::fromTheme(QStringLiteral("media-playback-start")), i18n("&Run"), this); connect(runSpeedAction, &QAction::triggered, this, &MainWindow::run); @@ -434,51 +423,27 @@ void MainWindow::setupActions() runSpeedActionMenu->setWhatsThis(i18n("Run: Execute the program, or use the drop down menu to select the run speed")); connect(runSpeedActionMenu, &QMenu::triggered, this, &MainWindow::run); - dedicatedSpeedAct = new QAction(i18nc("@option:radio", "Full Speed (&no highlighting and inspector)"), this); - actionCollection()->addAction(QStringLiteral("dedicated_speed"), dedicatedSpeedAct); - dedicatedSpeedAct->setCheckable(true); - dedicatedSpeedAct->setStatusTip(i18n("Run the program at full speed, with highlighting and inspector disabled")); - dedicatedSpeedAct->setWhatsThis(i18n("Full Speed: Run the program at full speed, with highlighting and inspector disabled")); - connect(dedicatedSpeedAct, &QAction::triggered, this, &MainWindow::setDedicatedSpeed); - runSpeedGroup->addAction(dedicatedSpeedAct); - runSpeedActionMenu->addAction(dedicatedSpeedAct); - - fullSpeedAct = new QAction(i18nc("@option:radio", "&Full Speed"), this); - actionCollection()->addAction(QStringLiteral("full_speed"), fullSpeedAct); - fullSpeedAct->setCheckable(true); - fullSpeedAct->setChecked(true); - fullSpeedAct->setStatusTip(i18n("Run the program at full speed")); - fullSpeedAct->setWhatsThis(i18n("Full Speed: Run the program at full speed")); - connect(fullSpeedAct, &QAction::triggered, this, &MainWindow::setFullSpeed); - runSpeedGroup->addAction(fullSpeedAct); - runSpeedActionMenu->addAction(fullSpeedAct); - - slowSpeedAct = new QAction(i18nc("@option:radio choose the slow speed", "&Slow"), this); - actionCollection()->addAction(QStringLiteral("slow_speed"), slowSpeedAct); - slowSpeedAct->setCheckable(true); - slowSpeedAct->setStatusTip(i18n("Run the program at a slow speed")); - slowSpeedAct->setWhatsThis(i18n("Slow Speed: Run the program at a slow speed")); - connect(slowSpeedAct, &QAction::triggered, this, &MainWindow::setSlowSpeed); - runSpeedGroup->addAction(slowSpeedAct); - runSpeedActionMenu->addAction(slowSpeedAct); - - slowerSpeedAct = new QAction(i18nc("@option:radio", "S&lower"), this); - actionCollection()->addAction(QStringLiteral("slower_speed"), slowerSpeedAct); - slowerSpeedAct->setCheckable(true); - slowerSpeedAct->setStatusTip(i18n("Run the program at a slower speed")); - slowerSpeedAct->setWhatsThis(i18n("Slower Speed: Run the program at a slower speed")); - connect(slowerSpeedAct, &QAction::triggered, this, &MainWindow::setSlowerSpeed); - runSpeedGroup->addAction(slowerSpeedAct); - runSpeedActionMenu->addAction(slowerSpeedAct); - - slowestSpeedAct = new QAction(i18nc("@option:radio", "Sl&owest"), this); - actionCollection()->addAction(QStringLiteral("slowest_speed"), slowestSpeedAct); - slowestSpeedAct->setCheckable(true); - slowestSpeedAct->setStatusTip(i18n("Run the program at the slowest speed")); - slowestSpeedAct->setWhatsThis(i18n("Slowest Speed: Run the program at the slowest speed")); - connect(slowestSpeedAct, &QAction::triggered, this, &MainWindow::setSlowestSpeed); - runSpeedGroup->addAction(slowestSpeedAct); - runSpeedActionMenu->addAction(slowestSpeedAct); + QWidget *speedWidget = new QWidget(); + + speedSlider = new QSlider(speedWidget); + speedSlider->setRange(1, 100); + speedSlider->setValue(100); + speedSlider->setOrientation(Qt::Horizontal); + + speedSliderLabel = new QLabel(speedWidget); + speedSliderLabel->setText(QStringLiteral("100%")); + speedSliderLabel->setAlignment(Qt::AlignCenter); + speedSliderLabel->setMinimumWidth(30); + + QHBoxLayout *speedLayout = new QHBoxLayout(speedWidget); + speedLayout->addWidget(speedSlider); + speedLayout->addWidget(speedSliderLabel); + + speedSliderAct = new QWidgetAction(this); + speedSliderAct->setDefaultWidget(speedWidget); + runSpeedActionMenu->addAction(speedSliderAct); + speedSliderAct->setStatusTip(i18n("Set the execution speed")); + connect(speedSlider, static_cast<void (QSlider::*)(int)>(&QSlider::valueChanged), this, &MainWindow::setSliderSpeed); stepSpeedAct = new QAction(i18nc("@option:radio", "S&tep-by-Step"), this); actionCollection()->addAction(QStringLiteral("step_speed"), stepSpeedAct); @@ -486,7 +451,6 @@ void MainWindow::setupActions() stepSpeedAct->setStatusTip(i18n("Run the program one step at a time")); stepSpeedAct->setWhatsThis(i18n("Step Speed: Run the program one step at a time")); connect(stepSpeedAct, &QAction::triggered, this, &MainWindow::setStepSpeed); - runSpeedGroup->addAction(stepSpeedAct); runSpeedActionMenu->addAction(stepSpeedAct); } @@ -860,7 +824,7 @@ void MainWindow::run() editor->disable(); console->disable(); executeConsoleAct->setEnabled(false); - toggleGuiFeedback(runSpeed != 0); + toggleGuiFeedback(true); // start parsing (always in full speed) iterationTimer->setSingleShot(false); @@ -932,26 +896,16 @@ void MainWindow::iterate() iterationTimer->setSingleShot(true); switch (runSpeed) { case 0: - iterationTimer->start(0); - break; - case 1: - iterationTimer->start(0); - break; - case 2: - iterationTimer->start(500); - break; - case 3: - iterationTimer->start(1000); - break; - case 4: - iterationTimer->start(3000); - break; - case 5: iterationTimer->stop(); interpreter->interpret(); pauseAct->setChecked(true); pause(); return; + default: + ratio = std::pow((100.f - runSpeed) / 100.f, 2); + delayTime = std::round(500.f * ratio); + iterationTimer->start(delayTime); + break; } } interpreter->interpret(); diff --git a/src/mainwindow.h b/src/mainwindow.h index 33ca13d..47d704c 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -21,8 +21,9 @@ #include "interpreter/interpreter.h" class QStackedWidget; - class KRecentFilesAction; +class QWidgetAction; +class QSlider; // extends the QDockWidget with an extra signal class LocalDockWidget : public QDockWidget @@ -91,29 +92,14 @@ private Q_SLOTS: void abort(); void iterate(); QString execute(const QString &); // for single command execution as by the console - void setDedicatedSpeed() - { - setRunSpeed(0); - } - void setFullSpeed() - { - setRunSpeed(1); - } - void setSlowSpeed() - { - setRunSpeed(2); - } - void setSlowerSpeed() - { - setRunSpeed(3); - } - void setSlowestSpeed() + + void setSliderSpeed(int value) { - setRunSpeed(4); + setRunSpeed(value); } void setStepSpeed() { - setRunSpeed(5); + setRunSpeed(0); } void slotInputDialog(QString &value); @@ -169,6 +155,8 @@ private: LocalDockWidget *inspectorDock; QTimer *iterationTimer; int runSpeed; + float ratio; + int delayTime; bool currentlyRunningConsole; QString currentLanguageCode; @@ -180,6 +168,9 @@ private: KRecentFilesAction *recentFilesAction; + QSlider *speedSlider; + QWidgetAction *speedSliderAct; + QAction *newAct; QAction *openAct; QAction *saveAct; @@ -194,21 +185,14 @@ private: QAction *executeConsoleAct; QAction *quitAct; QAction *contextHelpAct; - QAction *dedicatedSpeedAct; - QAction *fullSpeedAct; - QAction *slowSpeedAct; - QAction *slowerSpeedAct; - QAction *slowestSpeedAct; QAction *stepSpeedAct; - QActionGroup *runSpeedGroup; - QComboBox *runOptionBox; - QLabel *statusBarMessageLabel; QLabel *statusBarLanguageLabel; QLabel *statusBarPositionLabel; QLabel *statusBarOverwriteModeLabel; QLabel *statusBarFileNameLabel; + QLabel *speedSliderLabel; }; #endif // _MAINWINDOW_H_
