filemanager/src/fmfiledialog/src/fmfilewidget.cpp
changeset 25 b7bfdea70ca2
parent 16 ada7962b4308
child 32 39cf9ced4cc4
equal deleted inserted replaced
16:ada7962b4308 25:b7bfdea70ca2
    18 */
    18 */
    19 #include "fmfilewidget.h"
    19 #include "fmfilewidget.h"
    20 #include "fmutils.h"
    20 #include "fmutils.h"
    21 #include "fmdrivemodel.h"
    21 #include "fmdrivemodel.h"
    22 #include "fmdrivewatcher.h"
    22 #include "fmdrivewatcher.h"
       
    23 #include "fmcommon.h"
       
    24 #include "fmfileiconprovider.h"
    23 
    25 
    24 #include "hbstyle.h"
    26 #include "hbstyle.h"
    25 #include "hbabstractitemview.h"
    27 #include "hbabstractitemview.h"
    26 #include <hblistview.h>
    28 #include <hblistview.h>
    27 
    29 
    28 #include <QModelIndex>
    30 #include <QModelIndex>
    29 #include <QGraphicsLinearLayout>
    31 #include <QGraphicsLinearLayout>
    30 #include <QDirModel>
    32 #include <QFileSystemModel>
    31 #include <QTime>
    33 #include <QTime>
    32 #include <QFileInfo>
    34 #include <QFileInfo>
    33 
    35 
    34 
    36 
    35 
    37 
    40     init();
    42     init();
    41 }
    43 }
    42 FmFileWidget::~FmFileWidget()
    44 FmFileWidget::~FmFileWidget()
    43 {
    45 {
    44     setModel( 0 );
    46     setModel( 0 );
    45     delete mDirModel;
    47     delete mFileSystemModel;
       
    48     delete mFileIconProvider;
    46     delete mDriveModel;
    49     delete mDriveModel;
    47     
    50     
    48     mDriveWatcher->cancelWatch();
    51     mDriveWatcher->cancelWatch();
    49     delete mDriveWatcher;
    52     delete mDriveWatcher;
    50     mDriveWatcher = 0;
    53     mDriveWatcher = 0;
    54 {
    57 {
    55     QModelIndex index( mListView->rootIndex() );
    58     QModelIndex index( mListView->rootIndex() );
    56     if( !index.isValid() ) {
    59     if( !index.isValid() ) {
    57         return QFileInfo();
    60         return QFileInfo();
    58     }
    61     }
    59     if( mCurrentModel == mDirModel ) {
    62     if( mCurrentModel == mFileSystemModel ) {
    60         return mDirModel->fileInfo( index );
    63         return mFileSystemModel->fileInfo( index );
    61     } else {
    64     } else {
    62         return QFileInfo();
    65         return QFileInfo();
    63     }
    66     }
    64 }
    67 }
    65 
    68 
    66 void FmFileWidget::setRootPath( const QString &pathName )
    69 void FmFileWidget::setRootPath( const QString &pathName )
    67 {
    70 {
    68     if( pathName.isEmpty() || !FmUtils::isPathAccessabel( pathName ) ) {
    71     FmLogger::log( "FmFileWidget::setRootPath start" );
       
    72 	// If path is empty or can not access, set model as DriveModel
       
    73 	// Otherwise set model as FileSystemModel, means it will return to drive view if error occur.
       
    74     if( ( pathName.isEmpty() ) || ( FmErrNone != FmUtils::isPathAccessabel( pathName ) ) ) {
       
    75         FmLogger::log( "FmFileWidget::setRootPath set drive model" );
    69         setModel( mDriveModel );
    76         setModel( mDriveModel );
       
    77         FmLogger::log( "FmFileWidget::setRootPath set drive model end" );
    70         emit pathChanged( QString() );
    78         emit pathChanged( QString() );
    71     } else {
    79     } else {
    72         setModel( mDirModel );
    80         FmLogger::log( "FmFileWidget::setRootPath set dir model end" );
    73         mListView->setRootIndex( mDirModel->index( pathName ) );
    81         setModel( mFileSystemModel );
       
    82         FmLogger::log( "FmFileWidget::setRootPath set dir model end" );
       
    83 		mListView->setRootIndex( mFileSystemModel->setRootPath( pathName ) );
       
    84         FmLogger::log( "FmFileWidget::setRootPath set rootIndex" );
    74         emit pathChanged( pathName );
    85         emit pathChanged( pathName );
    75     }    
    86     }    
    76 }
    87     FmLogger::log( "FmFileWidget::setRootPath end" );
    77 
    88 }
    78 
       
    79 void FmFileWidget::on_list_activated( const QModelIndex &index )
    89 void FmFileWidget::on_list_activated( const QModelIndex &index )
    80 {
    90     {
       
    91     mActivatedModelIndex = index;
       
    92     emit listActivated();
       
    93     }
       
    94 
       
    95 void FmFileWidget::on_listActivated()
       
    96 {
       
    97     FmLogger::log("FmFileWidget::on_list_activated start" );
    81     if( mCurrentModel == mDriveModel ) {
    98     if( mCurrentModel == mDriveModel ) {
    82         QString driveName = mDriveModel->driveName( index );
    99     //If currenty model is DriveModel, open drive and set path
       
   100         QString driveName = mDriveModel->driveName( mActivatedModelIndex );
    83         QString checkedPath = FmUtils::checkDriveToFolderFilter( driveName );
   101         QString checkedPath = FmUtils::checkDriveToFolderFilter( driveName );
    84         if( checkedPath.isEmpty() ) {
   102         if( checkedPath.isEmpty() ) {
       
   103             FmLogger::log("FmFileWidget::on_list_activated end becaise checkedpath empty" );
    85             return;
   104             return;
    86         }
   105         }
    87 
   106 
    88         setModel( mDirModel );
   107         FmLogger::log("FmFileWidget::on_list_activated setModel dir start" );
    89         mListView->setRootIndex( mDirModel->index( checkedPath ) );
   108         setModel( mFileSystemModel );
       
   109         FmLogger::log("FmFileWidget::on_list_activated setModel dir end" );
       
   110         setRootPath( checkedPath );
       
   111         FmLogger::log("FmFileWidget::on_list_activated setRootIndex" );
    90         emit pathChanged( checkedPath );
   112         emit pathChanged( checkedPath );
    91     }
   113         FmLogger::log("FmFileWidget::on_list_activated finish emit pathChanged" );
    92     else if( mCurrentModel == mDirModel ) {
   114     }
    93         if ( mDirModel->isDir(index) ) {
   115     else if( mCurrentModel == mFileSystemModel ) {
    94             changeRootIndex( index );
   116     //If currenty model is FileSystemModel, open path or emit file activate signal.
       
   117         if ( mFileSystemModel->isDir( mActivatedModelIndex ) ) {
       
   118             FmLogger::log("FmFileWidget::on_list_activated start changeRootIndex" );
       
   119             changeRootIndex( mActivatedModelIndex );
       
   120             FmLogger::log("FmFileWidget::on_list_activated finish changeRootIndex" );
    95         } else {
   121         } else {
    96             QFileInfo fileInfo( mDirModel->filePath( index ) );
   122             QFileInfo fileInfo( mFileSystemModel->filePath( mActivatedModelIndex ) );
    97             if( fileInfo.isFile() ) {
   123             if( fileInfo.isFile() ) {
    98                 emit fileActivated( fileInfo.fileName() );
   124                 emit fileActivated( fileInfo.fileName() );
       
   125                 FmLogger::log("FmFileWidget::on_list_activated finish emit fileActivated" );
    99             }
   126             }
   100         }
   127         }
   101     } else {
   128     } else {
   102         Q_ASSERT( false );
   129         Q_ASSERT( false );
   103     }
   130     }
       
   131     FmLogger::log("FmFileWidget::on_list_activated end" );
   104 }
   132 }
   105 
   133 
   106 void FmFileWidget::setModelFilter( QDir::Filters filters )
   134 void FmFileWidget::setModelFilter( QDir::Filters filters )
   107 {
   135 {
   108     mDirModel->setFilter( filters );
   136     mFileSystemModel->setFilter( filters );
   109 }
   137 }
   110 
   138 
   111 void FmFileWidget::setNameFilters( const QStringList &nameFilters )
   139 void FmFileWidget::setNameFilters( const QStringList &nameFilters )
   112 {
   140 {
   113     mDirModel->setNameFilters( nameFilters );
   141     mFileSystemModel->setNameFilters( nameFilters );
   114 }
   142 }
   115 
   143 
   116 void FmFileWidget::changeRootIndex( const QModelIndex &index )
   144 void FmFileWidget::changeRootIndex( const QModelIndex &index )
   117 {
   145 {
   118     if( mCurrentModel != mDirModel ) {
   146     FmLogger::log("FmFileWidget::changeRootIndex start" );
       
   147     if( mCurrentModel != mFileSystemModel ) {
       
   148         FmLogger::log("FmFileWidget::changeRootIndex end because model not equal mFileSystemModel" );
   119         return;
   149         return;
   120     }
   150     }
   121 
   151 
   122     mDirModel->fetchMore(index);
   152     mFileSystemModel->fetchMore(index);
   123     mListView->setRootIndex( index );
   153 	QString filePath = mFileSystemModel->fileInfo( index ).absoluteFilePath();
   124     QFileInfo fileInfo = mDirModel->fileInfo( mListView->rootIndex() );
   154     // pathChanged signal will be emitted in setRootPath
   125     QString string = fileInfo.absoluteFilePath();
   155 	setRootPath( filePath );
   126     emit pathChanged( string );
   156     FmLogger::log("FmFileWidget::changeRootIndex end" );
   127 }
   157 }
   128 
   158 
   129 void FmFileWidget::init()
   159 void FmFileWidget::init()
   130 {
   160 {
   131     mLayout = new QGraphicsLinearLayout( this );
   161     mLayout = new QGraphicsLinearLayout( this );
   136     mListView->setObjectName( "list" );
   166     mListView->setObjectName( "list" );
   137     mLayout->addItem( mListView );
   167     mLayout->addItem( mListView );
   138 
   168 
   139     mDriveModel = new FmDriveModel( this, 
   169     mDriveModel = new FmDriveModel( this, 
   140         FmDriveModel::FillWithVolume | FmDriveModel::FillWithDefaultVolume | FmDriveModel::HideUnAvailableDrive );
   170         FmDriveModel::FillWithVolume | FmDriveModel::FillWithDefaultVolume | FmDriveModel::HideUnAvailableDrive );
   141     qDebug("constructed dirveModel");
   171     
   142     qDebug( QTime::currentTime().toString().toUtf8().data() );
   172     qDebug( QTime::currentTime().toString().toUtf8().data() );
   143     mDirModel = new QDirModel( this );
   173     mFileSystemModel = new QFileSystemModel( this );
   144     qDebug("constructed dirModel");
   174     mFileIconProvider = new FmFileIconProvider();
       
   175     mFileSystemModel->setIconProvider( mFileIconProvider );
       
   176     qDebug("constructed mFileSystemModel");
       
   177     
   145     qDebug( QTime::currentTime().toString().toUtf8().data() );
   178     qDebug( QTime::currentTime().toString().toUtf8().data() );
   146     setModel( mDriveModel );
   179     setModel( mDriveModel );
   147     qDebug("setmodel");
   180     qDebug("setmodel");
   148     qDebug( QTime::currentTime().toString().toUtf8().data() );
   181     qDebug( QTime::currentTime().toString().toUtf8().data() );
   149 
   182 
   152     mDriveWatcher->startWatch();
   185     mDriveWatcher->startWatch();
   153     
   186     
   154 //    QMetaObject::connectSlotsByName( this );
   187 //    QMetaObject::connectSlotsByName( this );
   155     connect( mListView, SIGNAL( activated( QModelIndex ) ),
   188     connect( mListView, SIGNAL( activated( QModelIndex ) ),
   156         this, SLOT( on_list_activated( QModelIndex ) ) );
   189         this, SLOT( on_list_activated( QModelIndex ) ) );
   157     
   190     connect( this, SIGNAL( listActivated() ),
       
   191         this, SLOT( on_listActivated() ), Qt::QueuedConnection );
       
   192         
   158     connect( mDriveWatcher, SIGNAL( driveAddedOrChanged() ),
   193     connect( mDriveWatcher, SIGNAL( driveAddedOrChanged() ),
   159             this, SLOT( on_driveWatcher_driveAddedOrChanged() ) );
   194             this, SLOT( on_driveWatcher_driveAddedOrChanged() ) );
   160     
   195     
   161 
   196 
   162 }
   197 }
   170 FmFileWidget::ViewType FmFileWidget::currentViewType()
   205 FmFileWidget::ViewType FmFileWidget::currentViewType()
   171 {
   206 {
   172     ViewType viewType = DriveView;
   207     ViewType viewType = DriveView;
   173     if( mCurrentModel == mDriveModel ) {
   208     if( mCurrentModel == mDriveModel ) {
   174         viewType = DriveView;
   209         viewType = DriveView;
   175     } else if( mCurrentModel == mDirModel ) {
   210     } else if( mCurrentModel == mFileSystemModel ) {
   176         viewType = DirView;
   211         viewType = DirView;
   177     } else {
   212     } else {
   178         Q_ASSERT( false );
   213         Q_ASSERT( false );
   179     }
   214     }
   180     return viewType;
   215     return viewType;
   184 bool FmFileWidget::cdUp()
   219 bool FmFileWidget::cdUp()
   185 {
   220 {
   186     QString path( FmUtils::checkFolderToDriveFilter( currentPath().absoluteFilePath() ) );
   221     QString path( FmUtils::checkFolderToDriveFilter( currentPath().absoluteFilePath() ) );
   187     QFileInfo fileInfo( path );
   222     QFileInfo fileInfo( path );
   188     QString cdUpPath;
   223     QString cdUpPath;
       
   224     // path length>3 means currenty path is a sub folder, then get up level path and navigate to it
       
   225     // Otherwise means current path is a top level drive path, Should navigate to drive view. So setRootPath with empty path string.
   189     if( path.length() > 3 ) {
   226     if( path.length() > 3 ) {
   190         cdUpPath = fileInfo.dir().absolutePath();
   227         cdUpPath = fileInfo.dir().absolutePath();
   191     }
   228     }
   192     setRootPath( FmUtils::fillPathWithSplash( cdUpPath ) );
   229     setRootPath( FmUtils::fillPathWithSplash( cdUpPath ) );
   193     return true;
   230     return true;
   200     if( currentViewType() == DriveView ) {
   237     if( currentViewType() == DriveView ) {
   201         setModel( 0 );
   238         setModel( 0 );
   202         setModel( mDriveModel );
   239         setModel( mDriveModel );
   203         emit pathChanged( QString() );
   240         emit pathChanged( QString() );
   204     } else if( currentViewType() == DirView ) {
   241     } else if( currentViewType() == DirView ) {
   205         if( !FmUtils::isPathAccessabel( currentPath().absoluteFilePath() ) ) {
   242         if( FmErrNone != FmUtils::isPathAccessabel( currentPath().absoluteFilePath() ) ) {
   206             // path not available, set model to drive
   243             // path not available, set model to drive
   207             FmLogger::log( QString( "FmFileDialog_FmFileWidget::on_driveWatcher_driveAddedOrChanged path not availeable, set drivemodel:"
   244             FmLogger::log( QString( "FmFileDialog_FmFileWidget::on_driveWatcher_driveAddedOrChanged path not availeable, set drivemodel:"
   208                     + currentPath().absoluteFilePath() ) );
   245                     + currentPath().absoluteFilePath() ) );
   209             setModel( mDriveModel );
   246             setModel( mDriveModel );
   210             emit pathChanged( QString() );
   247             emit pathChanged( QString() );