1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/helpviewer/helpviewer.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Simple HTML Help Browser</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Simple HTML Help Browser</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example implements a simple HTML help browser using
|
---|
37 | Qt's richtext capabilities.
|
---|
38 | <p> <hr>
|
---|
39 | <p> Header file:
|
---|
40 | <p> <pre>/****************************************************************************
|
---|
41 | ** $Id: helpviewer-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
42 | **
|
---|
43 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
44 | **
|
---|
45 | ** This file is part of an example program for Qt. This example
|
---|
46 | ** program may be used, distributed and modified without limitation.
|
---|
47 | **
|
---|
48 | *****************************************************************************/
|
---|
49 |
|
---|
50 | #ifndef HELPWINDOW_H
|
---|
51 | #define HELPWINDOW_H
|
---|
52 |
|
---|
53 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
54 | #include <<a href="qtextbrowser-h.html">qtextbrowser.h</a>>
|
---|
55 | #include <<a href="qstringlist-h.html">qstringlist.h</a>>
|
---|
56 | #include <<a href="qmap-h.html">qmap.h</a>>
|
---|
57 | #include <<a href="qdir-h.html">qdir.h</a>>
|
---|
58 |
|
---|
59 | class QComboBox;
|
---|
60 | class QPopupMenu;
|
---|
61 |
|
---|
62 | class HelpWindow : public <a href="qmainwindow.html">QMainWindow</a>
|
---|
63 | {
|
---|
64 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
65 | public:
|
---|
66 | HelpWindow( const <a href="qstring.html">QString</a>& home_, const <a href="qstring.html">QString</a>& path, QWidget* parent = 0, const char *name=0 );
|
---|
67 | ~HelpWindow();
|
---|
68 |
|
---|
69 | private slots:
|
---|
70 | void setBackwardAvailable( bool );
|
---|
71 | void setForwardAvailable( bool );
|
---|
72 |
|
---|
73 | void sourceChanged( const <a href="qstring.html">QString</a>& );
|
---|
74 | void about();
|
---|
75 | void aboutQt();
|
---|
76 | void openFile();
|
---|
77 | void newWindow();
|
---|
78 | void print();
|
---|
79 |
|
---|
80 | void pathSelected( const <a href="qstring.html">QString</a> & );
|
---|
81 | void histChosen( int );
|
---|
82 | void bookmChosen( int );
|
---|
83 | void addBookmark();
|
---|
84 |
|
---|
85 | private:
|
---|
86 | void readHistory();
|
---|
87 | void readBookmarks();
|
---|
88 |
|
---|
89 | <a href="qtextbrowser.html">QTextBrowser</a>* browser;
|
---|
90 | <a href="qcombobox.html">QComboBox</a> *pathCombo;
|
---|
91 | int backwardId, forwardId;
|
---|
92 | <a href="qstringlist.html">QStringList</a> history, bookmarks;
|
---|
93 | <a href="qmap.html">QMap</a><int, QString> mHistory, mBookmarks;
|
---|
94 | <a href="qpopupmenu.html">QPopupMenu</a> *hist, *bookm;
|
---|
95 |
|
---|
96 | };
|
---|
97 |
|
---|
98 |
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | </pre>
|
---|
105 |
|
---|
106 | <p> <hr>
|
---|
107 | <p> Implementation:
|
---|
108 | <p> <pre>/****************************************************************************
|
---|
109 | ** $Id: helpviewer-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
110 | **
|
---|
111 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
112 | **
|
---|
113 | ** This file is part of an example program for Qt. This example
|
---|
114 | ** program may be used, distributed and modified without limitation.
|
---|
115 | **
|
---|
116 | *****************************************************************************/
|
---|
117 |
|
---|
118 | #include "helpwindow.h"
|
---|
119 | #include <<a href="qstatusbar-h.html">qstatusbar.h</a>>
|
---|
120 | #include <<a href="qpixmap-h.html">qpixmap.h</a>>
|
---|
121 | #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
---|
122 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
123 | #include <<a href="qtoolbar-h.html">qtoolbar.h</a>>
|
---|
124 | #include <<a href="qtoolbutton-h.html">qtoolbutton.h</a>>
|
---|
125 | #include <<a href="qiconset-h.html">qiconset.h</a>>
|
---|
126 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
127 | #include <<a href="qtextstream-h.html">qtextstream.h</a>>
|
---|
128 | #include <<a href="qstylesheet-h.html">qstylesheet.h</a>>
|
---|
129 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
130 | #include <<a href="qfiledialog-h.html">qfiledialog.h</a>>
|
---|
131 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
132 | #include <<a href="qcombobox-h.html">qcombobox.h</a>>
|
---|
133 | #include <<a href="qevent-h.html">qevent.h</a>>
|
---|
134 | #include <<a href="qlineedit-h.html">qlineedit.h</a>>
|
---|
135 | #include <<a href="qobjectlist-h.html">qobjectlist.h</a>>
|
---|
136 | #include <<a href="qfileinfo-h.html">qfileinfo.h</a>>
|
---|
137 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
138 | #include <<a href="qdatastream-h.html">qdatastream.h</a>>
|
---|
139 | #include <<a href="qprinter-h.html">qprinter.h</a>>
|
---|
140 | #include <<a href="qsimplerichtext-h.html">qsimplerichtext.h</a>>
|
---|
141 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
142 | #include <<a href="qpaintdevicemetrics-h.html">qpaintdevicemetrics.h</a>>
|
---|
143 |
|
---|
144 | #include <ctype.h>
|
---|
145 |
|
---|
146 | <a name="f350"></a>HelpWindow::HelpWindow( const <a href="qstring.html">QString</a>& home_, const <a href="qstring.html">QString</a>& _path,
|
---|
147 | <a href="qwidget.html">QWidget</a>* parent, const char *name )
|
---|
148 | : <a href="qmainwindow.html">QMainWindow</a>( parent, name, WDestructiveClose ),
|
---|
149 | pathCombo( 0 )
|
---|
150 | {
|
---|
151 | readHistory();
|
---|
152 | readBookmarks();
|
---|
153 |
|
---|
154 | browser = new <a href="qtextbrowser.html">QTextBrowser</a>( this );
|
---|
155 |
|
---|
156 | <a name="x1030"></a> browser-><a href="qtextedit.html#mimeSourceFactory">mimeSourceFactory</a>()->setFilePath( _path );
|
---|
157 | browser-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );
|
---|
158 | <a name="x1027"></a> <a href="qobject.html#connect">connect</a>( browser, SIGNAL( <a href="qtextbrowser.html#sourceChanged">sourceChanged</a>(const <a href="qstring.html">QString</a>& ) ),
|
---|
159 | this, SLOT( sourceChanged( const <a href="qstring.html">QString</a>&) ) );
|
---|
160 |
|
---|
161 | <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( browser );
|
---|
162 |
|
---|
163 | if ( !home_.<a href="qstring.html#isEmpty">isEmpty</a>() )
|
---|
164 | <a name="x1025"></a> browser-><a href="qtextbrowser.html#setSource">setSource</a>( home_ );
|
---|
165 |
|
---|
166 | <a name="x1023"></a> <a href="qobject.html#connect">connect</a>( browser, SIGNAL( <a href="qtextbrowser.html#highlighted">highlighted</a>( const <a href="qstring.html">QString</a>&) ),
|
---|
167 | <a href="qmainwindow.html#statusBar">statusBar</a>(), SLOT( message( const <a href="qstring.html">QString</a>&)) );
|
---|
168 |
|
---|
169 | <a href="qwidget.html#resize">resize</a>( 640,700 );
|
---|
170 |
|
---|
171 | <a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
|
---|
172 | file-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("&New Window"), this, SLOT( newWindow() ), CTRL+Key_N );
|
---|
173 | file-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("&Open File"), this, SLOT( openFile() ), CTRL+Key_O );
|
---|
174 | file-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("&Print"), this, SLOT( print() ), CTRL+Key_P );
|
---|
175 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
176 | file-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("&Close"), this, SLOT( <a href="qwidget.html#close">close</a>() ), CTRL+Key_Q );
|
---|
177 | <a name="x980"></a> file-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("E&xit"), qApp, SLOT( <a href="qapplication.html#closeAllWindows">closeAllWindows</a>() ), CTRL+Key_X );
|
---|
178 |
|
---|
179 | // The same three icons are used twice each.
|
---|
180 | <a href="qiconset.html">QIconSet</a> icon_back( QPixmap("back.xpm") );
|
---|
181 | <a href="qiconset.html">QIconSet</a> icon_forward( QPixmap("forward.xpm") );
|
---|
182 | <a href="qiconset.html">QIconSet</a> icon_home( QPixmap("home.xpm") );
|
---|
183 |
|
---|
184 | <a href="qpopupmenu.html">QPopupMenu</a>* go = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
|
---|
185 | backwardId = go-><a href="qmenudata.html#insertItem">insertItem</a>( icon_back,
|
---|
186 | <a name="x1019"></a> <a href="qobject.html#tr">tr</a>("&Backward"), browser, SLOT( <a href="qtextbrowser.html#backward">backward</a>() ),
|
---|
187 | CTRL+Key_Left );
|
---|
188 | forwardId = go-><a href="qmenudata.html#insertItem">insertItem</a>( icon_forward,
|
---|
189 | <a name="x1021"></a> <a href="qobject.html#tr">tr</a>("&Forward"), browser, SLOT( <a href="qtextbrowser.html#forward">forward</a>() ),
|
---|
190 | CTRL+Key_Right );
|
---|
191 | <a name="x1024"></a> go-><a href="qmenudata.html#insertItem">insertItem</a>( icon_home, tr("&Home"), browser, SLOT( <a href="qtextbrowser.html#home">home</a>() ) );
|
---|
192 |
|
---|
193 | <a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
|
---|
194 | help-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("&About"), this, SLOT( about() ) );
|
---|
195 | help-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("About &Qt"), this, SLOT( aboutQt() ) );
|
---|
196 |
|
---|
197 | hist = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
|
---|
198 | QStringList::Iterator it = history.begin();
|
---|
199 | for ( ; it != history.end(); ++it )
|
---|
200 | mHistory[ hist-><a href="qmenudata.html#insertItem">insertItem</a>( *it ) ] = *it;
|
---|
201 | <a name="x1004"></a> <a href="qobject.html#connect">connect</a>( hist, SIGNAL( <a href="qpopupmenu.html#activated">activated</a>( int ) ),
|
---|
202 | this, SLOT( histChosen( int ) ) );
|
---|
203 |
|
---|
204 | bookm = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
|
---|
205 | bookm-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( "Add Bookmark" ), this, SLOT( addBookmark() ) );
|
---|
206 | bookm-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
207 |
|
---|
208 | QStringList::Iterator it2 = bookmarks.begin();
|
---|
209 | for ( ; it2 != bookmarks.end(); ++it2 )
|
---|
210 | mBookmarks[ bookm-><a href="qmenudata.html#insertItem">insertItem</a>( *it2 ) ] = *it2;
|
---|
211 | <a href="qobject.html#connect">connect</a>( bookm, SIGNAL( <a href="qpopupmenu.html#activated">activated</a>( int ) ),
|
---|
212 | this, SLOT( bookmChosen( int ) ) );
|
---|
213 |
|
---|
214 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( <a href="qobject.html#tr">tr</a>("&File"), file );
|
---|
215 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( <a href="qobject.html#tr">tr</a>("&Go"), go );
|
---|
216 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( <a href="qobject.html#tr">tr</a>( "History" ), hist );
|
---|
217 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( <a href="qobject.html#tr">tr</a>( "Bookmarks" ), bookm );
|
---|
218 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertSeparator();
|
---|
219 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( <a href="qobject.html#tr">tr</a>("&Help"), help );
|
---|
220 |
|
---|
221 | <a href="qmainwindow.html#menuBar">menuBar</a>()->setItemEnabled( forwardId, FALSE);
|
---|
222 | <a href="qmainwindow.html#menuBar">menuBar</a>()->setItemEnabled( backwardId, FALSE);
|
---|
223 | <a name="x1020"></a> <a href="qobject.html#connect">connect</a>( browser, SIGNAL( <a href="qtextbrowser.html#backwardAvailable">backwardAvailable</a>( bool ) ),
|
---|
224 | this, SLOT( setBackwardAvailable( bool ) ) );
|
---|
225 | <a name="x1022"></a> <a href="qobject.html#connect">connect</a>( browser, SIGNAL( <a href="qtextbrowser.html#forwardAvailable">forwardAvailable</a>( bool ) ),
|
---|
226 | this, SLOT( setForwardAvailable( bool ) ) );
|
---|
227 |
|
---|
228 |
|
---|
229 | <a href="qtoolbar.html">QToolBar</a>* toolbar = new <a href="qtoolbar.html">QToolBar</a>( this );
|
---|
230 | <a href="qmainwindow.html#addToolBar">addToolBar</a>( toolbar, "Toolbar");
|
---|
231 | <a href="qtoolbutton.html">QToolButton</a>* button;
|
---|
232 |
|
---|
233 | button = new <a href="qtoolbutton.html">QToolButton</a>( icon_back, tr("Backward"), "", browser, SLOT(<a href="qtextbrowser.html#backward">backward</a>()), toolbar );
|
---|
234 | <a name="x1035"></a> <a href="qobject.html#connect">connect</a>( browser, SIGNAL( <a href="qtextbrowser.html#backwardAvailable">backwardAvailable</a>(bool) ), button, SLOT( <a href="qwidget.html#setEnabled">setEnabled</a>(bool) ) );
|
---|
235 | button-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
236 | button = new <a href="qtoolbutton.html">QToolButton</a>( icon_forward, tr("Forward"), "", browser, SLOT(<a href="qtextbrowser.html#forward">forward</a>()), toolbar );
|
---|
237 | <a href="qobject.html#connect">connect</a>( browser, SIGNAL( <a href="qtextbrowser.html#forwardAvailable">forwardAvailable</a>(bool) ), button, SLOT( <a href="qwidget.html#setEnabled">setEnabled</a>(bool) ) );
|
---|
238 | button-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
239 | button = new <a href="qtoolbutton.html">QToolButton</a>( icon_home, tr("Home"), "", browser, SLOT(<a href="qtextbrowser.html#home">home</a>()), toolbar );
|
---|
240 |
|
---|
241 | <a name="x1033"></a> toolbar-><a href="qtoolbar.html#addSeparator">addSeparator</a>();
|
---|
242 |
|
---|
243 | pathCombo = new <a href="qcombobox.html">QComboBox</a>( TRUE, toolbar );
|
---|
244 | <a name="x981"></a> <a href="qobject.html#connect">connect</a>( pathCombo, SIGNAL( <a href="qcombobox.html#activated">activated</a>( const <a href="qstring.html">QString</a> & ) ),
|
---|
245 | this, SLOT( pathSelected( const <a href="qstring.html">QString</a> & ) ) );
|
---|
246 | <a name="x1034"></a> toolbar-><a href="qtoolbar.html#setStretchableWidget">setStretchableWidget</a>( pathCombo );
|
---|
247 | <a href="qmainwindow.html#setRightJustification">setRightJustification</a>( TRUE );
|
---|
248 | <a href="qmainwindow.html#setDockEnabled">setDockEnabled</a>( DockLeft, FALSE );
|
---|
249 | <a href="qmainwindow.html#setDockEnabled">setDockEnabled</a>( DockRight, FALSE );
|
---|
250 |
|
---|
251 | <a name="x983"></a> pathCombo-><a href="qcombobox.html#insertItem">insertItem</a>( home_ );
|
---|
252 | browser-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
253 |
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | void <a name="f351"></a>HelpWindow::setBackwardAvailable( bool b)
|
---|
258 | {
|
---|
259 | <a href="qmainwindow.html#menuBar">menuBar</a>()->setItemEnabled( backwardId, b);
|
---|
260 | }
|
---|
261 |
|
---|
262 | void <a name="f352"></a>HelpWindow::setForwardAvailable( bool b)
|
---|
263 | {
|
---|
264 | <a href="qmainwindow.html#menuBar">menuBar</a>()->setItemEnabled( forwardId, b);
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | void <a name="f353"></a>HelpWindow::sourceChanged( const <a href="qstring.html">QString</a>& url )
|
---|
269 | {
|
---|
270 | <a name="x1029"></a> if ( browser-><a href="qtextedit.html#documentTitle">documentTitle</a>().isNull() )
|
---|
271 | <a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Helpviewer - " + url );
|
---|
272 | else
|
---|
273 | <a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Helpviewer - " + browser-><a href="qtextedit.html#documentTitle">documentTitle</a>() ) ;
|
---|
274 |
|
---|
275 | if ( !url.<a href="qstring.html#isEmpty">isEmpty</a>() && pathCombo ) {
|
---|
276 | bool exists = FALSE;
|
---|
277 | int i;
|
---|
278 | <a name="x982"></a> for ( i = 0; i < pathCombo-><a href="qcombobox.html#count">count</a>(); ++i ) {
|
---|
279 | <a name="x985"></a> if ( pathCombo-><a href="qcombobox.html#text">text</a>( i ) == url ) {
|
---|
280 | exists = TRUE;
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | if ( !exists ) {
|
---|
285 | pathCombo-><a href="qcombobox.html#insertItem">insertItem</a>( url, 0 );
|
---|
286 | <a name="x984"></a> pathCombo-><a href="qcombobox.html#setCurrentItem">setCurrentItem</a>( 0 );
|
---|
287 | mHistory[ hist-><a href="qmenudata.html#insertItem">insertItem</a>( url ) ] = url;
|
---|
288 | } else
|
---|
289 | pathCombo-><a href="qcombobox.html#setCurrentItem">setCurrentItem</a>( i );
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | HelpWindow::~HelpWindow()
|
---|
294 | {
|
---|
295 | history = mHistory.values();
|
---|
296 |
|
---|
297 | <a name="x986"></a> <a href="qfile.html">QFile</a> f( QDir::<a href="qdir.html#currentDirPath">currentDirPath</a>() + "/.history" );
|
---|
298 | <a name="x989"></a> f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> );
|
---|
299 | <a href="qdatastream.html">QDataStream</a> s( &f );
|
---|
300 | s << history;
|
---|
301 | <a name="x987"></a> f.<a href="qfile.html#close">close</a>();
|
---|
302 |
|
---|
303 | bookmarks = mBookmarks.values();
|
---|
304 |
|
---|
305 | <a href="qfile.html">QFile</a> f2( QDir::<a href="qdir.html#currentDirPath">currentDirPath</a>() + "/.bookmarks" );
|
---|
306 | f2.<a href="qfile.html#open">open</a>( IO_WriteOnly );
|
---|
307 | <a href="qdatastream.html">QDataStream</a> s2( &f2 );
|
---|
308 | s2 << bookmarks;
|
---|
309 | f2.<a href="qfile.html#close">close</a>();
|
---|
310 | }
|
---|
311 |
|
---|
312 | void <a name="f354"></a>HelpWindow::about()
|
---|
313 | {
|
---|
314 | <a name="x994"></a> QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "HelpViewer Example",
|
---|
315 | "<p>This example implements a simple HTML help viewer "
|
---|
316 | "using Qt's rich text capabilities</p>"
|
---|
317 | "<p>It's just about 400 lines of C++ code, so don't expect too much :-)</p>"
|
---|
318 | );
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | void <a name="f355"></a>HelpWindow::aboutQt()
|
---|
323 | {
|
---|
324 | <a name="x995"></a> QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "QBrowser" );
|
---|
325 | }
|
---|
326 |
|
---|
327 | void <a name="f356"></a>HelpWindow::openFile()
|
---|
328 | {
|
---|
329 | #ifndef QT_NO_FILEDIALOG
|
---|
330 | <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( <a href="qstring.html#QString-null">QString::null</a>, QString::null, this );
|
---|
331 | if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() )
|
---|
332 | browser-><a href="qtextbrowser.html#setSource">setSource</a>( fn );
|
---|
333 | #endif
|
---|
334 | }
|
---|
335 |
|
---|
336 | void <a name="f357"></a>HelpWindow::newWindow()
|
---|
337 | {
|
---|
338 | <a name="x1026"></a> ( new HelpWindow(browser-><a href="qtextbrowser.html#source">source</a>(), "qbrowser") )->show();
|
---|
339 | }
|
---|
340 |
|
---|
341 | void <a name="f358"></a>HelpWindow::print()
|
---|
342 | {
|
---|
343 | #ifndef QT_NO_PRINTER
|
---|
344 | <a href="qprinter.html">QPrinter</a> printer( QPrinter::HighResolution );
|
---|
345 | <a name="x1006"></a> printer.<a href="qprinter.html#setFullPage">setFullPage</a>(TRUE);
|
---|
346 | <a name="x1007"></a> if ( printer.<a href="qprinter.html#setup">setup</a>( this ) ) {
|
---|
347 | <a href="qpainter.html">QPainter</a> p( &printer );
|
---|
348 | <a name="x1002"></a> if( !p.<a href="qpainter.html#isActive">isActive</a>() ) // starting printing failed
|
---|
349 | return;
|
---|
350 | <a name="x999"></a> <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics(p.<a href="qpainter.html#device">device</a>());
|
---|
351 | <a name="x997"></a> int dpiy = metrics.<a href="qpaintdevicemetrics.html#logicalDpiY">logicalDpiY</a>();
|
---|
352 | int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
|
---|
353 | <a name="x998"></a><a name="x996"></a> <a href="qrect.html">QRect</a> view( margin, margin, metrics.<a href="qpaintdevicemetrics.html#width">width</a>() - 2*margin, metrics.<a href="qpaintdevicemetrics.html#height">height</a>() - 2*margin );
|
---|
354 | <a name="x1032"></a> <a href="qsimplerichtext.html">QSimpleRichText</a> richText( browser-><a href="qtextedit.html#text">text</a>(),
|
---|
355 | QFont(),
|
---|
356 | <a name="x1028"></a> browser-><a href="qtextedit.html#context">context</a>(),
|
---|
357 | <a name="x1031"></a> browser-><a href="qtextedit.html#styleSheet">styleSheet</a>(),
|
---|
358 | browser-><a href="qtextedit.html#mimeSourceFactory">mimeSourceFactory</a>(),
|
---|
359 | view.<a href="qrect.html#height">height</a>() );
|
---|
360 | <a name="x1016"></a> richText.<a href="qsimplerichtext.html#setWidth">setWidth</a>( &p, view.<a href="qrect.html#width">width</a>() );
|
---|
361 | int page = 1;
|
---|
362 | do {
|
---|
363 | <a name="x1014"></a> richText.<a href="qsimplerichtext.html#draw">draw</a>( &p, margin, margin, view, colorGroup() );
|
---|
364 | <a name="x1010"></a> view.<a href="qrect.html#moveBy">moveBy</a>( 0, view.<a href="qrect.html#height">height</a>() );
|
---|
365 | p.<a href="qpainter.html#translate">translate</a>( 0 , -view.<a href="qrect.html#height">height</a>() );
|
---|
366 | <a name="x1018"></a><a name="x1001"></a> p.<a href="qpainter.html#drawText">drawText</a>( view.<a href="qrect.html#right">right</a>() - p.<a href="qpainter.html#fontMetrics">fontMetrics</a>().width( QString::<a href="qstring.html#number">number</a>(page) ),
|
---|
367 | <a name="x1008"></a> view.<a href="qrect.html#bottom">bottom</a>() + p.<a href="qpainter.html#fontMetrics">fontMetrics</a>().ascent() + 5, QString::number(page) );
|
---|
368 | <a name="x1015"></a> if ( view.<a href="qrect.html#top">top</a>() - margin >= richText.<a href="qsimplerichtext.html#height">height</a>() )
|
---|
369 | break;
|
---|
370 | <a name="x1005"></a> printer.<a href="qprinter.html#newPage">newPage</a>();
|
---|
371 | page++;
|
---|
372 | } while (TRUE);
|
---|
373 | }
|
---|
374 | #endif
|
---|
375 | }
|
---|
376 |
|
---|
377 | void <a name="f359"></a>HelpWindow::pathSelected( const <a href="qstring.html">QString</a> &_path )
|
---|
378 | {
|
---|
379 | browser-><a href="qtextbrowser.html#setSource">setSource</a>( _path );
|
---|
380 | if ( mHistory.values().contains(_path) )
|
---|
381 | mHistory[ hist-><a href="qmenudata.html#insertItem">insertItem</a>( _path ) ] = _path;
|
---|
382 | }
|
---|
383 |
|
---|
384 | void <a name="f360"></a>HelpWindow::readHistory()
|
---|
385 | {
|
---|
386 | <a name="x988"></a> if ( QFile::<a href="qfile.html#exists">exists</a>( QDir::<a href="qdir.html#currentDirPath">currentDirPath</a>() + "/.history" ) ) {
|
---|
387 | <a href="qfile.html">QFile</a> f( QDir::<a href="qdir.html#currentDirPath">currentDirPath</a>() + "/.history" );
|
---|
388 | f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> );
|
---|
389 | <a href="qdatastream.html">QDataStream</a> s( &f );
|
---|
390 | s >> history;
|
---|
391 | f.<a href="qfile.html#close">close</a>();
|
---|
392 | while ( history.count() > 20 )
|
---|
393 | history.remove( history.begin() );
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | void <a name="f361"></a>HelpWindow::readBookmarks()
|
---|
398 | {
|
---|
399 | if ( QFile::<a href="qfile.html#exists">exists</a>( QDir::<a href="qdir.html#currentDirPath">currentDirPath</a>() + "/.bookmarks" ) ) {
|
---|
400 | <a href="qfile.html">QFile</a> f( QDir::<a href="qdir.html#currentDirPath">currentDirPath</a>() + "/.bookmarks" );
|
---|
401 | f.<a href="qfile.html#open">open</a>( IO_ReadOnly );
|
---|
402 | <a href="qdatastream.html">QDataStream</a> s( &f );
|
---|
403 | s >> bookmarks;
|
---|
404 | f.<a href="qfile.html#close">close</a>();
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | void <a name="f362"></a>HelpWindow::histChosen( int i )
|
---|
409 | {
|
---|
410 | if ( mHistory.contains( i ) )
|
---|
411 | browser-><a href="qtextbrowser.html#setSource">setSource</a>( mHistory[ i ] );
|
---|
412 | }
|
---|
413 |
|
---|
414 | void <a name="f363"></a>HelpWindow::bookmChosen( int i )
|
---|
415 | {
|
---|
416 | if ( mBookmarks.contains( i ) )
|
---|
417 | browser-><a href="qtextbrowser.html#setSource">setSource</a>( mBookmarks[ i ] );
|
---|
418 | }
|
---|
419 |
|
---|
420 | void <a name="f364"></a>HelpWindow::addBookmark()
|
---|
421 | {
|
---|
422 | mBookmarks[ bookm-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qwidget.html#caption">caption</a>() ) ] = browser-><a href="qtextedit.html#context">context</a>();
|
---|
423 | }
|
---|
424 | </pre>
|
---|
425 |
|
---|
426 | <p> <hr>
|
---|
427 | <p> Main:
|
---|
428 | <p> <pre>/****************************************************************************
|
---|
429 | ** $Id: helpviewer-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
430 | **
|
---|
431 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
432 | **
|
---|
433 | ** This file is part of an example program for Qt. This example
|
---|
434 | ** program may be used, distributed and modified without limitation.
|
---|
435 | **
|
---|
436 | *****************************************************************************/
|
---|
437 |
|
---|
438 | #include "helpwindow.h"
|
---|
439 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
440 | #include <<a href="qdir-h.html">qdir.h</a>>
|
---|
441 | #include <stdlib.h>
|
---|
442 |
|
---|
443 |
|
---|
444 | int main( int argc, char ** argv )
|
---|
445 | {
|
---|
446 | <a name="x1041"></a> QApplication::<a href="qapplication.html#setColorSpec">setColorSpec</a>( QApplication::ManyColor );
|
---|
447 | <a href="qapplication.html">QApplication</a> a(argc, argv);
|
---|
448 |
|
---|
449 | <a href="qstring.html">QString</a> home;
|
---|
450 | if (argc > 1) {
|
---|
451 | home = argv[1];
|
---|
452 | } else {
|
---|
453 | // Use a hard coded path. It is only an example.
|
---|
454 | home = QDir( "../../doc/html/index.html" ).absPath();
|
---|
455 | }
|
---|
456 |
|
---|
457 | HelpWindow *help = new HelpWindow(home, ".", 0, "help viewer");
|
---|
458 | help-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Helpviewer");
|
---|
459 | <a name="x1037"></a> if ( QApplication::<a href="qapplication.html#desktop">desktop</a>()->width() > 400
|
---|
460 | && QApplication::<a href="qapplication.html#desktop">desktop</a>()->height() > 500 )
|
---|
461 | help-><a href="qwidget.html#show">show</a>();
|
---|
462 | else
|
---|
463 | <a name="x1045"></a> help-><a href="qwidget.html#showMaximized">showMaximized</a>();
|
---|
464 |
|
---|
465 | <a name="x1039"></a> QObject::<a href="qobject.html#connect">connect</a>( &a, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()),
|
---|
466 | &a, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
467 |
|
---|
468 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
469 | }
|
---|
470 | </pre>
|
---|
471 |
|
---|
472 | <p>See also <a href="examples.html">Examples</a>.
|
---|
473 |
|
---|
474 | <!-- eof -->
|
---|
475 | <p><address><hr><div align=center>
|
---|
476 | <table width=100% cellspacing=0 border=0><tr>
|
---|
477 | <td>Copyright © 2007
|
---|
478 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
479 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
480 | </table></div></address></body>
|
---|
481 | </html>
|
---|