Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 345 → Rev 346

/Misc Projects/PcMarlinInterface/MacroController.cpp
13,6 → 13,7
 
macroCount = 0;
connected = false;
lastKnownFilePath = ".";
 
ioLayout = new QHBoxLayout();
ioLayout->addStretch();
77,9 → 78,11
 
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");
101,8 → 104,9
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);
int height = item->widget()->height() + mainLayout->verticalSpacing();
if (!this->isMaximized())
this->resize(this->width(), this->height() + height);
 
btnDecreaseMacro->setEnabled(true);
}
117,9 → 121,11
delete item;
 
item = mainLayout->itemAtPosition(macroCount, 1);
int height = item->widget()->height();
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);
130,7 → 136,8
 
macroCount--;
 
this->resize(this->width(), this->height() - height - 8);
if (!this->isMaximized())
this->resize(this->width(), this->height() - height);
 
if (macroCount == 1)
btnDecreaseMacro->setEnabled(false);
138,10 → 145,105
 
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++;
}
}
}
}