Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_text_engine.cpp
- Timestamp:
- Nov 3, 2026, 10:53:35 AM (3 weeks ago)
- File:
-
- 1 edited
-
trunk/BNC/qwt/qwt_text_engine.cpp (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_text_engine.cpp
r9383 r11158 1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*-*****************************1 /****************************************************************************** 2 2 * Qwt Widget Library 3 3 * Copyright (C) 1997 Josef Wilgen … … 9 9 10 10 #include "qwt_text_engine.h" 11 #include "qwt_math.h"12 11 #include "qwt_painter.h" 12 13 13 #include <qpainter.h> 14 14 #include <qpixmap.h> … … 20 20 #include <qabstracttextdocumentlayout.h> 21 21 22 static QString taggedRichText( const QString &text, int flags )22 static QString taggedRichText( const QString& text, int flags ) 23 23 { 24 24 QString richText = text; … … 27 27 if ( flags & Qt::AlignJustify ) 28 28 { 29 richText.prepend( Q String::fromLatin1( "<div align=\"justify\">" ) );30 richText.append( Q String::fromLatin1( "</div>" ) );29 richText.prepend( QLatin1String( "<div align=\"justify\">" ) ); 30 richText.append( QLatin1String ( "</div>" ) ); 31 31 } 32 32 else if ( flags & Qt::AlignRight ) 33 33 { 34 richText.prepend( Q String::fromLatin1( "<div align=\"right\">" ) );35 richText.append( Q String::fromLatin1( "</div>" ) );34 richText.prepend( QLatin1String ( "<div align=\"right\">" ) ); 35 richText.append( QLatin1String ( "</div>" ) ); 36 36 } 37 37 else if ( flags & Qt::AlignHCenter ) 38 38 { 39 richText.prepend( Q String::fromLatin1( "<div align=\"center\">" ) );40 richText.append( Q String::fromLatin1( "</div>" ) );39 richText.prepend( QLatin1String ( "<div align=\"center\">" ) ); 40 richText.append( QLatin1String ( "</div>" ) ); 41 41 } 42 42 … … 44 44 } 45 45 46 class QwtRichTextDocument: public QTextDocument 47 { 48 public: 49 QwtRichTextDocument( const QString &text, int flags, const QFont &font ) 50 { 51 setUndoRedoEnabled( false ); 52 setDefaultFont( font ); 53 setHtml( text ); 54 55 // make sure we have a document layout 56 ( void )documentLayout(); 57 58 QTextOption option = defaultTextOption(); 59 if ( flags & Qt::TextWordWrap ) 60 option.setWrapMode( QTextOption::WordWrap ); 61 else 62 option.setWrapMode( QTextOption::NoWrap ); 63 64 option.setAlignment( static_cast<Qt::Alignment>( flags ) ); 65 setDefaultTextOption( option ); 66 67 QTextFrame *root = rootFrame(); 68 QTextFrameFormat fm = root->frameFormat(); 69 fm.setBorder( 0 ); 70 fm.setMargin( 0 ); 71 fm.setPadding( 0 ); 72 fm.setBottomMargin( 0 ); 73 fm.setLeftMargin( 0 ); 74 root->setFrameFormat( fm ); 75 76 adjustSize(); 77 } 78 }; 46 namespace 47 { 48 class QwtRichTextDocument : public QTextDocument 49 { 50 public: 51 QwtRichTextDocument( const QString& text, int flags, const QFont& font ) 52 { 53 setUndoRedoEnabled( false ); 54 setDefaultFont( font ); 55 setHtml( text ); 56 57 // make sure we have a document layout 58 ( void )documentLayout(); 59 60 QTextOption option = defaultTextOption(); 61 if ( flags & Qt::TextWordWrap ) 62 option.setWrapMode( QTextOption::WordWrap ); 63 else 64 option.setWrapMode( QTextOption::NoWrap ); 65 66 option.setAlignment( static_cast< Qt::Alignment >( flags ) ); 67 setDefaultTextOption( option ); 68 69 QTextFrame* root = rootFrame(); 70 QTextFrameFormat fm = root->frameFormat(); 71 fm.setBorder( 0 ); 72 fm.setMargin( 0 ); 73 fm.setPadding( 0 ); 74 fm.setBottomMargin( 0 ); 75 fm.setLeftMargin( 0 ); 76 root->setFrameFormat( fm ); 77 78 adjustSize(); 79 } 80 }; 81 } 79 82 80 83 class QwtPlainTextEngine::PrivateData 81 84 { 82 public: 83 int effectiveAscent( const QFont &font ) const85 public: 86 int effectiveAscent( const QFont& font ) const 84 87 { 85 88 const QString fontKey = font.key(); 86 89 87 QMap<QString, int>::const_iterator it = 88 d_ascentCache.constFind( fontKey );89 90 if ( it != d_ascentCache.constEnd() )90 QMap< QString, int >::const_iterator it = 91 m_ascentCache.constFind( fontKey ); 92 93 if ( it != m_ascentCache.constEnd() ) 91 94 return *it; 92 95 93 96 const int ascent = findAscent( font ); 94 d_ascentCache.insert( fontKey, ascent );97 m_ascentCache.insert( fontKey, ascent ); 95 98 96 99 return ascent; 97 100 } 98 101 99 private: 100 int findAscent( const QFont &font ) const102 private: 103 static int findAscent( const QFont& font ) 101 104 { 102 105 static const QString dummy( "E" ); … … 104 107 105 108 const QFontMetrics fm( font ); 106 QPixmap pm( fm.width( dummy ), fm.height() ); 109 110 QPixmap pm( QwtPainter::horizontalAdvance( fm, dummy ), fm.height() ); 107 111 pm.fill( white ); 108 112 … … 117 121 for ( row = 0; row < img.height(); row++ ) 118 122 { 119 const QRgb *line = reinterpret_cast<const QRgb*>(123 const QRgb* line = reinterpret_cast< const QRgb* >( 120 124 img.scanLine( row ) ); 121 125 … … 131 135 } 132 136 133 mutable QMap<QString, int > d_ascentCache;137 mutable QMap< QString, int > m_ascentCache; 134 138 }; 135 139 … … 147 151 QwtPlainTextEngine::QwtPlainTextEngine() 148 152 { 149 d_data = new PrivateData;153 m_data = new PrivateData; 150 154 } 151 155 … … 153 157 QwtPlainTextEngine::~QwtPlainTextEngine() 154 158 { 155 delete d_data;159 delete m_data; 156 160 } 157 161 … … 165 169 166 170 \return Calculated height 167 */ 171 */ 168 172 double QwtPlainTextEngine::heightForWidth( const QFont& font, int flags, 169 const QString& text, double width ) const173 const QString& text, double width ) const 170 174 { 171 175 const QFontMetricsF fm( font ); … … 177 181 178 182 /*! 179 Returns the size, that is needed to render text 180 181 \param font Font of the text 182 \param flags Bitwise OR of the flags used like in QPainter::drawText 183 \param text Text to be rendered 184 185 \return Calculated size 186 */ 187 QSizeF QwtPlainTextEngine::textSize( const QFont &font,183 Returns the size, that is needed to render text 184 185 \param font Font of the text 186 \param flags Bitwise OR of the flags used like in QPainter::drawText 187 \param text Text to be rendered 188 189 \return Calculated size 190 */ 191 QSizeF QwtPlainTextEngine::textSize( const QFont& font, 188 192 int flags, const QString& text ) const 189 193 { … … 196 200 197 201 /*! 198 Return margins around the texts 199 200 \param font Font of the text 201 \param left Return 0 202 \param right Return 0 203 \param top Return value for the top margin 204 \param bottom Return value for the bottom margin 205 */ 206 void QwtPlainTextEngine::textMargins( const QFont &font, const QString&,207 double &left, double&right, double&top, double&bottom ) const202 Return margins around the texts 203 204 \param font Font of the text 205 \param left Return 0 206 \param right Return 0 207 \param top Return value for the top margin 208 \param bottom Return value for the bottom margin 209 */ 210 void QwtPlainTextEngine::textMargins( const QFont& font, const QString&, 211 double& left, double& right, double& top, double& bottom ) const 208 212 { 209 213 left = right = top = 0; 210 214 211 215 const QFontMetricsF fm( font ); 212 top = fm.ascent() - d_data->effectiveAscent( font );216 top = fm.ascent() - m_data->effectiveAscent( font ); 213 217 bottom = fm.descent(); 214 218 } 215 219 216 220 /*! 217 \brief Draw the text in a clipping rectangle 218 219 A wrapper for QPainter::drawText. 220 221 \param painter Painter 222 \param rect Clipping rectangle 223 \param flags Bitwise OR of the flags used like in QPainter::drawText 224 \param text Text to be rendered 225 */ 226 void QwtPlainTextEngine::draw( QPainter *painter, const QRectF&rect,221 \brief Draw the text in a clipping rectangle 222 223 A wrapper for QPainter::drawText. 224 225 \param painter Painter 226 \param rect Clipping rectangle 227 \param flags Bitwise OR of the flags used like in QPainter::drawText 228 \param text Text to be rendered 229 */ 230 void QwtPlainTextEngine::draw( QPainter* painter, const QRectF& rect, 227 231 int flags, const QString& text ) const 228 232 { … … 231 235 232 236 /*! 233 Test if a string can be rendered by this text engine. 234 \return Always true. All texts can be rendered by QwtPlainTextEngine 235 */ 236 bool QwtPlainTextEngine::mightRender( const QString & ) const237 Test if a string can be rendered by this text engine. 238 \return Always true. All texts can be rendered by QwtPlainTextEngine 239 */ 240 bool QwtPlainTextEngine::mightRender( const QString& ) const 237 241 { 238 242 return true; … … 255 259 256 260 \return Calculated height 257 */ 261 */ 258 262 double QwtRichTextEngine::heightForWidth( const QFont& font, int flags, 259 const QString& text, double width ) const263 const QString& text, double width ) const 260 264 { 261 265 QwtRichTextDocument doc( text, flags, font ); … … 266 270 267 271 /*! 268 Returns the size, that is needed to render text 269 270 \param font Font of the text 271 \param flags Bitwise OR of the flags used like in QPainter::drawText() 272 \param text Text to be rendered 273 274 \return Calculated size 275 */ 276 277 QSizeF QwtRichTextEngine::textSize( const QFont &font,272 Returns the size, that is needed to render text 273 274 \param font Font of the text 275 \param flags Bitwise OR of the flags used like in QPainter::drawText() 276 \param text Text to be rendered 277 278 \return Calculated size 279 */ 280 281 QSizeF QwtRichTextEngine::textSize( const QFont& font, 278 282 int flags, const QString& text ) const 279 283 { … … 292 296 293 297 /*! 294 Draw the text in a clipping rectangle 295 296 \param painter Painter 297 \param rect Clipping rectangle 298 \param flags Bitwise OR of the flags like in for QPainter::drawText() 299 \param text Text to be rendered 300 */ 301 void QwtRichTextEngine::draw( QPainter *painter, const QRectF&rect,298 Draw the text in a clipping rectangle 299 300 \param painter Painter 301 \param rect Clipping rectangle 302 \param flags Bitwise OR of the flags like in for QPainter::drawText() 303 \param text Text to be rendered 304 */ 305 void QwtRichTextEngine::draw( QPainter* painter, const QRectF& rect, 302 306 int flags, const QString& text ) const 303 307 { … … 313 317 314 318 \return Tagged text 315 */ 316 QString QwtRichTextEngine::taggedText( const QString &text, int flags ) const319 */ 320 QString QwtRichTextEngine::taggedText( const QString& text, int flags ) const 317 321 { 318 322 return taggedRichText( text, flags ); … … 320 324 321 325 /*! 322 Test if a string can be rendered by this text engine 323 324 \param text Text to be tested 325 \return Qt::mightBeRichText(text); 326 */ 327 bool QwtRichTextEngine::mightRender( const QString &text ) const326 Test if a string can be rendered by this text engine 327 328 \param text Text to be tested 329 \return Qt::mightBeRichText(text); 330 */ 331 bool QwtRichTextEngine::mightRender( const QString& text ) const 328 332 { 329 333 return Qt::mightBeRichText( text ); … … 331 335 332 336 /*! 333 Return margins around the texts 334 335 \param left Return 0 336 \param right Return 0 337 \param top Return 0 338 \param bottom Return 0 339 */ 340 void QwtRichTextEngine::textMargins( const QFont &, const QString&,341 double &left, double&right, double&top, double&bottom ) const337 Return margins around the texts 338 339 \param left Return 0 340 \param right Return 0 341 \param top Return 0 342 \param bottom Return 0 343 */ 344 void QwtRichTextEngine::textMargins( const QFont&, const QString&, 345 double& left, double& right, double& top, double& bottom ) const 342 346 { 343 347 left = right = top = bottom = 0;
Note:
See TracChangeset
for help on using the changeset viewer.
