Module: Wuffl::Actions

Defined in:
lib/wuffl/actions.rb

Class Method Summary collapse

Class Method Details

.deactivate_buttons(button_set) ⇒ Object

deactivate all buttons



115
116
117
118
119
# File 'lib/wuffl/actions.rb', line 115

def self.deactivate_buttons (button_set)
   button_set.each do |button, sens_value|
     button_set[button].sensitive = false
   end
end

.get_op_sysObject

get the operating system of the machine



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wuffl/actions.rb', line 7

def self.get_op_sys
   gempath = File.expand_path(File.join(File.dirname(__FILE__), '..'))
   gempath.sub!("/lib", "")
	if (/darwin/ =~ RUBY_PLATFORM) != nil
		return 'mac'
	elsif (/linux/ =~ RUBY_PLATFORM) != nil
		return 'linux'
	elsif (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
		return 'windows'
	end
end

.get_resolutionObject

get the resolution of the display



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wuffl/actions.rb', line 20

def self.get_resolution

   operating_system = get_op_sys

	case operating_system
		
	when 'windows'
		output_dimensions = `wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value`
     begin_hor_res = output_dimensions.index("HorizontalResolution")
     begin_ver_res = output_dimensions.index("VerticalResolution")
     begin_video_mode = output_dimensions.index("VideoMode")
     length_hor = "HorizontalResolution=".length
     length_ver = "VerticalResolution=".length
     length_video = "VideoMode=".length
     char_hor = output_dimensions[begin_hor_res + length_hor]
     char_ver = output_dimensions[begin_ver_res + length_ver]
     char_video = output_dimensions[begin_video_mode + length_video]
     if char_hor != char_hor.to_i.to_s
       output_dimensions.sub!("HorizontalResolution", "")
     end
     if char_ver != char_ver.to_i.to_s
       output_dimensions.sub!("VerticalResolution", "")
     end
     if char_video != char_video.to_i.to_s
       output_dimensions.sub!("VideoMode", "")
     end

		# Horizontal Resolution of the screen
		hor_res_begin_index = output_dimensions.index("HorizontalResolution") + "HorizontalResolution=".length
		hor_res_end_index = output_dimensions.index("CurrentVertical") - 3
		win_width = output_dimensions.slice(hor_res_begin_index..hor_res_end_index).to_i

		# Vertical Resolution of the screen
		ver_res_begin_index = output_dimensions.index("VerticalResolution") + "VerticalResolution=".length
		ver_res_end_index = output_dimensions.index("VideoMode") - 3
		win_height = output_dimensions.slice(ver_res_begin_index..ver_res_end_index).to_i

	else
		if operating_system == "mac"
			output_dimensions = `system_profiler SPDisplaysDataType |grep Resolution`.chomp.tr(" ", "")			
			end_index = output_dimensions.index("Retina") - 1
		elsif operating_system == "linux"
			output_dimensions = `xdpyinfo | grep dimensions`.chomp.tr(" ", "")
			end_index = output_dimensions.index("p") - 1
		end
		start_index = output_dimensions.index(":") + 1
		resolution = output_dimensions.slice(start_index..end_index)
		win_width = resolution.slice(0..resolution.index("x")-1).to_i
		win_height = resolution.slice(resolution.index("x")+1.. resolution.length-1).to_i
	end	

	return [win_width, win_height]
end

.open_file_action(window, img_parameters, button_set) ⇒ Object

action when the “Open file” menu option is clicked



122
123
124
125
126
127
128
129
130
131
132
133
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/wuffl/actions.rb', line 122

def self.open_file_action(window, img_parameters, button_set)
  dialog = Gtk::FileChooserDialog.new(:title => "Open file", :parent => window, :action => Gtk::FileChooserAction::OPEN, 
      :buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT], [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])

  if dialog.run == Gtk::ResponseType::ACCEPT
    filename = dialog.filename
    img_parameters[:dir_path] = File.dirname(filename)
    img_parameters[:selected_path] = img_parameters[:dir_path] + "/Selected"
    img_parameters[:deleted_path] = img_parameters[:dir_path] + "/Deleted"
    all_files = Dir.entries(img_parameters[:dir_path])
    accepted_formats = [".jpg", ".JPG", ".png", ".PNG", ".gif", ".GIF"]
    img_parameters[:is_landscape] = true
    
    all_files.each do |name|
      if accepted_formats.include? File.extname(name)
        img_parameters[:all_orig_img] << name
      end
    end
    img_parameters[:all_orig_img] = img_parameters[:all_orig_img].sort
    just_the_name = File.basename filename

    img_parameters[:all_orig_img].each_with_index do |name, index|
      if just_the_name == name
        img_parameters[:ind] = index
      end
    end

    if File.directory?(img_parameters[:selected_path]) == false
      Dir.mkdir(img_parameters[:selected_path])
      FileUtils.chmod 0777, img_parameters[:selected_path]
    end

    if File.directory?(img_parameters[:deleted_path]) == false
      Dir.mkdir(img_parameters[:deleted_path])
      FileUtils.chmod 0777, img_parameters[:deleted_path]
    end

    # Prebuffer of all files
    img_parameters[:all_orig_img].each do |file|
      current_filename = img_parameters[:dir_path] + "/" + file
      if current_filename.include?("\\")
        current_filename.gsub!("\\", "/")
      end
      img_parameters[:all_orig_pb], img_parameters[:is_landscape] = ImageActions.prepare_pixbuf(current_filename, img_parameters[:all_orig_pb], img_parameters[:img_max_w], img_parameters[:img_max_h], img_parameters[:reduction_factor])
    end
    img_parameters[:pb_current] = img_parameters[:all_orig_pb][img_parameters[:ind]]

    ImageActions.show_img(img_parameters[:img_current], img_parameters[:pb_current])
    window.set_title File.basename filename

    # Activate all buttons
    button_set.each do |button, sens_value|
      button_set[button].sensitive = true
    end

  end
  dialog.destroy

  return img_parameters
end

.pack_boxes(window, img_current, box_set, button_set) ⇒ Object

pack the boxes for the GUI



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wuffl/actions.rb', line 75

def self.pack_boxes(window, img_current, box_set, button_set)
  box_set[:hbox].add button_set[:prev_btn]
  box_set[:hbox].add button_set[:select_btn]
  box_set[:hbox].add button_set[:rotate_btn]
  box_set[:hbox].add button_set[:delete_btn]
  box_set[:hbox].add button_set[:next_btn]
  
  box_set[:halign].add box_set[:hbox]

  box_set[:vbox].pack_start box_set[:mb], :expand => false, :fill => false, :padding => 5
  box_set[:vbox].pack_start img_current, :expand => true, :fill => true, :padding => 5
  box_set[:vbox].pack_start box_set[:halign], :expand => false, :fill => false, :padding => 5   
  

  window.add box_set[:vbox]

  return box_set
end

.prev_next_btn_action(window, img_parameters, previous_or_next_button) ⇒ Object

action when the “previous image” button or the “next image” button is clicked



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/wuffl/actions.rb', line 184

def self.prev_next_btn_action(window, img_parameters, previous_or_next_button)
  if previous_or_next_button == "prev" # previous image
    img_parameters[:ind] = set_prev_index(img_parameters[:ind], img_parameters[:all_orig_img])
  elsif previous_or_next_button == "next" # next button
    img_parameters[:ind] = set_next_index(img_parameters[:ind], img_parameters[:all_orig_img])
  end    
  filename = img_parameters[:dir_path] + "/" + img_parameters[:all_orig_img][img_parameters[:ind]]
  img_parameters[:is_landscape] = ImageActions.img_dimensions_fi(filename)[0]
  img_parameters[:pb_current] = img_parameters[:all_orig_pb][img_parameters[:ind]]
  ImageActions.show_img(img_parameters[:img_current], img_parameters[:pb_current])
  window.set_title File.basename filename

  return img_parameters
end

.rotate_btn_action(img_parameters) ⇒ Object

action when the rotate button is clicked



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/wuffl/actions.rb', line 200

def self.rotate_btn_action(img_parameters)
  if img_parameters[:is_landscape] == false # Original image in portrait format
    img_parameters[:pb_current] = img_parameters[:pb_current].rotate(:clockwise)
    
  else # Original image in landscape format
    if img_parameters[:rotation_case] == 'A'
      img_parameters[:pb_current] = img_parameters[:pb_current].rotate(:clockwise)
      width_pb = img_parameters[:pb_current].width
      height_pb = img_parameters[:pb_current].height
      if height_pb > img_parameters[:img_max_h]
        while height_pb > img_parameters[:img_max_h] do 
          height_pb *= img_parameters[:reduction_factor]
          width_pb *= img_parameters[:reduction_factor]
        end
        height_pb = height_pb.to_i
        width_pb = width_pb.to_i
        img_parameters[:pb_portrait] = img_parameters[:pb_current].scale(width_pb, height_pb, :bilinear)
        img_parameters[:pb_current] = img_parameters[:pb_portrait]
      else
        img_parameters[:pb_portrait] = img_parameters[:pb_current]
      end
      img_parameters[:rotation_case] = 'B'

    elsif img_parameters[:rotation_case] == 'B'
      img_parameters[:pb_current] = img_parameters[:all_orig_pb][img_parameters[:ind]].rotate(:upsidedown)
      img_parameters[:rotation_case] = 'C'

    elsif img_parameters[:rotation_case] == 'C'
      img_parameters[:pb_current] = img_parameters[:pb_portrait].rotate(:upsidedown)
      img_parameters[:rotation_case] = 'D'

    elsif img_parameters[:rotation_case] == 'D'
      img_parameters[:pb_current] = img_parameters[:all_orig_pb][img_parameters[:ind]]
      img_parameters[:rotation_case] = 'A'
    end
  end
  img_parameters[:img_current].set_pixbuf(img_parameters[:pb_current])

  return img_parameters
end

.select_delete_btn_action(window, img_parameters, button_set, select_delete_par) ⇒ Object

action for the select and delete buttons



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/wuffl/actions.rb', line 242

def self.select_delete_btn_action(window, img_parameters, button_set, select_delete_par)
  if img_parameters[:all_orig_img].length > 0

    just_the_name = img_parameters[:all_orig_img][img_parameters[:ind]]
    current_location = img_parameters[:dir_path] + "/" + just_the_name
    if select_delete_par == "select"
      new_location = img_parameters[:selected_path] + "/" + just_the_name
    elsif select_delete_par == "delete"
      new_location = img_parameters[:deleted_path] + "/" + just_the_name
    end
    FileUtils.mv(current_location, new_location) 

    img_parameters[:all_orig_img].delete_at(img_parameters[:ind])
    img_parameters[:all_orig_pb].delete_at(img_parameters[:ind])

    if img_parameters[:all_orig_img].length > 0
      if img_parameters[:ind] == img_parameters[:all_orig_img].length
        img_parameters[:ind] -= 1
      end

      just_the_name = img_parameters[:all_orig_img][img_parameters[:ind]]
      filename = img_parameters[:dir_path] + "/" + just_the_name
      img_parameters[:is_landscape] = ImageActions.img_dimensions_fi(filename)[0]
      img_parameters[:pb_current] = img_parameters[:all_orig_pb][img_parameters[:ind]]
      ImageActions.show_img(img_parameters[:img_current], img_parameters[:pb_current])
      window.set_title just_the_name

      if select_delete_par == "select"
        # Buffer the next image if not all loaded yet
        if img_parameters[:all_orig_pb].length != img_parameters[:all_orig_img].length
          current_filename = img_parameters[:dir_path] + "/" + img_parameters[:all_orig_img][img_parameters[:ind]+1]
          img_parameters[:all_orig_pb], img_parameters[:is_landscape]  = ImageActions.prepare_pixbuf(current_filename, img_parameters[:all_orig_pb], img_parameters[:img_max_w], img_parameters[:img_max_h], img_parameters[:reduction_factor])
        end
      end
    else
      img_parameters[:pb_current] = GdkPixbuf::Pixbuf.new
      img_parameters[:img_current].set_pixbuf(img_parameters[:pb_current])
      deactivate_buttons(button_set)
    end
  else
    img_parameters[:pb_current] = GdkPixbuf::Pixbuf.new
    img_parameters[:img_current].set_pixbuf(img_parameters[:pb_current])
    deactivate_buttons(button_set)
  end
    return img_parameters
end

.set_next_index(index, img_array) ⇒ Object

set the index for the next image



95
96
97
98
99
100
101
102
# File 'lib/wuffl/actions.rb', line 95

def self.set_next_index(index, img_array)
	if (index + 1) <= (img_array.length - 1)
		index += 1
	else
		index = 0
	end
	return index
end

.set_prev_index(index, img_array) ⇒ Object

set the index for the previous image



105
106
107
108
109
110
111
112
# File 'lib/wuffl/actions.rb', line 105

def self.set_prev_index(index, img_array)
	if (index - 1) < 0
		index = img_array.length - 1
	else
		index -= 1
	end
	return index
end