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
|
package FileListModel;
use strict;
use warnings;
use QtCore4;
use QtGui4;
#[0]
use QtCore4::isa qw( Qt::AbstractListModel );
use QtCore4::signals
numberPopulated => ['int'];
use List::Util qw(min);
use QtCore4::slots
setDirPath => ['const QString &'];
#Qt::StringList fileList;
#int fileCount;
#[0]
sub NEW {
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
}
#[4]
sub rowCount
{
return this->{fileCount};
}
sub data
{
my ($index, $role) = @_;
if (!$index->isValid()) {
return Qt::Variant();
}
if ($index->row() >= scalar @{this->{fileList}} || $index->row() < 0) {
return Qt::Variant();
}
if ($role == Qt::DisplayRole()) {
return Qt::Variant(this->{fileList}->[$index->row()]);
}
elsif ($role == Qt::BackgroundRole()) {
my $batch = ($index->row() / 100) % 2;
if ($batch == 0) {
return Qt::qVariantFromValue(qApp->palette()->base());
}
else {
return Qt::qVariantFromValue(qApp->palette()->alternateBase());
}
}
return Qt::Variant();
}
#[4]
#[1]
sub canFetchMore
{
if (this->{fileCount} < scalar @{this->{fileList}}) {
return 1;
}
else {
return 0;
}
}
#[1]
#[2]
sub fetchMore
{
my $remainder = scalar @{this->{fileList}} - this->{fileCount};
my $itemsToFetch = min(100, $remainder);
this->beginInsertRows(Qt::ModelIndex(), this->{fileCount}, this->{fileCount}+$itemsToFetch);
this->{fileCount} += $itemsToFetch;
this->endInsertRows();
emit this->numberPopulated($itemsToFetch);
}
#[2]
#[0]
sub setDirPath
{
my ($path) = @_;
my $dir = Qt::Dir($path);
this->{fileList} = $dir->entryList();
this->{fileCount} = 0;
this->reset();
}
#[0]
1;
|