1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 class ConstrainedTextItem < Qt::GraphicsItem
11 def initialize(text, parent, constraint = Qt::RectF.new, opts = {})
15 @constraint = constraint
17 @font = opts[:font] || Qt::Font.new
18 @fm = Qt::FontMetrics.new(@font)
19 @color = opts[:color] || Qt::Color.new(Qt::black)
24 def paint(p, opts, widget)
28 p.translate(@constraint.center)
29 p.scale(@factor, @factor)
30 p.translate(-@brect_max.center)
31 p.draw_text((@brect_max.width - @brect.width) / 2, 0, @text)
45 def constraint=(value)
59 @brect_max = @constraint
62 @brect = @fm.bounding_rect(@text)
63 @brect_max = @fm.bounding_rect('H' * @text.size)
65 @constraint.width / @brect_max.width,
66 @constraint.height / @brect_max.height].min