![]() | ![]() | DiaCanvas2 Reference Manual | ![]() |
---|
Table of Contents
Creating a diagram is no fun if the different objects on the canvas can not create relations with each other.
The easiest way (at first) seems to hard code the behavior that is desirable when objects need to have a relation with each other. This is a quite limited approach, since it is hard to tailor the objects behavior to the expectations of the user. It is also hard to verify if the code works correctly under all circumstances, since the code is scattered among a lot of different classes.
When using a constraint solver instead we can solve a bunch of problems at once:
We are free to create any relation between any object on the canvas.
All constraints on the canvas can easily be verified at once.
Stability: no strange behavior can be expected as long as the constraints are constructed properly.
It is easy to assign new values to a bunch of objects and then solve all constraints at once. This is desirable in an interactive environment where speed is a prerequisite.
Every variable is assigned a strength. When solving constraints the weakest variable in an equation will be adjusted to make the equation true.
DiaCanvas2 features a small constraint solver based on GObject. The big advantage is that you can send signals between the different parts of the constraint solver, this will keep the code clean.
The solver consists of three classes:
DiaVariable | A DiaVariable hold a variable value. If the value changes the “changed_internal” signal is emitted. This signal is emitted every time the variable is assigned a new value. The DiaVariable also has a “changed” signal. This signal is emitted if its value is changed by the constraint solver. You usually want to connect to this signal. |
DiaConstraint | A Constraint consists of an linear expression. For this purpose a helper class "DiaExpression" is created. An expression is an array of variable * constant pairs. This will result in an expression like: a*x + b*y +c = 0. A constraint will react on DiaVariable's “changed_internal ” signal by emitting “need_resolve”. |
DiaSolver | The solver is the most interesting class of all: this is where the actual work is done. You can add constraints to the solver. The solver will connect to the constraints “need_resolve” signal. If a constraint emits the “need_resolve” signal the constraint will be marked. If the resolve function is called the solver tries to satisfy all constraints. This is done by looking for the weakest variable in the constraint. If weakest variable is already edited the constraint is not resolved again. |
Figure 1. UML model of the DiaSolver
<< Exporting | DiaStrength >> |