1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "FileRecordModel.h"
#include "FileRecordBaseProperties.h"
#include "IconEngine.h"
#include "Singleton.h"
#include "XmlOptions.h"
#include <QUrl>
#include <algorithm>
//__________________________________________________________________
FileRecordModel::IconCache& FileRecordModel::_icons( void )
{
static IconCache cache;
return cache;
}
//__________________________________________________________________
FileRecordModel::FileRecordModel( QObject* parent ):
ListModel<FileRecord>( parent ),
Counter( "FileRecordModel" ),
iconPropertyId_( FileRecord::PropertyId::get( FileRecordProperties::Icon ) )
{
Debug::Throw("FileRecordModel::FileRecordModel.\n" );
columnTitles_ << tr( "File" );
columnTitles_ << tr( "Path" );
columnTitles_ << tr( "Last Accessed" );
connect( Singleton::get().application(), SIGNAL(configurationChanged()), SLOT(_updateConfiguration()) );
}
//__________________________________________________________________
int FileRecordModel::columnCount(const QModelIndex &parent) const
{ return columnTitles_.size(); }
//__________________________________________________________________
int FileRecordModel::findColumn( const QString& value ) const
{
for( int index = 0; index < columnTitles_.size(); ++index )
{ if( columnTitles_[index] == value ) return index; }
return -1;
}
//__________________________________________________________________
Qt::ItemFlags FileRecordModel::flags( const QModelIndex& index ) const
{
// default flags
Qt::ItemFlags flags;
if( !contains( index ) ) return flags;
// check associated record validity
const FileRecord& record( get(index) );
if( !record.isValid() ) return flags;
// default flags
flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if( index.column() == Filename ) flags |= Qt::ItemIsDragEnabled;
return flags;
}
//__________________________________________________________________
QVariant FileRecordModel::data( const QModelIndex& index, int role ) const
{
// check index
if( !contains( index ) ) return QVariant();
// retrieve associated file info
const FileRecord& record( get(index) );
// return text associated to file and column
if( role == Qt::DisplayRole ) {
switch( index.column() )
{
case Filename:
{
// store local nmae
const QString localName( useLocalNames_ ? record.file().localName(): record.file() );
// loop over previous rows to find a match and increment version number
unsigned int version( 0 );
for( int row = 0; row < index.row(); row++ )
{
const QString rowName( useLocalNames_ ?
get( this->index( row, Filename ) ).file().localName() :
get( this->index( row, Filename ) ).file() );
if( localName == rowName ) version++;
}
// form output string.
QString buffer;
QTextStream what( &buffer );
if( localName.isEmpty() ) what << "untitled";
else what << localName;
if( version ) what << " (" << version+1 << ")";
return buffer;
}
case Path: return record.file().path();
case Time: return TimeStamp( record.time() ).toString();
default:
if( index.column() < (int) columnTitles_.size() && record.hasProperty( columnTitles_[index.column()] ) )
{ return record.property( columnTitles_[index.column()] ); }
else return QVariant();
}
} else if( showIcons_ && role == Qt::DecorationRole && index.column() == Filename ) {
// icon
return record.hasProperty( iconPropertyId_ ) ? _icon( record.property( iconPropertyId_ ) ):_icon();
}
return QVariant();
}
//__________________________________________________________________
QVariant FileRecordModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(
orientation == Qt::Horizontal &&
role == Qt::DisplayRole &&
section >= 0 &&
section < (int) columnTitles_.size() )
{ return columnTitles_[section]; }
// return empty
return QVariant();
}
//______________________________________________________________________
QMimeData* FileRecordModel::mimeData(const QModelIndexList &indexes) const
{
// get selected filenames
QOrderedSet<QString> filenames;
foreach( const QModelIndex& index, indexes )
{
if( !index.isValid() ) continue;
const FileRecord record( get(index) );
filenames.insert( record.file() );
}
if( filenames.empty() ) return nullptr;
else {
QMimeData* mimeData = new QMimeData();
// fill text data
{
QString fullText;
QTextStream buffer( &fullText );
foreach( const QString& filename, filenames )
{ buffer << QString( "file://%1" ).arg(filename) << endl; }
mimeData->setText( fullText );
}
// fill url list
{
QList<QUrl> urlList;
foreach( const QString& filename, filenames )
{ urlList.append( QUrl( QString( "file://%1" ).arg(filename) ) ); }
mimeData->setUrls( urlList );
}
return mimeData;
}
}
//____________________________________________________________
void FileRecordModel::_sort( int column, Qt::SortOrder order )
{ std::sort( _get().begin(), _get().end(), SortFTor( column, order, columnTitles_ ) ); }
//____________________________________________________________
void FileRecordModel::_add( const ValueType& value )
{
_updateColumns( value );
ListModel<FileRecord>::_add( value );
}
//____________________________________________________________
void FileRecordModel::_updateConfiguration( void )
{
Debug::Throw( "FileRecordModel::_updateConfiguration.\n" );
_icons().clear();
}
//____________________________________________________________
void FileRecordModel::_updateColumns( const ValueType& value )
{
Debug::Throw( "FileRecordModel::_updateColumns.\n" );
// loop over available properties
const FileRecord::PropertyMap& properties( value.properties() );
for( FileRecord::PropertyMap::const_iterator iter = properties.begin(); iter != properties.end(); ++iter )
{
// look for property name in list of columns
if( std::find( columnTitles_.begin(), columnTitles_.end(), FileRecord::PropertyId::get( iter.key() ) ) == columnTitles_.end() )
{ columnTitles_ << FileRecord::PropertyId::get( iter.key() ); }
}
}
//________________________________________________________
bool FileRecordModel::SortFTor::operator () ( FileRecord first, FileRecord second ) const
{
if( order_ == Qt::DescendingOrder ) std::swap( first, second );
switch( type_ )
{
case Filename: return first.file().localName().compare( second.file().localName(), Qt::CaseInsensitive ) < 0;
case Path: return first.file().path() < second.file().path();
case Time: return (first.time() != second.time() ) ? (first.time() < second.time()):first.file().localName() < second.file().localName();
default:
{
if( type_ < columnTitles_.size() )
{
QString name( columnTitles_[type_] );
QString firstProperty( first.property( name ) );
QString secondProperty( second.property( name ) );
return ( firstProperty != secondProperty ) ? firstProperty < secondProperty : first.file().localName() < second.file().localName();
} else return false;
}
}
}
//________________________________________________________
const QIcon& FileRecordModel::_icon( const QString& name )
{
Debug::Throw( "FileRecordModel::_icon.\n" );
IconCache::const_iterator iter( _icons().find( name ) );
if( iter != _icons().end() ) return iter.value();
else return _icons().insert( name, IconEngine::get( name ) ).value();
}
|