⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dirview.cpp

📁 给予QT的qps开源最新源码
💻 CPP
字号:
/****************************************************************************
 ** $Id: dirview.cpp 237 2006-11-18 12:49:20Z ztep $
 **
 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
 **
 ** This file is part of an example program for Qt.  This example
 ** program may be used, distributed and modified without limitation.
 **
 ****************************************************************************/

#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 <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), p(parent),
   showDirsOnly(parent->showDirsOnly), pix( 0 )
{
   readable = QDir(fullName()).isReadable();

   if ( !readable ) {
      setPixmap(folderLocked);
   } else {
      setPixmap(folderClosed);
   }
}

Directory::Directory(QListView * parent, const QString & filename)
   : QListViewItem(parent), f(filename), p(0),
   showDirsOnly(((DirectoryView *)parent)->showDirsOnly()), pix( 0 )
{
   readable = QDir(fullName()).isReadable();
}

void Directory::setPixmap(QPixmap * px)
{
   pix = px;
   setup();
   widthChanged(0);
   invalidateHeight();
   repaint();
}

const QPixmap *Directory::pixmap(int i) const
{
   return ( i ? 0 : 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);
         for ( ; it.current(); ++it ) {
            QFileInfo * fi = it.current();
            if ( (fi->fileName() == ".") || (fi->fileName() == "..") ) {
               ; // nothing
            } else if ( fi->isSymLink() && !showDirsOnly ) {
               FileItem * item = new FileItem(this, fi->fileName(),
                     QObject::tr("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()
                     ? QObject::tr("File") : QObject::tr("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 QObject::tr("Directory");
   } else {
      return QObject::tr("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(pressed(QListViewItem *)),
         this, SLOT(slotFolderSelected(QListViewItem *)) );
   // 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;

#if 0
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();

}
#endif

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;
      }
   }
}

#if 0
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" );
         }
      }
   }
}
#endif

void DirectoryView::contentsMouseReleaseEvent(QMouseEvent *)
{
   mousePressed = false;
}

void DirectoryView::setDir(const QString & s)
{
   QListViewItemIterator it(this);
   for ( ++it; it.current(); ++it ) {
      it.current()->setOpen(false);
   }

   QStringList lst( QStringList::split( "/", s ) );
   QListViewItem * item = firstChild();
   QStringList::Iterator its = lst.begin();
   QStringList::Iterator ends = lst.end();
   for ( ; its != ends; ++its ) {
      while ( item ) {
         if ( item->text(0) == *its ) {
            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
{
   return ( i ? 0 : pix );
}

// end of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -