The '''<tt>CannonField</tt>''' now has a force value in addition to the angle.

The '''<tt>CannonField</tt>''' now has a force value in addition to the angle.

</translate>

</translate>

Line 41:

Line 50:

<translate>

<translate>

+

<!--T:13-->

The interface to the force follows the same practice as for the angle.

The interface to the force follows the same practice as for the angle.

</translate>

</translate>

Line 57:

Line 67:

<translate>

<translate>

+

<!--T:14-->

The force '''<tt>@currentForce</tt>''' is initialized to zero.

The force '''<tt>@currentForce</tt>''' is initialized to zero.

</translate>

</translate>

Line 79:

Line 90:

<translate>

<translate>

+

<!--T:15-->

We have made a slight change in the '''<tt>setAngle()</tt>''' function. It repaints only the portion of the widget that contains the cannon.

We have made a slight change in the '''<tt>setAngle()</tt>''' function. It repaints only the portion of the widget that contains the cannon.

</translate>

</translate>

Line 97:

Line 109:

<translate>

<translate>

+

<!--T:16-->

The implementation of '''<tt>setForce()</tt>''' is quite similar to that of '''<tt>setAngle()</tt>'''. The only difference is that because we don't show the force value, we don't need to repaint the widget.

The implementation of '''<tt>setForce()</tt>''' is quite similar to that of '''<tt>setAngle()</tt>'''. The only difference is that because we don't show the force value, we don't need to repaint the widget.

</translate>

</translate>

Line 116:

Line 129:

<translate>

<translate>

+

<!--T:17-->

We paint as in Chapter 9.

We paint as in Chapter 9.

</translate>

</translate>

Line 128:

Line 142:

<translate>

<translate>

+

<!--T:18-->

The '''<tt>cannonRect()</tt>''' function returns the rectangle enclosing the cannon in widget coordinates. First we create a rectangle with the size 50 x 50 and then move it so its bottom-left corner is equal to the widget's own bottom-left corner.

The '''<tt>cannonRect()</tt>''' function returns the rectangle enclosing the cannon in widget coordinates. First we create a rectangle with the size 50 x 50 and then move it so its bottom-left corner is equal to the widget's own bottom-left corner.

+

<!--T:19-->

The [http://doc.qt.nokia.com/latest/qwidget.html#rect-prop Qt::Widget::rect()] function returns the widget's enclosing rectangle in the widget's own coordinates. The top-left corner of the rectangle is always (0, 0).

The [http://doc.qt.nokia.com/latest/qwidget.html#rect-prop Qt::Widget::rect()] function returns the widget's enclosing rectangle in the widget's own coordinates. The top-left corner of the rectangle is always (0, 0).

</translate>

</translate>

Line 136:

Line 152:

<translate>

<translate>

+

<!--T:20-->

The constructor is mostly the same, but some new bits have been added.

The constructor is mostly the same, but some new bits have been added.

</translate>

</translate>

Line 145:

Line 162:

<translate>

<translate>

+

<!--T:21-->

We add a second '''<tt>LCDRange</tt>''', which will be used to set the force.

We add a second '''<tt>LCDRange</tt>''', which will be used to set the force.

</translate>

</translate>

Line 156:

Line 174:

<translate>

<translate>

+

<!--T:22-->

We connect the '''<tt>force</tt>''' widget and the '''<tt>cannonField</tt>''' widget, just like we did for the '''<tt>angle</tt>''' widget.

We connect the '''<tt>force</tt>''' widget and the '''<tt>cannonField</tt>''' widget, just like we did for the '''<tt>angle</tt>''' widget.

</translate>

</translate>

Line 172:

Line 191:

<translate>

<translate>

+

<!--T:23-->

In Chapter 9, we put '''<tt>angle</tt>''' in the lower-left cell of the layout. Now we want to have two widgets in that cell, so we make a vertical box, put the vertical box in the grid cell, and put each of '''<tt>angle</tt>''' and '''<tt>range</tt>''' in the vertical box.

In Chapter 9, we put '''<tt>angle</tt>''' in the lower-left cell of the layout. Now we want to have two widgets in that cell, so we make a vertical box, put the vertical box in the grid cell, and put each of '''<tt>angle</tt>''' and '''<tt>range</tt>''' in the vertical box.

</translate>

</translate>

Line 180:

Line 200:

<translate>

<translate>

+

<!--T:24-->

We initialize the force value to 25.

We initialize the force value to 25.

</translate>

</translate>

<translate>

<translate>

−

=== Running the Application ===

+

=== Running the Application === <!--T:25-->

+

<!--T:26-->

We now have a force control.

We now have a force control.

−

=== Exercises ===

+

=== Exercises === <!--T:27-->

Make the size of the cannon barrel be dependent on the force.

Make the size of the cannon barrel be dependent on the force.

+

<!--T:28-->

Put the cannon in the bottom-right corner.

Put the cannon in the bottom-right corner.

+

<!--T:29-->

Try adding a better keyboard interface. For example, make + and - increase and decrease the force and enter shoot. If you're bothered by the way the '''<tt>Left</tt>''' and '''<tt>Right</tt>''' keys work, change that too. [Hint: Reimplement [http://doc.qt.nokia.com/latest/qwidget.html#keyPressEvent Qt::Widget::keyPressEvent()].]

Try adding a better keyboard interface. For example, make + and - increase and decrease the force and enter shoot. If you're bothered by the way the '''<tt>Left</tt>''' and '''<tt>Right</tt>''' keys work, change that too. [Hint: Reimplement [http://doc.qt.nokia.com/latest/qwidget.html#keyPressEvent Qt::Widget::keyPressEvent()].]

The cannonRect() function returns the rectangle enclosing the cannon in widget coordinates. First we create a rectangle with the size 50 x 50 and then move it so its bottom-left corner is equal to the widget's own bottom-left corner.

The Qt::Widget::rect() function returns the widget's enclosing rectangle in the widget's own coordinates. The top-left corner of the rectangle is always (0, 0).

In Chapter 9, we put angle in the lower-left cell of the layout. Now we want to have two widgets in that cell, so we make a vertical box, put the vertical box in the grid cell, and put each of angle and range in the vertical box.

force.setValue(25)

We initialize the force value to 25.

Running the Application

We now have a force control.

Exercises

Make the size of the cannon barrel be dependent on the force.

Put the cannon in the bottom-right corner.

Try adding a better keyboard interface. For example, make + and - increase and decrease the force and enter shoot. If you're bothered by the way the Left and Right keys work, change that too. [Hint: Reimplement Qt::Widget::keyPressEvent().]