Qt 4.8
Classes | Functions
qtextformat.cpp File Reference
#include "qtextformat.h"
#include "qtextformat_p.h"
#include <qvariant.h>
#include <qdatastream.h>
#include <qdebug.h>
#include <qmap.h>
#include <qhash.h>

Go to the source code of this file.

Classes

class  QTextFormatPrivate
 
struct  QTextFormatPrivate::Property
 

Functions

static int getHash (const QTextFormatPrivate *d, int format)
 
static uint hash (float d)
 
static uint hash (const QColor &color)
 
static uint hash (const QPen &pen)
 
static uint hash (const QBrush &brush)
 
QDataStreamoperator<< (QDataStream &stream, const QTextLength &length)
 
Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &stream, const QTextFormat &fmt)
 
QDataStreamoperator>> (QDataStream &stream, QTextLength &length)
 
Q_GUI_EXPORT QDataStreamoperator>> (QDataStream &stream, QTextFormat &fmt)
 
static uint variantHash (const QVariant &variant)
 

Function Documentation

◆ getHash()

static int getHash ( const QTextFormatPrivate d,
int  format 
)
inlinestatic

Definition at line 347 of file qtextformat.cpp.

Referenced by QTextFormatCollection::hasFormatCached(), and QTextFormatCollection::indexForFormat().

348 {
349  return (d ? d->hash() : 0) + format;
350 }
uint hash() const

◆ hash() [1/4]

static uint hash ( float  d)
inlinestatic

Definition at line 296 of file qtextformat.cpp.

Referenced by QTextFormatCollection::hasFormatCached(), and QTextFormatCollection::indexForFormat().

297 {
298 #ifdef Q_CC_GNU
299  // this is a GCC extension and isn't guaranteed to work in other compilers
300  // the reinterpret_cast below generates a strict-aliasing warning with GCC
301  union { float f; uint u; } cvt;
302  cvt.f = d;
303  return cvt.u;
304 #else
305  return reinterpret_cast<uint&>(d);
306 #endif
307 }
double d
Definition: qnumeric_p.h:62
quint16 u
unsigned int uint
Definition: qglobal.h:996

◆ hash() [2/4]

static uint hash ( const QColor color)
inlinestatic

Definition at line 309 of file qtextformat.cpp.

310 {
311  return (color.isValid()) ? color.rgba() : 0x234109;
312 }
bool isValid() const
Returns true if the color is valid; otherwise returns false.
Definition: qcolor.h:295
QRgb rgba() const
Returns the RGB value of the color, including its alpha.
Definition: qcolor.cpp:1019

◆ hash() [3/4]

static uint hash ( const QPen pen)
inlinestatic

Definition at line 314 of file qtextformat.cpp.

315 {
316  return hash(pen.color()) + hash(pen.widthF());
317 }
QColor color() const
Returns the color of this pen&#39;s brush.
Definition: qpen.cpp:771
static uint hash(float d)
qreal widthF() const
Returns the pen width with floating point precision.
Definition: qpen.cpp:645

◆ hash() [4/4]

static uint hash ( const QBrush brush)
inlinestatic

Definition at line 319 of file qtextformat.cpp.

320 {
321  return hash(brush.color()) + (brush.style() << 3);
322 }
const QColor & color() const
Returns the brush color.
Definition: qbrush.h:183
static uint hash(float d)
Qt::BrushStyle style() const
Returns the brush style.
Definition: qbrush.h:182

◆ operator<<() [1/2]

QDataStream& operator<< ( QDataStream stream,
const QTextLength length 
)

Definition at line 179 of file qtextformat.cpp.

180 {
181  return stream << qint32(length.lengthType) << double(length.fixedValueOrPercentage);
182 }
int qint32
Definition: qglobal.h:937
Type lengthType
Definition: qtextformat.h:115
qreal fixedValueOrPercentage
Definition: qtextformat.h:116

◆ operator<<() [2/2]

Q_GUI_EXPORT QDataStream& operator<< ( QDataStream stream,
const QTextFormat fmt 
)

Definition at line 459 of file qtextformat.cpp.

460 {
461  stream << fmt.format_type << fmt.properties();
462  return stream;
463 }
QMap< int, QVariant > properties() const
Returns a map with all properties of this text format.
static FILE * stream
qint32 format_type
Definition: qtextformat.h:359

◆ operator>>() [1/2]

QDataStream& operator>> ( QDataStream stream,
QTextLength length 
)

Definition at line 184 of file qtextformat.cpp.

185 {
186  qint32 type;
187  double fixedValueOrPercentage;
188  stream >> type >> fixedValueOrPercentage;
189  length.fixedValueOrPercentage = fixedValueOrPercentage;
190  length.lengthType = QTextLength::Type(type);
191  return stream;
192 }
int type
Definition: qmetatype.cpp:239
int qint32
Definition: qglobal.h:937
Type
This enum describes the different types a length object can have.
Definition: qtextformat.h:87
static FILE * stream
Type lengthType
Definition: qtextformat.h:115
qreal fixedValueOrPercentage
Definition: qtextformat.h:116

◆ operator>>() [2/2]

Q_GUI_EXPORT QDataStream& operator>> ( QDataStream stream,
QTextFormat fmt 
)

Definition at line 465 of file qtextformat.cpp.

466 {
468  stream >> fmt.format_type >> properties;
469 
470  // QTextFormat's default constructor doesn't allocate the private structure, so
471  // we have to do this, in case fmt is a default constructed value.
472  if(!fmt.d)
473  fmt.d = new QTextFormatPrivate();
474 
476  it != properties.constEnd(); ++it)
477  fmt.d->insertProperty(it.key(), it.value());
478 
479  return stream;
480 }
#define it(className, varName)
static FILE * stream
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
qint32 format_type
Definition: qtextformat.h:359
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
QSharedDataPointer< QTextFormatPrivate > d
Definition: qtextformat.h:358
static const QCssKnownValue properties[NumProperties - 1]
Definition: qcssparser.cpp:67
void insertProperty(qint32 key, const QVariant &value)
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

◆ variantHash()

static uint variantHash ( const QVariant variant)
inlinestatic

Definition at line 324 of file qtextformat.cpp.

Referenced by QTextFormatPrivate::recalcHash().

325 {
326  // simple and fast hash functions to differentiate between type and value
327  switch (variant.userType()) { // sorted by occurrence frequency
328  case QVariant::String: return qHash(variant.toString());
329  case QVariant::Double: return hash(variant.toDouble());
330  case QVariant::Int: return 0x811890 + variant.toInt();
331  case QVariant::Brush:
332  return 0x01010101 + hash(qvariant_cast<QBrush>(variant));
333  case QVariant::Bool: return 0x371818 + variant.toBool();
334  case QVariant::Pen: return 0x02020202 + hash(qvariant_cast<QPen>(variant));
335  case QVariant::List:
336  return 0x8377 + qvariant_cast<QVariantList>(variant).count();
337  case QVariant::Color: return hash(qvariant_cast<QColor>(variant));
339  return 0x377 + hash(qvariant_cast<QTextLength>(variant).rawValue());
340  case QMetaType::Float: return hash(variant.toFloat());
341  case QVariant::Invalid: return 0;
342  default: break;
343  }
344  return qHash(variant.typeName());
345 }
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
static uint hash(float d)
const char * typeName() const
Returns the name of the type stored in the variant.
Definition: qvariant.cpp:1984
uint qHash(const QUrl &url)
Definition: qurl.h:285
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
float toFloat(bool *ok=0) const
Returns the variant as a float if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2725
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
const char * variant
T qvariant_cast(const QVariant &value)
Returns the given value converted to the template type T.
Definition: qvariant.h:571