#include "mainwindow.h" #include "ui_mainwindow.h" #include "application.h" #include "comdel/display/dialog/error_dialog.h" #include "comdel/display/dialog/success_dialog.h" #include "message_source.h" #include #include #include #include #include #include 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 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(); 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); } void MainWindow::onLoadLibrary() { auto filename = QFileDialog::getOpenFileName(this, QMESSAGE("msg_files_load_library"), "", QMESSAGE("msg_files_load_library_format")); if(!filename.isEmpty()) { std::ostringstream output; auto librarySource = filename.toStdString(); auto instance = Application::instance(); if(!instance->loadLibrary(librarySource, output)) { auto dialog = new display::ErrorDialog(output); dialog->exec(); } else { MessageSource::instance()->load(Application::instance()->getLibrary()->getMessages()); updateTranslations(); } libraryDisplay->refreshContent(); schemaDisplay->refreshContent(); } } void MainWindow::onLoadSchema() { auto filename = QFileDialog::getOpenFileName(this, QMESSAGE("msg_files_load_schema"), "", QMESSAGE("msg_files_load_schema_format")); if(!filename.isEmpty()) { std::ostringstream output; auto schemaSource = filename.toStdString(); auto instance = Application::instance(); auto result = instance->loadSchema(schemaSource, output); if(!result.first){ formatErrors(result.second, output); auto dialog = new display::ErrorDialog(output); dialog->exec(); } else { MessageSource::instance()->load(Application::instance()->getLibrary()->getMessages()); updateTranslations(); } libraryDisplay->refreshContent(); schemaDisplay->refreshContent(); } } void MainWindow::onStoreScheme() { auto filename = QFileDialog::getSaveFileName(this, QMESSAGE("msg_files_store_schema"), "", QMESSAGE("msg_files_store_schema_format")); if(!filename.isEmpty()) { std::ostringstream output; if(Application::instance()->generateSchema(output)) { std::ofstream out(filename.toStdString(), std::ios::out | std::ios::binary); out<exec(); } else { auto dialog = new display::ErrorDialog(output); dialog->exec(); } } } void MainWindow::onGenerateComdel() { auto filename = QFileDialog::getSaveFileName(this, QMESSAGE("msg_files_store_comdel"), "", QMESSAGE("msg_files_store_comdel_format")); if(!filename.isEmpty()) { std::ostringstream output; auto validationErrors = Application::instance()->generateComdel(output); std::ostringstream errorOutput; formatErrors(validationErrors, errorOutput); if(!Application::hasErrors(validationErrors)) { std::ofstream out(filename.toStdString(), std::ios::out | std::ios::binary); out<exec(); } else { auto dialog = new display::ErrorDialog(errorOutput); dialog->exec(); } } } void MainWindow::onValidateSchema(bool /*toggled*/) { auto errors = Application::instance()->validateSchema(); if(Application::hasErrors(errors)) { std::ostringstream buff; formatErrors(errors, buff); auto dialog = new display::ErrorDialog(buff); dialog->exec(); } else { auto dialog = new display::SuccessDialog(MESSAGE("msg_dialog_success_validation")); dialog->exec(); } } void MainWindow::formatErrors(std::vector& 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; } 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")); }