File: MainWindow.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 (102 lines) | stat: -rw-r--r-- 2,800 bytes parent folder | download
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
package MainWindow;

use strict;
use warnings;
use blib;

use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::MainWindow );
use AddressWidget;

use QtCore4::slots
    updateActions => ['QItemSelection'],
    openFile      => [],
    saveFile      => [];

sub NEW {
    shift->SUPER::NEW(@_);
    my $addressWidget = AddressWidget( undef );
    this->{addressWidget} = $addressWidget;
    this->setCentralWidget($addressWidget);
    createMenus();
    this->setWindowTitle(this->tr('Address Book'));
}

sub createMenus {
    my $addressWidget = this->{addressWidget};
    my $fileMenu = this->menuBar()->addMenu(this->tr("&File"));
    
    my $openAct = Qt::Action(this->tr("&Open..."), this);
    $fileMenu->addAction($openAct);
    this->connect($openAct, SIGNAL 'triggered()',
        this, SLOT 'openFile()');

    my $saveAct = Qt::Action(this->tr("&Save As..."), this);
    $fileMenu->addAction($saveAct);
    this->connect($saveAct, SIGNAL 'triggered()',
        this, SLOT 'saveFile()');

    $fileMenu->addSeparator();

    my $exitAct = Qt::Action(this->tr("E&xit"), this);
    $fileMenu->addAction($exitAct);
    this->connect($exitAct, SIGNAL 'triggered()',
        this, SLOT 'close()');

    my $toolMenu = this->menuBar()->addMenu(this->tr("&Tools"));

    my $addAct = Qt::Action(this->tr("&Add Entry..."), this);
    $toolMenu->addAction($addAct);
    this->connect($addAct, SIGNAL 'triggered()',
        $addressWidget, SLOT 'addEntry()');
    

    my $editAct = Qt::Action(this->tr("&Edit Entry..."), this);
    this->{editAct} = $editAct;
    $editAct->setEnabled(0);
    $toolMenu->addAction($editAct);
    this->connect($editAct, SIGNAL 'triggered()',
        $addressWidget, SLOT 'editEntry()');

    $toolMenu->addSeparator();

    my $removeAct = Qt::Action(this->tr("&Remove Entry"), this);
    this->{removeAct} = $removeAct;
    $removeAct->setEnabled(0);
    $toolMenu->addAction($removeAct);
    this->connect($removeAct, SIGNAL 'triggered()',
        $addressWidget, SLOT 'removeEntry()');

    this->connect($addressWidget, SIGNAL 'selectionChanged(QItemSelection)',
        this, SLOT 'updateActions(QItemSelection)');
}

sub openFile {
    my $fileName = Qt::FileDialog::getOpenFileName(this);
    if ($fileName) {
        this->{addressWidget}->readFromFile($fileName);
    }
}

sub saveFile {
    my $fileName = Qt::FileDialog::getSaveFileName(this);
    if ($fileName) {
        this->{addressWidget}->writeToFile($fileName);
    }
}

sub updateActions {
    my ($selection) = @_;
    my $indexes = $selection->indexes();

    if ( ref $indexes eq 'ARRAY' && @{$indexes} ) {
        this->{removeAct}->setEnabled(1);
        this->{editAct}->setEnabled(1);
    } else {
        this->{removeAct}->setEnabled(0);
        this->{editAct}->setEnabled(0);
    }
}

1;