home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / network / ftpclient / ftpview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  4.1 KB  |  172 lines

  1. /****************************************************************************
  2. ** $Id:  qt/ftpview.cpp   3.0.0   edited Sep 28 21:04 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "ftpview.h"
  12.  
  13. #include <qpixmap.h>
  14. #include <qvaluelist.h>
  15.  
  16. /* XPM */
  17. static const char* closed_xpm[]={
  18.     "15 15 6 1",
  19.     ". c None",
  20.     "b c #ffff00",
  21.     "d c #000000",
  22.     "* c #999999",
  23.     "a c #cccccc",
  24.     "c c #ffffff",
  25.     "...............",
  26.     "..*****........",
  27.     ".*ababa*.......",
  28.     "*abababa******.",
  29.     "*cccccccccccc*d",
  30.     "*cbababababab*d",
  31.     "*cabababababa*d",
  32.     "*cbababababab*d",
  33.     "*cabababababa*d",
  34.     "*cbababababab*d",
  35.     "*cabababababa*d",
  36.     "*cbababababab*d",
  37.     "**************d",
  38.     ".dddddddddddddd",
  39.     "..............."};
  40.  
  41. /* XPM */
  42. static const char* file_xpm[]={
  43.     "13 15 5 1",
  44.     ". c #7f7f7f",
  45.     "# c None",
  46.     "c c #000000",
  47.     "b c #bfbfbf",
  48.     "a c #ffffff",
  49.     "..........###",
  50.     ".aaaaaaaab.##",
  51.     ".aaaaaaaaba.#",
  52.     ".aaaaaaaacccc",
  53.     ".aaaaaaaaaabc",
  54.     ".aaaaaaaaaabc",
  55.     ".aaaaaaaaaabc",
  56.     ".aaaaaaaaaabc",
  57.     ".aaaaaaaaaabc",
  58.     ".aaaaaaaaaabc",
  59.     ".aaaaaaaaaabc",
  60.     ".aaaaaaaaaabc",
  61.     ".aaaaaaaaaabc",
  62.     ".bbbbbbbbbbbc",
  63.     "ccccccccccccc"};
  64.  
  65. QPixmap *folderIcon = 0;
  66. QPixmap *fileIcon = 0;
  67.  
  68. FtpViewItem::FtpViewItem( QListView *parent, const QUrlInfo &i )
  69.     : QListViewItem( parent, i.name() ), info( i )
  70. {
  71. }
  72.  
  73. int FtpViewItem::compare( QListViewItem * i, int col, bool ascending ) const
  74. {
  75.     FtpViewItem *other = (FtpViewItem*)i;
  76.     switch ( col ) {
  77.     case 1:
  78.     if ( info.size() == other->info.size() )
  79.         return 0;
  80.     else
  81.         return info.size() < other->info.size() ? -1 : 1;
  82.     case 2:
  83.     if ( info.lastModified() == other->info.lastModified() )
  84.         return 0;
  85.     else
  86.         return info.lastModified() < other->info.lastModified() ? -1 : 1;
  87.     default:
  88.     // use default method for colum 0 and others added in the future
  89.     return QListViewItem::compare( i, col, ascending );
  90.     };
  91. }
  92.  
  93. QString FtpViewItem::text( int c ) const
  94. {
  95.     switch ( c ) {
  96.     case 0:
  97.     return info.name();
  98.     case 1:
  99.     return QString::number( info.size() );
  100.     case 2:
  101.     return info.lastModified().toString();
  102.     }
  103.  
  104.     return "????";
  105. }
  106.  
  107. const QPixmap *FtpViewItem::pixmap( int c ) const
  108. {
  109.     if ( !folderIcon )
  110.     folderIcon = new QPixmap( closed_xpm );
  111.     if ( !fileIcon )
  112.     fileIcon = new QPixmap( file_xpm );
  113.     if ( info.isDir() && c == 0 )
  114.     return folderIcon;
  115.     else if ( info.isFile() && c == 0 )
  116.     return fileIcon;
  117.     return 0;
  118. }
  119.  
  120.  
  121. FtpView::FtpView( QWidget *parent )
  122.     : QListView( parent )
  123. {
  124.     addColumn( tr( "Name" ) );
  125.     addColumn( tr( "Size" ) );
  126.     addColumn( tr( "Last Modified" ) );
  127.     setColumnAlignment( 1, Qt::AlignRight );
  128.     setShowSortIndicator( TRUE );
  129.     setAllColumnsShowFocus( TRUE );
  130.     setSelectionMode( Extended );
  131.  
  132.     connect( this, SIGNAL( doubleClicked( QListViewItem * ) ),
  133.          this, SLOT( slotSelected( QListViewItem * ) ) );
  134.     connect( this, SIGNAL( returnPressed( QListViewItem * ) ),
  135.          this, SLOT( slotSelected( QListViewItem * ) ) );
  136. }
  137.  
  138. void FtpView::slotInsertEntries( const QValueList<QUrlInfo> &info )
  139. {
  140.     QValueList<QUrlInfo>::ConstIterator it;
  141.     for( it = info.begin(); it != info.end(); ++it ) {
  142.     if ( (*it).name() != ".." && (*it).name() != "." && (*it).name()[ 0 ] == '.' )
  143.         continue;
  144.     FtpViewItem *item = new FtpViewItem( this, (*it) );
  145.     if ( (*it).isDir() )
  146.         item->setSelectable( FALSE );
  147.     }
  148. }
  149.  
  150. void FtpView::slotSelected( QListViewItem *item )
  151. {
  152.     if ( !item )
  153.     return;
  154.  
  155.     FtpViewItem *i = (FtpViewItem*)item;
  156.     if ( i->entryInfo().isDir() )
  157.     emit itemSelected( i->entryInfo() );
  158. }
  159.  
  160. QValueList<QUrlInfo> FtpView::selectedItems() const
  161. {
  162.     QValueList<QUrlInfo> lst;
  163.     QListViewItemIterator it( (QListView*)this );
  164.     for ( ; it.current(); ++it ) {
  165.     if ( it.current()->isSelected() ) {
  166.         lst << ( (FtpViewItem*)it.current() )->entryInfo();
  167.     }
  168.     }
  169.  
  170.     return lst;
  171. }
  172.