Code Monkey home page Code Monkey logo

Comments (4)

sccolbert avatar sccolbert commented on July 20, 2024

It can get fairly complex depending on how much of a "layout system" you want to build.

Here are the gritty details for how we generate constraints in Enaml:
https://github.com/nucleic/enaml/tree/master/enaml/layout

Those helpers are the "engine" which allow us to have things like vbox(*widgets):
https://github.com/nucleic/enaml/blob/master/enaml/widgets/v_group.py#L60-L61
https://github.com/nucleic/enaml/blob/master/enaml/layout/layout_helpers.py#L64
https://github.com/nucleic/enaml/blob/master/enaml/layout/linear_box_helper.py#L31

For your simple case exactly, you can use a simple sum:
solver.addConstraint(sum([item.width for item in items]) == parentWidth)

from kiwi.

quaikohc avatar quaikohc commented on July 20, 2024

Thanks. Sorry but can you explain how can i do this in c++?

from kiwi.

hfossli avatar hfossli commented on July 20, 2024

You could build an Expression of Terms and create a constraint where that expression is set to equal parentWidth.

In swift I would (pseudo)

let width0 = Variable()
let width1 = Variable()
let width2 = Variable()
let parentWidth = Variable()
let allWidths = Expression([Term(width0), Term(width1), Term(width2)])
let constraint = allWidths == parentWidth
solver.addConstraint(constraint)

from kiwi.

sccolbert avatar sccolbert commented on July 20, 2024

You can do like @hfossli said, using an std::vector of Term objects:
https://github.com/nucleic/kiwi/blob/master/kiwi/expression.h#L26

Or use a simple loop to accumulate the expression, but that will be more expensive due to the copy constructor on the vector being invoked for each term. One could make a case for some in-place operator overloads on Expression to make this easier/cheaper.

from kiwi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.