Browse Source

Make RichString public

master
Debao Zhang 11 years ago
parent
commit
77d727748d
  1. BIN
      examples/xlsx/richtext/doc/images/richtext.png
  2. 11
      examples/xlsx/richtext/doc/src/richtext.qdoc
  3. 35
      examples/xlsx/richtext/main.cpp
  4. 9
      examples/xlsx/richtext/richtext.pro
  5. 1
      examples/xlsx/xlsx.pro
  6. 3
      src/xlsx/qtxlsx.pri
  7. 2
      src/xlsx/xlsxcell_p.h
  8. 4
      src/xlsx/xlsxformat.h
  9. 106
      src/xlsx/xlsxrichstring.cpp
  10. 81
      src/xlsx/xlsxrichstring.h
  11. 49
      src/xlsx/xlsxrichstring_p.h
  12. 2
      src/xlsx/xlsxsharedstrings.cpp
  13. 2
      src/xlsx/xlsxsharedstrings_p.h
  14. 42
      src/xlsx/xlsxworksheet.cpp
  15. 3
      src/xlsx/xlsxworksheet.h
  16. 2
      tests/auto/richstring/tst_richstringtest.cpp
  17. 2
      tests/auto/sharedstrings/tst_sharedstringstest.cpp
  18. 2
      tests/auto/worksheet/tst_worksheet.cpp

BIN
examples/xlsx/richtext/doc/images/richtext.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

11
examples/xlsx/richtext/doc/src/richtext.qdoc

@ -0,0 +1,11 @@
/*!
\example richtext
\title Rich Text Example
\brief This is a simplest Qt Xlsx example.
\ingroup qtxlsx-examples
This example demonstrates how to create a new
.xlsx file containing some basic data and calculations
with Qt Xlsx Library. So lets see how this is achieved.
\image richtext.png
*/

35
examples/xlsx/richtext/main.cpp

@ -0,0 +1,35 @@
#include <QtCore>
#include "xlsxdocument.h"
#include "xlsxrichstring.h"
#include "xlsxformat.h"
int main()
{
//![0]
QXlsx::Document xlsx;
//![0]
//![1]
QXlsx::Format blue;
blue.setFontColor(Qt::blue);
QXlsx::Format red;
red.setFontColor(Qt::red);
red.setFontSize(15);
QXlsx::Format bold;
bold.setFontBold(true);
QXlsx::RichString rich;
rich.addFragment("Hello ", blue);
rich.addFragment("Qt ", red);
rich.addFragment("Xlsx", bold);
xlsx.write("B2", rich);
//![1]
//![2]
xlsx.saveAs("Test1.xlsx");
//![2]
QXlsx::Document("Test1.xlsx");
xlsx.saveAs("Test2.xlsx");
return 0;
}

9
examples/xlsx/richtext/richtext.pro

@ -0,0 +1,9 @@
TARGET = hello
#include(../../../src/xlsx/qtxlsx.pri)
QT+=xlsx
CONFIG += console
CONFIG -= app_bundle
SOURCES += main.cpp

1
examples/xlsx/xlsx.pro

@ -10,5 +10,6 @@ SUBDIRS = hello \
datavalidation \
definename \
formulas \
richtext \
demo

3
src/xlsx/qtxlsx.pri

@ -32,7 +32,8 @@ HEADERS += $$PWD/xlsxdocpropscore_p.h \
$$PWD/xlsxdatavalidation.h \
$$PWD/xlsxdatavalidation_p.h \
$$PWD/xlsxcellrange.h \
$$PWD/xlsxrichstring_p.h
$$PWD/xlsxrichstring_p.h \
$$PWD/xlsxrichstring.h
SOURCES += $$PWD/xlsxdocpropscore.cpp \
$$PWD/xlsxdocpropsapp.cpp \

2
src/xlsx/xlsxcell_p.h

@ -28,7 +28,7 @@
#include "xlsxglobal.h"
#include "xlsxcell.h"
#include "xlsxcellrange.h"
#include "xlsxrichstring_p.h"
#include "xlsxrichstring.h"
#include <QList>
#include <QSharedPointer>

4
src/xlsx/xlsxformat.h

@ -39,7 +39,7 @@ QT_BEGIN_NAMESPACE_XLSX
class Styles;
class Worksheet;
class WorksheetPrivate;
class RichString;
class RichStringPrivate;
class SharedStrings;
class FormatPrivate;
@ -234,7 +234,7 @@ private:
friend class Styles;
friend class Worksheet;
friend class WorksheetPrivate;
friend class RichString;
friend class RichStringPrivate;
friend class SharedStrings;
friend class ::FormatTest;

106
src/xlsx/xlsxrichstring.cpp

@ -22,22 +22,72 @@
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "xlsxrichstring.h"
#include "xlsxrichstring_p.h"
#include "xlsxformat_p.h"
QT_BEGIN_NAMESPACE_XLSX
RichStringPrivate::RichStringPrivate()
:_dirty(true)
{
}
RichStringPrivate::RichStringPrivate(const RichStringPrivate &other)
:QSharedData(other), fragmentTexts(other.fragmentTexts)
,fragmentFormats(other.fragmentFormats)
, _idKey(other.idKey()), _dirty(other._dirty)
{
}
RichStringPrivate::~RichStringPrivate()
{
}
/*!
\class RichString
*/
RichString::RichString()
:m_dirty(true)
:d(new RichStringPrivate)
{
}
RichString::RichString(const QString text)
:m_dirty(true)
:d(new RichStringPrivate)
{
addFragment(text, Format());
}
RichString::RichString(const RichString &other)
:d(other.d)
{
}
RichString::~RichString()
{
}
RichString &RichString::operator =(const RichString &other)
{
this->d = other.d;
return *this;
}
/*!
Returns the rich string as a QVariant
*/
RichString::operator QVariant() const
{
return QVariant(qMetaTypeId<RichString>(), this);
}
bool RichString::isRichString() const
{
if (fragmentCount() > 1) //Is this enough??
@ -47,29 +97,29 @@ bool RichString::isRichString() const
bool RichString::isEmtpy() const
{
return m_fragmentTexts.size() == 0;
return d->fragmentTexts.size() == 0;
}
QString RichString::toPlainString() const
{
if (isEmtpy())
return QString();
if (m_fragmentTexts.size() == 1)
return m_fragmentTexts[0];
if (d->fragmentTexts.size() == 1)
return d->fragmentTexts[0];
return m_fragmentTexts.join(QString());
return d->fragmentTexts.join(QString());
}
int RichString::fragmentCount() const
{
return m_fragmentTexts.size();
return d->fragmentTexts.size();
}
void RichString::addFragment(const QString &text, const Format &format)
{
m_fragmentTexts.append(text);
m_fragmentFormats.append(format);
m_dirty = true;
d->fragmentTexts.append(text);
d->fragmentFormats.append(format);
d->_dirty = true;
}
QString RichString::fragmentText(int index) const
@ -77,7 +127,7 @@ QString RichString::fragmentText(int index) const
if (index < 0 || index >= fragmentCount())
return QString();
return m_fragmentTexts[index];
return d->fragmentTexts[index];
}
Format RichString::fragmentFormat(int index) const
@ -85,35 +135,35 @@ Format RichString::fragmentFormat(int index) const
if (index < 0 || index >= fragmentCount())
return Format();
return m_fragmentFormats[index];
return d->fragmentFormats[index];
}
/*!
* \internal
*/
QByteArray RichString::idKey() const
QByteArray RichStringPrivate::idKey() const
{
if (m_dirty) {
RichString *rs = const_cast<RichString *>(this);
if (_dirty) {
RichStringPrivate *rs = const_cast<RichStringPrivate *>(this);
QByteArray bytes;
if (!isRichString()) {
bytes = toPlainString().toUtf8();
if (fragmentTexts.size() == 1) {
bytes = fragmentTexts[0].toUtf8();
} else {
//Generate a hash value base on QByteArray ?
bytes.append("@@QtXlsxRichString=");
for (int i=0; i<fragmentCount(); ++i) {
for (int i=0; i<fragmentTexts.size(); ++i) {
bytes.append("@Text");
bytes.append(m_fragmentTexts[i].toUtf8());
bytes.append(fragmentTexts[i].toUtf8());
bytes.append("@Format");
if (m_fragmentFormats[i].hasFontData())
bytes.append(m_fragmentFormats[i].fontKey());
if (fragmentFormats[i].hasFontData())
bytes.append(fragmentFormats[i].fontKey());
}
}
rs->m_idKey = bytes;
rs->m_dirty = false;
rs->_idKey = bytes;
rs->_dirty = false;
}
return m_idKey;
return _idKey;
}
bool operator==(const RichString &rs1, const RichString &rs2)
@ -121,7 +171,7 @@ bool operator==(const RichString &rs1, const RichString &rs2)
if (rs1.fragmentCount() != rs2.fragmentCount())
return false;
return rs1.idKey() == rs2.idKey();
return rs1.d->idKey() == rs2.d->idKey();
}
bool operator!=(const RichString &rs1, const RichString &rs2)
@ -129,7 +179,7 @@ bool operator!=(const RichString &rs1, const RichString &rs2)
if (rs1.fragmentCount() != rs2.fragmentCount())
return true;
return rs1.idKey() != rs2.idKey();
return rs1.d->idKey() != rs2.d->idKey();
}
/*!
@ -137,7 +187,7 @@ bool operator!=(const RichString &rs1, const RichString &rs2)
*/
bool operator<(const RichString &rs1, const RichString &rs2)
{
return rs1.idKey() < rs2.idKey();
return rs1.d->idKey() < rs2.d->idKey();
}
bool operator ==(const RichString &rs1, const QString &rs2)
@ -168,7 +218,7 @@ bool operator !=(const QString &rs1, const RichString &rs2)
uint qHash(const RichString &rs, uint seed) Q_DECL_NOTHROW
{
return qHash(rs.idKey(), seed);
return qHash(rs.d->idKey(), seed);
}
QT_END_NAMESPACE_XLSX

81
src/xlsx/xlsxrichstring.h

@ -0,0 +1,81 @@
/****************************************************************************
** Copyright (c) 2013 Debao Zhang <hello@debao.me>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#ifndef XLSXRICHSTRING_H
#define XLSXRICHSTRING_H
#include "xlsxglobal.h"
#include "xlsxformat.h"
#include <QVariant>
#include <QStringList>
#include <QSharedDataPointer>
QT_BEGIN_NAMESPACE_XLSX
class RichStringPrivate;
class RichString;
// qHash is a friend, but we can't use default arguments for friends (§8.3.6.4)
Q_XLSX_EXPORT uint qHash(const RichString &rs, uint seed = 0) Q_DECL_NOTHROW;
class Q_XLSX_EXPORT RichString
{
public:
RichString();
explicit RichString(const QString text);
RichString(const RichString &other);
~RichString();
bool isRichString() const;
bool isEmtpy() const;
QString toPlainString() const;
int fragmentCount() const;
void addFragment(const QString &text, const Format &format);
QString fragmentText(int index) const;
Format fragmentFormat(int index) const;
operator QVariant() const;
RichString &operator=(const RichString &other);
private:
friend Q_XLSX_EXPORT uint qHash(const RichString &rs, uint seed) Q_DECL_NOTHROW;
friend Q_XLSX_EXPORT bool operator==(const RichString &rs1, const RichString &rs2);
friend Q_XLSX_EXPORT bool operator!=(const RichString &rs1, const RichString &rs2);
friend Q_XLSX_EXPORT bool operator<(const RichString &rs1, const RichString &rs2);
QSharedDataPointer<RichStringPrivate> d;
};
Q_XLSX_EXPORT bool operator==(const RichString &rs1, const RichString &rs2);
Q_XLSX_EXPORT bool operator!=(const RichString &rs1, const RichString &rs2);
Q_XLSX_EXPORT bool operator<(const RichString &rs1, const RichString &rs2);
Q_XLSX_EXPORT bool operator==(const RichString &rs1, const QString &rs2);
Q_XLSX_EXPORT bool operator==(const QString &rs1, const RichString &rs2);
Q_XLSX_EXPORT bool operator!=(const RichString &rs1, const QString &rs2);
Q_XLSX_EXPORT bool operator!=(const QString &rs1, const RichString &rs2);
QT_END_NAMESPACE_XLSX
Q_DECLARE_METATYPE(QXlsx::RichString)
#endif // XLSXRICHSTRING_H

49
src/xlsx/xlsxrichstring_p.h

@ -25,56 +25,25 @@
#ifndef XLSXRICHSTRING_P_H
#define XLSXRICHSTRING_P_H
#include "xlsxglobal.h"
#include "xlsxformat.h"
#include <QStringList>
#include <QSharedPointer>
#include "xlsxrichstring.h"
QT_BEGIN_NAMESPACE_XLSX
class RichString;
// qHash is a friend, but we can't use default arguments for friends (§8.3.6.4)
XLSX_AUTOTEST_EXPORT uint qHash(const RichString &rs, uint seed = 0) Q_DECL_NOTHROW;
class XLSX_AUTOTEST_EXPORT RichString
class RichStringPrivate : public QSharedData
{
public:
RichString();
explicit RichString(const QString text);
bool isRichString() const;
bool isEmtpy() const;
QString toPlainString() const;
int fragmentCount() const;
void addFragment(const QString &text, const Format &format);
QString fragmentText(int index) const;
Format fragmentFormat(int index) const;
private:
friend XLSX_AUTOTEST_EXPORT uint qHash(const RichString &rs, uint seed) Q_DECL_NOTHROW;
friend XLSX_AUTOTEST_EXPORT bool operator==(const RichString &rs1, const RichString &rs2);
friend XLSX_AUTOTEST_EXPORT bool operator!=(const RichString &rs1, const RichString &rs2);
friend XLSX_AUTOTEST_EXPORT bool operator<(const RichString &rs1, const RichString &rs2);
RichStringPrivate();
RichStringPrivate(const RichStringPrivate &other);
~RichStringPrivate();
QByteArray idKey() const;
QStringList m_fragmentTexts;
QList<Format> m_fragmentFormats;
QByteArray m_idKey;
bool m_dirty;
QStringList fragmentTexts;
QList<Format> fragmentFormats;
QByteArray _idKey;
bool _dirty;
};
XLSX_AUTOTEST_EXPORT bool operator==(const RichString &rs1, const RichString &rs2);
XLSX_AUTOTEST_EXPORT bool operator!=(const RichString &rs1, const RichString &rs2);
XLSX_AUTOTEST_EXPORT bool operator<(const RichString &rs1, const RichString &rs2);
XLSX_AUTOTEST_EXPORT bool operator==(const RichString &rs1, const QString &rs2);
XLSX_AUTOTEST_EXPORT bool operator==(const QString &rs1, const RichString &rs2);
XLSX_AUTOTEST_EXPORT bool operator!=(const RichString &rs1, const QString &rs2);
XLSX_AUTOTEST_EXPORT bool operator!=(const QString &rs1, const RichString &rs2);
QT_END_NAMESPACE_XLSX
Q_DECLARE_METATYPE(QXlsx::RichString)
#endif // XLSXRICHSTRING_P_H

2
src/xlsx/xlsxsharedstrings.cpp

@ -22,7 +22,7 @@
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "xlsxrichstring_p.h"
#include "xlsxrichstring.h"
#include "xlsxsharedstrings_p.h"
#include "xlsxxmlwriter_p.h"
#include "xlsxxmlreader_p.h"

2
src/xlsx/xlsxsharedstrings_p.h

@ -26,7 +26,7 @@
#define XLSXSHAREDSTRINGS_H
#include "xlsxglobal.h"
#include "xlsxrichstring_p.h"
#include "xlsxrichstring.h"
#include <QHash>
#include <QStringList>
#include <QSharedPointer>

42
src/xlsx/xlsxworksheet.cpp

@ -22,7 +22,7 @@
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "xlsxrichstring_p.h"
#include "xlsxrichstring.h"
#include "xlsxworksheet.h"
#include "xlsxworksheet_p.h"
#include "xlsxworkbook.h"
@ -441,6 +441,8 @@ int Worksheet::write(int row, int column, const QVariant &value, const Format &f
if (value.isNull()) { //blank
ret = writeBlank(row, column, format);
} else if (value.userType() == qMetaTypeId<RichString>()) {
ret = writeString(row, column, value.value<RichString>(), format);
} else if (value.userType() == QMetaType::Bool) { //Bool
ret = writeBool(row,column, value.toBool(), format);
} else if (value.toDateTime().isValid()) { //DateTime
@ -542,6 +544,44 @@ Format WorksheetPrivate::cellFormat(int row, int col) const
return cellTable[row][col]->format();
}
/*!
\overload
*/
int Worksheet::writeString(const QString &row_column, const RichString &value, const Format &format)
{
//convert the "A1" notation to row/column notation
QPoint pos = xl_cell_to_rowcol(row_column);
if (pos == QPoint(-1, -1))
return -1;
return writeString(pos.x(), pos.y(), value, format);
}
/*!
Write string \a value to the cell (\a row, \a column) with the \a format
*/
int Worksheet::writeString(int row, int column, const RichString &value, const Format &format)
{
Q_D(Worksheet);
int error = 0;
// QString content = value.toPlainString();
if (d->checkDimensions(row, column))
return -1;
// if (content.size() > d->xls_strmax) {
// content = content.left(d->xls_strmax);
// error = -2;
// }
d->sharedStrings()->addSharedString(value);
Format fmt = format.isValid() ? format : d->cellFormat(row, column);
d->workbook->styles()->addFormat(fmt);
QSharedPointer<Cell> cell = QSharedPointer<Cell>(new Cell(QString(), Cell::String, fmt, this));
cell->d_ptr->richString = value;
d->cellTable[row][column] = cell;
return error;
}
/*!
\overload
*/

3
src/xlsx/xlsxworksheet.h

@ -47,6 +47,7 @@ class Drawing;
class DataValidation;
class CellRange;
struct XlsxImageData;
class RichString;
class WorksheetPrivate;
class Q_XLSX_EXPORT Worksheet
@ -59,6 +60,8 @@ public:
QVariant read(int row, int column) const;
int writeString(const QString &row_column, const QString &value, const Format &format=Format());
int writeString(int row, int column, const QString &value, const Format &format=Format());
int writeString(const QString &row_column, const RichString &value, const Format &format=Format());
int writeString(int row, int column, const RichString &value, const Format &format=Format());
int writeInlineString(const QString &row_column, const QString &value, const Format &format=Format());
int writeInlineString(int row, int column, const QString &value, const Format &format=Format());
int writeNumeric(const QString &row_column, double value, const Format &format=Format());

2
tests/auto/richstring/tst_richstringtest.cpp

@ -1,4 +1,4 @@
#include "private/xlsxrichstring_p.h"
#include "xlsxrichstring.h"
#include <QtTest>
#include <QDebug>

2
tests/auto/sharedstrings/tst_sharedstringstest.cpp

@ -1,5 +1,5 @@
#include "private/xlsxsharedstrings_p.h"
#include "private/xlsxrichstring_p.h"
#include "xlsxrichstring.h"
#include "xlsxformat.h"
#include <QString>
#include <QtTest>

2
tests/auto/worksheet/tst_worksheet.cpp

@ -8,7 +8,7 @@
#include "private/xlsxworksheet_p.h"
#include "private/xlsxxmlreader_p.h"
#include "private/xlsxsharedstrings_p.h"
#include "private/xlsxrichstring_p.h"
#include "xlsxrichstring.h"
class WorksheetTest : public QObject
{

Loading…
Cancel
Save