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 # A NinePatchItem is rectangular item composed of 9 subitems,
11 # 4 at the sides, 4 at the corners, and one in the center.
12 # Subitems are specified in the items hash, with keys:
13 # :n, :w, :s, :e for the sides;
14 # :ne, :nw, :sw, :se for the corners;
15 # :center for the center.
16 # Subitems must respond to set_geometry.
18 class NinePatchItem < Qt::GraphicsItemGroup
21 def initialize(parent, scene, items)
25 @items.each do |key, item|
34 def set_geometry(rect)
35 self.pos = rect.top_left.to_f
36 g = lambda do |key, x, y, w, h|
38 @items[key].set_geometry(Qt::Rect.new(x, y, w, h))
41 g[:nw, 0, 0, @bsize, @bsize]
42 g[:n, @bsize, 0, rect.width - @bsize * 2, @bsize]
43 g[:ne, rect.width - @bsize, 0, @bsize, @bsize]
45 g[:sw, 0, rect.height - @bsize, @bsize, @bsize]
46 g[:s, @bsize, rect.height - @bsize, rect.width - @bsize * 2, @bsize]
47 g[:se, rect.width - @bsize, rect.height - @bsize, @bsize, @bsize]
49 g[:w, 0, @bsize, @bsize, rect.height - @bsize * 2]
50 g[:center, @bsize, @bsize, rect.width - @bsize * 2, rect.height - @bsize * 2]
51 g[:e, rect.width - @bsize, @bsize, @bsize, rect.height - @bsize * 2]