Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
345 Kevin 1
#include "MacroController.h"
2
 
3
MacroController::MacroController(QWidget *parent) : QWidget(parent)
4
{
5
    sigMapper = new QSignalMapper();
6
 
7
    btnExport = new QPushButton("&Export");
8
    btnImport = new QPushButton("&Import");
9
    btnIncreaseMacro = new QPushButton("&Add");
10
    btnDecreaseMacro = new QPushButton("&Remove");
11
 
12
    mainLayout = new QGridLayout();
13
 
14
    macroCount = 0;
15
    connected = false;
346 Kevin 16
    lastKnownFilePath = ".";
345 Kevin 17
 
18
    ioLayout = new QHBoxLayout();
19
    ioLayout->addStretch();
20
    ioLayout->addWidget(btnIncreaseMacro);
21
    ioLayout->addWidget(btnDecreaseMacro);
22
    ioLayout->addWidget(btnExport);
23
    ioLayout->addWidget(btnImport);
24
    mainLayout->addLayout(ioLayout, 0, 0, 1, 2);
25
 
26
    for (int i = 0; i < MACRO_DEFAULT_COUNT; i++) {
27
        Macro_AddEntry();
28
    }
29
 
30
    setLayout(mainLayout);
31
    this->resize(this->minimumSizeHint());
32
 
33
    connect(btnIncreaseMacro, SIGNAL(clicked()), this, SLOT(Macro_AddEntry()));
34
    connect(btnDecreaseMacro, SIGNAL(clicked()), this, SLOT(Macro_RemoveEntry()));
35
    connect(btnExport, SIGNAL(clicked()), this, SLOT(Macro_WriteToFile()));
36
    connect(btnImport, SIGNAL(clicked()), this, SLOT(Macro_ReadFromFile()));
37
    connect(sigMapper, SIGNAL(mapped(QWidget*)), this, SLOT(Macro_InitTransmit(QWidget*)));
38
 
39
    setWindowTitle("Macro Settings");
347 Kevin 40
    setWindowIcon(QIcon(":/External/Resources/Icon_16.bmp"));
345 Kevin 41
}
42
 
43
MacroController::~MacroController()
44
{
45
 
46
}
47
 
48
void MacroController::Macro_EnableTransmit()
49
{
50
    connected = true;
51
    for (int i = 0; i < macroBtnList.size(); i++) {
52
        macroBtnList[i]->setEnabled(true);
53
    }
54
}
55
 
56
void MacroController::Macro_DisableTransmit()
57
{
58
    connected = false;
59
    for (int i = 0; i < macroBtnList.size(); i++) {
60
        macroBtnList[i]->setEnabled(false);
61
    }
62
}
63
 
64
void MacroController::Macro_InitTransmit(QWidget *t)
65
{
66
    QTextEdit *text = qobject_cast<QTextEdit*>(t);
67
    emit Macro_TransmitText(text->toPlainText());
68
}
69
 
70
void MacroController::Macro_AddEntry()
71
{
72
    macroCount++;
73
 
74
    // Create new layout/widgets
75
    QLineEdit *lineEdit = new QLineEdit(QString("Macro %1").arg(macroCount));
76
    lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
77
    lineEdit->setAlignment(Qt::AlignCenter | Qt::AlignTop);
78
    macroNameList.append(lineEdit);
79
 
80
    QTextEdit *textEdit = new QTextEdit();
81
    textEdit->setMinimumHeight(50);
346 Kevin 82
    textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
345 Kevin 83
    textEdit->setWordWrapMode(QTextOption::NoWrap);
84
    textEdit->setFont(QFont("Consolas", 8));
85
    textEdit->setAcceptRichText(false);
346 Kevin 86
    textEdit->setTabChangesFocus(true);
345 Kevin 87
    macroValueList.append(textEdit);
88
 
89
    QPushButton *pushButton = new QPushButton("Send Macro");
90
    pushButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
91
    pushButton->setEnabled(connected);
92
    macroBtnList.append(pushButton);
93
 
94
    QVBoxLayout *tmpLayout = new QVBoxLayout();
95
    tmpLayout->addWidget(lineEdit);
96
    tmpLayout->addWidget(pushButton);
97
 
98
    // Add layout/widgets to main layout
99
    mainLayout->addLayout(tmpLayout, macroCount, 0);
100
    mainLayout->addWidget(textEdit, macroCount, 1);
101
    mainLayout->setColumnStretch(1, 1);
102
 
103
    // Associate PushButton with its corresponding TextEdit
104
    sigMapper->setMapping(pushButton, textEdit);
105
    connect(pushButton, SIGNAL(clicked()), sigMapper, SLOT(map()));
106
 
107
    QLayoutItem *item = mainLayout->itemAtPosition(1, 1);
346 Kevin 108
    int height = item->widget()->height() + mainLayout->verticalSpacing();
109
    if (!this->isMaximized())
110
        this->resize(this->width(), this->height() + height);
345 Kevin 111
 
112
    btnDecreaseMacro->setEnabled(true);
113
}
114
 
115
void MacroController::Macro_RemoveEntry()
116
{
117
 
118
    // Remove and delete layout/widgets at last macro slot
119
    QLayoutItem *item = mainLayout->itemAtPosition(macroCount, 0);
120
    while(!item->isEmpty())
121
        delete item->layout()->takeAt(0)->widget();
122
    delete item;
123
 
124
    item = mainLayout->itemAtPosition(macroCount, 1);
125
    delete item->widget();
126
 
346 Kevin 127
    item = mainLayout->itemAtPosition(1, 1);
128
    int height = item->widget()->height() + mainLayout->verticalSpacing();
129
 
345 Kevin 130
    // Unmap and remove widgets from lists
131
    QPushButton *pushButton = macroBtnList.back();
132
    sigMapper->removeMappings(pushButton);
133
 
134
    macroNameList.pop_back();
135
    macroValueList.pop_back();
136
    macroBtnList.pop_back();
137
 
138
    macroCount--;
139
 
346 Kevin 140
    if (!this->isMaximized())
141
        this->resize(this->width(), this->height() - height);
345 Kevin 142
 
143
    if (macroCount == 1)
144
        btnDecreaseMacro->setEnabled(false);
145
}
146
 
147
void MacroController::Macro_WriteToFile()
148
{
346 Kevin 149
    QString file = QFileDialog::getSaveFileName(this, "Settings XML File", lastKnownFilePath, "XML File (*.xml)");
150
    QCoreApplication::processEvents(); // Wait for dialog to close
345 Kevin 151
 
346 Kevin 152
    if (file.size() == 0) return;
153
 
154
    // If file was selected, save directory for next time
155
    QFileInfo fileInfo = QFileInfo(file);
156
    lastKnownFilePath = fileInfo.absolutePath();
157
 
158
    QFile inputFile(file);
159
    if (!inputFile.open(QIODevice::ReadWrite | QIODevice::Text)) return;
160
 
161
    QXmlStreamWriter stream(&inputFile);
162
    stream.setAutoFormatting(true);
163
 
164
    stream.writeStartDocument();
165
    stream.writeStartElement("Settings");
166
    stream.writeStartElement("Macro");
167
 
168
    for (int i = 0; i < macroCount; i++) {
169
        stream.writeStartElement("Macro_Entry");
170
 
171
        stream.writeAttribute("Name", macroNameList[i]->text());
172
        stream.writeTextElement("Value", macroValueList[i]->toPlainText());
173
 
174
        stream.writeEndElement(); // Macro Entry
175
    }
176
 
177
    stream.writeEndElement(); // Macro
178
    stream.writeEndElement(); // Settings
179
    stream.writeEndDocument();
180
 
181
    inputFile.close();
345 Kevin 182
}
183
 
184
void MacroController::Macro_ReadFromFile()
185
{
346 Kevin 186
    int counter = 0;
345 Kevin 187
 
346 Kevin 188
    QString file = QFileDialog::getOpenFileName(this, "Settings XML File", lastKnownFilePath, "XML File (*.xml)");
189
    QCoreApplication::processEvents(); // Wait for dialog to close
190
 
191
    if (file.size() == 0) return;
192
 
193
    // If file was selected, save directory for next time
194
    QFileInfo fileInfo = QFileInfo(file);
195
    lastKnownFilePath = fileInfo.absolutePath();
196
 
197
    QFile inputFile(file);
198
    if (!inputFile.open(QIODevice::ReadWrite | QIODevice::Text)) return;
199
 
200
    QXmlStreamReader stream(&inputFile);
201
 
202
    // Parse the XML file till we reach the end  (or errors)
203
    while (!stream.atEnd() && !stream.hasError()) {
204
        QXmlStreamReader::TokenType token = stream.readNext();
205
 
206
        // Ignore StartDocument
207
        if (token == QXmlStreamReader::StartDocument) continue;
208
 
209
        // Parse StartElements
210
        if (token == QXmlStreamReader::StartElement) {
211
 
212
            // Ignore elements <Settings> and <Macro>
213
            if (stream.name() == "Settings") continue;
214
            if (stream.name() == "Macro") continue;
215
 
216
            // Parse element <Macro_Entry>
217
            if (stream.name() == "Macro_Entry") {
218
                QString name, value;
219
 
220
                // Read and save attribute value
221
                QXmlStreamAttributes attr = stream.attributes();
222
                if (attr.hasAttribute("Name"))
223
                    name = attr.value("Name").toString();
224
 
225
                stream.readNext();
226
 
227
                // Loop in this element to find all sub-elements
228
                while (!(stream.tokenType() == QXmlStreamReader::EndElement && stream.name() == "Macro_Entry")) {
229
 
230
                    // Parse and save element value
231
                    if (stream.tokenType() == QXmlStreamReader::StartElement) {
232
                        if (stream.name() == "Value") {
233
                            stream.readNext();
234
                            value = stream.text().toString();
235
                        }
236
                    }
237
                    stream.readNext();
238
                }
239
 
240
                // Write values to GUI
241
                if (counter == macroCount)
242
                    Macro_AddEntry();
243
 
244
                macroNameList[counter]->setText(name);
245
                macroValueList[counter]->setText(value);
246
                counter++;
247
            }
248
        }
249
    }
345 Kevin 250
}