source: trunk/doc/html/dirview-example.html@ 190

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

reference documentation added

File size: 29.6 KB
Line 
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/dirview/dirview.doc:5 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>A Directory Browser</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>A Directory Browser</h1>
33
34
35<p>
36This example program demonstrates how to use a listview and
37listview items to build a multi-column hierarchical, memory- and
38CPU-efficient directory browser. It also demonstrates how to use
39Drag&Drop in a listview.
40<p> <hr>
41<p> Header file:
42<p> <pre>/****************************************************************************
43** $Id: dirview-example.html 2051 2007-02-21 10:04:20Z chehrlic $
44**
45** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
46**
47** This file is part of an example program for Qt. This example
48** program may be used, distributed and modified without limitation.
49**
50*****************************************************************************/
51
52#ifndef DIRVIEW_H
53#define DIRVIEW_H
54
55#include &lt;<a href="qlistview-h.html">qlistview.h</a>&gt;
56#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
57#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
58#include &lt;<a href="qfileinfo-h.html">qfileinfo.h</a>&gt;
59#include &lt;<a href="qtimer-h.html">qtimer.h</a>&gt;
60
61class QWidget;
62class QDragEnterEvent;
63class QDragMoveEvent;
64class QDragLeaveEvent;
65class QDropEvent;
66
67class FileItem : public <a href="qlistviewitem.html">QListViewItem</a>
68{
69public:
70 FileItem( <a href="qlistviewitem.html">QListViewItem</a> *parent, const <a href="qstring.html">QString</a> &amp;s1, const <a href="qstring.html">QString</a> &amp;s2 )
71 : <a href="qlistviewitem.html">QListViewItem</a>( parent, s1, s2 ), pix( 0 ) {}
72
73 const <a href="qpixmap.html">QPixmap</a> *pixmap( int i ) const;
74#if !defined(Q_NO_USING_KEYWORD)
75 using QListViewItem::setPixmap;
76#endif
77 void setPixmap( <a href="qpixmap.html">QPixmap</a> *p );
78
79private:
80 <a href="qpixmap.html">QPixmap</a> *pix;
81
82};
83
84class Directory : public <a href="qlistviewitem.html">QListViewItem</a>
85{
86public:
87 Directory( <a href="qlistview.html">QListView</a> * parent, const <a href="qstring.html">QString</a>&amp; filename );
88 Directory( Directory * parent, const <a href="qstring.html">QString</a>&amp; filename, const <a href="qstring.html">QString</a> &amp;col2 )
89 : <a href="qlistviewitem.html">QListViewItem</a>( parent, filename, col2 ), pix( 0 ) {}
90 Directory( Directory * parent, const <a href="qstring.html">QString</a>&amp; filename );
91
92 <a href="qstring.html">QString</a> text( int column ) const;
93
94 <a href="qstring.html">QString</a> fullName();
95
96 void setOpen( bool );
97 void setup();
98
99 const <a href="qpixmap.html">QPixmap</a> *pixmap( int i ) const;
100#if !defined(Q_NO_USING_KEYWORD)
101 using QListViewItem::setPixmap;
102#endif
103 void setPixmap( <a href="qpixmap.html">QPixmap</a> *p );
104
105private:
106 <a href="qfile.html">QFile</a> f;
107 Directory * p;
108 bool readable;
109 bool showDirsOnly;
110 <a href="qpixmap.html">QPixmap</a> *pix;
111
112};
113
114class DirectoryView : public <a href="qlistview.html">QListView</a>
115{
116 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
117
118public:
119 DirectoryView( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0, bool sdo = FALSE );
120 bool showDirsOnly() { return dirsOnly; }
121
122public slots:
123 void setDir( const <a href="qstring.html">QString</a> &amp; );
124
125signals:
126 void folderSelected( const <a href="qstring.html">QString</a> &amp; );
127
128protected slots:
129 void slotFolderSelected( <a href="qlistviewitem.html">QListViewItem</a> * );
130 void openFolder();
131
132protected:
133 void contentsDragEnterEvent( <a href="qdragenterevent.html">QDragEnterEvent</a> *e );
134 void contentsDragMoveEvent( <a href="qdragmoveevent.html">QDragMoveEvent</a> *e );
135 void contentsDragLeaveEvent( <a href="qdragleaveevent.html">QDragLeaveEvent</a> *e );
136 void contentsDropEvent( <a href="qdropevent.html">QDropEvent</a> *e );
137 void contentsMouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
138 void contentsMousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
139 void contentsMouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
140
141private:
142 <a href="qstring.html">QString</a> fullPath(QListViewItem* item);
143 bool dirsOnly;
144 <a href="qlistviewitem.html">QListViewItem</a> *oldCurrent;
145 <a href="qlistviewitem.html">QListViewItem</a> *dropItem;
146 <a href="qtimer.html">QTimer</a>* autoopen_timer;
147 <a href="qpoint.html">QPoint</a> presspos;
148 bool mousePressed;
149
150};
151
152#endif
153</pre>
154
155<p> <hr>
156<p> And here is the main implementation file. Note the way the program
157scans subdirectories only when it has to. This allows the program
158to handle very large file systems efficiently. The same technique
159can be used in any other trees.
160<p> <pre>/****************************************************************************
161** $Id: dirview-example.html 2051 2007-02-21 10:04:20Z chehrlic $
162**
163** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
164**
165** This file is part of an example program for Qt. This example
166** program may be used, distributed and modified without limitation.
167**
168*****************************************************************************/
169
170#include "dirview.h"
171
172#include &lt;<a href="qdir-h.html">qdir.h</a>&gt;
173#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
174#include &lt;<a href="qfileinfo-h.html">qfileinfo.h</a>&gt;
175#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
176#include &lt;<a href="qevent-h.html">qevent.h</a>&gt;
177#include &lt;<a href="qpoint-h.html">qpoint.h</a>&gt;
178#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
179#include &lt;<a href="qdragobject-h.html">qdragobject.h</a>&gt;
180#include &lt;<a href="qmime-h.html">qmime.h</a>&gt;
181#include &lt;<a href="qstrlist-h.html">qstrlist.h</a>&gt;
182#include &lt;<a href="qstringlist-h.html">qstringlist.h</a>&gt;
183#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
184#include &lt;<a href="qheader-h.html">qheader.h</a>&gt;
185
186static const char* folder_closed_xpm[]={
187 "16 16 9 1",
188 "g c #808080",
189 "b c #c0c000",
190 "e c #c0c0c0",
191 "# c #000000",
192 "c c #ffff00",
193 ". c None",
194 "a c #585858",
195 "f c #a0a0a4",
196 "d c #ffffff",
197 "..###...........",
198 ".#abc##.........",
199 ".#daabc#####....",
200 ".#ddeaabbccc#...",
201 ".#dedeeabbbba...",
202 ".#edeeeeaaaab#..",
203 ".#deeeeeeefe#ba.",
204 ".#eeeeeeefef#ba.",
205 ".#eeeeeefeff#ba.",
206 ".#eeeeefefff#ba.",
207 ".##geefeffff#ba.",
208 "...##gefffff#ba.",
209 ".....##fffff#ba.",
210 ".......##fff#b##",
211 ".........##f#b##",
212 "...........####."};
213
214static const char* folder_open_xpm[]={
215 "16 16 11 1",
216 "# c #000000",
217 "g c #c0c0c0",
218 "e c #303030",
219 "a c #ffa858",
220 "b c #808080",
221 "d c #a0a0a4",
222 "f c #585858",
223 "c c #ffdca8",
224 "h c #dcdcdc",
225 "i c #ffffff",
226 ". c None",
227 "....###.........",
228 "....#ab##.......",
229 "....#acab####...",
230 "###.#acccccca#..",
231 "#ddefaaaccccca#.",
232 "#bdddbaaaacccab#",
233 ".eddddbbaaaacab#",
234 ".#bddggdbbaaaab#",
235 "..edgdggggbbaab#",
236 "..#bgggghghdaab#",
237 "...ebhggghicfab#",
238 "....#edhhiiidab#",
239 "......#egiiicfb#",
240 "........#egiibb#",
241 "..........#egib#",
242 "............#ee#"};
243
244static const char * folder_locked[]={
245 "16 16 10 1",
246 "h c #808080",
247 "b c #ffa858",
248 "f c #c0c0c0",
249 "e c #c05800",
250 "# c #000000",
251 "c c #ffdca8",
252 ". c None",
253 "a c #585858",
254 "g c #a0a0a4",
255 "d c #ffffff",
256 "..#a#...........",
257 ".#abc####.......",
258 ".#daa#eee#......",
259 ".#ddf#e##b#.....",
260 ".#dfd#e#bcb##...",
261 ".#fdccc#daaab#..",
262 ".#dfbbbccgfg#ba.",
263 ".#ffb#ebbfgg#ba.",
264 ".#ffbbe#bggg#ba.",
265 ".#fffbbebggg#ba.",
266 ".##hf#ebbggg#ba.",
267 "...###e#gggg#ba.",
268 ".....#e#gggg#ba.",
269 "......###ggg#b##",
270 ".........##g#b##",
271 "...........####."};
272
273static const char * pix_file []={
274 "16 16 7 1",
275 "# c #000000",
276 "b c #ffffff",
277 "e c #000000",
278 "d c #404000",
279 "c c #c0c000",
280 "a c #ffffc0",
281 ". c None",
282 "................",
283 ".........#......",
284 "......#.#a##....",
285 ".....#b#bbba##..",
286 "....#b#bbbabbb#.",
287 "...#b#bba##bb#..",
288 "..#b#abb#bb##...",
289 ".#a#aab#bbbab##.",
290 "#a#aaa#bcbbbbbb#",
291 "#ccdc#bcbbcbbb#.",
292 ".##c#bcbbcabb#..",
293 "...#acbacbbbe...",
294 "..#aaaacaba#....",
295 "...##aaaaa#.....",
296 ".....##aa#......",
297 ".......##......."};
298
299QPixmap *folderLocked = 0;
300QPixmap *folderClosed = 0;
301QPixmap *folderOpen = 0;
302QPixmap *fileNormal = 0;
303
304/*****************************************************************************
305 *
306 * Class Directory
307 *
308 *****************************************************************************/
309
310<a name="f476"></a>Directory::Directory( Directory * parent, const <a href="qstring.html">QString</a>&amp; filename )
311 : <a href="qlistviewitem.html">QListViewItem</a>( parent ), f(filename),
312 showDirsOnly( parent-&gt;showDirsOnly ),
313 pix( 0 )
314{
315 p = parent;
316 readable = QDir( fullName() ).isReadable();
317
318 if ( !readable )
319 <a href="qlistviewitem.html#setPixmap">setPixmap</a>( folderLocked );
320 else
321 <a href="qlistviewitem.html#setPixmap">setPixmap</a>( folderClosed );
322}
323
324
325Directory::Directory( <a href="qlistview.html">QListView</a> * parent, const <a href="qstring.html">QString</a>&amp; filename )
326 : <a href="qlistviewitem.html">QListViewItem</a>( parent ), f(filename),
327 showDirsOnly( ( (DirectoryView*)parent )-&gt;showDirsOnly() ),
328 pix( 0 )
329{
330 p = 0;
331 readable = QDir( fullName() ).isReadable();
332}
333
334
335<a name="x1691"></a>void Directory::<a href="qlistviewitem.html#setPixmap">setPixmap</a>( <a href="qpixmap.html">QPixmap</a> *px )
336{
337 pix = px;
338 <a href="qlistviewitem.html#setup">setup</a>();
339 <a href="qlistviewitem.html#widthChanged">widthChanged</a>( 0 );
340 <a href="qlistviewitem.html#invalidateHeight">invalidateHeight</a>();
341 <a href="qlistviewitem.html#repaint">repaint</a>();
342}
343
344
345<a name="x1689"></a>const QPixmap *Directory::<a href="qlistviewitem.html#pixmap">pixmap</a>( int i ) const
346{
347 if ( i )
348 return 0;
349 return pix;
350}
351
352<a name="x1690"></a>void Directory::<a href="qlistviewitem.html#setOpen">setOpen</a>( bool o )
353{
354 if ( o )
355 <a href="qlistviewitem.html#setPixmap">setPixmap</a>( folderOpen );
356 else
357 <a href="qlistviewitem.html#setPixmap">setPixmap</a>( folderClosed );
358
359 if ( o &amp;&amp; !childCount() ) {
360 <a href="qstring.html">QString</a> s( fullName() );
361 <a href="qdir.html">QDir</a> thisDir( s );
362<a name="x1667"></a> if ( !thisDir.<a href="qdir.html#isReadable">isReadable</a>() ) {
363 readable = FALSE;
364 <a href="qlistviewitem.html#setExpandable">setExpandable</a>( FALSE );
365 return;
366 }
367
368 <a href="qlistviewitem.html#listView">listView</a>()-&gt;setUpdatesEnabled( FALSE );
369<a name="x1666"></a> const QFileInfoList * files = thisDir.<a href="qdir.html#entryInfoList">entryInfoList</a>();
370 if ( files ) {
371 QFileInfoListIterator it( *files );
372 <a href="qfileinfo.html">QFileInfo</a> * fi;
373<a name="x1694"></a> while( (fi=it.<a href="qlistviewitemiterator.html#current">current</a>()) != 0 ) {
374 ++it;
375<a name="x1675"></a> if ( fi-&gt;<a href="qfileinfo.html#fileName">fileName</a>() == "." || fi-&gt;<a href="qfileinfo.html#fileName">fileName</a>() == ".." )
376 ; // nothing
377<a name="x1678"></a> else if ( fi-&gt;<a href="qfileinfo.html#isSymLink">isSymLink</a>() &amp;&amp; !showDirsOnly ) {
378 FileItem *item = new FileItem( this, fi-&gt;<a href="qfileinfo.html#fileName">fileName</a>(),
379 "Symbolic Link" );
380 item-&gt;<a href="qlistviewitem.html#setPixmap">setPixmap</a>( fileNormal );
381 }
382<a name="x1676"></a> else if ( fi-&gt;<a href="qfileinfo.html#isDir">isDir</a>() )
383 (void)new Directory( this, fi-&gt;<a href="qfileinfo.html#fileName">fileName</a>() );
384 else if ( !showDirsOnly ) {
385 FileItem *item
386 = new FileItem( this, fi-&gt;<a href="qfileinfo.html#fileName">fileName</a>(),
387<a name="x1677"></a> fi-&gt;<a href="qfileinfo.html#isFile">isFile</a>()?"File":"Special" );
388 item-&gt;<a href="qlistviewitem.html#setPixmap">setPixmap</a>( fileNormal );
389 }
390 }
391 }
392 listView()-&gt;setUpdatesEnabled( TRUE );
393 }
394 QListViewItem::<a href="qlistviewitem.html#setOpen">setOpen</a>( o );
395}
396
397
398<a name="x1692"></a>void Directory::<a href="qlistviewitem.html#setup">setup</a>()
399{
400 setExpandable( TRUE );
401 QListViewItem::<a href="qlistviewitem.html#setup">setup</a>();
402}
403
404
405QString <a name="f477"></a>Directory::fullName()
406{
407 <a href="qstring.html">QString</a> s;
408 if ( p ) {
409 s = p-&gt;fullName();
410<a name="x1701"></a> s.<a href="qstring.html#append">append</a>( f.name() );
411 s.<a href="qstring.html#append">append</a>( "/" );
412 } else {
413 s = f.name();
414 }
415 return s;
416}
417
418
419<a name="x1693"></a>QString Directory::<a href="qlistviewitem.html#text">text</a>( int column ) const
420{
421 if ( column == 0 )
422 return f.name();
423 else if ( readable )
424 return "Directory";
425 else
426 return "Unreadable Directory";
427}
428
429/*****************************************************************************
430 *
431 * Class DirectoryView
432 *
433 *****************************************************************************/
434
435<a name="f478"></a>DirectoryView::DirectoryView( <a href="qwidget.html">QWidget</a> *parent, const char *name, bool sdo )
436 : <a href="qlistview.html">QListView</a>( parent, name ), dirsOnly( sdo ), oldCurrent( 0 ),
437 dropItem( 0 ), mousePressed( FALSE )
438{
439 autoopen_timer = new <a href="qtimer.html">QTimer</a>( this );
440 if ( !folderLocked ) {
441 folderLocked = new <a href="qpixmap.html">QPixmap</a>( folder_locked );
442 folderClosed = new <a href="qpixmap.html">QPixmap</a>( folder_closed_xpm );
443 folderOpen = new <a href="qpixmap.html">QPixmap</a>( folder_open_xpm );
444 fileNormal = new <a href="qpixmap.html">QPixmap</a>( pix_file );
445 }
446
447 <a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qlistview.html#doubleClicked">doubleClicked</a>( <a href="qlistviewitem.html">QListViewItem</a> * ) ),
448 this, SLOT( slotFolderSelected( <a href="qlistviewitem.html">QListViewItem</a> * ) ) );
449 <a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qlistview.html#returnPressed">returnPressed</a>( <a href="qlistviewitem.html">QListViewItem</a> * ) ),
450 this, SLOT( slotFolderSelected( <a href="qlistviewitem.html">QListViewItem</a> * ) ) );
451
452 <a href="qwidget.html#setAcceptDrops">setAcceptDrops</a>( TRUE );
453 <a href="qscrollview.html#viewport">viewport</a>()-&gt;setAcceptDrops( TRUE );
454
455<a name="x1706"></a> <a href="qobject.html#connect">connect</a>( autoopen_timer, SIGNAL( <a href="qtimer.html#timeout">timeout</a>() ),
456 this, SLOT( openFolder() ) );
457}
458
459void <a name="f479"></a>DirectoryView::slotFolderSelected( <a href="qlistviewitem.html">QListViewItem</a> *i )
460{
461 if ( !i || !showDirsOnly() )
462 return;
463
464 Directory *dir = (Directory*)i;
465 emit folderSelected( dir-&gt;fullName() );
466}
467
468void <a name="f480"></a>DirectoryView::openFolder()
469{
470<a name="x1705"></a> autoopen_timer-&gt;<a href="qtimer.html#stop">stop</a>();
471 if ( dropItem &amp;&amp; !dropItem-&gt;isOpen() ) {
472 dropItem-&gt;setOpen( TRUE );
473 dropItem-&gt;repaint();
474 }
475}
476
477static const int autoopenTime = 750;
478
479
480<a name="x1679"></a>void DirectoryView::<a href="qscrollview.html#contentsDragEnterEvent">contentsDragEnterEvent</a>( <a href="qdragenterevent.html">QDragEnterEvent</a> *e )
481{
482 if ( !QUriDrag::canDecode(e) ) {
483<a name="x1669"></a> e-&gt;<a href="qdragmoveevent.html#ignore">ignore</a>();
484 return;
485 }
486
487 oldCurrent = <a href="qlistview.html#currentItem">currentItem</a>();
488
489<a name="x1673"></a> <a href="qlistviewitem.html">QListViewItem</a> *i = <a href="qlistview.html#itemAt">itemAt</a>( <a href="qscrollview.html#contentsToViewport">contentsToViewport</a>(e-&gt;<a href="qdropevent.html#pos">pos</a>()) );
490 if ( i ) {
491 dropItem = i;
492<a name="x1704"></a> autoopen_timer-&gt;<a href="qtimer.html#start">start</a>( autoopenTime );
493 }
494}
495
496
497<a name="x1681"></a>void DirectoryView::<a href="qscrollview.html#contentsDragMoveEvent">contentsDragMoveEvent</a>( <a href="qdragmoveevent.html">QDragMoveEvent</a> *e )
498{
499 if ( !QUriDrag::canDecode(e) ) {
500 e-&gt;<a href="qdragmoveevent.html#ignore">ignore</a>();
501 return;
502 }
503
504 <a href="qpoint.html">QPoint</a> vp = <a href="qscrollview.html#contentsToViewport">contentsToViewport</a>( ( (QDragMoveEvent*)e )-&gt;pos() );
505 <a href="qlistviewitem.html">QListViewItem</a> *i = <a href="qlistview.html#itemAt">itemAt</a>( vp );
506 if ( i ) {
507 <a href="qlistview.html#setSelected">setSelected</a>( i, TRUE );
508<a name="x1668"></a> e-&gt;<a href="qdragmoveevent.html#accept">accept</a>();
509 if ( i != dropItem ) {
510 autoopen_timer-&gt;<a href="qtimer.html#stop">stop</a>();
511 dropItem = i;
512 autoopen_timer-&gt;<a href="qtimer.html#start">start</a>( autoopenTime );
513 }
514<a name="x1672"></a> switch ( e-&gt;<a href="qdropevent.html#action">action</a>() ) {
515 case QDropEvent::Copy:
516 break;
517 case QDropEvent::Move:
518<a name="x1671"></a> e-&gt;<a href="qdropevent.html#acceptAction">acceptAction</a>();
519 break;
520 case QDropEvent::Link:
521 e-&gt;<a href="qdropevent.html#acceptAction">acceptAction</a>();
522 break;
523 default:
524 ;
525 }
526 } else {
527 e-&gt;<a href="qdragmoveevent.html#ignore">ignore</a>();
528 autoopen_timer-&gt;<a href="qtimer.html#stop">stop</a>();
529 dropItem = 0;
530 }
531}
532
533<a name="x1680"></a>void DirectoryView::<a href="qscrollview.html#contentsDragLeaveEvent">contentsDragLeaveEvent</a>( <a href="qdragleaveevent.html">QDragLeaveEvent</a> * )
534{
535 autoopen_timer-&gt;<a href="qtimer.html#stop">stop</a>();
536 dropItem = 0;
537
538 <a href="qlistview.html#setCurrentItem">setCurrentItem</a>( oldCurrent );
539 <a href="qlistview.html#setSelected">setSelected</a>( oldCurrent, TRUE );
540}
541
542<a name="x1682"></a>void DirectoryView::<a href="qscrollview.html#contentsDropEvent">contentsDropEvent</a>( <a href="qdropevent.html">QDropEvent</a> *e )
543{
544 autoopen_timer-&gt;<a href="qtimer.html#stop">stop</a>();
545
546 if ( !QUriDrag::canDecode(e) ) {
547 e-&gt;<a href="qdragmoveevent.html#ignore">ignore</a>();
548 return;
549 }
550
551 <a href="qlistviewitem.html">QListViewItem</a> *item = <a href="qlistview.html#itemAt">itemAt</a>( <a href="qscrollview.html#contentsToViewport">contentsToViewport</a>(e-&gt;<a href="qdropevent.html#pos">pos</a>()) );
552 if ( item ) {
553
554 <a href="qstrlist.html">QStrList</a> lst;
555
556<a name="x1707"></a> QUriDrag::<a href="quridrag.html#decode">decode</a>( e, lst );
557
558 <a href="qstring.html">QString</a> str;
559
560 switch ( e-&gt;<a href="qdropevent.html#action">action</a>() ) {
561 case QDropEvent::Copy:
562 str = "Copy";
563 break;
564 case QDropEvent::Move:
565 str = "Move";
566 e-&gt;<a href="qdropevent.html#acceptAction">acceptAction</a>();
567 break;
568 case QDropEvent::Link:
569 str = "Link";
570 e-&gt;<a href="qdropevent.html#acceptAction">acceptAction</a>();
571 break;
572 default:
573 str = "Unknown";
574 }
575
576 str += "\n\n";
577
578 e-&gt;<a href="qdragmoveevent.html#accept">accept</a>();
579
580<a name="x1699"></a> for ( uint i = 0; i &lt; lst.<a href="qptrlist.html#count">count</a>(); ++i ) {
581<a name="x1709"></a><a name="x1697"></a><a name="x1664"></a> <a href="qstring.html">QString</a> filename = QDir::<a href="qdir.html#convertSeparators">convertSeparators</a>(QUriDrag::<a href="quridrag.html#uriToLocalFile">uriToLocalFile</a>(lst.<a href="qptrlist.html#at">at</a>(i)));
582 str += filename + "\n";
583 }
584 str += QString( "\nTo\n\n %1" )
585 .arg( QDir::<a href="qdir.html#convertSeparators">convertSeparators</a>(fullPath(item)) );
586
587<a name="x1695"></a> QMessageBox::<a href="qmessagebox.html#information">information</a>( this, "Drop target", str, "Not implemented" );
588 } else
589 e-&gt;<a href="qdragmoveevent.html#ignore">ignore</a>();
590
591}
592
593
594QString <a name="f481"></a>DirectoryView::fullPath(QListViewItem* item)
595{
596 <a href="qstring.html">QString</a> fullpath = item-&gt;<a href="qlistviewitem.html#text">text</a>(0);
597<a name="x1688"></a> while ( (item=item-&gt;<a href="qlistviewitem.html#parent">parent</a>()) ) {
598 if ( item-&gt;<a href="qlistviewitem.html#parent">parent</a>() )
599 fullpath = item-&gt;<a href="qlistviewitem.html#text">text</a>(0) + "/" + fullpath;
600 else
601 fullpath = item-&gt;<a href="qlistviewitem.html#text">text</a>(0) + fullpath;
602 }
603#ifdef Q_WS_WIN
604<a name="x1702"></a> if (fullpath.<a href="qstring.html#length">length</a>() &gt; 2 &amp;&amp; fullpath[1] != ':') {
605 <a href="qdir.html">QDir</a> dir(fullpath);
606<a name="x1665"></a> fullpath = dir.<a href="qdir.html#currentDirPath">currentDirPath</a>().left(2) + fullpath;
607 }
608#endif
609
610 return fullpath;
611}
612
613<a name="x1684"></a>void DirectoryView::<a href="qlistview.html#contentsMousePressEvent">contentsMousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a>* e )
614{
615 QListView::<a href="qlistview.html#contentsMousePressEvent">contentsMousePressEvent</a>(e);
616 <a href="qpoint.html">QPoint</a> p( contentsToViewport( e-&gt;<a href="qdropevent.html#pos">pos</a>() ) );
617 <a href="qlistviewitem.html">QListViewItem</a> *i = itemAt( p );
618 if ( i ) {
619 // if the user clicked into the root decoration of the item, don't try to start a drag!
620<a name="x1696"></a> if ( p.<a href="qpoint.html#x">x</a>() &gt; header()-&gt;cellPos( header()-&gt;mapToActual( 0 ) ) +
621<a name="x1686"></a> treeStepSize() * ( i-&gt;<a href="qlistviewitem.html#depth">depth</a>() + ( rootIsDecorated() ? 1 : 0) ) + itemMargin() ||
622 p.<a href="qpoint.html#x">x</a>() &lt; header()-&gt;cellPos( header()-&gt;mapToActual( 0 ) ) ) {
623 presspos = e-&gt;<a href="qdropevent.html#pos">pos</a>();
624 mousePressed = TRUE;
625 }
626 }
627}
628
629<a name="x1683"></a>void DirectoryView::<a href="qlistview.html#contentsMouseMoveEvent">contentsMouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a>* e )
630{
631 if ( mousePressed &amp;&amp; ( presspos - e-&gt;<a href="qdropevent.html#pos">pos</a>() ).manhattanLength() &gt; QApplication::<a href="qapplication.html#startDragDistance">startDragDistance</a>() ) {
632 mousePressed = FALSE;
633 <a href="qlistviewitem.html">QListViewItem</a> *item = itemAt( contentsToViewport(presspos) );
634 if ( item ) {
635 <a href="qstring.html">QString</a> source = fullPath(item);
636<a name="x1674"></a> if ( QFile::<a href="qfile.html#exists">exists</a>(source) ) {
637 <a href="quridrag.html">QUriDrag</a>* ud = new <a href="quridrag.html">QUriDrag</a>(viewport());
638<a name="x1708"></a> ud-&gt;<a href="quridrag.html#setFileNames">setFileNames</a>( source );
639<a name="x1670"></a> if ( ud-&gt;<a href="qdragobject.html#drag">drag</a>() )
640 QMessageBox::<a href="qmessagebox.html#information">information</a>( this, "Drag source",
641 QString("Delete ") + QDir::convertSeparators(source), "Not implemented" );
642 }
643 }
644 }
645}
646
647<a name="x1685"></a>void DirectoryView::<a href="qlistview.html#contentsMouseReleaseEvent">contentsMouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * )
648{
649 mousePressed = FALSE;
650}
651
652void <a name="f482"></a>DirectoryView::setDir( const <a href="qstring.html">QString</a> &amp;s )
653{
654 <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( this );
655 ++it;
656 for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it ) {
657 it.<a href="qlistviewitemiterator.html#current">current</a>()-&gt;setOpen( FALSE );
658 }
659
660<a name="x1703"></a> <a href="qstringlist.html">QStringList</a> lst( QStringList::<a href="qstringlist.html#split">split</a>( "/", s ) );
661 <a href="qlistviewitem.html">QListViewItem</a> *item = <a href="qlistviewitem.html#firstChild">firstChild</a>();
662<a name="x1698"></a> QStringList::Iterator it2 = lst.<a href="qvaluelist.html#begin">begin</a>();
663<a name="x1700"></a> for ( ; it2 != lst.<a href="qvaluelist.html#end">end</a>(); ++it2 ) {
664 while ( item ) {
665 if ( item-&gt;<a href="qlistviewitem.html#text">text</a>( 0 ) == *it2 ) {
666 item-&gt;<a href="qlistviewitem.html#setOpen">setOpen</a>( TRUE );
667 break;
668 }
669<a name="x1687"></a> item = item-&gt;<a href="qlistviewitem.html#itemBelow">itemBelow</a>();
670 }
671 }
672
673 if ( item )
674 setCurrentItem( item );
675}
676
677void FileItem::<a href="qlistviewitem.html#setPixmap">setPixmap</a>( <a href="qpixmap.html">QPixmap</a> *p )
678{
679 pix = p;
680 <a href="qlistviewitem.html#setup">setup</a>();
681 <a href="qlistviewitem.html#widthChanged">widthChanged</a>( 0 );
682 <a href="qlistviewitem.html#invalidateHeight">invalidateHeight</a>();
683 <a href="qlistviewitem.html#repaint">repaint</a>();
684}
685
686
687const QPixmap *FileItem::<a href="qlistviewitem.html#pixmap">pixmap</a>( int i ) const
688{
689 if ( i )
690 return 0;
691 return pix;
692}
693</pre>
694
695<p> <hr>
696<p> Main:
697<p> <pre>/****************************************************************************
698** $Id: dirview-example.html 2051 2007-02-21 10:04:20Z chehrlic $
699**
700** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
701**
702** This file is part of an example program for Qt. This example
703** program may be used, distributed and modified without limitation.
704**
705*****************************************************************************/
706
707#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
708#include &lt;<a href="qfileinfo-h.html">qfileinfo.h</a>&gt;
709#include &lt;<a href="qdir-h.html">qdir.h</a>&gt;
710#include "dirview.h"
711
712int main( int argc, char ** argv )
713{
714 <a href="qapplication.html">QApplication</a> a( argc, argv );
715
716 DirectoryView mw;
717
718<a name="x1714"></a> mw.<a href="qlistview.html#addColumn">addColumn</a>( "Name" );
719 mw.<a href="qlistview.html#addColumn">addColumn</a>( "Type" );
720<a name="x1716"></a> mw.<a href="qlistview.html#setTreeStepSize">setTreeStepSize</a>( 20 );
721
722<a name="x1712"></a> const QFileInfoList* roots = QDir::<a href="qdir.html#drives">drives</a>();
723 <a href="qptrlistiterator.html">QPtrListIterator</a>&lt;QFileInfo&gt; i(*roots);
724 <a href="qfileinfo.html">QFileInfo</a>* fi;
725 while ( (fi = *i) ) {
726 ++i;
727<a name="x1713"></a> Directory * root = new Directory( &amp;mw, fi-&gt;<a href="qfileinfo.html#filePath">filePath</a>() );
728 if ( roots-&gt;count() &lt;= 1 )
729<a name="x1718"></a> root-&gt;<a href="qlistviewitem.html#setOpen">setOpen</a>( TRUE ); // be interesting
730 }
731
732<a name="x1719"></a> mw.<a href="qwidget.html#resize">resize</a>( 400, 400 );
733 mw.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Directory Browser" );
734<a name="x1715"></a> mw.<a href="qlistview.html#setAllColumnsShowFocus">setAllColumnsShowFocus</a>( TRUE );
735 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;mw );
736<a name="x1717"></a> mw.<a href="qwidget.html#show">show</a>();
737
738 return a.<a href="qapplication.html#exec">exec</a>();
739}
740</pre>
741
742<p>See also <a href="examples.html">Examples</a>.
743
744<!-- eof -->
745<p><address><hr><div align=center>
746<table width=100% cellspacing=0 border=0><tr>
747<td>Copyright &copy; 2007
748<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
749<td align=right><div align=right>Qt 3.3.8</div>
750</table></div></address></body>
751</html>
Note: See TracBrowser for help on using the repository browser.