Skip to content

Commit

Permalink
scripting: v0.1
Browse files Browse the repository at this point in the history
Signed-off-by: IonutMuthi <[email protected]>
  • Loading branch information
IonutMuthi committed Dec 10, 2024
1 parent 72ef3a8 commit a2a08fe
Show file tree
Hide file tree
Showing 14 changed files with 757 additions and 2 deletions.
2 changes: 2 additions & 0 deletions js/scopycommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ function disconnect(){
scopy.disconnectDevice()
exit(0)
}


3 changes: 2 additions & 1 deletion pluginbase/include/pluginbase/scopyjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class SCOPY_PLUGINBASE_EXPORT ScopyJS : public QObject
Q_INVOKABLE void exit();
Q_INVOKABLE void sleep(unsigned long s);
Q_INVOKABLE void msleep(unsigned long ms);
Q_INVOKABLE void printToConsole(const QString &text);
// Q_INVOKABLE void printToConsole(const QString &text);
Q_INVOKABLE QString printToConsole(const QString &text);
Q_INVOKABLE QString readFromConsole(const QString &text);
Q_INVOKABLE void returnToApplication();
Q_INVOKABLE void suppressScopyMessages(bool b);
Expand Down
8 changes: 7 additions & 1 deletion pluginbase/src/scopyjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ void ScopyJS::msleep(unsigned long ms)
}
}

void ScopyJS::printToConsole(const QString &text) { cout << text.toStdString() << std::endl; }
// void ScopyJS::printToConsole(const QString &text) { cout << text.toStdString() << std::endl; }

QString ScopyJS::printToConsole(const QString &text)
{
cout << text.toStdString() << std::endl;
return text;
}

QString ScopyJS::readFromConsole(const QString &request)
{
Expand Down
7 changes: 7 additions & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ option(ENABLE_PLUGIN_SWIOT "Enable SWIOT plugin" ON)
option(ENABLE_PLUGIN_PQM "Enable PQM plugin" ON)
option(ENABLE_PLUGIN_DATALOGGER "Enable DATALOGGER plugin" ON)
option(ENABLE_PLUGIN_DAC "Enable DAC plugin" ON)
option(ENABLE_PLUGIN_SCRIPTING "Enable SCRIPTING plugin" ON)

if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${SCOPY_PLUGIN_BUILD_PATH})
Expand Down Expand Up @@ -110,6 +111,12 @@ if(ENABLE_PLUGIN_PQM)
list(APPEND PLUGINS ${PQM_TARGET_NAME})
endif()

if(ENABLE_PLUGIN_SCRIPTING)
add_subdirectory(scripting)
list(APPEND PLUGINS ${PLUGIN_NAME})
endif()


if(ENABLE_PLUGIN_M2K)
if(NOT WITH_PYTHON)
message(STATUS "Python is disabled or not found, M2K plugin disabled")
Expand Down
2 changes: 2 additions & 0 deletions plugins/scripting/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include/scripting/scopy-scripting_export.h
include/scripting/scopy-scripting_config.h
72 changes: 72 additions & 0 deletions plugins/scripting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
cmake_minimum_required(VERSION 3.9)

set(SCOPY_MODULE scripting)

message(STATUS "building plugin: " ${SCOPY_MODULE})

project(scopy-${SCOPY_MODULE} VERSION 0.1 LANGUAGES CXX)

set(PLUGIN_DISPLAY_NAME "Scripting")
set(PLUGIN_DESCRIPTION ""THIS TOOL IS USED FOR RUNNING SCPITS USING API CALLS FROM SCOPY"")

include(GenerateExportHeader)

# TODO: split stylesheet/resources and add here TODO: export header files correctly

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/ui)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)

file(GLOB SRC_LIST src/*.cpp src/*.cc)
file(GLOB HEADER_LIST include/${SCOPY_MODULE}/*.h include/${SCOPY_MODULE}/*.hpp)
file(GLOB UI_LIST ui/*.ui)

set(ENABLE_TESTING ON)
if(ENABLE_TESTING)
add_subdirectory(test)
endif()

set(PROJECT_SOURCES ${SRC_LIST} ${HEADER_LIST} ${UI_LIST})
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS REQUIRED Widgets Core)

qt_add_resources(PROJECT_RESOURCES res/resources.qrc)
add_library(${PROJECT_NAME} SHARED ${PROJECT_SOURCES} ${PROJECT_RESOURCES})

generate_export_header(
${PROJECT_NAME} EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/include/${SCOPY_MODULE}/${PROJECT_NAME}_export.h
)

configure_file(
include/${SCOPY_MODULE}/scopy-${SCOPY_MODULE}_config.h.cmakein
${CMAKE_CURRENT_SOURCE_DIR}/include/${SCOPY_MODULE}/scopy-${SCOPY_MODULE}_config.h @ONLY
)

target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/${SCOPY_MODULE})

target_include_directories(${PROJECT_NAME} PUBLIC scopy-pluginbase scopy-gui)

target_link_libraries(
${PROJECT_NAME}
PUBLIC Qt::Widgets
Qt::Core
scopy-pluginbase
scopy-gui
scopy-iioutil
)


if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
configureinstallersettings(${SCOPY_MODULE} ${PLUGIN_DESCRIPTION} FALSE)
endif()

set(scripting_TARGET_NAME ${PROJECT_NAME} PARENT_SCOPE)
39 changes: 39 additions & 0 deletions plugins/scripting/include/scripting/scripting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef SCRIPTING_H
#define SCRIPTING_H

#define SCOPY_PLUGIN_NAME Scripting

#include "scopy-scripting_export.h"
#include <QObject>
#include <pluginbase/plugin.h>
#include <pluginbase/pluginbase.h>

namespace scopy {
namespace scripting {

class SCOPY_SCRIPTING_EXPORT Scripting : public QObject, public PluginBase
{
Q_OBJECT
SCOPY_PLUGIN;

public:
bool compatible(QString m_param, QString category) override;
bool loadPage() override;
bool loadIcon() override;
void loadToolList() override;
void unload() override;
void initMetadata() override;
QString description() override;

QWidget *getTool();

public Q_SLOTS:
bool onConnect() override;
bool onDisconnect() override;

private:
QWidget *m_scriptingWidget = nullptr;
};
} // namespace scripting
} // namespace scopy
#endif // SCRIPTING_H
100 changes: 100 additions & 0 deletions plugins/scripting/src/scopycodeeditor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "scopycodeeditor.h"

#include <QPainter>
#include <QTextBlock>
#include <style.h>

using namespace scopy;
using namespace scripting;

ScopyCodeEditor::ScopyCodeEditor(QWidget *parent)
: QPlainTextEdit{parent}
{
lineNumberArea = new LineNumberArea(this);

connect(this, &ScopyCodeEditor::blockCountChanged, this, &ScopyCodeEditor::updateLineNumberAreaWidth);
connect(this, &ScopyCodeEditor::updateRequest, this, &ScopyCodeEditor::updateLineNumberArea);
connect(this, &ScopyCodeEditor::cursorPositionChanged, this, &ScopyCodeEditor::highlightCurrentLine);

updateLineNumberAreaWidth(0);
highlightCurrentLine();
}

void ScopyCodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
int bottom = top + qRound(blockBoundingRect(block).height());
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}

block = block.next();
top = bottom;
bottom = top + qRound(blockBoundingRect(block).height());
++blockNumber;
}
}

int ScopyCodeEditor::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}

int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;

return space;
}

void ScopyCodeEditor::resizeEvent(QResizeEvent *event)
{
QPlainTextEdit::resizeEvent(event);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}

void ScopyCodeEditor::updateLineNumberAreaWidth(int newBlockCount)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}

void ScopyCodeEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;

if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;

QColor lineColor = Style::getColor(json::theme::interactive_subtle_hover);

selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}

setExtraSelections(extraSelections);
}

void ScopyCodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());

if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
57 changes: 57 additions & 0 deletions plugins/scripting/src/scopycodeeditor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef SCOPYCODEEDITOR_H
#define SCOPYCODEEDITOR_H

#include <QPlainTextEdit>

namespace scopy {
namespace scripting {

class ScopyCodeEditor : public QPlainTextEdit
{
Q_OBJECT
public:
explicit ScopyCodeEditor(QWidget *parent = nullptr);

void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();

protected:
void resizeEvent(QResizeEvent *event) override;

private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateLineNumberArea(const QRect &rect, int dy);

private:
QWidget *lineNumberArea;
};


class LineNumberArea : public QWidget
{
Q_OBJECT

public:
LineNumberArea(ScopyCodeEditor *editor) : QWidget(editor), codeEditor(editor)
{}

QSize sizeHint() const override
{
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}

protected:
void paintEvent(QPaintEvent *event) override
{
codeEditor->lineNumberAreaPaintEvent(event);
}

private:
ScopyCodeEditor *codeEditor;
};


} // namespace scripting
} // namespace scopy
#endif // SCOPYCODEEDITOR_H
Loading

0 comments on commit a2a08fe

Please sign in to comment.