File: AddressBook.pm

package info (click to toggle)
qt4-perl 4.5~~svn1145508-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,144 kB
  • ctags: 5,947
  • sloc: perl: 29,224; cpp: 18,849; xml: 98; makefile: 91; sh: 4
file content (132 lines) | stat: -rw-r--r-- 4,056 bytes parent folder | download | duplicates (4)
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
package AddressBook;

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

use QtCore4::isa qw( Qt::Widget );
# [slots]
use QtCore4::slots
    addContact => [],
    submitContact => [],
    cancel => [];
# [slots]

sub NEW
{
    my ($class, $parent) = @_;
    $class->SUPER::NEW($parent);
    my $nameLabel = Qt::Label(this->tr('Name:'));
    this->{nameLine} = Qt::LineEdit();
# [setting readonly 1]
    this->{nameLine}->setReadOnly(1);
# [setting readonly 1]
    my $addressLabel = Qt::Label(this->tr('Address:'));
    this->{addressText} = Qt::TextEdit();
# [setting readonly 2]    
    this->{addressText}->setReadOnly(1);
# [setting readonly 2]
   
# [pushbutton declaration]
    this->{addButton} = Qt::PushButton(this->tr('&Add'));
    this->{addButton}->show();
    this->{submitButton} = Qt::PushButton(this->tr('&Submit'));
    this->{submitButton}->hide();
    this->{cancelButton} = Qt::PushButton(this->tr('&Cancel'));
    this->{cancelButton}->hide();
# [pushbutton declaration]
# [connecting signals and slots]
    this->connect(this->{addButton}, SIGNAL 'clicked()', this, SLOT 'addContact()');
    this->connect(this->{submitButton}, SIGNAL 'clicked()', this, SLOT 'submitContact()');
    this->connect(this->{cancelButton}, SIGNAL 'clicked()', this, SLOT 'cancel()');
# [connecting signals and slots]
# [vertical layout]
    my $buttonLayout1 = Qt::VBoxLayout();
    $buttonLayout1->addWidget(this->{addButton}, Qt::AlignTop());
    $buttonLayout1->addWidget(this->{submitButton});
    $buttonLayout1->addWidget(this->{cancelButton});
    $buttonLayout1->addStretch();
# [vertical layout]
# [grid layout]
    my $mainLayout = Qt::GridLayout();
    $mainLayout->addWidget($nameLabel, 0, 0);
    $mainLayout->addWidget(this->{nameLine}, 0, 1);
    $mainLayout->addWidget($addressLabel, 1, 0, Qt::AlignTop());
    $mainLayout->addWidget(this->{addressText}, 1, 1);
    $mainLayout->addLayout($buttonLayout1, 1, 2);
# [grid layout]
    this->setLayout($mainLayout);
    this->setWindowTitle(this->tr('Simple Address Book'));
}
# [addContact]
sub addContact
{
    this->{oldName} = this->{nameLine}->text();
    this->{oldAddress} = this->{addressText}->toPlainText();

    this->{nameLine}->clear();
    this->{addressText}->clear();
    
    this->{nameLine}->setReadOnly(0);
    this->{nameLine}->setFocus(Qt::OtherFocusReason());
    this->{addressText}->setReadOnly(0);

    this->{addButton}->setEnabled(0);
    this->{submitButton}->show();
    this->{cancelButton}->show();
}
# [addContact]

# [submitContact part1]
sub submitContact
{
    my $name = this->{nameLine}->text();
    my $address = this->{addressText}->toPlainText();
    
    if ($name eq '' || $address eq '') {
        Qt::MessageBox::information(this, this->tr('Empty Field'),
            this->tr('Please enter a name and address.'));
        return;
    }
# [submitContact part1]
# [submitContact part2]
    if (!exists this->{contacts}->{$name}) {
        this->{contacts}->{$name} = $address;
        Qt::MessageBox::information(this, this->tr('Add Successful'),
            sprintf this->tr('\'%s\' has been added to your address book.'), $name);
    } else {
        Qt::MessageBox::information(this, this->tr('Add Unsuccessful'),
            sprintf this->tr('Sorry, \'%s\' is already in your address book.'), $name);
        return;
    }
# [submitContact part2]
# [submitContact part3]
    if (scalar keys %{this->{contacts}} == 0) {
        this->{nameLine}->clear();
        this->{addressText}->clear();
    }

    this->{nameLine}->setReadOnly(1);
    this->{addressText}->setReadOnly(1);
    this->{addButton}->setEnabled(1);
    this->{submitButton}->hide();
    this->{cancelButton}->hide();
}
# [submitContact part3]
# [cancel]
sub cancel
{
    this->{nameLine}->setText(this->{oldName});
    this->{nameLine}->setReadOnly(1);

    this->{addressText}->setText(this->{oldAddress});
    this->{addressText}->setReadOnly(1);

    this->{addButton}->setEnabled(1);
    this->{submitButton}->hide();
    this->{cancelButton}->hide();    
}
# [cancel]

1;