Subversion Repositories Code-Repo

Rev

Rev 346 | Blame | Last modification | View Log | RSS feed

#include "MacroController.h"

MacroController::MacroController(QWidget *parent) : QWidget(parent)
{
    sigMapper = new QSignalMapper();

    btnExport = new QPushButton("&Export");
    btnImport = new QPushButton("&Import");
    btnIncreaseMacro = new QPushButton("&Add");
    btnDecreaseMacro = new QPushButton("&Remove");

    mainLayout = new QGridLayout();

    macroCount = 0;
    connected = false;
    lastKnownFilePath = ".";

    ioLayout = new QHBoxLayout();
    ioLayout->addStretch();
    ioLayout->addWidget(btnIncreaseMacro);
    ioLayout->addWidget(btnDecreaseMacro);
    ioLayout->addWidget(btnExport);
    ioLayout->addWidget(btnImport);
    mainLayout->addLayout(ioLayout, 0, 0, 1, 2);

    for (int i = 0; i < MACRO_DEFAULT_COUNT; i++) {
        Macro_AddEntry();
    }

    setLayout(mainLayout);
    this->resize(this->minimumSizeHint());

    connect(btnIncreaseMacro, SIGNAL(clicked()), this, SLOT(Macro_AddEntry()));
    connect(btnDecreaseMacro, SIGNAL(clicked()), this, SLOT(Macro_RemoveEntry()));
    connect(btnExport, SIGNAL(clicked()), this, SLOT(Macro_WriteToFile()));
    connect(btnImport, SIGNAL(clicked()), this, SLOT(Macro_ReadFromFile()));
    connect(sigMapper, SIGNAL(mapped(QWidget*)), this, SLOT(Macro_InitTransmit(QWidget*)));

    setWindowTitle("Macro Settings");
    setWindowIcon(QIcon(":/External/Resources/Icon_16.bmp"));
}

MacroController::~MacroController()
{

}

void MacroController::Macro_EnableTransmit()
{
    connected = true;
    for (int i = 0; i < macroBtnList.size(); i++) {
        macroBtnList[i]->setEnabled(true);
    }
}

void MacroController::Macro_DisableTransmit()
{
    connected = false;
    for (int i = 0; i < macroBtnList.size(); i++) {
        macroBtnList[i]->setEnabled(false);
    }
}

void MacroController::Macro_InitTransmit(QWidget *t)
{
    QTextEdit *text = qobject_cast<QTextEdit*>(t);
    emit Macro_TransmitText(text->toPlainText());
}

void MacroController::Macro_AddEntry()
{
    macroCount++;

    // Create new layout/widgets
    QLineEdit *lineEdit = new QLineEdit(QString("Macro %1").arg(macroCount));
    lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    lineEdit->setAlignment(Qt::AlignCenter | Qt::AlignTop);
    macroNameList.append(lineEdit);

    QTextEdit *textEdit = new QTextEdit();
    textEdit->setMinimumHeight(50);
    textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    textEdit->setWordWrapMode(QTextOption::NoWrap);
    textEdit->setFont(QFont("Consolas", 8));
    textEdit->setAcceptRichText(false);
    textEdit->setTabChangesFocus(true);
    macroValueList.append(textEdit);

    QPushButton *pushButton = new QPushButton("Send Macro");
    pushButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    pushButton->setEnabled(connected);
    macroBtnList.append(pushButton);

    QVBoxLayout *tmpLayout = new QVBoxLayout();
    tmpLayout->addWidget(lineEdit);
    tmpLayout->addWidget(pushButton);

    // Add layout/widgets to main layout
    mainLayout->addLayout(tmpLayout, macroCount, 0);
    mainLayout->addWidget(textEdit, macroCount, 1);
    mainLayout->setColumnStretch(1, 1);

    // Associate PushButton with its corresponding TextEdit
    sigMapper->setMapping(pushButton, textEdit);
    connect(pushButton, SIGNAL(clicked()), sigMapper, SLOT(map()));

    QLayoutItem *item = mainLayout->itemAtPosition(1, 1);
    int height = item->widget()->height() + mainLayout->verticalSpacing();
    if (!this->isMaximized())
        this->resize(this->width(), this->height() + height);

    btnDecreaseMacro->setEnabled(true);
}

void MacroController::Macro_RemoveEntry()
{

    // Remove and delete layout/widgets at last macro slot
    QLayoutItem *item = mainLayout->itemAtPosition(macroCount, 0);
    while(!item->isEmpty())
        delete item->layout()->takeAt(0)->widget();
    delete item;

    item = mainLayout->itemAtPosition(macroCount, 1);
    delete item->widget();

    item = mainLayout->itemAtPosition(1, 1);
    int height = item->widget()->height() + mainLayout->verticalSpacing();

    // Unmap and remove widgets from lists
    QPushButton *pushButton = macroBtnList.back();
    sigMapper->removeMappings(pushButton);

    macroNameList.pop_back();
    macroValueList.pop_back();
    macroBtnList.pop_back();

    macroCount--;

    if (!this->isMaximized())
        this->resize(this->width(), this->height() - height);

    if (macroCount == 1)
        btnDecreaseMacro->setEnabled(false);
}

void MacroController::Macro_WriteToFile()
{
    QString file = QFileDialog::getSaveFileName(this, "Settings XML File", lastKnownFilePath, "XML File (*.xml)");
    QCoreApplication::processEvents(); // Wait for dialog to close

    if (file.size() == 0) return;

    // If file was selected, save directory for next time
    QFileInfo fileInfo = QFileInfo(file);
    lastKnownFilePath = fileInfo.absolutePath();

    QFile inputFile(file);
    if (!inputFile.open(QIODevice::ReadWrite | QIODevice::Text)) return;

    QXmlStreamWriter stream(&inputFile);
    stream.setAutoFormatting(true);

    stream.writeStartDocument();
    stream.writeStartElement("Settings");
    stream.writeStartElement("Macro");

    for (int i = 0; i < macroCount; i++) {
        stream.writeStartElement("Macro_Entry");

        stream.writeAttribute("Name", macroNameList[i]->text());
        stream.writeTextElement("Value", macroValueList[i]->toPlainText());

        stream.writeEndElement(); // Macro Entry
    }

    stream.writeEndElement(); // Macro
    stream.writeEndElement(); // Settings
    stream.writeEndDocument();

    inputFile.close();
}

void MacroController::Macro_ReadFromFile()
{
    int counter = 0;

    QString file = QFileDialog::getOpenFileName(this, "Settings XML File", lastKnownFilePath, "XML File (*.xml)");
    QCoreApplication::processEvents(); // Wait for dialog to close

    if (file.size() == 0) return;

    // If file was selected, save directory for next time
    QFileInfo fileInfo = QFileInfo(file);
    lastKnownFilePath = fileInfo.absolutePath();

    QFile inputFile(file);
    if (!inputFile.open(QIODevice::ReadWrite | QIODevice::Text)) return;

    QXmlStreamReader stream(&inputFile);

    // Parse the XML file till we reach the end  (or errors)
    while (!stream.atEnd() && !stream.hasError()) {
        QXmlStreamReader::TokenType token = stream.readNext();

        // Ignore StartDocument
        if (token == QXmlStreamReader::StartDocument) continue;

        // Parse StartElements
        if (token == QXmlStreamReader::StartElement) {

            // Ignore elements <Settings> and <Macro>
            if (stream.name() == "Settings") continue;
            if (stream.name() == "Macro") continue;

            // Parse element <Macro_Entry>
            if (stream.name() == "Macro_Entry") {
                QString name, value;

                // Read and save attribute value
                QXmlStreamAttributes attr = stream.attributes();
                if (attr.hasAttribute("Name"))
                    name = attr.value("Name").toString();

                stream.readNext();

                // Loop in this element to find all sub-elements
                while (!(stream.tokenType() == QXmlStreamReader::EndElement && stream.name() == "Macro_Entry")) {

                    // Parse and save element value
                    if (stream.tokenType() == QXmlStreamReader::StartElement) {
                        if (stream.name() == "Value") {
                            stream.readNext();
                            value = stream.text().toString();
                        }
                    }
                    stream.readNext();
                }

                // Write values to GUI
                if (counter == macroCount)
                    Macro_AddEntry();

                macroNameList[counter]->setText(name);
                macroValueList[counter]->setText(value);
                counter++;
            }
        }
    }
}