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
|
package TableModel;
use strict;
use warnings;
use blib;
use QtCore4;
use QtGui4;
use QtCore4::isa qw(Qt::AbstractTableModel);
Qt::_internal::installsignal( 'Qt::AbstractTableModel::dataChanged' );
sub NEW {
my ( $class, $pairs, $parent ) = @_;
my $listOfPairs;
if ( ref $pairs eq 'ARRAY' && defined $parent ) {
$listOfPairs = $pairs;
}
else {
$listOfPairs = [];
$parent = $pairs;
}
$class->SUPER::NEW( $parent );
this->{listOfPairs} = $listOfPairs;
}
sub rowCount {
return scalar @{this->{listOfPairs}};
}
sub columnCount {
return 2;
}
sub data {
my ($index, $role) = @_;
my $listOfPairs = this->{listOfPairs};
if (!$index->isValid()) {
return Qt::Variant();
}
if ($index->row() >= scalar @{$listOfPairs} || $index->row() < 0) {
return Qt::Variant();
}
if ($role == Qt::DisplayRole()) {
my $pair = $listOfPairs->[$index->row()];
if ($index->column() == 0) {
return $pair->[0] ? Qt::Variant($pair->[0]) : Qt::Variant();
}
elsif ($index->column() == 1) {
return $pair->[1] ? Qt::Variant($pair->[1]) : Qt::Variant();
}
}
return Qt::Variant();
}
sub headerData {
my ($section, $orientation, $role) = @_;
if ($role != Qt::DisplayRole()) {
return Qt::Variant();
}
if ($orientation == Qt::Horizontal()) {
if ($section == 0) {
return Qt::Variant(Qt::String(this->tr("Name")));
}
elsif ($section == 1) {
return Qt::Variant(Qt::String(this->tr("Address")));
}
else {
return Qt::Variant();
}
}
return Qt::Variant();
}
sub insertRows {
my ($position, $rows, $index) = @_;
my $listOfPairs = this->{listOfPairs};
this->beginInsertRows(Qt::ModelIndex(), $position, $position+$rows-1);
foreach my $row (0..$rows-1) {
if( $position == 0 ) {
unshift @{$listOfPairs}, [ ' ', ' ' ];
}
elsif( $position == scalar @{$listOfPairs} ) {
push @{$listOfPairs}, [ ' ', ' ' ];
}
else {
#$listOfPairs->[$position] = [ ' ', ' ' ];
}
}
this->endInsertRows();
return 1;
}
sub removeRows {
my ($position, $rows, $index) = @_;
my $listOfPairs = this->{listOfPairs};
this->beginRemoveRows(Qt::ModelIndex(), $position, $position+$rows-1);
foreach my $row (0..$rows-1) {
splice( @{$listOfPairs}, $position, 1 );
}
this->endRemoveRows();
return 1;
}
sub setData {
my ($index, $value, $role) = @_;
my $listOfPairs = this->{listOfPairs};
if ($index->isValid() && $role == Qt::EditRole()) {
my $row = $index->row();
my $p = $listOfPairs->[$row];
if ($index->column() == 0) {
$p->[0] = $value;
}
elsif ($index->column() == 1) {
$p->[1] = $value;
}
else {
return 0;
}
$listOfPairs->[$row] = $p;
#Qt::_internal::setDebug(0xffffff);
emit dataChanged($index, $index);
#Qt::_internal::setDebug(0);
return 1;
}
return 0;
}
sub flags {
my ($index) = @_;
if (!$index->isValid()) {
return Qt::ItemIsEnabled();
}
return bless( \this->SUPER::flags($index), 'Qt::ItemFlag') | Qt::ItemIsEditable();
}
sub getList {
return this->{listOfPairs};
}
1;
|