-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
DirTreeModel.cpp
1390 lines (1066 loc) · 32.2 KB
/
DirTreeModel.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File name: DirTreeModel.cpp
* Summary: Qt data model for directory tree
* License: GPL V2 - See file LICENSE for details.
*
* Author: Stefan Hundhammer <Stefan.Hundhammer@gmx.de>
*/
#include <QPalette>
#include "Qt4Compat.h"
#include "DirTreeModel.h"
#include "DirTree.h"
#include "DirInfo.h"
#include "FileInfoIterator.h"
#include "DataColumns.h"
#include "SelectionModel.h"
#include "Settings.h"
#include "SettingsHelpers.h"
#include "Logger.h"
#include "FormatUtil.h"
#include "Exception.h"
#include "DebugHelpers.h"
// Number of clusters up to which a file will be considered small and will also
// display the allocated size like (4k).
#define SMALL_FILE_CLUSTERS 2
// Used used percent below which a small file will also display the allocated size
// like (4k)
#define SMALL_FILE_SHOW_ALLOC_THRESHOLD 75
using namespace QDirStat;
DirTreeModel::DirTreeModel( QObject * parent ):
QAbstractItemModel( parent ),
_tree(0),
_selectionModel(0),
_readJobsCol( PercentBarCol ),
_updateTimerMillisec( 333 ),
_slowUpdateMillisec( 3000 ),
_slowUpdate( false ),
_sortCol( NameCol ),
_sortOrder( Qt::AscendingOrder ),
_removingRows( false )
{
createTree();
readSettings();
loadIcons();
_updateTimer.setInterval( _updateTimerMillisec );
connect( &_updateTimer, SIGNAL( timeout() ),
this, SLOT ( sendPendingUpdates() ) );
}
DirTreeModel::~DirTreeModel()
{
writeSettings();
if ( _tree )
delete _tree;
}
void DirTreeModel::readSettings()
{
Settings settings;
settings.beginGroup( "DirectoryTree" );
_tree->setCrossFilesystems( settings.value( "CrossFilesystems", false ).toBool() );
_useBoldForDominantItems = settings.value( "UseBoldForDominant", true ).toBool();
FileInfo::setIgnoreHardLinks( settings.value( "IgnoreHardLinks", false ).toBool() );
_treeIconDir = settings.value( "TreeIconDir" , ":/icons/tree-medium/" ).toString();
_updateTimerMillisec = settings.value( "UpdateTimerMillisec", 333 ).toInt();
_slowUpdateMillisec = settings.value( "SlowUpdateMillisec", 3000 ).toInt();
settings.endGroup();
if ( usingLightTheme() )
{
settings.beginGroup( "TreeTheme-light" );
_dirReadErrColor = readColorEntry( settings, "DirReadErrColor", QColor( Qt::red ) );
_subtreeReadErrColor = readColorEntry( settings, "SubtreeReadErrColor", QColor( 0xa0, 0x00, 0x00 ) );
settings.endGroup();
}
else // dark theme
{
settings.beginGroup( "TreeTheme-dark" );
_dirReadErrColor = readColorEntry( settings, "DirReadErrColor", QColor( Qt::red ) );
_subtreeReadErrColor = readColorEntry( settings, "SubtreeReadErrColor", QColor( Qt::yellow ) );
settings.endGroup();
}
}
void DirTreeModel::writeSettings()
{
Settings settings;
settings.beginGroup( "DirectoryTree" );
settings.setValue( "SlowUpdateMillisec", _slowUpdateMillisec );
settings.setDefaultValue( "CrossFilesystems", _tree ? _tree->crossFilesystems() : false );
settings.setDefaultValue( "UseBoldForDominant", _useBoldForDominantItems );
settings.setDefaultValue( "IgnoreHardLinks", FileInfo::ignoreHardLinks() );
settings.setDefaultValue( "TreeIconDir", _treeIconDir );
settings.setDefaultValue( "UpdateTimerMillisec", _updateTimerMillisec );
settings.endGroup();
settings.beginGroup( usingLightTheme() ? "TreeTheme-light" : "TreeTheme-dark" );
writeColorEntry( settings, "DirReadErrColor", _dirReadErrColor );
writeColorEntry( settings, "SubtreeReadErrColor", _subtreeReadErrColor );
settings.endGroup();
}
bool DirTreeModel::usingDarkTheme()
{
QColor background = qAppPalette().color( QPalette::Active, QPalette::Base );
return background.lightness() < 128; // 0 (black) .. 255 (white)
}
void DirTreeModel::setSlowUpdate( bool slow )
{
_slowUpdate = slow;
_updateTimer.setInterval( _slowUpdate ? _slowUpdateMillisec : _updateTimerMillisec );
if ( slow )
logInfo() << "Display update every " << _updateTimer.interval() << " millisec" << endl;
}
void DirTreeModel::createTree()
{
_tree = new DirTree();
CHECK_NEW( _tree );
connect( _tree, SIGNAL( startingReading() ),
this, SLOT ( busyDisplay() ) );
connect( _tree, SIGNAL( finished() ),
this, SLOT ( readingFinished() ) );
connect( _tree, SIGNAL( aborted() ),
this, SLOT ( readingFinished() ) );
connect( _tree, SIGNAL( readJobFinished( DirInfo * ) ),
this, SLOT ( readJobFinished( DirInfo * ) ) );
connect( _tree, SIGNAL( deletingChild( FileInfo * ) ),
this, SLOT ( deletingChild( FileInfo * ) ) );
connect( _tree, SIGNAL( clearingSubtree( DirInfo * ) ),
this, SLOT ( clearingSubtree( DirInfo * ) ) );
connect( _tree, SIGNAL( subtreeCleared( DirInfo * ) ),
this, SLOT ( subtreeCleared( DirInfo * ) ) );
connect( _tree, SIGNAL( childDeleted() ),
this, SLOT ( childDeleted() ) );
}
void DirTreeModel::clear()
{
if ( _tree )
{
beginResetModel();
// logDebug() << "After beginResetModel()" << endl;
// dumpPersistentIndexList();
_tree->clear();
endResetModel();
// logDebug() << "After endResetModel()" << endl;
// dumpPersistentIndexList();
}
}
void DirTreeModel::openUrl( const QString & url )
{
CHECK_PTR( _tree );
if ( _tree->root() && _tree->root()->hasChildren() )
clear();
_updateTimer.start();
_tree->startReading( url );
}
void DirTreeModel::readPkg( const PkgFilter & pkgFilter )
{
// logDebug() << "Reading " << pkgFilter << endl;
CHECK_PTR( _tree );
if ( _tree->root() && _tree->root()->hasChildren() )
clear();
_updateTimer.start();
_tree->readPkg( pkgFilter );
}
void DirTreeModel::loadIcons()
{
if ( _treeIconDir.isEmpty() )
{
logWarning() << "No tree icons" << endl;
return;
}
if ( ! _treeIconDir.endsWith( "/" ) )
_treeIconDir += "/";
_dirIcon = QIcon( _treeIconDir + "dir.png" );
_dotEntryIcon = QIcon( _treeIconDir + "dot-entry.png" );
_fileIcon = QIcon( _treeIconDir + "file.png" );
_symlinkIcon = QIcon( _treeIconDir + "symlink.png" );
_unreadableDirIcon = QIcon( _treeIconDir + "unreadable-dir.png" );
_mountPointIcon = QIcon( _treeIconDir + "mount-point.png" );
_stopIcon = QIcon( _treeIconDir + "stop.png" );
_excludedIcon = QIcon( _treeIconDir + "excluded.png" );
_blockDeviceIcon = QIcon( _treeIconDir + "block-device.png" );
_charDeviceIcon = QIcon( _treeIconDir + "char-device.png" );
_specialIcon = QIcon( _treeIconDir + "special.png" );
_pkgIcon = QIcon( _treeIconDir + "folder-pkg.png" );
_atticIcon = _dirIcon;
}
void DirTreeModel::setColumns( const DataColumnList & columns )
{
beginResetModel();
DataColumns::instance()->setColumns( columns );
endResetModel();
}
FileInfo * DirTreeModel::findChild( DirInfo * parent, int childNo ) const
{
CHECK_PTR( parent );
const FileInfoList & childrenList =
parent->sortedChildren( _sortCol, _sortOrder,
true ); // includeAttic
if ( childNo < 0 || childNo >= childrenList.size() )
{
logError() << "Child #" << childNo << " is out of range: 0.."
<< childrenList.size()-1 << " children for "
<< parent << endl;
Debug::dumpChildrenList( parent, childrenList );
return 0;
}
// Debug::dumpChildrenList( parent, childrenList );
return childrenList.at( childNo );
}
int DirTreeModel::rowNumber( FileInfo * child ) const
{
if ( ! child->parent() )
return 0;
const FileInfoList & childrenList =
child->parent()->sortedChildren( _sortCol, _sortOrder,
true ); // includeAttic
int row = childrenList.indexOf( child );
if ( row < 0 )
{
// Not found
logError() << "Child " << child
<< " (" << (void *) child << ")"
<< " not found in \""
<< child->parent() << "\"" << endl;
Debug::dumpDirectChildren( child->parent() );
}
return row;
}
FileInfo * DirTreeModel::itemFromIndex( const QModelIndex & index )
{
FileInfo * item = 0;
if ( index.isValid() )
{
item = static_cast<FileInfo *>( index.internalPointer() );
CHECK_MAGIC( item );
}
return item;
}
//
// Reimplemented from QAbstractItemModel
//
int DirTreeModel::rowCount( const QModelIndex & parentIndex ) const
{
if ( ! _tree )
return 0;
int count = 0;
FileInfo * item = 0;
if ( parentIndex.isValid() )
{
item = static_cast<FileInfo *>( parentIndex.internalPointer() );
CHECK_MAGIC( item );
}
else
item = _tree->root();
if ( ! item->isDirInfo() )
return 0;
if ( item->toDirInfo()->isLocked() )
{
// logDebug() << item << " is locked - returning 0" << endl;
return 0;
}
switch ( item->readState() )
{
case DirQueued:
count = 0; // Nothing yet
break;
case DirReading:
// Don't mess with directories that are currently being read: If we
// tell our view about them, the view might begin fetching model
// indexes for them, and when the tree later sends the
// readJobFinished() signal, the beginInsertRows() call in our
// readJobFinished() slot will confuse the view; it would assume
// that the number of children reported in that beginInsertRows()
// call needs to be added to the number reported here. We'd have to
// keep track how many children we already reported, and how many
// new ones to report later.
//
// Better keep it simple: Don't report any children until they
// are complete.
count = 0;
break;
case DirError:
case DirPermissionDenied:
// This is a hybrid case: Depending on the dir reader, the dir may
// or may not be finished at this time. For a local dir, it most
// likely is; for a cache reader, there might be more to come.
if ( _tree->isBusy() )
count = 0;
else
count = directChildrenCount( item );
break;
case DirFinished:
case DirOnRequestOnly:
case DirCached:
case DirAborted:
count = directChildrenCount( item );
break;
// intentionally omitting 'default' case so the compiler can report
// missing enum values
}
// logDebug() << dirName << ": " << count << endl;
return count;
}
int DirTreeModel::columnCount( const QModelIndex & parent ) const
{
Q_UNUSED( parent );
return DataColumns::instance()->colCount();
}
QVariant DirTreeModel::data( const QModelIndex & index, int role ) const
{
if ( ! index.isValid() )
return QVariant();
DataColumn col = DataColumns::fromViewCol( index.column() );
FileInfo * item = static_cast<FileInfo *>( index.internalPointer() );
CHECK_MAGIC( item );
switch ( role )
{
case Qt::DisplayRole: // Text
{
QVariant result = columnText( item, col );
if ( item && item->isDirInfo() )
{
// logDebug() << "Touching " << col << "\tof " << item << endl;
item->toDirInfo()->touch();
}
return result;
}
case Qt::ForegroundRole: // Text color
{
if ( item->isIgnored() || item->isAttic() )
return qAppPalette().brush( QPalette::Disabled, QPalette::Foreground );
if ( item->isDir() )
{
if ( item->readError() )
return _dirReadErrColor;
if ( item->errSubDirCount() > 0 )
return _subtreeReadErrColor;
}
return QVariant();
}
case Qt::DecorationRole: // Icon
return columnIcon( item, col );
case Qt::FontRole:
return columnFont( item, col );
case Qt::TextAlignmentRole:
return columnAlignment( item, col );
case RawDataRole: // Send raw data to our item delegate (the PercentBarDelegate)
return columnRawData( item, col );
default:
return QVariant();
}
/*NOTREACHED*/
return QVariant();
}
QVariant DirTreeModel::headerData( int section,
Qt::Orientation orientation,
int role ) const
{
if ( orientation != Qt::Horizontal )
return QVariant();
switch ( role )
{
case Qt::DisplayRole:
switch ( DataColumns::fromViewCol( section ) )
{
case NameCol: return tr( "Name" );
case PercentBarCol: return tr( "Subtree Percentage" );
case PercentNumCol: return tr( "%" );
case SizeCol: return tr( "Size" );
case TotalItemsCol: return tr( "Items" );
case TotalFilesCol: return tr( "Files" );
case TotalSubDirsCol: return tr( "Subdirs" );
case LatestMTimeCol: return tr( "Last Modified" );
case OldestFileMTimeCol: return tr( "Oldest File" );
case UserCol: return tr( "User" );
case GroupCol: return tr( "Group" );
case PermissionsCol: return tr( "Permissions" );
case OctalPermissionsCol: return tr( "Perm." );
default: return QVariant();
}
case Qt::TextAlignmentRole:
switch ( DataColumns::fromViewCol( section ) )
{
case PercentBarCol:
case PercentNumCol:
case SizeCol:
case TotalItemsCol:
case TotalFilesCol:
case TotalSubDirsCol:
case LatestMTimeCol:
case OldestFileMTimeCol:
case PermissionsCol:
case OctalPermissionsCol: return Qt::AlignHCenter;
default: return Qt::AlignLeft;
}
default:
return QVariant();
}
}
Qt::ItemFlags DirTreeModel::flags( const QModelIndex & index ) const
{
if ( ! index.isValid() )
return Qt::NoItemFlags;
FileInfo * item = static_cast<FileInfo *>( index.internalPointer() );
CHECK_MAGIC( item );
Qt::ItemFlags baseFlags = Qt::ItemIsEnabled;
#if (QT_VERSION >= QT_VERSION_CHECK( 5, 1, 0 ))
if ( ! item->isDirInfo() )
baseFlags |= Qt::ItemNeverHasChildren;
#endif
// logDebug() << "Flags for " << index << endl;
DataColumn col = DataColumns::fromViewCol( index.column() );
switch ( col )
{
case PercentBarCol:
return baseFlags;
default:
return baseFlags | Qt::ItemIsSelectable;
}
}
QModelIndex DirTreeModel::index( int row, int column, const QModelIndex & parentIndex ) const
{
if ( ! _tree || ! _tree->root() || ! hasIndex( row, column, parentIndex ) )
return QModelIndex();
FileInfo *parent;
if ( parentIndex.isValid() )
{
parent = static_cast<FileInfo *>( parentIndex.internalPointer() );
CHECK_MAGIC( parent );
}
else
parent = _tree->root();
if ( parent->isDirInfo() )
{
FileInfo * child = findChild( parent->toDirInfo(), row );
CHECK_PTR( child );
if ( child )
return createIndex( row, column, child );
}
return QModelIndex();
}
QModelIndex DirTreeModel::parent( const QModelIndex & index ) const
{
if ( ! index.isValid() )
return QModelIndex();
FileInfo * child = static_cast<FileInfo*>( index.internalPointer() );
if ( ! child || ! child->checkMagicNumber() )
return QModelIndex();
FileInfo * parent = child->parent();
if ( ! parent || parent == _tree->root() )
return QModelIndex();
int row = rowNumber( parent );
// logDebug() << "Parent of " << child << " is " << parent << " #" << row << endl;
return createIndex( row, 0, parent );
}
void DirTreeModel::sort( int column, Qt::SortOrder order )
{
if ( column == _sortCol && order == _sortOrder )
return;
logDebug() << "Sorting by " << static_cast<DataColumn>( column )
<< ( order == Qt::AscendingOrder ? " ascending" : " descending" )
<< endl;
// logDebug() << "Before layoutAboutToBeChanged()" << endl;
// dumpPersistentIndexList();
emit layoutAboutToBeChanged();
_sortCol = DataColumns::fromViewCol( column );
_sortOrder = order;
updatePersistentIndexes();
emit layoutChanged();
// logDebug() << "After layoutChanged()" << endl;
// dumpPersistentIndexList();
}
//---------------------------------------------------------------------------
void DirTreeModel::busyDisplay()
{
emit layoutAboutToBeChanged();
_sortCol = NameCol;
// logDebug() << "Sorting by " << _sortCol << " during reading" << endl;
updatePersistentIndexes();
emit layoutChanged();
}
void DirTreeModel::idleDisplay()
{
emit layoutAboutToBeChanged();
_sortCol = PercentNumCol;
// logDebug() << "Sorting by " << _sortCol << " after reading is finished" << endl;
updatePersistentIndexes();
emit layoutChanged();
}
QModelIndex DirTreeModel::modelIndex( FileInfo * item, int column ) const
{
CHECK_PTR( _tree );
CHECK_PTR( _tree->root() );
if ( ! item || ! item->checkMagicNumber() || item == _tree->root() )
return QModelIndex();
else
{
int row = rowNumber( item );
// logDebug() << item << " is row #" << row << " of " << item->parent() << endl;
return row < 0 ? QModelIndex() : createIndex( row, column, item );
}
}
QVariant DirTreeModel::columnText( FileInfo * item, int col ) const
{
CHECK_PTR( item );
if ( col == _readJobsCol && item->isBusy() )
return tr( "[%1 Read Jobs]" ).arg( item->pendingReadJobs() );
bool limitedInfo = item->isPseudoDir() || item->isPkgInfo();
if ( item->isAttic() && col == PercentNumCol )
return QVariant();
if ( item->isPkgInfo() &&
item->readState() == DirAborted &&
! item->firstChild() &&
col != NameCol )
{
return "?";
}
switch ( col )
{
case NameCol: return item->name();
case PercentBarCol: return item->isExcluded() ? tr( "[Excluded]" ) : QVariant();
case PercentNumCol: return item == _tree->firstToplevel() ? QVariant() : formatPercent( item->subtreeAllocatedPercent() );
case SizeCol: return sizeColText( item );
case LatestMTimeCol: return QString( " " ) + formatTime( item->latestMtime() );
case UserCol: return limitedInfo ? QVariant() : item->userName();
case GroupCol: return limitedInfo ? QVariant() : item->groupName();
case PermissionsCol: return limitedInfo ? QVariant() : item->symbolicPermissions();
case OctalPermissionsCol: return limitedInfo ? QVariant() : item->octalPermissions();
}
if ( item->isDirInfo() )
{
if ( item->readError() )
{
switch ( col )
{
case TotalItemsCol:
case TotalFilesCol:
case TotalSubDirsCol:
return "?";
default:
break;
}
}
QString prefix = item->sizePrefix();
switch ( col )
{
case TotalItemsCol: return prefix + QString( "%1" ).arg( item->totalItems() );
case TotalFilesCol: return prefix + QString( "%1" ).arg( item->totalFiles() );
case TotalSubDirsCol:
if ( item->isDotEntry() )
return QVariant();
else
return prefix + QString( "%1" ).arg( item->totalSubDirs() );
case OldestFileMTimeCol: return QString( " " ) + formatTime( item->oldestFileMtime() );
}
}
return QVariant();
}
QVariant DirTreeModel::columnAlignment( FileInfo * item, int col ) const
{
Q_UNUSED( item );
int alignment = Qt::AlignVCenter;
switch ( col )
{
case PercentBarCol:
case PercentNumCol:
case SizeCol:
case TotalItemsCol:
case TotalFilesCol:
case TotalSubDirsCol:
case OctalPermissionsCol:
alignment |= Qt::AlignRight;
break;
case NameCol:
case LatestMTimeCol:
case OldestFileMTimeCol:
case UserCol:
case GroupCol:
default:
alignment |= Qt::AlignLeft;
break;
case PermissionsCol:
alignment |= Qt::AlignHCenter;
break;
}
return alignment;
}
QVariant DirTreeModel::columnFont( FileInfo * item, int col ) const
{
if ( _useBoldForDominantItems && item && item->isDominant() )
return dominantItemColumnFont( item, col );
else
return QVariant();
}
QVariant DirTreeModel::dominantItemColumnFont( FileInfo * item, int col ) const
{
Q_UNUSED( item );
switch ( _sortCol )
{
// Only if sorting by size or percent
case PercentBarCol:
case PercentNumCol:
case SizeCol:
break;
default:
return QVariant();
}
if ( _sortOrder != Qt::DescendingOrder )
return QVariant();
switch ( col )
{
case NameCol:
case PercentNumCol:
case SizeCol:
// Notice that the SizeColDelegate will override this
// for tiny files or symlinks for the size column
return _boldItemFont;
break;
default:
break;
}
return QVariant();
}
QVariant DirTreeModel::columnRawData( FileInfo * item, int col ) const
{
switch ( col )
{
case NameCol: return item->name();
case PercentBarCol:
{
if ( ( item->parent() && item->parent()->isBusy() ) ||
item == _tree->firstToplevel() ||
item->isAttic() )
{
return -1.0;
}
else
{
return item->subtreeAllocatedPercent();
}
}
case PercentNumCol: return item->subtreeAllocatedPercent();
case SizeCol: return item->totalSize();
case TotalItemsCol: return item->totalItems();
case TotalFilesCol: return item->totalFiles();
case TotalSubDirsCol: return item->totalSubDirs();
case LatestMTimeCol: return (qulonglong) item->latestMtime();
case OldestFileMTimeCol: return (qulonglong) item->oldestFileMtime();
case UserCol: return item->uid();
case GroupCol: return item->gid();
case PermissionsCol: return item->mode();
case OctalPermissionsCol: return item->mode();
default: return QVariant();
}
}
int DirTreeModel::directChildrenCount( FileInfo * subtree ) const
{
if ( ! subtree )
return 0;
int count = subtree->directChildrenCount();
if ( subtree->attic() )
++count;
return count;
}
QString DirTreeModel::sizeText( FileInfo * item, QString (*fmtSz)(FileSize) )
{
if ( ! item->isFile() )
return "";
QString text;
if ( item->links() > 1 ) // Multiple hard links
{
if ( item->isSparseFile() )
{
text = tr( "%1 / %2 Links (allocated: %3)" )
.arg( fmtSz( item->rawByteSize() ) )
.arg( item->links() )
.arg( fmtSz( item->rawAllocatedSize() ) );
}
else
{
text = tr( "%1 / %2 Links" )
.arg( fmtSz( item->rawByteSize() ) )
.arg( item->links() );
}
}
else // No multiple hard links
{
if ( item->isSparseFile() )
{
text = tr( "%1 (allocated: %2)" )
.arg( fmtSz( item->rawByteSize() ) )
.arg( fmtSz( item->rawAllocatedSize() ) );
}
}
return text;
}
QString DirTreeModel::smallSizeText( FileInfo * item )
{
if ( ! item->isFile() && ! item->isSymLink() )
return "";
FileSize allocated = item->allocatedSize();
FileSize size = item->size();
QString text;
if ( allocated >= 1024 ) // at least 1k so the (?k) makes sense
{
if ( allocated % 1024 == 0 && // if it's really even kB
allocated < 1024 * 1024 ) // and below 1 MB
// && item->usedPercent() < SMALL_FILE_SHOW_ALLOC_THRESHOLD &&
{
if ( size < 1024 )
{
text = QString( "%1 B (%2k)" )
.arg( size )
.arg( allocated / 1024 );
}
else
{
text = QString( "%1 (%2k)" )
.arg( formatSize( size ) )
.arg( allocated / 1024 );
}
}
}
if ( text.isEmpty() )
return formatSize( size );
return text;
}
bool DirTreeModel::isSmallFileOrSymLink( FileInfo * item )
{
if ( item &&
( item->isFile() || item->isSymLink() ) &&
item->blocks() > 0 &&
! item->isSparseFile() &&
item->tree() )
{
FileSize clusterSize = item->tree()->clusterSize();
if ( clusterSize > 0 )
{
if ( item->allocatedSize() <= clusterSize * SMALL_FILE_CLUSTERS )
return true;
if ( item->allocatedSize() <= clusterSize * ( SMALL_FILE_CLUSTERS + 1 ) )
{
FileSize unused = item->allocatedSize() - item->rawByteSize();
// 'unused' might be negative for sparse files, but the check
// will still be successful.
if ( unused > clusterSize / 2 )
return true;
}
}
}
return false;
}
QVariant DirTreeModel::sizeColText( FileInfo * item ) const
{
if ( item->isDevice() )
return QVariant();
QString leftMargin( 2, ' ' );
if ( item->isDirInfo() )
return leftMargin + item->sizePrefix() + formatSize( item->totalAllocatedSize() );
QString text = sizeText( item );
if ( text.isEmpty() && isSmallFileOrSymLink( item ) )
text = smallSizeText( item );
if ( text.isEmpty() )
text = leftMargin + formatSize( item->size() );
return text;
}
QVariant DirTreeModel::columnIcon( FileInfo * item, int col ) const