1 class ConstrainedTextItem < Qt::GraphicsItem
4 def initialize(text, parent, constraint = Qt::RectF.new, opts = {})
8 @constraint = constraint
10 @font = opts[:font] || Qt::Font.new
11 @fm = Qt::FontMetrics.new(@font)
12 @color = opts[:color] || Qt::Color.new(Qt::black)
17 def paint(p, opts, widget)
21 p.translate(@constraint.center)
22 p.scale(@factor, @factor)
23 p.translate(-@brect_max.center)
24 p.draw_text((@brect_max.width - @brect.width) / 2, 0, @text)
38 def constraint=(value)
47 @brect_max = @constraint
50 @brect = @fm.bounding_rect(@text)
51 @brect_max = @fm.bounding_rect('H' * @text.size)
53 @constraint.width / @brect_max.width,
54 @constraint.height / @brect_max.height].min