Load default frame parser script from resources

This commit is contained in:
Alex Spataru 2022-06-10 07:04:07 -05:00
parent b44310e049
commit 58d472495a
3 changed files with 32 additions and 21 deletions

View File

@ -179,5 +179,6 @@
<file>window-border/restore.svg</file> <file>window-border/restore.svg</file>
<file>window-border/unmaximize.svg</file> <file>window-border/unmaximize.svg</file>
<file>icons/paste.svg</file> <file>icons/paste.svg</file>
<file>scripts/frame-parser.js</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -0,0 +1,20 @@
/*
* @brief Frame parsing function, you can modify this to suit your needs.
*
* By customizing this code, you can use a single JSON project file to
* process different kinds of frames that are sent by the microcontroller
* or any data source that is connected to Serial Studio. Frame parsing code
* is specific to every JSON project that you create.
*
* @param frame string with the latest received frame.
* @param separator data separator sequence defined by the JSON project.
*
* @note. only data that is *inside* the data delimiters will
* be processed by the frame parser.
*
* @note you can safely declare global variables outside the
* @c parse() function.
*/
function parse(frame, separator) {
return frame.split(separator);
}

View File

@ -28,24 +28,6 @@
#include <Misc/Utilities.h> #include <Misc/Utilities.h>
#include <Misc/ThemeManager.h> #include <Misc/ThemeManager.h>
static const QString DEFAULT_CODE
= "/* \n"
" * Frame parsing function, you can modify this to suit your\n"
" * needs. By customizing this code, you can use a single JSON\n"
" * project file to process different kinds of frames that are\n"
" * sent by the microcontroller or device that is connected to\n"
" * Serial Studio.\n"
" *\n"
" * @note only data that is *inside* the data delimiters will\n"
" * be processed by this function!\n"
" *\n"
" * @param frame string with the latest received frame.\n"
" * @param separator data sepatator sequence set by the JSON project.\n"
" */\n"
"function parse(frame, separator) {\n"
" return frame.split(separator);\n"
"}";
Project::CodeEditor::CodeEditor() Project::CodeEditor::CodeEditor()
{ {
// Setup syntax highlighter // Setup syntax highlighter
@ -94,7 +76,7 @@ Project::CodeEditor::CodeEditor()
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
palette.setColor(QPalette::PlaceholderText, theme->consolePlaceholderText()); palette.setColor(QPalette::PlaceholderText, theme->consolePlaceholderText());
#endif #endif
m_textEdit.setPalette(palette); setPalette(palette);
// Setup layout // Setup layout
auto layout = new QVBoxLayout(this); auto layout = new QVBoxLayout(this);
@ -121,7 +103,15 @@ Project::CodeEditor &Project::CodeEditor::instance()
QString Project::CodeEditor::defaultCode() const QString Project::CodeEditor::defaultCode() const
{ {
return DEFAULT_CODE; QString code;
QFile file(":/scripts/frame-parser.js");
if (file.open(QFile::ReadOnly))
{
code = QString::fromUtf8(file.readAll());
file.close();
}
return code;
} }
void Project::CodeEditor::displayWindow() void Project::CodeEditor::displayWindow()
@ -143,7 +133,7 @@ void Project::CodeEditor::onNewClicked()
} }
// Load default template // Load default template
m_textEdit.setPlainText(DEFAULT_CODE); m_textEdit.setPlainText(defaultCode());
save(true); save(true);
} }