File indexing completed on 2024-12-22 04:17:34
0001 /*************************************************************************** 0002 labelrenderer.h 0003 ------------------ 0004 begin : Jun 17 2005 0005 copyright : (C) 2005 by The University of Toronto 0006 email : netterfield@astro.utoronto.ca 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * * 0011 * This program is free software; you can redistribute it and/or modify * 0012 * it under the terms of the GNU General Public License as published by * 0013 * the Free Software Foundation; either version 2 of the License, or * 0014 * (at your option) any later version. * 0015 * * 0016 ***************************************************************************/ 0017 0018 #ifndef LABELRENDERER_H 0019 #define LABELRENDERER_H 0020 0021 #include <qfont.h> 0022 #include <qpainter.h> 0023 #include <qpair.h> 0024 #include <qstring.h> 0025 #include <qvariant.h> 0026 #ifndef KST_NO_PRINTER 0027 #include <QPrinter> 0028 #endif 0029 0030 #include "vector.h" 0031 #include "scalar.h" 0032 #include "string.h" 0033 #include "kst_export.h" 0034 0035 namespace Label { 0036 0037 struct RenderedText { 0038 QPointF location; 0039 QString text; 0040 QFont font; 0041 QPen pen; 0042 }; 0043 0044 // inline for speed. 0045 class RenderContext : public QObject { 0046 Q_OBJECT 0047 0048 public: 0049 RenderContext(const QFont& font, QPainter *p) 0050 : QObject(), p(p), _fm(_font) { 0051 x = y = xMax = xStart = 0; 0052 precision = 8; 0053 setFont(font); 0054 lines = 0; 0055 } 0056 0057 inline void addToCache(QPointF location, QString &text, QFont &font, QPen &pen) { 0058 RenderedText cacheEntry; 0059 cacheEntry.location = location; 0060 cacheEntry.text = text; 0061 cacheEntry.font = font; 0062 cacheEntry.pen = pen; 0063 cachedText.append(cacheEntry); 0064 } 0065 0066 inline const QFont& font() const { 0067 if (p) { 0068 return p->font(); 0069 } else { 0070 return _font; 0071 } 0072 } 0073 0074 inline void setFont(const QFont& f_in) { 0075 QFont f = f_in; 0076 _fontSize = f.pointSize(); 0077 0078 if (p) { 0079 p->setFont(f); 0080 _ascent = p->fontMetrics().ascent(); 0081 _descent = p->fontMetrics().descent(); 0082 _height = p->fontMetrics().height(); 0083 _lineSpacing = p->fontMetrics().lineSpacing(); 0084 } else { 0085 _font = f; 0086 _fm = QFontMetrics(_font); 0087 _ascent = _fm.ascent(); 0088 _lineSpacing = _fm.lineSpacing(); 0089 _descent = _fm.descent(); 0090 _height = _fm.height(); 0091 } 0092 } 0093 0094 inline void addObject(Kst::VectorPtr vp) { 0095 _refObjects.append(vp); 0096 connect(vp.data(), SIGNAL(dirty()), this, SIGNAL(labelDirty())); 0097 } 0098 0099 inline void addObject(Kst::ScalarPtr scalar) { 0100 _refObjects.append(scalar); 0101 connect(scalar.data(), SIGNAL(dirty()), this, SIGNAL(labelDirty())); 0102 } 0103 0104 inline void addObject(Kst::StringPtr string) { 0105 _refObjects.append(string); 0106 connect(string.data(), SIGNAL(dirty()), this, SIGNAL(labelDirty())); 0107 } 0108 0109 inline int fontSize() const { 0110 return _fontSize; 0111 } 0112 0113 inline int fontAscent() const { 0114 return _ascent; 0115 } 0116 0117 inline int lineSpacing() const { 0118 return _lineSpacing; 0119 } 0120 0121 inline int fontDescent() const { 0122 return _descent; 0123 } 0124 0125 inline int fontHeight() const { 0126 return _height; 0127 } 0128 0129 inline int fontWidth(const QString& txt) const { 0130 if (p) { 0131 return p->fontMetrics().width(txt); 0132 } else { 0133 return _fm.width(txt); 0134 } 0135 } 0136 0137 int x, y; // Coordinates we're rendering at 0138 int xMax, xStart; 0139 QString fontName; 0140 int size; 0141 QPainter *p; 0142 int precision; 0143 QList<Kst::Primitive*> _refObjects; 0144 QPen pen; 0145 int lines; 0146 QVector<RenderedText> cachedText; 0147 0148 Q_SIGNALS: 0149 void labelDirty(); 0150 0151 private: 0152 QFont _font; 0153 QFontMetrics _fm; 0154 int _ascent, _descent, _height, _lineSpacing; // caches to avoid performance problem with QFont* 0155 int _fontSize; 0156 }; 0157 0158 struct Chunk; 0159 void renderLabel(RenderContext& rc, Chunk *fi, bool cache, bool draw); 0160 void paintLabel(RenderContext& rc, QPainter *p); 0161 } 0162 0163 #endif 0164 // vim: ts=2 sw=2 et