schema_editor/mainwindow.cpp

205 lines
5.4 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QHBoxLayout>
#include <comdel/parser/parsecontext.h>
#include <comdel/parser/parserutil.h>
#include <comdel/domain/schemacreator.h>
#include <iostream>
#include <QPlainTextEdit>
#include <sstream>
#include <comdel/domain/comdelvalidator.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setupUi();
// define allowed methods
signatures = domain::getSupportedFunctions();
}
void MainWindow::setupUi()
{
setWindowTitle("COMDEL schema editor");
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);
connect(ui->actionValidate, &QAction::triggered, this, &MainWindow::onValidateSchema);
// setup central content
libraryDisplay = new display::Library();
auto schemaParent = new QWidget();
auto schemaLayout = new QVBoxLayout();
schemaParent->setLayout(schemaLayout);
schemaDisplay = new display::Schema();
schemaLayout->setMargin(0);
schemaLayout->addWidget(schemaDisplay, 1);
layout->setMargin(0);
layout->addWidget(libraryDisplay);
layout->addWidget(schemaParent, 1);
log = new QPlainTextEdit();
log->setFont(QFont("Courier"));
log->setReadOnly(false);
schemaLayout->addWidget(log);
}
void MainWindow::onLoadLibrary() {
auto filename = QFileDialog::getOpenFileName(this,
tr("Open library"), "/home", tr("Comdel library (*.csl)"));
std::ostringstream buffer;
log->clear();
if(!filename.isEmpty()) {
clear();
ParseContext parseContext;
auto libraryNode = loadLibraryFromFile(&parseContext, filename.toStdString().c_str(), buffer);
if(libraryNode) {
domain::SchemaCreator generator(signatures);
library = generator.loadLibrary(*libraryNode);
for (auto& error : generator.getErrors()) {
parseContext.formatError(error, std::cout, "ERROR: ");
}
if(generator.getErrors().empty()) {
libraryDisplay->setLibrary(library);
// on library load we create a new schema
schema = new domain::Schema();
schemaDisplay->setSchema(schema);
}
} else {
std::cout<<"Bad request"<<std::endl;
}
}
log->appendPlainText(QString::fromStdString(buffer.str()));
}
void MainWindow::onLoadSchema() {
auto filename = QFileDialog::getOpenFileName(this,
tr("Open schema"), "/home", tr("Comdel schema (*.csl)"));
std::ostringstream buffer;
log->clear();
if(!filename.isEmpty()) {
clear();
ParseContext parseContext;
auto schemaNode = loadSchemaFromFile(&parseContext, filename.toStdString().c_str(), buffer);
if(schemaNode) {
domain::SchemaCreator generator(signatures);
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: ");
}
}
if(generator.getErrors().empty()) {
libraryDisplay->setLibrary(library);
schemaDisplay->setSchema(schema);
}
}
}
log->appendPlainText(QString::fromStdString(buffer.str()));
}
void MainWindow::onValidateSchema(bool /*toggled*/) {
if(schema == nullptr) {
return;
}
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;
}
log->appendPlainText(QString::fromStdString(buff.str()));
validationErrors = errors;
}
void MainWindow::clear() {
validationErrors.clear();
schema = nullptr;
library = std::nullopt;
libraryDisplay->setLibrary(library);
schemaDisplay->setSchema(schema);
}
MainWindow::~MainWindow()
{
delete ui;
}