source: trunk/doc/html/qdir-example.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 18.7 KB
RevLine 
[190]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/qdir/qdir.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QDir</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>QDir</h1>
33
34
35<p>
36<hr>
37<p> Main:
38<p> <pre>/****************************************************************************
39** $Id: qdir-example.html 2051 2007-02-21 10:04:20Z chehrlic $
40**
41** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
42**
43** This file is part of an example program for Qt. This example
44** program may be used, distributed and modified without limitation.
45**
46*****************************************************************************/
47
48#include "../dirview/dirview.h"
49#include "qdir.h"
50
51#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
52#include &lt;<a href="qtextview-h.html">qtextview.h</a>&gt;
53#include &lt;<a href="qfileinfo-h.html">qfileinfo.h</a>&gt;
54#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
55#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
56#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;
57#include &lt;<a href="qspinbox-h.html">qspinbox.h</a>&gt;
58#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
59#include &lt;<a href="qmultilineedit-h.html">qmultilineedit.h</a>&gt;
60#include &lt;<a href="qheader-h.html">qheader.h</a>&gt;
61#include &lt;<a href="qevent-h.html">qevent.h</a>&gt;
62#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
63#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
64#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
65#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
66#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
67#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
68#include &lt;<a href="qtooltip-h.html">qtooltip.h</a>&gt;
69
70#include &lt;stdlib.h&gt;
71
72/* XPM */
73static const char *bookmarks[]={
74 "22 14 8 1",
75 "# c #000080",
76 "a c #585858",
77 "b c #000000",
78 "c c #ffffff",
79 "d c #ffffff",
80 "e c #ffffff",
81 "f c #000000",
82 ". c None",
83 "...bb.................",
84 "..bacb....bbb.........",
85 "..badcb.bbccbab.......",
86 "..bacccbadccbab.......",
87 "..baecdbcccdbab.......",
88 "..bacccbacccbab.......",
89 "..badcdbcecdfab.......",
90 "..bacecbacccbab.......",
91 "..baccdbcccdbab.......",
92 "...badcbacdbbab.......",
93 "....bacbcbbccab.......",
94 ".....babbaaaaab.......",
95 ".....bbabbbbbbb.......",
96 "......bb.............."
97};
98
99/* XPM */
100static const char *home[]={
101 "16 15 4 1",
102 "# c #000000",
103 "a c #ffffff",
104 "b c #c0c0c0",
105 ". c None",
106 ".......##.......",
107 "..#...####......",
108 "..#..#aabb#.....",
109 "..#.#aaaabb#....",
110 "..##aaaaaabb#...",
111 "..#aaaaaaaabb#..",
112 ".#aaaaaaaaabbb#.",
113 "###aaaaaaaabb###",
114 "..#aaaaaaaabb#..",
115 "..#aaa###aabb#..",
116 "..#aaa#.#aabb#..",
117 "..#aaa#.#aabb#..",
118 "..#aaa#.#aabb#..",
119 "..#aaa#.#aabb#..",
120 "..#####.######.."
121};
122
123// ****************************************************************************************************
124
125<a name="f495"></a>PixmapView::PixmapView( <a href="qwidget.html">QWidget</a> *parent )
126 : <a href="qscrollview.html">QScrollView</a>( parent )
127{
128 <a href="qscrollview.html#viewport">viewport</a>()-&gt;setBackgroundMode( PaletteBase );
129}
130
131void <a name="f496"></a>PixmapView::setPixmap( const <a href="qpixmap.html">QPixmap</a> &amp;pix )
132{
133 pixmap = pix;
134<a name="x1855"></a> <a href="qscrollview.html#resizeContents">resizeContents</a>( pixmap.<a href="qwidget.html#size">size</a>().width(), pixmap.<a href="qwidget.html#size">size</a>().height() );
135 <a href="qscrollview.html#viewport">viewport</a>()-&gt;repaint( FALSE );
136}
137
138<a name="x1837"></a>void PixmapView::<a href="qscrollview.html#drawContents">drawContents</a>( <a href="qpainter.html">QPainter</a> *p, int cx, int cy, int cw, int ch )
139{
140<a name="x1833"></a> p-&gt;<a href="qpainter.html#fillRect">fillRect</a>( cx, cy, cw, ch, colorGroup().brush( QColorGroup::Base ) );
141<a name="x1832"></a> p-&gt;<a href="qpainter.html#drawPixmap">drawPixmap</a>( 0, 0, pixmap );
142}
143
144// ****************************************************************************************************
145
146<a name="f497"></a>Preview::Preview( <a href="qwidget.html">QWidget</a> *parent )
147 : <a href="qwidgetstack.html">QWidgetStack</a>( parent )
148{
149 normalText = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this );
150<a name="x1845"></a> normalText-&gt;<a href="qtextedit.html#setReadOnly">setReadOnly</a>( TRUE );
151 html = new <a href="qtextview.html">QTextView</a>( this );
152 pixmap = new PixmapView( this );
153 <a href="qwidgetstack.html#raiseWidget">raiseWidget</a>( normalText );
154}
155
156void <a name="f498"></a>Preview::showPreview( const <a href="qurl.html">QUrl</a> &amp;u, int size )
157{
158<a name="x1849"></a> if ( u.<a href="qurl.html#isLocalFile">isLocalFile</a>() ) {
159<a name="x1850"></a> <a href="qstring.html">QString</a> path = u.<a href="qurl.html#path">path</a>();
160 <a href="qfileinfo.html">QFileInfo</a> fi( path );
161<a name="x1825"></a><a name="x1824"></a> if ( fi.<a href="qfileinfo.html#isFile">isFile</a>() &amp;&amp; (int)fi.<a href="qfileinfo.html#size">size</a>() &gt; size * 1000 ) {
162<a name="x1846"></a> normalText-&gt;<a href="qtextedit.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "The File\n%1\nis too large, so I don't show it!" ).arg( path ) );
163 <a href="qwidgetstack.html#raiseWidget">raiseWidget</a>( normalText );
164 return;
165 }
166
167 <a href="qpixmap.html">QPixmap</a> pix( path );
168<a name="x1834"></a> if ( pix.<a href="qpixmap.html#isNull">isNull</a>() ) {
169 if ( fi.<a href="qfileinfo.html#isFile">isFile</a>() ) {
170 <a href="qfile.html">QFile</a> f( path );
171<a name="x1815"></a> if ( f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) ) {
172 <a href="qtextstream.html">QTextStream</a> ts( &amp;f );
173<a name="x1847"></a> <a href="qstring.html">QString</a> text = ts.<a href="qtextstream.html#read">read</a>();
174<a name="x1814"></a> f.<a href="qfile.html#close">close</a>();
175<a name="x1823"></a> if ( fi.<a href="qfileinfo.html#extension">extension</a>().lower().contains( "htm" ) ) {
176<a name="x1844"></a><a name="x1843"></a> <a href="qstring.html">QString</a> url = html-&gt;<a href="qtextedit.html#mimeSourceFactory">mimeSourceFactory</a>()-&gt;makeAbsolute( path, html-&gt;<a href="qtextedit.html#context">context</a>() );
177 html-&gt;<a href="qtextedit.html#setText">setText</a>( text, url );
178 raiseWidget( html );
179 return;
180 } else {
181 normalText-&gt;<a href="qtextedit.html#setText">setText</a>( text );
182 raiseWidget( normalText );
183 return;
184 }
185 }
186 }
187 normalText-&gt;<a href="qtextedit.html#setText">setText</a>( <a href="qstring.html#QString-null">QString::null</a> );
188 raiseWidget( normalText );
189 } else {
190 pixmap-&gt;setPixmap( pix );
191 raiseWidget( pixmap );
192 }
193 } else {
194 normalText-&gt;<a href="qtextedit.html#setText">setText</a>( "I only show local files!" );
195 raiseWidget( normalText );
196 }
197}
198
199// ****************************************************************************************************
200
201<a name="f499"></a>PreviewWidget::PreviewWidget( <a href="qwidget.html">QWidget</a> *parent )
202 : <a href="qvbox.html">QVBox</a>( parent ), QFilePreview()
203{
204 setSpacing( 5 );
205 setMargin( 5 );
206 <a href="qhbox.html">QHBox</a> *row = new <a href="qhbox.html">QHBox</a>( this );
207<a name="x1826"></a> row-&gt;<a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
208 (void)new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "Only show files smaller than: " ), row );
209 sizeSpinBox = new <a href="qspinbox.html">QSpinBox</a>( 1, 10000, 1, row );
210<a name="x1838"></a> sizeSpinBox-&gt;<a href="qspinbox.html#setSuffix">setSuffix</a>( " KB" );
211<a name="x1839"></a> sizeSpinBox-&gt;<a href="qspinbox.html#setValue">setValue</a>( 64 );
212<a name="x1852"></a><a name="x1840"></a> row-&gt;<a href="qwidget.html#setFixedHeight">setFixedHeight</a>( 10 + sizeSpinBox-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().height() );
213 preview = new Preview( this );
214}
215
216void <a name="f500"></a>PreviewWidget::previewUrl( const <a href="qurl.html">QUrl</a> &amp;u )
217{
218<a name="x1841"></a> preview-&gt;showPreview( u, sizeSpinBox-&gt;<a href="qspinbox.html#value">value</a>() );
219}
220
221// ****************************************************************************************************
222
223<a name="f491"></a>CustomFileDialog::CustomFileDialog()
224 : <a href="qfiledialog.html">QFileDialog</a>( 0, 0, TRUE )
225{
226 <a href="qfiledialog.html#setDir">setDir</a>( "/" );
227
228 dirView = new DirectoryView( this, 0, TRUE );
229<a name="x1827"></a> dirView-&gt;<a href="qlistview.html#addColumn">addColumn</a>( "" );
230<a name="x1828"></a> dirView-&gt;<a href="qlistview.html#header">header</a>()-&gt;hide();
231 ::Directory *root = new ::Directory( dirView, "/" );
232 root-&gt;setOpen( TRUE );
233<a name="x1853"></a> dirView-&gt;<a href="qwidget.html#setFixedWidth">setFixedWidth</a>( 150 );
234
235 <a href="qfiledialog.html#addLeftWidget">addLeftWidget</a>( dirView );
236
237 <a href="qpushbutton.html">QPushButton</a> *p = new <a href="qpushbutton.html">QPushButton</a>( this );
238<a name="x1811"></a> p-&gt;<a href="qbutton.html#setPixmap">setPixmap</a>( QPixmap( bookmarks ) );
239<a name="x1848"></a> QToolTip::<a href="qtooltip.html#add">add</a>( p, tr( "Bookmarks" ) );
240
241 bookmarkMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
242<a name="x1835"></a> <a href="qobject.html#connect">connect</a>( bookmarkMenu, SIGNAL( <a href="qpopupmenu.html#activated">activated</a>( int ) ),
243 this, SLOT( bookmarkChosen( int ) ) );
244 addId = bookmarkMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( "Add bookmark" ) );
245 bookmarkMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
246
247 <a href="qfile.html">QFile</a> f( ".bookmarks" );
248 if ( f.<a href="qfile.html#open">open</a>( IO_ReadOnly ) ) {
249 <a href="qdatastream.html">QDataStream</a> ds( &amp;f );
250 ds &gt;&gt; bookmarkList;
251 f.<a href="qfile.html#close">close</a>();
252
253 QStringList::Iterator it = bookmarkList.begin();
254 for ( ; it != bookmarkList.end(); ++it ) {
255 bookmarkMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( *it );
256 }
257 }
258
259<a name="x1836"></a> p-&gt;<a href="qpushbutton.html#setPopup">setPopup</a>( bookmarkMenu );
260
261 <a href="qfiledialog.html#addToolButton">addToolButton</a>( p, TRUE );
262
263 <a href="qobject.html#connect">connect</a>( dirView, SIGNAL( folderSelected( const <a href="qstring.html">QString</a> &amp; ) ),
264 this, SLOT( setDir2( const <a href="qstring.html">QString</a> &amp; ) ) );
265 <a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qfiledialog.html#dirEntered">dirEntered</a>( const <a href="qstring.html">QString</a> &amp; ) ),
266 dirView, SLOT( <a href="qfiledialog.html#setDir">setDir</a>( const <a href="qstring.html">QString</a> &amp; ) ) );
267
268 <a href="qtoolbutton.html">QToolButton</a> *b = new <a href="qtoolbutton.html">QToolButton</a>( this );
269 QToolTip::<a href="qtooltip.html#add">add</a>( b, tr( "Go Home!" ) );
270 b-&gt;<a href="qbutton.html#setPixmap">setPixmap</a>( QPixmap( home ) );
271 <a href="qobject.html#connect">connect</a>( b, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ),
272 this, SLOT( goHome() ) );
273
274 <a href="qfiledialog.html#addToolButton">addToolButton</a>( b );
275
276 <a href="qwidget.html#resize">resize</a>( <a href="qwidget.html#width">width</a>() + width() / 3, height() );
277}
278
279CustomFileDialog::~CustomFileDialog()
280{
281 if ( !bookmarkList.isEmpty() ) {
282 <a href="qfile.html">QFile</a> f( ".bookmarks" );
283 if ( f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) {
284 <a href="qdatastream.html">QDataStream</a> ds( &amp;f );
285 ds &lt;&lt; bookmarkList;
286 f.<a href="qfile.html#close">close</a>();
287 }
288 }
289}
290
291void <a name="f492"></a>CustomFileDialog::setDir2( const <a href="qstring.html">QString</a> &amp;s )
292{
293 <a href="qobject.html#blockSignals">blockSignals</a>( TRUE );
294 <a href="qfiledialog.html#setDir">setDir</a>( s );
295 <a href="qobject.html#blockSignals">blockSignals</a>( FALSE );
296}
297
298<a name="x1854"></a>void CustomFileDialog::<a href="qwidget.html#showEvent">showEvent</a>( <a href="qshowevent.html">QShowEvent</a> *e )
299{
300 QFileDialog::<a href="qwidget.html#showEvent">showEvent</a>( e );
301 dirView-&gt;setDir( <a href="qfiledialog.html#dirPath">dirPath</a>() );
302}
303
304void <a name="f493"></a>CustomFileDialog::bookmarkChosen( int i )
305{
306 if ( i == addId ) {
307 bookmarkList &lt;&lt; dirPath();
308 bookmarkMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( <a href="qfiledialog.html#dirPath">dirPath</a>() );
309 } else {
310<a name="x1831"></a> <a href="qfiledialog.html#setDir">setDir</a>( bookmarkMenu-&gt;<a href="qmenudata.html#text">text</a>( i ) );
311 }
312}
313
314void <a name="f494"></a>CustomFileDialog::goHome()
315{
316 if ( getenv( "HOME" ) )
317 <a href="qfiledialog.html#setDir">setDir</a>( getenv( "HOME" ) );
318 else
319 <a href="qfiledialog.html#setDir">setDir</a>( "/" );
320}
321
322// ****************************************************************************************************
323
324int main( int argc, char ** argv )
325{
326 QFileDialog::Mode mode = QFileDialog::ExistingFile;
327 <a href="qstring.html">QString</a> start;
328 <a href="qstring.html">QString</a> filter;
329 <a href="qstring.html">QString</a> caption;
330 bool preview = FALSE;
331 bool custom = FALSE;
332 <a href="qapplication.html">QApplication</a> a( argc, argv );
333 for (int i=1; i&lt;argc; i++) {
334 <a href="qstring.html">QString</a> arg = argv[i];
335 if ( arg == "-any" )
336 mode = QFileDialog::AnyFile;
337 else if ( arg == "-dir" )
338 mode = QFileDialog::Directory;
339 else if ( arg == "-default" )
340 start = argv[++i];
341 else if ( arg == "-filter" )
342 filter = argv[++i];
343 else if ( arg == "-preview" )
344 preview = TRUE;
345 else if ( arg == "-custom" )
346 custom = TRUE;
347 else if ( arg[0] == '-' ) {
348 <a href="qapplication.html#qDebug">qDebug</a>("Usage: qdir [-any | -dir | -custom] [-preview] [-default f] {-filter f} [caption ...]\n"
349 " -any Get any filename, need not exist.\n"
350 " -dir Return a directory rather than a file.\n"
351 " -custom Opens a customized QFileDialog with \n"
352 " dir browser, bookmark menu, etc.\n"
353 " -preview Show a preview widget.\n"
354 " -default f Start from directory/file f.\n"
355 " -filter f eg. '*.gif' '*.bmp'\n"
356 " caption ... Caption for dialog.\n"
357 );
358 return 1;
359 } else {
360<a name="x1842"></a> if ( !caption.<a href="qstring.html#isNull">isNull</a>() )
361 caption += ' ';
362 caption += arg;
363 }
364 }
365
366 if ( !start )
367<a name="x1813"></a> start = QDir::<a href="qdir.html#currentDirPath">currentDirPath</a>();
368
369 if ( !caption )
370 caption = mode == QFileDialog::Directory
371 ? "Choose directory..." : "Choose file...";
372
373 if ( !custom ) {
374 <a href="qfiledialog.html">QFileDialog</a> fd( QString::null, filter, 0, 0, TRUE );
375<a name="x1819"></a> fd.<a href="qfiledialog.html#setMode">setMode</a>( mode );
376 if ( preview ) {
377<a name="x1818"></a> fd.<a href="qfiledialog.html#setContentsPreviewEnabled">setContentsPreviewEnabled</a>( TRUE );
378 PreviewWidget *pw = new PreviewWidget( &amp;fd );
379<a name="x1817"></a> fd.<a href="qfiledialog.html#setContentsPreview">setContentsPreview</a>( pw, pw );
380<a name="x1822"></a> fd.<a href="qfiledialog.html#setViewMode">setViewMode</a>( QFileDialog::List );
381<a name="x1820"></a> fd.<a href="qfiledialog.html#setPreviewMode">setPreviewMode</a>( QFileDialog::Contents );
382 }
383 fd.<a href="qwidget.html#setCaption">setCaption</a>( caption );
384<a name="x1821"></a> fd.<a href="qfiledialog.html#setSelection">setSelection</a>( start );
385<a name="x1812"></a> if ( fd.<a href="qdialog.html#exec">exec</a>() == QDialog::Accepted ) {
386<a name="x1816"></a> <a href="qstring.html">QString</a> result = fd.<a href="qfiledialog.html#selectedFile">selectedFile</a>();
387 printf("%s\n", (const char*)result);
388 return 0;
389 } else {
390 return 1;
391 }
392 } else {
393 CustomFileDialog fd;
394 fd.<a href="qdialog.html#exec">exec</a>();
395 return 1;
396 }
397}
398</pre>
399
400<p>See also <a href="examples.html">Examples</a>.
401
402<!-- eof -->
403<p><address><hr><div align=center>
404<table width=100% cellspacing=0 border=0><tr>
405<td>Copyright &copy; 2007
406<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
407<td align=right><div align=right>Qt 3.3.8</div>
408</table></div></address></body>
409</html>
Note: See TracBrowser for help on using the repository browser.