qwt_text.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #include "qwt_text.h"
11 #include "qwt_painter.h"
12 #include "qwt_text_engine.h"
13 #include <qmap.h>
14 #include <qfont.h>
15 #include <qcolor.h>
16 #include <qpen.h>
17 #include <qbrush.h>
18 #include <qpainter.h>
19 #include <qapplication.h>
20 #include <qdesktopwidget.h>
21 #include <qmath.h>
22 
24 {
25 public:
26  static QwtTextEngineDict &dict();
27 
29 
31  const QwtTextEngine *textEngine( const QString &,
32  QwtText::TextFormat ) const;
33 
34 private:
37 
38  typedef QMap<int, QwtTextEngine *> EngineMap;
39 
40  inline const QwtTextEngine *engine( EngineMap::const_iterator &it ) const
41  {
42  return it.value();
43  }
44 
45  EngineMap d_map;
46 };
47 
49 {
50  static QwtTextEngineDict engineDict;
51  return engineDict;
52 }
53 
55 {
57 #ifndef QT_NO_RICHTEXT
58  d_map.insert( QwtText::RichText, new QwtRichTextEngine() );
59 #endif
60 }
61 
63 {
64  for ( EngineMap::const_iterator it = d_map.constBegin();
65  it != d_map.constEnd(); ++it )
66  {
67  const QwtTextEngine *textEngine = engine( it );
68  delete textEngine;
69  }
70 }
71 
72 const QwtTextEngine *QwtTextEngineDict::textEngine( const QString& text,
73  QwtText::TextFormat format ) const
74 {
75  if ( format == QwtText::AutoText )
76  {
77  for ( EngineMap::const_iterator it = d_map.begin();
78  it != d_map.end(); ++it )
79  {
80  if ( it.key() != QwtText::PlainText )
81  {
82  const QwtTextEngine *e = engine( it );
83  if ( e && e->mightRender( text ) )
84  return e;
85  }
86  }
87  }
88 
89  EngineMap::const_iterator it = d_map.find( format );
90  if ( it != d_map.end() )
91  {
92  const QwtTextEngine *e = engine( it );
93  if ( e )
94  return e;
95  }
96 
97  it = d_map.find( QwtText::PlainText );
98  return engine( it );
99 }
100 
103 {
104  if ( format == QwtText::AutoText )
105  return;
106 
107  if ( format == QwtText::PlainText && engine == NULL )
108  return;
109 
110  EngineMap::const_iterator it = d_map.constFind( format );
111  if ( it != d_map.constEnd() )
112  {
113  const QwtTextEngine *e = this->engine( it );
114  if ( e )
115  delete e;
116 
117  d_map.remove( format );
118  }
119 
120  if ( engine != NULL )
121  d_map.insert( format, engine );
122 }
123 
125  QwtText::TextFormat format ) const
126 {
127  const QwtTextEngine *e = NULL;
128 
129  EngineMap::const_iterator it = d_map.find( format );
130  if ( it != d_map.end() )
131  e = engine( it );
132 
133  return e;
134 }
135 
137 {
138 public:
140  renderFlags( Qt::AlignCenter ),
141  borderRadius( 0 ),
142  borderPen( Qt::NoPen ),
143  backgroundBrush( Qt::NoBrush ),
144  paintAttributes( 0 ),
145  layoutAttributes( 0 ),
146  textEngine( NULL )
147  {
148  }
149 
151  QString text;
152  QFont font;
153  QColor color;
154  double borderRadius;
155  QPen borderPen;
157 
160 
162 };
163 
165 {
166 public:
167  void invalidate()
168  {
169  textSize = QSizeF();
170  }
171 
172  QFont font;
173  QSizeF textSize;
174 };
175 
182 QwtText::QwtText( const QString &text, QwtText::TextFormat textFormat )
183 {
184  d_data = new PrivateData;
185  d_data->text = text;
186  d_data->textEngine = textEngine( text, textFormat );
187 
188  d_layoutCache = new LayoutCache;
189 }
190 
192 QwtText::QwtText( const QwtText &other )
193 {
194  d_data = new PrivateData;
195  *d_data = *other.d_data;
196 
197  d_layoutCache = new LayoutCache;
198  *d_layoutCache = *other.d_layoutCache;
199 }
200 
203 {
204  delete d_data;
205  delete d_layoutCache;
206 }
207 
210 {
211  *d_data = *other.d_data;
212  *d_layoutCache = *other.d_layoutCache;
213  return *this;
214 }
215 
217 bool QwtText::operator==( const QwtText &other ) const
218 {
219  return d_data->renderFlags == other.d_data->renderFlags &&
220  d_data->text == other.d_data->text &&
221  d_data->font == other.d_data->font &&
222  d_data->color == other.d_data->color &&
223  d_data->borderRadius == other.d_data->borderRadius &&
224  d_data->borderPen == other.d_data->borderPen &&
225  d_data->backgroundBrush == other.d_data->backgroundBrush &&
226  d_data->paintAttributes == other.d_data->paintAttributes &&
227  d_data->textEngine == other.d_data->textEngine;
228 }
229 
231 bool QwtText::operator!=( const QwtText &other ) const // invalidate
232 {
233  return !( other == *this );
234 }
235 
244 void QwtText::setText( const QString &text,
245  QwtText::TextFormat textFormat )
246 {
247  d_data->text = text;
248  d_data->textEngine = textEngine( text, textFormat );
249  d_layoutCache->invalidate();
250 }
251 
256 QString QwtText::text() const
257 {
258  return d_data->text;
259 }
260 
271 void QwtText::setRenderFlags( int renderFlags )
272 {
273  if ( renderFlags != d_data->renderFlags )
274  {
275  d_data->renderFlags = renderFlags;
276  d_layoutCache->invalidate();
277  }
278 }
279 
285 {
286  return d_data->renderFlags;
287 }
288 
296 void QwtText::setFont( const QFont &font )
297 {
298  d_data->font = font;
299  setPaintAttribute( PaintUsingTextFont );
300 }
301 
303 QFont QwtText::font() const
304 {
305  return d_data->font;
306 }
307 
317 QFont QwtText::usedFont( const QFont &defaultFont ) const
318 {
319  if ( d_data->paintAttributes & PaintUsingTextFont )
320  return d_data->font;
321 
322  return defaultFont;
323 }
324 
332 void QwtText::setColor( const QColor &color )
333 {
334  d_data->color = color;
335  setPaintAttribute( PaintUsingTextColor );
336 }
337 
339 QColor QwtText::color() const
340 {
341  return d_data->color;
342 }
343 
353 QColor QwtText::usedColor( const QColor &defaultColor ) const
354 {
355  if ( d_data->paintAttributes & PaintUsingTextColor )
356  return d_data->color;
357 
358  return defaultColor;
359 }
360 
367 void QwtText::setBorderRadius( double radius )
368 {
369  d_data->borderRadius = qMax( 0.0, radius );
370 }
371 
376 double QwtText::borderRadius() const
377 {
378  return d_data->borderRadius;
379 }
380 
387 void QwtText::setBorderPen( const QPen &pen )
388 {
389  d_data->borderPen = pen;
390  setPaintAttribute( PaintBackground );
391 }
392 
397 QPen QwtText::borderPen() const
398 {
399  return d_data->borderPen;
400 }
401 
408 void QwtText::setBackgroundBrush( const QBrush &brush )
409 {
410  d_data->backgroundBrush = brush;
411  setPaintAttribute( PaintBackground );
412 }
413 
419 {
420  return d_data->backgroundBrush;
421 }
422 
433 void QwtText::setPaintAttribute( PaintAttribute attribute, bool on )
434 {
435  if ( on )
436  d_data->paintAttributes |= attribute;
437  else
438  d_data->paintAttributes &= ~attribute;
439 }
440 
450 {
451  return d_data->paintAttributes & attribute;
452 }
453 
462 {
463  if ( on )
464  d_data->layoutAttributes |= attribute;
465  else
466  d_data->layoutAttributes &= ~attribute;
467 }
468 
478 {
479  return d_data->layoutAttributes | attribute;
480 }
481 
490 double QwtText::heightForWidth( double width, const QFont &defaultFont ) const
491 {
492  // We want to calculate in screen metrics. So
493  // we need a font that uses screen metrics
494 
495  const QFont font( usedFont( defaultFont ), QApplication::desktop() );
496 
497  double h = 0;
498 
499  if ( d_data->layoutAttributes & MinimumLayout )
500  {
501  double left, right, top, bottom;
502  d_data->textEngine->textMargins( font, d_data->text,
503  left, right, top, bottom );
504 
505  h = d_data->textEngine->heightForWidth(
506  font, d_data->renderFlags, d_data->text,
507  width + left + right );
508 
509  h -= top + bottom;
510  }
511  else
512  {
513  h = d_data->textEngine->heightForWidth(
514  font, d_data->renderFlags, d_data->text, width );
515  }
516 
517  return h;
518 }
519 
526 QSizeF QwtText::textSize( const QFont &defaultFont ) const
527 {
528  // We want to calculate in screen metrics. So
529  // we need a font that uses screen metrics
530 
531  const QFont font( usedFont( defaultFont ), QApplication::desktop() );
532 
533  if ( !d_layoutCache->textSize.isValid()
534  || d_layoutCache->font != font )
535  {
536  d_layoutCache->textSize = d_data->textEngine->textSize(
537  font, d_data->renderFlags, d_data->text );
538  d_layoutCache->font = font;
539  }
540 
541  QSizeF sz = d_layoutCache->textSize;
542 
543  if ( d_data->layoutAttributes & MinimumLayout )
544  {
545  double left, right, top, bottom;
546  d_data->textEngine->textMargins( font, d_data->text,
547  left, right, top, bottom );
548  sz -= QSizeF( left + right, top + bottom );
549  }
550 
551  return sz;
552 }
553 
560 void QwtText::draw( QPainter *painter, const QRectF &rect ) const
561 {
562  if ( d_data->paintAttributes & PaintBackground )
563  {
564  if ( d_data->borderPen != Qt::NoPen ||
565  d_data->backgroundBrush != Qt::NoBrush )
566  {
567  painter->save();
568 
569  painter->setPen( d_data->borderPen );
570  painter->setBrush( d_data->backgroundBrush );
571 
572  if ( d_data->borderRadius == 0 )
573  {
574  QwtPainter::drawRect( painter, rect );
575  }
576  else
577  {
578  painter->setRenderHint( QPainter::Antialiasing, true );
579  painter->drawRoundedRect( rect,
580  d_data->borderRadius, d_data->borderRadius );
581  }
582 
583  painter->restore();
584  }
585  }
586 
587  painter->save();
588 
589  if ( d_data->paintAttributes & PaintUsingTextFont )
590  {
591  painter->setFont( d_data->font );
592  }
593 
594  if ( d_data->paintAttributes & PaintUsingTextColor )
595  {
596  if ( d_data->color.isValid() )
597  painter->setPen( d_data->color );
598  }
599 
600  QRectF expandedRect = rect;
601  if ( d_data->layoutAttributes & MinimumLayout )
602  {
603  // We want to calculate in screen metrics. So
604  // we need a font that uses screen metrics
605 
606  const QFont font( painter->font(), QApplication::desktop() );
607 
608  double left, right, top, bottom;
609  d_data->textEngine->textMargins(
610  font, d_data->text, left, right, top, bottom );
611 
612  expandedRect.setTop( rect.top() - top );
613  expandedRect.setBottom( rect.bottom() + bottom );
614  expandedRect.setLeft( rect.left() - left );
615  expandedRect.setRight( rect.right() + right );
616  }
617 
618  d_data->textEngine->draw( painter, expandedRect,
619  d_data->renderFlags, d_data->text );
620 
621  painter->restore();
622 }
623 
639 const QwtTextEngine *QwtText::textEngine( const QString &text,
640  QwtText::TextFormat format )
641 {
642  return QwtTextEngineDict::dict().textEngine( text, format );
643 }
644 
661 {
662  QwtTextEngineDict::dict().setTextEngine( format, engine );
663 }
664 
674 {
675  return QwtTextEngineDict::dict().textEngine( format );
676 }
QFlags< LayoutAttribute > LayoutAttributes
Layout attributes.
Definition: qwt_text.h:140
virtual bool mightRender(const QString &text) const =0
QwtText::PaintAttributes paintAttributes
Definition: qwt_text.cpp:158
void setFont(const QFont &)
Definition: qwt_text.cpp:296
EngineMap d_map
Definition: qwt_text.cpp:45
int renderFlags() const
Definition: qwt_text.cpp:284
void setText(const QString &, QwtText::TextFormat textFormat=AutoText)
Definition: qwt_text.cpp:244
TextFormat
Text format.
Definition: qwt_text.h:64
void setBorderPen(const QPen &)
Definition: qwt_text.cpp:387
static const QwtTextEngine * textEngine(const QString &text, QwtText::TextFormat=AutoText)
Definition: qwt_text.cpp:639
void setTextEngine(QwtText::TextFormat, QwtTextEngine *)
Definition: qwt_text.cpp:101
Use the Scribe framework (Qt Rich Text) to render the text.
Definition: qwt_text.h:78
bool operator!=(const QwtText &) const
Relational operator.
Definition: qwt_text.cpp:231
QwtText(const QString &=QString(), TextFormat textFormat=AutoText)
Definition: qwt_text.cpp:182
QFlags< PaintAttribute > PaintAttributes
Paint attributes.
Definition: qwt_text.h:122
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:560
const QwtTextEngine * textEngine(QwtText::TextFormat) const
Definition: qwt_text.cpp:124
void setColor(const QColor &)
Definition: qwt_text.cpp:332
bool testLayoutAttribute(LayoutAttribute) const
Definition: qwt_text.cpp:477
static void setTextEngine(QwtText::TextFormat, QwtTextEngine *)
Definition: qwt_text.cpp:659
LayoutCache * d_layoutCache
Definition: qwt_text.h:202
const QwtTextEngine * textEngine
Definition: qwt_text.cpp:161
bool testPaintAttribute(PaintAttribute) const
Definition: qwt_text.cpp:449
QwtText::LayoutAttributes layoutAttributes
Definition: qwt_text.cpp:159
QSizeF textSize(const QFont &=QFont()) const
Definition: qwt_text.cpp:526
PaintAttribute
Paint Attributes.
Definition: qwt_text.h:109
void setRenderFlags(int flags)
Change the render flags.
Definition: qwt_text.cpp:271
double borderRadius() const
Definition: qwt_text.cpp:376
static void drawRect(QPainter *, double x, double y, double w, double h)
Wrapper for QPainter::drawRect()
QFont usedFont(const QFont &) const
Definition: qwt_text.cpp:317
QMap< int, QwtTextEngine * > EngineMap
Definition: qwt_text.cpp:38
void setLayoutAttribute(LayoutAttribute, bool on=true)
Definition: qwt_text.cpp:461
QColor color() const
Return the pen color, used for painting the text.
Definition: qwt_text.cpp:339
A class representing a text.
Definition: qwt_text.h:51
A text engine for Qt rich texts.
Abstract base class for rendering text strings.
QString text() const
Definition: qwt_text.cpp:256
bool operator==(const QwtText &) const
Relational operator.
Definition: qwt_text.cpp:217
void setBackgroundBrush(const QBrush &)
Definition: qwt_text.cpp:408
void setPaintAttribute(PaintAttribute, bool on=true)
Definition: qwt_text.cpp:433
QPen borderPen() const
Definition: qwt_text.cpp:397
~QwtText()
Destructor.
Definition: qwt_text.cpp:202
LayoutAttribute
Layout Attributes The layout attributes affects some aspects of the layout of the text...
Definition: qwt_text.h:128
static QwtTextEngineDict & dict()
Definition: qwt_text.cpp:48
PrivateData * d_data
Definition: qwt_text.h:199
void setBorderRadius(double)
Definition: qwt_text.cpp:367
QBrush backgroundBrush() const
Definition: qwt_text.cpp:418
QFont font() const
Return the font.
Definition: qwt_text.cpp:303
const QwtTextEngine * engine(EngineMap::const_iterator &it) const
Definition: qwt_text.cpp:40
double heightForWidth(double width, const QFont &=QFont()) const
Definition: qwt_text.cpp:490
QColor usedColor(const QColor &) const
Definition: qwt_text.cpp:353
Draw the text as it is, using a QwtPlainTextEngine.
Definition: qwt_text.h:75
A text engine for plain texts.
QwtText & operator=(const QwtText &)
Assignment operator.
Definition: qwt_text.cpp:209


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:18