schema_editor/mainwindow.cpp

188 lines
5.4 KiB
C++
Raw Normal View History

2022-03-29 19:31:45 +00:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "application.h"
2022-03-29 19:31:45 +00:00
2022-04-07 22:21:23 +00:00
#include <QFileDialog>
#include <QHBoxLayout>
2022-05-27 06:18:17 +00:00
#include <comdel/parser/parse_context.h>
#include <comdel/parser/parser_util.h>
#include <comdel/domain/schema_creator.h>
2022-04-07 22:21:23 +00:00
#include <iostream>
2022-04-09 19:11:17 +00:00
#include <QPlainTextEdit>
2022-05-27 06:18:17 +00:00
#include <comdel/domain/comdel_validator.h>
2022-04-24 20:21:45 +00:00
#include <fstream>
#include <comdel/domain/comdel_generator.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();
}
void MainWindow::setupUi()
{
2022-04-12 21:37:05 +00:00
setWindowTitle("COMDEL schema editor");
2022-04-07 22:21:23 +00:00
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);
2022-04-24 20:21:45 +00:00
ui->toolBar->addAction("Save schema", this, &MainWindow::onStoreScheme);
ui->toolBar->addAction("Generate system", this, &MainWindow::onGenerateComdel);
2022-04-07 22:21:23 +00:00
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"));
2022-04-12 21:37:05 +00:00
log->setReadOnly(false);
2022-04-09 19:11:17 +00:00
schemaLayout->addWidget(log);
2022-04-07 22:21:23 +00:00
}
2022-04-12 21:37:05 +00:00
void MainWindow::onLoadLibrary() {
auto filename = QFileDialog::getOpenFileName(this,
tr("Open library"), "/home", tr("Comdel library (*.csl)"));
2022-04-07 22:21:23 +00:00
if(!filename.isEmpty()) {
std::ostringstream output;
2022-04-07 22:21:23 +00:00
clear();
auto librarySource = filename.toStdString();
2022-04-07 22:21:23 +00:00
auto instance = Application::instance();
if(!instance->loadLibrary(librarySource, output)) {
log->appendPlainText(QString::fromStdString(output.str()));
2022-04-07 22:21:23 +00:00
}
libraryDisplay->setLibrary(instance->getLibrary());
schemaDisplay->setSchema(instance->getSchema(), instance->getLibrary());
}
2022-04-07 22:21:23 +00:00
}
void MainWindow::onLoadSchema() {
auto filename = QFileDialog::getOpenFileName(this,
tr("Open schema"), "/home", tr("Comdel schema (*.csl)"));
if(!filename.isEmpty()) {
std::ostringstream output;
2022-04-07 22:21:23 +00:00
clear();
auto schemaSource = filename.toStdString();
2022-04-07 22:21:23 +00:00
auto instance = Application::instance();
if(!instance->loadSchema(schemaSource, output)) {
log->appendPlainText(QString::fromStdString(output.str()));
2022-04-07 22:21:23 +00:00
}
2022-04-12 21:37:05 +00:00
libraryDisplay->setLibrary(instance->getLibrary());
schemaDisplay->setSchema(instance->getSchema(), instance->getLibrary());
}
2022-04-10 12:23:18 +00:00
}
2022-04-24 20:21:45 +00:00
void MainWindow::onStoreScheme() {
auto filename = QFileDialog::getSaveFileName(this,
tr("Save schema"), "/home", tr("Comdel schema (*.csl)"));
if(!filename.isEmpty()) {
log->clear();
2022-04-24 20:21:45 +00:00
std::ostringstream output;
2022-04-24 20:21:45 +00:00
if(Application::instance()->generateSchema(output)) {
std::ofstream out(filename.toStdString(), std::ios::out | std::ios::binary);
out<<output.str();
out.close();
log->appendPlainText("Successfully stored schema\n");
} else {
log->appendPlainText("Failed storing schema\n");
}
}
2022-04-24 20:21:45 +00:00
}
2022-04-24 20:21:45 +00:00
void MainWindow::onGenerateComdel() {
auto filename = QFileDialog::getSaveFileName(this,
tr("Save schema"), "/home", tr("Comdel system (*.system)"));
if(!filename.isEmpty()) {
log->clear();
2022-04-24 20:21:45 +00:00
std::ostringstream output;
2022-04-24 20:21:45 +00:00
auto validationErrors = Application::instance()->generateComdel(output);
2022-04-24 20:21:45 +00:00
std::ostringstream buff;
formatErrors(validationErrors, buff);
log->appendPlainText(QString::fromStdString(buff.str()));
2022-04-24 20:21:45 +00:00
if(Application::hasErrors(validationErrors)) {
std::ofstream out(filename.toStdString(), std::ios::out | std::ios::binary);
out<<output.str();
out.close();
log->appendPlainText("Successfully generated comdel\n");
} else {
log->appendPlainText("Failed generating comdel\n");
}
2022-04-12 21:37:05 +00:00
}
}
2022-04-12 21:37:05 +00:00
2022-04-10 12:23:18 +00:00
void MainWindow::onValidateSchema(bool /*toggled*/) {
2022-04-10 12:23:18 +00:00
log->clear();
2022-04-10 12:23:18 +00:00
auto errors = Application::instance()->validateSchema();
2022-05-19 22:44:40 +00:00
std::ostringstream buff;
formatErrors(errors, buff);
2022-05-19 22:44:40 +00:00
log->appendPlainText(QString::fromStdString(buff.str()));
2022-04-10 12:23:18 +00:00
}
2022-04-10 12:23:18 +00:00
void MainWindow::formatErrors(std::vector<domain::ValidationError>& errors, std::ostream& output) {
for(auto& err: errors) {
if(err.instance != nullptr) {
output << err.instance->name;
2022-04-10 12:23:18 +00:00
}
if(err.attribute != nullptr) {
output << "::" << err.attribute->name;
2022-04-10 12:23:18 +00:00
}
if(err.type == domain::Action::ERROR) {
output << " [ERROR] ";
2022-04-10 12:23:18 +00:00
} else {
output << " [WARNING] ";
2022-04-10 12:23:18 +00:00
}
output << err.message << std::endl;
2022-04-10 12:23:18 +00:00
}
2022-04-07 22:21:23 +00:00
}
void MainWindow::clear() {
log->clear();
this->schemaDisplay->setSchema(nullptr, std::nullopt);
this->libraryDisplay->setLibrary(std::nullopt);
2022-03-29 19:31:45 +00:00
}
MainWindow::~MainWindow()
{
delete ui;
2022-04-24 20:21:45 +00:00
}