Subversion Repositories Code-Repo

Rev

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;

    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");
}

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->setWordWrapMode(QTextOption::NoWrap);
    textEdit->setFont(QFont("Consolas", 8));
    textEdit->setAcceptRichText(false);
    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();
    this->resize(this->width(), this->height() + height + 8);

    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);
    int height = item->widget()->height();
    delete item->widget();

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

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

    macroCount--;

    this->resize(this->width(), this->height() - height - 8);

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

void MacroController::Macro_WriteToFile()
{

}

void MacroController::Macro_ReadFromFile()
{

}