Qt 4.8
Public Functions | Public Variables | Static Public Variables | List of all members
QDeclarativeStyledTextPrivate Class Reference

Public Functions

void parse ()
 
QPair< QStringRef, QStringRefparseAttribute (const QChar *&ch, const QString &textIn)
 
bool parseCloseTag (const QChar *&ch, const QString &textIn)
 
void parseEntity (const QChar *&ch, const QString &textIn, QString &textOut)
 
bool parseFontAttributes (const QChar *&ch, const QString &textIn, QTextCharFormat &format)
 
bool parseTag (const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format)
 
QStringRef parseValue (const QChar *&ch, const QString &textIn)
 
 QDeclarativeStyledTextPrivate (const QString &t, QTextLayout &l)
 
void skipSpace (const QChar *&ch)
 

Public Variables

QFont baseFont
 
QTextLayoutlayout
 
QString text
 

Static Public Variables

static const QChar ampersand
 
static const QChar doubleQuote
 
static const QChar equals
 
static const QChar greaterThan
 
static const QChar lessThan
 
static const QChar singleQuote
 
static const QChar slash
 

Detailed Description

Definition at line 63 of file qdeclarativestyledtext.cpp.

Constructors and Destructors

◆ QDeclarativeStyledTextPrivate()

QDeclarativeStyledTextPrivate::QDeclarativeStyledTextPrivate ( const QString t,
QTextLayout l 
)
inline

Definition at line 66 of file qdeclarativestyledtext.cpp.

66 : text(t), layout(l), baseFont(layout.font()) {}
QFont font() const
Returns the current font that is used for the layout, or a default font if none is set...

Functions

◆ parse()

void QDeclarativeStyledTextPrivate::parse ( )

Definition at line 120 of file qdeclarativestyledtext.cpp.

Referenced by QDeclarativeStyledText::parse(), and QDeclarativeStyledTextPrivate().

121 {
123  QStack<QTextCharFormat> formatStack;
124 
125  QString drawText;
126  drawText.reserve(text.count());
127 
128  int textStart = 0;
129  int textLength = 0;
130  int rangeStart = 0;
131  const QChar *ch = text.constData();
132  while (!ch->isNull()) {
133  if (*ch == lessThan) {
134  if (textLength)
135  drawText.append(QStringRef(&text, textStart, textLength));
136  if (rangeStart != drawText.length() && formatStack.count()) {
137  QTextLayout::FormatRange formatRange;
138  formatRange.format = formatStack.top();
139  formatRange.start = rangeStart;
140  formatRange.length = drawText.length() - rangeStart;
141  ranges.append(formatRange);
142  }
143  rangeStart = drawText.length();
144  ++ch;
145  if (*ch == slash) {
146  ++ch;
147  if (parseCloseTag(ch, text)) {
148  if (formatStack.count())
149  formatStack.pop();
150  }
151  } else {
153  if (formatStack.count())
154  format = formatStack.top();
155  if (parseTag(ch, text, drawText, format))
156  formatStack.push(format);
157  }
158  textStart = ch - text.constData() + 1;
159  textLength = 0;
160  } else if (*ch == ampersand) {
161  ++ch;
162  drawText.append(QStringRef(&text, textStart, textLength));
163  parseEntity(ch, text, drawText);
164  textStart = ch - text.constData() + 1;
165  textLength = 0;
166  } else {
167  ++textLength;
168  }
169  if (!ch->isNull())
170  ++ch;
171  }
172  if (textLength)
173  drawText.append(QStringRef(&text, textStart, textLength));
174  if (rangeStart != drawText.length() && formatStack.count()) {
175  QTextLayout::FormatRange formatRange;
176  formatRange.format = formatStack.top();
177  formatRange.start = rangeStart;
178  formatRange.length = drawText.length() - rangeStart;
179  ranges.append(formatRange);
180  }
181 
182  layout.setText(drawText);
184 }
void setAdditionalFormats(const QList< FormatRange > &overrides)
Sets the additional formats supported by the text layout to formatList.
void setText(const QString &string)
Sets the layout&#39;s text to the given string.
The QTextLayout::FormatRange structure is used to apply extra formatting information for a specified ...
Definition: qtextlayout.h:128
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
int start
Specifies the beginning of the format range within the text layout&#39;s text.
Definition: qtextlayout.h:129
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool parseTag(const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format)
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
bool parseCloseTag(const QChar *&ch, const QString &textIn)
T pop()
Removes the top item from the stack and returns it.
Definition: qstack.h:67
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
void parseEntity(const QChar *&ch, const QString &textIn, QString &textOut)
void push(const T &t)
Adds element t to the top of the stack.
Definition: qstack.h:60
int count() const
Definition: qstring.h:103
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
QString & append(QChar c)
Definition: qstring.cpp:1777
int length
Specifies the numer of characters the format range spans.
Definition: qtextlayout.h:130
QTextCharFormat format
Specifies the format to apply.
Definition: qtextlayout.h:131
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
T & top()
Returns a reference to the stack&#39;s top item.
Definition: qstack.h:72

◆ parseAttribute()

QPair< QStringRef, QStringRef > QDeclarativeStyledTextPrivate::parseAttribute ( const QChar *&  ch,
const QString textIn 
)

Definition at line 299 of file qdeclarativestyledtext.cpp.

Referenced by QDeclarativeStyledTextPrivate().

300 {
301  skipSpace(ch);
302 
303  int attrStart = ch - textIn.constData();
304  int attrLength = 0;
305  while (!ch->isNull()) {
306  if (*ch == greaterThan) {
307  break;
308  } else if (*ch == equals) {
309  ++ch;
310  if (*ch != singleQuote && *ch != doubleQuote) {
311  while (*ch != greaterThan && !ch->isNull())
312  ++ch;
313  break;
314  }
315  ++ch;
316  if (!attrLength)
317  break;
318  QStringRef attr(&textIn, attrStart, attrLength);
319  QStringRef val = parseValue(ch, textIn);
320  if (!val.isEmpty())
321  return QPair<QStringRef,QStringRef>(attr,val);
322  break;
323  } else {
324  ++attrLength;
325  }
326  ++ch;
327  }
328 
330 }
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
bool isEmpty() const
Returns true if the string reference has no characters; otherwise returns false.
Definition: qstring.h:1169
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
QStringRef parseValue(const QChar *&ch, const QString &textIn)
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ parseCloseTag()

bool QDeclarativeStyledTextPrivate::parseCloseTag ( const QChar *&  ch,
const QString textIn 
)

Definition at line 224 of file qdeclarativestyledtext.cpp.

Referenced by QDeclarativeStyledTextPrivate().

225 {
226  skipSpace(ch);
227 
228  int tagStart = ch - textIn.constData();
229  int tagLength = 0;
230  while (!ch->isNull()) {
231  if (*ch == greaterThan) {
232  QStringRef tag(&textIn, tagStart, tagLength);
233  const QChar char0 = tag.at(0);
234  if (char0 == QLatin1Char('b')) {
235  if (tagLength == 1)
236  return true;
237  else if (tag.at(1) == QLatin1Char('r') && tagLength == 2)
238  return true;
239  } else if (char0 == QLatin1Char('i')) {
240  if (tagLength == 1)
241  return true;
242  } else if (tag == QLatin1String("font")) {
243  return true;
244  }
245  return false;
246  } else if (!ch->isSpace()){
247  tagLength++;
248  }
249  ++ch;
250  }
251 
252  return false;
253 }
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
bool isSpace() const
Returns true if the character is a separator character (Separator_* categories); otherwise returns fa...
Definition: qchar.cpp:609
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ parseEntity()

void QDeclarativeStyledTextPrivate::parseEntity ( const QChar *&  ch,
const QString textIn,
QString textOut 
)

Definition at line 255 of file qdeclarativestyledtext.cpp.

Referenced by QDeclarativeStyledTextPrivate().

256 {
257  int entityStart = ch - textIn.constData();
258  int entityLength = 0;
259  while (!ch->isNull()) {
260  if (*ch == QLatin1Char(';')) {
261  QStringRef entity(&textIn, entityStart, entityLength);
262  if (entity == QLatin1String("gt"))
263  textOut += QChar(62);
264  else if (entity == QLatin1String("lt"))
265  textOut += QChar(60);
266  else if (entity == QLatin1String("amp"))
267  textOut += QChar(38);
268  return;
269  }
270  ++entityLength;
271  ++ch;
272  }
273 }
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ parseFontAttributes()

bool QDeclarativeStyledTextPrivate::parseFontAttributes ( const QChar *&  ch,
const QString textIn,
QTextCharFormat format 
)

Definition at line 275 of file qdeclarativestyledtext.cpp.

Referenced by QDeclarativeStyledTextPrivate().

276 {
277  bool valid = false;
279  do {
280  attr = parseAttribute(ch, textIn);
281  if (attr.first == QLatin1String("color")) {
282  valid = true;
283  format.setForeground(QColor(attr.second.toString()));
284  } else if (attr.first == QLatin1String("size")) {
285  valid = true;
286  int size = attr.second.toString().toInt();
287  if (attr.second.at(0) == QLatin1Char('-') || attr.second.at(0) == QLatin1Char('+'))
288  size += 3;
289  if (size >= 1 && size <= 7) {
290  static const qreal scaling[] = { 0.7, 0.8, 1.0, 1.2, 1.5, 2.0, 2.4 };
291  format.setFontPointSize(baseFont.pointSize() * scaling[size-1]);
292  }
293  }
294  } while (!ch->isNull() && !attr.first.isEmpty());
295 
296  return valid;
297 }
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
QString toString() const
Returns a copy of the string reference as a QString object.
Definition: qstring.cpp:8653
double qreal
Definition: qglobal.h:1193
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
QPair< QStringRef, QStringRef > parseAttribute(const QChar *&ch, const QString &textIn)
T1 first
Definition: qpair.h:65
T2 second
Definition: qpair.h:66
bool isEmpty() const
Returns true if the string reference has no characters; otherwise returns false.
Definition: qstring.h:1169
void setFontPointSize(qreal size)
Sets the text format&#39;s font size.
Definition: qtextformat.h:406
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
void setForeground(const QBrush &brush)
Sets the foreground brush to the specified brush.
Definition: qtextformat.h:350
int pointSize() const
Returns the point size of the font.
Definition: qfont.cpp:981
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const QChar at(int i) const
Returns the character at the given index position in the string reference.
Definition: qstring.h:1174

◆ parseTag()

bool QDeclarativeStyledTextPrivate::parseTag ( const QChar *&  ch,
const QString textIn,
QString textOut,
QTextCharFormat format 
)

Definition at line 186 of file qdeclarativestyledtext.cpp.

Referenced by QDeclarativeStyledTextPrivate().

187 {
188  skipSpace(ch);
189 
190  int tagStart = ch - textIn.constData();
191  int tagLength = 0;
192  while (!ch->isNull()) {
193  if (*ch == greaterThan) {
194  QStringRef tag(&textIn, tagStart, tagLength);
195  const QChar char0 = tag.at(0);
196  if (char0 == QLatin1Char('b')) {
197  if (tagLength == 1)
198  format.setFontWeight(QFont::Bold);
199  else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) {
201  return false;
202  }
203  } else if (char0 == QLatin1Char('i')) {
204  if (tagLength == 1)
205  format.setFontItalic(true);
206  }
207  return true;
208  } else if (ch->isSpace()) {
209  // may have params.
210  QStringRef tag(&textIn, tagStart, tagLength);
211  if (tag == QLatin1String("font"))
212  return parseFontAttributes(ch, textIn, format);
213  if (*ch == greaterThan || ch->isNull())
214  continue;
215  } else if (*ch != slash){
216  tagLength++;
217  }
218  ++ch;
219  }
220 
221  return false;
222 }
void setFontItalic(bool italic)
If italic is true, sets the text format&#39;s font to be italic; otherwise the font will be non-italic...
Definition: qtextformat.h:415
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
bool isSpace() const
Returns true if the character is a separator character (Separator_* categories); otherwise returns fa...
Definition: qchar.cpp:609
void setFontWeight(int weight)
Sets the text format&#39;s font weight to weight.
Definition: qtextformat.h:411
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
QString & append(QChar c)
Definition: qstring.cpp:1777
bool parseFontAttributes(const QChar *&ch, const QString &textIn, QTextCharFormat &format)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ parseValue()

QStringRef QDeclarativeStyledTextPrivate::parseValue ( const QChar *&  ch,
const QString textIn 
)

Definition at line 332 of file qdeclarativestyledtext.cpp.

Referenced by QDeclarativeStyledTextPrivate().

333 {
334  int valStart = ch - textIn.constData();
335  int valLength = 0;
336  while (*ch != singleQuote && *ch != doubleQuote && !ch->isNull()) {
337  ++valLength;
338  ++ch;
339  }
340  if (ch->isNull())
341  return QStringRef();
342  ++ch; // skip quote
343 
344  return QStringRef(&textIn, valStart, valLength);
345 }
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712

◆ skipSpace()

void QDeclarativeStyledTextPrivate::skipSpace ( const QChar *&  ch)
inline

Definition at line 76 of file qdeclarativestyledtext.cpp.

76  {
77  while (ch->isSpace() && !ch->isNull())
78  ++ch;
79  }
bool isNull() const
Returns true if the character is the Unicode character 0x0000 (&#39;\0&#39;); otherwise returns false...
Definition: qchar.h:262
bool isSpace() const
Returns true if the character is a separator character (Separator_* categories); otherwise returns fa...
Definition: qchar.cpp:609

Properties

◆ ampersand

const QChar QDeclarativeStyledTextPrivate::ampersand
static

Definition at line 91 of file qdeclarativestyledtext.cpp.

◆ baseFont

QFont QDeclarativeStyledTextPrivate::baseFont

Definition at line 83 of file qdeclarativestyledtext.cpp.

◆ doubleQuote

const QChar QDeclarativeStyledTextPrivate::doubleQuote
static

Definition at line 89 of file qdeclarativestyledtext.cpp.

◆ equals

const QChar QDeclarativeStyledTextPrivate::equals
static

Definition at line 87 of file qdeclarativestyledtext.cpp.

◆ greaterThan

const QChar QDeclarativeStyledTextPrivate::greaterThan
static

Definition at line 86 of file qdeclarativestyledtext.cpp.

◆ layout

QTextLayout& QDeclarativeStyledTextPrivate::layout

Definition at line 82 of file qdeclarativestyledtext.cpp.

◆ lessThan

const QChar QDeclarativeStyledTextPrivate::lessThan
static

Definition at line 85 of file qdeclarativestyledtext.cpp.

◆ singleQuote

const QChar QDeclarativeStyledTextPrivate::singleQuote
static

Definition at line 88 of file qdeclarativestyledtext.cpp.

◆ slash

const QChar QDeclarativeStyledTextPrivate::slash
static

Definition at line 90 of file qdeclarativestyledtext.cpp.

◆ text

QString QDeclarativeStyledTextPrivate::text

Definition at line 81 of file qdeclarativestyledtext.cpp.


The documentation for this class was generated from the following file: