Created
November 3, 2014 16:48
-
-
Save nmeylan/cf499daadc41ea122139 to your computer and use it in GitHub Desktop.
Ruby qt binding sample app (graphics view/scene)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Nicolas Meylan | |
# Date: 22.10.14 | |
# Encoding: UTF-8 | |
# File: qt_app.rb | |
require 'Qt' | |
class QtApp < Qt::Widget | |
# Constants | |
MINERAL_COLORS = {A: '#006BFF', B: '#00ff45', V: '#e4e4e4', AGENT: '#A1499E'} | |
# Avoid Memory leaks | |
BLACK = Qt::Pen.new(Qt::Color.new('#000000')) | |
GRAY = Qt::Pen.new(Qt::Color.new('#e4e4e4')) | |
FIRST_LABEL_POINT = Qt::Point.new(0, 30) | |
SECOND_LABEL_POINT = Qt::Point.new(0, 60) | |
THIRD_LABEL_POINT = Qt::Point.new(0, 90) | |
COLORS_BRUSHES = {A: Qt::Brush.new(Qt::Color.new(MINERAL_COLORS[:A])), | |
B: Qt::Brush.new(Qt::Color.new(MINERAL_COLORS[:B])), | |
AGENT: Qt::Brush.new(Qt::Color.new(MINERAL_COLORS[:AGENT])), | |
} | |
slots 'refresh_ui()' | |
def initialize(environment) | |
super() | |
move 300, 300 | |
@graphic_scene = Qt::GraphicsScene.new(self) | |
@graphic_view = Qt::GraphicsView.new(@graphic_scene,self) | |
@graphic_view.resize(600, 600) | |
@graphic_view.viewport_update_mode = 3 | |
setWindowTitle "Collective Sorting" | |
@refresh_ui_timer = Qt::Timer.new(@graphic_scene) | |
connect(@refresh_ui_timer, SIGNAL('timeout()'), self, SLOT('refresh_ui()')) | |
@refresh_ui_timer.interval = 16 | |
@refresh_ui_timer.start | |
@environment = environment | |
@old_store = Hash.new { |h, k| h[k] = [] } | |
@fps = 0 | |
update_block_size | |
@start_time = @last_time = @current_time = Qt::Time.current_time | |
show | |
end | |
def update_block_size | |
@block_width = (self.size.width - 100) / @environment.cols | |
@block_height = (self.size.height - 100) / @environment.rows | |
end | |
def set_environment(environment) | |
@environment = environment | |
end | |
def draw_map | |
@number_agents = 0 | |
@environment.rows.times.each do |row_index| | |
@environment.cols.times.each do |col_index| | |
cell_identifier = @environment.at(row_index, col_index) | |
if should_draw?(cell_identifier) | |
draw_rect(row_index, col_index, cell_identifier) | |
end | |
end | |
end | |
end | |
def should_draw?(cell_identifier) | |
!cell_identifier.eql?(:V) | |
end | |
def draw_rect(x, y, cell) | |
if cell.is_a?(Agent) | |
cell = agent_cell_identifier(cell) | |
@number_agents += 1 | |
end | |
@graphic_scene.addRect(100 + @block_width * x, @block_height * y, @block_width, @block_height, pen(cell), COLORS_BRUSHES[cell]) | |
end | |
def agent_cell_identifier(cell) | |
if cell.looted_identifier | |
cell = cell.looted_identifier | |
else | |
cell = :AGENT | |
end | |
cell | |
end | |
def pen(cell) | |
if cell.eql?(:AGENT) | |
BLACK | |
else | |
GRAY | |
end | |
end | |
def fps_counter | |
@last_time = @current_time | |
@current_time = Qt::Time.current_time | |
@fps = 1000 / (@current_time.second * 1000 + @current_time.msec - (@current_time.second * 1000 + @last_time.msec)) | |
end | |
def refresh_ui | |
@graphic_scene.clear | |
fps_counter | |
draw_map | |
@graphic_view.viewport.update | |
end | |
def closeEvent(event) | |
@environment.interrupt | |
# event.dispose | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment