Skip to content

Commit

Permalink
for #410, improvements to user layout delation
Browse files Browse the repository at this point in the history
* beqin conversion of cassowary solver from python to ruby
  • Loading branch information
Cecil committed Oct 15, 2018
1 parent ffec6c9 commit d117ff1
Show file tree
Hide file tree
Showing 27 changed files with 4,632 additions and 70 deletions.
62 changes: 32 additions & 30 deletions Tests/layout/l2.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@

class MyLayout
attr_accessor :pos_x, :pos_y, :w, :h
attr_accessor :incr_x, :incr_y
attr_accessor :incr_x, :incr_y, :canvas

def initialize()
puts "initialized"
clear
end

def setup(canvas, attr)
@canvas = canvas
@w = attr[:width]
@h = attr[:height]
puts "callback: setup #{@w} X #{@h}"
end

def add(canvas, widget)
puts "callback add: #{widget.inspect} #{canvas.contents.size}"
puts "callback add: #{widget.class} #{canvas.contents.size}"
puts "w: #{widget.width} h: #{widget.height}"
widget.move @pos_x, @pos_y
@pos_x += @incr_x
if @pos_x < 0
@pos_x = 0
@incr_x = 25
end
if @pos_x >= @w
@pos_x = @w
@incr_x = -25
end
@pos_y += @incr_y
if @pos_y <= 0
@pos_y = 0
@incr_y = +25
end
if @pos_y >= @h
@pos_y = @h - 25
@incr_y = -25
end
widget.move @pos_x, @pos_y
end

def clear
@pos_x = -20
@pos_y = -20
@pos_x = 5
@pos_y = 5
@incr_x = 25
@incr_y = 25
puts "callback: clear"
end

def refresh
@pos_x = 5
@pos_y = 5
@canvas.contents.each do |widget|
widget.move @pos_x, @pos_y
@pos_x += @incr_x
@pos_y += @incr_y
puts "w: #{widget.width} h: #{widget.height}"
end
end

end

Shoes.app width: 350, height: 450, resizeable: true do
Shoes.app width: 380, height: 450, resizeable: true do
stack do
@p = para "Before layout"
@ml = MyLayout.new
@lay =layout manager: @ml, width: 340, height: 380 do
@lay = layout manager: @ml, width: 340, height: 380 do
background yellow
p1 = para "First Para"
a = button "one"
Expand All @@ -61,17 +58,22 @@ def clear
@p.text = @lay.inspect
@lay.finish
end
button "Append" do
@lay.append { para "appended" }
@el = edit_line width:40
@el.text = '-1'
button "Insert" do
@lay.insert @el.text.to_i do
para "inserted #{@el.text}"
end
end
button "delete_at" do
@lay.delete_at @el.text.to_i do
para "replaced by deletion"
end
end
button "Clear" do
@lay.clear { background white }
end
button "Prepend" do
# problem here?
@lay.prepend { para "prepended" }
end
button "refresh" do
@lay.refresh
@ml.refresh
end
end
4 changes: 2 additions & 2 deletions crontasks/nbuild-shoes-all
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# my crontab so user name may not be set up for chroot
#schroot -c debx86 -u ccoupe -- ~/Projects/shoes3/crontasks/nbuild-shoes-lin64
~/Projects/shoes3/crontasks/nbuild-shoes-xlin64
#~/Projects/shoes3/crontasks/nbuild-shoes-xwin7
~/Projects/shoes3/crontasks/nbuild-shoes-mxe
~/Projects/shoes3/crontasks/nbuild-shoes-xwin7
#~/Projects/shoes3/crontasks/nbuild-shoes-mxe
# ssh to Mac mini and build.
#~/Projects/shoes3/crontasks/nbuild-shoes-mavericks
~/Projects/shoes3/crontasks/nbuild-shoes-yosemite
Expand Down
16 changes: 16 additions & 0 deletions lib/cassowary/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import print_function, unicode_literals, absolute_import

from .expression import Variable
from .error import RequiredFailure, ConstraintNotFound, InternalError
from .simplex_solver import SimplexSolver
from .utils import REQUIRED, STRONG, MEDIUM, WEAK

# Examples of valid version strings
# __version__ = '1.2.3.dev1' # Development release 1
# __version__ = '1.2.3a1' # Alpha Release 1
# __version__ = '1.2.3b1' # Beta Release 1
# __version__ = '1.2.3rc1' # RC Release 1
# __version__ = '1.2.3' # Final Release
# __version__ = '1.2.3.post1' # Post Release 1

__version__ = '0.5.1'
238 changes: 238 additions & 0 deletions lib/cassowary/constraint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
module Cassowary
###########################################################################
# Constraint
#
# Constraints are the restrictions on linear programming; an equality or
# inequality between two expressions.
###########################################################################
=begin
class AbstractConstraint(object):
def __init__(self, strength, weight=1.0):
self.strength = strength
self.weight = weight
self.is_edit_constraint = False
self.is_inequality = False
self.is_stay_constraint = False
@property
def is_required(self):
return self.strength == REQUIRED
def __repr__(self):
return '%s:{%s}(%s)' % (repr_strength(self.strength), self.weight, self.expression)
=end
class AbstractConstraint
attr_accessor :strength, :weight, :s_edit_constraint, :is_inequality,
:is_stay_contraint

def initialize(strength, weight=1.0)
@strength = strength
@weight = weight
@is_edit_constraint = false
@is_inequality = false
@is_stay_contraint = false
end

def is_required
@strength == REQUIRED
end
end

class EditConstraint < AbstractConstraint
attr_accessor :variable, :expression

def initialize(variable, strength=STRONG, weight=1.0)
super(strength, weight)
@variable = variable
@expression = Expression.new(variable, -1.0, variable.value)
@is_edit_constraint = true
end
end

class StayConstraint < AbstractConstraint
attr_accessor :variable, :expression

def initialize(variable, strength=STRONG, weight=1.0)
super(strength, weight)
@variable = variable
@expression = Expression.new(variable, -1.0, variable.value)
@is_stay_constraint = true
end
end

=begin
class EditConstraint(AbstractConstraint):
def __init__(self, variable, strength=STRONG, weight=1.0):
super(EditConstraint, self).__init__(strength, weight)
self.variable = variable
self.expression = Expression(variable, -1.0, variable.value)
self.is_edit_constraint = True
def __repr__(self):
return 'edit:%s' % super(EditConstraint, self).__repr__()
class StayConstraint(AbstractConstraint):
def __init__(self, variable, strength=STRONG, weight=1.0):
super(StayConstraint, self).__init__(strength, weight)
self.variable = variable
self.expression = Expression(variable, -1.0, variable.value)
self.is_stay_constraint=True
def __repr__(self):
return 'stay:%s' % super(StayConstraint, self).__repr__()
=end

class Constraint < AbstractConstraint
LEQ = -1
EQ = 0
GEQ = 1

def initialize(param1, operator=EQ, param2=None, strength=REQUIRED, weight=1.0)
# Define a new linear constraint.
#
# param1 may be an expression or variable
# param2 may be an expression, variable, or constant, or may be ommitted entirely.
# If param2 is specified, the operator must be either LEQ, EQ, or GEQ

if param1.kind_of? Expression
if param2 == nil
super(strength=strength, weight=weight)
@expression = param1
elsif param2.kind_of? Expression
super(strength=strength, weight=weight)
@expression = param1.clone()
if operator == self.LEQ
@expression.multiply(-1.0)
@expression.add_expression(param2, 1.0)
elsif operator == self.EQ
@expression.add_expression(param2, -1.0)
elsif operator == self.GEQ
@expression.add_expression(param2, -1.0)
else
raise InternalError("Invalid operator in Constraint constructor")
end
elsif param2.kind_of? Variable
super(strength=strength, weight=weight)
@expression = param1.clone()
if operator == self.LEQ
@expression.multiply(-1.0)
@expression.add_variable(param2, 1.0)
elsif operator == self.EQ
@expression.add_variable(param2, -1.0)
elsif operator == self.GEQ
@expression.add_variable(param2, -1.0)
else
raise InternalError("Invalid operator in Constraint constructor")
end
elsif param2.kind_of? Numeric
super(strength=strength, weight=weight)
@expression = param1.clone()
if operator == self.LEQ
@expression.multiply(-1.0)
@expression.add_expression(Expression(constant=param2), 1.0)
elsif operator == self.EQ
@expression.add_expression(Expression(constant=param2), -1.0)
elsif operator == self.GEQ
@expression.add_expression(Expression(constant=param2), -1.0)
else
raise InternalError("Invalid operator in Constraint constructor")
end
else
raise InternalError("Invalid parameters to Constraint constructor")
end
elsif param1.kind_of? Variable
if param2 == nil
super(strength=strength, weight=weight)
@expression = Expression.new(param1)
elsif param2.kind_of? Expression
super(strength=strength, weight=weight)
@expression = param2.clone()
if operator == self.LEQ
@expression.add_variable(param1, -1.0)
elsif operator == self.EQ
@expression.add_variable(param1, -1.0)
elsif operator == self.GEQ
@expression.multiply(-1.0)
@expression.add_variable(param1, 1.0)
else
raise InternalError("Invalid operator in Constraint constructor")
end
elsif param2.kind_of? Variable
super(strength=strength, weight=weight)
@expression = Expression.new(param2)
if operator == self.LEQ
@expression.add_variable(param1, -1.0)
elsif operator == self.EQ
@expression.add_variable(param1, -1.0)
elsif operator == self.GEQ
@expression.multiply(-1.0)
@expression.add_variable(param1, 1.0)
else
raise InternalError("Invalid operator in Constraint constructor")
end
elsif param2.kind_of? Numeric
super(strength=strength, weight=weight)
@expression = Expression.new(constant=param2)
if operator == self.LEQ
@expression.add_variable(param1, -1.0)
elsif operator == self.EQ
@expression.add_variable(param1, -1.0)
elsif operator == self.GEQ
@expression.multiply(-1.0)
@expression.add_variable(param1, 1.0)
else
raise InternalError("Invalid operator in Constraint constructor")
end
else
raise InternalError("Invalid parameters to Constraint constructor")
end
elsif param1.kind_of? Numeric
if param2 == nil
super(strength=strength, weight=weight)
@expression = Expression(constant=param1)
elsif param2.kind_of? Expression
super(strength=strength, weight=weight)
@expression = param2.clone()
if operator == self.LEQ
@expression.add_expression(Expression.new(constant=param1), -1.0)
elsif operator == self.EQ
@expression.add_expression(Expression.new(constant=param1), -1.0)
elsif operator == self.GEQ
@expression.multiply(-1.0)
@expression.add_expression(Expression.new(constant=param1), 1.0)
else
raise InternalError("Invalid operator in Constraint constructor")
end
elsif param2.kind_of? Variable
super(strength=strength, weight=weight)
@expression = Expression,new(constant=param1)
if operator == self.LEQ
@expression.add_variable(param2, -1.0)
elsif operator == self.EQ
@expression.add_variable(param2, -1.0)
elsif operator == self.GEQ
@expression.multiply(-1.0)
@expression.add_variable(param2, 1.0)
else
raise InternalError("Invalid operator in Constraint constructor")
end
elsif param2.kind_of? Numeric
raise InternalError("Cannot create an inequality between constants")
else
raise InternalError("Invalid parameters to Constraint constructor")
end
else
raise InternalError("Invalid parameters to Constraint constructor")
end
@is_inequality = operator != self.EQ
end

def clone()
c = Constraint(@expression, strength=@strength, weight=@weight)
c.is_inequality = @is_inequality
return c
end
end # class Constraint
end # module Cassowary
19 changes: 19 additions & 0 deletions lib/cassowary/edit_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import print_function, unicode_literals, absolute_import, division


class EditInfo(object):
def __init__(self, constraint, edit_plus, edit_minus, prev_edit_constant, index):
self.constraint = constraint
self.edit_plus = edit_plus
self.edit_minus = edit_minus
self.prev_edit_constant = prev_edit_constant
self.index = index

def __repr__(self):
return '<cn=%s ep=%s em=%s pec=%s index=%s>' % (
self.constraint,
self.edit_plus,
self.edit_minus,
self.prev_edit_constant,
self.index
)
Loading

0 comments on commit d117ff1

Please sign in to comment.