XMLCommProtocolWidget.cc 6.1 KB
Newer Older
1 2 3 4
#include <QFileDialog>
#include <QTextBrowser>
#include <QMessageBox>
#include <QSettings>
lm's avatar
lm committed
5
#include <QDesktopServices>
6 7 8 9

#include "XMLCommProtocolWidget.h"
#include "ui_XMLCommProtocolWidget.h"
#include "MAVLinkXMLParser.h"
lm's avatar
lm committed
10
#include "MAVLinkXMLParserV10.h"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include <QDebug>
#include <iostream>

XMLCommProtocolWidget::XMLCommProtocolWidget(QWidget *parent) :
    QWidget(parent),
    model(NULL),
m_ui(new Ui::XMLCommProtocolWidget)
{
    m_ui->setupUi(this);
	
    connect(m_ui->selectFileButton, SIGNAL(clicked()), this, SLOT(selectXMLFile()));
    connect(m_ui->selectOutputButton, SIGNAL(clicked()), this, SLOT(selectOutputDirectory()));
    connect(m_ui->generateButton, SIGNAL(clicked()), this, SLOT(generate()));
    connect(m_ui->saveButton, SIGNAL(clicked()), this, SLOT(save()));
	
    // Make sure text background is white
    m_ui->xmlTextView->setStyleSheet("QGCMAVLinkTextEdit { background-color: #FFFFFF; }");
}

void XMLCommProtocolWidget::selectXMLFile()
{
    QSettings settings("MAVLink Consortium", "MAVLink Generator");
    const QString mavlinkXML = "MAVLINK_XML_FILE";
    QString dirPath = settings.value(mavlinkXML, QCoreApplication::applicationDirPath() + "../").toString();
    QFileInfo dir(dirPath);
    QFileDialog dialog;
    dialog.setDirectory(dir.absoluteDir());
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setFilter(tr("MAVLink XML (*.xml)"));
    dialog.setViewMode(QFileDialog::Detail);
    QStringList fileNames;
lm's avatar
lm committed
43 44
    if (dialog.exec())
    {
45 46 47
        fileNames = dialog.selectedFiles();
    }
	
lm's avatar
lm committed
48 49
    if (fileNames.size() > 0)
    {
50
        QFile file(fileNames.first());
lm's avatar
lm committed
51
        m_ui->fileNameLabel->setText(file.fileName());
52
		
lm's avatar
lm committed
53 54
        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
55 56 57 58 59
            const QString instanceText(QString::fromUtf8(file.readAll()));
            setXML(instanceText);
            // Store filename for next time
            settings.setValue(mavlinkXML, QFileInfo(file).absoluteFilePath());
            settings.sync();
lm's avatar
lm committed
60 61 62
        }
        else
        {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            QMessageBox msgBox;
            msgBox.setText("Could not read XML file. Permission denied");
            msgBox.exec();
        }
    }
}

void XMLCommProtocolWidget::setXML(const QString& xml)
{
    m_ui->xmlTextView->setText(xml);
    QDomDocument doc;
	
    if (doc.setContent(xml)) {
        m_ui->validXMLLabel->setText(tr("<font color=\"green\">Valid XML file</font>"));
    } else {
        m_ui->validXMLLabel->setText(tr("<font color=\"red\">File is NOT valid XML, please fix in editor</font>"));
    }
}

void XMLCommProtocolWidget::selectOutputDirectory()
{
    QSettings settings("MAVLink Consortium", "MAVLink Generator");
    const QString mavlinkOutputDir = "MAVLINK_OUTPUT_DIR";
lm's avatar
lm committed
86
    QString dirPath = settings.value(mavlinkOutputDir, QDesktopServices::DesktopLocation).toString();
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    QFileDialog dialog;
    dialog.setDirectory(dirPath);
    dialog.setFileMode(QFileDialog::Directory);
    dialog.setViewMode(QFileDialog::Detail);
    QStringList fileNames;
    if (dialog.exec()) {
        fileNames = dialog.selectedFiles();
    }
	
    if (fileNames.size() > 0) {
        m_ui->outputDirNameLabel->setText(fileNames.first());
        // Store directory for next time
        settings.setValue(mavlinkOutputDir, QFileInfo(fileNames.first()).absoluteFilePath());
        settings.sync();
        //QFile file(fileName);
    }
}

void XMLCommProtocolWidget::generate()
{
    // Check if input file is present
    if (!QFileInfo(m_ui->fileNameLabel->text().trimmed()).isFile()) {
        QMessageBox::critical(this, tr("Please select an XML input file first"), tr("You have to select an input XML file before generating C files."), QMessageBox::Ok);
        return;
    }
	
    // Check if output dir is selected
    if (!QFileInfo(m_ui->outputDirNameLabel->text().trimmed()).isDir()) {
        QMessageBox::critical(this, tr("Please select output directory first"), tr("You have to select an output directory before generating C files."), QMessageBox::Ok);
        return;
    }
	
    // First save file
    save();
    // Clean log
    m_ui->compileLog->clear();
	
    // Check XML validity
    if (!m_ui->xmlTextView->syntaxcheck())
    {
        // Syntax check already gives output
        return;
    }
lm's avatar
lm committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    MAVLinkXMLParser* parser = NULL;
    MAVLinkXMLParserV10* parserV10 = NULL;

    bool result = false;

    if (m_ui->versionComboBox->currentIndex() == 1)
    {
        MAVLinkXMLParser* parser = new MAVLinkXMLParser(m_ui->fileNameLabel->text().trimmed(), m_ui->outputDirNameLabel->text().trimmed());
        connect(parser, SIGNAL(parseState(QString)), m_ui->compileLog, SLOT(appendHtml(QString)));
        result = parser->generate();
    }
    else if (m_ui->versionComboBox->currentIndex() == 0)
    {
        MAVLinkXMLParserV10* parserV10 = new MAVLinkXMLParserV10(m_ui->fileNameLabel->text().trimmed(), m_ui->outputDirNameLabel->text().trimmed());
        connect(parserV10, SIGNAL(parseState(QString)), m_ui->compileLog, SLOT(appendHtml(QString)));
        result = parserV10->generate();
    }

    if (result)
    {
151 152 153
        QMessageBox msgBox;
        msgBox.setText(QString("The C code / headers have been generated in folder\n%1").arg(m_ui->outputDirNameLabel->text().trimmed()));
        msgBox.exec();
lm's avatar
lm committed
154 155 156
    }
    else
    {
157 158
        QMessageBox::critical(this, tr("C code generation failed, please see the compile log for further information"), QString("The C code / headers could not be written to folder\n%1").arg(m_ui->outputDirNameLabel->text().trimmed()), QMessageBox::Ok);
    }
lm's avatar
lm committed
159 160
    if (parser) delete parser;
    if (parserV10) delete parserV10;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}

void XMLCommProtocolWidget::save()
{
    QFile file(m_ui->fileNameLabel->text().trimmed());
    setXML(m_ui->xmlTextView->document()->toPlainText().toUtf8());
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    file.write(m_ui->xmlTextView->document()->toPlainText().toUtf8());
}

XMLCommProtocolWidget::~XMLCommProtocolWidget()
{
    if (model) delete model;
    delete m_ui;
}

void XMLCommProtocolWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
		case QEvent::LanguageChange:
			m_ui->retranslateUi(this);
			break;
		default:
			break;
    }
}