Browse Source

Add basic html rich text support

master
Debao Zhang 11 years ago
parent
commit
2455ebb138
  1. 5
      examples/xlsx/richtext/main.cpp
  2. 3
      src/xlsx/xlsxformat.cpp
  3. 35
      src/xlsx/xlsxrichstring.cpp
  4. 2
      src/xlsx/xlsxrichstring.h
  5. 13
      src/xlsx/xlsxworkbook.cpp
  6. 2
      src/xlsx/xlsxworkbook.h
  7. 1
      src/xlsx/xlsxworkbook_p.h
  8. 22
      src/xlsx/xlsxworksheet.cpp

5
examples/xlsx/richtext/main.cpp

@ -1,7 +1,9 @@
#include <QtCore> #include <QtCore>
#include "xlsxdocument.h" #include "xlsxdocument.h"
#include "xlsxrichstring.h" #include "xlsxrichstring.h"
#include "xlsxworkbook.h"
#include "xlsxformat.h" #include "xlsxformat.h"
int main() int main()
{ {
//![0] //![0]
@ -22,6 +24,9 @@ int main()
rich.addFragment("Qt ", red); rich.addFragment("Qt ", red);
rich.addFragment("Xlsx", bold); rich.addFragment("Xlsx", bold);
xlsx.write("B2", rich); xlsx.write("B2", rich);
xlsx.workbook()->setHtmlToRichStringEnabled(true);
xlsx.write("B4", "<b>Hello</b> <font color=\"red\">Qt</font> <i>Xlsx</i>");
//![1] //![1]
//![2] //![2]

3
src/xlsx/xlsxformat.cpp

@ -331,7 +331,7 @@ int Format::fontSize() const
*/ */
void Format::setFontSize(int size) void Format::setFontSize(int size)
{ {
setProperty(FormatPrivate::P_Font_Size, size); setProperty(FormatPrivate::P_Font_Size, size, 0);
} }
/*! /*!
@ -486,6 +486,7 @@ QFont Format::font() const
void Format::setFont(const QFont &font) void Format::setFont(const QFont &font)
{ {
setFontName(font.family()); setFontName(font.family());
if (font.pointSize() > 0)
setFontSize(font.pointSize()); setFontSize(font.pointSize());
setFontBold(font.bold()); setFontBold(font.bold());
setFontItalic(font.italic()); setFontItalic(font.italic());

35
src/xlsx/xlsxrichstring.cpp

@ -26,6 +26,8 @@
#include "xlsxrichstring_p.h" #include "xlsxrichstring_p.h"
#include "xlsxformat_p.h" #include "xlsxformat_p.h"
#include <QDebug> #include <QDebug>
#include <QTextDocument>
#include <QTextFragment>
QT_BEGIN_NAMESPACE_XLSX QT_BEGIN_NAMESPACE_XLSX
@ -68,6 +70,9 @@ RichString::RichString()
RichString::RichString(const QString text) RichString::RichString(const QString text)
:d(new RichStringPrivate) :d(new RichStringPrivate)
{ {
if (Qt::mightBeRichText(text))
setHtml(text);
else
addFragment(text, Format()); addFragment(text, Format());
} }
@ -149,6 +154,36 @@ QString RichString::toPlainString() const
return d->fragmentTexts.join(QString()); 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. Returns fragment count.
*/ */

2
src/xlsx/xlsxrichstring.h

@ -49,6 +49,8 @@ public:
bool isNull() const; bool isNull() const;
bool isEmtpy() const; bool isEmtpy() const;
QString toPlainString() const; QString toPlainString() const;
QString toHtml() const;
void setHtml(const QString &text);
int fragmentCount() const; int fragmentCount() const;
void addFragment(const QString &text, const Format &format); void addFragment(const QString &text, const Format &format);

13
src/xlsx/xlsxworkbook.cpp

@ -51,6 +51,7 @@ WorkbookPrivate::WorkbookPrivate(Workbook *q) :
window_height = 9660; window_height = 9660;
strings_to_numbers_enabled = false; strings_to_numbers_enabled = false;
html_to_richstring_enabled = false;
date1904 = false; date1904 = false;
defaultDateFormat = QStringLiteral("yyyy-mm-dd"); defaultDateFormat = QStringLiteral("yyyy-mm-dd");
activesheetIndex = 0; activesheetIndex = 0;
@ -113,6 +114,18 @@ bool Workbook::isStringsToNumbersEnabled() const
return d->strings_to_numbers_enabled; 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 QString Workbook::defaultDateFormat() const
{ {
Q_D(const Workbook); Q_D(const Workbook);

2
src/xlsx/xlsxworkbook.h

@ -70,6 +70,8 @@ public:
void setDate1904(bool date1904); void setDate1904(bool date1904);
bool isStringsToNumbersEnabled() const; bool isStringsToNumbersEnabled() const;
void setStringsToNumbersEnabled(bool enable=true); void setStringsToNumbersEnabled(bool enable=true);
bool isHtmlToRichStringEnabled() const;
void setHtmlToRichStringEnabled(bool enable=true);
QString defaultDateFormat() const; QString defaultDateFormat() const;
void setDefaultDateFormat(const QString &format); void setDefaultDateFormat(const QString &format);

1
src/xlsx/xlsxworkbook_p.h

@ -94,6 +94,7 @@ public:
QList<XlsxSheetItemInfo> sheetItemInfoList;//Data from xml file QList<XlsxSheetItemInfo> sheetItemInfoList;//Data from xml file
bool strings_to_numbers_enabled; bool strings_to_numbers_enabled;
bool html_to_richstring_enabled;
bool date1904; bool date1904;
QString defaultDateFormat; QString defaultDateFormat;

22
src/xlsx/xlsxworksheet.cpp

@ -47,6 +47,7 @@
#include <QBuffer> #include <QBuffer>
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QTextDocument>
#include <math.h> #include <math.h>
@ -621,7 +622,7 @@ int Worksheet::writeString(int row, int column, const RichString &value, const F
d->sharedStrings()->addSharedString(value); d->sharedStrings()->addSharedString(value);
Format fmt = format.isValid() ? format : d->cellFormat(row, column); Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->workbook->styles()->addXfFormat(fmt); 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; cell->d_ptr->richString = value;
d->cellTable[row][column] = cell; d->cellTable[row][column] = cell;
return error; 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 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) int Worksheet::writeString(int row, int column, const QString &value, const Format &format)
{ {
Q_D(Worksheet); Q_D(Worksheet);
int error = 0;
QString content = value;
if (d->checkDimensions(row, column)) if (d->checkDimensions(row, column))
return -1; return -1;
if (value.size() > XLSX_STRING_MAX) { RichString rs;
content = value.left(XLSX_STRING_MAX); if (Qt::mightBeRichText(value) && d->workbook->isHtmlToRichStringEnabled())
error = -2; rs.setHtml(value);
} else
rs.addFragment(value, Format());
d->sharedStrings()->addSharedString(content); return writeString(row, column, rs, format);
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;
} }
/*! /*!

Loading…
Cancel
Save