filemanager/src/inc/fmfilesystemproxymodel.cpp
changeset 48 1bebd60c0f00
child 49 81668a704644
equal deleted inserted replaced
44:22e202702210 48:1bebd60c0f00
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: proxy model for QFileSystemModel
       
    15 *
       
    16 */
       
    17 
       
    18 #include "fmfilesystemproxymodel.h"
       
    19 #include "fmutils.h"
       
    20 #include <QFileSystemModel>
       
    21 
       
    22 #include <hbglobal.h>
       
    23 
       
    24 // name column number, this define comes from implementation of QFileSystemModel
       
    25 const int NameColumn = 0;
       
    26 
       
    27 /*!
       
    28     Constructor
       
    29 */
       
    30 FmFileSystemProxyModel::FmFileSystemProxyModel( QObject *parent ) :
       
    31     QSortFilterProxyModel( parent )
       
    32 {
       
    33  
       
    34 }
       
    35 
       
    36 /*!
       
    37     Destructor
       
    38 */
       
    39 FmFileSystemProxyModel::~FmFileSystemProxyModel()
       
    40 {
       
    41 
       
    42 }
       
    43                                                                                    
       
    44 /*!
       
    45     return data by \a index and \a role
       
    46     this function will localize the folder by HbDirectoryNameLocalizer
       
    47 */
       
    48 QVariant FmFileSystemProxyModel::data ( const QModelIndex & index, int role ) const
       
    49 {
       
    50     QAbstractItemModel *itemModel = sourceModel();
       
    51     QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
       
    52     if( sourceModel && ( role == Qt::DisplayRole ) ) {
       
    53         QString name( FmUtils::localize(sourceModel->fileInfo( mapToSource( index ) ).absoluteFilePath()) );
       
    54         if( name.isEmpty() ) {
       
    55             return sourceModel->data( mapToSource( index ), role );
       
    56         } else {
       
    57             return name;
       
    58         }
       
    59     }
       
    60     if( sourceModel )
       
    61     {
       
    62 	    return sourceModel->data( mapToSource( index ), role );
       
    63     }
       
    64 		else
       
    65     {
       
    66 	    return QVariant();
       
    67     }
       
    68 }
       
    69 
       
    70 /*!
       
    71     Return fileInfo by \a index
       
    72     Return empty QFileInfo if sourceModel is not QFileSystemModel
       
    73 */
       
    74 QFileInfo FmFileSystemProxyModel::fileInfo ( const QModelIndex & index ) const
       
    75 {
       
    76     QAbstractItemModel *itemModel = sourceModel();
       
    77     QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
       
    78     if( sourceModel ) {
       
    79         return sourceModel->fileInfo( mapToSource( index ) );
       
    80     }
       
    81 
       
    82     return QFileInfo();
       
    83 }
       
    84 
       
    85 /*!
       
    86     Sets the directory \a newPath as current path to display
       
    87     Return empty QModelIndex if sourceModel is not QFileSystemModel
       
    88 */
       
    89 QModelIndex FmFileSystemProxyModel::setRootPath ( const QString & newPath )
       
    90 {
       
    91     QAbstractItemModel *itemModel = sourceModel();
       
    92     QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
       
    93     if( sourceModel ) {
       
    94         return mapFromSource( sourceModel->setRootPath( newPath ) );
       
    95     }
       
    96 
       
    97     return QModelIndex();
       
    98 }
       
    99 
       
   100 /*!
       
   101     Judge if object pointed by \a index is a directory.
       
   102     Return false also if sourceModel is not QFileSystemModel
       
   103 */
       
   104 bool FmFileSystemProxyModel::isDir ( const QModelIndex & index ) const
       
   105 {
       
   106     QAbstractItemModel *itemModel = sourceModel();
       
   107     QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
       
   108     if( sourceModel ) {
       
   109         return sourceModel->isDir( mapToSource( index ) );
       
   110     }
       
   111 
       
   112     return false;
       
   113 }
       
   114 
       
   115 /*!
       
   116     Get filePath by \a index. 
       
   117     Return empty string if sourceModel is not QFileSystemModel 
       
   118 */
       
   119 QString FmFileSystemProxyModel::filePath ( const QModelIndex & index ) const
       
   120 {
       
   121     QAbstractItemModel *itemModel = sourceModel();
       
   122     QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
       
   123     if( sourceModel ) {
       
   124         return sourceModel->filePath( mapToSource( index ) );
       
   125     }
       
   126     
       
   127     return QString();
       
   128 }
       
   129 
       
   130 /*!
       
   131     Set model filters 
       
   132     Nothing will be done if sourceModel is not QFileSystemModel 
       
   133 */
       
   134 void FmFileSystemProxyModel::setFilter ( QDir::Filters filters )
       
   135 {
       
   136     QAbstractItemModel *itemModel = sourceModel();
       
   137     QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
       
   138     if( sourceModel ) {
       
   139         sourceModel->setFilter( filters );
       
   140     }
       
   141 }
       
   142 
       
   143 /*!
       
   144     Set name filters 
       
   145     Nothing will be done if sourceModel is not QFileSystemModel 
       
   146 */
       
   147 void FmFileSystemProxyModel::setNameFilters(const QStringList &filters)
       
   148 {
       
   149     QAbstractItemModel *itemModel = sourceModel();
       
   150     QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
       
   151     if( sourceModel ) {
       
   152         sourceModel->setNameFilters( filters );
       
   153     }
       
   154 }
       
   155 
       
   156 /*
       
   157     Filter directories that should not be seen by user.
       
   158     For example, return false for such folders: C:\sys\, C:\system\, C:\private\, C:\resource\
       
   159     Return default value(true) if sourceModel is not QFileSystemModel 
       
   160 */
       
   161 bool FmFileSystemProxyModel::filterAcceptsRow(int sourceRow,
       
   162         const QModelIndex &sourceParent) const
       
   163 {
       
   164     QAbstractItemModel *itemModel = sourceModel();
       
   165     QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
       
   166     if( sourceModel ) {
       
   167         QModelIndex nameIndex = sourceModel->index(sourceRow, NameColumn, sourceParent);
       
   168         QFileInfo fileInfo = sourceModel->fileInfo( nameIndex );
       
   169         QString absoluteFilePath = fileInfo.absoluteFilePath();
       
   170         if( FmUtils::isSystemFolder( absoluteFilePath ) ) {
       
   171             return false;
       
   172         }
       
   173     }
       
   174     return true;
       
   175 }
       
   176