From 42ecc00951bc2ed0115319fe7a69fe13d7001269 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Fri, 19 Jun 2009 21:58:54 +0200 Subject: [PATCH] Add navigation actions. Navigation actions are now in place in the main window. Removed right button navigation in Board. --- lib/board/board.rb | 7 +------ lib/controller.rb | 2 +- lib/mainwindow.rb | 17 +++++++++++++---- lib/qtutils.rb | 24 ++++++++++++++++++++++-- test/test_controller.rb | 11 +++++++---- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/lib/board/board.rb b/lib/board/board.rb index 80f519a..9e72479 100644 --- a/lib/board/board.rb +++ b/lib/board/board.rb @@ -18,7 +18,6 @@ class Board < Qt::GraphicsItemGroup square_tag :selection square_tag :last_move_src, :highlight square_tag :last_move_dst, :highlight -# square_tag :dst, :highlight def initialize(scene, theme, game, state = nil) super(nil, scene) @@ -100,11 +99,7 @@ class Board < Qt::GraphicsItemGroup end def mousePressEvent(e) - if e.button == Qt::RightButton - # go back using the right button - self.selection = nil - fire :back - else + if e.button == Qt::LeftButton p = to_logical(e.pos) if selection diff --git a/lib/controller.rb b/lib/controller.rb index 8fc2f19..fa73bae 100644 --- a/lib/controller.rb +++ b/lib/controller.rb @@ -29,7 +29,7 @@ class Controller def on_forward state, move = @history.forward @board.forward(state.dup, move) - @board.highlight(@history.move) + @board.highlight(move) rescue History::OutOfBound puts "error: last move" end diff --git a/lib/mainwindow.rb b/lib/mainwindow.rb index 70aea68..e5b0520 100644 --- a/lib/mainwindow.rb +++ b/lib/mainwindow.rb @@ -26,7 +26,16 @@ private std_action :open_new do puts "new game" end - std_action :quit, :close + std_action :quit, :slot => :close + regular_action :back, :icon => 'go-previous', + :text => KDE.i18n("&Back") do + @board.fire :back + end + regular_action :forward, :icon => 'go-next', + :text => KDE.i18n("&Forward") do + @board.fire :forward + end + end def load_board(game) @@ -42,12 +51,12 @@ private state = game.state.new.tap {|s| s.setup } - board = Board.new(scene, theme, game, state) + @board = Board.new(scene, theme, game, state) - table = Table.new(scene, self, board) + table = Table.new(scene, self, @board) self.central_widget = table history = History.new(state) - controller = Controller.new(board, history) + controller = Controller.new(@board, history) end end diff --git a/lib/qtutils.rb b/lib/qtutils.rb index 475d702..98d2b25 100644 --- a/lib/qtutils.rb +++ b/lib/qtutils.rb @@ -185,13 +185,33 @@ class KDE::Application end module ActionHandler - def std_action(action, s = nil, &blk) + def std_action(action, opts = {}, &blk) + target, slot = get_slot(opts[:slot], &blk) + KDE::StandardAction.send(action, target, slot, action_collection) + end + + def get_slot(s = nil, &blk) target, slot = if block_given? [Qt::BlockInvocation.new(self, blk, 'invoke()'), SLOT(:invoke)] else [self, SLOT(s)] end + end + + def regular_action(name, opts, &blk) + icon = if opts[:icon] + case opts[:icon] + when Qt::Icon + opts[:icon] + else + KDE::Icon.new(opts[:icon].to_s) + end + end - KDE::StandardAction.send(action, target, slot, action_collection) + KDE::Action.new(icon, opts[:text], self).tap do |a| + action_collection.add_action(name.to_s, a) + target, slot = get_slot(opts[:slot], &blk) + connect(a, SIGNAL('triggered(bool)'), target, slot) + end end end diff --git a/test/test_controller.rb b/test/test_controller.rb index 32587d9..bf9f1d7 100644 --- a/test/test_controller.rb +++ b/test/test_controller.rb @@ -13,19 +13,22 @@ class TestController < Test::Unit::TestCase def test_on_new_move @history.expects(:add_move).once.with('state', 'move') - @board.changed - @board.notify_observers :new_move => { :state => 'state', :move => 'move' } + @board.expects(:highlight).once.with('move') + @board.fire :new_move => { :state => 'state', :move => 'move' } end def test_back @history.expects(:back).returns(['state', 'move']) + @history.expects(:move).returns('last_move') @board.expects(:back).with('state', 'move') - @controller.on_back + @board.expects(:highlight).once.with('last_move') + @board.fire :back end def test_forward @history.expects(:forward).returns(['state', 'move']) @board.expects(:forward).with('state', 'move') - @controller.on_forward + @board.expects(:highlight).once.with('move') + @board.fire :forward end end -- 2.11.4.GIT