lib/alexandria/ui/listview.rb in alexandria-book-collection-manager-0.6.9 vs lib/alexandria/ui/listview.rb in alexandria-book-collection-manager-0.7.0
- old
+ new
@@ -1,9 +1,9 @@
# Copyright (C) 2004-2006 Laurent Sansonetti
# Copyright (C) 2008 Joseph Method
# Copyright (C) 2010 Cathal Mc Ginley
-# Copyright (C) 2011 Matijs van Zuijlen
+# Copyright (C) 2011, 2016 Matijs van Zuijlen
#
# Alexandria is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License aso
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
@@ -25,11 +25,11 @@
GetText.bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
class ListViewManager
include Logging
include GetText
include DragAndDropable
- BOOKS_TARGET_TABLE = [['ALEXANDRIA_BOOKS', Gtk::Drag::TARGET_SAME_APP, 0]]
+ BOOKS_TARGET_TABLE = [['ALEXANDRIA_BOOKS', :same_app, 0]].freeze
MAX_RATING_STARS = 5
module Columns
COVER_LIST, COVER_ICON, TITLE, TITLE_REDUCED, AUTHORS,
ISBN, PUBLISHER, PUBLISH_DATE, EDITION, RATING, IDENT,
@@ -49,72 +49,38 @@
def setup_title_column
title = _('Title')
log.debug { 'Create listview column for %s' % title }
column = Gtk::TreeViewColumn.new(title)
- column.widget = Gtk::Label.new(title).show
+
renderer = Gtk::CellRendererPixbuf.new
column.pack_start(renderer, false)
- column.set_cell_data_func(renderer) do |_col, cell, _model, iter|
- iter = @listview_model.convert_iter_to_child_iter(iter)
- iter = @filtered_model.convert_iter_to_child_iter(iter)
- cell.pixbuf = iter[Columns::COVER_LIST]
- end
- renderer = Gtk::CellRendererText.new
- renderer.ellipsize = Pango::ELLIPSIZE_END if Pango.ellipsizable?
- # Editable tree views are behaving strangely
- # make_renderer_editable renderer
+ column.add_attribute(renderer, 'pixbuf', Columns::COVER_LIST)
+ renderer = Gtk::CellRendererText.new
+ renderer.ellipsize = Pango::ELLIPSIZE_END
column.pack_start(renderer, true)
+ column.add_attribute(renderer, 'text', Columns::TITLE)
- column.set_cell_data_func(renderer) do |_col, cell, _model, iter|
- iter = @listview_model.convert_iter_to_child_iter(iter)
- iter = @filtered_model.convert_iter_to_child_iter(iter)
- cell.text = iter[Columns::TITLE]
- cell.editable = false # true
- end
-
column.sort_column_id = Columns::TITLE
column.resizable = true
@listview.append_column(column)
end
- def make_renderer_editable(renderer)
- renderer.signal_connect('editing_started') do |_cell, entry, _path_string|
- log.debug { 'editing_started' }
- entry.complete_titles
- end
-
- renderer.signal_connect('edited') do |_cell, path_string, new_string|
- log.debug { 'edited' }
- path = Gtk::TreePath.new(path_string)
- path = @listview_model.convert_path_to_child_path(path)
- path = @filtered_model.convert_path_to_child_path(path)
- iter = @listview.model.get_iter(path)
- book = @parent.book_from_iter(@parent.selected_library, iter)
- book.title = new_string
- @listview.freeze
- @iconview.freeze
- @parent.fill_iter_with_book(iter, book)
- @iconview.unfreeze
- @listview.unfreeze
- end
- end
-
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
def setup_books_listview
log.debug { 'setup_books_listview' }
@listview.model = @listview_model
setup_title_column
@@ -123,60 +89,53 @@
end
CHECK_COLUMNS.each do |title, iterid|
setup_check_column title, iterid
end
setup_rating_column
- @listview.selection.mode = Gtk::SELECTION_MULTIPLE
+ @listview.selection.mode = :multiple
@listview.selection.signal_connect('changed') do
log.debug { 'changed' }
@parent.on_books_selection_changed
end
setup_tags_column
- setup_listview_hack
+ setup_row_activation
setup_view_source_dnd(@listview)
end
def setup_tags_column
# adding tags column...
title = _('Tags')
log.debug { 'Create listview column for tags...' }
renderer = Gtk::CellRendererText.new
- renderer.ellipsize = Pango::ELLIPSIZE_END if Pango.ellipsizable?
+ renderer.ellipsize = Pango::ELLIPSIZE_END
column = Gtk::TreeViewColumn.new(title, renderer,
text: Columns::TAGS)
- column.widget = Gtk::Label.new(title).show
column.sort_column_id = Columns::TAGS
column.resizable = true
@listview.append_column(column)
end
- def setup_listview_hack
+ def setup_row_activation
@listview.signal_connect('row-activated') do
- # Dirty hack to avoid the beginning of a drag within this
- # handler.
log.debug { 'row-activated' }
- Gtk.timeout_add(100) do
- @actiongroup['Properties'].activate
- false
- end
+ @actiongroup['Properties'].activate
+ false
end
end
def setup_rating_column
title = _('Rating')
log.debug { 'Create listview column for %s...' % title }
column = Gtk::TreeViewColumn.new(title)
- column.widget = Gtk::Label.new(title).show
- column.sizing = Gtk::TreeViewColumn::FIXED
- column.fixed_width = column.min_width = column.max_width =
- (Icons::STAR_SET.width + 1) * MAX_RATING_STARS
+ column.sizing = :fixed
+ width = (Icons::STAR_SET.width + 1) * MAX_RATING_STARS
+ column.fixed_width = column.min_width = column.max_width = width
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 |_col, cell, _model, iter|
- iter = @listview_model.convert_iter_to_child_iter(iter)
- iter = @filtered_model.convert_iter_to_child_iter(iter)
+ column.set_cell_data_func(renderer) do |_tree_column, cell, _tree_model, iter|
rating = (iter[Columns::RATING] - MAX_RATING_STARS).abs
cell.pixbuf = rating >= i.succ ?
Icons::STAR_SET : Icons::STAR_UNSET
end
end
@@ -223,45 +182,29 @@
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.widget = Gtk::Label.new(title).show
column.sort_column_id = iterid
column.resizable = true
log.debug { 'Create listview column for %s...' % title }
- setup_column = proc do |iter, cell, col|
- state = iter[col]
- cell.set_active(state)
- cell.activatable = true
+
+ column.add_attribute(renderer, 'active', iterid)
+ if iterid == Columns::WANT
+ column.add_attribute(renderer, 'inconsistent', Columns::OWN)
end
- log.debug { "Setting cell_data_func for #{renderer}" }
- column.set_cell_data_func(renderer) do |_col, cell, _model, iter|
- iter = @listview_model.convert_iter_to_child_iter(iter)
- iter = @filtered_model.convert_iter_to_child_iter(iter)
- case iterid
- when 12
- setup_column.call(iter, cell, Columns::REDD)
- when 13
- setup_column.call(iter, cell, Columns::OWN)
- when 14
- setup_column.call(iter, cell, Columns::WANT)
- own_state = iter[Columns::OWN]
- cell.inconsistent = own_state
- end
- end
+
log.debug { "append_column #{column}" }
@listview.append_column(column)
end
def setup_text_column(title, iterid)
log.debug { 'Create listview column for %s...' % title }
renderer = Gtk::CellRendererText.new
- renderer.ellipsize = Pango::ELLIPSIZE_END if Pango.ellipsizable?
+ renderer.ellipsize = Pango::ELLIPSIZE_END
column = Gtk::TreeViewColumn.new(title, renderer,
text: iterid)
- column.widget = Gtk::Label.new(title).show
column.sort_column_id = iterid
column.resizable = true
@listview.append_column(column)
end
@@ -296,11 +239,13 @@
cols_width = YAML.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]}" }
- c.sizing = Gtk::TreeViewColumn::FIXED
- c.fixed_width = 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