NepomukDaemons
folderselectionmodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "folderselectionmodel.h"
00027
00028 #include <QtCore/QDir>
00029 #include <QtCore/QFileInfo>
00030 #include <QtGui/QColor>
00031 #include <QtGui/QBrush>
00032 #include <QtGui/QPalette>
00033
00034 #include <KDebug>
00035 #include <KIcon>
00036 #include <KLocale>
00037
00038
00039 FolderSelectionModel::FolderSelectionModel( QObject* parent )
00040 : QFileSystemModel( parent )
00041 {
00042 setFilter( QDir::AllDirs | QDir::NoDotAndDotDot );
00043 }
00044
00045
00046 FolderSelectionModel::~FolderSelectionModel()
00047 {
00048 }
00049
00050
00051 Qt::ItemFlags FolderSelectionModel::flags( const QModelIndex &index ) const
00052 {
00053 Qt::ItemFlags flags = QFileSystemModel::flags( index );
00054 flags |= Qt::ItemIsUserCheckable;
00055 if( isForbiddenPath( filePath( index ) ) )
00056 flags ^= Qt::ItemIsEnabled;
00057 return flags;
00058 }
00059
00060
00061 QVariant FolderSelectionModel::data( const QModelIndex& index, int role ) const
00062 {
00063 if( index.isValid() && index.column() == 0 ) {
00064 if( role == Qt::CheckStateRole ) {
00065 const IncludeState state = includeState( index );
00066 switch( state ) {
00067 case StateNone:
00068 case StateExclude:
00069 case StateExcludeInherited:
00070 return Qt::Unchecked;
00071 case StateInclude:
00072 case StateIncludeInherited:
00073 return Qt::Checked;
00074 }
00075 }
00076 else if( role == IncludeStateRole ) {
00077 return includeState( index );
00078 }
00079 else if( role == Qt::ForegroundRole ) {
00080 IncludeState state = includeState( index );
00081 QBrush brush = QFileSystemModel::data( index, role ).value<QBrush>();
00082 switch( state ) {
00083 case StateInclude:
00084 case StateIncludeInherited:
00085
00086 break;
00087 case StateNone:
00088 case StateExclude:
00089 case StateExcludeInherited:
00090 brush = QPalette().brush( QPalette::Disabled, QPalette::Foreground );
00091 break;
00092 }
00093 return QVariant::fromValue( brush );
00094 }
00095 else if ( role == Qt::ToolTipRole ) {
00096 IncludeState state = includeState( index );
00097 if ( state == StateInclude || state == StateIncludeInherited ) {
00098 return i18nc( "@info:tooltip %1 is the path of the folder in a listview",
00099 "<filename>%1</filename><nl/>(will be indexed for desktop search)", filePath( index ) );
00100 }
00101 else {
00102 return i18nc( "@info:tooltip %1 is the path of the folder in a listview",
00103 "<filename>%1</filename><nl/> (will <emphasis>not</emphasis> be indexed for desktop search)", filePath( index ) );
00104 }
00105 }
00106 else if ( role == Qt::DecorationRole ) {
00107 if ( filePath( index ) == QDir::homePath() ) {
00108 return KIcon( "user-home" );
00109 }
00110 }
00111 }
00112
00113 return QFileSystemModel::data( index, role );
00114 }
00115
00116
00117 bool FolderSelectionModel::setData( const QModelIndex& index, const QVariant& value, int role )
00118 {
00119 if( index.isValid() && index.column() == 0 && role == Qt::CheckStateRole ) {
00120 const QString path = filePath( index );
00121 const IncludeState state = includeState( path );
00122
00123
00124
00125 if( state == StateInclude ||
00126 state == StateIncludeInherited ) {
00127 excludePath( path );
00128 return true;
00129 }
00130 else {
00131 includePath( path );
00132 return true;
00133 }
00134 return false;
00135 }
00136
00137 return QFileSystemModel::setData( index, value, role );
00138 }
00139
00140
00141 namespace {
00142 void removeSubDirs( const QString& path, QSet<QString>& set ) {
00143 QSet<QString>::iterator it = set.begin();
00144 while( it != set.end() ) {
00145 if( it->startsWith( path ) )
00146 it = set.erase( it );
00147 else
00148 ++it;
00149 }
00150 }
00151
00152 QModelIndex findLastLeaf( const QModelIndex& index, FolderSelectionModel* model ) {
00153 int rows = model->rowCount( index );
00154 if( rows > 0 ) {
00155 return findLastLeaf( model->index( rows-1, 0, index ), model );
00156 }
00157 else {
00158 return index;
00159 }
00160 }
00161 }
00162
00163 void FolderSelectionModel::includePath( const QString& path )
00164 {
00165 if( !m_included.contains( path ) ) {
00166
00167 removeSubDirs( path, m_included );
00168 removeSubDirs( path, m_excluded );
00169 m_excluded.remove( path );
00170
00171
00172 if( includeState( path ) != StateIncludeInherited ) {
00173 m_included.insert( path );
00174 }
00175 emit dataChanged( index( path ), findLastLeaf( index( path ), this ) );
00176 }
00177 }
00178
00179
00180 void FolderSelectionModel::excludePath( const QString& path )
00181 {
00182 if( !m_excluded.contains( path ) ) {
00183
00184 removeSubDirs( path, m_included );
00185 removeSubDirs( path, m_excluded );
00186 m_included.remove( path );
00187
00188
00189 if( includeState( path ) == StateIncludeInherited ) {
00190 m_excluded.insert( path );
00191 }
00192 emit dataChanged( index( path ), findLastLeaf( index( path ), this ) );
00193 }
00194 }
00195
00196
00197 void FolderSelectionModel::setFolders( const QStringList& includeDirs, const QStringList& excludeDirs )
00198 {
00199 m_included = QSet<QString>::fromList( includeDirs );
00200 m_excluded = QSet<QString>::fromList( excludeDirs );
00201 reset();
00202 }
00203
00204
00205 QStringList FolderSelectionModel::includeFolders() const
00206 {
00207 return m_included.toList();
00208 }
00209
00210
00211 QStringList FolderSelectionModel::excludeFolders() const
00212 {
00213 return m_excluded.toList();
00214 }
00215
00216
00217 inline bool FolderSelectionModel::isForbiddenPath( const QString& path ) const
00218 {
00219
00220 QString _path = path.endsWith( "/" ) ? path : path + "/";
00221 QFileInfo fi( _path );
00222 return( _path.startsWith( "/proc/" ) ||
00223 _path.startsWith( "/dev/" ) ||
00224 _path.startsWith( "/sys/" ) ||
00225 !fi.isReadable() ||
00226 !fi.isExecutable() );
00227 }
00228
00229
00230 FolderSelectionModel::IncludeState FolderSelectionModel::includeState( const QModelIndex& index ) const
00231 {
00232 return includeState( filePath( index ) );
00233 }
00234
00235
00236 FolderSelectionModel::IncludeState FolderSelectionModel::includeState( const QString& path ) const
00237 {
00238 if( m_included.contains( path ) ) {
00239 return StateInclude;
00240 }
00241 else if( m_excluded.contains( path ) ) {
00242 return StateExclude;
00243 }
00244 else {
00245 QString parent = path.section( QDir::separator(), 0, -2, QString::SectionSkipEmpty|QString::SectionIncludeLeadingSep );
00246 if( parent.isEmpty() ) {
00247 return StateNone;
00248 }
00249 else {
00250 IncludeState state = includeState( parent );
00251 if( state == StateNone )
00252 return StateNone;
00253 else if( state == StateInclude || state == StateIncludeInherited )
00254 return StateIncludeInherited;
00255 else
00256 return StateExcludeInherited;
00257 }
00258 }
00259 }
00260
00261 #include "folderselectionmodel.moc"