47 #include <QGraphicsItem> 48 #include <QFontMetrics> 67 QGraphicsSimpleTextItem (_parent),
68 backgroundSet_ (false),
71 alignment_ (
Qt::AlignLeft),
80 Text::Text (
const QString &_text, QGraphicsItem *_parent) :
81 QGraphicsSimpleTextItem (_text, _parent),
83 backgroundSet_ (false),
86 alignment_ (
Qt::AlignLeft),
104 QFontMetrics fn (font ());
106 int width = _rect.width ();
111 width -= _rect.height () / 2;
113 width -= BACK_OFFSET * 2;
116 width -= _rect.height () / 2;
118 width -= BACK_OFFSET * 2;
121 if (fn.boundingRect (text_).width () > width)
123 QString nt = text_ +
"...";
124 while (fn.boundingRect (nt).width () > width && nt.length () > 3)
125 nt.remove (nt.length () - 4, 1);
126 QGraphicsSimpleTextItem::setText (nt);
129 QGraphicsSimpleTextItem::setText (text_);
131 QGraphicsSimpleTextItem::setPos (_rect.topLeft ());
132 QGraphicsLayoutItem::setGeometry (_rect);
138 QSizeF Text::sizeHint (Qt::SizeHint _which,
const QSizeF &)
const 140 QFontMetrics fn (font ());
145 w += (fn.height () / 2) + (BACK_OFFSET * 2);
147 w += BACK_OFFSET * 2;
149 w += (fn.height () / 2) + (BACK_OFFSET * 2);
151 w += BACK_OFFSET * 2;
153 QSizeF bOff = QSizeF (w, BACK_OFFSET * 2);
156 case Qt::MinimumSize:
157 sh = QSizeF(fn.boundingRect (
"...").width (), fn.height ());
161 case Qt::PreferredSize:
162 sh = QSizeF(fn.boundingRect (text_).width (), fn.height ());
166 case Qt::MaximumSize:
167 sh = QSizeF(fn.boundingRect (text_).width (), fn.height ());
171 sh += QSizeF (65535, 0);
173 case Qt::MinimumDescent:
174 sh = QSizeF(0, fn.descent ());
185 void Text::paint (QPainter *_painter,
const QStyleOptionGraphicsItem *_option, QWidget *_widget)
187 qreal h = geometry ().size ().height () / 2.0;
193 path.moveTo (geometry ().size ().width () / 2, 0);
197 path.arcTo (0, 0, h * 2.0, h * 2.0, 90, 180);
202 path.arcTo (-h * 2, 0, h * 2.0, h * 2.0, 90, -180);
207 path.lineTo (geometry ().size ().width () - h, geometry ().size ().height ());
208 path.arcTo (geometry ().size ().width () - (h * 2), 0, h * 2.0, h * 2.0, -90, 180);
212 path.lineTo (geometry ().size ().width () + h, geometry ().size ().height ());
213 path.arcTo (geometry ().size ().width (), 0, h * 2.0, h * 2.0, -90, -180);
215 path.lineTo (geometry ().size ().width () / 2, 0);
217 _painter->setBrush (backgroundBrush_);
218 _painter->setPen (backgroundPen_);
219 _painter->drawPath (path);
225 lG.setFinalStop(0, h * 2);
226 lG.setColorAt(0, Qt::transparent);
227 lG.setColorAt(1, QColor (0, 0, 0, 192));
229 _painter->setBrush (lG);
230 _painter->setPen (Qt::NoPen);
231 _painter->drawPath(path);
234 lG.setFinalStop(0, 0);
235 lG.setColorAt(0, Qt::transparent);
236 lG.setColorAt(1, QColor (255, 255, 255, 192));
239 pen.setColor (Qt::transparent);
241 _painter->setBrush (lG);
242 _painter->setPen (pen);
243 _painter->drawPath(path);
246 _painter->translate (h, BACK_OFFSET);
248 _painter->translate (BACK_OFFSET * 2, BACK_OFFSET);
251 QFontMetrics fn (font ());
253 if (alignment_ == Qt::AlignHCenter)
255 trans = geometry ().size ().width ();
262 trans -= BACK_OFFSET * 2;
266 trans -= BACK_OFFSET * 2;
269 _painter->translate ((trans - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
271 else if (alignment_ == Qt::AlignRight)
273 trans = geometry ().size ().width ();
280 trans -= BACK_OFFSET * 2;
284 trans -= BACK_OFFSET * 2;
287 _painter->translate (trans - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);
294 QGraphicsSimpleTextItem::paint (_painter, _option, _widget);
302 backgroundSet_ =
true;
304 rightOut_ = _rightOut;
313 if (_alignment & Qt::AlignLeft)
314 alignment_ = Qt::AlignLeft;
315 else if (_alignment & Qt::AlignHCenter)
316 alignment_ = Qt::AlignHCenter;
317 else if (_alignment & Qt::AlignRight)
318 alignment_ = Qt::AlignRight;
326 hStretch_ = _stretch;
335 backgroundSet_ =
false;
344 QFontMetrics fn (font ());
349 QRectF rect = QGraphicsSimpleTextItem::boundingRect ();
351 if (alignment_ == Qt::AlignHCenter)
352 rect.translate ((geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
353 else if (alignment_ == Qt::AlignRight)
354 rect.translate (geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);
359 w = geometry ().size ().width ();
364 w += (fn.height () / 2) + (BACK_OFFSET * 2);
365 x -= (fn.height () / 2) + (BACK_OFFSET * 2);
370 w += (fn.height () / 2) + (BACK_OFFSET * 2);
373 return QRectF (QPointF (x, 0), QSizeF(w, geometry ().size ().height ()));
381 backgroundBrush_ = _brush;
390 backgroundPen_ = _pen;
401 QPainterPath
shape = QGraphicsSimpleTextItem::shape ();
403 QFontMetrics fn (font ());
405 if (alignment_ == Qt::AlignHCenter || alignment_ == Qt::AlignRight)
408 if (alignment_ == Qt::AlignHCenter)
409 trans = QTransform::fromTranslate ((geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
410 else if (alignment_ == Qt::AlignRight)
411 trans = QTransform::fromTranslate (geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);
413 QList<QPolygonF> poly = shape.toSubpathPolygons (trans);
415 shape = QPainterPath ();
417 foreach (
const QPolygonF &p, poly)
424 qreal h = geometry ().size ().height () / 2.0;
428 path.moveTo (geometry ().size ().width () / 2, 0);
432 path.arcTo (0, 0, h * 2.0, h * 2.0, 90, 180);
437 path.arcTo (-h * 2, 0, h * 2.0, h * 2.0, 90, -180);
442 path.lineTo (geometry ().size ().width () - h, geometry ().size ().height ());
443 path.arcTo (geometry ().size ().width () - (h * 2), 0, h * 2.0, h * 2.0, -90, 180);
447 path.lineTo (geometry ().size ().width () + h, geometry ().size ().height ());
448 path.arcTo (geometry ().size ().width (), 0, h * 2.0, h * 2.0, -90, -180);
450 path.lineTo (geometry ().size ().width () / 2, 0);
virtual QRectF boundingRect() const
Bounding rectangle.
virtual void setBackgroundBrush(QBrush _brush)
Sets the background brush.
virtual void setBackgroundPen(QPen _pen)
Sets the background pen.
void setBackground(bool _leftOut, bool _rightOut)
Enables background painting.
virtual void setGeometry(const QRectF &_rect)
Sets the geometry.
Text(QGraphicsItem *_parent=0)
Constructor.
void setHorizontalStretch(bool _stretch)
Should this widget be stretchable in horizontal direction.
virtual void paint(QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget=0)
Background painting.
void setAlignment(Qt::Alignment _alignment)
Placement of the text in a stretched widget.
QPainterPath shape() const
Returns the shape for proper repainting/event handling.
void clearBackground()
Disables backgorund painting.