source: trunk/examples/fileiconview/qfileiconview.cpp

Last change on this file was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 23.4 KB
Line 
1/****************************************************************************
2** $Id: qfileiconview.cpp 160 2006-12-11 20:15:57Z dmik $
3**
4** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "qfileiconview.h"
12#include <qpainter.h>
13#include <qstringlist.h>
14#include <qpixmap.h>
15#include <qmime.h>
16#include <qstrlist.h>
17#include <qdragobject.h>
18#include <qmessagebox.h>
19#include <qevent.h>
20#include <qpopupmenu.h>
21#include <qcursor.h>
22#include <qapplication.h>
23#include <qwmatrix.h>
24
25#include <stdlib.h>
26
27static const char * file_icon[]={
28 "32 32 17 1",
29 "# c #000000",
30 "a c #ffffff",
31 "j c #808080",
32 "n c #a0a0a4",
33 "g c #c0c0c0",
34 "m c #004000",
35 "o c #000000",
36 "l c #004040",
37 "k c #404000",
38 "i c #c0c000",
39 "h c #ffff00",
40 "b c #ffffc0",
41 "e c #ff8000",
42 "f c #c05800",
43 "c c #ffa858",
44 "d c #ffdca8",
45 ". c None",
46 "................................",
47 "................................",
48 "................................",
49 "................................",
50 ".............#....###...........",
51 "...###......#a##.#aba##.........",
52 "..#cdb#....#aaaa#aaaaaa##.......",
53 "..#ecdb#..#aaaa#aaaaaaaba##.....",
54 "..#fecdb##aaaa#aaaaaaaaaaab##...",
55 "...#fecdb#aaa#aaaaaaabaabaaaa##.",
56 "....#fecdb#a#baaaaa#baaaaaabaaa#",
57 ".....#fecdb#aaaaab#a##baaaaaaa#.",
58 ".....##fecdb#bbba#aaaa##baaab#..",
59 "....#bb#fecdb#ba#aaaaaaa##aa#...",
60 "...#bbbb#fecdb##aaabaaaaaa##....",
61 "..#bbbb#b#fecdb#aaaaaaabaaaa##..",
62 ".#bbbb#bbb#fecdg#aaaaaaaaaaaba#.",
63 "#hhbb#bbbbb#fegg#iiaaaaaaaaaaaa#",
64 "#jhhhklibbbk#ggj#aaiiaaaaaaaaa#j",
65 ".#mjhhhkmikab####aaabiiaaaaaa#j.",
66 "...##jhhhmaaibbaaiibaaaiiaab#n..",
67 ".....##j#baaaiiabaaiibaabaa#n...",
68 "......##baibaabiibaaaiiabb#j....",
69 "......#bbbbiiaabbiiaaaaabon.....",
70 ".....#bbbbbbbiiabbaiiaab#n......",
71 ".....#jbbbbbbbbiibaabba#n.......",
72 "......##jbbbbbbbbiiaabmj........",
73 "........##jbbbbbbbbbb#j.........",
74 "..........##nbbbbbbbmj..........",
75 "............##jbbbb#j...........",
76 "..............#mjj#n............",
77 "................##n............."};
78
79static const char * folder_icon[]={
80 "32 32 11 1",
81 "# c #000000",
82 "b c #c0c000",
83 "d c #585858",
84 "a c #ffff00",
85 "i c #400000",
86 "h c #a0a0a4",
87 "e c #000000",
88 "c c #ffffff",
89 "f c #303030",
90 "g c #c0c0c0",
91 ". c None",
92 "...###..........................",
93 "...#aa##........................",
94 ".###baaa##......................",
95 ".#cde#baaa##....................",
96 ".#cccdeebaaa##..##f.............",
97 ".#cccccdeebaaa##aaa##...........",
98 ".#cccccccdeebaaaaaaaa##.........",
99 ".#cccccccccdeebababaaa#.........",
100 ".#cccccgcgghhebbbbbbbaa#........",
101 ".#ccccccgcgggdebbbbbbba#........",
102 ".#cccgcgcgcgghdeebiebbba#.......",
103 ".#ccccgcggggggghdeddeeba#.......",
104 ".#cgcgcgcggggggggghghdebb#......",
105 ".#ccgcggggggggghghghghd#b#......",
106 ".#cgcgcggggggggghghghhd#b#......",
107 ".#gcggggggggghghghhhhhd#b#......",
108 ".#cgcggggggggghghghhhhd#b#......",
109 ".#ggggggggghghghhhhhhhdib#......",
110 ".#gggggggggghghghhhhhhd#b#......",
111 ".#hhggggghghghhhhhhhhhd#b#......",
112 ".#ddhhgggghghghhhhhhhhd#b#......",
113 "..##ddhhghghhhhhhhhhhhdeb#......",
114 "....##ddhhhghhhhhhhhhhd#b#......",
115 "......##ddhhhhhhhhhhhhd#b#......",
116 "........##ddhhhhhhhhhhd#b#......",
117 "..........##ddhhhhhhhhd#b#......",
118 "............##ddhhhhhhd#b###....",
119 "..............##ddhhhhd#b#####..",
120 "................##ddhhd#b######.",
121 "..................##dddeb#####..",
122 "....................##d#b###....",
123 "......................####......"};
124
125
126static const char * link_icon[]={
127 "32 32 12 1",
128 "# c #000000",
129 "h c #a0a0a4",
130 "b c #c00000",
131 "d c #585858",
132 "i c #400000",
133 "c c #ffffff",
134 "e c #000000",
135 "g c #c0c0c0",
136 "a c #ff0000",
137 "f c #303030",
138 "n c white",
139 ". c None",
140 "...###..........................",
141 "...#aa##........................",
142 ".###baaa##......................",
143 ".#cde#baaa##....................",
144 ".#cccdeebaaa##..##f.............",
145 ".#cccccdeebaaa##aaa##...........",
146 ".#cccccccdeebaaaaaaaa##.........",
147 ".#cccccccccdeebababaaa#.........",
148 ".#cccccgcgghhebbbbbbbaa#........",
149 ".#ccccccgcgggdebbbbbbba#........",
150 ".#cccgcgcgcgghdeebiebbba#.......",
151 ".#ccccgcggggggghdeddeeba#.......",
152 ".#cgcgcgcggggggggghghdebb#......",
153 ".#ccgcggggggggghghghghd#b#......",
154 ".#cgcgcggggggggghghghhd#b#......",
155 ".#gcggggggggghghghhhhhd#b#......",
156 ".#cgcggggggggghghghhhhd#b#......",
157 ".#ggggggggghghghhhhhhhdib#......",
158 ".#gggggggggghghghhhhhhd#b#......",
159 ".#hhggggghghghhhhhhhhhd#b#......",
160 ".#ddhhgggghghghhhhhhhhd#b#......",
161 "..##ddhhghghhhhhhhhhhhdeb#......",
162 "############hhhhhhhhhhd#b#......",
163 "#nnnnnnnnnn#hhhhhhhhhhd#b#......",
164 "#nnnnnnnnnn#hhhhhhhhhhd#b#......",
165 "#nn#nn#nnnn#ddhhhhhhhhd#b#......",
166 "#nn##n##nnn###ddhhhhhhd#b###....",
167 "#nnn#####nn#..##ddhhhhd#b#####..",
168 "#nnnnn##nnn#....##ddhhd#b######.",
169 "#nnnnn#nnnn#......##dddeb#####..",
170 "#nnnnnnnnnn#........##d#b###....",
171 "############..........####......"};
172
173static const char * folder_locked_icon[]={
174 "32 32 12 1",
175 "# c #000000",
176 "g c #808080",
177 "h c #c0c0c0",
178 "f c #c05800",
179 "c c #ffffff",
180 "d c #585858",
181 "b c #ffa858",
182 "a c #ffdca8",
183 "e c #000000",
184 "i c #a0a0a4",
185 "j c #c0c0c0",
186 ". c None",
187 "...###..........................",
188 "...#aa##........................",
189 ".###baaa##......................",
190 ".#cde#baaa##....................",
191 ".#cccdeeba#######...............",
192 ".#cccccde##fffff##..............",
193 ".#cccccc##fffgggg#..............",
194 ".#ccccccc#ffg####a##............",
195 ".#ccccchc#ffg#eebbaa##..........",
196 ".#ccccccc#ffg#ddeebbba##........",
197 ".#ccchccc#ffg#ihddeebbba##......",
198 ".#cccccaa#ffg#ihhhddeeba##......",
199 ".#chchhbbaafg#ihhhihidebb#......",
200 ".#cchccbbbbaa#ihhihihid#b#......",
201 ".#chchhbb#bbbaaiihihiid#b#......",
202 ".#hchhcbb#fbbbafhiiiiid#b#......",
203 ".#chchhbb#ffgbbfihiiiid#b#......",
204 ".#hhhhhbb#ffg#bfiiiiiid#b#......",
205 ".#hhhhhbbaffg#bfiiiiiid#b#......",
206 ".#iihhhjbbaab#bfiiiiiid#b#......",
207 ".#ddiihhh#bbbabfiiiiiid#b#......",
208 "..##ddiih#ffbbbfiiiiiid#b#......",
209 "....##ddi#ffg#biiiiiiid#b#......",
210 "......##d#ffg#iiiiiiiid#b#......",
211 "........##ffg#iiiiiiiid#b#......",
212 ".........#ffg#iiiiiiiid#b#......",
213 ".........#ffg#ddiiiiiid#b###....",
214 ".........##fg###ddiiiid#b#####..",
215 "...........####.##ddiid#b######.",
216 "..................##dddeb#####..",
217 "....................##d#b###....",
218 "......................####......"};
219
220static QPixmap *iconFolderLockedLarge = 0;
221static QPixmap *iconFolderLarge = 0;
222static QPixmap *iconFileLarge = 0;
223static QPixmap *iconLinkLarge = 0;
224static QPixmap *iconFolderLockedSmall = 0;
225static QPixmap *iconFolderSmall = 0;
226static QPixmap *iconFileSmall = 0;
227static QPixmap *iconLinkSmall = 0;
228
229static void cleanup()
230{
231 delete iconFolderLockedLarge;
232 iconFolderLockedLarge = 0;
233 delete iconFolderLarge;
234 iconFolderLarge = 0;
235 delete iconFileLarge;
236 iconFileLarge = 0;
237 delete iconLinkLarge;
238 iconLinkLarge = 0;
239 delete iconFolderLockedSmall;
240 iconFolderLockedSmall = 0;
241 delete iconFolderSmall;
242 iconFolderSmall = 0;
243 delete iconFileSmall;
244 iconFileSmall = 0;
245 delete iconLinkSmall;
246 iconLinkSmall = 0;
247}
248
249/*****************************************************************************
250 *
251 * Class QtFileIconDrag
252 *
253 *****************************************************************************/
254
255QtFileIconDrag::QtFileIconDrag( QWidget * dragSource, const char* name )
256 : QIconDrag( dragSource, name )
257{
258}
259
260const char* QtFileIconDrag::format( int i ) const
261{
262 if ( i == 0 )
263 return "application/x-qiconlist";
264 else if ( i == 1 )
265 return "text/uri-list";
266 else
267 return 0;
268}
269
270QByteArray QtFileIconDrag::encodedData( const char* mime ) const
271{
272 QByteArray a;
273 if ( QString( mime ) == "application/x-qiconlist" ) {
274 a = QIconDrag::encodedData( mime );
275 } else if ( QString( mime ) == "text/uri-list" ) {
276 QString s = urls.join( "\r\n" );
277 a.resize( s.length() );
278 memcpy( a.data(), s.latin1(), s.length() );
279 }
280 return a;
281}
282
283bool QtFileIconDrag::canDecode( QMimeSource* e )
284{
285 return e->provides( "application/x-qiconlist" ) ||
286 e->provides( "text/uri-list" );
287}
288
289void QtFileIconDrag::append( const QIconDragItem &item, const QRect &pr,
290 const QRect &tr, const QString &url )
291{
292 QIconDrag::append( item, pr, tr );
293 QString ourUrl = url;
294#ifdef Q_WS_WIN
295 if (ourUrl.length() > 2 && ourUrl[1] != ':') {
296 QDir dir(ourUrl);
297 ourUrl = dir.absPath();
298 }
299#endif
300 urls << QUriDrag::localFileToUri(ourUrl);
301}
302
303/*****************************************************************************
304 *
305 * Class QtFileIconViewItem
306 *
307 *****************************************************************************/
308
309QtFileIconViewItem::QtFileIconViewItem( QtFileIconView *parent, QFileInfo *fi )
310 : QIconViewItem( parent, fi->fileName() ), itemFileName( fi->filePath() ),
311 itemFileInfo( fi ), checkSetText( FALSE )
312{
313 vm = QtFileIconView::Large;
314
315 if ( itemFileInfo->isDir() )
316 itemType = Dir;
317 else if ( itemFileInfo->isFile() )
318 itemType = File;
319 if ( itemFileInfo->isSymLink() )
320 itemType = Link;
321
322 viewModeChanged( ( (QtFileIconView*)iconView() )->viewMode() );
323
324 if ( itemFileInfo->fileName() == "." ||
325 itemFileInfo->fileName() == ".." )
326 setRenameEnabled( FALSE );
327
328 checkSetText = TRUE;
329
330 QObject::connect( &timer, SIGNAL( timeout() ),
331 iconView(), SLOT( openFolder() ) );
332}
333
334void QtFileIconViewItem::paintItem( QPainter *p, const QColorGroup &cg )
335{
336 if ( itemFileInfo->isSymLink() ) {
337 QFont f( p->font() );
338 f.setItalic( TRUE );
339 p->setFont( f );
340 }
341
342 QIconViewItem::paintItem( p, cg );
343}
344
345void QtFileIconViewItem::viewModeChanged( QtFileIconView::ViewMode m )
346{
347 vm = m;
348 setDropEnabled( itemType == Dir && QDir( itemFileName ).isReadable() );
349 calcRect();
350}
351
352QPixmap *QtFileIconViewItem::pixmap() const
353{
354 switch ( itemType ) {
355 case Dir:
356 {
357 if ( !QDir( itemFileName ).isReadable() ) {
358 if ( vm == QtFileIconView::Small )
359 return iconFolderLockedSmall;
360 else
361 return iconFolderLockedLarge;
362 } else {
363 if ( vm == QtFileIconView::Small )
364 return iconFolderSmall;
365 else
366 return iconFolderLarge;
367 }
368 }
369 case Link:
370 {
371 if ( vm == QtFileIconView::Small )
372 return iconLinkSmall;
373 else
374 return iconLinkLarge;
375 }
376 default:
377 {
378 if ( vm == QtFileIconView::Small )
379 return iconFileSmall;
380 else
381 return iconFileLarge;
382 }
383 }
384}
385
386QtFileIconViewItem::~QtFileIconViewItem()
387{
388 delete itemFileInfo;
389}
390
391void QtFileIconViewItem::setText( const QString &text )
392{
393 if ( checkSetText ) {
394 if ( text == "." || text == "." || text.isEmpty() )
395 return;
396 QDir dir( itemFileInfo->dir() );
397 if ( dir.rename( itemFileInfo->fileName(), text ) ) {
398 itemFileName = itemFileInfo->dirPath( TRUE ) + "/" + text;
399 delete itemFileInfo;
400 itemFileInfo = new QFileInfo( itemFileName );
401 QIconViewItem::setText( text );
402 }
403 } else {
404 QIconViewItem::setText( text );
405 }
406}
407
408bool QtFileIconViewItem::acceptDrop( const QMimeSource *e ) const
409{
410 if ( type() == Dir && e->provides( "text/uri-list" ) &&
411 dropEnabled() )
412 return TRUE;
413
414 return FALSE;
415}
416
417void QtFileIconViewItem::dropped( QDropEvent *e, const QValueList<QIconDragItem> & )
418{
419 timer.stop();
420
421 if ( !QUriDrag::canDecode( e ) ) {
422 e->ignore();
423 return;
424 }
425
426 QStringList lst;
427 QUriDrag::decodeLocalFiles( e, lst );
428
429 QString str;
430 if ( e->action() == QDropEvent::Copy )
431 str = "Copy\n\n";
432 else
433 str = "Move\n\n";
434 for ( uint i = 0; i < lst.count(); ++i )
435 str += QString( " %1\n" ).arg( lst[i] );
436 str += QString( "\n"
437 "To\n\n"
438 " %1" ).arg( filename() );
439
440 QMessageBox::information( iconView(), e->action() == QDropEvent::Copy ? "Copy" : "Move" , str, "Not Implemented" );
441 if ( e->action() == QDropEvent::Move )
442 QMessageBox::information( iconView(), "Remove" , str, "Not Implemented" );
443 e->acceptAction();
444}
445
446void QtFileIconViewItem::dragEntered()
447{
448 if ( type() != Dir ||
449 type() == Dir && !QDir( itemFileName ).isReadable() )
450 return;
451
452 ( (QtFileIconView*)iconView() )->setOpenItem( this );
453 timer.start( 1500 );
454}
455
456void QtFileIconViewItem::dragLeft()
457{
458 if ( type() != Dir ||
459 type() == Dir && !QDir( itemFileName ).isReadable() )
460 return;
461
462 timer.stop();
463}
464
465/*****************************************************************************
466 *
467 * Class QtFileIconView
468 *
469 *****************************************************************************/
470
471QtFileIconView::QtFileIconView( const QString &dir, QWidget *parent, const char *name )
472 : QIconView( parent, name ), viewDir( dir ), newFolderNum( 0 )
473{
474 if ( !iconFolderLockedLarge ) {
475 qAddPostRoutine( cleanup );
476 QWMatrix m;
477 m.scale( 0.6, 0.6 );
478 QPixmap iconpix( folder_locked_icon );
479 iconFolderLockedLarge = new QPixmap( folder_locked_icon );
480 iconpix = iconpix.xForm( m );
481 iconFolderLockedSmall = new QPixmap( iconpix );
482 iconpix = QPixmap( folder_icon );
483 iconFolderLarge = new QPixmap( folder_icon );
484 iconpix = iconpix.xForm( m );
485 iconFolderSmall = new QPixmap( iconpix );
486 iconpix = QPixmap( file_icon );
487 iconFileLarge = new QPixmap( file_icon );
488 iconpix = iconpix.xForm( m );
489 iconFileSmall = new QPixmap( iconpix );
490 iconpix = QPixmap( link_icon );
491 iconLinkLarge = new QPixmap( link_icon );
492 iconpix = iconpix.xForm( m );
493 iconLinkSmall = new QPixmap( iconpix );
494 }
495
496 vm = Large;
497
498 setGridX( 75 );
499 setResizeMode( Adjust );
500 setWordWrapIconText( FALSE );
501
502 connect( this, SIGNAL( doubleClicked( QIconViewItem * ) ),
503 this, SLOT( itemDoubleClicked( QIconViewItem * ) ) );
504 connect( this, SIGNAL( returnPressed( QIconViewItem * ) ),
505 this, SLOT( itemDoubleClicked( QIconViewItem * ) ) );
506 connect( this, SIGNAL( dropped( QDropEvent *, const QValueList<QIconDragItem> & ) ),
507 this, SLOT( slotDropped( QDropEvent *, const QValueList<QIconDragItem> & ) ) );
508 connect( this, SIGNAL( contextMenuRequested( QIconViewItem *, const QPoint & ) ),
509 this, SLOT( slotRightPressed( QIconViewItem * ) ) );
510
511 setHScrollBarMode( AlwaysOff );
512 setVScrollBarMode( Auto );
513
514 setAutoArrange( TRUE );
515 setSorting( TRUE );
516 openItem = 0;
517}
518
519void QtFileIconView::openFolder()
520{
521 if ( !openItem )
522 return;
523 if ( openItem->type() != QtFileIconViewItem::Dir ||
524 openItem->type() == QtFileIconViewItem::Dir &&
525 !QDir( openItem->itemFileName ).isReadable() )
526 return;
527
528 openItem->timer.stop();
529 setDirectory( openItem->itemFileName );
530}
531
532void QtFileIconView::setDirectory( const QString &dir )
533{
534 viewDir = QDir( dir );
535 readDir( viewDir );
536}
537
538void QtFileIconView::setDirectory( const QDir &dir )
539{
540 viewDir = dir;
541 readDir( viewDir );
542}
543
544void QtFileIconView::newDirectory()
545{
546 setAutoArrange( FALSE );
547 selectAll( FALSE );
548 if ( viewDir.mkdir( QString( "New Folder %1" ).arg( ++newFolderNum ) ) ) {
549 QFileInfo *fi = new QFileInfo( viewDir, QString( "New Folder %1" ).arg( newFolderNum ) );
550 QtFileIconViewItem *item = new QtFileIconViewItem( this, new QFileInfo( *fi ) );
551 item->setKey( QString( "000000%1" ).arg( fi->fileName() ) );
552 delete fi;
553 repaintContents( contentsX(), contentsY(), contentsWidth(), contentsHeight(), FALSE );
554 ensureItemVisible( item );
555 item->setSelected( TRUE, TRUE );
556 setCurrentItem( item );
557 repaintItem( item );
558 qApp->processEvents();
559 item->rename();
560 }
561 setAutoArrange( TRUE );
562}
563
564QDir QtFileIconView::currentDir()
565{
566 return viewDir;
567}
568
569static bool isRoot( const QString &s )
570{
571#if defined(Q_OS_UNIX)
572 if ( s == "/" )
573 return TRUE;
574#elif defined(Q_OS_WIN32)
575 QString p = s;
576 if ( p.length() == 3 &&
577 p.right( 2 ) == ":/" )
578 return TRUE;
579 if ( p[ 0 ] == '/' && p[ 1 ] == '/' ) {
580 int slashes = p.contains( '/' );
581 if ( slashes <= 3 )
582 return TRUE;
583 if ( slashes == 4 && p[ (int)p.length() - 1 ] == '/' )
584 return TRUE;
585 }
586#endif
587
588 return FALSE;
589}
590
591void QtFileIconView::readDir( const QDir &dir )
592{
593 if ( !dir.isReadable() )
594 return;
595
596 if ( isRoot( dir.absPath() ) )
597 emit disableUp();
598 else
599 emit enableUp();
600
601 clear();
602
603 emit directoryChanged( dir.absPath() );
604
605 const QFileInfoList *filist = dir.entryInfoList( QDir::DefaultFilter, QDir::DirsFirst | QDir::Name );
606
607 emit startReadDir( filist->count() );
608
609 QFileInfoListIterator it( *filist );
610 QFileInfo *fi;
611 bool allowRename = FALSE, allowRenameSet = FALSE;
612 while ( ( fi = it.current() ) != 0 ) {
613 ++it;
614 if ( fi && fi->fileName() == ".." && ( fi->dirPath() == "/" || fi->dirPath().isEmpty() ) )
615 continue;
616 emit readNextDir();
617 QtFileIconViewItem *item = new QtFileIconViewItem( this, new QFileInfo( *fi ) );
618 if ( fi->isDir() )
619 item->setKey( QString( "000000%1" ).arg( fi->fileName() ) );
620 else
621 item->setKey( fi->fileName() );
622 if ( !allowRenameSet ) {
623 if ( !QFileInfo( fi->absFilePath() ).isWritable() ||
624 item->text() == "." || item->text() == ".." )
625 allowRename = FALSE;
626 else
627 allowRename = TRUE;
628 if ( item->text() == "." || item->text() == ".." )
629 allowRenameSet = FALSE;
630 else
631 allowRenameSet = TRUE;
632 }
633 item->setRenameEnabled( allowRename );
634 }
635
636 if ( !QFileInfo( dir.absPath() ).isWritable() )
637 emit disableMkdir();
638 else
639 emit enableMkdir();
640
641 emit readDirDone();
642}
643
644void QtFileIconView::itemDoubleClicked( QIconViewItem *i )
645{
646 QtFileIconViewItem *item = ( QtFileIconViewItem* )i;
647
648 if ( item->type() == QtFileIconViewItem::Dir ) {
649 viewDir = QDir( item->filename() );
650 readDir( viewDir );
651 } else if ( item->type() == QtFileIconViewItem::Link &&
652 QFileInfo( QFileInfo( item->filename() ).readLink() ).isDir() ) {
653 viewDir = QDir( QFileInfo( item->filename() ).readLink() );
654 readDir( viewDir );
655 }
656}
657
658QDragObject *QtFileIconView::dragObject()
659{
660 if ( !currentItem() )
661 return 0;
662
663 QPoint orig = viewportToContents( viewport()->mapFromGlobal( QCursor::pos() ) );
664 QtFileIconDrag *drag = new QtFileIconDrag( viewport() );
665 drag->setPixmap( *currentItem()->pixmap(),
666 QPoint( currentItem()->pixmapRect().width() / 2, currentItem()->pixmapRect().height() / 2 ) );
667 for ( QtFileIconViewItem *item = (QtFileIconViewItem*)firstItem(); item;
668 item = (QtFileIconViewItem*)item->nextItem() ) {
669 if ( item->isSelected() ) {
670 QIconDragItem id;
671 id.setData( QCString( item->filename() ) );
672 drag->append( id,
673 QRect( item->pixmapRect( FALSE ).x() - orig.x(),
674 item->pixmapRect( FALSE ).y() - orig.y(),
675 item->pixmapRect().width(), item->pixmapRect().height() ),
676 QRect( item->textRect( FALSE ).x() - orig.x(),
677 item->textRect( FALSE ).y() - orig.y(),
678 item->textRect().width(), item->textRect().height() ),
679 QString( item->filename() ) );
680 }
681 }
682
683 return drag;
684}
685
686void QtFileIconView::keyPressEvent( QKeyEvent *e )
687{
688 if ( e->key() == Key_N &&
689 ( e->state() & ControlButton ) )
690 newDirectory();
691 else
692 QIconView::keyPressEvent( e );
693}
694
695void QtFileIconView::slotDropped( QDropEvent *e, const QValueList<QIconDragItem> & )
696{
697 if ( openItem )
698 openItem->timer.stop();
699 if ( !QUriDrag::canDecode( e ) ) {
700 e->ignore();
701 return;
702 }
703
704 QStringList lst;
705 QUriDrag::decodeLocalFiles( e, lst );
706
707 QString str;
708 if ( e->action() == QDropEvent::Copy )
709 str = "Copy\n\n";
710 else
711 str = "Move\n\n";
712 for ( uint i = 0; i < lst.count(); ++i )
713 str += QString( " %1\n" ).arg( QDir::convertSeparators(lst[i]) );
714 str += QString( "\n"
715 "To\n\n"
716 " %1" ).arg( viewDir.absPath() );
717
718 QMessageBox::information( this, e->action() == QDropEvent::Copy ? "Copy" : "Move" , str, "Not Implemented" );
719 if ( e->action() == QDropEvent::Move )
720 QMessageBox::information( this, "Remove" , QDir::convertSeparators(lst.join("\n")), "Not Implemented" );
721 e->acceptAction();
722 openItem = 0;
723}
724
725void QtFileIconView::viewLarge()
726{
727 setViewMode( Large );
728}
729
730void QtFileIconView::viewSmall()
731{
732 setViewMode( Small );
733}
734
735void QtFileIconView::viewBottom()
736{
737 setItemTextPos( Bottom );
738}
739
740void QtFileIconView::viewRight()
741{
742 setItemTextPos( Right );
743}
744
745void QtFileIconView::flowEast()
746{
747 setHScrollBarMode( AlwaysOff );
748 setVScrollBarMode( Auto );
749 setArrangement( LeftToRight );
750}
751
752void QtFileIconView::flowSouth()
753{
754 setVScrollBarMode( AlwaysOff );
755 setHScrollBarMode( Auto );
756 setArrangement( TopToBottom );
757}
758
759void QtFileIconView::sortAscending()
760{
761 sort( TRUE );
762}
763
764void QtFileIconView::sortDescending()
765{
766 sort( FALSE );
767}
768
769void QtFileIconView::itemTextTruncate()
770{
771 setWordWrapIconText( FALSE );
772}
773
774void QtFileIconView::itemTextWordWrap()
775{
776 setWordWrapIconText( TRUE );
777}
778
779void QtFileIconView::slotRightPressed( QIconViewItem *item )
780{
781 if ( !item ) { // right pressed on viewport
782 QPopupMenu menu( this );
783
784 menu.insertItem( "&Large view", this, SLOT( viewLarge() ) );
785 menu.insertItem( "&Small view", this, SLOT( viewSmall() ) );
786 menu.insertSeparator();
787 menu.insertItem( "Text at the &bottom", this, SLOT( viewBottom() ) );
788 menu.insertItem( "Text at the &right", this, SLOT( viewRight() ) );
789 menu.insertSeparator();
790 menu.insertItem( "Arrange l&eft to right", this, SLOT( flowEast() ) );
791 menu.insertItem( "Arrange t&op to bottom", this, SLOT( flowSouth() ) );
792 menu.insertSeparator();
793 menu.insertItem( "&Truncate item text", this, SLOT( itemTextTruncate() ) );
794 menu.insertItem( "&Wordwrap item text", this, SLOT( itemTextWordWrap() ) );
795 menu.insertSeparator();
796 menu.insertItem( "Arrange items in &grid", this, SLOT( arrangeItemsInGrid() ) );
797 menu.insertSeparator();
798 menu.insertItem( "Sort &ascending", this, SLOT( sortAscending() ) );
799 menu.insertItem( "Sort &descending", this, SLOT( sortDescending() ) );
800
801 menu.setMouseTracking( TRUE );
802 menu.exec( QCursor::pos() );
803 } else { // on item
804 QPopupMenu menu( this );
805
806 int RENAME_ITEM = menu.insertItem( "Rename Item" );
807 int REMOVE_ITEM = menu.insertItem( "Remove Item" );
808
809 menu.setMouseTracking( TRUE );
810 int id = menu.exec( QCursor::pos() );
811
812 if ( id == -1 )
813 return;
814
815 if ( id == RENAME_ITEM && item->renameEnabled() ) {
816 item->rename();
817 } else if ( id == REMOVE_ITEM ) {
818 delete item;
819 QMessageBox::information( this, "Not implemented!", "Deleting files not implemented yet,\n"
820 "The item has only been removed from the view! " );
821 }
822 }
823}
824
825void QtFileIconView::setViewMode( ViewMode m )
826{
827 if ( m == vm )
828 return;
829
830 vm = m;
831 QtFileIconViewItem *item = (QtFileIconViewItem*)firstItem();
832 for ( ; item; item = (QtFileIconViewItem*)item->nextItem() )
833 item->viewModeChanged( vm );
834
835 arrangeItemsInGrid();
836}
Note: See TracBrowser for help on using the repository browser.