一个qt程序:
----------------------------------------------------------------------------------------------------------------
1.dirview.h:
#ifndef DIRVIEW_H
#define DIRVIEW_H
#include <qlistview.h>
#include <qstring.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qtimer.h>
class QWidget;
class QDragEnterEvent;
class QDragMoveEvent;
class QDragLeaveEvent;
class QDropEvent;
class FileItem : public QListViewItem
{
public:
FileItem( QListViewItem *parent, const QString &s1, const QString &s2 )
: QListViewItem( parent, s1, s2 ), pix( 0 ) {}
const QPixmap *pixmap( int i ) const;
#if !defined(Q_NO_USING_KEYWORD)
using QListViewItem::setPixmap;
#endif
void setPixmap( QPixmap *p );
private:
QPixmap *pix;
};
class Directory : public QListViewItem
{
public:
Directory( QListView * parent, const QString& filename );
Directory( Directory * parent, const QString& filename, const QString &col2 )
: QListViewItem( parent, filename, col2 ), pix( 0 ) {}
Directory( Directory * parent, const QString& filename );
QString text( int column ) const;
QString fullName();
void setOpen( bool );
void setup();
const QPixmap *pixmap( int i ) const;
#if !defined(Q_NO_USING_KEYWORD)
using QListViewItem::setPixmap;
#endif
void setPixmap( QPixmap *p );
private:
QFile f;
Directory * p;
bool readable;
bool showDirsOnly;
QPixmap *pix;
};
class DirectoryView : public QListView
{
Q_OBJECT
public:
DirectoryView( QWidget *parent = 0, const char *name = 0, bool sdo = FALSE );
bool showDirsOnly() { return dirsOnly; }
public slots:
void setDir( const QString & );
signals:
void folderSelected( const QString & );
protected slots:
void slotFolderSelected( QListViewItem * );
void openFolder();
protected:
void contentsDragEnterEvent( QDragEnterEvent *e );
void contentsDragMoveEvent( QDragMoveEvent *e );
void contentsDragLeaveEvent( QDragLeaveEvent *e );
void contentsDropEvent( QDropEvent *e );
void contentsMouseMoveEvent( QMouseEvent *e );
void contentsMousePressEvent( QMouseEvent *e );
void contentsMouseReleaseEvent( QMouseEvent *e );
private:
QString fullPath(QListViewItem* item);
bool dirsOnly;
QListViewItem *oldCurrent;
QListViewItem *dropItem;
QTimer* autoopen_timer;
QPoint presspos;
bool mousePressed;
};
#endif
----------------------------------------------------------------------------------------------------------------------------
2.dirview.cpp
#include "dirview.h"
#include <qdir.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qpixmap.h>
#include <qevent.h>
#include <qpoint.h>
#include <qmessagebox.h>
#include <qdragobject.h>
#include <qmime.h>
#include <qstrlist.h>
#include <qstringlist.h>
#include <qapplication.h>
#include <qheader.h>
static const char* folder_closed_xpm[]={
"16 16 9 1",
"g c #808080",
"b c #c0c000",
"e c #c0c0c0",
"# c #000000",
"c c #ffff00",
". c None",
"a c #585858",
"f c #a0a0a4",
"d c #ffffff",
"..###...........",
".#abc##.........",
".#daabc#####....",
".#ddeaabbccc#...",
".#dedeeabbbba...",
".#edeeeeaaaab#..",
".#deeeeeeefe#ba.",
".#eeeeeeefef#ba.",
".#eeeeeefeff#ba.",
".#eeeeefefff#ba.",
".##geefeffff#ba.",
"...##gefffff#ba.",
".....##fffff#ba.",
".......##fff#b##",
".........##f#b##",
"...........####."};
static const char* folder_open_xpm[]={
"16 16 11 1",
"# c #000000",
"g c #c0c0c0",
"e c #303030",
"a c #ffa858",
"b c #808080",
"d c #a0a0a4",
"f c #585858",
"c c #ffdca8",
"h c #dcdcdc",
"i c #ffffff",
". c None",
"....###.........",
"....#ab##.......",
"....#acab####...",
"###.#acccccca#..",
"#ddefaaaccccca#.",
"#bdddbaaaacccab#",
".eddddbbaaaacab#",
".#bddggdbbaaaab#",
"..edgdggggbbaab#",
"..#bgggghghdaab#",
"...ebhggghicfab#",
"....#edhhiiidab#",
"......#egiiicfb#",
"........#egiibb#",
"..........#egib#",
"............#ee#"};
static const char * folder_locked[]={
"16 16 10 1",
"h c #808080",
"b c #ffa858",
"f c #c0c0c0",
"e c #c05800",
"# c #000000",
"c c #ffdca8",
". c None",
"a c #585858",
"g c #a0a0a4",
"d c #ffffff",
"..#a#...........",
".#abc####.......",
".#daa#eee#......",
".#ddf#e##b#.....",
".#dfd#e#bcb##...",
".#fdccc#daaab#..",
".#dfbbbccgfg#ba.",
".#ffb#ebbfgg#ba.",
".#ffbbe#bggg#ba.",
".#fffbbebggg#ba.",
".##hf#ebbggg#ba.",
"...###e#gggg#ba.",
".....#e#gggg#ba.",
"......###ggg#b##",
".........##g#b##",
"...........####."};
static const char * pix_file []={
"16 16 7 1",
"# c #000000",
"b c #ffffff",
"e c #000000",
"d c #404000",
"c c #c0c000",
"a c #ffffc0",
". c None",
"................",
".........#......",
"......#.#a##....",
".....#b#bbba##..",
"....#b#bbbabbb#.",
"...#b#bba##bb#..",
"..#b#abb#bb##...",
".#a#aab#bbbab##.",
"#a#aaa#bcbbbbbb#",
"#ccdc#bcbbcbbb#.",
".##c#bcbbcabb#..",
"...#acbacbbbe...",
"..#aaaacaba#....",
"...##aaaaa#.....",
".....##aa#......",
".......##......."};
QPixmap *folderLocked = 0;
QPixmap *folderClosed = 0;
QPixmap *folderOpen = 0;
QPixmap *fileNormal = 0;
/*****************************************************************************
*
* Class Directory
*
*****************************************************************************/
Directory::Directory( Directory * parent, const QString& filename )
: QListViewItem( parent ), f(filename),
showDirsOnly( parent->showDirsOnly ),
pix( 0 )
{
p = parent;
readable = QDir( fullName() ).isReadable();
if ( !readable )
setPixmap( folderLocked );
else
setPixmap( folderClosed );
}
Directory::Directory( QListView * parent, const QString& filename )
: QListViewItem( parent ), f(filename),
showDirsOnly( ( (DirectoryView*)parent )->showDirsOnly() ),
pix( 0 )
{
p = 0;
readable = QDir( fullName() ).isReadable();
}
void Directory::setPixmap( QPixmap *px )
{
pix = px;
setup();
widthChanged( 0 );
invalidateHeight();
repaint();
}
const QPixmap *Directory::pixmap( int i ) const
{
if ( i )
return 0;
return pix;
}
void Directory::setOpen( bool o )
{
if ( o )
setPixmap( folderOpen );
else
setPixmap( folderClosed );
if ( o && !childCount() ) {
QString s( fullName() );
QDir thisDir( s );
if ( !thisDir.isReadable() ) {
readable = FALSE;
setExpandable( FALSE );
return;
}
listView()->setUpdatesEnabled( FALSE );
const QFileInfoList * files = thisDir.entryInfoList();
if ( files ) {
QFileInfoListIterator it( *files );
QFileInfo * fi;
while( (fi=it.current()) != 0 ) {
++it;
if ( fi->fileName() == "." || fi->fileName() == ".." )
; // nothing
else if ( fi->isSymLink() && !showDirsOnly ) {
FileItem *item = new FileItem( this, fi->fileName(),
"Symbolic Link" );
item->setPixmap( fileNormal );
}
else if ( fi->isDir() )
(void)new Directory( this, fi->fileName() );
else if ( !showDirsOnly ) {
FileItem *item
= new FileItem( this, fi->fileName(),
fi->isFile()?"File":"Special" );
item->setPixmap( fileNormal );
}
}
}
listView()->setUpdatesEnabled( TRUE );
}
QListViewItem::setOpen( o );
}
void Directory::setup()
{
setExpandable( TRUE );
QListViewItem::setup();
}
QString Directory::fullName()
{
QString s;
if ( p ) {
s = p->fullName();
s.append( f.name() );
s.append( "/" );
} else {
s = f.name();
}
return s;
}
QString Directory::text( int column ) const
{
if ( column == 0 )
return f.name();
else if ( readable )
return "Directory";
else
return "Unreadable Directory";
}
/*****************************************************************************
*
* Class DirectoryView
*
*****************************************************************************/
DirectoryView::DirectoryView( QWidget *parent, const char *name, bool sdo )
: QListView( parent, name ), dirsOnly( sdo ), oldCurrent( 0 ),
dropItem( 0 ), mousePressed( FALSE )
{
autoopen_timer = new QTimer( this );
if ( !folderLocked ) {
folderLocked = new QPixmap( folder_locked );
folderClosed = new QPixmap( folder_closed_xpm );
folderOpen = new QPixmap( folder_open_xpm );
fileNormal = new QPixmap( pix_file );
}
connect( this, SIGNAL( doubleClicked( QListViewItem * ) ),
this, SLOT( slotFolderSelected( QListViewItem * ) ) );
connect( this, SIGNAL( returnPressed( QListViewItem * ) ),
this, SLOT( slotFolderSelected( QListViewItem * ) ) );
setAcceptDrops( TRUE );
viewport()->setAcceptDrops( TRUE );
connect( autoopen_timer, SIGNAL( timeout() ),
this, SLOT( openFolder() ) );
}
void DirectoryView::slotFolderSelected( QListViewItem *i )
{
if ( !i || !showDirsOnly() )
return;
Directory *dir = (Directory*)i;
emit folderSelected( dir->fullName() );
}
void DirectoryView::openFolder()
{
autoopen_timer->stop();
if ( dropItem && !dropItem->isOpen() ) {
dropItem->setOpen( TRUE );
dropItem->repaint();
}
}
static const int autoopenTime = 750;
void DirectoryView::contentsDragEnterEvent( QDragEnterEvent *e )
{
if ( !QUriDrag::canDecode(e) ) {
e->ignore();
return;
}
oldCurrent = currentItem();
QListViewItem *i = itemAt( contentsToViewport(e->pos()) );
if ( i ) {
dropItem = i;
autoopen_timer->start( autoopenTime );
}
}
void DirectoryView::contentsDragMoveEvent( QDragMoveEvent *e )
{
if ( !QUriDrag::canDecode(e) ) {
e->ignore();
return;
}
QPoint vp = contentsToViewport( ( (QDragMoveEvent*)e )->pos() );
QListViewItem *i = itemAt( vp );
if ( i ) {
setSelected( i, TRUE );
e->accept();
if ( i != dropItem ) {
autoopen_timer->stop();
dropItem = i;
autoopen_timer->start( autoopenTime );
}
switch ( e->action() ) {
case QDropEvent::Copy:
break;
case QDropEvent::Move:
e->acceptAction();
break;
case QDropEvent::Link:
e->acceptAction();
break;
default:
;
}
} else {
e->ignore();
autoopen_timer->stop();
dropItem = 0;
}
}
void DirectoryView::contentsDragLeaveEvent( QDragLeaveEvent * )
{
autoopen_timer->stop();
dropItem = 0;
setCurrentItem( oldCurrent );
setSelected( oldCurrent, TRUE );
}
void DirectoryView::contentsDropEvent( QDropEvent *e )
{
autoopen_timer->stop();
if ( !QUriDrag::canDecode(e) ) {
e->ignore();
return;
}
QListViewItem *item = itemAt( contentsToViewport(e->pos()) );
if ( item ) {
QStrList lst;
QUriDrag::decode( e, lst );
QString str;
switch ( e->action() ) {
case QDropEvent::Copy:
str = "Copy";
break;
case QDropEvent::Move:
str = "Move";
e->acceptAction();
break;
case QDropEvent::Link:
str = "Link";
e->acceptAction();
break;
default:
str = "Unknown";
}
str += "/n/n";
e->accept();
for ( uint i = 0; i < lst.count(); ++i ) {
QString filename = lst.at( i );
str += filename + "/n";
}
str += QString( "/nTo/n/n %1" )
.arg( fullPath(item) );
QMessageBox::information( this, "Drop target", str, "Not implemented" );
} else
e->ignore();
}
QString DirectoryView::fullPath(QListViewItem* item)
{
QString fullpath = item->text(0);
while ( (item=item->parent()) ) {
if ( item->parent() )
fullpath = item->text(0) + "/" + fullpath;
else
fullpath = item->text(0) + fullpath;
}
return fullpath;
}
void DirectoryView::contentsMousePressEvent( QMouseEvent* e )
{
QListView::contentsMousePressEvent(e);
QPoint p( contentsToViewport( e->pos() ) );
QListViewItem *i = itemAt( p );
if ( i ) {
// if the user clicked into the root decoration of the item, don't try to start a drag!
if ( p.x() > header()->cellPos( header()->mapToActual( 0 ) ) +
treeStepSize() * ( i->depth() + ( rootIsDecorated() ? 1 : 0) ) + itemMargin() ||
p.x() < header()->cellPos( header()->mapToActual( 0 ) ) ) {
presspos = e->pos();
mousePressed = TRUE;
}
}
}
void DirectoryView::contentsMouseMoveEvent( QMouseEvent* e )
{
if ( mousePressed && ( presspos - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
mousePressed = FALSE;
QListViewItem *item = itemAt( contentsToViewport(presspos) );
if ( item ) {
QString source = fullPath(item);
if ( QFile::exists(source) ) {
QUriDrag* ud = new QUriDrag(viewport());
ud->setUnicodeUris( source );
if ( ud->drag() )
QMessageBox::information( this, "Drag source",
QString("Delete ")+source, "Not implemented" );
}
}
}
}
void DirectoryView::contentsMouseReleaseEvent( QMouseEvent * )
{
mousePressed = FALSE;
}
void DirectoryView::setDir( const QString &s )
{
QListViewItemIterator it( this );
++it;
for ( ; it.current(); ++it ) {
it.current()->setOpen( FALSE );
}
QStringList lst( QStringList::split( "/", s ) );
QListViewItem *item = firstChild();
QStringList::Iterator it2 = lst.begin();
for ( ; it2 != lst.end(); ++it2 ) {
while ( item ) {
if ( item->text( 0 ) == *it2 ) {
item->setOpen( TRUE );
break;
}
item = item->itemBelow();
}
}
if ( item )
setCurrentItem( item );
}
void FileItem::setPixmap( QPixmap *p )
{
pix = p;
setup();
widthChanged( 0 );
invalidateHeight();
repaint();
}
const QPixmap *FileItem::pixmap( int i ) const
{
if ( i )
return 0;
return pix;
}
----------------------------------------------------------------------------------------------------------------
3.qfileiconview.h:
#ifndef QTFILEICONVIEW_H
#define QTFILEICONVIEW_H
#include <qiconset.h>
#include <qstring.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <qtimer.h>
#include <qiconview.h>
class QtFileIconView;
class QDragObject;
class QResizeEvent;
/*****************************************************************************
*
* Class QtFileIconDrag
*
*****************************************************************************/
class QtFileIconDrag : public QIconDrag
{
Q_OBJECT
public:
QtFileIconDrag( QWidget * dragSource, const char* name = 0 );
const char* format( int i ) const;
QByteArray encodedData( const char* mime ) const;
static bool canDecode( QMimeSource* e );
void append( const QIconDragItem &item, const QRect &pr, const QRect &tr, const QString &url );
private:
QStringList urls;
};
/*****************************************************************************
*
* Class QtFileIconView
*
*****************************************************************************/
class QtFileIconViewItem;
class QtFileIconView : public QIconView
{
Q_OBJECT
public:
QtFileIconView( const QString &dir, QWidget *parent = 0, const char *name = 0 );
enum ViewMode { Large, Small };
void setViewMode( ViewMode m );
ViewMode viewMode() const { return vm; }
void setOpenItem( QtFileIconViewItem *i ) {
openItem = i;
}
public slots:
void setDirectory( const QString &dir );
void setDirectory( const QDir &dir );
void newDirectory();
QDir currentDir();
signals:
void directoryChanged( const QString & );
void startReadDir( int dirs );
void readNextDir();
void readDirDone();
void enableUp();
void disableUp();
void enableMkdir();
void disableMkdir();
protected slots:
void itemDoubleClicked( QIconViewItem *i );
void slotDropped( QDropEvent *e, const QValueList<QIconDragItem> & );
void viewLarge();
void viewSmall();
void viewBottom();
void viewRight();
void flowEast();
void flowSouth();
void itemTextTruncate();
void itemTextWordWrap();
void sortAscending();
void sortDescending();
void arrangeItemsInGrid() {
QIconView::arrangeItemsInGrid( TRUE );
}
void slotRightPressed( QIconViewItem *item );
void openFolder();
protected:
void readDir( const QDir &dir );
virtual QDragObject *dragObject();
virtual void keyPressEvent( QKeyEvent *e );
QDir viewDir;
int newFolderNum;
QSize sz;
QPixmap pix;
ViewMode vm;
QtFileIconViewItem *openItem;
};
/*****************************************************************************
*
* Class QtFileIconViewItem
*
*****************************************************************************/
class QtFileIconViewItem : public QIconViewItem
{
friend class QtFileIconView;
public:
enum ItemType {
File = 0,
Dir,
Link
};
QtFileIconViewItem( QtFileIconView *parent, QFileInfo *fi );
virtual ~QtFileIconViewItem();
ItemType type() const
{ return itemType; }
QString filename() const { return itemFileName; }
virtual bool acceptDrop( const QMimeSource *e ) const;
virtual void setText( const QString &text );
virtual QPixmap *pixmap() const;
virtual void dragEntered();
virtual void dragLeft();
void viewModeChanged( QtFileIconView::ViewMode m );
void paintItem( QPainter *p, const QColorGroup &cg );
protected:
virtual void dropped( QDropEvent *e, const QValueList<QIconDragItem> & );
QString itemFileName;
QFileInfo *itemFileInfo;
ItemType itemType;
bool checkSetText;
QTimer timer;
QtFileIconView::ViewMode vm;
};
#endif
-------------------------------------------------------------------------------------------------------------------------
4.qfileiconview.cpp :
#include "qfileiconview.h"
#include <qpainter.h>
#include <qstringlist.h>
#include <qpixmap.h>
#include <qmime.h>
#include <qstrlist.h>
#include <qdragobject.h>
#include <qmessagebox.h>
#include <qevent.h>
#include <qpopupmenu.h>
#include <qcursor.h>
#include <qapplication.h>
#include <qwmatrix.h>
#include <stdlib.h>
static const char * file_icon[]={
"32 32 17 1",
"# c #000000",
"a c #ffffff",
"j c #808080",
"n c #a0a0a4",
"g c #c0c0c0",
"m c #004000",
"o c #000000",
"l c #004040",
"k c #404000",
"i c #c0c000",
"h c #ffff00",
"b c #ffffc0",
"e c #ff8000",
"f c #c05800",
"c c #ffa858",
"d c #ffdca8",
". c None",
"................................",
"................................",
"................................",
"................................",
".............#....###...........",
"...###......#a##.#aba##.........",
"..#cdb#....#aaaa#aaaaaa##.......",
"..#ecdb#..#aaaa#aaaaaaaba##.....",
"..#fecdb##aaaa#aaaaaaaaaaab##...",
"...#fecdb#aaa#aaaaaaabaabaaaa##.",
"....#fecdb#a#baaaaa#baaaaaabaaa#",
".....#fecdb#aaaaab#a##baaaaaaa#.",
".....##fecdb#bbba#aaaa##baaab#..",
"....#bb#fecdb#ba#aaaaaaa##aa#...",
"...#bbbb#fecdb##aaabaaaaaa##....",
"..#bbbb#b#fecdb#aaaaaaabaaaa##..",
".#bbbb#bbb#fecdg#aaaaaaaaaaaba#.",
"#hhbb#bbbbb#fegg#iiaaaaaaaaaaaa#",
"#jhhhklibbbk#ggj#aaiiaaaaaaaaa#j",
".#mjhhhkmikab####aaabiiaaaaaa#j.",
"...##jhhhmaaibbaaiibaaaiiaab#n..",
".....##j#baaaiiabaaiibaabaa#n...",
"......##baibaabiibaaaiiabb#j....",
"......#bbbbiiaabbiiaaaaabon.....",
".....#bbbbbbbiiabbaiiaab#n......",
".....#jbbbbbbbbiibaabba#n.......",
"......##jbbbbbbbbiiaabmj........",
"........##jbbbbbbbbbb#j.........",
"..........##nbbbbbbbmj..........",
"............##jbbbb#j...........",
"..............#mjj#n............",
"................##n............."};
static const char * folder_icon[]={
"32 32 11 1",
"# c #000000",
"b c #c0c000",
"d c #585858",
"a c #ffff00",
"i c #400000",
"h c #a0a0a4",
"e c #000000",
"c c #ffffff",
"f c #303030",
"g c #c0c0c0",
". c None",
"...###..........................",
"...#aa##........................",
".###baaa##......................",
".#cde#baaa##....................",
".#cccdeebaaa##..##f.............",
".#cccccdeebaaa##aaa##...........",
".#cccccccdeebaaaaaaaa##.........",
".#cccccccccdeebababaaa#.........",
".#cccccgcgghhebbbbbbbaa#........",
".#ccccccgcgggdebbbbbbba#........",
".#cccgcgcgcgghdeebiebbba#.......",
".#ccccgcggggggghdeddeeba#.......",
".#cgcgcgcggggggggghghdebb#......",
".#ccgcggggggggghghghghd#b#......",
".#cgcgcggggggggghghghhd#b#......",
".#gcggggggggghghghhhhhd#b#......",
".#cgcggggggggghghghhhhd#b#......",
".#ggggggggghghghhhhhhhdib#......",
".#gggggggggghghghhhhhhd#b#......",
".#hhggggghghghhhhhhhhhd#b#......",
".#ddhhgggghghghhhhhhhhd#b#......",
"..##ddhhghghhhhhhhhhhhdeb#......",
"....##ddhhhghhhhhhhhhhd#b#......",
"......##ddhhhhhhhhhhhhd#b#......",
"........##ddhhhhhhhhhhd#b#......",
"..........##ddhhhhhhhhd#b#......",
"............##ddhhhhhhd#b###....",
"..............##ddhhhhd#b#####..",
"................##ddhhd#b######.",
"..................##dddeb#####..",
"....................##d#b###....",
"......................####......"};
static const char * link_icon[]={
"32 32 12 1",
"# c #000000",
"h c #a0a0a4",
"b c #c00000",
"d c #585858",
"i c #400000",
"c c #ffffff",
"e c #000000",
"g c #c0c0c0",
"a c #ff0000",
"f c #303030",
"n c white",
". c None",
"...###..........................",
"...#aa##........................",
".###baaa##......................",
".#cde#baaa##....................",
".#cccdeebaaa##..##f.............",
".#cccccdeebaaa##aaa##...........",
".#cccccccdeebaaaaaaaa##.........",
".#cccccccccdeebababaaa#.........",
".#cccccgcgghhebbbbbbbaa#........",
".#ccccccgcgggdebbbbbbba#........",
".#cccgcgcgcgghdeebiebbba#.......",
".#ccccgcggggggghdeddeeba#.......",
".#cgcgcgcggggggggghghdebb#......",
".#ccgcggggggggghghghghd#b#......",
".#cgcgcggggggggghghghhd#b#......",
".#gcggggggggghghghhhhhd#b#......",
".#cgcggggggggghghghhhhd#b#......",
".#ggggggggghghghhhhhhhdib#......",
".#gggggggggghghghhhhhhd#b#......",
".#hhggggghghghhhhhhhhhd#b#......",
".#ddhhgggghghghhhhhhhhd#b#......",
"..##ddhhghghhhhhhhhhhhdeb#......",
"############hhhhhhhhhhd#b#......",
"#nnnnnnnnnn#hhhhhhhhhhd#b#......",
"#nnnnnnnnnn#hhhhhhhhhhd#b#......",
"#nn#nn#nnnn#ddhhhhhhhhd#b#......",
"#nn##n##nnn###ddhhhhhhd#b###....",
"#nnn#####nn#..##ddhhhhd#b#####..",
"#nnnnn##nnn#....##ddhhd#b######.",
"#nnnnn#nnnn#......##dddeb#####..",
"#nnnnnnnnnn#........##d#b###....",
"############..........####......"};
static const char * folder_locked_icon[]={
"32 32 12 1",
"# c #000000",
"g c #808080",
"h c #c0c0c0",
"f c #c05800",
"c c #ffffff",
"d c #585858",
"b c #ffa858",
"a c #ffdca8",
"e c #000000",
"i c #a0a0a4",
"j c #c0c0c0",
". c None",
"...###..........................",
"...#aa##........................",
".###baaa##......................",
".#cde#baaa##....................",
".#cccdeeba#######...............",
".#cccccde##fffff##..............",
".#cccccc##fffgggg#..............",
".#ccccccc#ffg####a##............",
".#ccccchc#ffg#eebbaa##..........",
".#ccccccc#ffg#ddeebbba##........",
".#ccchccc#ffg#ihddeebbba##......",
".#cccccaa#ffg#ihhhddeeba##......",
".#chchhbbaafg#ihhhihidebb#......",
".#cchccbbbbaa#ihhihihid#b#......",
".#chchhbb#bbbaaiihihiid#b#......",
".#hchhcbb#fbbbafhiiiiid#b#......",
".#chchhbb#ffgbbfihiiiid#b#......",
".#hhhhhbb#ffg#bfiiiiiid#b#......",
".#hhhhhbbaffg#bfiiiiiid#b#......",
".#iihhhjbbaab#bfiiiiiid#b#......",
".#ddiihhh#bbbabfiiiiiid#b#......",
"..##ddiih#ffbbbfiiiiiid#b#......",
"....##ddi#ffg#biiiiiiid#b#......",
"......##d#ffg#iiiiiiiid#b#......",
"........##ffg#iiiiiiiid#b#......",
".........#ffg#iiiiiiiid#b#......",
".........#ffg#ddiiiiiid#b###....",
".........##fg###ddiiiid#b#####..",
"...........####.##ddiid#b######.",
"..................##dddeb#####..",
"....................##d#b###....",
"......................####......"};
static QPixmap *iconFolderLockedLarge = 0;
static QPixmap *iconFolderLarge = 0;
static QPixmap *iconFileLarge = 0;
static QPixmap *iconLinkLarge = 0;
static QPixmap *iconFolderLockedSmall = 0;
static QPixmap *iconFolderSmall = 0;
static QPixmap *iconFileSmall = 0;
static QPixmap *iconLinkSmall = 0;
static void cleanup()
{
delete iconFolderLockedLarge;
iconFolderLockedLarge = 0;
delete iconFolderLarge;
iconFolderLarge = 0;
delete iconFileLarge;
iconFileLarge = 0;
delete iconLinkLarge;
iconLinkLarge = 0;
delete iconFolderLockedSmall;
iconFolderLockedSmall = 0;
delete iconFolderSmall;
iconFolderSmall = 0;
delete iconFileSmall;
iconFileSmall = 0;
delete iconLinkSmall;
iconLinkSmall = 0;
}
/*****************************************************************************
*
* Class QtFileIconDrag
*
*****************************************************************************/
QtFileIconDrag::QtFileIconDrag( QWidget * dragSource, const char* name )
: QIconDrag( dragSource, name )
{
}
const char* QtFileIconDrag::format( int i ) const
{
if ( i == 0 )
return "application/x-qiconlist";
else if ( i == 1 )
return "text/uri-list";
else
return 0;
}
QByteArray QtFileIconDrag::encodedData( const char* mime ) const
{
QByteArray a;
if ( QString( mime ) == "application/x-qiconlist" ) {
a = QIconDrag::encodedData( mime );
} else if ( QString( mime ) == "text/uri-list" ) {
QString s = urls.join( "/r/n" );
a.resize( s.length() );
memcpy( a.data(), s.latin1(), s.length() );
}
return a;
}
bool QtFileIconDrag::canDecode( QMimeSource* e )
{
return e->provides( "application/x-qiconlist" ) ||
e->provides( "text/uri-list" );
}
void QtFileIconDrag::append( const QIconDragItem &item, const QRect &pr,
const QRect &tr, const QString &url )
{
QIconDrag::append( item, pr, tr );
urls << url;
}
/*****************************************************************************
*
* Class QtFileIconViewItem
*
*****************************************************************************/
QtFileIconViewItem::QtFileIconViewItem( QtFileIconView *parent, QFileInfo *fi )
: QIconViewItem( parent, fi->fileName() ), itemFileName( fi->filePath() ),
itemFileInfo( fi ), checkSetText( FALSE )
{
vm = QtFileIconView::Large;
if ( itemFileInfo->isDir() )
itemType = Dir;
else if ( itemFileInfo->isFile() )
itemType = File;
if ( itemFileInfo->isSymLink() )
itemType = Link;
viewModeChanged( ( (QtFileIconView*)iconView() )->viewMode() );
if ( itemFileInfo->fileName() == "." ||
itemFileInfo->fileName() == ".." )
setRenameEnabled( FALSE );
checkSetText = TRUE;
QObject::connect( &timer, SIGNAL( timeout() ),
iconView(), SLOT( openFolder() ) );
}
void QtFileIconViewItem::paintItem( QPainter *p, const QColorGroup &cg )
{
if ( itemFileInfo->isSymLink() ) {
QFont f( p->font() );
f.setItalic( TRUE );
p->setFont( f );
}
QIconViewItem::paintItem( p, cg );
}
void QtFileIconViewItem::viewModeChanged( QtFileIconView::ViewMode m )
{
vm = m;
setDropEnabled( itemType == Dir && QDir( itemFileName ).isReadable() );
calcRect();
}
QPixmap *QtFileIconViewItem::pixmap() const
{
switch ( itemType ) {
case Dir:
{
if ( !QDir( itemFileName ).isReadable() ) {
if ( vm == QtFileIconView::Small )
return iconFolderLockedSmall;
else
return iconFolderLockedLarge;
} else {
if ( vm == QtFileIconView::Small )
return iconFolderSmall;
else
return iconFolderLarge;
}
}
case Link:
{
if ( vm == QtFileIconView::Small )
return iconLinkSmall;
else
return iconLinkLarge;
}
default:
{
if ( vm == QtFileIconView::Small )
return iconFileSmall;
else
return iconFileLarge;
}
}
}
QtFileIconViewItem::~QtFileIconViewItem()
{
delete itemFileInfo;
}
void QtFileIconViewItem::setText( const QString &text )
{
if ( checkSetText ) {
if ( text == "." || text == "." || text.isEmpty() )
return;
QDir dir( itemFileInfo->dir() );
if ( dir.rename( itemFileInfo->fileName(), text ) ) {
itemFileName = itemFileInfo->dirPath( TRUE ) + "/" + text;
delete itemFileInfo;
itemFileInfo = new QFileInfo( itemFileName );
QIconViewItem::setText( text );
}
} else {
QIconViewItem::setText( text );
}
}
bool QtFileIconViewItem::acceptDrop( const QMimeSource *e ) const
{
if ( type() == Dir && e->provides( "text/uri-list" ) &&
dropEnabled() )
return TRUE;
return FALSE;
}
void QtFileIconViewItem::dropped( QDropEvent *e, const QValueList<QIconDragItem> & )
{
timer.stop();
if ( !QUriDrag::canDecode( e ) ) {
e->ignore();
return;
}
QStrList lst;
QUriDrag::decode( e, lst );
QString str;
if ( e->action() == QDropEvent::Copy )
str = "Copy/n/n";
else
str = "Move/n/n";
for ( uint i = 0; i < lst.count(); ++i )
str += QString( " %1/n" ).arg( lst.at( i ) );
str += QString( "/n"
"To/n/n"
" %1" ).arg( filename() );
QMessageBox::information( iconView(), e->action() == QDropEvent::Copy ? "Copy" : "Move" , str, "Not Implemented" );
if ( e->action() == QDropEvent::Move )
QMessageBox::information( iconView(), "Remove" , str, "Not Implemented" );
e->acceptAction();
}
void QtFileIconViewItem::dragEntered()
{
if ( type() != Dir ||
type() == Dir && !QDir( itemFileName ).isReadable() )
return;
( (QtFileIconView*)iconView() )->setOpenItem( this );
timer.start( 1500 );
}
void QtFileIconViewItem::dragLeft()
{
if ( type() != Dir ||
type() == Dir && !QDir( itemFileName ).isReadable() )
return;
timer.stop();
}
/*****************************************************************************
*
* Class QtFileIconView
*
*****************************************************************************/
QtFileIconView::QtFileIconView( const QString &dir, QWidget *parent, const char *name )
: QIconView( parent, name ), viewDir( dir ), newFolderNum( 0 )
{
if ( !iconFolderLockedLarge ) {
qAddPostRoutine( cleanup );
QWMatrix m;
m.scale( 0.6, 0.6 );
QPixmap iconpix( folder_locked_icon );
iconFolderLockedLarge = new QPixmap( folder_locked_icon );
iconpix = iconpix.xForm( m );
iconFolderLockedSmall = new QPixmap( iconpix );
iconpix = QPixmap( folder_icon );
iconFolderLarge = new QPixmap( folder_icon );
iconpix = iconpix.xForm( m );
iconFolderSmall = new QPixmap( iconpix );
iconpix = QPixmap( file_icon );
iconFileLarge = new QPixmap( file_icon );
iconpix = iconpix.xForm( m );
iconFileSmall = new QPixmap( iconpix );
iconpix = QPixmap( link_icon );
iconLinkLarge = new QPixmap( link_icon );
iconpix = iconpix.xForm( m );
iconLinkSmall = new QPixmap( iconpix );
}
vm = Large;
setGridX( 75 );
setResizeMode( Adjust );
setWordWrapIconText( FALSE );
connect( this, SIGNAL( doubleClicked( QIconViewItem * ) ),
this, SLOT( itemDoubleClicked( QIconViewItem * ) ) );
connect( this, SIGNAL( returnPressed( QIconViewItem * ) ),
this, SLOT( itemDoubleClicked( QIconViewItem * ) ) );
connect( this, SIGNAL( dropped( QDropEvent *, const QValueList<QIconDragItem> & ) ),
this, SLOT( slotDropped( QDropEvent *, const QValueList<QIconDragItem> & ) ) );
connect( this, SIGNAL( contextMenuRequested( QIconViewItem *, const QPoint & ) ),
this, SLOT( slotRightPressed( QIconViewItem * ) ) );
setHScrollBarMode( AlwaysOff );
setVScrollBarMode( Auto );
setAutoArrange( TRUE );
setSorting( TRUE );
openItem = 0;
}
void QtFileIconView::openFolder()
{
if ( !openItem )
return;
if ( openItem->type() != QtFileIconViewItem::Dir ||
openItem->type() == QtFileIconViewItem::Dir &&
!QDir( openItem->itemFileName ).isReadable() )
return;
openItem->timer.stop();
setDirectory( openItem->itemFileName );
}
void QtFileIconView::setDirectory( const QString &dir )
{
viewDir = QDir( dir );
readDir( viewDir );
}
void QtFileIconView::setDirectory( const QDir &dir )
{
viewDir = dir;
readDir( viewDir );
}
void QtFileIconView::newDirectory()
{
setAutoArrange( FALSE );
selectAll( FALSE );
if ( viewDir.mkdir( QString( "New Folder %1" ).arg( ++newFolderNum ) ) ) {
QFileInfo *fi = new QFileInfo( viewDir, QString( "New Folder %1" ).arg( newFolderNum ) );
QtFileIconViewItem *item = new QtFileIconViewItem( this, new QFileInfo( *fi ) );
item->setKey( QString( "000000%1" ).arg( fi->fileName() ) );
delete fi;
repaintContents( contentsX(), contentsY(), contentsWidth(), contentsHeight(), FALSE );
ensureItemVisible( item );
item->setSelected( TRUE, TRUE );
setCurrentItem( item );
repaintItem( item );
qApp->processEvents();
item->rename();
}
setAutoArrange( TRUE );
}
QDir QtFileIconView::currentDir()
{
return viewDir;
}
static bool isRoot( const QString &s )
{
#if defined(Q_OS_UNIX)
if ( s == "/" )
return TRUE;
#elif defined(Q_OS_WIN32)
QString p = s;
if ( p.length() == 3 &&
p.right( 2 ) == ":/" )
return TRUE;
if ( p[ 0 ] == '/' && p[ 1 ] == '/' ) {
int slashes = p.contains( '/' );
if ( slashes <= 3 )
return TRUE;
if ( slashes == 4 && p[ (int)p.length() - 1 ] == '/' )
return TRUE;
}
#endif
return FALSE;
}
void QtFileIconView::readDir( const QDir &dir )
{
if ( !dir.isReadable() )
return;
if ( isRoot( dir.absPath() ) )
emit disableUp();
else
emit enableUp();
clear();
emit directoryChanged( dir.absPath() );
const QFileInfoList *filist = dir.entryInfoList( QDir::DefaultFilter, QDir::DirsFirst | QDir::Name );
emit startReadDir( filist->count() );
QFileInfoListIterator it( *filist );
QFileInfo *fi;
bool allowRename = FALSE, allowRenameSet = FALSE;
while ( ( fi = it.current() ) != 0 ) {
++it;
if ( fi && fi->fileName() == ".." && ( fi->dirPath() == "/" || fi->dirPath().isEmpty() ) )
continue;
emit readNextDir();
QtFileIconViewItem *item = new QtFileIconViewItem( this, new QFileInfo( *fi ) );
if ( fi->isDir() )
item->setKey( QString( "000000%1" ).arg( fi->fileName() ) );
else
item->setKey( fi->fileName() );
if ( !allowRenameSet ) {
if ( !QFileInfo( fi->absFilePath() ).isWritable() ||
item->text() == "." || item->text() == ".." )
allowRename = FALSE;
else
allowRename = TRUE;
if ( item->text() == "." || item->text() == ".." )
allowRenameSet = FALSE;
else
allowRenameSet = TRUE;
}
item->setRenameEnabled( allowRename );
}
if ( !QFileInfo( dir.absPath() ).isWritable() )
emit disableMkdir();
else
emit enableMkdir();
emit readDirDone();
}
void QtFileIconView::itemDoubleClicked( QIconViewItem *i )
{
QtFileIconViewItem *item = ( QtFileIconViewItem* )i;
if ( item->type() == QtFileIconViewItem::Dir ) {
viewDir = QDir( item->filename() );
readDir( viewDir );
} else if ( item->type() == QtFileIconViewItem::Link &&
QFileInfo( QFileInfo( item->filename() ).readLink() ).isDir() ) {
viewDir = QDir( QFileInfo( item->filename() ).readLink() );
readDir( viewDir );
}
}
QDragObject *QtFileIconView::dragObject()
{
if ( !currentItem() )
return 0;
QPoint orig = viewportToContents( viewport()->mapFromGlobal( QCursor::pos() ) );
QtFileIconDrag *drag = new QtFileIconDrag( viewport() );
drag->setPixmap( *currentItem()->pixmap(),
QPoint( currentItem()->pixmapRect().width() / 2, currentItem()->pixmapRect().height() / 2 ) );
for ( QtFileIconViewItem *item = (QtFileIconViewItem*)firstItem(); item;
item = (QtFileIconViewItem*)item->nextItem() ) {
if ( item->isSelected() ) {
QIconDragItem id;
id.setData( QCString( item->filename() ) );
drag->append( id,
QRect( item->pixmapRect( FALSE ).x() - orig.x(),
item->pixmapRect( FALSE ).y() - orig.y(),
item->pixmapRect().width(), item->pixmapRect().height() ),
QRect( item->textRect( FALSE ).x() - orig.x(),
item->textRect( FALSE ).y() - orig.y(),
item->textRect().width(), item->textRect().height() ),
QString( item->filename() ) );
}
}
return drag;
}
void QtFileIconView::keyPressEvent( QKeyEvent *e )
{
if ( e->key() == Key_N &&
( e->state() & ControlButton ) )
newDirectory();
else
QIconView::keyPressEvent( e );
}
void QtFileIconView::slotDropped( QDropEvent *e, const QValueList<QIconDragItem> & )
{
if ( openItem )
openItem->timer.stop();
if ( !QUriDrag::canDecode( e ) ) {
e->ignore();
return;
}
QStrList lst;
QUriDrag::decode( e, lst );
QString str;
if ( e->action() == QDropEvent::Copy )
str = "Copy/n/n";
else
str = "Move/n/n";
for ( uint i = 0; i < lst.count(); ++i )
str += QString( " %1/n" ).arg( lst.at( i ) );
str += QString( "/n"
"To/n/n"
" %1" ).arg( viewDir.absPath() );
QMessageBox::information( this, e->action() == QDropEvent::Copy ? "Copy" : "Move" , str, "Not Implemented" );
if ( e->action() == QDropEvent::Move )
QMessageBox::information( this, "Remove" , str, "Not Implemented" );
e->acceptAction();
openItem = 0;
}
void QtFileIconView::viewLarge()
{
setViewMode( Large );
}
void QtFileIconView::viewSmall()
{
setViewMode( Small );
}
void QtFileIconView::viewBottom()
{
setItemTextPos( Bottom );
}
void QtFileIconView::viewRight()
{
setItemTextPos( Right );
}
void QtFileIconView::flowEast()
{
setHScrollBarMode( AlwaysOff );
setVScrollBarMode( Auto );
setArrangement( LeftToRight );
}
void QtFileIconView::flowSouth()
{
setVScrollBarMode( AlwaysOff );
setHScrollBarMode( Auto );
setArrangement( TopToBottom );
}
void QtFileIconView::sortAscending()
{
sort( TRUE );
}
void QtFileIconView::sortDescending()
{
sort( FALSE );
}
void QtFileIconView::itemTextTruncate()
{
setWordWrapIconText( FALSE );
}
void QtFileIconView::itemTextWordWrap()
{
setWordWrapIconText( TRUE );
}
void QtFileIconView::slotRightPressed( QIconViewItem *item )
{
if ( !item ) { // right pressed on viewport
QPopupMenu menu( this );
menu.insertItem( "&Large view", this, SLOT( viewLarge() ) );
menu.insertItem( "&Small view", this, SLOT( viewSmall() ) );
menu.insertSeparator();
menu.insertItem( "Text at the &bottom", this, SLOT( viewBottom() ) );
menu.insertItem( "Text at the &right", this, SLOT( viewRight() ) );
menu.insertSeparator();
menu.insertItem( "Arrange l&eft to right", this, SLOT( flowEast() ) );
menu.insertItem( "Arrange t&op to bottom", this, SLOT( flowSouth() ) );
menu.insertSeparator();
menu.insertItem( "&Truncate item text", this, SLOT( itemTextTruncate() ) );
menu.insertItem( "&Wordwrap item text", this, SLOT( itemTextWordWrap() ) );
menu.insertSeparator();
menu.insertItem( "Arrange items in &grid", this, SLOT( arrangeItemsInGrid() ) );
menu.insertSeparator();
menu.insertItem( "Sort &ascending", this, SLOT( sortAscending() ) );
menu.insertItem( "Sort &descending", this, SLOT( sortDescending() ) );
menu.setMouseTracking( TRUE );
menu.exec( QCursor::pos() );
} else { // on item
QPopupMenu menu( this );
int RENAME_ITEM = menu.insertItem( "Rename Item" );
int REMOVE_ITEM = menu.insertItem( "Remove Item" );
menu.setMouseTracking( TRUE );
int id = menu.exec( QCursor::pos() );
if ( id == -1 )
return;
if ( id == RENAME_ITEM && item->renameEnabled() ) {
item->rename();
} else if ( id == REMOVE_ITEM ) {
delete item;
QMessageBox::information( this, "Not implemented!", "Deleting files not implemented yet,/n"
"The item has only been removed from the view! " );
}
}
}
void QtFileIconView::setViewMode( ViewMode m )
{
if ( m == vm )
return;
vm = m;
QtFileIconViewItem *item = (QtFileIconViewItem*)firstItem();
for ( ; item; item = (QtFileIconViewItem*)item->nextItem() )
item->viewModeChanged( vm );
arrangeItemsInGrid();
}
-----------------------------------------------------------------------------------------------------------------------------------------------
5.mainwindow.h :
#ifndef MAINWIN_H
#define MAINWIN_H
#include <qmainwindow.h>
class QtFileIconView;
class DirectoryView;
class QProgressBar;
class QLabel;
class QComboBox;
class QToolButton;
class FileMainWindow : public QMainWindow
{
Q_OBJECT
public:
FileMainWindow();
QtFileIconView *fileView() { return fileview; }
DirectoryView *dirList() { return dirlist; }
void show();
protected:
void setup();
void setPathCombo();
QtFileIconView *fileview;
DirectoryView *dirlist;
QProgressBar *progress;
QLabel *label;
QComboBox *pathCombo;
QToolButton *upButton, *mkdirButton;
protected slots:
void directoryChanged( const QString & );
void slotStartReadDir( int dirs );
void slotReadNextDir();
void slotReadDirDone();
void cdUp();
void newFolder();
void changePath( const QString &path );
void enableUp();
void disableUp();
void enableMkdir();
void disableMkdir();
};
#endif
----------------------------------------------------------------------------------------------------------------------------------------
6.mainwindow.cpp:
#include "mainwindow.h"
#include "qfileiconview.h"
#include "../dirview/dirview.h"
#include <qsplitter.h>
#include <qprogressbar.h>
#include <qlabel.h>
#include <qstatusbar.h>
#include <qtoolbar.h>
#include <qcombobox.h>
#include <qpixmap.h>
#include <qtoolbutton.h>
#include <qdir.h>
#include <qfileinfo.h>
static const char* cdtoparent_xpm[]={
"15 13 3 1",
". c None",
"* c #000000",
"a c #ffff99",
"..*****........",
".*aaaaa*.......",
"***************",
"*aaaaaaaaaaaaa*",
"*aaaa*aaaaaaaa*",
"*aaa***aaaaaaa*",
"*aa*****aaaaaa*",
"*aaaa*aaaaaaaa*",
"*aaaa*aaaaaaaa*",
"*aaaa******aaa*",
"*aaaaaaaaaaaaa*",
"*aaaaaaaaaaaaa*",
"***************"};
static const char* newfolder_xpm[] = {
"15 14 4 1",
" c None",
". c #000000",
"+ c #FFFF00",
"@ c #FFFFFF",
" . ",
" ",
" . ",
" . . ",
" .... . . . ",
" .+@+@. . . ",
".......... . .",
".@+@+@+@+@.. ",
".+@+@+@+@+. . ",
".@+@+@+@+@. . ",
".+@+@+@+@+. ",
".@+@+@+@+@. ",
".+@+@+@+@+. ",
"........... "};
FileMainWindow::FileMainWindow()
: QMainWindow()
{
setup();
}
void FileMainWindow::show()
{
QMainWindow::show();
}
void FileMainWindow::setup()
{
QSplitter *splitter = new QSplitter( this );
dirlist = new DirectoryView( splitter, "dirlist", TRUE );
dirlist->addColumn( "Name" );
dirlist->addColumn( "Type" );
Directory *root = new Directory( dirlist, "/" );
root->setOpen( TRUE );
splitter->setResizeMode( dirlist, QSplitter::KeepSize );
fileview = new QtFileIconView( "/", splitter );
fileview->setSelectionMode( QIconView::Extended );
setCentralWidget( splitter );
QToolBar *toolbar = new QToolBar( this, "toolbar" );
setRightJustification( TRUE );
(void)new QLabel( tr( " Path: " ), toolbar );
pathCombo = new QComboBox( TRUE, toolbar );
pathCombo->setAutoCompletion( TRUE );
toolbar->setStretchableWidget( pathCombo );
connect( pathCombo, SIGNAL( activated( const QString & ) ),
this, SLOT ( changePath( const QString & ) ) );
toolbar->addSeparator();
QPixmap pix;
pix = QPixmap( cdtoparent_xpm );
upButton = new QToolButton( pix, "One directory up", QString::null,
this, SLOT( cdUp() ), toolbar, "cd up" );
pix = QPixmap( newfolder_xpm );
mkdirButton = new QToolButton( pix, "New Folder", QString::null,
this, SLOT( newFolder() ), toolbar, "new folder" );
connect( dirlist, SIGNAL( folderSelected( const QString & ) ),
fileview, SLOT ( setDirectory( const QString & ) ) );
connect( fileview, SIGNAL( directoryChanged( const QString & ) ),
this, SLOT( directoryChanged( const QString & ) ) );
connect( fileview, SIGNAL( startReadDir( int ) ),
this, SLOT( slotStartReadDir( int ) ) );
connect( fileview, SIGNAL( readNextDir() ),
this, SLOT( slotReadNextDir() ) );
connect( fileview, SIGNAL( readDirDone() ),
this, SLOT( slotReadDirDone() ) );
setDockEnabled( DockLeft, FALSE );
setDockEnabled( DockRight, FALSE );
label = new QLabel( statusBar() );
statusBar()->addWidget( label, 2, TRUE );
progress = new QProgressBar( statusBar() );
statusBar()->addWidget( progress, 1, TRUE );
connect( fileview, SIGNAL( enableUp() ),
this, SLOT( enableUp() ) );
connect( fileview, SIGNAL( disableUp() ),
this, SLOT( disableUp() ) );
connect( fileview, SIGNAL( enableMkdir() ),
this, SLOT( enableMkdir() ) );
connect( fileview, SIGNAL( disableMkdir() ),
this, SLOT( disableMkdir() ) );
}
void FileMainWindow::setPathCombo()
{
QString dir = caption();
int i = 0;
bool found = FALSE;
for ( i = 0; i < pathCombo->count(); ++i ) {
if ( pathCombo->text( i ) == dir) {
found = TRUE;
break;
}
}
if ( found )
pathCombo->setCurrentItem( i );
else {
pathCombo->insertItem( dir );
pathCombo->setCurrentItem( pathCombo->count() - 1 );
}
}
void FileMainWindow::directoryChanged( const QString &dir )
{
setCaption( dir );
setPathCombo();
}
void FileMainWindow::slotStartReadDir( int dirs )
{
label->setText( tr( " Reading Directory..." ) );
progress->reset();
progress->setTotalSteps( dirs );
}
void FileMainWindow::slotReadNextDir()
{
int p = progress->progress();
progress->setProgress( ++p );
}
void FileMainWindow::slotReadDirDone()
{
label->setText( tr( " Reading Directory Done." ) );
progress->setProgress( progress->totalSteps() );
}
void FileMainWindow::cdUp()
{
QDir dir = fileview->currentDir();
dir.cd( ".." );
fileview->setDirectory( dir );
}
void FileMainWindow::newFolder()
{
fileview->newDirectory();
}
void FileMainWindow::changePath( const QString &path )
{
if ( QFileInfo( path ).exists() )
fileview->setDirectory( path );
else
setPathCombo();
}
void FileMainWindow::enableUp()
{
upButton->setEnabled( TRUE );
}
void FileMainWindow::disableUp()
{
upButton->setEnabled( FALSE );
}
void FileMainWindow::enableMkdir()
{
mkdirButton->setEnabled( TRUE );
}
void FileMainWindow::disableMkdir()
{
mkdirButton->setEnabled( FALSE );
}
----------------------------------------------------------------------------------------------------------------
1.dirview.h:
#ifndef DIRVIEW_H
#define DIRVIEW_H
#include <qlistview.h>
#include <qstring.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qtimer.h>
class QWidget;
class QDragEnterEvent;
class QDragMoveEvent;
class QDragLeaveEvent;
class QDropEvent;
class FileItem : public QListViewItem
{
public:
FileItem( QListViewItem *parent, const QString &s1, const QString &s2 )
: QListViewItem( parent, s1, s2 ), pix( 0 ) {}
const QPixmap *pixmap( int i ) const;
#if !defined(Q_NO_USING_KEYWORD)
using QListViewItem::setPixmap;
#endif
void setPixmap( QPixmap *p );
private:
QPixmap *pix;
};
class Directory : public QListViewItem
{
public:
Directory( QListView * parent, const QString& filename );
Directory( Directory * parent, const QString& filename, const QString &col2 )
: QListViewItem( parent, filename, col2 ), pix( 0 ) {}
Directory( Directory * parent, const QString& filename );
QString text( int column ) const;
QString fullName();
void setOpen( bool );
void setup();
const QPixmap *pixmap( int i ) const;
#if !defined(Q_NO_USING_KEYWORD)
using QListViewItem::setPixmap;
#endif
void setPixmap( QPixmap *p );
private:
QFile f;
Directory * p;
bool readable;
bool showDirsOnly;
QPixmap *pix;
};
class DirectoryView : public QListView
{
Q_OBJECT
public:
DirectoryView( QWidget *parent = 0, const char *name = 0, bool sdo = FALSE );
bool showDirsOnly() { return dirsOnly; }
public slots:
void setDir( const QString & );
signals:
void folderSelected( const QString & );
protected slots:
void slotFolderSelected( QListViewItem * );
void openFolder();
protected:
void contentsDragEnterEvent( QDragEnterEvent *e );
void contentsDragMoveEvent( QDragMoveEvent *e );
void contentsDragLeaveEvent( QDragLeaveEvent *e );
void contentsDropEvent( QDropEvent *e );
void contentsMouseMoveEvent( QMouseEvent *e );
void contentsMousePressEvent( QMouseEvent *e );
void contentsMouseReleaseEvent( QMouseEvent *e );
private:
QString fullPath(QListViewItem* item);
bool dirsOnly;
QListViewItem *oldCurrent;
QListViewItem *dropItem;
QTimer* autoopen_timer;
QPoint presspos;
bool mousePressed;
};
#endif
----------------------------------------------------------------------------------------------------------------------------
2.dirview.cpp
#include "dirview.h"
#include <qdir.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qpixmap.h>
#include <qevent.h>
#include <qpoint.h>
#include <qmessagebox.h>
#include <qdragobject.h>
#include <qmime.h>
#include <qstrlist.h>
#include <qstringlist.h>
#include <qapplication.h>
#include <qheader.h>
static const char* folder_closed_xpm[]={
"16 16 9 1",
"g c #808080",
"b c #c0c000",
"e c #c0c0c0",
"# c #000000",
"c c #ffff00",
". c None",
"a c #585858",
"f c #a0a0a4",
"d c #ffffff",
"..###...........",
".#abc##.........",
".#daabc#####....",
".#ddeaabbccc#...",
".#dedeeabbbba...",
".#edeeeeaaaab#..",
".#deeeeeeefe#ba.",
".#eeeeeeefef#ba.",
".#eeeeeefeff#ba.",
".#eeeeefefff#ba.",
".##geefeffff#ba.",
"...##gefffff#ba.",
".....##fffff#ba.",
".......##fff#b##",
".........##f#b##",
"...........####."};
static const char* folder_open_xpm[]={
"16 16 11 1",
"# c #000000",
"g c #c0c0c0",
"e c #303030",
"a c #ffa858",
"b c #808080",
"d c #a0a0a4",
"f c #585858",
"c c #ffdca8",
"h c #dcdcdc",
"i c #ffffff",
". c None",
"....###.........",
"....#ab##.......",
"....#acab####...",
"###.#acccccca#..",
"#ddefaaaccccca#.",
"#bdddbaaaacccab#",
".eddddbbaaaacab#",
".#bddggdbbaaaab#",
"..edgdggggbbaab#",
"..#bgggghghdaab#",
"...ebhggghicfab#",
"....#edhhiiidab#",
"......#egiiicfb#",
"........#egiibb#",
"..........#egib#",
"............#ee#"};
static const char * folder_locked[]={
"16 16 10 1",
"h c #808080",
"b c #ffa858",
"f c #c0c0c0",
"e c #c05800",
"# c #000000",
"c c #ffdca8",
". c None",
"a c #585858",
"g c #a0a0a4",
"d c #ffffff",
"..#a#...........",
".#abc####.......",
".#daa#eee#......",
".#ddf#e##b#.....",
".#dfd#e#bcb##...",
".#fdccc#daaab#..",
".#dfbbbccgfg#ba.",
".#ffb#ebbfgg#ba.",
".#ffbbe#bggg#ba.",
".#fffbbebggg#ba.",
".##hf#ebbggg#ba.",
"...###e#gggg#ba.",
".....#e#gggg#ba.",
"......###ggg#b##",
".........##g#b##",
"...........####."};
static const char * pix_file []={
"16 16 7 1",
"# c #000000",
"b c #ffffff",
"e c #000000",
"d c #404000",
"c c #c0c000",
"a c #ffffc0",
". c None",
"................",
".........#......",
"......#.#a##....",
".....#b#bbba##..",
"....#b#bbbabbb#.",
"...#b#bba##bb#..",
"..#b#abb#bb##...",
".#a#aab#bbbab##.",
"#a#aaa#bcbbbbbb#",
"#ccdc#bcbbcbbb#.",
".##c#bcbbcabb#..",
"...#acbacbbbe...",
"..#aaaacaba#....",
"...##aaaaa#.....",
".....##aa#......",
".......##......."};
QPixmap *folderLocked = 0;
QPixmap *folderClosed = 0;
QPixmap *folderOpen = 0;
QPixmap *fileNormal = 0;
/*****************************************************************************
*
* Class Directory
*
*****************************************************************************/
Directory::Directory( Directory * parent, const QString& filename )
: QListViewItem( parent ), f(filename),
showDirsOnly( parent->showDirsOnly ),
pix( 0 )
{
p = parent;
readable = QDir( fullName() ).isReadable();
if ( !readable )
setPixmap( folderLocked );
else
setPixmap( folderClosed );
}
Directory::Directory( QListView * parent, const QString& filename )
: QListViewItem( parent ), f(filename),
showDirsOnly( ( (DirectoryView*)parent )->showDirsOnly() ),
pix( 0 )
{
p = 0;
readable = QDir( fullName() ).isReadable();
}
void Directory::setPixmap( QPixmap *px )
{
pix = px;
setup();
widthChanged( 0 );
invalidateHeight();
repaint();
}
const QPixmap *Directory::pixmap( int i ) const
{
if ( i )
return 0;
return pix;
}
void Directory::setOpen( bool o )
{
if ( o )
setPixmap( folderOpen );
else
setPixmap( folderClosed );
if ( o && !childCount() ) {
QString s( fullName() );
QDir thisDir( s );
if ( !thisDir.isReadable() ) {
readable = FALSE;
setExpandable( FALSE );
return;
}
listView()->setUpdatesEnabled( FALSE );
const QFileInfoList * files = thisDir.entryInfoList();
if ( files ) {
QFileInfoListIterator it( *files );
QFileInfo * fi;
while( (fi=it.current()) != 0 ) {
++it;
if ( fi->fileName() == "." || fi->fileName() == ".." )
; // nothing
else if ( fi->isSymLink() && !showDirsOnly ) {
FileItem *item = new FileItem( this, fi->fileName(),
"Symbolic Link" );
item->setPixmap( fileNormal );
}
else if ( fi->isDir() )
(void)new Directory( this, fi->fileName() );
else if ( !showDirsOnly ) {
FileItem *item
= new FileItem( this, fi->fileName(),
fi->isFile()?"File":"Special" );
item->setPixmap( fileNormal );
}
}
}
listView()->setUpdatesEnabled( TRUE );
}
QListViewItem::setOpen( o );
}
void Directory::setup()
{
setExpandable( TRUE );
QListViewItem::setup();
}
QString Directory::fullName()
{
QString s;
if ( p ) {
s = p->fullName();
s.append( f.name() );
s.append( "/" );
} else {
s = f.name();
}
return s;
}
QString Directory::text( int column ) const
{
if ( column == 0 )
return f.name();
else if ( readable )
return "Directory";
else
return "Unreadable Directory";
}
/*****************************************************************************
*
* Class DirectoryView
*
*****************************************************************************/
DirectoryView::DirectoryView( QWidget *parent, const char *name, bool sdo )
: QListView( parent, name ), dirsOnly( sdo ), oldCurrent( 0 ),
dropItem( 0 ), mousePressed( FALSE )
{
autoopen_timer = new QTimer( this );
if ( !folderLocked ) {
folderLocked = new QPixmap( folder_locked );
folderClosed = new QPixmap( folder_closed_xpm );
folderOpen = new QPixmap( folder_open_xpm );
fileNormal = new QPixmap( pix_file );
}
connect( this, SIGNAL( doubleClicked( QListViewItem * ) ),
this, SLOT( slotFolderSelected( QListViewItem * ) ) );
connect( this, SIGNAL( returnPressed( QListViewItem * ) ),
this, SLOT( slotFolderSelected( QListViewItem * ) ) );
setAcceptDrops( TRUE );
viewport()->setAcceptDrops( TRUE );
connect( autoopen_timer, SIGNAL( timeout() ),
this, SLOT( openFolder() ) );
}
void DirectoryView::slotFolderSelected( QListViewItem *i )
{
if ( !i || !showDirsOnly() )
return;
Directory *dir = (Directory*)i;
emit folderSelected( dir->fullName() );
}
void DirectoryView::openFolder()
{
autoopen_timer->stop();
if ( dropItem && !dropItem->isOpen() ) {
dropItem->setOpen( TRUE );
dropItem->repaint();
}
}
static const int autoopenTime = 750;
void DirectoryView::contentsDragEnterEvent( QDragEnterEvent *e )
{
if ( !QUriDrag::canDecode(e) ) {
e->ignore();
return;
}
oldCurrent = currentItem();
QListViewItem *i = itemAt( contentsToViewport(e->pos()) );
if ( i ) {
dropItem = i;
autoopen_timer->start( autoopenTime );
}
}
void DirectoryView::contentsDragMoveEvent( QDragMoveEvent *e )
{
if ( !QUriDrag::canDecode(e) ) {
e->ignore();
return;
}
QPoint vp = contentsToViewport( ( (QDragMoveEvent*)e )->pos() );
QListViewItem *i = itemAt( vp );
if ( i ) {
setSelected( i, TRUE );
e->accept();
if ( i != dropItem ) {
autoopen_timer->stop();
dropItem = i;
autoopen_timer->start( autoopenTime );
}
switch ( e->action() ) {
case QDropEvent::Copy:
break;
case QDropEvent::Move:
e->acceptAction();
break;
case QDropEvent::Link:
e->acceptAction();
break;
default:
;
}
} else {
e->ignore();
autoopen_timer->stop();
dropItem = 0;
}
}
void DirectoryView::contentsDragLeaveEvent( QDragLeaveEvent * )
{
autoopen_timer->stop();
dropItem = 0;
setCurrentItem( oldCurrent );
setSelected( oldCurrent, TRUE );
}
void DirectoryView::contentsDropEvent( QDropEvent *e )
{
autoopen_timer->stop();
if ( !QUriDrag::canDecode(e) ) {
e->ignore();
return;
}
QListViewItem *item = itemAt( contentsToViewport(e->pos()) );
if ( item ) {
QStrList lst;
QUriDrag::decode( e, lst );
QString str;
switch ( e->action() ) {
case QDropEvent::Copy:
str = "Copy";
break;
case QDropEvent::Move:
str = "Move";
e->acceptAction();
break;
case QDropEvent::Link:
str = "Link";
e->acceptAction();
break;
default:
str = "Unknown";
}
str += "/n/n";
e->accept();
for ( uint i = 0; i < lst.count(); ++i ) {
QString filename = lst.at( i );
str += filename + "/n";
}
str += QString( "/nTo/n/n %1" )
.arg( fullPath(item) );
QMessageBox::information( this, "Drop target", str, "Not implemented" );
} else
e->ignore();
}
QString DirectoryView::fullPath(QListViewItem* item)
{
QString fullpath = item->text(0);
while ( (item=item->parent()) ) {
if ( item->parent() )
fullpath = item->text(0) + "/" + fullpath;
else
fullpath = item->text(0) + fullpath;
}
return fullpath;
}
void DirectoryView::contentsMousePressEvent( QMouseEvent* e )
{
QListView::contentsMousePressEvent(e);
QPoint p( contentsToViewport( e->pos() ) );
QListViewItem *i = itemAt( p );
if ( i ) {
// if the user clicked into the root decoration of the item, don't try to start a drag!
if ( p.x() > header()->cellPos( header()->mapToActual( 0 ) ) +
treeStepSize() * ( i->depth() + ( rootIsDecorated() ? 1 : 0) ) + itemMargin() ||
p.x() < header()->cellPos( header()->mapToActual( 0 ) ) ) {
presspos = e->pos();
mousePressed = TRUE;
}
}
}
void DirectoryView::contentsMouseMoveEvent( QMouseEvent* e )
{
if ( mousePressed && ( presspos - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
mousePressed = FALSE;
QListViewItem *item = itemAt( contentsToViewport(presspos) );
if ( item ) {
QString source = fullPath(item);
if ( QFile::exists(source) ) {
QUriDrag* ud = new QUriDrag(viewport());
ud->setUnicodeUris( source );
if ( ud->drag() )
QMessageBox::information( this, "Drag source",
QString("Delete ")+source, "Not implemented" );
}
}
}
}
void DirectoryView::contentsMouseReleaseEvent( QMouseEvent * )
{
mousePressed = FALSE;
}
void DirectoryView::setDir( const QString &s )
{
QListViewItemIterator it( this );
++it;
for ( ; it.current(); ++it ) {
it.current()->setOpen( FALSE );
}
QStringList lst( QStringList::split( "/", s ) );
QListViewItem *item = firstChild();
QStringList::Iterator it2 = lst.begin();
for ( ; it2 != lst.end(); ++it2 ) {
while ( item ) {
if ( item->text( 0 ) == *it2 ) {
item->setOpen( TRUE );
break;
}
item = item->itemBelow();
}
}
if ( item )
setCurrentItem( item );
}
void FileItem::setPixmap( QPixmap *p )
{
pix = p;
setup();
widthChanged( 0 );
invalidateHeight();
repaint();
}
const QPixmap *FileItem::pixmap( int i ) const
{
if ( i )
return 0;
return pix;
}
----------------------------------------------------------------------------------------------------------------
3.qfileiconview.h:
#ifndef QTFILEICONVIEW_H
#define QTFILEICONVIEW_H
#include <qiconset.h>
#include <qstring.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <qtimer.h>
#include <qiconview.h>
class QtFileIconView;
class QDragObject;
class QResizeEvent;
/*****************************************************************************
*
* Class QtFileIconDrag
*
*****************************************************************************/
class QtFileIconDrag : public QIconDrag
{
Q_OBJECT
public:
QtFileIconDrag( QWidget * dragSource, const char* name = 0 );
const char* format( int i ) const;
QByteArray encodedData( const char* mime ) const;
static bool canDecode( QMimeSource* e );
void append( const QIconDragItem &item, const QRect &pr, const QRect &tr, const QString &url );
private:
QStringList urls;
};
/*****************************************************************************
*
* Class QtFileIconView
*
*****************************************************************************/
class QtFileIconViewItem;
class QtFileIconView : public QIconView
{
Q_OBJECT
public:
QtFileIconView( const QString &dir, QWidget *parent = 0, const char *name = 0 );
enum ViewMode { Large, Small };
void setViewMode( ViewMode m );
ViewMode viewMode() const { return vm; }
void setOpenItem( QtFileIconViewItem *i ) {
openItem = i;
}
public slots:
void setDirectory( const QString &dir );
void setDirectory( const QDir &dir );
void newDirectory();
QDir currentDir();
signals:
void directoryChanged( const QString & );
void startReadDir( int dirs );
void readNextDir();
void readDirDone();
void enableUp();
void disableUp();
void enableMkdir();
void disableMkdir();
protected slots:
void itemDoubleClicked( QIconViewItem *i );
void slotDropped( QDropEvent *e, const QValueList<QIconDragItem> & );
void viewLarge();
void viewSmall();
void viewBottom();
void viewRight();
void flowEast();
void flowSouth();
void itemTextTruncate();
void itemTextWordWrap();
void sortAscending();
void sortDescending();
void arrangeItemsInGrid() {
QIconView::arrangeItemsInGrid( TRUE );
}
void slotRightPressed( QIconViewItem *item );
void openFolder();
protected:
void readDir( const QDir &dir );
virtual QDragObject *dragObject();
virtual void keyPressEvent( QKeyEvent *e );
QDir viewDir;
int newFolderNum;
QSize sz;
QPixmap pix;
ViewMode vm;
QtFileIconViewItem *openItem;
};
/*****************************************************************************
*
* Class QtFileIconViewItem
*
*****************************************************************************/
class QtFileIconViewItem : public QIconViewItem
{
friend class QtFileIconView;
public:
enum ItemType {
File = 0,
Dir,
Link
};
QtFileIconViewItem( QtFileIconView *parent, QFileInfo *fi );
virtual ~QtFileIconViewItem();
ItemType type() const
{ return itemType; }
QString filename() const { return itemFileName; }
virtual bool acceptDrop( const QMimeSource *e ) const;
virtual void setText( const QString &text );
virtual QPixmap *pixmap() const;
virtual void dragEntered();
virtual void dragLeft();
void viewModeChanged( QtFileIconView::ViewMode m );
void paintItem( QPainter *p, const QColorGroup &cg );
protected:
virtual void dropped( QDropEvent *e, const QValueList<QIconDragItem> & );
QString itemFileName;
QFileInfo *itemFileInfo;
ItemType itemType;
bool checkSetText;
QTimer timer;
QtFileIconView::ViewMode vm;
};
#endif
-------------------------------------------------------------------------------------------------------------------------
4.qfileiconview.cpp :
#include "qfileiconview.h"
#include <qpainter.h>
#include <qstringlist.h>
#include <qpixmap.h>
#include <qmime.h>
#include <qstrlist.h>
#include <qdragobject.h>
#include <qmessagebox.h>
#include <qevent.h>
#include <qpopupmenu.h>
#include <qcursor.h>
#include <qapplication.h>
#include <qwmatrix.h>
#include <stdlib.h>
static const char * file_icon[]={
"32 32 17 1",
"# c #000000",
"a c #ffffff",
"j c #808080",
"n c #a0a0a4",
"g c #c0c0c0",
"m c #004000",
"o c #000000",
"l c #004040",
"k c #404000",
"i c #c0c000",
"h c #ffff00",
"b c #ffffc0",
"e c #ff8000",
"f c #c05800",
"c c #ffa858",
"d c #ffdca8",
". c None",
"................................",
"................................",
"................................",
"................................",
".............#....###...........",
"...###......#a##.#aba##.........",
"..#cdb#....#aaaa#aaaaaa##.......",
"..#ecdb#..#aaaa#aaaaaaaba##.....",
"..#fecdb##aaaa#aaaaaaaaaaab##...",
"...#fecdb#aaa#aaaaaaabaabaaaa##.",
"....#fecdb#a#baaaaa#baaaaaabaaa#",
".....#fecdb#aaaaab#a##baaaaaaa#.",
".....##fecdb#bbba#aaaa##baaab#..",
"....#bb#fecdb#ba#aaaaaaa##aa#...",
"...#bbbb#fecdb##aaabaaaaaa##....",
"..#bbbb#b#fecdb#aaaaaaabaaaa##..",
".#bbbb#bbb#fecdg#aaaaaaaaaaaba#.",
"#hhbb#bbbbb#fegg#iiaaaaaaaaaaaa#",
"#jhhhklibbbk#ggj#aaiiaaaaaaaaa#j",
".#mjhhhkmikab####aaabiiaaaaaa#j.",
"...##jhhhmaaibbaaiibaaaiiaab#n..",
".....##j#baaaiiabaaiibaabaa#n...",
"......##baibaabiibaaaiiabb#j....",
"......#bbbbiiaabbiiaaaaabon.....",
".....#bbbbbbbiiabbaiiaab#n......",
".....#jbbbbbbbbiibaabba#n.......",
"......##jbbbbbbbbiiaabmj........",
"........##jbbbbbbbbbb#j.........",
"..........##nbbbbbbbmj..........",
"............##jbbbb#j...........",
"..............#mjj#n............",
"................##n............."};
static const char * folder_icon[]={
"32 32 11 1",
"# c #000000",
"b c #c0c000",
"d c #585858",
"a c #ffff00",
"i c #400000",
"h c #a0a0a4",
"e c #000000",
"c c #ffffff",
"f c #303030",
"g c #c0c0c0",
". c None",
"...###..........................",
"...#aa##........................",
".###baaa##......................",
".#cde#baaa##....................",
".#cccdeebaaa##..##f.............",
".#cccccdeebaaa##aaa##...........",
".#cccccccdeebaaaaaaaa##.........",
".#cccccccccdeebababaaa#.........",
".#cccccgcgghhebbbbbbbaa#........",
".#ccccccgcgggdebbbbbbba#........",
".#cccgcgcgcgghdeebiebbba#.......",
".#ccccgcggggggghdeddeeba#.......",
".#cgcgcgcggggggggghghdebb#......",
".#ccgcggggggggghghghghd#b#......",
".#cgcgcggggggggghghghhd#b#......",
".#gcggggggggghghghhhhhd#b#......",
".#cgcggggggggghghghhhhd#b#......",
".#ggggggggghghghhhhhhhdib#......",
".#gggggggggghghghhhhhhd#b#......",
".#hhggggghghghhhhhhhhhd#b#......",
".#ddhhgggghghghhhhhhhhd#b#......",
"..##ddhhghghhhhhhhhhhhdeb#......",
"....##ddhhhghhhhhhhhhhd#b#......",
"......##ddhhhhhhhhhhhhd#b#......",
"........##ddhhhhhhhhhhd#b#......",
"..........##ddhhhhhhhhd#b#......",
"............##ddhhhhhhd#b###....",
"..............##ddhhhhd#b#####..",
"................##ddhhd#b######.",
"..................##dddeb#####..",
"....................##d#b###....",
"......................####......"};
static const char * link_icon[]={
"32 32 12 1",
"# c #000000",
"h c #a0a0a4",
"b c #c00000",
"d c #585858",
"i c #400000",
"c c #ffffff",
"e c #000000",
"g c #c0c0c0",
"a c #ff0000",
"f c #303030",
"n c white",
". c None",
"...###..........................",
"...#aa##........................",
".###baaa##......................",
".#cde#baaa##....................",
".#cccdeebaaa##..##f.............",
".#cccccdeebaaa##aaa##...........",
".#cccccccdeebaaaaaaaa##.........",
".#cccccccccdeebababaaa#.........",
".#cccccgcgghhebbbbbbbaa#........",
".#ccccccgcgggdebbbbbbba#........",
".#cccgcgcgcgghdeebiebbba#.......",
".#ccccgcggggggghdeddeeba#.......",
".#cgcgcgcggggggggghghdebb#......",
".#ccgcggggggggghghghghd#b#......",
".#cgcgcggggggggghghghhd#b#......",
".#gcggggggggghghghhhhhd#b#......",
".#cgcggggggggghghghhhhd#b#......",
".#ggggggggghghghhhhhhhdib#......",
".#gggggggggghghghhhhhhd#b#......",
".#hhggggghghghhhhhhhhhd#b#......",
".#ddhhgggghghghhhhhhhhd#b#......",
"..##ddhhghghhhhhhhhhhhdeb#......",
"############hhhhhhhhhhd#b#......",
"#nnnnnnnnnn#hhhhhhhhhhd#b#......",
"#nnnnnnnnnn#hhhhhhhhhhd#b#......",
"#nn#nn#nnnn#ddhhhhhhhhd#b#......",
"#nn##n##nnn###ddhhhhhhd#b###....",
"#nnn#####nn#..##ddhhhhd#b#####..",
"#nnnnn##nnn#....##ddhhd#b######.",
"#nnnnn#nnnn#......##dddeb#####..",
"#nnnnnnnnnn#........##d#b###....",
"############..........####......"};
static const char * folder_locked_icon[]={
"32 32 12 1",
"# c #000000",
"g c #808080",
"h c #c0c0c0",
"f c #c05800",
"c c #ffffff",
"d c #585858",
"b c #ffa858",
"a c #ffdca8",
"e c #000000",
"i c #a0a0a4",
"j c #c0c0c0",
". c None",
"...###..........................",
"...#aa##........................",
".###baaa##......................",
".#cde#baaa##....................",
".#cccdeeba#######...............",
".#cccccde##fffff##..............",
".#cccccc##fffgggg#..............",
".#ccccccc#ffg####a##............",
".#ccccchc#ffg#eebbaa##..........",
".#ccccccc#ffg#ddeebbba##........",
".#ccchccc#ffg#ihddeebbba##......",
".#cccccaa#ffg#ihhhddeeba##......",
".#chchhbbaafg#ihhhihidebb#......",
".#cchccbbbbaa#ihhihihid#b#......",
".#chchhbb#bbbaaiihihiid#b#......",
".#hchhcbb#fbbbafhiiiiid#b#......",
".#chchhbb#ffgbbfihiiiid#b#......",
".#hhhhhbb#ffg#bfiiiiiid#b#......",
".#hhhhhbbaffg#bfiiiiiid#b#......",
".#iihhhjbbaab#bfiiiiiid#b#......",
".#ddiihhh#bbbabfiiiiiid#b#......",
"..##ddiih#ffbbbfiiiiiid#b#......",
"....##ddi#ffg#biiiiiiid#b#......",
"......##d#ffg#iiiiiiiid#b#......",
"........##ffg#iiiiiiiid#b#......",
".........#ffg#iiiiiiiid#b#......",
".........#ffg#ddiiiiiid#b###....",
".........##fg###ddiiiid#b#####..",
"...........####.##ddiid#b######.",
"..................##dddeb#####..",
"....................##d#b###....",
"......................####......"};
static QPixmap *iconFolderLockedLarge = 0;
static QPixmap *iconFolderLarge = 0;
static QPixmap *iconFileLarge = 0;
static QPixmap *iconLinkLarge = 0;
static QPixmap *iconFolderLockedSmall = 0;
static QPixmap *iconFolderSmall = 0;
static QPixmap *iconFileSmall = 0;
static QPixmap *iconLinkSmall = 0;
static void cleanup()
{
delete iconFolderLockedLarge;
iconFolderLockedLarge = 0;
delete iconFolderLarge;
iconFolderLarge = 0;
delete iconFileLarge;
iconFileLarge = 0;
delete iconLinkLarge;
iconLinkLarge = 0;
delete iconFolderLockedSmall;
iconFolderLockedSmall = 0;
delete iconFolderSmall;
iconFolderSmall = 0;
delete iconFileSmall;
iconFileSmall = 0;
delete iconLinkSmall;
iconLinkSmall = 0;
}
/*****************************************************************************
*
* Class QtFileIconDrag
*
*****************************************************************************/
QtFileIconDrag::QtFileIconDrag( QWidget * dragSource, const char* name )
: QIconDrag( dragSource, name )
{
}
const char* QtFileIconDrag::format( int i ) const
{
if ( i == 0 )
return "application/x-qiconlist";
else if ( i == 1 )
return "text/uri-list";
else
return 0;
}
QByteArray QtFileIconDrag::encodedData( const char* mime ) const
{
QByteArray a;
if ( QString( mime ) == "application/x-qiconlist" ) {
a = QIconDrag::encodedData( mime );
} else if ( QString( mime ) == "text/uri-list" ) {
QString s = urls.join( "/r/n" );
a.resize( s.length() );
memcpy( a.data(), s.latin1(), s.length() );
}
return a;
}
bool QtFileIconDrag::canDecode( QMimeSource* e )
{
return e->provides( "application/x-qiconlist" ) ||
e->provides( "text/uri-list" );
}
void QtFileIconDrag::append( const QIconDragItem &item, const QRect &pr,
const QRect &tr, const QString &url )
{
QIconDrag::append( item, pr, tr );
urls << url;
}
/*****************************************************************************
*
* Class QtFileIconViewItem
*
*****************************************************************************/
QtFileIconViewItem::QtFileIconViewItem( QtFileIconView *parent, QFileInfo *fi )
: QIconViewItem( parent, fi->fileName() ), itemFileName( fi->filePath() ),
itemFileInfo( fi ), checkSetText( FALSE )
{
vm = QtFileIconView::Large;
if ( itemFileInfo->isDir() )
itemType = Dir;
else if ( itemFileInfo->isFile() )
itemType = File;
if ( itemFileInfo->isSymLink() )
itemType = Link;
viewModeChanged( ( (QtFileIconView*)iconView() )->viewMode() );
if ( itemFileInfo->fileName() == "." ||
itemFileInfo->fileName() == ".." )
setRenameEnabled( FALSE );
checkSetText = TRUE;
QObject::connect( &timer, SIGNAL( timeout() ),
iconView(), SLOT( openFolder() ) );
}
void QtFileIconViewItem::paintItem( QPainter *p, const QColorGroup &cg )
{
if ( itemFileInfo->isSymLink() ) {
QFont f( p->font() );
f.setItalic( TRUE );
p->setFont( f );
}
QIconViewItem::paintItem( p, cg );
}
void QtFileIconViewItem::viewModeChanged( QtFileIconView::ViewMode m )
{
vm = m;
setDropEnabled( itemType == Dir && QDir( itemFileName ).isReadable() );
calcRect();
}
QPixmap *QtFileIconViewItem::pixmap() const
{
switch ( itemType ) {
case Dir:
{
if ( !QDir( itemFileName ).isReadable() ) {
if ( vm == QtFileIconView::Small )
return iconFolderLockedSmall;
else
return iconFolderLockedLarge;
} else {
if ( vm == QtFileIconView::Small )
return iconFolderSmall;
else
return iconFolderLarge;
}
}
case Link:
{
if ( vm == QtFileIconView::Small )
return iconLinkSmall;
else
return iconLinkLarge;
}
default:
{
if ( vm == QtFileIconView::Small )
return iconFileSmall;
else
return iconFileLarge;
}
}
}
QtFileIconViewItem::~QtFileIconViewItem()
{
delete itemFileInfo;
}
void QtFileIconViewItem::setText( const QString &text )
{
if ( checkSetText ) {
if ( text == "." || text == "." || text.isEmpty() )
return;
QDir dir( itemFileInfo->dir() );
if ( dir.rename( itemFileInfo->fileName(), text ) ) {
itemFileName = itemFileInfo->dirPath( TRUE ) + "/" + text;
delete itemFileInfo;
itemFileInfo = new QFileInfo( itemFileName );
QIconViewItem::setText( text );
}
} else {
QIconViewItem::setText( text );
}
}
bool QtFileIconViewItem::acceptDrop( const QMimeSource *e ) const
{
if ( type() == Dir && e->provides( "text/uri-list" ) &&
dropEnabled() )
return TRUE;
return FALSE;
}
void QtFileIconViewItem::dropped( QDropEvent *e, const QValueList<QIconDragItem> & )
{
timer.stop();
if ( !QUriDrag::canDecode( e ) ) {
e->ignore();
return;
}
QStrList lst;
QUriDrag::decode( e, lst );
QString str;
if ( e->action() == QDropEvent::Copy )
str = "Copy/n/n";
else
str = "Move/n/n";
for ( uint i = 0; i < lst.count(); ++i )
str += QString( " %1/n" ).arg( lst.at( i ) );
str += QString( "/n"
"To/n/n"
" %1" ).arg( filename() );
QMessageBox::information( iconView(), e->action() == QDropEvent::Copy ? "Copy" : "Move" , str, "Not Implemented" );
if ( e->action() == QDropEvent::Move )
QMessageBox::information( iconView(), "Remove" , str, "Not Implemented" );
e->acceptAction();
}
void QtFileIconViewItem::dragEntered()
{
if ( type() != Dir ||
type() == Dir && !QDir( itemFileName ).isReadable() )
return;
( (QtFileIconView*)iconView() )->setOpenItem( this );
timer.start( 1500 );
}
void QtFileIconViewItem::dragLeft()
{
if ( type() != Dir ||
type() == Dir && !QDir( itemFileName ).isReadable() )
return;
timer.stop();
}
/*****************************************************************************
*
* Class QtFileIconView
*
*****************************************************************************/
QtFileIconView::QtFileIconView( const QString &dir, QWidget *parent, const char *name )
: QIconView( parent, name ), viewDir( dir ), newFolderNum( 0 )
{
if ( !iconFolderLockedLarge ) {
qAddPostRoutine( cleanup );
QWMatrix m;
m.scale( 0.6, 0.6 );
QPixmap iconpix( folder_locked_icon );
iconFolderLockedLarge = new QPixmap( folder_locked_icon );
iconpix = iconpix.xForm( m );
iconFolderLockedSmall = new QPixmap( iconpix );
iconpix = QPixmap( folder_icon );
iconFolderLarge = new QPixmap( folder_icon );
iconpix = iconpix.xForm( m );
iconFolderSmall = new QPixmap( iconpix );
iconpix = QPixmap( file_icon );
iconFileLarge = new QPixmap( file_icon );
iconpix = iconpix.xForm( m );
iconFileSmall = new QPixmap( iconpix );
iconpix = QPixmap( link_icon );
iconLinkLarge = new QPixmap( link_icon );
iconpix = iconpix.xForm( m );
iconLinkSmall = new QPixmap( iconpix );
}
vm = Large;
setGridX( 75 );
setResizeMode( Adjust );
setWordWrapIconText( FALSE );
connect( this, SIGNAL( doubleClicked( QIconViewItem * ) ),
this, SLOT( itemDoubleClicked( QIconViewItem * ) ) );
connect( this, SIGNAL( returnPressed( QIconViewItem * ) ),
this, SLOT( itemDoubleClicked( QIconViewItem * ) ) );
connect( this, SIGNAL( dropped( QDropEvent *, const QValueList<QIconDragItem> & ) ),
this, SLOT( slotDropped( QDropEvent *, const QValueList<QIconDragItem> & ) ) );
connect( this, SIGNAL( contextMenuRequested( QIconViewItem *, const QPoint & ) ),
this, SLOT( slotRightPressed( QIconViewItem * ) ) );
setHScrollBarMode( AlwaysOff );
setVScrollBarMode( Auto );
setAutoArrange( TRUE );
setSorting( TRUE );
openItem = 0;
}
void QtFileIconView::openFolder()
{
if ( !openItem )
return;
if ( openItem->type() != QtFileIconViewItem::Dir ||
openItem->type() == QtFileIconViewItem::Dir &&
!QDir( openItem->itemFileName ).isReadable() )
return;
openItem->timer.stop();
setDirectory( openItem->itemFileName );
}
void QtFileIconView::setDirectory( const QString &dir )
{
viewDir = QDir( dir );
readDir( viewDir );
}
void QtFileIconView::setDirectory( const QDir &dir )
{
viewDir = dir;
readDir( viewDir );
}
void QtFileIconView::newDirectory()
{
setAutoArrange( FALSE );
selectAll( FALSE );
if ( viewDir.mkdir( QString( "New Folder %1" ).arg( ++newFolderNum ) ) ) {
QFileInfo *fi = new QFileInfo( viewDir, QString( "New Folder %1" ).arg( newFolderNum ) );
QtFileIconViewItem *item = new QtFileIconViewItem( this, new QFileInfo( *fi ) );
item->setKey( QString( "000000%1" ).arg( fi->fileName() ) );
delete fi;
repaintContents( contentsX(), contentsY(), contentsWidth(), contentsHeight(), FALSE );
ensureItemVisible( item );
item->setSelected( TRUE, TRUE );
setCurrentItem( item );
repaintItem( item );
qApp->processEvents();
item->rename();
}
setAutoArrange( TRUE );
}
QDir QtFileIconView::currentDir()
{
return viewDir;
}
static bool isRoot( const QString &s )
{
#if defined(Q_OS_UNIX)
if ( s == "/" )
return TRUE;
#elif defined(Q_OS_WIN32)
QString p = s;
if ( p.length() == 3 &&
p.right( 2 ) == ":/" )
return TRUE;
if ( p[ 0 ] == '/' && p[ 1 ] == '/' ) {
int slashes = p.contains( '/' );
if ( slashes <= 3 )
return TRUE;
if ( slashes == 4 && p[ (int)p.length() - 1 ] == '/' )
return TRUE;
}
#endif
return FALSE;
}
void QtFileIconView::readDir( const QDir &dir )
{
if ( !dir.isReadable() )
return;
if ( isRoot( dir.absPath() ) )
emit disableUp();
else
emit enableUp();
clear();
emit directoryChanged( dir.absPath() );
const QFileInfoList *filist = dir.entryInfoList( QDir::DefaultFilter, QDir::DirsFirst | QDir::Name );
emit startReadDir( filist->count() );
QFileInfoListIterator it( *filist );
QFileInfo *fi;
bool allowRename = FALSE, allowRenameSet = FALSE;
while ( ( fi = it.current() ) != 0 ) {
++it;
if ( fi && fi->fileName() == ".." && ( fi->dirPath() == "/" || fi->dirPath().isEmpty() ) )
continue;
emit readNextDir();
QtFileIconViewItem *item = new QtFileIconViewItem( this, new QFileInfo( *fi ) );
if ( fi->isDir() )
item->setKey( QString( "000000%1" ).arg( fi->fileName() ) );
else
item->setKey( fi->fileName() );
if ( !allowRenameSet ) {
if ( !QFileInfo( fi->absFilePath() ).isWritable() ||
item->text() == "." || item->text() == ".." )
allowRename = FALSE;
else
allowRename = TRUE;
if ( item->text() == "." || item->text() == ".." )
allowRenameSet = FALSE;
else
allowRenameSet = TRUE;
}
item->setRenameEnabled( allowRename );
}
if ( !QFileInfo( dir.absPath() ).isWritable() )
emit disableMkdir();
else
emit enableMkdir();
emit readDirDone();
}
void QtFileIconView::itemDoubleClicked( QIconViewItem *i )
{
QtFileIconViewItem *item = ( QtFileIconViewItem* )i;
if ( item->type() == QtFileIconViewItem::Dir ) {
viewDir = QDir( item->filename() );
readDir( viewDir );
} else if ( item->type() == QtFileIconViewItem::Link &&
QFileInfo( QFileInfo( item->filename() ).readLink() ).isDir() ) {
viewDir = QDir( QFileInfo( item->filename() ).readLink() );
readDir( viewDir );
}
}
QDragObject *QtFileIconView::dragObject()
{
if ( !currentItem() )
return 0;
QPoint orig = viewportToContents( viewport()->mapFromGlobal( QCursor::pos() ) );
QtFileIconDrag *drag = new QtFileIconDrag( viewport() );
drag->setPixmap( *currentItem()->pixmap(),
QPoint( currentItem()->pixmapRect().width() / 2, currentItem()->pixmapRect().height() / 2 ) );
for ( QtFileIconViewItem *item = (QtFileIconViewItem*)firstItem(); item;
item = (QtFileIconViewItem*)item->nextItem() ) {
if ( item->isSelected() ) {
QIconDragItem id;
id.setData( QCString( item->filename() ) );
drag->append( id,
QRect( item->pixmapRect( FALSE ).x() - orig.x(),
item->pixmapRect( FALSE ).y() - orig.y(),
item->pixmapRect().width(), item->pixmapRect().height() ),
QRect( item->textRect( FALSE ).x() - orig.x(),
item->textRect( FALSE ).y() - orig.y(),
item->textRect().width(), item->textRect().height() ),
QString( item->filename() ) );
}
}
return drag;
}
void QtFileIconView::keyPressEvent( QKeyEvent *e )
{
if ( e->key() == Key_N &&
( e->state() & ControlButton ) )
newDirectory();
else
QIconView::keyPressEvent( e );
}
void QtFileIconView::slotDropped( QDropEvent *e, const QValueList<QIconDragItem> & )
{
if ( openItem )
openItem->timer.stop();
if ( !QUriDrag::canDecode( e ) ) {
e->ignore();
return;
}
QStrList lst;
QUriDrag::decode( e, lst );
QString str;
if ( e->action() == QDropEvent::Copy )
str = "Copy/n/n";
else
str = "Move/n/n";
for ( uint i = 0; i < lst.count(); ++i )
str += QString( " %1/n" ).arg( lst.at( i ) );
str += QString( "/n"
"To/n/n"
" %1" ).arg( viewDir.absPath() );
QMessageBox::information( this, e->action() == QDropEvent::Copy ? "Copy" : "Move" , str, "Not Implemented" );
if ( e->action() == QDropEvent::Move )
QMessageBox::information( this, "Remove" , str, "Not Implemented" );
e->acceptAction();
openItem = 0;
}
void QtFileIconView::viewLarge()
{
setViewMode( Large );
}
void QtFileIconView::viewSmall()
{
setViewMode( Small );
}
void QtFileIconView::viewBottom()
{
setItemTextPos( Bottom );
}
void QtFileIconView::viewRight()
{
setItemTextPos( Right );
}
void QtFileIconView::flowEast()
{
setHScrollBarMode( AlwaysOff );
setVScrollBarMode( Auto );
setArrangement( LeftToRight );
}
void QtFileIconView::flowSouth()
{
setVScrollBarMode( AlwaysOff );
setHScrollBarMode( Auto );
setArrangement( TopToBottom );
}
void QtFileIconView::sortAscending()
{
sort( TRUE );
}
void QtFileIconView::sortDescending()
{
sort( FALSE );
}
void QtFileIconView::itemTextTruncate()
{
setWordWrapIconText( FALSE );
}
void QtFileIconView::itemTextWordWrap()
{
setWordWrapIconText( TRUE );
}
void QtFileIconView::slotRightPressed( QIconViewItem *item )
{
if ( !item ) { // right pressed on viewport
QPopupMenu menu( this );
menu.insertItem( "&Large view", this, SLOT( viewLarge() ) );
menu.insertItem( "&Small view", this, SLOT( viewSmall() ) );
menu.insertSeparator();
menu.insertItem( "Text at the &bottom", this, SLOT( viewBottom() ) );
menu.insertItem( "Text at the &right", this, SLOT( viewRight() ) );
menu.insertSeparator();
menu.insertItem( "Arrange l&eft to right", this, SLOT( flowEast() ) );
menu.insertItem( "Arrange t&op to bottom", this, SLOT( flowSouth() ) );
menu.insertSeparator();
menu.insertItem( "&Truncate item text", this, SLOT( itemTextTruncate() ) );
menu.insertItem( "&Wordwrap item text", this, SLOT( itemTextWordWrap() ) );
menu.insertSeparator();
menu.insertItem( "Arrange items in &grid", this, SLOT( arrangeItemsInGrid() ) );
menu.insertSeparator();
menu.insertItem( "Sort &ascending", this, SLOT( sortAscending() ) );
menu.insertItem( "Sort &descending", this, SLOT( sortDescending() ) );
menu.setMouseTracking( TRUE );
menu.exec( QCursor::pos() );
} else { // on item
QPopupMenu menu( this );
int RENAME_ITEM = menu.insertItem( "Rename Item" );
int REMOVE_ITEM = menu.insertItem( "Remove Item" );
menu.setMouseTracking( TRUE );
int id = menu.exec( QCursor::pos() );
if ( id == -1 )
return;
if ( id == RENAME_ITEM && item->renameEnabled() ) {
item->rename();
} else if ( id == REMOVE_ITEM ) {
delete item;
QMessageBox::information( this, "Not implemented!", "Deleting files not implemented yet,/n"
"The item has only been removed from the view! " );
}
}
}
void QtFileIconView::setViewMode( ViewMode m )
{
if ( m == vm )
return;
vm = m;
QtFileIconViewItem *item = (QtFileIconViewItem*)firstItem();
for ( ; item; item = (QtFileIconViewItem*)item->nextItem() )
item->viewModeChanged( vm );
arrangeItemsInGrid();
}
-----------------------------------------------------------------------------------------------------------------------------------------------
5.mainwindow.h :
#ifndef MAINWIN_H
#define MAINWIN_H
#include <qmainwindow.h>
class QtFileIconView;
class DirectoryView;
class QProgressBar;
class QLabel;
class QComboBox;
class QToolButton;
class FileMainWindow : public QMainWindow
{
Q_OBJECT
public:
FileMainWindow();
QtFileIconView *fileView() { return fileview; }
DirectoryView *dirList() { return dirlist; }
void show();
protected:
void setup();
void setPathCombo();
QtFileIconView *fileview;
DirectoryView *dirlist;
QProgressBar *progress;
QLabel *label;
QComboBox *pathCombo;
QToolButton *upButton, *mkdirButton;
protected slots:
void directoryChanged( const QString & );
void slotStartReadDir( int dirs );
void slotReadNextDir();
void slotReadDirDone();
void cdUp();
void newFolder();
void changePath( const QString &path );
void enableUp();
void disableUp();
void enableMkdir();
void disableMkdir();
};
#endif
----------------------------------------------------------------------------------------------------------------------------------------
6.mainwindow.cpp:
#include "mainwindow.h"
#include "qfileiconview.h"
#include "../dirview/dirview.h"
#include <qsplitter.h>
#include <qprogressbar.h>
#include <qlabel.h>
#include <qstatusbar.h>
#include <qtoolbar.h>
#include <qcombobox.h>
#include <qpixmap.h>
#include <qtoolbutton.h>
#include <qdir.h>
#include <qfileinfo.h>
static const char* cdtoparent_xpm[]={
"15 13 3 1",
". c None",
"* c #000000",
"a c #ffff99",
"..*****........",
".*aaaaa*.......",
"***************",
"*aaaaaaaaaaaaa*",
"*aaaa*aaaaaaaa*",
"*aaa***aaaaaaa*",
"*aa*****aaaaaa*",
"*aaaa*aaaaaaaa*",
"*aaaa*aaaaaaaa*",
"*aaaa******aaa*",
"*aaaaaaaaaaaaa*",
"*aaaaaaaaaaaaa*",
"***************"};
static const char* newfolder_xpm[] = {
"15 14 4 1",
" c None",
". c #000000",
"+ c #FFFF00",
"@ c #FFFFFF",
" . ",
" ",
" . ",
" . . ",
" .... . . . ",
" .+@+@. . . ",
".......... . .",
".@+@+@+@+@.. ",
".+@+@+@+@+. . ",
".@+@+@+@+@. . ",
".+@+@+@+@+. ",
".@+@+@+@+@. ",
".+@+@+@+@+. ",
"........... "};
FileMainWindow::FileMainWindow()
: QMainWindow()
{
setup();
}
void FileMainWindow::show()
{
QMainWindow::show();
}
void FileMainWindow::setup()
{
QSplitter *splitter = new QSplitter( this );
dirlist = new DirectoryView( splitter, "dirlist", TRUE );
dirlist->addColumn( "Name" );
dirlist->addColumn( "Type" );
Directory *root = new Directory( dirlist, "/" );
root->setOpen( TRUE );
splitter->setResizeMode( dirlist, QSplitter::KeepSize );
fileview = new QtFileIconView( "/", splitter );
fileview->setSelectionMode( QIconView::Extended );
setCentralWidget( splitter );
QToolBar *toolbar = new QToolBar( this, "toolbar" );
setRightJustification( TRUE );
(void)new QLabel( tr( " Path: " ), toolbar );
pathCombo = new QComboBox( TRUE, toolbar );
pathCombo->setAutoCompletion( TRUE );
toolbar->setStretchableWidget( pathCombo );
connect( pathCombo, SIGNAL( activated( const QString & ) ),
this, SLOT ( changePath( const QString & ) ) );
toolbar->addSeparator();
QPixmap pix;
pix = QPixmap( cdtoparent_xpm );
upButton = new QToolButton( pix, "One directory up", QString::null,
this, SLOT( cdUp() ), toolbar, "cd up" );
pix = QPixmap( newfolder_xpm );
mkdirButton = new QToolButton( pix, "New Folder", QString::null,
this, SLOT( newFolder() ), toolbar, "new folder" );
connect( dirlist, SIGNAL( folderSelected( const QString & ) ),
fileview, SLOT ( setDirectory( const QString & ) ) );
connect( fileview, SIGNAL( directoryChanged( const QString & ) ),
this, SLOT( directoryChanged( const QString & ) ) );
connect( fileview, SIGNAL( startReadDir( int ) ),
this, SLOT( slotStartReadDir( int ) ) );
connect( fileview, SIGNAL( readNextDir() ),
this, SLOT( slotReadNextDir() ) );
connect( fileview, SIGNAL( readDirDone() ),
this, SLOT( slotReadDirDone() ) );
setDockEnabled( DockLeft, FALSE );
setDockEnabled( DockRight, FALSE );
label = new QLabel( statusBar() );
statusBar()->addWidget( label, 2, TRUE );
progress = new QProgressBar( statusBar() );
statusBar()->addWidget( progress, 1, TRUE );
connect( fileview, SIGNAL( enableUp() ),
this, SLOT( enableUp() ) );
connect( fileview, SIGNAL( disableUp() ),
this, SLOT( disableUp() ) );
connect( fileview, SIGNAL( enableMkdir() ),
this, SLOT( enableMkdir() ) );
connect( fileview, SIGNAL( disableMkdir() ),
this, SLOT( disableMkdir() ) );
}
void FileMainWindow::setPathCombo()
{
QString dir = caption();
int i = 0;
bool found = FALSE;
for ( i = 0; i < pathCombo->count(); ++i ) {
if ( pathCombo->text( i ) == dir) {
found = TRUE;
break;
}
}
if ( found )
pathCombo->setCurrentItem( i );
else {
pathCombo->insertItem( dir );
pathCombo->setCurrentItem( pathCombo->count() - 1 );
}
}
void FileMainWindow::directoryChanged( const QString &dir )
{
setCaption( dir );
setPathCombo();
}
void FileMainWindow::slotStartReadDir( int dirs )
{
label->setText( tr( " Reading Directory..." ) );
progress->reset();
progress->setTotalSteps( dirs );
}
void FileMainWindow::slotReadNextDir()
{
int p = progress->progress();
progress->setProgress( ++p );
}
void FileMainWindow::slotReadDirDone()
{
label->setText( tr( " Reading Directory Done." ) );
progress->setProgress( progress->totalSteps() );
}
void FileMainWindow::cdUp()
{
QDir dir = fileview->currentDir();
dir.cd( ".." );
fileview->setDirectory( dir );
}
void FileMainWindow::newFolder()
{
fileview->newDirectory();
}
void FileMainWindow::changePath( const QString &path )
{
if ( QFileInfo( path ).exists() )
fileview->setDirectory( path );
else
setPathCombo();
}
void FileMainWindow::enableUp()
{
upButton->setEnabled( TRUE );
}
void FileMainWindow::disableUp()
{
upButton->setEnabled( FALSE );
}
void FileMainWindow::enableMkdir()
{
mkdirButton->setEnabled( TRUE );
}
void FileMainWindow::disableMkdir()
{
mkdirButton->setEnabled( FALSE );
}