Code Monkey home page Code Monkey logo

Comments (15)

sccolbert avatar sccolbert commented on July 20, 2024 1

You can also set an override flag on your widget, and check that before applying the constraint variable values. If the flag is set, ignore the constraint values and use the override values. There's lot's of ways you could solve this without hacking into the kiwi source.

from kiwi.

sccolbert avatar sccolbert commented on July 20, 2024

context is there for you store anything you want to be associated with a variable. It can be any Python object, so storing a function there would be fine.

from kiwi.

yihuang avatar yihuang commented on July 20, 2024

What I want is a way to update effected widgets attributes when variable's value changed. I can think of two ways:
1, Associate the widget attribute with variable with context attribute, and iterate solver->m_impl->m_vars and apply variable value to widget attribute, after calling updateVariables.
2, Hack Variable::setValue method to set the value to widget attribute immediately.

Either way, I need to hack kiwi's source code somehow.

from kiwi.

sccolbert avatar sccolbert commented on July 20, 2024

What widgets are you talking about, and what attribute do you want to set?

If you are using Kiwi directly on your own, you know when you call updateVariables, so you can just iterate your variables then. If you are talking about Enaml's widgets, you may be better off hooking into its layout system rather than hacking Kiwi's source.

Basically, I need to know more about what you are trying to do before I can give you better advice.

from kiwi.

yihuang avatar yihuang commented on July 20, 2024

I'm using kiwi on my own, and I want to only update the variables that are constrained rather than all the variables defined by widget.
For example, each widget defines left, top, width, height, but for some widgets I only add a constraint for width and height, and want to set the left and top manually.
So I can only iterate the variables in solver->m_impl->m_rows rather than all the variables defined by the widgets.

from kiwi.

yihuang avatar yihuang commented on July 20, 2024

PS, I find that there is a problem with solver->m_impl->m_vars, is that it's never get removed except when reset the solver. So If I keep adding and removing constraints, the m_vars keep growing.

from kiwi.

sccolbert avatar sccolbert commented on July 20, 2024

m_impl is private and not part of the public API.

from kiwi.

sccolbert avatar sccolbert commented on July 20, 2024

If you want to set left and top of your widgets manually, you are free to do so. I'm not really seeing how this is an issue with Kiwi.

from kiwi.

yihuang avatar yihuang commented on July 20, 2024

The problem is I have no way to know which variable is constrained and which not (except through private m_impl->m_rows I guess).

If I choose the enaml way to do this, then the left and top would be reseted to zero every time re-layout:

        """ Update the geometry of the underlying toolkit widget.
        This should not be called directly by user code.
        """
        d = self.constrainable()
        x = d.left.value()
        y = d.top.value()
        w = d.width.value()
        h = d.height.value()
        self.set_geometry(x, y, w, h)

from kiwi.

yihuang avatar yihuang commented on July 20, 2024

I've changed Variable's initial value to NAN, and set the value to NAN if not found in m_rows, so I can detect the un-constrained variables.
Here's the diff in case you are interested.

diff --git a/libs/kiwi/kiwi/solverimpl.h b/libs/kiwi/kiwi/solverimpl.h
index 33b0711..fd517cc 100644
--- a/libs/kiwi/kiwi/solverimpl.h
+++ b/libs/kiwi/kiwi/solverimpl.h
@@ -10,6 +10,7 @@
 #include <limits>
 #include <memory>
 #include <vector>
+#include <cmath>
 #include "constraint.h"
 #include "errors.h"
 #include "expression.h"
@@ -305,7 +306,7 @@ public:
            Variable& var( const_cast<Variable&>( var_it->first ) );
            row_iter_t row_it = m_rows.find( var_it->second );
            if( row_it == row_end )
-               var.setValue( 0.0 );
+               var.setValue( NAN );
            else
                var.setValue( row_it->second->constant() );
        }
diff --git a/libs/kiwi/kiwi/variable.h b/libs/kiwi/kiwi/variable.h
index 525f434..e4de813 100644
--- a/libs/kiwi/kiwi/variable.h
+++ b/libs/kiwi/kiwi/variable.h
@@ -8,6 +8,7 @@
 #pragma once
 #include <memory>
 #include <string>
+#include <cmath>
 #include "shareddata.h"


@@ -91,13 +92,13 @@ private:
            SharedData(),
            m_name( name ),
            m_context( context ),
-           m_value( 0.0 ) {}
+           m_value(NAN) {}

        VariableData( const char* name, Context* context ) :
            SharedData(),
            m_name( name ),
            m_context( context ),
-           m_value( 0.0 ) {}
+           m_value(NAN) {}

        ~VariableData() {}

from kiwi.

sccolbert avatar sccolbert commented on July 20, 2024

You said you are using kiwi on your own. So you should know if the variables are constrained or not because you added them to the solver.

from kiwi.

sccolbert avatar sccolbert commented on July 20, 2024

You can also just added a constraint for the left and top values to constrain them to what you want e.g.: left == 42, top == 78

from kiwi.

yihuang avatar yihuang commented on July 20, 2024

Updating a constraint is more expensive, since it could be an animation or scrollview updating it every frame.
Traversing the constraint structure to find out what variables it uses is doable i think.

from kiwi.

yihuang avatar yihuang commented on July 20, 2024

Setting a flag is another useful feature I think, but I still think checking the NAN is a good default behaviour, thanks for your suggestions ;D

from kiwi.

hfossli avatar hfossli commented on July 20, 2024

Checking for NAN values doesn't cover a situation where constraints have been added and then removed later. The variables may still be underconstrained..

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.