Add basic html rich text support

This commit is contained in:
Debao Zhang
2014-01-09 12:22:56 +08:00
parent 01c4c2e4f4
commit 2455ebb138
8 changed files with 72 additions and 15 deletions
+5
View File
@@ -1,7 +1,9 @@
#include <QtCore>
#include "xlsxdocument.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
#include "xlsxformat.h"
int main()
{
//![0]
@@ -22,6 +24,9 @@ int main()
rich.addFragment("Qt ", red);
rich.addFragment("Xlsx", bold);
xlsx.write("B2", rich);
xlsx.workbook()->setHtmlToRichStringEnabled(true);
xlsx.write("B4", "<b>Hello</b> <font color=\"red\">Qt</font> <i>Xlsx</i>");
//![1]
//![2]
+3 -2
View File
@@ -331,7 +331,7 @@ int Format::fontSize() const
*/
void Format::setFontSize(int size)
{
setProperty(FormatPrivate::P_Font_Size, size);
setProperty(FormatPrivate::P_Font_Size, size, 0);
}
/*!
@@ -486,7 +486,8 @@ QFont Format::font() const
void Format::setFont(const QFont &font)
{
setFontName(font.family());
setFontSize(font.pointSize());
if (font.pointSize() > 0)
setFontSize(font.pointSize());
setFontBold(font.bold());
setFontItalic(font.italic());
setFontUnderline(font.underline() ? FontUnderlineSingle : FontUnderlineNone);
+36 -1
View File
@@ -26,6 +26,8 @@
#include "xlsxrichstring_p.h"
#include "xlsxformat_p.h"
#include <QDebug>
#include <QTextDocument>
#include <QTextFragment>
QT_BEGIN_NAMESPACE_XLSX
@@ -68,7 +70,10 @@ RichString::RichString()
RichString::RichString(const QString text)
:d(new RichStringPrivate)
{
addFragment(text, Format());
if (Qt::mightBeRichText(text))
setHtml(text);
else
addFragment(text, Format());
}
/*!
@@ -149,6 +154,36 @@ QString RichString::toPlainString() const
return d->fragmentTexts.join(QString());
}
/*!
Converts to html string
*/
QString RichString::toHtml() const
{
//: Todo
return QString();
}
/*!
Replaces the entire contents of the document
with the given HTML-formatted text in the \a text string
*/
void RichString::setHtml(const QString &text)
{
QTextDocument doc;
doc.setHtml(text);
QTextBlock block = doc.firstBlock();
QTextBlock::iterator it;
for (it = block.begin(); !(it.atEnd()); ++it) {
QTextFragment textFragment = it.fragment();
if (textFragment.isValid()) {
Format fmt;
fmt.setFont(textFragment.charFormat().font());
fmt.setFontColor(textFragment.charFormat().foreground().color());
addFragment(textFragment.text(), fmt);
}
}
}
/*!
Returns fragment count.
*/
+2
View File
@@ -49,6 +49,8 @@ public:
bool isNull() const;
bool isEmtpy() const;
QString toPlainString() const;
QString toHtml() const;
void setHtml(const QString &text);
int fragmentCount() const;
void addFragment(const QString &text, const Format &format);
+13
View File
@@ -51,6 +51,7 @@ WorkbookPrivate::WorkbookPrivate(Workbook *q) :
window_height = 9660;
strings_to_numbers_enabled = false;
html_to_richstring_enabled = false;
date1904 = false;
defaultDateFormat = QStringLiteral("yyyy-mm-dd");
activesheetIndex = 0;
@@ -113,6 +114,18 @@ bool Workbook::isStringsToNumbersEnabled() const
return d->strings_to_numbers_enabled;
}
void Workbook::setHtmlToRichStringEnabled(bool enable)
{
Q_D(Workbook);
d->html_to_richstring_enabled = enable;
}
bool Workbook::isHtmlToRichStringEnabled() const
{
Q_D(const Workbook);
return d->html_to_richstring_enabled;
}
QString Workbook::defaultDateFormat() const
{
Q_D(const Workbook);
+2
View File
@@ -70,6 +70,8 @@ public:
void setDate1904(bool date1904);
bool isStringsToNumbersEnabled() const;
void setStringsToNumbersEnabled(bool enable=true);
bool isHtmlToRichStringEnabled() const;
void setHtmlToRichStringEnabled(bool enable=true);
QString defaultDateFormat() const;
void setDefaultDateFormat(const QString &format);
+1
View File
@@ -94,6 +94,7 @@ public:
QList<XlsxSheetItemInfo> sheetItemInfoList;//Data from xml file
bool strings_to_numbers_enabled;
bool html_to_richstring_enabled;
bool date1904;
QString defaultDateFormat;
+10 -12
View File
@@ -47,6 +47,7 @@
#include <QBuffer>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QTextDocument>
#include <math.h>
@@ -621,7 +622,7 @@ int Worksheet::writeString(int row, int column, const RichString &value, const F
d->sharedStrings()->addSharedString(value);
Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->workbook->styles()->addXfFormat(fmt);
QSharedPointer<Cell> cell = QSharedPointer<Cell>(new Cell(QString(), Cell::String, fmt, this));
QSharedPointer<Cell> cell = QSharedPointer<Cell>(new Cell(value.toPlainString(), Cell::String, fmt, this));
cell->d_ptr->richString = value;
d->cellTable[row][column] = cell;
return error;
@@ -641,26 +642,23 @@ int Worksheet::writeString(const QString &row_column, const QString &value, cons
}
/*!
\overload
Write string \a value to the cell (\a row, \a column) with the \a format
*/
int Worksheet::writeString(int row, int column, const QString &value, const Format &format)
{
Q_D(Worksheet);
int error = 0;
QString content = value;
if (d->checkDimensions(row, column))
return -1;
if (value.size() > XLSX_STRING_MAX) {
content = value.left(XLSX_STRING_MAX);
error = -2;
}
RichString rs;
if (Qt::mightBeRichText(value) && d->workbook->isHtmlToRichStringEnabled())
rs.setHtml(value);
else
rs.addFragment(value, Format());
d->sharedStrings()->addSharedString(content);
Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->workbook->styles()->addXfFormat(fmt);
d->cellTable[row][column] = QSharedPointer<Cell>(new Cell(content, Cell::String, fmt, this));
return error;
return writeString(row, column, rs, format);
}
/*!