]> vaikene.ee Git - evaf/blob - www/pswgen10.html
Merge branch 'master' into qt5
[evaf] / www / pswgen10.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html lang="et" xmlns="http://www.w3.org/1999/xhtml" xml:lang="et">
3
4 <head>
5 <meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
6 <title>eVaf Tutorial - 10 - GUI Module</title>
7 <meta name="Author" content="Enar Väikene" />
8 <meta name="description" content="eVaf Tutorial" />
9 <meta name="keywords" content="evaf c++ application development framework tutorial password generator" />
10 <link rel="StyleSheet" href="evaf.css" type="text/css" media="all" />
11 <link rel="StyleSheet" href="highlight.css" type="text/css" media="all" />
12 </head>
13
14 <body>
15
16 <p>Next: <a href="pswgen11.html">11 - Building GUI Module</a>, Previous: <a href="pswgen09.html">09 - GUI Module</a></p>
17
18 <h1>eVaf Tutorial</h1>
19
20 <h2>10 - GUI Module</h2>
21
22 <p>Create the <tt>gui.cpp</tt> file in the <tt>src/apps/PswGen/GUI</tt> directory:</p>
23
24 <pre class="hl"><span class="hl com">/**</span>
25 <span class="hl com"> * &#64;file PswGen/GUI/gui.cpp</span>
26 <span class="hl com"> */</span>
27
28 <span class="hl ppc">#include</span> <span class="hl pps">&quot;gui.h&quot;</span><span class="hl ppc"></span>
29
30 <span class="hl ppc">#include &lt;QtGui&gt;</span></pre>
31
32 <p>Copy version information files from the <tt>Storage</tt> module:</p>
33
34 <pre>evaf/src/apps/PswGen/GUI $ <code>cp ../Storage/version.{h,rc} .</code></pre>
35
36 <p>Modify the <tt>version.h</tt> file:</p>
37
38 <pre class="hl"><span class="hl com">/**</span>
39 <span class="hl com"> * &#64;file PswGen/GUI/version.h</span>
40 <span class="hl com"> */</span>
41
42 <span class="hl ppc">#ifndef __PSWGEN_GUI_VERSION_H</span>
43 <span class="hl ppc"># define __PSWGEN_GUI_VERSION_H</span>
44
45 <span class="hl ppc">#include &lt;version_rc.h&gt;</span>
46
47 <span class="hl com">/**</span>
48 <span class="hl com"> * Module/library version number in the form major,minor,release,build</span>
49 <span class="hl com"> */</span>
50 <span class="hl ppc">#define VER_FILE_VERSION 0,1,1,1</span>
51
52 <span class="hl com">/**</span>
53 <span class="hl com"> * Module/library version number in the string format (shall end with \0)</span>
54 <span class="hl com"> */</span>
55 <span class="hl ppc">#define VER_FILE_VERSION_STR</span> <span class="hl pps">&quot;0.1.1.2\0&quot;</span><span class="hl ppc"></span>
56
57 <span class="hl com">/**</span>
58 <span class="hl com"> * Module/library name (shall end with \0)</span>
59 <span class="hl com"> */</span>
60 <span class="hl ppc">#define VER_MODULE_NAME_STR</span> <span class="hl pps">&quot;PswGui\0&quot;</span><span class="hl ppc"></span>
61
62 <span class="hl com">/**</span>
63 <span class="hl com"> * Module type (see version_rc.h for all the types)</span>
64 <span class="hl com"> */</span>
65 <span class="hl ppc">#define VER_MODULE_TYPE MT_GENERIC</span>
66
67 <span class="hl com">/**</span>
68 <span class="hl com"> * Module type in the string format (see version_rc for all the types)</span>
69 <span class="hl com"> */</span>
70 <span class="hl ppc">#define VER_MODULE_TYPE_STR MT_GENERIC</span>
71
72 <span class="hl com">/**</span>
73 <span class="hl com"> * Original file name for windows (shall end with \0)</span>
74 <span class="hl com"> */</span>
75 <span class="hl ppc">#define VER_ORIGINAL_FILE_NAME_STR</span> <span class="hl pps">&quot;PswGui.dll\0&quot;</span><span class="hl ppc"></span>
76
77 <span class="hl com">/**</span>
78 <span class="hl com"> * Description of the module/library (shall end with \0)</span>
79 <span class="hl com"> */</span>
80 <span class="hl ppc">#define VER_FILE_DESCRIPTION_STR</span> <span class="hl pps">&quot;User interface for the PswGen application.\0&quot;</span><span class="hl ppc"></span>
81
82 <span class="hl ppc">#endif</span> <span class="hl slc">// version.h</span><span class="hl ppc"></span></pre>
83
84 <p>Include the <tt>version.h</tt> header file in the <tt>gui.cpp</tt> file and export version information:</p>
85
86 <pre class="hl"><span class="hl ppc">#include</span> <span class="hl pps">&quot;version.h&quot;</span><span class="hl ppc"></span>
87 <span class="hl kwd">VER_EXPORT_VERSION_INFO</span><span class="hl opt">()</span></pre>
88
89 <p>Use the <tt>Q_EXPORT_PLUGIN2()</tt> macro to make it a Qt plugin:</p>
90
91 <pre class="hl"><span class="hl kwd">Q_EXPORT_PLUGIN2</span><span class="hl opt">(</span>VER_MODULE_NAME_STR<span class="hl opt">,</span> eVaf<span class="hl opt">::</span>PswGen<span class="hl opt">::</span>GUI<span class="hl opt">::</span>Module<span class="hl opt">)</span></pre>
92
93 <p>The main job of setting up the user interface is done in the <tt>init()</tt> function and our constructor and destructor are
94 very simple:</p>
95
96 <pre class="hl">Module<span class="hl opt">::</span><span class="hl kwd">Module</span><span class="hl opt">()</span>
97 <span class="hl opt">:</span> Plugins<span class="hl opt">::</span><span class="hl kwd">iPlugin</span><span class="hl opt">()</span>
98 <span class="hl opt">,</span> <span class="hl kwd">mReady</span><span class="hl opt">(</span><span class="hl kwa">false</span><span class="hl opt">)</span>
99 <span class="hl opt">,</span> <span class="hl kwd">mGenerator</span><span class="hl opt">(</span><span class="hl num">0</span><span class="hl opt">)</span>
100 <span class="hl opt">,</span> <span class="hl kwd">mStorage</span><span class="hl opt">(</span><span class="hl num">0</span><span class="hl opt">)</span>
101 <span class="hl opt">{</span>
102 <span class="hl kwd">setObjectName</span><span class="hl opt">(</span><span class="hl kwd">QString</span><span class="hl opt">(</span><span class="hl str">&quot;%1.%2&quot;</span><span class="hl opt">).</span><span class="hl kwd">arg</span><span class="hl opt">(</span>VER_MODULE_NAME_STR<span class="hl opt">).</span><span class="hl kwd">arg</span><span class="hl opt">(</span>__FUNCTION__<span class="hl opt">));</span>
103
104 <span class="hl kwd">EVAF_INFO</span><span class="hl opt">(</span><span class="hl str">&quot;%s created&quot;</span><span class="hl opt">,</span> <span class="hl kwd">qPrintable</span><span class="hl opt">(</span><span class="hl kwd">objectName</span><span class="hl opt">()));</span>
105 <span class="hl opt">}</span>
106
107 Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span class="hl opt">()</span>
108 <span class="hl opt">{</span>
109 <span class="hl kwd">EVAF_INFO</span><span class="hl opt">(</span><span class="hl str">&quot;%s destroyed&quot;</span><span class="hl opt">,</span> <span class="hl kwd">qPrintable</span><span class="hl opt">(</span><span class="hl kwd">objectName</span><span class="hl opt">()));</span>
110 <span class="hl opt">}</span></pre>
111
112 <p>The <tt>init()</tt> function:</p>
113
114 <ol>
115 <li>Queries and stores interfaces implemented in other modules. The <tt>iGenerator</tt> interface must be implemented;
116 the <tt>iStorage</tt> interface is optional and we can work without it.</li>
117 <li>Gets the main window interface <tt>iSdiWindow</tt>.</li>
118 <li>Adds our widgets to the main window layout.</li>
119 <li>Connects signals on widgets to slots in this class.</li>
120 <li>Uses actions <tt>QAction</tt> to add keyboard shortcuts -- <tt>Enter</tt> key generates a password and <tt>Esc</tt> key
121 quits the application.</li>
122 <li>Sets the <tt>mReady</tt> flag to true when all this is done.</li>
123 </ol>
124
125 <p>The main window has an empty <tt>QVBoxLayout</tt> layout and we could add our widgets directly to this layout, but for keyboard
126 shortcuts to work properly, we need a widget that fills the main window. We create a master widget, add it to the main window
127 layout and then fill the master widget with other widgets.</p>
128
129 <pre class="hl"><span class="hl kwb">bool</span> Module<span class="hl opt">::</span><span class="hl kwd">init</span><span class="hl opt">(</span>QString <span class="hl kwb">const</span> <span class="hl opt">&amp;</span> args<span class="hl opt">)</span>
130 <span class="hl opt">{</span>
131 <span class="hl kwd">Q_UNUSED</span><span class="hl opt">(</span>args<span class="hl opt">);</span>
132
133 <span class="hl slc">// Get the iGenerator interface</span>
134 <span class="hl kwd">EVAF_TEST_X</span><span class="hl opt">((</span>mGenerator <span class="hl opt">=</span> evafQueryInterface<span class="hl opt">&lt;</span>PswGen<span class="hl opt">::</span>iGenerator<span class="hl opt">&gt;(</span><span class="hl str">&quot;iGenerator&quot;</span><span class="hl opt">)),</span> <span class="hl str">&quot;No iGenerator interface&quot;</span><span class="hl opt">);</span>
135
136 <span class="hl slc">// Get the iStorage interface (can be null)</span>
137 mStorage <span class="hl opt">=</span> evafQueryInterface<span class="hl opt">&lt;</span>PswGen<span class="hl opt">::</span>iStorage<span class="hl opt">&gt;(</span><span class="hl str">&quot;iStorage&quot;</span><span class="hl opt">);</span>
138 <span class="hl kwa">if</span> <span class="hl opt">(!</span>mStorage<span class="hl opt">)</span>
139 <span class="hl kwd">EVAF_WARNING</span><span class="hl opt">(</span><span class="hl str">&quot;No iStorage interface&quot;</span><span class="hl opt">);</span>
140
141 <span class="hl slc">// Get the main window interface and fill it with the widgets</span>
142 SdiWindow<span class="hl opt">::</span>iSdiWindow <span class="hl opt">*</span> win <span class="hl opt">=</span> evafQueryInterface<span class="hl opt">&lt;</span>SdiWindow<span class="hl opt">::</span>iSdiWindow<span class="hl opt">&gt;(</span><span class="hl str">&quot;iSdiWindow&quot;</span><span class="hl opt">);</span>
143 <span class="hl kwd">EVAF_TEST_X</span><span class="hl opt">(</span>win<span class="hl opt">,</span> <span class="hl str">&quot;No iSdiWindow interface&quot;</span><span class="hl opt">);</span>
144
145 QWidget <span class="hl opt">*</span> masterWidget <span class="hl opt">=</span> <span class="hl kwa">new</span> QWidget<span class="hl opt">;</span>
146 win<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
147
148 QVBoxLayout <span class="hl opt">*</span> v <span class="hl opt">=</span> <span class="hl kwa">new</span> QVBoxLayout<span class="hl opt">;</span>
149 masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">setLayout</span><span class="hl opt">(</span>v<span class="hl opt">);</span>
150
151 QGridLayout <span class="hl opt">*</span> g <span class="hl opt">=</span> <span class="hl kwa">new</span> QGridLayout<span class="hl opt">;</span>
152 v<span class="hl opt">-&gt;</span><span class="hl kwd">addLayout</span><span class="hl opt">(</span>g<span class="hl opt">);</span>
153 g<span class="hl opt">-&gt;</span><span class="hl kwd">setColumnStretch</span><span class="hl opt">(</span><span class="hl num">2</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
154
155 QLabel <span class="hl opt">*</span> l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;Master &amp;password:&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
156 l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
157 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>l<span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">);</span>
158
159 wMasterPassword <span class="hl opt">=</span> <span class="hl kwa">new</span> QLineEdit<span class="hl opt">;</span>
160 l<span class="hl opt">-&gt;</span><span class="hl kwd">setBuddy</span><span class="hl opt">(</span>wMasterPassword<span class="hl opt">);</span>
161 <span class="hl kwd">connect</span><span class="hl opt">(</span>wMasterPassword<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)));</span>
162 wMasterPassword<span class="hl opt">-&gt;</span><span class="hl kwd">setEchoMode</span><span class="hl opt">(</span>QLineEdit<span class="hl opt">::</span>Password<span class="hl opt">);</span>
163 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wMasterPassword<span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
164
165 l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;Web site or application &amp;name:&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
166 l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
167 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>l<span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">);</span>
168
169 wName <span class="hl opt">=</span> <span class="hl kwa">new</span> QLineEdit<span class="hl opt">;</span>
170 l<span class="hl opt">-&gt;</span><span class="hl kwd">setBuddy</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
171 <span class="hl kwa">if</span> <span class="hl opt">(</span>mStorage<span class="hl opt">) {</span>
172 QCompleter <span class="hl opt">*</span> completer <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QCompleter</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
173 completer<span class="hl opt">-&gt;</span><span class="hl kwd">setModel</span><span class="hl opt">(</span>mStorage<span class="hl opt">-&gt;</span><span class="hl kwd">autoCompletionModel</span><span class="hl opt">());</span>
174 completer<span class="hl opt">-&gt;</span><span class="hl kwd">setCompletionMode</span><span class="hl opt">(</span>QCompleter<span class="hl opt">::</span>InlineCompletion<span class="hl opt">);</span>
175 wName<span class="hl opt">-&gt;</span><span class="hl kwd">setCompleter</span><span class="hl opt">(</span>completer<span class="hl opt">);</span>
176 <span class="hl opt">}</span>
177 <span class="hl kwd">connect</span><span class="hl opt">(</span>wName<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)));</span>
178 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wName<span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
179 masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">setFocusProxy</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
180
181 l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;&amp;Length of the password:&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
182 l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
183 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>l<span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">);</span>
184
185 wLength <span class="hl opt">=</span> <span class="hl kwa">new</span> QSpinBox<span class="hl opt">;</span>
186 l<span class="hl opt">-&gt;</span><span class="hl kwd">setBuddy</span><span class="hl opt">(</span>wLength<span class="hl opt">);</span>
187 wLength<span class="hl opt">-&gt;</span><span class="hl kwd">setRange</span><span class="hl opt">(</span><span class="hl num">0</span><span class="hl opt">,</span> mGenerator<span class="hl opt">-&gt;</span><span class="hl kwd">maxLength</span><span class="hl opt">());</span>
188 wLength<span class="hl opt">-&gt;</span><span class="hl kwd">setValue</span><span class="hl opt">(</span>DefaultPasswordLength<span class="hl opt">);</span>
189 wLength<span class="hl opt">-&gt;</span><span class="hl kwd">setSpecialValueText</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;Maximum&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
190 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wLength<span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">);</span>
191
192 l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;Password:&quot;</span><span class="hl opt">));</span>
193 l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
194 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>l<span class="hl opt">,</span> <span class="hl num">3</span><span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">);</span>
195
196 wPassword <span class="hl opt">=</span> <span class="hl kwa">new</span> QLineEdit<span class="hl opt">;</span>
197 wPassword<span class="hl opt">-&gt;</span><span class="hl kwd">setReadOnly</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
198 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wPassword<span class="hl opt">,</span> <span class="hl num">3</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
199
200 v<span class="hl opt">-&gt;</span><span class="hl kwd">addStretch</span><span class="hl opt">();</span>
201
202 QHBoxLayout <span class="hl opt">*</span> h <span class="hl opt">=</span> <span class="hl kwa">new</span> QHBoxLayout<span class="hl opt">;</span>
203 h<span class="hl opt">-&gt;</span><span class="hl kwd">addStretch</span><span class="hl opt">();</span>
204 v<span class="hl opt">-&gt;</span><span class="hl kwd">addLayout</span><span class="hl opt">(</span>h<span class="hl opt">);</span>
205
206 wGenerate <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QPushButton</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;&amp;Generate&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
207 wGenerate<span class="hl opt">-&gt;</span><span class="hl kwd">setDisabled</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
208 wGenerate<span class="hl opt">-&gt;</span><span class="hl kwd">setDefault</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
209 <span class="hl kwd">connect</span><span class="hl opt">(</span>wGenerate<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">clicked</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">generateClicked</span><span class="hl opt">()));</span>
210 h<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wGenerate<span class="hl opt">);</span>
211
212 wCopy <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QPushButton</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;&amp;Copy to Clipboard&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
213 wCopy<span class="hl opt">-&gt;</span><span class="hl kwd">setDisabled</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
214 <span class="hl kwd">connect</span><span class="hl opt">(</span>wCopy<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">clicked</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">copyClicked</span><span class="hl opt">()));</span>
215 h<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wCopy<span class="hl opt">);</span>
216
217 QAction <span class="hl opt">*</span> a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
218 a<span class="hl opt">-&gt;</span><span class="hl kwd">setShortcut</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>Key_Return<span class="hl opt">);</span>
219 <span class="hl kwd">connect</span><span class="hl opt">(</span>a<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">triggered</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">generateClicked</span><span class="hl opt">()));</span>
220 masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
221
222 a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
223 a<span class="hl opt">-&gt;</span><span class="hl kwd">setShortcut</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>Key_Escape<span class="hl opt">);</span>
224 <span class="hl kwd">connect</span><span class="hl opt">(</span>a<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">triggered</span><span class="hl opt">()),</span> qApp<span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">quit</span><span class="hl opt">()));</span>
225 masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
226
227 mReady <span class="hl opt">=</span> <span class="hl kwa">true</span><span class="hl opt">;</span>
228
229 <span class="hl kwd">EVAF_INFO</span><span class="hl opt">(</span><span class="hl str">&quot;%s initialized&quot;</span><span class="hl opt">,</span> <span class="hl kwd">qPrintable</span><span class="hl opt">(</span><span class="hl kwd">objectName</span><span class="hl opt">()));</span>
230
231 <span class="hl kwa">return true</span><span class="hl opt">;</span>
232 <span class="hl opt">}</span></pre>
233
234 <p>The <tt>done()</tt> function simply sets the <tt>mReady</tt> flag back to <tt>false</tt>:</p>
235
236 <pre class="hl"><span class="hl kwb">void</span> Module<span class="hl opt">::</span><span class="hl kwd">done</span><span class="hl opt">()</span>
237 <span class="hl opt">{</span>
238 mReady <span class="hl opt">=</span> <span class="hl kwa">false</span><span class="hl opt">;</span>
239
240 <span class="hl kwd">EVAF_INFO</span><span class="hl opt">(</span><span class="hl str">&quot;%s finalized&quot;</span><span class="hl opt">,</span> <span class="hl kwd">qPrintable</span><span class="hl opt">(</span><span class="hl kwd">objectName</span><span class="hl opt">()));</span>
241 <span class="hl opt">}</span></pre>
242
243 <p>The <tt>Generate</tt> push button is disabled by default. We connected <tt>textChanged()</tt> signals to the <tt>textChanged()</tt> slot, where we enable the <tt>Generate</tt> push button if master password and name fields are not empty.</p>
244
245 <p>We can also query for stored passwords and update fields on the window with data from the storage. The <tt>iStorage::query()</tt>
246 method can return an empty shared data pointer and we have to check for it before using fields in the shared data object.</p>
247
248 <pre class="hl"><span class="hl kwb">void</span> Module<span class="hl opt">::</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString <span class="hl kwb">const</span> <span class="hl opt">&amp;)</span>
249 <span class="hl opt">{</span>
250 wGenerate<span class="hl opt">-&gt;</span><span class="hl kwd">setDisabled</span><span class="hl opt">(</span>wMasterPassword<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">() ||</span> wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">());</span>
251 <span class="hl kwa">if</span> <span class="hl opt">(!</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">() &amp;&amp;</span> mStorage<span class="hl opt">) {</span>
252 QExplicitlySharedDataPointer<span class="hl opt">&lt;</span>PswGen<span class="hl opt">::</span>Storage<span class="hl opt">::</span>Data<span class="hl opt">&gt;</span> data <span class="hl opt">=</span> mStorage<span class="hl opt">-&gt;</span><span class="hl kwd">query</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">());</span>
253 <span class="hl kwa">if</span> <span class="hl opt">(</span>data<span class="hl opt">)</span>
254 wLength<span class="hl opt">-&gt;</span><span class="hl kwd">setValue</span><span class="hl opt">(</span>data<span class="hl opt">-&gt;</span><span class="hl kwd">length</span><span class="hl opt">());</span>
255 <span class="hl opt">}</span>
256 <span class="hl opt">}</span></pre>
257
258 <p>The <tt>generateClicked()</tt> slot is connected to the <tt>Generate</tt> push button and also to the <tt>Enter</tt> key
259 action. The push button is disabled if master password or name fields are empty, but not the <tt>Enter</tt> key action and
260 we need to check for it once more.</p>
261
262 <p>Then we generate the password using the <tt>iGenerator::generatePassword()</tt> method and show it on the window. We also
263 enable the <tt>Copy to Clipboard</tt> push button.</p>
264
265 <p>If we have the <tt>iStorage</tt>, we either store the new data record or update an existing one in the storage.</p>
266
267 <pre class="hl"><span class="hl kwb">void</span> Module<span class="hl opt">::</span><span class="hl kwd">generateClicked</span><span class="hl opt">()</span>
268 <span class="hl opt">{</span>
269 <span class="hl kwa">if</span> <span class="hl opt">(</span>wMasterPassword<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">() ||</span> wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">())</span>
270 <span class="hl kwa">return</span><span class="hl opt">;</span>
271 wPassword<span class="hl opt">-&gt;</span><span class="hl kwd">setText</span><span class="hl opt">(</span>mGenerator<span class="hl opt">-&gt;</span><span class="hl kwd">generatePassword</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">(),</span> wMasterPassword<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">(),</span> wLength<span class="hl opt">-&gt;</span><span class="hl kwd">value</span><span class="hl opt">()));</span>
272 wCopy<span class="hl opt">-&gt;</span><span class="hl kwd">setEnabled</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
273 <span class="hl kwa">if</span> <span class="hl opt">(</span>mStorage<span class="hl opt">) {</span>
274 QExplicitlySharedDataPointer<span class="hl opt">&lt;</span>PswGen<span class="hl opt">::</span>Storage<span class="hl opt">::</span>Data<span class="hl opt">&gt;</span> data <span class="hl opt">=</span> mStorage<span class="hl opt">-&gt;</span><span class="hl kwd">query</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">());</span>
275 <span class="hl kwa">if</span> <span class="hl opt">(!</span>data<span class="hl opt">)</span>
276 data <span class="hl opt">=</span> <span class="hl kwa">new</span> Storage<span class="hl opt">::</span><span class="hl kwd">Data</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">(),</span> wLength<span class="hl opt">-&gt;</span><span class="hl kwd">value</span><span class="hl opt">());</span>
277 <span class="hl kwa">else</span>
278 data<span class="hl opt">-&gt;</span><span class="hl kwd">setLength</span><span class="hl opt">(</span>wLength<span class="hl opt">-&gt;</span><span class="hl kwd">value</span><span class="hl opt">());</span>
279 mStorage<span class="hl opt">-&gt;</span><span class="hl kwd">save</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">(),</span> data<span class="hl opt">);</span>
280 <span class="hl opt">}</span>
281 <span class="hl opt">}</span></pre>
282
283 <p>The final method in our module is the <tt>copyClicked()</tt> slot, which simply copies anything from the generated password
284 field to the clipboard:</p>
285
286 <pre class="hl"><span class="hl kwb">void</span> Module<span class="hl opt">::</span><span class="hl kwd">copyClicked</span><span class="hl opt">()</span>
287 <span class="hl opt">{</span>
288 QClipboard <span class="hl opt">*</span> clipboard <span class="hl opt">=</span> QApplication<span class="hl opt">::</span><span class="hl kwd">clipboard</span><span class="hl opt">();</span>
289 <span class="hl kwa">if</span> <span class="hl opt">(</span>clipboard<span class="hl opt">)</span>
290 clipboard<span class="hl opt">-&gt;</span><span class="hl kwd">setText</span><span class="hl opt">(</span>wPassword<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">());</span>
291 <span class="hl opt">}</span></pre>
292
293 <p>Here is the final <tt>gui.cpp</tt> file:</p>
294
295 <pre class="hl"><span class="hl com">/**</span>
296 <span class="hl com"> * &#64;file PswGen/GUI/gui.cpp</span>
297 <span class="hl com"> */</span>
298
299 <span class="hl ppc">#include</span> <span class="hl pps">&quot;gui.h&quot;</span><span class="hl ppc"></span>
300 <span class="hl ppc">#include</span> <span class="hl pps">&quot;version.h&quot;</span><span class="hl ppc"></span>
301
302 <span class="hl ppc">#include</span> <span class="hl pps">&quot;Generator/iGenerator&quot;</span><span class="hl ppc"></span>
303 <span class="hl ppc">#include</span> <span class="hl pps">&quot;Storage/iStorage&quot;</span><span class="hl ppc"></span>
304
305 <span class="hl ppc">#include &lt;Common/Globals&gt;</span>
306 <span class="hl ppc">#include &lt;Common/iLogger&gt;</span>
307 <span class="hl ppc">#include &lt;Common/iRegistry&gt;</span>
308 <span class="hl ppc">#include &lt;SdiWindow/iSdiWindow&gt;</span>
309
310 <span class="hl ppc">#include &lt;QtGui&gt;</span>
311
312 <span class="hl kwd">VER_EXPORT_VERSION_INFO</span><span class="hl opt">()</span>
313 <span class="hl kwd">Q_EXPORT_PLUGIN2</span><span class="hl opt">(</span>VER_MODULE_NAME_STR<span class="hl opt">,</span> eVaf<span class="hl opt">::</span>PswGen<span class="hl opt">::</span>GUI<span class="hl opt">::</span>Module<span class="hl opt">)</span>
314
315 <span class="hl kwa">using namespace</span> eVaf<span class="hl opt">;</span>
316 <span class="hl kwa">using namespace</span> eVaf<span class="hl opt">::</span>PswGen<span class="hl opt">::</span>GUI<span class="hl opt">;</span>
317
318 <span class="hl kwb">int const</span> Module<span class="hl opt">::</span>DefaultPasswordLength <span class="hl opt">=</span> <span class="hl num">16</span><span class="hl opt">;</span>
319
320 Module<span class="hl opt">::</span><span class="hl kwd">Module</span><span class="hl opt">()</span>
321 <span class="hl opt">:</span> Plugins<span class="hl opt">::</span><span class="hl kwd">iPlugin</span><span class="hl opt">()</span>
322 <span class="hl opt">,</span> <span class="hl kwd">mReady</span><span class="hl opt">(</span><span class="hl kwa">false</span><span class="hl opt">)</span>
323 <span class="hl opt">,</span> <span class="hl kwd">mGenerator</span><span class="hl opt">(</span><span class="hl num">0</span><span class="hl opt">)</span>
324 <span class="hl opt">,</span> <span class="hl kwd">mStorage</span><span class="hl opt">(</span><span class="hl num">0</span><span class="hl opt">)</span>
325 <span class="hl opt">{</span>
326 <span class="hl kwd">setObjectName</span><span class="hl opt">(</span><span class="hl kwd">QString</span><span class="hl opt">(</span><span class="hl str">&quot;%1.%2&quot;</span><span class="hl opt">).</span><span class="hl kwd">arg</span><span class="hl opt">(</span>VER_MODULE_NAME_STR<span class="hl opt">).</span><span class="hl kwd">arg</span><span class="hl opt">(</span>__FUNCTION__<span class="hl opt">));</span>
327
328 <span class="hl kwd">EVAF_INFO</span><span class="hl opt">(</span><span class="hl str">&quot;%s created&quot;</span><span class="hl opt">,</span> <span class="hl kwd">qPrintable</span><span class="hl opt">(</span><span class="hl kwd">objectName</span><span class="hl opt">()));</span>
329 <span class="hl opt">}</span>
330
331 Module<span class="hl opt">::~</span><span class="hl kwd">Module</span><span class="hl opt">()</span>
332 <span class="hl opt">{</span>
333 <span class="hl kwd">EVAF_INFO</span><span class="hl opt">(</span><span class="hl str">&quot;%s destroyed&quot;</span><span class="hl opt">,</span> <span class="hl kwd">qPrintable</span><span class="hl opt">(</span><span class="hl kwd">objectName</span><span class="hl opt">()));</span>
334 <span class="hl opt">}</span>
335
336 <span class="hl kwb">bool</span> Module<span class="hl opt">::</span><span class="hl kwd">init</span><span class="hl opt">(</span>QString <span class="hl kwb">const</span> <span class="hl opt">&amp;</span> args<span class="hl opt">)</span>
337 <span class="hl opt">{</span>
338 <span class="hl kwd">Q_UNUSED</span><span class="hl opt">(</span>args<span class="hl opt">);</span>
339
340 <span class="hl slc">// Get the iGenerator interface</span>
341 <span class="hl kwd">EVAF_TEST_X</span><span class="hl opt">((</span>mGenerator <span class="hl opt">=</span> evafQueryInterface<span class="hl opt">&lt;</span>PswGen<span class="hl opt">::</span>iGenerator<span class="hl opt">&gt;(</span><span class="hl str">&quot;iGenerator&quot;</span><span class="hl opt">)),</span> <span class="hl str">&quot;No iGenerator interface&quot;</span><span class="hl opt">);</span>
342
343 <span class="hl slc">// Get the iStorage interface (can be null)</span>
344 mStorage <span class="hl opt">=</span> evafQueryInterface<span class="hl opt">&lt;</span>PswGen<span class="hl opt">::</span>iStorage<span class="hl opt">&gt;(</span><span class="hl str">&quot;iStorage&quot;</span><span class="hl opt">);</span>
345 <span class="hl kwa">if</span> <span class="hl opt">(!</span>mStorage<span class="hl opt">)</span>
346 <span class="hl kwd">EVAF_WARNING</span><span class="hl opt">(</span><span class="hl str">&quot;No iStorage interface&quot;</span><span class="hl opt">);</span>
347
348 <span class="hl slc">// Get the main window interface and fill it with the widgets</span>
349 SdiWindow<span class="hl opt">::</span>iSdiWindow <span class="hl opt">*</span> win <span class="hl opt">=</span> evafQueryInterface<span class="hl opt">&lt;</span>SdiWindow<span class="hl opt">::</span>iSdiWindow<span class="hl opt">&gt;(</span><span class="hl str">&quot;iSdiWindow&quot;</span><span class="hl opt">);</span>
350 <span class="hl kwd">EVAF_TEST_X</span><span class="hl opt">(</span>win<span class="hl opt">,</span> <span class="hl str">&quot;No iSdiWindow interface&quot;</span><span class="hl opt">);</span>
351
352 QWidget <span class="hl opt">*</span> masterWidget <span class="hl opt">=</span> <span class="hl kwa">new</span> QWidget<span class="hl opt">;</span>
353 win<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
354
355 QVBoxLayout <span class="hl opt">*</span> v <span class="hl opt">=</span> <span class="hl kwa">new</span> QVBoxLayout<span class="hl opt">;</span>
356 masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">setLayout</span><span class="hl opt">(</span>v<span class="hl opt">);</span>
357
358 QGridLayout <span class="hl opt">*</span> g <span class="hl opt">=</span> <span class="hl kwa">new</span> QGridLayout<span class="hl opt">;</span>
359 v<span class="hl opt">-&gt;</span><span class="hl kwd">addLayout</span><span class="hl opt">(</span>g<span class="hl opt">);</span>
360 g<span class="hl opt">-&gt;</span><span class="hl kwd">setColumnStretch</span><span class="hl opt">(</span><span class="hl num">2</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
361
362 QLabel <span class="hl opt">*</span> l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;Master &amp;password:&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
363 l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
364 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>l<span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">);</span>
365
366 wMasterPassword <span class="hl opt">=</span> <span class="hl kwa">new</span> QLineEdit<span class="hl opt">;</span>
367 l<span class="hl opt">-&gt;</span><span class="hl kwd">setBuddy</span><span class="hl opt">(</span>wMasterPassword<span class="hl opt">);</span>
368 <span class="hl kwd">connect</span><span class="hl opt">(</span>wMasterPassword<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)));</span>
369 wMasterPassword<span class="hl opt">-&gt;</span><span class="hl kwd">setEchoMode</span><span class="hl opt">(</span>QLineEdit<span class="hl opt">::</span>Password<span class="hl opt">);</span>
370 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wMasterPassword<span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
371
372 l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;Web site or application &amp;name:&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
373 l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
374 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>l<span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">);</span>
375
376 wName <span class="hl opt">=</span> <span class="hl kwa">new</span> QLineEdit<span class="hl opt">;</span>
377 l<span class="hl opt">-&gt;</span><span class="hl kwd">setBuddy</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
378 <span class="hl kwa">if</span> <span class="hl opt">(</span>mStorage<span class="hl opt">) {</span>
379 QCompleter <span class="hl opt">*</span> completer <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QCompleter</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
380 completer<span class="hl opt">-&gt;</span><span class="hl kwd">setModel</span><span class="hl opt">(</span>mStorage<span class="hl opt">-&gt;</span><span class="hl kwd">autoCompletionModel</span><span class="hl opt">());</span>
381 completer<span class="hl opt">-&gt;</span><span class="hl kwd">setCompletionMode</span><span class="hl opt">(</span>QCompleter<span class="hl opt">::</span>InlineCompletion<span class="hl opt">);</span>
382 wName<span class="hl opt">-&gt;</span><span class="hl kwd">setCompleter</span><span class="hl opt">(</span>completer<span class="hl opt">);</span>
383 <span class="hl opt">}</span>
384 <span class="hl kwd">connect</span><span class="hl opt">(</span>wName<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString<span class="hl opt">)));</span>
385 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wName<span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
386 masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">setFocusProxy</span><span class="hl opt">(</span>wName<span class="hl opt">);</span>
387
388 l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;&amp;Length of the password:&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
389 l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
390 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>l<span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">);</span>
391
392 wLength <span class="hl opt">=</span> <span class="hl kwa">new</span> QSpinBox<span class="hl opt">;</span>
393 l<span class="hl opt">-&gt;</span><span class="hl kwd">setBuddy</span><span class="hl opt">(</span>wLength<span class="hl opt">);</span>
394 wLength<span class="hl opt">-&gt;</span><span class="hl kwd">setRange</span><span class="hl opt">(</span><span class="hl num">0</span><span class="hl opt">,</span> mGenerator<span class="hl opt">-&gt;</span><span class="hl kwd">maxLength</span><span class="hl opt">());</span>
395 wLength<span class="hl opt">-&gt;</span><span class="hl kwd">setValue</span><span class="hl opt">(</span>DefaultPasswordLength<span class="hl opt">);</span>
396 wLength<span class="hl opt">-&gt;</span><span class="hl kwd">setSpecialValueText</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;Maximum&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
397 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wLength<span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">);</span>
398
399 l <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QLabel</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;Password:&quot;</span><span class="hl opt">));</span>
400 l<span class="hl opt">-&gt;</span><span class="hl kwd">setAlignment</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>AlignRight<span class="hl opt">);</span>
401 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>l<span class="hl opt">,</span> <span class="hl num">3</span><span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">);</span>
402
403 wPassword <span class="hl opt">=</span> <span class="hl kwa">new</span> QLineEdit<span class="hl opt">;</span>
404 wPassword<span class="hl opt">-&gt;</span><span class="hl kwd">setReadOnly</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
405 g<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wPassword<span class="hl opt">,</span> <span class="hl num">3</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">1</span><span class="hl opt">,</span> <span class="hl num">2</span><span class="hl opt">);</span>
406
407 v<span class="hl opt">-&gt;</span><span class="hl kwd">addStretch</span><span class="hl opt">();</span>
408
409 QHBoxLayout <span class="hl opt">*</span> h <span class="hl opt">=</span> <span class="hl kwa">new</span> QHBoxLayout<span class="hl opt">;</span>
410 h<span class="hl opt">-&gt;</span><span class="hl kwd">addStretch</span><span class="hl opt">();</span>
411 v<span class="hl opt">-&gt;</span><span class="hl kwd">addLayout</span><span class="hl opt">(</span>h<span class="hl opt">);</span>
412
413 wGenerate <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QPushButton</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;&amp;Generate&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
414 wGenerate<span class="hl opt">-&gt;</span><span class="hl kwd">setDisabled</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
415 wGenerate<span class="hl opt">-&gt;</span><span class="hl kwd">setDefault</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
416 <span class="hl kwd">connect</span><span class="hl opt">(</span>wGenerate<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">clicked</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">generateClicked</span><span class="hl opt">()));</span>
417 h<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wGenerate<span class="hl opt">);</span>
418
419 wCopy <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QPushButton</span><span class="hl opt">(</span><span class="hl kwd">tr</span><span class="hl opt">(</span><span class="hl str">&quot;&amp;Copy to Clipboard&quot;</span><span class="hl opt">,</span> VER_MODULE_NAME_STR<span class="hl opt">));</span>
420 wCopy<span class="hl opt">-&gt;</span><span class="hl kwd">setDisabled</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
421 <span class="hl kwd">connect</span><span class="hl opt">(</span>wCopy<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">clicked</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">copyClicked</span><span class="hl opt">()));</span>
422 h<span class="hl opt">-&gt;</span><span class="hl kwd">addWidget</span><span class="hl opt">(</span>wCopy<span class="hl opt">);</span>
423
424 QAction <span class="hl opt">*</span> a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
425 a<span class="hl opt">-&gt;</span><span class="hl kwd">setShortcut</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>Key_Return<span class="hl opt">);</span>
426 <span class="hl kwd">connect</span><span class="hl opt">(</span>a<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">triggered</span><span class="hl opt">()),</span> <span class="hl kwa">this</span><span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">generateClicked</span><span class="hl opt">()));</span>
427 masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
428
429 a <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QAction</span><span class="hl opt">(</span>masterWidget<span class="hl opt">);</span>
430 a<span class="hl opt">-&gt;</span><span class="hl kwd">setShortcut</span><span class="hl opt">(</span>Qt<span class="hl opt">::</span>Key_Escape<span class="hl opt">);</span>
431 <span class="hl kwd">connect</span><span class="hl opt">(</span>a<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">triggered</span><span class="hl opt">()),</span> qApp<span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">quit</span><span class="hl opt">()));</span>
432 masterWidget<span class="hl opt">-&gt;</span><span class="hl kwd">addAction</span><span class="hl opt">(</span>a<span class="hl opt">);</span>
433
434 mReady <span class="hl opt">=</span> <span class="hl kwa">true</span><span class="hl opt">;</span>
435
436 <span class="hl kwd">EVAF_INFO</span><span class="hl opt">(</span><span class="hl str">&quot;%s initialized&quot;</span><span class="hl opt">,</span> <span class="hl kwd">qPrintable</span><span class="hl opt">(</span><span class="hl kwd">objectName</span><span class="hl opt">()));</span>
437
438 <span class="hl kwa">return true</span><span class="hl opt">;</span>
439 <span class="hl opt">}</span>
440
441 <span class="hl kwb">void</span> Module<span class="hl opt">::</span><span class="hl kwd">done</span><span class="hl opt">()</span>
442 <span class="hl opt">{</span>
443 mReady <span class="hl opt">=</span> <span class="hl kwa">false</span><span class="hl opt">;</span>
444
445 <span class="hl kwd">EVAF_INFO</span><span class="hl opt">(</span><span class="hl str">&quot;%s finalized&quot;</span><span class="hl opt">,</span> <span class="hl kwd">qPrintable</span><span class="hl opt">(</span><span class="hl kwd">objectName</span><span class="hl opt">()));</span>
446 <span class="hl opt">}</span>
447
448 <span class="hl kwb">void</span> Module<span class="hl opt">::</span><span class="hl kwd">textChanged</span><span class="hl opt">(</span>QString <span class="hl kwb">const</span> <span class="hl opt">&amp;)</span>
449 <span class="hl opt">{</span>
450 wGenerate<span class="hl opt">-&gt;</span><span class="hl kwd">setDisabled</span><span class="hl opt">(</span>wMasterPassword<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">() ||</span> wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">());</span>
451 <span class="hl kwa">if</span> <span class="hl opt">(!</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">() &amp;&amp;</span> mStorage<span class="hl opt">) {</span>
452 QExplicitlySharedDataPointer<span class="hl opt">&lt;</span>PswGen<span class="hl opt">::</span>Storage<span class="hl opt">::</span>Data<span class="hl opt">&gt;</span> data <span class="hl opt">=</span> mStorage<span class="hl opt">-&gt;</span><span class="hl kwd">query</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">());</span>
453 <span class="hl kwa">if</span> <span class="hl opt">(</span>data<span class="hl opt">)</span>
454 wLength<span class="hl opt">-&gt;</span><span class="hl kwd">setValue</span><span class="hl opt">(</span>data<span class="hl opt">-&gt;</span><span class="hl kwd">length</span><span class="hl opt">());</span>
455 <span class="hl opt">}</span>
456 <span class="hl opt">}</span>
457
458 <span class="hl kwb">void</span> Module<span class="hl opt">::</span><span class="hl kwd">generateClicked</span><span class="hl opt">()</span>
459 <span class="hl opt">{</span>
460 <span class="hl kwa">if</span> <span class="hl opt">(</span>wMasterPassword<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">() ||</span> wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">().</span><span class="hl kwd">isEmpty</span><span class="hl opt">())</span>
461 <span class="hl kwa">return</span><span class="hl opt">;</span>
462 wPassword<span class="hl opt">-&gt;</span><span class="hl kwd">setText</span><span class="hl opt">(</span>mGenerator<span class="hl opt">-&gt;</span><span class="hl kwd">generatePassword</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">(),</span> wMasterPassword<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">(),</span> wLength<span class="hl opt">-&gt;</span><span class="hl kwd">value</span><span class="hl opt">()));</span>
463 wCopy<span class="hl opt">-&gt;</span><span class="hl kwd">setEnabled</span><span class="hl opt">(</span><span class="hl kwa">true</span><span class="hl opt">);</span>
464 <span class="hl kwa">if</span> <span class="hl opt">(</span>mStorage<span class="hl opt">) {</span>
465 QExplicitlySharedDataPointer<span class="hl opt">&lt;</span>PswGen<span class="hl opt">::</span>Storage<span class="hl opt">::</span>Data<span class="hl opt">&gt;</span> data <span class="hl opt">=</span> mStorage<span class="hl opt">-&gt;</span><span class="hl kwd">query</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">());</span>
466 <span class="hl kwa">if</span> <span class="hl opt">(!</span>data<span class="hl opt">)</span>
467 data <span class="hl opt">=</span> <span class="hl kwa">new</span> Storage<span class="hl opt">::</span><span class="hl kwd">Data</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">(),</span> wLength<span class="hl opt">-&gt;</span><span class="hl kwd">value</span><span class="hl opt">());</span>
468 <span class="hl kwa">else</span>
469 data<span class="hl opt">-&gt;</span><span class="hl kwd">setLength</span><span class="hl opt">(</span>wLength<span class="hl opt">-&gt;</span><span class="hl kwd">value</span><span class="hl opt">());</span>
470 mStorage<span class="hl opt">-&gt;</span><span class="hl kwd">save</span><span class="hl opt">(</span>wName<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">(),</span> data<span class="hl opt">);</span>
471 <span class="hl opt">}</span>
472 <span class="hl opt">}</span>
473
474 <span class="hl kwb">void</span> Module<span class="hl opt">::</span><span class="hl kwd">copyClicked</span><span class="hl opt">()</span>
475 <span class="hl opt">{</span>
476 QClipboard <span class="hl opt">*</span> clipboard <span class="hl opt">=</span> QApplication<span class="hl opt">::</span><span class="hl kwd">clipboard</span><span class="hl opt">();</span>
477 <span class="hl kwa">if</span> <span class="hl opt">(</span>clipboard<span class="hl opt">)</span>
478 clipboard<span class="hl opt">-&gt;</span><span class="hl kwd">setText</span><span class="hl opt">(</span>wPassword<span class="hl opt">-&gt;</span><span class="hl kwd">text</span><span class="hl opt">());</span>
479 <span class="hl opt">}</span></pre>
480
481 <p>Build the <a href="pswgen11.html">PswGen application</a>.</p>
482
483 </body>
484 </html>