Class: Alexandria::UI::ListViewManager

Inherits:
Object
  • Object
show all
Includes:
Logging, DragAndDropable, GetText
Defined in:
lib/alexandria/ui/listview.rb

Constant Summary collapse

BOOKS_TARGET_TABLE =
[['ALEXANDRIA_BOOKS', :same_app, 0]].freeze
TEXT_COLUMNS =
[
  [_('Authors'), Columns::AUTHORS],
  [_('ISBN'), Columns::ISBN],
  [_('Publisher'), Columns::PUBLISHER],
  [_('Publish Year'), Columns::PUBLISH_DATE],
  [_('Binding'), Columns::EDITION],
  [_('Loaned To'), Columns::LOANED_TO]
].freeze
CHECK_COLUMNS =
[
  [_('Read'), Columns::REDD],
  [_('Own'), Columns::OWN],
  [_('Want'), Columns::WANT]
].freeze

Constants included from DragAndDropable

DragAndDropable::BADGE_MARKUP

Instance Method Summary collapse

Methods included from DragAndDropable

#setup_view_source_dnd

Methods included from Logging

included, #log

Constructor Details

#initialize(_listview, parent) ⇒ ListViewManager

Returns a new instance of ListViewManager.



36
37
38
39
40
41
42
43
44
45
# File 'lib/alexandria/ui/listview.rb', line 36

def initialize(_listview, parent)
  @parent = parent
  @prefs = @parent.prefs
  @listview = @parent.listview
  @listview_model = @parent.listview_model
  @filtered_model = @parent.filtered_model
  @model = @parent.model
  @actiongroup = @parent.actiongroup
  setup_books_listview
end

Instance Method Details

#setup_books_listviewObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/alexandria/ui/listview.rb', line 80

def setup_books_listview
  log.debug { 'setup_books_listview' }
  @listview.model = @listview_model
  setup_title_column
  TEXT_COLUMNS.each do |title, iterid|
    setup_text_column title, iterid
  end
  CHECK_COLUMNS.each do |title, iterid|
    setup_check_column title, iterid
  end
  setup_rating_column
  @listview.selection.mode = :multiple
  @listview.selection.signal_connect('changed') do
    log.debug { 'changed' }
    @parent.on_books_selection_changed
  end
  setup_tags_column
  setup_row_activation
  setup_view_source_dnd(@listview)
end

#setup_check_column(title, iterid) ⇒ Object



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
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/alexandria/ui/listview.rb', line 144

def setup_check_column(title, iterid)
  renderer = CellRendererToggle.new
  renderer.activatable = true
  renderer.signal_connect('toggled') do |_rndrr, path|
    begin
      tree_path = Gtk::TreePath.new(path)
      child_path = @listview_model.convert_path_to_child_path(tree_path)
      if child_path
        unfiltered_path = @filtered_model.convert_path_to_child_path(child_path)
        # FIX this sometimes returns a nil path for iconview...
        if unfiltered_path
          iter = @model.get_iter(unfiltered_path)
          if iter
            book = @parent.book_from_iter(@parent.selected_library, iter)
            toggle_state = case iterid
                           when Columns::REDD then book.redd
                           when Columns::OWN then book.own
                           when Columns::WANT then book.want
                           end
            # invert toggle_state
            unless iterid == Columns::WANT && book.own
              toggle_state = !toggle_state
              case iterid
              when Columns::REDD then book.redd = toggle_state
              when Columns::OWN then book.own = toggle_state
              when Columns::WANT then book.want = toggle_state
              end
              iter[iterid] = toggle_state
              lib = @parent.selected_library
              lib.save(book)
            end
          end
        end

      end
    rescue => e
      log.error { "toggle failed for path #{path} #{e}\n" + e.backtrace.join("\n") }
    end
  end
  column = Gtk::TreeViewColumn.new(title, renderer, text: iterid)
  column.sort_column_id = iterid
  column.resizable = true
  log.debug { format('Create listview column for %s...', title) }

  column.add_attribute(renderer, 'active', iterid)
  column.add_attribute(renderer, 'inconsistent', Columns::OWN) if iterid == Columns::WANT

  log.debug { "append_column #{column}" }
  @listview.append_column(column)
end

#setup_listview_columns_visibilityObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/alexandria/ui/listview.rb', line 206

def setup_listview_columns_visibility
  log.debug { 'setup_listview_columns_visibility' }
  # Show or hide list view columns according to the preferences.
  cols_visibility = [
    @prefs.col_authors_visible,
    @prefs.col_isbn_visible,
    @prefs.col_publisher_visible,
    @prefs.col_publish_date_visible,
    @prefs.col_edition_visible,
    @prefs.col_loaned_to_visible,
    @prefs.col_redd_visible,
    @prefs.col_own_visible,
    @prefs.col_want_visible,
    @prefs.col_rating_visible,
    @prefs.col_tags_visible
  ]
  cols = @listview.columns[1..-1] # skip "Title"
  cols.each_index do |i|
    cols[i].visible = cols_visibility[i]
  end
  log.debug { 'Columns visibility: ' + cols.map { |col| "#{col.title} #{col.visible?}" }.join(', ') }
end

#setup_listview_columns_widthObject

Sets the width of each column based on any respective preference value stored.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/alexandria/ui/listview.rb', line 231

def setup_listview_columns_width
  log.debug { "setup_listview_columns_width #{@prefs.cols_width}" }
  if @prefs.cols_width
    cols_width = YAML.safe_load(@prefs.cols_width)
    log.debug { "cols_width: #{cols_width.inspect}" }
    @listview.columns.each do |c|
      if cols_width.key?(c.title)
        log.debug { "#{c.title} : #{cols_width[c.title]}" }
        width = cols_width[c.title]
        next if width.zero?
        c.sizing = :fixed
        c.fixed_width = width
      end
    end
  end
  log.debug {
    'Columns width: ' +
      @listview.columns.map { |col| "#{col.title} #{col.width}" }.join(', ')
  }
end

#setup_rating_columnObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/alexandria/ui/listview.rb', line 122

def setup_rating_column
  title = _('Rating')
  log.debug { format('Create listview column for %s...', title) }
  column = Gtk::TreeViewColumn.new(title)
  column.sizing = :fixed
  width = (Icons::STAR_SET.width + 1) * Book::MAX_RATING_STARS
  column.fixed_width = column.min_width = column.max_width = width
  Book::MAX_RATING_STARS.times do |i|
    renderer = Gtk::CellRendererPixbuf.new
    renderer.xalign = 0.0
    column.pack_start(renderer, false)
    column.set_cell_data_func(renderer) do |_tree_column, cell, _tree_model, iter|
      rating = (iter[Columns::RATING] - Book::MAX_RATING_STARS).abs
      cell.pixbuf = rating >= i.succ ?
        Icons::STAR_SET : Icons::STAR_UNSET
    end
  end
  column.sort_column_id = Columns::RATING
  column.resizable = false
  @listview.append_column(column)
end

#setup_row_activationObject



114
115
116
117
118
119
120
# File 'lib/alexandria/ui/listview.rb', line 114

def setup_row_activation
  @listview.signal_connect('row-activated') do
    log.debug { 'row-activated' }
    @actiongroup['Properties'].activate
    false
  end
end

#setup_tags_columnObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/alexandria/ui/listview.rb', line 101

def setup_tags_column
  # adding tags column...
  title = _('Tags')
  log.debug { 'Create listview column for tags...' }
  renderer = Gtk::CellRendererText.new
  renderer.ellipsize = :end
  column = Gtk::TreeViewColumn.new(title, renderer,
                                   text: Columns::TAGS)
  column.sort_column_id = Columns::TAGS
  column.resizable = true
  @listview.append_column(column)
end

#setup_text_column(title, iterid) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/alexandria/ui/listview.rb', line 195

def setup_text_column(title, iterid)
  log.debug { format('Create listview column for %s...', title) }
  renderer = Gtk::CellRendererText.new
  renderer.ellipsize = :end
  column = Gtk::TreeViewColumn.new(title, renderer,
                                   text: iterid)
  column.sort_column_id = iterid
  column.resizable = true
  @listview.append_column(column)
end

#setup_title_columnObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/alexandria/ui/listview.rb', line 47

def setup_title_column
  title = _('Title')
  log.debug { format('Create listview column for %s', title) }
  column = Gtk::TreeViewColumn.new(title)

  renderer = Gtk::CellRendererPixbuf.new
  column.pack_start(renderer, false)
  column.add_attribute(renderer, 'pixbuf', Columns::COVER_LIST)

  renderer = Gtk::CellRendererText.new
  renderer.ellipsize = :end
  column.pack_start(renderer, true)
  column.add_attribute(renderer, 'text', Columns::TITLE)

  column.sort_column_id = Columns::TITLE
  column.resizable = true
  @listview.append_column(column)
end