|  | Home · Overviews · Examples

# Address Book 4 - Editing and Removing Addresses

In this chapter, we look at ways to modify the contents of contact stored in the address book application.

We now have an address book that not only holds contacts in an organized manner, but also allows navigation. It would be convenient to include edit and remove functions so that a contact's details can be changed when needed. However, this requires a little improvement, in the form of enums. In our previous chapters, we had two modes: `AddingMode` and `NavigationMode` - but they weren't defined as enums. Instead, we enabled and disabled the corresponding buttons manually, resulting in multiple lines of repeated code.

In this chapter, we define the `Mode` enum with three different values:

- `NavigationMode`,
- `AddingMode`, and
- `EditingMode`.

## Defining the AddressBook Class

The `addressbook.h` file is updated to contain the `Mode` enum: Missing snippet: tutorials/addressbook/part4/addressbook.h. We also add two new slots, `editContact()` and `removeContact()`, to our current list of public slots. Missing snippet: tutorials/addressbook/part4/addressbook.h. In order to switch between modes, we introduce the `updateInterface()` function to control the enabling and disabling of all QPushButton objects. We also add two new push buttons, `editButton` and `removeButton`, for the edit and remove functions mentioned earlier. Missing snippet: tutorials/addressbook/part4/addressbook.h. ... Missing snippet: tutorials/addressbook/part4/addressbook.h. ... Missing snippet: tutorials/addressbook/part4/addressbook.h. Lastly, we declare `currentMode` to keep track of the current mode of the enum.

## Implementing the AddressBook Class

We now have to implement the mode-changing features of the address book application. The `editButton` and `removeButton` are instantiated and disabled by default, as the address book starts up with zero contacts in memory. Missing snippet: tutorials/addressbook/part4/addressbook.cpp. These buttons are then connected to their respective slots, `editContact()` and `removeContact()`, and we add them to `buttonLayout1`. Missing snippet: tutorials/addressbook/part4/addressbook.cpp. ... Missing snippet: tutorials/addressbook/part4/addressbook.cpp. The `editContact()` function stores the contact's old details in `oldName` and `oldAddress`, before switching the mode to `EditingMode`. In this mode, the `submitButton` and `cancelButton` are both enabled, hence, the user can change the contact's details and click either button. Missing snippet: tutorials/addressbook/part4/addressbook.cpp. The `submitContact()` function has been divided in two with an `if-else` statement. We check `currentMode` to see if its in `AddingMode`. If it is, we proceed with our adding process. Missing snippet: tutorials/addressbook/part4/addressbook.cpp. ... Missing snippet: tutorials/addressbook/part4/addressbook.cpp. Otherwise, we check to see if `currentMode` is in `EditingMode`. If it is, we compare `oldName` with `name`. If the name has changed, we remove the old contact from `contacts` and insert the newly updated contact. Missing snippet: tutorials/addressbook/part4/addressbook.cpp. If only the address has changed (i.e., `oldAddress` is not the same as `address`), we update the contact's address. Lastly, we set `currentMode` to `NavigationMode`. This is an important step as it re-enables all the disabled push buttons.

To remove a contact from the address book, we implement the `removeContact()` function. This function checks to see if the contact exists in `contacts`. Missing snippet: tutorials/addressbook/part4/addressbook.cpp. If it does, we display a QMessageBox, to confirm the removal with the user. Once the user has confirmed, we call `previous()` to ensure that the user interface shows another contact, and we remove the contact using QMap's remove() function. As a courtesy, we display a QMessageBox to inform the user. Both the message boxes used in this function are shown below:

### Updating the User Interface

We mentioned the `updateInterface()` function earlier as a means to enable and disable the push buttons depending on the current mode. The function updates the current mode according to the `mode` argument passed to it, assigning it to `currentMode` before checking its value.

Each of the push buttons is then enabled or disabled, depending on the current mode. The code for `AddingMode` and `EditingMode` is shown below: Missing snippet: tutorials/addressbook/part4/addressbook.cpp. For `NavigationMode`, however, we include conditions within the parameters of the QPushButton::setEnabled(). This is to ensure that the `editButton` and `removeButton` push buttons are enabled when there is at least one contact in the address book; `nextButton` and `previousButton` are only enabled when there is more than one contact in the address book. Missing snippet: tutorials/addressbook/part4/addressbook.cpp. By performing the task of setting the mode and updating the user interface in the same function, we avoid the possibility of the user interface getting "out of sync" with the internal state of the application.