2022-03-29 19:31:45 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
2022-05-30 21:11:06 +00:00
|
|
|
#include "application.h"
|
2022-06-13 22:53:46 +00:00
|
|
|
#include "comdel/display/dialog/error_dialog.h"
|
|
|
|
#include "comdel/display/dialog/success_dialog.h"
|
2022-06-19 18:10:44 +00:00
|
|
|
#include "message_source.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>
|
2022-06-13 22:53:46 +00:00
|
|
|
#include <comdel/domain/comdel_validator.h>
|
2022-04-07 22:21:23 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2022-04-24 20:21:45 +00:00
|
|
|
#include <fstream>
|
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
|
2022-06-19 18:10:44 +00:00
|
|
|
loadLibrary = ui->toolBar->addAction(QMESSAGE("msg_toolbar_load_library"), this, &MainWindow::onLoadLibrary);
|
|
|
|
loadSchema = ui->toolBar->addAction(QMESSAGE("msg_toolbar_load_schema"), this, &MainWindow::onLoadSchema);
|
|
|
|
saveSchema = ui->toolBar->addAction(QMESSAGE("msg_toolbar_save_schema"), this, &MainWindow::onStoreScheme);
|
|
|
|
generateComdel = ui->toolBar->addAction(QMESSAGE("msg_toolbar_generate_comdel"), this, &MainWindow::onGenerateComdel);
|
|
|
|
|
|
|
|
updateTranslations();
|
2022-04-07 22:21:23 +00:00
|
|
|
|
2022-06-09 18:24:27 +00:00
|
|
|
connect(ui->actionSave_schema, &QAction::triggered, this, &MainWindow::onStoreScheme);
|
|
|
|
connect(ui->actionExport_schema, &QAction::triggered, this, &MainWindow::onGenerateComdel);
|
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-06-09 18:24:27 +00:00
|
|
|
schemaLayout->setContentsMargins(0, 0, 0, 0);
|
2022-04-09 19:11:17 +00:00
|
|
|
schemaLayout->addWidget(schemaDisplay, 1);
|
2022-04-07 22:21:23 +00:00
|
|
|
|
2022-06-09 18:24:27 +00:00
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
2022-04-07 22:21:23 +00:00
|
|
|
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-12 21:37:05 +00:00
|
|
|
void MainWindow::onLoadLibrary() {
|
|
|
|
auto filename = QFileDialog::getOpenFileName(this,
|
2022-06-19 18:10:44 +00:00
|
|
|
QMESSAGE("msg_files_load_library"),
|
|
|
|
"",
|
|
|
|
QMESSAGE("msg_files_load_library_format"));
|
2022-04-07 22:21:23 +00:00
|
|
|
if(!filename.isEmpty()) {
|
2022-05-30 21:11:06 +00:00
|
|
|
std::ostringstream output;
|
2022-04-07 22:21:23 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
auto librarySource = filename.toStdString();
|
2022-04-07 22:21:23 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
auto instance = Application::instance();
|
|
|
|
if(!instance->loadLibrary(librarySource, output)) {
|
2022-06-13 22:53:46 +00:00
|
|
|
auto dialog = new display::ErrorDialog(output);
|
|
|
|
dialog->exec();
|
2022-06-19 18:10:44 +00:00
|
|
|
} else {
|
|
|
|
MessageSource::instance()->load(Application::instance()->getLibrary()->getMessages());
|
|
|
|
updateTranslations();
|
2022-04-07 22:21:23 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:37:14 +00:00
|
|
|
libraryDisplay->refreshContent();
|
|
|
|
schemaDisplay->refreshContent();
|
2022-05-30 21:11:06 +00:00
|
|
|
}
|
2022-04-07 22:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::onLoadSchema() {
|
|
|
|
auto filename = QFileDialog::getOpenFileName(this,
|
2022-06-19 18:10:44 +00:00
|
|
|
QMESSAGE("msg_files_load_schema"),
|
|
|
|
"",
|
|
|
|
QMESSAGE("msg_files_load_schema_format"));
|
2022-04-07 22:21:23 +00:00
|
|
|
if(!filename.isEmpty()) {
|
2022-05-30 21:11:06 +00:00
|
|
|
std::ostringstream output;
|
2022-04-07 22:21:23 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
auto schemaSource = filename.toStdString();
|
2022-04-07 22:21:23 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
auto instance = Application::instance();
|
2022-06-01 23:37:14 +00:00
|
|
|
auto result = instance->loadSchema(schemaSource, output);
|
|
|
|
if(!result.first){
|
|
|
|
formatErrors(result.second, output);
|
2022-06-13 22:53:46 +00:00
|
|
|
auto dialog = new display::ErrorDialog(output);
|
|
|
|
dialog->exec();
|
2022-06-19 18:10:44 +00:00
|
|
|
} else {
|
|
|
|
MessageSource::instance()->load(Application::instance()->getLibrary()->getMessages());
|
|
|
|
updateTranslations();
|
2022-04-07 22:21:23 +00:00
|
|
|
}
|
2022-04-12 21:37:05 +00:00
|
|
|
|
2022-06-01 23:37:14 +00:00
|
|
|
libraryDisplay->refreshContent();
|
|
|
|
schemaDisplay->refreshContent();
|
2022-05-30 21:11:06 +00:00
|
|
|
}
|
2022-04-10 12:23:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 20:21:45 +00:00
|
|
|
void MainWindow::onStoreScheme() {
|
|
|
|
auto filename = QFileDialog::getSaveFileName(this,
|
2022-06-19 18:10:44 +00:00
|
|
|
QMESSAGE("msg_files_store_schema"),
|
|
|
|
"",
|
|
|
|
QMESSAGE("msg_files_store_schema_format"));
|
2022-05-30 21:11:06 +00:00
|
|
|
if(!filename.isEmpty()) {
|
|
|
|
std::ostringstream output;
|
2022-04-24 20:21:45 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
if(Application::instance()->generateSchema(output)) {
|
|
|
|
std::ofstream out(filename.toStdString(), std::ios::out | std::ios::binary);
|
|
|
|
out<<output.str();
|
|
|
|
out.close();
|
|
|
|
|
2022-06-19 18:10:44 +00:00
|
|
|
auto dialog = new display::SuccessDialog(MESSAGE("msg_dialog_success_save_schema"));
|
2022-06-13 22:53:46 +00:00
|
|
|
dialog->exec();
|
2022-05-30 21:11:06 +00:00
|
|
|
} else {
|
2022-06-13 22:53:46 +00:00
|
|
|
auto dialog = new display::ErrorDialog(output);
|
|
|
|
dialog->exec();
|
2022-05-30 21:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-24 20:21:45 +00:00
|
|
|
|
2022-05-08 22:51:47 +00:00
|
|
|
}
|
2022-04-24 20:21:45 +00:00
|
|
|
|
2022-05-08 22:51:47 +00:00
|
|
|
void MainWindow::onGenerateComdel() {
|
|
|
|
auto filename = QFileDialog::getSaveFileName(this,
|
2022-06-19 18:10:44 +00:00
|
|
|
QMESSAGE("msg_files_store_comdel"),
|
|
|
|
"",
|
|
|
|
QMESSAGE("msg_files_store_comdel_format"));
|
2022-05-30 21:11:06 +00:00
|
|
|
if(!filename.isEmpty()) {
|
|
|
|
std::ostringstream output;
|
2022-04-24 20:21:45 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
auto validationErrors = Application::instance()->generateComdel(output);
|
2022-04-24 20:21:45 +00:00
|
|
|
|
2022-06-19 18:10:44 +00:00
|
|
|
std::ostringstream errorOutput;
|
|
|
|
formatErrors(validationErrors, errorOutput);
|
2022-04-24 20:21:45 +00:00
|
|
|
|
2022-06-05 17:02:44 +00:00
|
|
|
if(!Application::hasErrors(validationErrors)) {
|
2022-05-30 21:11:06 +00:00
|
|
|
std::ofstream out(filename.toStdString(), std::ios::out | std::ios::binary);
|
|
|
|
out<<output.str();
|
|
|
|
out.close();
|
2022-05-08 22:51:47 +00:00
|
|
|
|
2022-06-19 18:10:44 +00:00
|
|
|
auto dialog = new display::SuccessDialog(MESSAGE("msg_dialog_success_comdel_generation"));
|
2022-06-13 22:53:46 +00:00
|
|
|
dialog->exec();
|
2022-05-30 21:11:06 +00:00
|
|
|
} else {
|
2022-06-19 18:10:44 +00:00
|
|
|
auto dialog = new display::ErrorDialog(errorOutput);
|
2022-06-13 22:53:46 +00:00
|
|
|
dialog->exec();
|
2022-05-30 21:11:06 +00:00
|
|
|
}
|
2022-04-12 21:37:05 +00:00
|
|
|
}
|
2022-05-30 21:11:06 +00:00
|
|
|
}
|
2022-04-12 21:37:05 +00:00
|
|
|
|
2022-04-10 12:23:18 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
void MainWindow::onValidateSchema(bool /*toggled*/) {
|
2022-04-10 12:23:18 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
auto errors = Application::instance()->validateSchema();
|
2022-05-19 22:44:40 +00:00
|
|
|
|
2022-06-13 22:53:46 +00:00
|
|
|
if(Application::hasErrors(errors)) {
|
|
|
|
std::ostringstream buff;
|
|
|
|
formatErrors(errors, buff);
|
|
|
|
auto dialog = new display::ErrorDialog(buff);
|
|
|
|
dialog->exec();
|
|
|
|
} else {
|
2022-06-19 18:10:44 +00:00
|
|
|
auto dialog = new display::SuccessDialog(MESSAGE("msg_dialog_success_validation"));
|
2022-06-13 22:53:46 +00:00
|
|
|
dialog->exec();
|
|
|
|
}
|
2022-05-30 21:11:06 +00:00
|
|
|
}
|
2022-04-10 12:23:18 +00:00
|
|
|
|
2022-05-30 21:11:06 +00:00
|
|
|
void MainWindow::formatErrors(std::vector<domain::ValidationError>& errors, std::ostream& output) {
|
|
|
|
for(auto& err: errors) {
|
2022-05-01 16:13:01 +00:00
|
|
|
if(err.instance != nullptr) {
|
2022-05-30 21:11:06 +00:00
|
|
|
output << err.instance->name;
|
2022-04-10 12:23:18 +00:00
|
|
|
}
|
2022-05-01 16:13:01 +00:00
|
|
|
if(err.attribute != nullptr) {
|
2022-05-30 21:11:06 +00:00
|
|
|
output << "::" << err.attribute->name;
|
2022-04-10 12:23:18 +00:00
|
|
|
}
|
|
|
|
if(err.type == domain::Action::ERROR) {
|
2022-05-30 21:11:06 +00:00
|
|
|
output << " [ERROR] ";
|
2022-04-10 12:23:18 +00:00
|
|
|
} else {
|
2022-05-30 21:11:06 +00:00
|
|
|
output << " [WARNING] ";
|
2022-04-10 12:23:18 +00:00
|
|
|
}
|
2022-05-30 21:11:06 +00:00
|
|
|
output << err.message << std::endl;
|
2022-04-10 12:23:18 +00:00
|
|
|
}
|
2022-04-07 22:21:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 19:31:45 +00:00
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
2022-06-09 18:24:27 +00:00
|
|
|
}
|
2022-06-19 18:10:44 +00:00
|
|
|
|
|
|
|
void MainWindow::updateTranslations() {
|
|
|
|
loadLibrary->setText(QMESSAGE("msg_toolbar_load_library"));
|
|
|
|
loadSchema->setText(QMESSAGE("msg_toolbar_load_schema"));
|
|
|
|
saveSchema->setText(QMESSAGE("msg_toolbar_save_schema"));
|
|
|
|
generateComdel->setText(QMESSAGE("msg_toolbar_generate_comdel"));
|
|
|
|
}
|