From e05d5e1e5f478985a01b8cf350f307b5c4a90d57 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Tue, 23 Jun 2009 21:49:46 +0200 Subject: [PATCH] Simple linear move list. --- lib/history.rb | 60 +++++++++++++++++++++++++++++---- lib/mainwindow.rb | 10 ++++++ lib/plugins/clocks/digital.rb | 1 + lib/plugins/movelist/simple_movelist.rb | 56 ++++++++++++++++++++++++++++++ lib/qtutils.rb | 28 +++++++++++++++ 5 files changed, 148 insertions(+), 7 deletions(-) create mode 100644 lib/plugins/movelist/simple_movelist.rb diff --git a/lib/history.rb b/lib/history.rb index cdd42dd..e28a9a8 100644 --- a/lib/history.rb +++ b/lib/history.rb @@ -1,16 +1,18 @@ require 'observer_utils.rb' -class History +class History < Qt::AbstractListModel include Enumerable include Observer + include ModelUtils attr_reader :current - Item = Struct.new(:state, :move) + Item = Struct.new(:state, :move, :text) OutOfBound = Class.new(Exception) def initialize(state) - @history = [Item.new(state.dup, nil)] + super(nil) + @history = [Item.new(state.dup, nil, "Mainline")] @current = 0 end @@ -19,10 +21,16 @@ class History end def add_move(state, move) - item = Item.new(state.dup, move) - @history = @history[0..@current] - @history << item - @current = @history.size - 1 + item = Item.new(state.dup, move, nil) + + removing_rows(nil, @current + 1, @history.size - 1) do + @history = @history[0..@current] + end + + inserting_rows(nil, @current + 1, @current + 1) do + @history << item + @current = @history.size - 1 + end end def forward @@ -50,4 +58,42 @@ class History def size @history.size end + + def [](index) + @history[index] + end + + # model interface + + # set a serializer for this model + # if no serializer has been set, it will + # be impossible to use it with a view + def serializer=(ser) + @serializer = ser + end + + def data(index, role) + if @serializer and role == Qt::DisplayRole + unless @history[index.row].text + state = @history[index.row - 1].state + move = @history[index.row].move + san = @serializer.serialize(move, state) + + count = index.row / 2 + 1 + dots = if index.row % 2 == 0 + '.' + else + '...' + end + + @history[index.row].text = "#{count}#{dots} #{san}" + end + @history[index.row].text + end + end + + def rowCount(parent) + size + end + end diff --git a/lib/mainwindow.rb b/lib/mainwindow.rb index de31c25..7fdc277 100644 --- a/lib/mainwindow.rb +++ b/lib/mainwindow.rb @@ -82,6 +82,16 @@ private history = History.new(state) @controller = Controller.new(scene, elements, game, history) + movelist = @loader.get_matching(%w(movelist)). + new(parent, history, game) + + movelist_dock = Qt::DockWidget.new(self) + movelist_dock.widget = movelist + movelist_dock.window_title = KDE.i18n("Move list") + movelist_dock.object_name = "movelist" + add_dock_widget(Qt::LeftDockWidgetArea, movelist_dock, Qt::Vertical) + movelist_dock.show + self.central_widget = table end end diff --git a/lib/plugins/clocks/digital.rb b/lib/plugins/clocks/digital.rb index f1a4749..207d331 100644 --- a/lib/plugins/clocks/digital.rb +++ b/lib/plugins/clocks/digital.rb @@ -1,5 +1,6 @@ require 'plugins/plugin' require 'constrained_text_item' +require 'board/item_bag' class DigitalClock < Qt::GraphicsItemGroup include Plugin diff --git a/lib/plugins/movelist/simple_movelist.rb b/lib/plugins/movelist/simple_movelist.rb new file mode 100644 index 0000000..a6791fb --- /dev/null +++ b/lib/plugins/movelist/simple_movelist.rb @@ -0,0 +1,56 @@ +require 'plugins/plugin' + +class SimpleMoveList < Qt::ListView + include Plugin + + plugin :name => 'Simple Move List', + :keywords => %w(movelist) + + def initialize(parent, history, game, opts = {}) + super(parent) +# self.model = Qt::StringListModel.new +# @history.add_observer(self) +# moves = (1...@history.size).map do |index| +# move(index) +# end +# self.model.set_string_list(moves) + + history.serializer = game.serializer.new(:compact) + self.model = history + +# if opts[:font] + self.font = opts[:font] if opts[:font] +# else +# f = font +# f.point_size = 16 +# self.font = f +# end + end + +# def on_added(index) +# index -= 1 +# self.model.remove_rows(index, @history.size - index - 1) +# self.model.insert_rows(index, 1) +# self.model.set_data(self.model.index(index, 0), +# move(index), +# Qt::DisplayRole) +# end + +# private + +# def move(index) +# state = @history[index].state +# move = @history[index+ 1].move +# san = @serializer.serialize(move, state) +# +# count = index/ 2 + 1 +# dots = if index% 2 == 0 +# '.' +# else +# '...' +# end +# +# "#{count}#{dots} #{san}" +# end +end + diff --git a/lib/qtutils.rb b/lib/qtutils.rb index 420c04f..772cbd1 100644 --- a/lib/qtutils.rb +++ b/lib/qtutils.rb @@ -186,6 +186,34 @@ class Qt::Timer end end +module ModelUtils + def removing_rows(parent, first, last) + if first > last + yield + else + begin + begin_remove_rows(parent || Qt::ModelIndex.new, first, last) + yield + ensure + end_remove_rows + end + end + end + + def inserting_rows(parent, first, last) + if first > last + yield + else + begin + begin_insert_rows(parent || Qt::ModelIndex.new, first, last) + yield + ensure + end_insert_rows + end + end + end +end + class KDE::Application def self.init(data) about = KDE::AboutData.new( -- 2.11.4.GIT