53 #include <QGraphicsItem> 54 #include <QFontMetrics> 73 QGraphicsSimpleTextItem (_parent),
74 backgroundSet_ (false),
77 alignment_ (
Qt::AlignLeft),
86 Text::Text (
const QString &_text, QGraphicsItem *_parent) :
87 QGraphicsSimpleTextItem (_text, _parent),
89 backgroundSet_ (false),
92 alignment_ (
Qt::AlignLeft),
110 QFontMetrics fn (font ());
112 int width = _rect.width ();
117 width -= _rect.height () / 2;
119 width -= BACK_OFFSET * 2;
122 width -= _rect.height () / 2;
124 width -= BACK_OFFSET * 2;
127 if (fn.boundingRect (text_).width () > width)
129 QString nt = text_ +
"...";
130 while (fn.boundingRect (nt).width () > width && nt.length () > 3)
131 nt.remove (nt.length () - 4, 1);
132 QGraphicsSimpleTextItem::setText (nt);
135 QGraphicsSimpleTextItem::setText (text_);
137 QGraphicsSimpleTextItem::setPos (_rect.topLeft ());
138 QGraphicsLayoutItem::setGeometry (_rect);
144 QSizeF Text::sizeHint (Qt::SizeHint _which,
const QSizeF &)
const 146 QFontMetrics fn (font ());
151 w += (fn.height () / 2) + (BACK_OFFSET * 2);
153 w += BACK_OFFSET * 2;
155 w += (fn.height () / 2) + (BACK_OFFSET * 2);
157 w += BACK_OFFSET * 2;
159 QSizeF bOff = QSizeF (w, BACK_OFFSET * 2);
162 case Qt::MinimumSize:
163 sh = QSizeF(fn.boundingRect (
"...").width (), fn.height ());
167 case Qt::PreferredSize:
168 sh = QSizeF(fn.boundingRect (text_).width (), fn.height ());
172 case Qt::MaximumSize:
173 sh = QSizeF(fn.boundingRect (text_).width (), fn.height ());
177 sh += QSizeF (65535, 0);
179 case Qt::MinimumDescent:
180 sh = QSizeF(0, fn.descent ());
191 void Text::paint (QPainter *_painter,
const QStyleOptionGraphicsItem *_option, QWidget *_widget)
193 qreal h = geometry ().size ().height () / 2.0;
199 path.moveTo (geometry ().size ().width () / 2, 0);
203 path.arcTo (0, 0, h * 2.0, h * 2.0, 90, 180);
208 path.arcTo (-h * 2, 0, h * 2.0, h * 2.0, 90, -180);
213 path.lineTo (geometry ().size ().width () - h, geometry ().size ().height ());
214 path.arcTo (geometry ().size ().width () - (h * 2), 0, h * 2.0, h * 2.0, -90, 180);
218 path.lineTo (geometry ().size ().width () + h, geometry ().size ().height ());
219 path.arcTo (geometry ().size ().width (), 0, h * 2.0, h * 2.0, -90, -180);
221 path.lineTo (geometry ().size ().width () / 2, 0);
223 _painter->setBrush (backgroundBrush_);
224 _painter->setPen (backgroundPen_);
225 _painter->drawPath (path);
231 lG.setFinalStop(0, h * 2);
232 lG.setColorAt(0, Qt::transparent);
233 lG.setColorAt(1, QColor (0, 0, 0, 192));
235 _painter->setBrush (lG);
236 _painter->setPen (Qt::NoPen);
237 _painter->drawPath(path);
240 lG.setFinalStop(0, 0);
241 lG.setColorAt(0, Qt::transparent);
242 lG.setColorAt(1, QColor (255, 255, 255, 192));
245 pen.setColor (Qt::transparent);
247 _painter->setBrush (lG);
248 _painter->setPen (pen);
249 _painter->drawPath(path);
252 _painter->translate (h, BACK_OFFSET);
254 _painter->translate (BACK_OFFSET * 2, BACK_OFFSET);
257 QFontMetrics fn (font ());
259 if (alignment_ == Qt::AlignHCenter)
261 trans = geometry ().size ().width ();
268 trans -= BACK_OFFSET * 2;
272 trans -= BACK_OFFSET * 2;
275 _painter->translate ((trans - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
277 else if (alignment_ == Qt::AlignRight)
279 trans = geometry ().size ().width ();
286 trans -= BACK_OFFSET * 2;
290 trans -= BACK_OFFSET * 2;
293 _painter->translate (trans - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);
300 QGraphicsSimpleTextItem::paint (_painter, _option, _widget);
308 backgroundSet_ =
true;
310 rightOut_ = _rightOut;
319 if (_alignment & Qt::AlignLeft)
320 alignment_ = Qt::AlignLeft;
321 else if (_alignment & Qt::AlignHCenter)
322 alignment_ = Qt::AlignHCenter;
323 else if (_alignment & Qt::AlignRight)
324 alignment_ = Qt::AlignRight;
332 hStretch_ = _stretch;
341 backgroundSet_ =
false;
350 QFontMetrics fn (font ());
355 QRectF rect = QGraphicsSimpleTextItem::boundingRect ();
357 if (alignment_ == Qt::AlignHCenter)
358 rect.translate ((geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
359 else if (alignment_ == Qt::AlignRight)
360 rect.translate (geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);
365 w = geometry ().size ().width ();
370 w += (fn.height () / 2) + (BACK_OFFSET * 2);
371 x -= (fn.height () / 2) + (BACK_OFFSET * 2);
376 w += (fn.height () / 2) + (BACK_OFFSET * 2);
379 return QRectF (QPointF (x, 0), QSizeF(w, geometry ().size ().height ()));
387 backgroundBrush_ = _brush;
396 backgroundPen_ = _pen;
407 QPainterPath
shape = QGraphicsSimpleTextItem::shape ();
409 QFontMetrics fn (font ());
411 if (alignment_ == Qt::AlignHCenter || alignment_ == Qt::AlignRight)
414 if (alignment_ == Qt::AlignHCenter)
415 trans = QTransform::fromTranslate ((geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
416 else if (alignment_ == Qt::AlignRight)
417 trans = QTransform::fromTranslate (geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);
419 QList<QPolygonF> poly = shape.toSubpathPolygons (trans);
421 shape = QPainterPath ();
423 foreach (
const QPolygonF &p, poly)
430 qreal h = geometry ().size ().height () / 2.0;
434 path.moveTo (geometry ().size ().width () / 2, 0);
438 path.arcTo (0, 0, h * 2.0, h * 2.0, 90, 180);
443 path.arcTo (-h * 2, 0, h * 2.0, h * 2.0, 90, -180);
448 path.lineTo (geometry ().size ().width () - h, geometry ().size ().height ());
449 path.arcTo (geometry ().size ().width () - (h * 2), 0, h * 2.0, h * 2.0, -90, 180);
453 path.lineTo (geometry ().size ().width () + h, geometry ().size ().height ());
454 path.arcTo (geometry ().size ().width (), 0, h * 2.0, h * 2.0, -90, -180);
456 path.lineTo (geometry ().size ().width () / 2, 0);
virtual void setBackgroundBrush(QBrush _brush)
Sets the background brush.
void clearBackground()
Disables backgorund painting.
virtual QRectF boundingRect() const
Bounding rectangle.
void setHorizontalStretch(bool _stretch)
Should this widget be stretchable in horizontal direction.
void setAlignment(Qt::Alignment _alignment)
Placement of the text in a stretched widget.
void setBackground(bool _leftOut, bool _rightOut)
Enables background painting.
virtual void setGeometry(const QRectF &_rect)
Sets the geometry.
virtual void paint(QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget=0)
Background painting.
QPainterPath shape() const
Returns the shape for proper repainting/event handling.
Text(QGraphicsItem *_parent=0)
Constructor.
virtual void setBackgroundPen(QPen _pen)
Sets the background pen.