Class: MultiImageCanvas

Inherits:
Fox::FXScrollWindow
  • Object
show all
Includes:
Fox, Responder
Defined in:
lib/piggy-gui/multiimagecanvas.rb

Overview

Shows multiple images in a sigle widget.

Constant Summary collapse

CacheSize =
200

Instance Method Summary collapse

Constructor Details

#initialize(p1, p2) ⇒ MultiImageCanvas

Returns a new instance of MultiImageCanvas.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/piggy-gui/multiimagecanvas.rb', line 15

def initialize(p1, p2)
  super(p1, p2)
  draw_area = FXCanvas.new(self, nil, 0,
    FRAME_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_Y)
  draw_area.connect(SEL_PAINT, method(:on_paint))
  @contentWidth = width
  @contentHeight = height
  @images = Array.new
  @buffer = nil
  @thumbs = Hash.new
  @fifoThumbsKeys = Array.new
  @imgProcessor = ImageProcessor.new getApp
end

Instance Method Details

#cache_thumb_for(file, img) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/piggy-gui/multiimagecanvas.rb', line 98

def cache_thumb_for(file, img)
  @imgProcessor.fit_image(img, 100, 100, 0)
  @thumbs[file] = img
  @fifoThumbsKeys.push(file)
  if @fifoThumbsKeys.size > CacheSize
    thumb_to_destroy = @fifoThumbsKeys.shift
    @thumbs[thumb_to_destroy].destroy
    @thumbs.delete(thumb_to_destroy)
  end
end

#destroy_all(imageCache) ⇒ Object



29
30
31
# File 'lib/piggy-gui/multiimagecanvas.rb', line 29

def destroy_all(imageCache)
  @imgProcessor.destroy_all(imageCache)
end

#fitObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/piggy-gui/multiimagecanvas.rb', line 109

def fit
  img = image
  w = getWidth
  h = getHeight
  return if img.nil? || w < 5 || h < 5
  FXMemoryStream.open(FXStreamLoad, @buffer) do |stream|
    img.loadPixels(stream)
  end
  @imgProcessor.fit_image(img, w, h)
  force_refresh
end

#force_refreshObject



127
128
129
130
131
132
# File 'lib/piggy-gui/multiimagecanvas.rb', line 127

def force_refresh
  contentWindow.update
  contentWindow.repaint
  contentWindow.recalc
  repaint
end

#get_content_heightObject



41
42
43
# File 'lib/piggy-gui/multiimagecanvas.rb', line 41

def get_content_height
  return @contentHeight
end

#get_content_widthObject



37
38
39
# File 'lib/piggy-gui/multiimagecanvas.rb', line 37

def get_content_width
  return @contentWidth
end

#imageObject



49
50
51
# File 'lib/piggy-gui/multiimagecanvas.rb', line 49

def image
  return @images.empty? ? nil : @images[0]
end

#image_processorObject



33
34
35
# File 'lib/piggy-gui/multiimagecanvas.rb', line 33

def image_processor
  return @imgProcessor
end

#imagesObject



45
46
47
# File 'lib/piggy-gui/multiimagecanvas.rb', line 45

def images
  return @images
end

#load_and_show_image(file) ⇒ Object



57
58
59
# File 'lib/piggy-gui/multiimagecanvas.rb', line 57

def load_and_show_image(file)
  load_image(file)
end

#load_image(file, additive = false, degree = 0) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/piggy-gui/multiimagecanvas.rb', line 61

def load_image(file, additive=false, degree=0)
  if additive && @thumbs.has_key?(file)
    images.push(@thumbs[file]) 
    return
  end
  unless @imgProcessor.is_supported_image_file? file
    FXMessageBox.error(self, MBOX_OK, "Warning image type",
      "Unsupported image type: #{file}")
    return
  end
  getApp.beginWaitCursor do
    img = @imgProcessor.load_image(file, false)
    unless degree == 0
      @imgProcessor.rotate_image(img, degree)
    end
    destroy_all(@images) if !additive
    images.push(img)
    if additive
      cache_thumb_for(file, img)
    else
      w = img.getWidth
      h = img.getHeight
      if(w > 1600 && h > 1000)
        # reduce footprint
        @imgProcessor.fit_image(img, 1600, 1000, 1)
      end
      FXMemoryStream.open(FXStreamSave, nil) do |stream|
        img.savePixels(stream)
        @buffer = stream.takeBuffer
      end
      @imgProcessor.fit_image(img, getWidth, getHeight, 1)
    end
    img.create
    force_refresh if !additive
  end
end

#load_images(files) ⇒ Object



121
122
123
124
125
# File 'lib/piggy-gui/multiimagecanvas.rb', line 121

def load_images(files)
  images.clear
  files.each { |f| load_image(f, true) }
  force_refresh
end

#on_paint(sender, sel, unused) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/piggy-gui/multiimagecanvas.rb', line 134

def on_paint(sender, sel, unused)
  sdc = FXDCWindow.new(contentWindow)
  sdc.fillRectangle(0, 0, width, height)
  unless images.empty? || width < 5 || height < 5
    # Draw image table
    x = 0;
    y = 0;
    line_height = 0
    sub_width = images.size == 1 ? 0 : 16 # width for vScrollbar
    max_w = [images[0].width + sub_width, width].max
    images.each { |img|
      imgHeight = img.height
      imgWidth = img.width
      max_w = imgWidth if imgWidth + sub_width > max_w
      if x + img.width > max_w - sub_width
        x = 0
        y += line_height + 4
        line_height = 0
      end
      line_height = imgHeight if imgHeight > line_height
      sdc.drawImage(img, x, y)
      sdc.fillRectangle(x, y + imgHeight, x + imgWidth, y + line_height)
      x += imgWidth + 4
    }
    @contentWidth = max_w
    @contentHeight = y + line_height
  end
  sdc.end
end

#set_images(imageArray) ⇒ Object



53
54
55
# File 'lib/piggy-gui/multiimagecanvas.rb', line 53

def set_images(imageArray)
  @images = imageArray
end