Class: Alexandria::UI::BookPropertiesDialogBase

Inherits:
BuilderBase
  • Object
show all
Extended by:
GetText
Includes:
Logging, CalendarPopup, GetText
Defined in:
lib/alexandria/ui/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 =
Dir.home

Instance Method Summary collapse

Methods included from Logging

included, #log

Methods included from CalendarPopup

#assign_selected_date, #clear_date_entry, #display_calendar_popup, #setup_calendar_widgets

Constructor Details

#initialize(parent, cover_file) ⇒ BookPropertiesDialogBase

Returns a new instance of BookPropertiesDialogBase.



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

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_date_widgets
  @block_calendar_popup = false
end

Instance Method Details

#on_add_authorObject



107
108
109
110
111
112
113
114
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 107

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



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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 152

def on_change_cover
  dialog = Gtk::FileChooserDialog.new(title: _("Select a cover image"),
                                      parent: @book_properties_dialog,
                                      action: :open,
                                      buttons: [[_("No Cover"), :reject],
                                                [Gtk::Stock::CANCEL, :cancel],
                                                [Gtk::Stock::OPEN, :accept]])
  dialog.current_folder = @@latest_filechooser_directory
  response = dialog.run
  case response
  when Gtk::ResponseType::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) && !@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
      if cover.height > COVER_ABSOLUTE_MAXHEIGHT
        FileUtils.cp(dialog.filename, "#{@cover_file}.orig")
        new_width = cover.width / (cover.height / COVER_ABSOLUTE_MAXHEIGHT.to_f)
        log.info do
          "Scaling large cover image to " \
            "#{new_width.to_i} x #{COVER_ABSOLUTE_MAXHEIGHT}"
        end
        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 => ex
      ErrorDialog.new(@book_properties_dialog, ex.message).display
    end
  when Gtk::ResponseType::REJECT
    ## FileUtils.rm_f(@cover_file) # fixing bug #16707
    @delete_cover_file = true

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

#on_destroyObject



200
201
202
203
204
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 200

def on_destroy
  @book_properties_dialog.hide
  # Stop notebook trying to set tab labels at this time
  @notebook.show_tabs = false
end

#on_image_no_rating_pressObject



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

def on_image_no_rating_press
  self.rating = 0
end

#on_image_rating1_pressObject



121
122
123
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 121

def on_image_rating1_press
  self.rating = 1
end

#on_image_rating2_pressObject



125
126
127
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 125

def on_image_rating2_press
  self.rating = 2
end

#on_image_rating3_pressObject



129
130
131
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 129

def on_image_rating3_press
  self.rating = 3
end

#on_image_rating4_pressObject



133
134
135
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 133

def on_image_rating4_press
  self.rating = 4
end

#on_image_rating5_pressObject



137
138
139
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 137

def on_image_rating5_press
  self.rating = 5
end

#on_loanedObject



206
207
208
209
210
211
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 206

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



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/alexandria/ui/book_properties_dialog_base.rb', line 213

def on_loaned_date_changed
  date_regexes =  [%r{[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



116
117
118
119
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 116

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

#on_title_changedObject



98
99
100
101
102
103
104
105
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 98

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



145
146
147
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 145

def own_toggled
  @checkbutton_want.inconsistent = @checkbutton_own.active?
end

#redd_toggledObject



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 241

def redd_toggled
  if @checkbutton_redd.active?
    @redd_date.sensitive = true

    return if @block_calendar_popup

    display_calendar_popup(@redd_date) if @redd_date.text.strip.empty?
  else
    @redd_date.sensitive = false
  end
end

#setup_date_widgetsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 66

def setup_date_widgets
  @redd_date.signal_connect("icon-press") do |entry, primary, _icon|
    case primary.nick
    when "primary"
      display_calendar_popup(entry)
    when "secondary"
      clear_date_entry(entry)
    end
  end

  @date_loaned_since.signal_connect("icon-press") do |entry, primary, _icon|
    case primary.nick
    when "primary"
      display_calendar_popup(entry)
    when "secondary"
      clear_date_entry(entry)
      @label_loaning_duration.label = ""
    end
  end
end

#showObject



62
63
64
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 62

def show
  @book_properties_dialog.show
end

#want_toggledObject



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

def want_toggled; end

#widget_namesObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/alexandria/ui/book_properties_dialog_base.rb', line 87

def widget_names
  [:book_properties_dialog, :button_box, :button_cover,
   :checkbutton_loaned, :checkbutton_own, :checkbutton_redd,
   :checkbutton_want, :date_loaned_since, :entry_edition,
   :entry_loaned_to, :entry_publish_date, :entry_publisher, :entry_isbn,
   :entry_tags, :entry_title, :image_cover, :image_rating1,
   :image_rating2, :image_rating3, :image_rating4, :image_rating5,
   :label_loaning_duration, :notebook, :redd_date, :textview_notes,
   :treeview_authors]
end