schema_editor/mainwindow.cpp

217 lines
6.0 KiB
C++
Raw Normal View History

2022-03-29 19:31:45 +00:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
2022-04-07 22:21:23 +00:00
#include <QFileDialog>
#include <QHBoxLayout>
#include <comdel/parser/parsecontext.h>
#include <comdel/parser/parserutil.h>
2022-04-09 18:03:43 +00:00
#include <comdel/domain/schemacreator.h>
2022-04-07 22:21:23 +00:00
#include <iostream>
2022-04-09 19:11:17 +00:00
#include <QPlainTextEdit>
#include <sstream>
2022-04-10 12:23:18 +00:00
#include <comdel/domain/comdelvalidator.h>
2022-04-07 22:21:23 +00:00
2022-03-29 19:31:45 +00:00
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
2022-04-07 22:21:23 +00:00
setupUi();
// define allowed methods
2022-04-10 12:23:18 +00:00
signatures = domain::getSupportedFunctions();
2022-04-07 22:21:23 +00:00
}
void MainWindow::setupUi()
{
auto layout = new QHBoxLayout();
ui->centralwidget->setLayout(layout);
// setup toolbar
ui->toolBar->addAction("Load library", this, &MainWindow::onLoadLibrary);
ui->toolBar->addAction("Load schema", this, &MainWindow::onLoadSchema);
ui->toolBar->addAction("Load test", this, &MainWindow::onTestModal);
2022-04-10 12:23:18 +00:00
connect(ui->actionValidate, &QAction::triggered, this, &MainWindow::onValidateSchema);
2022-04-07 22:21:23 +00:00
// setup central content
libraryDisplay = new display::Library();
2022-04-09 19:11:17 +00:00
auto schemaParent = new QWidget();
auto schemaLayout = new QVBoxLayout();
schemaParent->setLayout(schemaLayout);
2022-04-07 22:21:23 +00:00
schemaDisplay = new display::Schema();
2022-04-09 19:11:17 +00:00
schemaLayout->setMargin(0);
schemaLayout->addWidget(schemaDisplay, 1);
2022-04-07 22:21:23 +00:00
layout->setMargin(0);
layout->addWidget(libraryDisplay);
2022-04-09 19:11:17 +00:00
layout->addWidget(schemaParent, 1);
2022-04-07 22:21:23 +00:00
2022-04-09 19:11:17 +00:00
log = new QPlainTextEdit();
log->setFont(QFont("Courier"));
schemaLayout->addWidget(log);
2022-04-07 22:21:23 +00:00
}
void MainWindow::onTestModal() {
QString filename = "/home/bbr/Documents/personal/projects/modeler/schema.csl";
2022-04-09 19:11:17 +00:00
std::ostringstream buffer;
2022-04-07 22:21:23 +00:00
if(!filename.isEmpty()) {
clear();
ParseContext parseContext;
2022-04-09 19:11:17 +00:00
auto schemaNode = loadSchemaFromFile(&parseContext, filename.toStdString().c_str(), buffer);
2022-04-07 22:21:23 +00:00
if(schemaNode) {
2022-04-09 18:03:43 +00:00
domain::SchemaCreator generator(signatures);
2022-04-07 22:21:23 +00:00
library = generator.loadLibrary(*schemaNode->library);
if(library) {
schema = generator.loadSchema(*schemaNode, *library);
2022-04-09 19:11:17 +00:00
}
2022-04-07 22:21:23 +00:00
2022-04-09 19:11:17 +00:00
for (auto& error : generator.getErrors()) {
parseContext.formatError(error, buffer, "ERROR: ");
}
if(generator.getErrors().empty()) {
libraryDisplay->setLibrary(library);
schemaDisplay->setSchema(schema);
} else {
libraryDisplay->setLibrary(std::nullopt);
schemaDisplay->setSchema(std::nullopt);
2022-04-07 22:21:23 +00:00
}
}
}
2022-04-09 19:11:17 +00:00
log->appendPlainText(QString::fromStdString(buffer.str()));
2022-04-07 22:21:23 +00:00
}
void MainWindow::onLoadLibrary() {
auto filename = QFileDialog::getOpenFileName(this,
tr("Open library"), "/home", tr("Comdel library (*.csl)"));
if(!filename.isEmpty()) {
clear();
ParseContext parseContext;
auto libraryNode = loadLibraryFromFile(&parseContext, filename.toStdString().c_str(), std::cout);
if(libraryNode) {
2022-04-09 18:03:43 +00:00
domain::SchemaCreator generator(signatures);
2022-04-07 22:21:23 +00:00
library = generator.loadLibrary(*libraryNode);
for (auto& error : generator.getErrors()) {
parseContext.formatError(error, std::cout, "ERROR: ");
}
libraryDisplay->setLibrary(library);
} else {
std::cout<<"Bad request"<<std::endl;
}
}
}
void MainWindow::onLoadSchema() {
auto filename = QFileDialog::getOpenFileName(this,
tr("Open schema"), "/home", tr("Comdel schema (*.csl)"));
if(!filename.isEmpty()) {
clear();
ParseContext parseContext;
auto schemaNode = loadSchemaFromFile(&parseContext, filename.toStdString().c_str(), std::cout);
if(schemaNode) {
2022-04-09 18:03:43 +00:00
domain::SchemaCreator generator(signatures);
2022-04-07 22:21:23 +00:00
library = generator.loadLibrary(*schemaNode->library);
for (auto& error : generator.getErrors()) {
parseContext.formatError(error, std::cout, "ERROR: ");
}
if(library) {
schema = generator.loadSchema(*schemaNode, *library);
for (auto& error : generator.getErrors()) {
parseContext.formatError(error, std::cout, "ERROR: ");
}
}
2022-04-09 19:11:17 +00:00
2022-04-07 22:21:23 +00:00
libraryDisplay->setLibrary(library);
schemaDisplay->setSchema(schema);
}
}
2022-04-10 12:23:18 +00:00
}
void MainWindow::onValidateSchema(bool /*toggled*/) {
this->validationErrors.clear();
std::map<std::string, domain::FunctionCallback> callbacks;
auto functions = domain::getSupportedFunctions();
for(auto &func: functions) {
callbacks[func.name] = func.callback;
}
domain::ComdelValidator validator{callbacks};
domain::ValidationContext context;
context.instance = nullptr;
context.attribute = nullptr;
context.addressSpaces = {};
for(auto &lib: library->getAddressSpaces()) {
context.addressSpaces.insert(std::make_pair(lib.getName(), lib));
}
auto errors = validator.validateSchema(*schema, context);
errors.erase(std::remove_if(
errors.begin(), errors.end(), [](const domain::ValidationError& error) { return error.type == domain::Action::WARNING;}),
errors.end());
std::ostringstream buff;
for(auto err: errors) {
if(err.instance != NULL) {
buff << err.instance->name;
}
if(err.attribute != NULL) {
buff << "::" << err.attribute->name;
}
if(err.type == domain::Action::ERROR) {
buff << " [ERROR] ";
} else {
buff << " [WARNING] ";
}
buff << err.message << std::endl;
}
2022-04-07 22:21:23 +00:00
2022-04-10 12:23:18 +00:00
log->appendPlainText(QString::fromStdString(buff.str()));
validationErrors = errors;
2022-04-07 22:21:23 +00:00
}
2022-04-10 12:23:18 +00:00
2022-04-07 22:21:23 +00:00
void MainWindow::clear() {
2022-04-10 12:23:18 +00:00
validationErrors.clear();
2022-04-07 22:21:23 +00:00
schema = std::nullopt;
library = std::nullopt;
libraryDisplay->setLibrary(library);
schemaDisplay->setSchema(schema);
2022-03-29 19:31:45 +00:00
}
MainWindow::~MainWindow()
{
delete ui;
}