schema_editor/mainwindow.cpp

187 lines
5.4 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "application.h"
#include <QFileDialog>
#include <QHBoxLayout>
#include <comdel/parser/parse_context.h>
#include <comdel/parser/parser_util.h>
#include <comdel/domain/schema_creator.h>
#include <iostream>
#include <QPlainTextEdit>
#include <comdel/domain/comdel_validator.h>
#include <fstream>
#include <comdel/domain/comdel_generator.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setupUi();
}
void MainWindow::setupUi()
{
setWindowTitle("COMDEL schema editor");
auto layout = new QHBoxLayout();
ui->centralwidget->setLayout(layout);
// setup toolbar
ui->toolBar->addAction("Učitaj bibiloteku", this, &MainWindow::onLoadLibrary);
ui->toolBar->addAction("Učitaj shemu", this, &MainWindow::onLoadSchema);
ui->toolBar->addAction("Spremi shemu", this, &MainWindow::onStoreScheme);
ui->toolBar->addAction("Generiraj comdel", this, &MainWindow::onGenerateComdel);
connect(ui->actionSave_schema, &QAction::triggered, this, &MainWindow::onStoreScheme);
connect(ui->actionExport_schema, &QAction::triggered, this, &MainWindow::onGenerateComdel);
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->setContentsMargins(0, 0, 0, 0);
schemaLayout->addWidget(schemaDisplay, 1);
layout->setContentsMargins(0, 0, 0, 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("Otvori biblioteku"), "", tr("Comdel biblioteka (*.csl)"));
if(!filename.isEmpty()) {
std::ostringstream output;
log->clear();
auto librarySource = filename.toStdString();
auto instance = Application::instance();
if(!instance->loadLibrary(librarySource, output)) {
log->appendPlainText(QString::fromStdString(output.str()));
}
libraryDisplay->refreshContent();
schemaDisplay->refreshContent();
}
}
void MainWindow::onLoadSchema() {
auto filename = QFileDialog::getOpenFileName(this,
tr("Otvori shemu"), "", tr("Comdel shema (*.csl)"));
if(!filename.isEmpty()) {
std::ostringstream output;
log->clear();
auto schemaSource = filename.toStdString();
auto instance = Application::instance();
auto result = instance->loadSchema(schemaSource, output);
if(!result.first){
formatErrors(result.second, output);
log->appendPlainText(QString::fromStdString(output.str()));
}
libraryDisplay->refreshContent();
schemaDisplay->refreshContent();
}
}
void MainWindow::onStoreScheme() {
auto filename = QFileDialog::getSaveFileName(this,
tr("Spremi shemu"), "", tr("Comdel shema (*.csl)"));
if(!filename.isEmpty()) {
log->clear();
std::ostringstream output;
if(Application::instance()->generateSchema(output)) {
std::ofstream out(filename.toStdString(), std::ios::out | std::ios::binary);
out<<output.str();
out.close();
log->appendPlainText("Uspješno spremljena shema\n");
} else {
log->appendPlainText("Greška tijekom spremanja sheme\n");
}
}
}
void MainWindow::onGenerateComdel() {
auto filename = QFileDialog::getSaveFileName(this,
tr("Spremi shemu"), "", tr("Comdel sustav (*.system)"));
if(!filename.isEmpty()) {
log->clear();
std::ostringstream output;
auto validationErrors = Application::instance()->generateComdel(output);
std::ostringstream buff;
formatErrors(validationErrors, buff);
log->appendPlainText(QString::fromStdString(buff.str()));
if(!Application::hasErrors(validationErrors)) {
std::ofstream out(filename.toStdString(), std::ios::out | std::ios::binary);
out<<output.str();
out.close();
log->appendPlainText("Uspješno generiranje comdel modela\n");
} else {
log->appendPlainText("Neuspješno generiranje comdel modela\n");
}
}
}
void MainWindow::onValidateSchema(bool /*toggled*/) {
log->clear();
auto errors = Application::instance()->validateSchema();
std::ostringstream buff;
formatErrors(errors, buff);
log->appendPlainText(QString::fromStdString(buff.str()));
}
void MainWindow::formatErrors(std::vector<domain::ValidationError>& errors, std::ostream& output) {
for(auto& err: errors) {
if(err.instance != nullptr) {
output << err.instance->name;
}
if(err.attribute != nullptr) {
output << "::" << err.attribute->name;
}
if(err.type == domain::Action::ERROR) {
output << " [ERROR] ";
} else {
output << " [WARNING] ";
}
output << err.message << std::endl;
}
}
MainWindow::~MainWindow()
{
delete ui;
}