From 811e8db825f32274e382c42e8b8e95e0a3b2eec4 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Mon, 22 Jun 2009 16:40:42 +0200 Subject: [PATCH] Add constrained text item. --- lib/constrained_text_item.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/constrained_text_item.rb diff --git a/lib/constrained_text_item.rb b/lib/constrained_text_item.rb new file mode 100644 index 0000000..694fc17 --- /dev/null +++ b/lib/constrained_text_item.rb @@ -0,0 +1,36 @@ +class ConstrainedTextItem < Qt::GraphicsItem + def initialize(text, parent, constraint, opts = {}) + super(parent) + @text = text + @parent = parent + @constraint = constraint + + @font = opts[:font] || Qt::Font.new + @color = opts[:color] || Qt::Color.new(Qt::black) + + @brect = Qt::FontMetrics.new(@font).bounding_rect(@text) + @factor = [ + 0.9 * @constraint.width / @brect.width, + @constraint.height / @brect.height].min + end + + def paint(p, opts, widget) + p.pen = @color + p.font = @font + p.draw_rect @constraint + p.saving do + p.translate(@constraint.center) + p.scale(@factor, @factor) + p.translate(-@brect.center) + p.draw_text(0, 0, @text) + end + end + + def boundingRect + @constraint + end + + def name + @text + end +end \ No newline at end of file -- 2.11.4.GIT