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"><!--
|
---|
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>A Directory Browser</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example program demonstrates how to use a listview and
|
---|
37 | listview items to build a multi-column hierarchical, memory- and
|
---|
38 | CPU-efficient directory browser. It also demonstrates how to use
|
---|
39 | Drag&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 <<a href="qlistview-h.html">qlistview.h</a>>
|
---|
56 | #include <<a href="qstring-h.html">qstring.h</a>>
|
---|
57 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
58 | #include <<a href="qfileinfo-h.html">qfileinfo.h</a>>
|
---|
59 | #include <<a href="qtimer-h.html">qtimer.h</a>>
|
---|
60 |
|
---|
61 | class QWidget;
|
---|
62 | class QDragEnterEvent;
|
---|
63 | class QDragMoveEvent;
|
---|
64 | class QDragLeaveEvent;
|
---|
65 | class QDropEvent;
|
---|
66 |
|
---|
67 | class FileItem : public <a href="qlistviewitem.html">QListViewItem</a>
|
---|
68 | {
|
---|
69 | public:
|
---|
70 | FileItem( <a href="qlistviewitem.html">QListViewItem</a> *parent, const <a href="qstring.html">QString</a> &s1, const <a href="qstring.html">QString</a> &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 |
|
---|
79 | private:
|
---|
80 | <a href="qpixmap.html">QPixmap</a> *pix;
|
---|
81 |
|
---|
82 | };
|
---|
83 |
|
---|
84 | class Directory : public <a href="qlistviewitem.html">QListViewItem</a>
|
---|
85 | {
|
---|
86 | public:
|
---|
87 | Directory( <a href="qlistview.html">QListView</a> * parent, const <a href="qstring.html">QString</a>& filename );
|
---|
88 | Directory( Directory * parent, const <a href="qstring.html">QString</a>& filename, const <a href="qstring.html">QString</a> &col2 )
|
---|
89 | : <a href="qlistviewitem.html">QListViewItem</a>( parent, filename, col2 ), pix( 0 ) {}
|
---|
90 | Directory( Directory * parent, const <a href="qstring.html">QString</a>& 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 |
|
---|
105 | private:
|
---|
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 |
|
---|
114 | class DirectoryView : public <a href="qlistview.html">QListView</a>
|
---|
115 | {
|
---|
116 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
117 |
|
---|
118 | public:
|
---|
119 | DirectoryView( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0, bool sdo = FALSE );
|
---|
120 | bool showDirsOnly() { return dirsOnly; }
|
---|
121 |
|
---|
122 | public slots:
|
---|
123 | void setDir( const <a href="qstring.html">QString</a> & );
|
---|
124 |
|
---|
125 | signals:
|
---|
126 | void folderSelected( const <a href="qstring.html">QString</a> & );
|
---|
127 |
|
---|
128 | protected slots:
|
---|
129 | void slotFolderSelected( <a href="qlistviewitem.html">QListViewItem</a> * );
|
---|
130 | void openFolder();
|
---|
131 |
|
---|
132 | protected:
|
---|
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 |
|
---|
141 | private:
|
---|
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
|
---|
157 | scans subdirectories only when it has to. This allows the program
|
---|
158 | to handle very large file systems efficiently. The same technique
|
---|
159 | can 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 <<a href="qdir-h.html">qdir.h</a>>
|
---|
173 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
174 | #include <<a href="qfileinfo-h.html">qfileinfo.h</a>>
|
---|
175 | #include <<a href="qpixmap-h.html">qpixmap.h</a>>
|
---|
176 | #include <<a href="qevent-h.html">qevent.h</a>>
|
---|
177 | #include <<a href="qpoint-h.html">qpoint.h</a>>
|
---|
178 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
179 | #include <<a href="qdragobject-h.html">qdragobject.h</a>>
|
---|
180 | #include <<a href="qmime-h.html">qmime.h</a>>
|
---|
181 | #include <<a href="qstrlist-h.html">qstrlist.h</a>>
|
---|
182 | #include <<a href="qstringlist-h.html">qstringlist.h</a>>
|
---|
183 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
184 | #include <<a href="qheader-h.html">qheader.h</a>>
|
---|
185 |
|
---|
186 | static 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 |
|
---|
214 | static 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 |
|
---|
244 | static 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 |
|
---|
273 | static 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 |
|
---|
299 | QPixmap *folderLocked = 0;
|
---|
300 | QPixmap *folderClosed = 0;
|
---|
301 | QPixmap *folderOpen = 0;
|
---|
302 | QPixmap *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>& filename )
|
---|
311 | : <a href="qlistviewitem.html">QListViewItem</a>( parent ), f(filename),
|
---|
312 | showDirsOnly( parent->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 |
|
---|
325 | Directory::Directory( <a href="qlistview.html">QListView</a> * parent, const <a href="qstring.html">QString</a>& filename )
|
---|
326 | : <a href="qlistviewitem.html">QListViewItem</a>( parent ), f(filename),
|
---|
327 | showDirsOnly( ( (DirectoryView*)parent )->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 && !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>()->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-><a href="qfileinfo.html#fileName">fileName</a>() == "." || fi-><a href="qfileinfo.html#fileName">fileName</a>() == ".." )
|
---|
376 | ; // nothing
|
---|
377 | <a name="x1678"></a> else if ( fi-><a href="qfileinfo.html#isSymLink">isSymLink</a>() && !showDirsOnly ) {
|
---|
378 | FileItem *item = new FileItem( this, fi-><a href="qfileinfo.html#fileName">fileName</a>(),
|
---|
379 | "Symbolic Link" );
|
---|
380 | item-><a href="qlistviewitem.html#setPixmap">setPixmap</a>( fileNormal );
|
---|
381 | }
|
---|
382 | <a name="x1676"></a> else if ( fi-><a href="qfileinfo.html#isDir">isDir</a>() )
|
---|
383 | (void)new Directory( this, fi-><a href="qfileinfo.html#fileName">fileName</a>() );
|
---|
384 | else if ( !showDirsOnly ) {
|
---|
385 | FileItem *item
|
---|
386 | = new FileItem( this, fi-><a href="qfileinfo.html#fileName">fileName</a>(),
|
---|
387 | <a name="x1677"></a> fi-><a href="qfileinfo.html#isFile">isFile</a>()?"File":"Special" );
|
---|
388 | item-><a href="qlistviewitem.html#setPixmap">setPixmap</a>( fileNormal );
|
---|
389 | }
|
---|
390 | }
|
---|
391 | }
|
---|
392 | listView()->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 |
|
---|
405 | QString <a name="f477"></a>Directory::fullName()
|
---|
406 | {
|
---|
407 | <a href="qstring.html">QString</a> s;
|
---|
408 | if ( p ) {
|
---|
409 | s = p->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>()->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 |
|
---|
459 | void <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->fullName() );
|
---|
466 | }
|
---|
467 |
|
---|
468 | void <a name="f480"></a>DirectoryView::openFolder()
|
---|
469 | {
|
---|
470 | <a name="x1705"></a> autoopen_timer-><a href="qtimer.html#stop">stop</a>();
|
---|
471 | if ( dropItem && !dropItem->isOpen() ) {
|
---|
472 | dropItem->setOpen( TRUE );
|
---|
473 | dropItem->repaint();
|
---|
474 | }
|
---|
475 | }
|
---|
476 |
|
---|
477 | static 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-><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-><a href="qdropevent.html#pos">pos</a>()) );
|
---|
490 | if ( i ) {
|
---|
491 | dropItem = i;
|
---|
492 | <a name="x1704"></a> autoopen_timer-><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-><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 )->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-><a href="qdragmoveevent.html#accept">accept</a>();
|
---|
509 | if ( i != dropItem ) {
|
---|
510 | autoopen_timer-><a href="qtimer.html#stop">stop</a>();
|
---|
511 | dropItem = i;
|
---|
512 | autoopen_timer-><a href="qtimer.html#start">start</a>( autoopenTime );
|
---|
513 | }
|
---|
514 | <a name="x1672"></a> switch ( e-><a href="qdropevent.html#action">action</a>() ) {
|
---|
515 | case QDropEvent::Copy:
|
---|
516 | break;
|
---|
517 | case QDropEvent::Move:
|
---|
518 | <a name="x1671"></a> e-><a href="qdropevent.html#acceptAction">acceptAction</a>();
|
---|
519 | break;
|
---|
520 | case QDropEvent::Link:
|
---|
521 | e-><a href="qdropevent.html#acceptAction">acceptAction</a>();
|
---|
522 | break;
|
---|
523 | default:
|
---|
524 | ;
|
---|
525 | }
|
---|
526 | } else {
|
---|
527 | e-><a href="qdragmoveevent.html#ignore">ignore</a>();
|
---|
528 | autoopen_timer-><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-><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-><a href="qtimer.html#stop">stop</a>();
|
---|
545 |
|
---|
546 | if ( !QUriDrag::canDecode(e) ) {
|
---|
547 | e-><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-><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-><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-><a href="qdropevent.html#acceptAction">acceptAction</a>();
|
---|
567 | break;
|
---|
568 | case QDropEvent::Link:
|
---|
569 | str = "Link";
|
---|
570 | e-><a href="qdropevent.html#acceptAction">acceptAction</a>();
|
---|
571 | break;
|
---|
572 | default:
|
---|
573 | str = "Unknown";
|
---|
574 | }
|
---|
575 |
|
---|
576 | str += "\n\n";
|
---|
577 |
|
---|
578 | e-><a href="qdragmoveevent.html#accept">accept</a>();
|
---|
579 |
|
---|
580 | <a name="x1699"></a> for ( uint i = 0; i < 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-><a href="qdragmoveevent.html#ignore">ignore</a>();
|
---|
590 |
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | QString <a name="f481"></a>DirectoryView::fullPath(QListViewItem* item)
|
---|
595 | {
|
---|
596 | <a href="qstring.html">QString</a> fullpath = item-><a href="qlistviewitem.html#text">text</a>(0);
|
---|
597 | <a name="x1688"></a> while ( (item=item-><a href="qlistviewitem.html#parent">parent</a>()) ) {
|
---|
598 | if ( item-><a href="qlistviewitem.html#parent">parent</a>() )
|
---|
599 | fullpath = item-><a href="qlistviewitem.html#text">text</a>(0) + "/" + fullpath;
|
---|
600 | else
|
---|
601 | fullpath = item-><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>() > 2 && 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-><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>() > header()->cellPos( header()->mapToActual( 0 ) ) +
|
---|
621 | <a name="x1686"></a> treeStepSize() * ( i-><a href="qlistviewitem.html#depth">depth</a>() + ( rootIsDecorated() ? 1 : 0) ) + itemMargin() ||
|
---|
622 | p.<a href="qpoint.html#x">x</a>() < header()->cellPos( header()->mapToActual( 0 ) ) ) {
|
---|
623 | presspos = e-><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 && ( presspos - e-><a href="qdropevent.html#pos">pos</a>() ).manhattanLength() > 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-><a href="quridrag.html#setFileNames">setFileNames</a>( source );
|
---|
639 | <a name="x1670"></a> if ( ud-><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 |
|
---|
652 | void <a name="f482"></a>DirectoryView::setDir( const <a href="qstring.html">QString</a> &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>()->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-><a href="qlistviewitem.html#text">text</a>( 0 ) == *it2 ) {
|
---|
666 | item-><a href="qlistviewitem.html#setOpen">setOpen</a>( TRUE );
|
---|
667 | break;
|
---|
668 | }
|
---|
669 | <a name="x1687"></a> item = item-><a href="qlistviewitem.html#itemBelow">itemBelow</a>();
|
---|
670 | }
|
---|
671 | }
|
---|
672 |
|
---|
673 | if ( item )
|
---|
674 | setCurrentItem( item );
|
---|
675 | }
|
---|
676 |
|
---|
677 | void 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 |
|
---|
687 | const 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 <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
708 | #include <<a href="qfileinfo-h.html">qfileinfo.h</a>>
|
---|
709 | #include <<a href="qdir-h.html">qdir.h</a>>
|
---|
710 | #include "dirview.h"
|
---|
711 |
|
---|
712 | int 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><QFileInfo> 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( &mw, fi-><a href="qfileinfo.html#filePath">filePath</a>() );
|
---|
728 | if ( roots->count() <= 1 )
|
---|
729 | <a name="x1718"></a> root-><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>( &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 © 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>
|
---|