Adding growth functionality

Now that the interface switches correctly between the two different views we need to add the actual functionality to the program. The user should be able to press either button on the crop status view to grow the crop. There are two available options:

  1. Automatically grow crop
  2. Manually grow crop

Automatically grow the crop

When the user presses the automatically grow button the crop should generate a random amount of light and water and then use these values to grow the crop. Our crop class already has a method called grow() that we can use to actually grow the crop, so we can use that. Therefore, there are three tasks we must accomplish:

  1. Generating the random values
  2. Growing the crop using the grow() method
  3. Updating the interface to reflect the growth

The video demonstrates how to do this.


Task 4a

Use the above video to add the functionality necessary to grow the crop automatically.


Manually grow the crop

Rather than generating random values, when the user presses the manually grow button they are presented with a dialog window which enables them to enter values for light and water. These values are then passed to the crop grow() method. To grow the crop manually we must:

  1. Create a dialog window for entering the values
  2. Get the entered values from the dialog
  3. Growing the crop using the grow() method
  4. Updating the interface to reflect the growth

Manual grow dialog window

The design for the manual grow dialog window looks like this:

Manual grow dialog

Notice that it contains a new component that allows you to select a value using the arrows. This is called a spinbox (QSpinBox) and it is useful in situations where you want to control the range of values the user should be permitted to enter and therefore you can ensure that they are providing valid values.

The video demonstrates how to create the dialog window.


Task 4b

Use the above video to create the manual grow dialog class.


Using the values from the dialog

Having created the dialog we must add to our program the ability to show this dialog and then use the values entered to grow the crop and finally update the interface.

The video demonstrates how to do this.


Task 4c

Use the above video to add the functionality necessary to grow the crop manually.