File: tutorials-addressbook-part4.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (229 lines) | stat: -rw-r--r-- 20,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- addressbook.qdoc -->
  <title>Qt 4.8: Part 4 - Editing and Removing Addresses</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Part 4 - Editing and Removing Addresses</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#defining-the-addressbook-class">Defining the AddressBook Class</a></li>
<li class="level1"><a href="#implementing-the-addressbook-class">Implementing the AddressBook Class</a></li>
<li class="level2"><a href="#updating-the-user-interface">Updating the User Interface</a></li>
</ul>
</div>
<h1 class="title">Part 4 - Editing and Removing Addresses</h1>
<span class="subtitle"></span>
<!-- $$$tutorials/addressbook/part4-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="tutorials-addressbook-part4-addressbook-cpp.html">tutorials/addressbook/part4/addressbook.cpp</a></li>
<li><a href="tutorials-addressbook-part4-addressbook-h.html">tutorials/addressbook/part4/addressbook.h</a></li>
<li><a href="tutorials-addressbook-part4-main-cpp.html">tutorials/addressbook/part4/main.cpp</a></li>
<li><a href="tutorials-addressbook-part4-part4-pro.html">tutorials/addressbook/part4/part4.pro</a></li>
</ul>
<p>Now we look at ways to modify the contents of contacts stored in the address book.</p>
<p class="centerAlign"><img src="images/addressbook-tutorial-screenshot.png" alt="" /></p><p>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. We defined two modes: <tt>AddingMode</tt> and <tt>NavigationMode</tt>, but they were not defined as enum values. Instead, we enabled and disabled the corresponding buttons manually, resulting in multiple lines of repeated code.</p>
<p>Here we define the <tt>Mode</tt> enum with three different values:</p>
<ul>
<li><tt>NavigationMode</tt>,</li>
<li><tt>AddingMode</tt>, and</li>
<li><tt>EditingMode</tt>.</li>
</ul>
<a name="defining-the-addressbook-class"></a>
<h2>Defining the AddressBook Class</h2>
<p>The <tt>addressbook.h</tt> file is updated to contain the <tt>Mode</tt> enum:</p>
<pre class="cpp">     <span class="keyword">enum</span> Mode { NavigationMode<span class="operator">,</span> AddingMode<span class="operator">,</span> EditingMode };</pre>
<p>We also add two new slots, <tt>editContact()</tt> and <tt>removeContact()</tt>, to our current list of public slots.</p>
<pre class="cpp">     <span class="type">void</span> editContact();
     <span class="type">void</span> removeContact();</pre>
<p>In order to switch between modes, we introduce the <tt>updateInterface()</tt> function to control the enabling and disabling of all <a href="qpushbutton.html">QPushButton</a> objects. We also add two new push buttons, <tt>editButton</tt> and <tt>removeButton</tt>, for the edit and remove functions mentioned earlier.</p>
<pre class="cpp">     <span class="type">void</span> updateInterface(Mode mode);
     ...
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>editButton;
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>removeButton;
     ...
     Mode currentMode;</pre>
<p>Lastly, we declare <tt>currentMode</tt> to keep track of the enum's current mode.</p>
<a name="implementing-the-addressbook-class"></a>
<h2>Implementing the AddressBook Class</h2>
<p>We now implement the mode-changing features of the address book. The <tt>editButton</tt> and <tt>removeButton</tt> are instantiated and disabled by default. The address book starts with zero contacts in memory.</p>
<pre class="cpp">     editButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;&amp;Edit&quot;</span>));
     editButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
     removeButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;&amp;Remove&quot;</span>));
     removeButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);</pre>
<p>These buttons are then connected to their respective slots, <tt>editContact()</tt> and <tt>removeContact()</tt>, and we add them to <tt>buttonLayout1</tt>.</p>
<pre class="cpp">     connect(editButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(editContact()));
     connect(removeButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(removeContact()));
     ...
     buttonLayout1<span class="operator">-</span><span class="operator">&gt;</span>addWidget(editButton);
     buttonLayout1<span class="operator">-</span><span class="operator">&gt;</span>addWidget(removeButton);</pre>
<p>The <tt>editContact()</tt> function stores the contact's old details in <tt>oldName</tt> and <tt>oldAddress</tt>, before switching the mode to <tt>EditingMode</tt>. In this mode, the <tt>submitButton</tt> and <tt>cancelButton</tt> are both enabled, hence, the user can change the contact's details and click either button.</p>
<pre class="cpp"> <span class="type">void</span> AddressBook<span class="operator">::</span>editContact()
 {
     oldName <span class="operator">=</span> nameLine<span class="operator">-</span><span class="operator">&gt;</span>text();
     oldAddress <span class="operator">=</span> addressText<span class="operator">-</span><span class="operator">&gt;</span>toPlainText();

     updateInterface(EditingMode);
 }</pre>
<p>The <tt>submitContact()</tt> function has been divided in two with an <tt>if-else</tt> statement. We check <tt>currentMode</tt> to see if it's in <tt>AddingMode</tt>. If it is, we proceed with our adding process.</p>
<pre class="cpp"> <span class="type">void</span> AddressBook<span class="operator">::</span>submitContact()
 {
     ...
     <span class="keyword">if</span> (currentMode <span class="operator">=</span><span class="operator">=</span> AddingMode) {

         <span class="keyword">if</span> (<span class="operator">!</span>contacts<span class="operator">.</span>contains(name)) {
             contacts<span class="operator">.</span>insert(name<span class="operator">,</span> address);
             <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Add Successful&quot;</span>)<span class="operator">,</span>
                 tr(<span class="string">&quot;\&quot;%1\&quot; has been added to your address book.&quot;</span>)<span class="operator">.</span>arg(name));
         } <span class="keyword">else</span> {
             <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Add Unsuccessful&quot;</span>)<span class="operator">,</span>
                 tr(<span class="string">&quot;Sorry, \&quot;%1\&quot; is already in your address book.&quot;</span>)<span class="operator">.</span>arg(name));
         }</pre>
<p>Otherwise, we check to see if <tt>currentMode</tt> is in <tt>EditingMode</tt>. If it is, we compare <tt>oldName</tt> with <tt>name</tt>. If the name has changed, we remove the old contact from <tt>contacts</tt> and insert the newly updated contact.</p>
<pre class="cpp">     } <span class="keyword">else</span> <span class="keyword">if</span> (currentMode <span class="operator">=</span><span class="operator">=</span> EditingMode) {

         <span class="keyword">if</span> (oldName <span class="operator">!</span><span class="operator">=</span> name) {
             <span class="keyword">if</span> (<span class="operator">!</span>contacts<span class="operator">.</span>contains(name)) {
                 <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Edit Successful&quot;</span>)<span class="operator">,</span>
                     tr(<span class="string">&quot;\&quot;%1\&quot; has been edited in your address book.&quot;</span>)<span class="operator">.</span>arg(oldName));
                 contacts<span class="operator">.</span>remove(oldName);
                 contacts<span class="operator">.</span>insert(name<span class="operator">,</span> address);
             } <span class="keyword">else</span> {
                 <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Edit Unsuccessful&quot;</span>)<span class="operator">,</span>
                     tr(<span class="string">&quot;Sorry, \&quot;%1\&quot; is already in your address book.&quot;</span>)<span class="operator">.</span>arg(name));
             }
         } <span class="keyword">else</span> <span class="keyword">if</span> (oldAddress <span class="operator">!</span><span class="operator">=</span> address) {
             <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Edit Successful&quot;</span>)<span class="operator">,</span>
                 tr(<span class="string">&quot;\&quot;%1\&quot; has been edited in your address book.&quot;</span>)<span class="operator">.</span>arg(name));
             contacts<span class="operator">[</span>name<span class="operator">]</span> <span class="operator">=</span> address;
         }
     }

     updateInterface(NavigationMode);
 }</pre>
<p>If only the address has changed (i.e&#x2e;, <tt>oldAddress</tt> is not the same as <tt>address</tt>), we update the contact's address. Lastly, we set <tt>currentMode</tt> to <tt>NavigationMode</tt>. This is an important step as it re-enables all the disabled push buttons.</p>
<p>To remove a contact from the address book, we implement the <tt>removeContact()</tt> function. This function checks to see if the contact exists in <tt>contacts</tt>.</p>
<pre class="cpp"> <span class="type">void</span> AddressBook<span class="operator">::</span>removeContact()
 {
     <span class="type"><a href="qstring.html">QString</a></span> name <span class="operator">=</span> nameLine<span class="operator">-</span><span class="operator">&gt;</span>text();
     <span class="type"><a href="qstring.html">QString</a></span> address <span class="operator">=</span> addressText<span class="operator">-</span><span class="operator">&gt;</span>toPlainText();

     <span class="keyword">if</span> (contacts<span class="operator">.</span>contains(name)) {

         <span class="type">int</span> button <span class="operator">=</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>question(<span class="keyword">this</span><span class="operator">,</span>
             tr(<span class="string">&quot;Confirm Remove&quot;</span>)<span class="operator">,</span>
             tr(<span class="string">&quot;Are you sure you want to remove \&quot;%1\&quot;?&quot;</span>)<span class="operator">.</span>arg(name)<span class="operator">,</span>
             <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes <span class="operator">|</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>No);

         <span class="keyword">if</span> (button <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes) {

             previous();
             contacts<span class="operator">.</span>remove(name);

             <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>information(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Remove Successful&quot;</span>)<span class="operator">,</span>
                 tr(<span class="string">&quot;\&quot;%1\&quot; has been removed from your address book.&quot;</span>)<span class="operator">.</span>arg(name));
         }
     }

     updateInterface(NavigationMode);
 }</pre>
<p>If it does, we display a <a href="qmessagebox.html">QMessageBox</a>, to confirm the removal with the user. Once the user has confirmed, we call <tt>previous()</tt> to ensure that the user interface shows another contact, and we remove the contact using <a href="qmap.html">QMap</a>'s <a href="qmap.html#remove">remove()</a> function. As a courtesy, we display a <a href="qmessagebox.html">QMessageBox</a> to inform the user. Both the message boxes used in this function are shown below:</p>
<p class="centerAlign"><img src="images/addressbook-tutorial-part4-remove.png" alt="" /></p><a name="updating-the-user-interface"></a>
<h3>Updating the User Interface</h3>
<p>We mentioned the <tt>updateInterface()</tt> 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 <tt>mode</tt> argument passed to it, assigning it to <tt>currentMode</tt> before checking its value.</p>
<p>Each of the push buttons is then enabled or disabled, depending on the current mode. The code for <tt>AddingMode</tt> and <tt>EditingMode</tt> is shown below:</p>
<pre class="cpp"> <span class="type">void</span> AddressBook<span class="operator">::</span>updateInterface(Mode mode)
 {
     currentMode <span class="operator">=</span> mode;

     <span class="keyword">switch</span> (currentMode) {

     <span class="keyword">case</span> AddingMode:
     <span class="keyword">case</span> EditingMode:

         nameLine<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">false</span>);
         nameLine<span class="operator">-</span><span class="operator">&gt;</span>setFocus(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>OtherFocusReason);
         addressText<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">false</span>);

         addButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
         editButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
         removeButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

         nextButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
         previousButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

         submitButton<span class="operator">-</span><span class="operator">&gt;</span>show();
         cancelButton<span class="operator">-</span><span class="operator">&gt;</span>show();
         <span class="keyword">break</span>;</pre>
<p>For <tt>NavigationMode</tt>, however, we include conditions within the parameters of the <a href="qwidget.html#enabled-prop">QPushButton::setEnabled</a>() function. This is to ensure that <tt>editButton</tt> and <tt>removeButton</tt> are enabled when there is at least one contact in the address book; <tt>nextButton</tt> and <tt>previousButton</tt> are only enabled when there is more than one contact in the address book.</p>
<pre class="cpp">     <span class="keyword">case</span> NavigationMode:

         <span class="keyword">if</span> (contacts<span class="operator">.</span>isEmpty()) {
             nameLine<span class="operator">-</span><span class="operator">&gt;</span>clear();
             addressText<span class="operator">-</span><span class="operator">&gt;</span>clear();
         }

         nameLine<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">true</span>);
         addressText<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">true</span>);
         addButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);

         <span class="type">int</span> number <span class="operator">=</span> contacts<span class="operator">.</span>size();
         editButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(number <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">1</span>);
         removeButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(number <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">1</span>);
         nextButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(number <span class="operator">&gt;</span> <span class="number">1</span>);
         previousButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(number <span class="operator">&gt;</span><span class="number">1</span> );

         submitButton<span class="operator">-</span><span class="operator">&gt;</span>hide();
         cancelButton<span class="operator">-</span><span class="operator">&gt;</span>hide();
         <span class="keyword">break</span>;
     }
 }</pre>
<p>By 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.</p>
</div>
<!-- @@@tutorials/addressbook/part4 -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>