47
|
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>
|
49
|
23 |
#include <hbdirectorynamelocalizer.h>
|
47
|
24 |
|
|
25 |
// name column number, this define comes from implementation of QFileSystemModel
|
|
26 |
const int NameColumn = 0;
|
|
27 |
|
|
28 |
/*!
|
|
29 |
Constructor
|
|
30 |
*/
|
|
31 |
FmFileSystemProxyModel::FmFileSystemProxyModel( QObject *parent ) :
|
49
|
32 |
QSortFilterProxyModel( parent ), localizer( 0 )
|
47
|
33 |
{
|
49
|
34 |
localizer = new HbDirectoryNameLocalizer;
|
47
|
35 |
}
|
|
36 |
|
|
37 |
/*!
|
|
38 |
Destructor
|
|
39 |
*/
|
|
40 |
FmFileSystemProxyModel::~FmFileSystemProxyModel()
|
|
41 |
{
|
49
|
42 |
delete localizer;
|
47
|
43 |
}
|
|
44 |
|
|
45 |
/*!
|
|
46 |
return data by \a index and \a role
|
|
47 |
this function will localize the folder by HbDirectoryNameLocalizer
|
|
48 |
*/
|
|
49 |
QVariant FmFileSystemProxyModel::data ( const QModelIndex & index, int role ) const
|
|
50 |
{
|
|
51 |
QAbstractItemModel *itemModel = sourceModel();
|
|
52 |
QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
|
|
53 |
if( sourceModel && ( role == Qt::DisplayRole ) ) {
|
49
|
54 |
// get absolute path
|
|
55 |
QString path( sourceModel->fileInfo( mapToSource( index ) ).absoluteFilePath() );
|
|
56 |
// get localized name
|
|
57 |
QString localizedName( localizer->translate( path ) );
|
|
58 |
|
|
59 |
if( localizedName.isEmpty() ) {
|
47
|
60 |
return sourceModel->data( mapToSource( index ), role );
|
|
61 |
} else {
|
49
|
62 |
return localizedName;
|
47
|
63 |
}
|
|
64 |
}
|
|
65 |
if( sourceModel )
|
|
66 |
{
|
|
67 |
return sourceModel->data( mapToSource( index ), role );
|
|
68 |
}
|
|
69 |
else
|
|
70 |
{
|
|
71 |
return QVariant();
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
/*!
|
|
76 |
Return fileInfo by \a index
|
|
77 |
Return empty QFileInfo if sourceModel is not QFileSystemModel
|
|
78 |
*/
|
|
79 |
QFileInfo FmFileSystemProxyModel::fileInfo ( const QModelIndex & index ) const
|
|
80 |
{
|
|
81 |
QAbstractItemModel *itemModel = sourceModel();
|
|
82 |
QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
|
|
83 |
if( sourceModel ) {
|
|
84 |
return sourceModel->fileInfo( mapToSource( index ) );
|
|
85 |
}
|
|
86 |
|
|
87 |
return QFileInfo();
|
|
88 |
}
|
|
89 |
|
|
90 |
/*!
|
|
91 |
Sets the directory \a newPath as current path to display
|
|
92 |
Return empty QModelIndex if sourceModel is not QFileSystemModel
|
|
93 |
*/
|
|
94 |
QModelIndex FmFileSystemProxyModel::setRootPath ( const QString & newPath )
|
|
95 |
{
|
|
96 |
QAbstractItemModel *itemModel = sourceModel();
|
|
97 |
QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
|
|
98 |
if( sourceModel ) {
|
|
99 |
return mapFromSource( sourceModel->setRootPath( newPath ) );
|
|
100 |
}
|
|
101 |
|
|
102 |
return QModelIndex();
|
|
103 |
}
|
|
104 |
|
|
105 |
/*!
|
|
106 |
Judge if object pointed by \a index is a directory.
|
|
107 |
Return false also if sourceModel is not QFileSystemModel
|
|
108 |
*/
|
|
109 |
bool FmFileSystemProxyModel::isDir ( const QModelIndex & index ) const
|
|
110 |
{
|
|
111 |
QAbstractItemModel *itemModel = sourceModel();
|
|
112 |
QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
|
|
113 |
if( sourceModel ) {
|
|
114 |
return sourceModel->isDir( mapToSource( index ) );
|
|
115 |
}
|
|
116 |
|
|
117 |
return false;
|
|
118 |
}
|
|
119 |
|
|
120 |
/*!
|
|
121 |
Get filePath by \a index.
|
|
122 |
Return empty string if sourceModel is not QFileSystemModel
|
|
123 |
*/
|
|
124 |
QString FmFileSystemProxyModel::filePath ( const QModelIndex & index ) const
|
|
125 |
{
|
|
126 |
QAbstractItemModel *itemModel = sourceModel();
|
|
127 |
QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
|
|
128 |
if( sourceModel ) {
|
|
129 |
return sourceModel->filePath( mapToSource( index ) );
|
|
130 |
}
|
|
131 |
|
|
132 |
return QString();
|
|
133 |
}
|
|
134 |
|
|
135 |
/*!
|
|
136 |
Set model filters
|
|
137 |
Nothing will be done if sourceModel is not QFileSystemModel
|
|
138 |
*/
|
|
139 |
void FmFileSystemProxyModel::setFilter ( QDir::Filters filters )
|
|
140 |
{
|
|
141 |
QAbstractItemModel *itemModel = sourceModel();
|
|
142 |
QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
|
|
143 |
if( sourceModel ) {
|
|
144 |
sourceModel->setFilter( filters );
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
/*!
|
|
149 |
Set name filters
|
|
150 |
Nothing will be done if sourceModel is not QFileSystemModel
|
|
151 |
*/
|
|
152 |
void FmFileSystemProxyModel::setNameFilters(const QStringList &filters)
|
|
153 |
{
|
|
154 |
QAbstractItemModel *itemModel = sourceModel();
|
|
155 |
QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
|
|
156 |
if( sourceModel ) {
|
|
157 |
sourceModel->setNameFilters( filters );
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
/*
|
|
162 |
Filter directories that should not be seen by user.
|
|
163 |
For example, return false for such folders: C:\sys\, C:\system\, C:\private\, C:\resource\
|
|
164 |
Return default value(true) if sourceModel is not QFileSystemModel
|
|
165 |
*/
|
|
166 |
bool FmFileSystemProxyModel::filterAcceptsRow(int sourceRow,
|
|
167 |
const QModelIndex &sourceParent) const
|
|
168 |
{
|
|
169 |
QAbstractItemModel *itemModel = sourceModel();
|
|
170 |
QFileSystemModel *sourceModel = qobject_cast<QFileSystemModel*>( itemModel );
|
|
171 |
if( sourceModel ) {
|
|
172 |
QModelIndex nameIndex = sourceModel->index(sourceRow, NameColumn, sourceParent);
|
|
173 |
QFileInfo fileInfo = sourceModel->fileInfo( nameIndex );
|
|
174 |
QString absoluteFilePath = fileInfo.absoluteFilePath();
|
|
175 |
if( FmUtils::isSystemFolder( absoluteFilePath ) ) {
|
|
176 |
return false;
|
|
177 |
}
|
|
178 |
}
|
|
179 |
return true;
|
|
180 |
}
|
|
181 |
|