source: trunk/doc/src/examples/simpletextviewer.qdoc@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 19.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \example help/simpletextviewer
44 \title Simple Text Viewer Example
45
46 The Simple Text Viewer example shows how to use \QA as a customized
47 help viewer for your application.
48
49 This is done in two stages. Firstly, documentation is created and \QA
50 is customized; secondly, the functionality to launch and control
51 \QA is added to the application.
52
53 \image simpletextviewer-example.png
54
55 The Simple Text Viewer application lets the user select and view
56 existing files.
57
58 The application provides its own custom documentation that is
59 available from the \gui Help menu in the main window's menu bar
60 or by clicking the \gui Help button in the application's find file
61 dialog.
62
63 The example consists of four classes:
64
65 \list
66 \o \c Assistant provides functionality to launch \QA.
67 \o \c MainWindow is the main application window.
68 \o \c FindFileDialog allows the user to search for
69 files using wildcard matching.
70 \o \c TextEdit provides a rich text browser that makes
71 sure that images referenced in HTML documents are
72 displayed properly.
73 \endlist
74
75 Note that we will only comment on the parts of the implementation
76 that are relevant to the main issue, that is making Qt Assistant
77 act as a customized help viewer for our Simple Text Viewer
78 application.
79
80 \section1 Creating Documentation and Customizing \QA
81
82 How to create the actual documentation in the form of HTML pages is
83 not in the scope of this example. In general, HTML pages can either
84 be written by hand or generated with the help of documentation tools
85 like qdoc or Doxygen. For the purposes of this example we assume that
86 the HTML files have already been created. So, the only thing that
87 remains to be done is to tell \QA how to structure and display the
88 help information.
89
90 \section2 Organizing Documentation for \QA
91
92 Plain HTML files only contain text or documentation about specific topics,
93 but they usually include no information about how several HTML documents
94 relate to each other or in which order they are supposed to be read.
95 What is missing is a table of contents along with an index to access
96 certain help contents quickly, without having to browse through a lot
97 of documents in order to find a piece of information.
98
99 To organize the documentation and make it available for \QA, we have
100 to create a Qt help project (.qhp) file. The first and most important
101 part of the project file is the definition of the namespace. The namespace
102 has to be unique and will be the first part of the page URL in \QA.
103 In addition, we have to set a virtual folder which acts as a common
104 folder for documentation sets. This means, that two documentation sets
105 identified by two different namespaces can cross reference HTML files
106 since those files are in one big virtual folder. However, for this
107 example, we'll only have one documentation set available, so the
108 virtual folder name and functionality are not important.
109
110 \code
111 <?xml version="1.0" encoding="UTF-8"?>
112 <QtHelpProject version="1.0">
113 <namespace>com.trolltech.examples.simpletextviewer</namespace>
114 <virtualFolder>doc</virtualFolder>
115 \endcode
116
117 The next step is to define the filter section. A filter section
118 contains the table of contents, indices and a complete list of
119 all documentation files, and can have any number of filter attributes
120 assigned to it. A filter attribute is an ordinary string which can
121 be freely chosen. Later in \QA, users can then define a custom
122 filter referencing those attributes. If the attributes of a filter
123 section match the attributes of the custom filter the documentation
124 will be shown, otherwise \QA will hide the documentation.
125
126 Again, since we'll only have one documentation set, we do not need
127 the filtering functionality of \QA and can therefore skip the
128 filter attributes.
129
130 Now, we build up the table of contents. An item in the table is
131 defined by the \c section tag which contains the attributes for the
132 item title as well as link to the actual page. Section tags can be
133 nested infinitely, but for practical reasons it is not recommended
134 to nest them deeper than three or four levels. For our example we
135 want to use the following outline for the table of contents:
136
137 \list
138 \o Simple Text Viewer
139 \list
140 \o Find File
141 \list
142 \o File Dialog
143 \o Wildcard Matching
144 \o Browse
145 \endlist
146 \o Open File
147 \endlist
148 \endlist
149
150 In the help project file, the outline is represented by:
151
152 \code
153 <filterSection>
154 <toc>
155 <section title="Simple Text Viewer" ref="index.html">
156 <section title="Find File" ref="./findfile.html">
157 <section title="File Dialog" ref="./filedialog.html"></section>
158 <section title="Wildcard Matching" ref="./wildcardmatching.html"></section>
159 <section title="Browse" ref="./browse.html"></section>
160 </section>
161 <section title="Open File" ref="./openfile.html"></section>
162 </section>
163 </toc>
164 \endcode
165
166 After the table of contents is defined, we will list all index keywords:
167
168 \code
169 <keywords>
170 <keyword name="Display" ref="./index.html"/>
171 <keyword name="Rich text" ref="./index.html"/>
172 <keyword name="Plain text" ref="./index.html"/>
173 <keyword name="Find" ref="./findfile.html"/>
174 <keyword name="File menu" ref="./findfile.html"/>
175 <keyword name="File name" ref="./filedialog.html"/>
176 <keyword name="File dialog" ref="./filedialog.html"/>
177 <keyword name="File globbing" ref="./wildcardmatching.html"/>
178 <keyword name="Wildcard matching" ref="./wildcardmatching.html"/>
179 <keyword name="Wildcard syntax" ref="./wildcardmatching.html"/>
180 <keyword name="Browse" ref="./browse.html"/>
181 <keyword name="Directory" ref="./browse.html"/>
182 <keyword name="Open" ref="./openfile.html"/>
183 <keyword name="Select" ref="./openfile.html"/>
184 </keywords>
185 \endcode
186
187 As the last step, we have to list all files making up the documentation.
188 An important point to note here is that all files have to listed, including
189 image files, and even stylesheets if they are used.
190
191 \code
192 <files>
193 <file>browse.html</file>
194 <file>filedialog.html</file>
195 <file>findfile.html</file>
196 <file>index.html</file>
197 <file>intro.html</file>
198 <file>openfile.html</file>
199 <file>wildcardmatching.html</file>
200 <file>images/browse.png</file>
201 <file>images/fadedfilemenu.png</file>
202 <file>images/filedialog.png</file>
203 <file>images/handbook.png</file>
204 <file>images/mainwindow.png</file>
205 <file>images/open.png</file>
206 <file>images/wildcard.png</file>
207 </files>
208 </filterSection>
209 </QtHelpProject>
210 \endcode
211
212 The help project file is now finished. If you want to see the resulting
213 documentation in \QA, you have to generate a Qt compressed help file
214 out of it and register it with the default help collection of \QA.
215
216 \code
217 qhelpgenerator simpletextviewer.qhp -o simpletextviewer.qch
218 assistant -register simpletextviewer.qch
219 \endcode
220
221 If you start \QA now, you'll see the Simple Text Viewer documentation
222 beside the Qt documentation. This is OK for testing purposes, but
223 for the final version we want to only have the Simple Text Viewer
224 documentation in \QA.
225
226 \section2 Customizing \QA
227
228 The easiest way to make \QA only display the Simple Text Viewer
229 documentation is to create our own help collection file. A collection
230 file is stored in a binary format, similar to the compressed help file,
231 and generated from a help collection project file (*.qhcp). With
232 the help of a collection file, we can customize the appearance and even
233 some functionality offered by \QA.
234
235 At first, we change the window title and icon. Instead of showing "\QA"
236 it will show "Simple Text Viewer", so it is much clearer for the user
237 that the help viewer actually belongs to our application.
238
239 \code
240 <?xml version="1.0" encoding="UTF-8"?>
241 <QHelpCollectionProject version="1.0">
242 <assistant>
243 <title>Simple Text Viewer</title>
244 <applicationIcon>images/handbook.png</applicationIcon>
245 <cacheDirectory>Trolltech/SimpleTextViewer</cacheDirectory>
246 \endcode
247
248 The \c cacheDirectory tag specifies a subdirectory of the users
249 data directory (see the
250 \l{Using Qt Assistant as a Custom Help Viewer#Qt Help Collection Files}{Qt Help Collection Files})
251 where the cache file for the full text search or the settings file will
252 be stored.
253
254 After this, we set the page displayed by \QA when launched for the very
255 first time in its new configuration. The URL consists of the namespace
256 and virtual folder defined in the Qt help project file, followed by the
257 actual page file name.
258
259 \code
260 <startPage>qthelp://com.trolltech.examples.simpletextviewer/doc/index.html</startPage>
261 \endcode
262
263 Next, we alter the name of the "About" menu item to "About Simple
264 Text Viewer". The contents of the \gui{About} dialog are also changed
265 by specifying a file where the about text or icon is taken from.
266
267 \code
268 <aboutMenuText>
269 <text>About Simple Text Viewer</text>
270 </aboutMenuText>
271 <aboutDialog>
272 <file>about.txt</file>
273 <icon>images/icon.png</icon>
274 </aboutDialog>
275 \endcode
276
277 \QA offers the possibility to add or remove documentation via its
278 preferences dialog. This functionality is helpful when using \QA
279 as the central help viewer for more applications, but in our case
280 we want to actually prevent the user from removing the documentation.
281 So, we disable the documentation manager.
282
283 Since the address bar is not really relevant in such a small
284 documentation set we switch it off as well. By having just one filter
285 section, without any filter attributes, we can also disable the filter
286 functionality of \QA, which means that the filter page and the filter
287 toolbar will not be available.
288
289 \code
290 <enableDocumentationManager>false</enableDocumentationManager>
291 <enableAddressBar>false</enableAddressBar>
292 <enableFilterFunctionality>false</enableFilterFunctionality>
293 </assistant>
294 \endcode
295
296 For testing purposes, we already generated the compressed help file
297 and registered it with \QA's default help collection. With the
298 following lines we achieve the same result. The only and important
299 difference is that we register the compressed help file, not in
300 the default collection, but in our own collection file.
301
302 \code
303 <docFiles>
304 <generate>
305 <file>
306 <input>simpletextviewer.qhp</input>
307 <output>simpletextviewer.qch</output>
308 </file>
309 </generate>
310 <register>
311 <file>simpletextviewer.qch</file>
312 </register>
313 </docFiles>
314 </QHelpCollectionProject>
315 \endcode
316
317 As the last step, we have to generate the binary collection file
318 out of the help collection project file. This is done by running the
319 \c qcollectiongenerator tool.
320
321 \code
322 qcollectiongenerator simpletextviewer.qhcp -o simpletextviewer.qhc
323 \endcode
324
325 To test all our customizations made to \QA, we add the collection
326 file name to the command line:
327
328 \code
329 assistant -collectionFile simpletextviewer.qhc
330 \endcode
331
332 \section1 Controlling \QA via the Assistant Class
333
334 We will first take a look at how to start and operate \QA from a
335 remote application. For that purpose, we create a class called
336 \c Assistant.
337
338 This class provides a public function that is used to show pages
339 of the documentation, and one private helper function to make sure
340 that \QA is up and running.
341
342 Launching \QA is done in the function \c startAssistant() by simply
343 creating and starting a QProcess. If the process is already running,
344 the function returns immediately. Otherwise, the process has
345 to be set up and started.
346
347 \snippet examples/help/simpletextviewer/assistant.cpp 2
348
349 To start the process we need the executable name of \QA as well as the
350 command line arguments for running \QA in a customized mode. The
351 executable name is a little bit tricky since it depends on the
352 platform, but fortunately it is only different on Mac OS X.
353
354 The displayed documentation can be altered using the \c -collectionFile
355 command line argument when launching \QA. When started without any options,
356 \QA displays a default set of documentation. When Qt is installed,
357 the default documentation set in \QA contains the Qt reference
358 documentation as well as the tools that come with Qt, such as Qt
359 Designer and \c qmake.
360
361 In our example, we replace the default documentation set with our
362 custom documentation by passing our application-specific collection
363 file to the process's command line options.
364
365 As the last argument, we add \c -enableRemoteControl, which makes \QA
366 listen to its \c stdin channel for commands, such as those to display
367 a certain page in the documentation.
368 Then we start the process and wait until it is actually running. If,
369 for some reason \QA cannot be started, \c startAssistant() will return
370 false.
371
372 The implementation for \c showDocumentation() is now straightforward.
373 Firstly, we ensure that \QA is running, then we send the request to
374 display the \a page via the \c stdin channel of the process. It is very
375 important here that the command is terminated by the '\\0' character
376 followed by an end of line token to flush the channel.
377
378 \snippet examples/help/simpletextviewer/assistant.cpp 1
379
380 Finally, we make sure that \QA is terminated properly in the case that
381 the application is shut down. The destructor of QProcess kills the
382 process, meaning that the application has no possibility to do things
383 like save user settings, which would result in corrupted settings files.
384 To avoid this, we ask \QA to terminate in the destructor of the
385 \c Assistant class.
386
387 \snippet examples/help/simpletextviewer/assistant.cpp 0
388
389 \section1 MainWindow Class
390
391 \image simpletextviewer-mainwindow.png
392
393 The \c MainWindow class provides the main application window with
394 two menus: the \gui File menu lets the user open and view an
395 existing file, while the \gui Help menu provides information about
396 the application and about Qt, and lets the user open \QA to
397 display the application's documentation.
398
399 To be able to access the help functionality, we initialize the
400 \c Assistant object in the \c MainWindow's constructor.
401
402 \snippet examples/help/simpletextviewer/mainwindow.cpp 0
403 \dots
404 \snippet examples/help/simpletextviewer/mainwindow.cpp 1
405
406 Then we create all the actions for the Simple Text Viewer application.
407 Of special interest is the \c assistantAct action accessible
408 via the \key{F1} shortcut or the \menu{Help|Help Contents} menu item.
409 This action is connected to the \c showDocumentation() slot of
410 the \c MainWindow class.
411
412 \snippet examples/help/simpletextviewer/mainwindow.cpp 4
413 \dots
414 \snippet examples/help/simpletextviewer/mainwindow.cpp 5
415
416 In the \c showDocumentation() slot, we call the \c showDocumentation()
417 function of the \c Assistant class with the URL of home page of the
418 documentation.
419
420 \snippet examples/help/simpletextviewer/mainwindow.cpp 3
421
422 Finally, we must reimplement the protected QWidget::closeEvent()
423 event handler to ensure that the application's \QA instance is
424 properly closed before we terminate the application.
425
426 \snippet examples/help/simpletextviewer/mainwindow.cpp 2
427
428 \section1 FindFileDialog Class
429
430 \image simpletextviewer-findfiledialog.png
431
432 The Simple Text Viewer application provides a find file dialog
433 allowing the user to search for files using wildcard matching. The
434 search is performed within the specified directory, and the user
435 is given an option to browse the existing file system to find the
436 relevant directory.
437
438 In the constructor we save the references to the \c Assistant
439 and \c QTextEdit objects passed as arguments. The \c Assistant
440 object will be used in the \c FindFileDialog's \c help() slot,
441 as we will see shortly, while the QTextEdit will be used in the
442 dialog's \c openFile() slot to display the chosen file.
443
444 \snippet examples/help/simpletextviewer/findfiledialog.cpp 0
445 \dots
446 \snippet examples/help/simpletextviewer/findfiledialog.cpp 1
447
448 The most relevant member to observe in the \c FindFileDialog
449 class is the private \c help() slot. The slot is connected to the
450 dialog's \gui Help button, and brings the current \QA instance
451 to the foreground with the documentation for the dialog by
452 calling \c Assistant's \c showDocumentation() function.
453
454 \snippet examples/help/simpletextviewer/findfiledialog.cpp 2
455
456 \section1 Summary
457
458 In order to make \QA act as a customized help tool for
459 your application, you must provide your application with a
460 process that controls \QA in addition to a custom help collection
461 file including Qt compressed help files.
462
463 The \l{Using Qt Assistant as a Custom Help Viewer} document contains
464 more information about the options and settings available to
465 applications that use \QA as a custom help viewer.
466*/
Note: See TracBrowser for help on using the repository browser.