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

#include "XMLCommProtocolWidget.h"
#include "ui_XMLCommProtocolWidget.h"
#include "MAVLinkXMLParser.h"
9
#include "MAVLinkSyntaxHighlighter.h"
10
#include "QGC.h"
pixhawk's avatar
pixhawk committed
11 12 13 14 15 16 17 18 19 20

#include <QDebug>
#include <iostream>

XMLCommProtocolWidget::XMLCommProtocolWidget(QWidget *parent) :
        QWidget(parent),
        m_ui(new Ui::XMLCommProtocolWidget)
{
    m_ui->setupUi(this);

21 22 23
    // Now set syntax highlighter
    highlighter = new MAVLinkSyntaxHighlighter(m_ui->xmlTextView->document());

pixhawk's avatar
pixhawk committed
24 25 26 27
    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()));
28 29

    this->setVisible(false);
pixhawk's avatar
pixhawk committed
30 31 32 33 34
}

void XMLCommProtocolWidget::selectXMLFile()
{
    //QString fileName = QFileDialog::getOpenFileName(this, tr("Load Protocol Definition File"), ".", "*.xml");
35
    QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
36 37
    const QString mavlinkXML = "MAVLINK_XML_FILE";
    QString dirPath = settings.value(mavlinkXML, QCoreApplication::applicationDirPath() + "../").toString();
pixhawk's avatar
pixhawk committed
38
    QFileDialog dialog;
39
    dialog.setDirectory(dirPath);
pixhawk's avatar
pixhawk committed
40 41 42 43 44 45 46 47 48 49 50 51 52
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setFilter(tr("MAVLink XML (*.xml)"));
    dialog.setViewMode(QFileDialog::Detail);
    QStringList fileNames;
    if (dialog.exec())
    {
        fileNames = dialog.selectedFiles();
    }

    if (fileNames.size() > 0)
    {
        m_ui->fileNameLabel->setText(fileNames.first());
        QFile file(fileNames.first());
53

pixhawk's avatar
pixhawk committed
54 55 56 57
        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            const QString instanceText(QString::fromUtf8(file.readAll()));
            setXML(instanceText);
58 59
            // Store filename for next time
            settings.setValue(mavlinkXML, QFileInfo(file).absoluteFilePath());
60
            settings.sync();
pixhawk's avatar
pixhawk committed
61 62 63 64
        }
        else
        {
            QMessageBox msgBox;
65
            msgBox.setText("Could not read XML file. Permission denied");
pixhawk's avatar
pixhawk committed
66 67 68 69 70 71 72 73 74 75 76 77
            msgBox.exec();
        }
    }
}

void XMLCommProtocolWidget::setXML(const QString& xml)
{
    m_ui->xmlTextView->setText(xml);
    QDomDocument doc;

    if (doc.setContent(xml))
    {
78
        m_ui->validXMLLabel->setText(tr("<font color=\"green\">Valid XML file</font>"));
pixhawk's avatar
pixhawk committed
79 80 81
    }
    else
    {
82
        m_ui->validXMLLabel->setText(tr("<font color=\"red\">File is NOT valid XML, please fix in editor</font>"));
pixhawk's avatar
pixhawk committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96
    }

    if (model != NULL)
    {
        m_ui->xmlTreeView->reset();
        //delete model;
    }
    model = new DomModel(doc, this);
    m_ui->xmlTreeView->setModel(model);
    m_ui->xmlTreeView->repaint();
}

void XMLCommProtocolWidget::selectOutputDirectory()
{
97
    QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
98 99
    const QString mavlinkOutputDir = "MAVLINK_OUTPUT_DIR";
    QString dirPath = settings.value(mavlinkOutputDir, QCoreApplication::applicationDirPath() + "../").toString();
pixhawk's avatar
pixhawk committed
100
    QFileDialog dialog;
101
    dialog.setDirectory(dirPath);
pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108 109 110 111 112 113
    dialog.setFileMode(QFileDialog::Directory);
    dialog.setFilter(tr("MAVLink XML (*.xml)"));
    dialog.setViewMode(QFileDialog::Detail);
    QStringList fileNames;
    if (dialog.exec())
    {
        fileNames = dialog.selectedFiles();
    }

    if (fileNames.size() > 0)
    {
        m_ui->outputDirNameLabel->setText(fileNames.first());
114
        // Store directory for next time
115
        settings.setValue(mavlinkOutputDir, QFileInfo(fileNames.first()).absoluteFilePath());
116
        settings.sync();
pixhawk's avatar
pixhawk committed
117 118 119 120 121 122
        //QFile file(fileName);
    }
}

void XMLCommProtocolWidget::generate()
{
123 124 125 126 127 128 129 130 131 132 133 134 135 136
    // 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;
    }

pixhawk's avatar
pixhawk committed
137 138
    // First save file
    save();
139 140
    // Clean log
    m_ui->compileLog->clear();
141

142 143 144
    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)));
    bool result = parser->generate();
pixhawk's avatar
pixhawk committed
145 146 147 148 149 150 151 152
    if (result)
    {
        QMessageBox msgBox;
        msgBox.setText(QString("The C code / headers have been generated in folder\n%1").arg(m_ui->outputDirNameLabel->text().trimmed()));
        msgBox.exec();
    }
    else
    {
153
        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);
pixhawk's avatar
pixhawk committed
154
    }
155
    delete parser;
pixhawk's avatar
pixhawk committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

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()
{
    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;
    }
}