Skip to content

Commit 4c8e820

Browse files
authored
Add custom speed factor parameter to 2d-model (#797)
* Add custom speed factor parameter to 2d-model --speed 5..50 * Fixes after @IKhonakhbeeva
1 parent 7c5c114 commit 4c8e820

File tree

8 files changed

+68
-33
lines changed

8 files changed

+68
-33
lines changed

plugins/robots/checker/twoDModelRunner/main.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,33 @@ const QString description = QObject::tr(
3535
"Example: \n") +
3636
" 2D-model -b --platform minimal --report report.json --trajectory trajectory.fifo example.qrs";
3737

38-
void loadTranslators(const QString &locale)
38+
bool loadTranslators(const QString &locale)
3939
{
4040
QDir translationsDirectory(qReal::PlatformInfo::invariantSettingsPath("pathToTranslations") + "/" + locale);
4141
QDirIterator directories(translationsDirectory, QDirIterator::Subdirectories);
42+
bool hasTranslations = false;
4243
while (directories.hasNext()) {
4344
for (const QFileInfo &translatorFile : QDir(directories.next()).entryInfoList(QDir::Files)) {
4445
QTranslator *translator = new QTranslator(qApp);
4546
translator->load(translatorFile.absoluteFilePath());
4647
QCoreApplication::installTranslator(translator);
48+
hasTranslations = true;
4749
}
4850
}
51+
return hasTranslations;
4952
}
5053

5154
void setDefaultLocale()
5255
{
53-
const QString locale = QLocale().name().left(2);
54-
if (!locale.isEmpty()) {
55-
QLocale::setDefault(QLocale(locale));
56-
loadTranslators(locale);
56+
const QString lang = QLocale().name().left(2);
57+
if (lang.isEmpty()) {
58+
return;
59+
}
60+
61+
// Reset to default country for this language
62+
QLocale::setDefault(QLocale(lang));
63+
if (!loadTranslators(lang)) {
64+
QLOG_INFO() << "Missing translations for language" << lang;
5765
}
5866
}
5967

@@ -70,9 +78,15 @@ void initLogging()
7078
int main(int argc, char *argv[])
7179
{
7280
qReal::PlatformInfo::enableHiDPISupport();
81+
qsrand(time(0));
82+
initLogging();
7383
QApplication app(argc, argv);
7484
QCoreApplication::setApplicationName("2D-model");
75-
QCoreApplication::setApplicationVersion("1.0");
85+
QCoreApplication::setApplicationVersion("2020.2");
86+
QLOG_INFO() << "------------------- APPLICATION STARTED --------------------";
87+
QLOG_INFO() << "Running on" << QSysInfo::prettyProductName();
88+
QLOG_INFO() << "Arguments:" << app.arguments();
89+
QLOG_INFO() << "Setting default locale to" << QLocale().name();
7690
setDefaultLocale();
7791

7892
QCommandLineParser parser;
@@ -83,28 +97,27 @@ int main(int argc, char *argv[])
8397
QCommandLineOption backgroundOption({"b", "background"}, QObject::tr("Run emulation in background."));
8498
QCommandLineOption platformOption("platform"
8599
, QObject::tr("Use this option set to \"minimal\" to disable connection to X server"), "minimal");
86-
QCommandLineOption reportOption("report", QObject::tr("A path to file where checker results will be written (JSON)")
87-
, "path-to-report", "report.json");
88-
QCommandLineOption trajectoryOption("trajectory", QObject::tr("A path to file where robot`s trajectory will be"\
100+
QCommandLineOption reportOption({"r","report"}
101+
, QObject::tr("A path to file where checker results will be written (JSON)")
102+
, "path-to-report", "report.json");
103+
QCommandLineOption trajectoryOption({"t", "trajectory"}
104+
, QObject::tr("A path to file where robot`s trajectory will be"\
89105
" written. The writing will not be performed not immediately, each trajectory point will be written"\
90106
" just when obtained by checker, so FIFOs are recommended to be targets for this option.")
91107
, "path-to-trajectory", "trajectory.fifo");
92-
QCommandLineOption inputOption("input", QObject::tr("Inputs for JavaScript solution")// probably others too
108+
QCommandLineOption inputOption({"i", "input"}, QObject::tr("Inputs for JavaScript solution")// probably others too
93109
, "path-to-input", "inputs.txt");
94-
QCommandLineOption modeOption("mode", QObject::tr("Interpret mode"), "mode", "diagram");
110+
QCommandLineOption modeOption({"m", "mode"}, QObject::tr("Interpret mode"), "mode", "diagram");
111+
QCommandLineOption speedOption({"s", "speed"}
112+
, QObject::tr("Speed factor, try from 5 to 20, or even 1000 (at your own risk!)")
113+
, "speed", "0");
95114
parser.addOption(backgroundOption);
96115
parser.addOption(platformOption);
97116
parser.addOption(reportOption);
98117
parser.addOption(trajectoryOption);
99118
parser.addOption(inputOption);
100119
parser.addOption(modeOption);
101-
102-
qsrand(time(0));
103-
initLogging();
104-
QLOG_INFO() << "------------------- APPLICATION STARTED --------------------";
105-
QLOG_INFO() << "Running on" << QSysInfo::prettyProductName();
106-
QLOG_INFO() << "Arguments:" << app.arguments();
107-
QLOG_INFO() << "Setting default locale to" << QLocale().name();
120+
parser.addOption(speedOption);
108121

109122
parser.process(app);
110123

@@ -120,7 +133,9 @@ int main(int argc, char *argv[])
120133
const QString input = parser.isSet(inputOption) ? parser.value(inputOption) : QString();
121134
const QString mode = parser.isSet(modeOption) ? parser.value(modeOption) : QString("diagram");
122135
twoDModel::Runner runner(report, trajectory, input, mode);
123-
if (!runner.interpret(qrsFile, backgroundMode)) {
136+
137+
auto speedFactor = parser.value(speedOption).toInt();
138+
if (!runner.interpret(qrsFile, backgroundMode, speedFactor)) {
124139
return 2;
125140
}
126141

plugins/robots/checker/twoDModelRunner/runner.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <twoDModel/engine/view/twoDModelWidget.h>
2828
#include <twoDModel/engine/model/model.h>
29+
#include <twoDModel/engine/model/timeline.h>
2930

3031
using namespace twoDModel;
3132

@@ -69,7 +70,7 @@ Runner::~Runner()
6970
mReporter.reportMessages();
7071
}
7172

72-
bool Runner::interpret(const QString &saveFile, bool background)
73+
bool Runner::interpret(const QString &saveFile, bool background, int customSpeedFactor)
7374
{
7475
if (!mProjectManager.open(saveFile)) {
7576
return false;
@@ -94,6 +95,7 @@ bool Runner::interpret(const QString &saveFile, bool background)
9495
for (view::TwoDModelWidget * const twoDModelWindow : twoDModelWindows) {
9596
connect(twoDModelWindow, &view::TwoDModelWidget::widgetClosed, &mMainWindow
9697
, [this]() { this->mMainWindow.emulateClose(); });
98+
9799
auto layout = dynamic_cast<QGridLayout*>(twoDModelWindow->layout());
98100
qReal::ui::ConsoleDock* console = nullptr;
99101
if (layout) {
@@ -102,10 +104,16 @@ bool Runner::interpret(const QString &saveFile, bool background)
102104
// TODO: hack to add console for each widget
103105
layout->addWidget(console, layout->rowCount(), 0, 1, -1);
104106
}
105-
twoDModelWindow->model().timeline().setImmediateMode(background);
107+
106108
for (const model::RobotModel *robotModel : twoDModelWindow->model().robotModels()) {
107109
connectRobotModel(robotModel, console);
108110
}
111+
112+
auto &t = twoDModelWindow->model().timeline();
113+
t.setImmediateMode(background);
114+
if (!background && customSpeedFactor >= model::Timeline::normalSpeedFactor) {
115+
t.setSpeedFactor(customSpeedFactor);
116+
}
109117
}
110118

111119
mReporter.onInterpretationStart();

plugins/robots/checker/twoDModelRunner/runner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class Runner : public QObject
6363
/// @param saveFile QReal save file (qrs) that will be opened and interpreted.
6464
/// @param background If true then the save file will be interpreted in the fastest speed and 2D model window
6565
/// will be closed immediately after the interpretation stopped.
66-
bool interpret(const QString &saveFile, bool background);
66+
/// @param customSpeedFactor can be used when not in background mode to tune interpretation speed
67+
bool interpret(const QString &saveFile, bool background, int customSpeedFactor);
6768

6869
private slots:
6970
void close();

plugins/robots/interpreters/interpreterCore/src/robotsPluginFacade.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ void RobotsPluginFacade::init(const qReal::PluginConfigurator &configurer)
228228
const qReal::Id activeDiagram = mMainWindow->activeDiagram();
229229
// QString() need to use method with binding
230230
// TODO: need refactoring showInTextEditor(?)
231-
mTextManager->showInTextEditor(codePath, QString(), qReal::text::Languages::pickByExtension(codePath.suffix()));
231+
mTextManager->showInTextEditor(codePath, QString()
232+
, qReal::text::Languages::pickByExtension(codePath.suffix()));
232233
if (!activeDiagram.isNull()
233234
&& mRobotModelManager.model().friendlyName().contains("2d", Qt::CaseInsensitive)) {
234235
mMainWindow->activateItemOrDiagram(activeDiagram);

qrtranslations/fr/plugins/robots/interpreterCore_fr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@
309309
<translation type="unfinished"></translation>
310310
</message>
311311
<message>
312-
<location line="+103"/>
312+
<location line="+104"/>
313313
<source>No saved code found in the qrs file</source>
314314
<translation type="unfinished"></translation>
315315
</message>

qrtranslations/fr/plugins/robots/twoDModelRunner_fr.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ In background mode the session will be terminated just after the execution ended
1111
<translation>Émule le comportement du robot sur le modèle 2D TRIK Studio séparément de l&apos;environnement de programmation. Le fichier .qrs sera interprété comme si le bouton &apos;Executer&apos; était appuyé dans TRIK Studio.</translation>
1212
</message>
1313
<message>
14-
<location line="+52"/>
14+
<location line="+66"/>
1515
<source>Save file to be interpreted.</source>
1616
<translation>Le fichier de sauvegarde à interpréter.</translation>
1717
</message>
@@ -26,12 +26,12 @@ In background mode the session will be terminated just after the execution ended
2626
<translation>Mettez &quot;minimal&quot; dans cette option pour désactiver la connexion au serveur X</translation>
2727
</message>
2828
<message>
29-
<location line="+1"/>
29+
<location line="+2"/>
3030
<source>A path to file where checker results will be written (JSON)</source>
3131
<translation>Le chemin au fichier de sauvegarde des resultats (JSON)</translation>
3232
</message>
3333
<message>
34-
<location line="+2"/>
34+
<location line="+3"/>
3535
<source>A path to file where robot`s trajectory will be written. The writing will not be performed not immediately, each trajectory point will be written just when obtained by checker, so FIFOs are recommended to be targets for this option.</source>
3636
<translation>Le chemin au ficher ou la trajectoire du robot sera enregistrée. L&apos;enregistrement ne sera pas fait immediatement, chaque point de trajectoire sera écrit dès lors qu&apos;il est obteneur par le checker, donc FIFO sont récommandés pour cette option.</translation>
3737
</message>
@@ -45,11 +45,16 @@ In background mode the session will be terminated just after the execution ended
4545
<source>Interpret mode</source>
4646
<translation type="unfinished"></translation>
4747
</message>
48+
<message>
49+
<location line="+2"/>
50+
<source>Speed factor, try from 5 to 20, or even 1000 (at your own risk!)</source>
51+
<translation type="unfinished"></translation>
52+
</message>
4853
</context>
4954
<context>
5055
<name>twoDModel::Runner</name>
5156
<message>
52-
<location filename="../../../../plugins/robots/checker/twoDModelRunner/runner.cpp" line="+100"/>
57+
<location filename="../../../../plugins/robots/checker/twoDModelRunner/runner.cpp" line="+102"/>
5358
<source>Robot console</source>
5459
<translation type="unfinished"></translation>
5560
</message>

qrtranslations/ru/plugins/robots/interpreterCore_ru.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
<translation>Экспорт упражнения по заданному пути не удался (попробуйте другой путь)</translation>
224224
</message>
225225
<message>
226-
<location line="+103"/>
226+
<location line="+104"/>
227227
<source>No saved code found in the qrs file</source>
228228
<translation>В qrs не найден сохраннёный код</translation>
229229
</message>

qrtranslations/ru/plugins/robots/twoDModelRunner_ru.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ In background mode the session will be terminated just after the execution ended
1717
</translation>
1818
</message>
1919
<message>
20-
<location line="+52"/>
20+
<location line="+66"/>
2121
<source>Save file to be interpreted.</source>
2222
<translation>Файл сохранения, который будет исполнен.</translation>
2323
</message>
@@ -32,12 +32,12 @@ In background mode the session will be terminated just after the execution ended
3232
<translation>Используйте эту опцию, установленную в &quot;minimal&quot; для того, чтобы запустить программу без X-сервера</translation>
3333
</message>
3434
<message>
35-
<location line="+1"/>
35+
<location line="+2"/>
3636
<source>A path to file where checker results will be written (JSON)</source>
3737
<translation>Путь к файлу, куда будут записаны результаты проверки (JSON)</translation>
3838
</message>
3939
<message>
40-
<location line="+2"/>
40+
<location line="+3"/>
4141
<source>A path to file where robot`s trajectory will be written. The writing will not be performed not immediately, each trajectory point will be written just when obtained by checker, so FIFOs are recommended to be targets for this option.</source>
4242
<translation>Путь к файлу, куда будет выводиться траектори робота. Запись не будет осуществлена единомоментно, каждый узел траектории будет записан по факту его просчета проверяющей системой. Поэтому разумно использования FIFO-файлов в качестве значения этого параметра.</translation>
4343
</message>
@@ -51,6 +51,11 @@ In background mode the session will be terminated just after the execution ended
5151
<source>Interpret mode</source>
5252
<translation type="unfinished"></translation>
5353
</message>
54+
<message>
55+
<location line="+2"/>
56+
<source>Speed factor, try from 5 to 20, or even 1000 (at your own risk!)</source>
57+
<translation>Ускорение, попробуйте от 5 до 20, но можно и 1000 пробовать </translation>
58+
</message>
5459
<message>
5560
<source>A path to file where robot`s trajectory will be written. The writing will not be performed not immediately, each trajectory point will be written just when obtained by checker, engine so FIFOs are recommended to be targets for this option.</source>
5661
<translation type="obsolete">Путь к файлу, куда будует выводиться траектория робота. Записть не будет осуществлена одномоментно, каждый узел траектории будет записан по факту его подсчета проверяющей системой</translation>
@@ -59,7 +64,7 @@ In background mode the session will be terminated just after the execution ended
5964
<context>
6065
<name>twoDModel::Runner</name>
6166
<message>
62-
<location filename="../../../../plugins/robots/checker/twoDModelRunner/runner.cpp" line="+100"/>
67+
<location filename="../../../../plugins/robots/checker/twoDModelRunner/runner.cpp" line="+102"/>
6368
<source>Robot console</source>
6469
<translation>Консоль робота</translation>
6570
</message>

0 commit comments

Comments
 (0)