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

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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