Class: Alexandria::UI::BookPropertiesDialogBase

Inherits:
BuilderBase
  • Object
show all
Extended by:
GetText
Includes:
GetText
Defined in:
lib/alexandria/ui/dialogs/book_properties_dialog_base.rb

Direct Known Subclasses

BookPropertiesDialog, NewBookDialogManual

Constant Summary collapse

COVER_MAXWIDTH =

pixels

140
COVER_ABSOLUTE_MAXHEIGHT =

pixels, above this we scale down…

250
@@latest_filechooser_directory =

Instance Method Summary collapse

Constructor Details

#initialize(parent, cover_file) ⇒ BookPropertiesDialogBase

Returns a new instance of BookPropertiesDialogBase.



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
73
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 34

def initialize(parent, cover_file)
  super('book_properties_dialog__builder.glade', widget_names)
  @setup_finished = false
  @book_properties_dialog.transient_for = parent
  @parent = parent
  @cover_file = cover_file
  @original_cover_file = nil
  @delete_cover_file = false # fixing bug #16707

  @entry_title.complete_titles
  @entry_title.grab_focus
  @entry_publisher.complete_publishers
  @entry_edition.complete_editions
  @entry_loaned_to.complete_borrowers

  @entry_tags.complete_tags

  @treeview_authors.model = Gtk::ListStore.new(String, TrueClass)
  @treeview_authors.selection.mode = :single
  renderer = Gtk::CellRendererText.new
  renderer.signal_connect('edited') do |_cell, path_string, new_text|
    path = Gtk::TreePath.new(path_string)
    iter = @treeview_authors.model.get_iter(path)
    iter[0] = new_text
  end
  renderer.signal_connect('editing_started') do |_cell, entry, _path_string|
    entry.complete_authors
  end
  col = Gtk::TreeViewColumn.new('', renderer,
                                text: 0,
                                editable: 1)
  @treeview_authors.append_column(col)

  setup_calendar_widgets
  GLib::Timeout.add(150) do
    @setup_finished = true

    false
  end
end

Instance Method Details

#clear_date_entry(entry) ⇒ Object



140
141
142
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 140

def clear_date_entry(entry)
  entry.text = ''
end

#display_calendar_popup(entry) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 167

def display_calendar_popup(entry)
  if @popup_displayed
    hide_calendar_popup
  else
    @calendar_popup_for_entry = entry
    unless entry.text.strip.empty?
      time = parse_date(entry.text)
      unless time.nil?
        @calendar.year = time.year
        @calendar.month = time.month - 1
        @calendar.day = time.day
      end
    end
    @book_properties_dialog.modal = false
    @calendar_popup.move(*get_entry_popup_coords(entry))
    @calendar_popup.show_all
    @popup_displayed = true
  end
end

#get_entry_popup_coords(entry) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 187

def get_entry_popup_coords(entry)
  gdk_win = entry.parent_window
  x, y = gdk_win.origin
  alloc = entry.allocation
  x += alloc.x
  y += alloc.y
  y += alloc.height
  # x = [0, x].max
  # y = [0, y].max
  [x, y]
end

#hide_calendar_popupObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 144

def hide_calendar_popup
  @calendar_popup_for_entry = nil

  @calendar_popup.hide_all
  @book_properties_dialog.modal = true

  GLib::Timeout.add(150) do
    # If we set @popup_displayed=false immediately, then a click
    # event on the primary icon of the Entry simultaneous with
    # the focus-out-event of the Calendar causes the Calendar to
    # pop up again milliseconds after being closed.
    #
    # This is never what the user intends.
    #
    # So we add a small delay before the primary icon's event
    # handler is told to pop up the calendar in response to
    # clicks.

    @popup_displayed = false
    false
  end
end

#on_add_authorObject



227
228
229
230
231
232
233
234
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 227

def on_add_author
  iter = @treeview_authors.model.append
  iter[0] = _('Author')
  iter[1] = true
  @treeview_authors.set_cursor(iter.path,
                               @treeview_authors.get_column(0),
                               true)
end

#on_change_coverObject



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 276

def on_change_cover
  backend = `uname`.chomp == 'FreeBSD' ? 'neant' : 'gnome-vfs'
  dialog = Gtk::FileChooserDialog.new(_('Select a cover image'),
                                      @book_properties_dialog,
                                      Gtk::FileChooser::ACTION_OPEN,
                                      backend,
                                      [_('No Cover'), :reject],
                                      [Gtk::Stock::CANCEL, :cancel],
                                      [Gtk::Stock::OPEN, :accept])
  dialog.current_folder = @@latest_filechooser_directory
  response = dialog.run
  if response == :accept
    begin
      @delete_cover_file = false
      cover = GdkPixbuf::Pixbuf.new(file: dialog.filename)
      # At this stage the file format is recognized.

      if File.exist?(@cover_file)
        unless @original_cover_file
          # make a back up, but only of the original
          @original_cover_file = "#{@cover_file}~"
          FileUtils.cp(@cover_file, @original_cover_file)
        end
      end
      if cover.height > COVER_ABSOLUTE_MAXHEIGHT
        FileUtils.cp(dialog.filename, "#{@cover_file}.orig")
        new_width = cover.width / (cover.height / COVER_ABSOLUTE_MAXHEIGHT.to_f)
        puts "Scaling large cover image to #{new_width.to_i} x #{COVER_ABSOLUTE_MAXHEIGHT}"
        cover = cover.scale(new_width.to_i, COVER_ABSOLUTE_MAXHEIGHT)
        cover.save(@cover_file, 'jpeg')
      else
        FileUtils.cp(dialog.filename, @cover_file)
      end

      self.cover = cover
      @@latest_filechooser_directory = dialog.current_folder
    rescue RuntimeError => e
      ErrorDialog.new(@book_properties_dialog, e.message)
    end
  elsif response == :reject
    ## FileUtils.rm_f(@cover_file) # fixing bug #16707
    @delete_cover_file = true

    self.cover = Icons::BOOK_ICON
  end
  dialog.destroy
end

#on_destroyObject

no action by default



324
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 324

def on_destroy; end

#on_image_no_rating_pressObject



261
262
263
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 261

def on_image_no_rating_press
  self.rating = 0
end

#on_image_rating1_pressObject



241
242
243
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 241

def on_image_rating1_press
  self.rating = 1
end

#on_image_rating2_pressObject



245
246
247
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 245

def on_image_rating2_press
  self.rating = 2
end

#on_image_rating3_pressObject



249
250
251
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 249

def on_image_rating3_press
  self.rating = 3
end

#on_image_rating4_pressObject



253
254
255
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 253

def on_image_rating4_press
  self.rating = 4
end

#on_image_rating5_pressObject



257
258
259
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 257

def on_image_rating5_press
  self.rating = 5
end

#on_loanedObject



326
327
328
329
330
331
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 326

def on_loaned
  loaned = @checkbutton_loaned.active?
  @entry_loaned_to.sensitive = loaned
  @date_loaned_since.sensitive = loaned
  @label_loaning_duration.visible = loaned
end

#on_loaned_date_changedObject



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 333

def on_loaned_date_changed
  date_regexes =  [/[0123]?[0-9]\/[0123]?[0-9]\/[0-9]{4}/,
                   /[0-9]{4}-[0123]?[0-9]-[0123]?[0-9]/]
  matches_regex = false
  date_regexes.each do |regex|
    matches_regex = regex.match(@date_loaned_since.text)
    break if matches_regex
  end
  return unless matches_regex
  t = parse_date(@date_loaned_since.text)
  if t.nil?
    @label_loaning_duration.label = ''
    return
  end
  loaned_time = Time.at(t)
  n_days = ((Time.now - loaned_time) / (3600 * 24)).to_i
  if n_days > 365_250 # 1,000 years
    @label_loaning_duration.label = ''
    return
  end
  @label_loaning_duration.label = if n_days > 0
                                    n_('%d day', '%d days', n_days) % n_days
                                  else
                                    ''
                                  end
end

#on_remove_authorObject



236
237
238
239
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 236

def on_remove_author
  iter = @treeview_authors.selection.selected
  @treeview_authors.model.remove(iter) if iter
end

#on_title_changedObject



218
219
220
221
222
223
224
225
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 218

def on_title_changed
  title = @entry_title.text.strip
  @book_properties_dialog.title = if title.empty?
                                    _('Properties')
                                  else
                                    _("Properties for '%s'") % title
                                  end
end

#own_toggledObject



265
266
267
268
269
270
271
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 265

def own_toggled
  @checkbutton_want.inconsistent = if @checkbutton_own.active?
                                     true
                                   else
                                     false
                                   end
end

#redd_toggledObject



360
361
362
363
364
365
366
367
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 360

def redd_toggled
  redd_yes = @checkbutton_redd.active?
  @redd_date.sensitive = redd_yes
  if @setup_finished
    # don't do this when popping up the dialog for the first time
    display_calendar_popup(@redd_date) if redd_yes && @redd_date.text.strip.empty?
  end
end

#setup_calendar_widgetsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 75

def setup_calendar_widgets
  @popup_displayed = false
  @calendar_popup = Gtk::Window.new # Gtk::Window::POPUP)
  # @calendar_popup.modal = true
  @calendar_popup.decorated = false
  @calendar_popup.skip_taskbar_hint = true
  @calendar_popup.skip_pager_hint = true
  @calendar_popup.events = [:focus_change_mask]

  @calendar_popup.set_transient_for(@book_properties_dialog)
  @calendar_popup.set_type_hint :dialog
  @calendar_popup.name = 'calendar-popup'
  @calendar_popup.resizable = false
  # @calendar_popup.border_width = 4
  # @calendar_popup.app_paintable = true

  @calendar_popup.signal_connect('focus-out-event') do |_popup, _event|
    hide_calendar_popup
    false
  end

  @calendar = Gtk::Calendar.new
  @calendar_popup.add(@calendar)

  @calendar.signal_connect('day-selected') do
    date_arr = @calendar.date
    year = date_arr[0]
    month = date_arr[1] # + 1 # gtk : months 0-indexed, Time.gm : 1-index
    day = date_arr[2]
    if @calendar_popup_for_entry
      time = Time.gm(year, month, day)
      @calendar_popup_for_entry.text = format_date(time)
    end
  end

  @calendar.signal_connect('day-selected-double-click') do
    date_arr = @calendar.date
    year = date_arr[0]
    month = date_arr[1] # + 1 # gtk : months 0-indexed, Time.gm : 1-index
    day = date_arr[2]
    if @calendar_popup_for_entry
      time = Time.gm(year, month, day)
      @calendar_popup_for_entry.text = format_date(time)
    end
    hide_calendar_popup
  end

  @redd_date.signal_connect('icon-press') do |entry, primary, _icon|
    if primary.nick == 'primary'
      display_calendar_popup(entry)
    elsif primary.nick == 'secondary'
      clear_date_entry(entry)
    end
  end

  @date_loaned_since.signal_connect('icon-press') do |entry, primary, _icon|
    if primary.nick == 'primary'
      display_calendar_popup(entry)
    elsif primary.nick == 'secondary'
      clear_date_entry(entry)
      @label_loaning_duration.label = ''
    end
  end
end

#want_toggledObject



273
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 273

def want_toggled; end

#widget_namesObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/alexandria/ui/dialogs/book_properties_dialog_base.rb', line 199

def widget_names
  [:book_properties_dialog, :dialog_vbox1, :button_box,
   :notebook1, :hbox1, :table1, :label1, :label7, :entry_title,
   :entry_publisher, :label5, :entry_isbn, :hbox3,
   :scrolledwindow2, :treeview_authors, :vbox2, :button3,
   :image2, :button4, :image3, :label3, :label9, :entry_edition,
   :label16, :entry_publish_date, :label17, :entry_tags,
   :vseparator1, :vbox1, :label12, :button_cover, :image_cover,
   :vbox4, :vbox5, :checkbutton_own, :vbox6, :checkbutton_redd,
   :redd_date, :checkbutton_want, :eventbox8, :hbox2,
   :eventbox6, :image5, :eventbox1, :image_rating1, :eventbox5,
   :image_rating2, :eventbox4, :image_rating3, :eventbox3,
   :image_rating4, :eventbox2, :image_rating5, :eventbox7,
   :image4, :label11, :label9, :vbox3, :checkbutton_loaned,
   :table2, :entry_loaned_to, :label_loaning_duration, :label15,
   :label14, :date_loaned_since, :label13, :scrolledwindow1,
   :textview_notes, :label10]
end