#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QtXml/QDomDocument>
#include <QTextStream>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_toolButton_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(), tr("Rich Text Files (*.rtf);;"));
if (fileName.isEmpty())
return;
if (!fileName.endsWith(QLatin1String(".rtf")))
fileName += QLatin1String(".rtf");
QFile file(fileName);
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
return;
}
QDomDocument doc;
doc.setContent(&file);
QDomElement root = doc.documentElement();
QDomElement body;
if (!root.isNull()) {
body = root.firstChildElement(QLatin1String("body"));
}
if (body.isNull()) {
root = doc.createElement(QLatin1String("html"));
QDomElement head = doc.createElement(QLatin1String("head"));
QDomElement meta = doc.createElement(QLatin1String("meta"));
meta.setAttribute(QLatin1String("http-equiv"), QLatin1String("Content-Type"));
meta.setAttribute(QLatin1String("content"), QLatin1String("text/html; charset=utf-8"));
head.appendChild(meta);
root.appendChild(head);
body = doc.createElement(QLatin1String("body"));
root.appendChild(body);
doc.appendChild(root);
}
if (body.isNull())
return;
QDomElement subtitleParagraph = doc.createElement(QLatin1String("p"));
QDomText subtitleText = doc.createTextNode(tr("Subtitle:")+QLatin1String("this is a test"));
subtitleParagraph.appendChild(subtitleText);
QDomElement tempLabel = doc.createElement(QLatin1String("span"));
tempLabel.setAttribute(QLatin1String("style"), QLatin1String("color:red"));
QDomText tempNode = doc.createTextNode(QLatin1String(" this is red"));
tempLabel.appendChild(tempNode);
subtitleParagraph.appendChild(tempLabel);
body.appendChild(subtitleParagraph);
QTextStream out(&file);
doc.save(out, 4);
file.close();
}