File: TableEditor.pm

package info (click to toggle)
qt4-perl 4.8.4-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,636 kB
  • ctags: 8,100
  • sloc: perl: 42,963; cpp: 28,039; makefile: 160; xml: 98; sh: 4
file content (78 lines) | stat: -rw-r--r-- 2,350 bytes parent folder | download | duplicates (3)
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
package TableEditor;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtSql4;

# [0]
use QtCore4::isa qw( Qt::Dialog );
use QtCore4::slots
    submit => [];
# [0]

# [0]
sub NEW 
{
    my ($class, $tableName, $parent) = @_;
    $class->SUPER::NEW($parent);

    this->{model} = Qt::SqlTableModel(this);
    this->{model}->setTable($tableName);
    this->{model}->setEditStrategy(Qt::SqlTableModel::OnManualSubmit());
    this->{model}->select();

    this->{model}->setHeaderData(0, Qt::Horizontal(), Qt::Variant(Qt::String(this->tr('ID'))));
    this->{model}->setHeaderData(1, Qt::Horizontal(), Qt::Variant(Qt::String(this->tr('First name'))));
    this->{model}->setHeaderData(2, Qt::Horizontal(), Qt::Variant(Qt::String(this->tr('Last name'))));

# [0] //! [1]
    my $view = Qt::TableView();
    $view->setModel(this->{model});
# [1]

# [2]
    this->{submitButton} = Qt::PushButton(this->tr('Submit'));
    this->{submitButton}->setDefault(1);
    this->{revertButton} = Qt::PushButton(this->tr('&Revert'));
    this->{quitButton} = Qt::PushButton(this->tr('Quit'));

    this->{buttonBox} = Qt::DialogButtonBox(Qt::Vertical());
    this->{buttonBox}->addButton(this->{submitButton}, Qt::DialogButtonBox::ActionRole());
    this->{buttonBox}->addButton(this->{revertButton}, Qt::DialogButtonBox::ActionRole());
    this->{buttonBox}->addButton(this->{quitButton}, Qt::DialogButtonBox::RejectRole());
# [2]

# [3]
    this->connect(this->{submitButton}, SIGNAL 'clicked()', this, SLOT 'submit()');
    this->connect(this->{revertButton}, SIGNAL 'clicked()', this->{model}, SLOT 'revertAll()');
    this->connect(this->{quitButton}, SIGNAL 'clicked()', this, SLOT 'close()');
# [3]

# [4]
    my $mainLayout = Qt::HBoxLayout();
    $mainLayout->addWidget($view);
    $mainLayout->addWidget(this->{buttonBox});
    this->setLayout($mainLayout);

    this->setWindowTitle(this->tr('Cached Table'));
}
# [4]

# [5]
sub submit
{
    this->{model}->database()->transaction();
    if (this->{model}->submitAll()) {
        this->{model}->database()->commit();
    } else {
        this->{model}->database()->rollback();
        Qt::MessageBox::warning(this, this->tr('Cached Table'),
                     sprintf this->tr('The database reported an error: %s'),
                             this->{model}->lastError()->text());
    }
}
# [5]

1;