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.
10 class ConstrainedTextItem < Qt::GraphicsItem
13 def initialize(text, parent, constraint = Qt::RectF.new, opts = {})
17 @constraint = constraint
19 @font = opts[:font] || Qt::Font.new
20 @fm = Qt::FontMetrics.new(@font)
21 @color = opts[:color] || Qt::Color.new(Qt::black)
26 def paint(p, opts, widget)
30 p.translate(@constraint.center)
31 p.scale(@factor, @factor)
32 p.translate(-@brect_max.center)
33 p.draw_text((@brect_max.width - @brect.width) / 2, 0, @text)
47 def constraint=(value)
61 @brect_max = @constraint
64 @brect = @fm.bounding_rect(@text)
65 @brect_max = @fm.bounding_rect('H' * @text.size)
67 @constraint.width / @brect_max.width,
68 @constraint.height / @brect_max.height].min