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 (152 lines) | stat: -rw-r--r-- 4,911 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
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
package MainWindow;

use strict;
use warnings;
use blib;

use PieView;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::MainWindow );
use QtCore4::slots
    openFile => ['const Qt::String'],
    openFile => [],
    saveFile => [];

sub NEW {
    shift->SUPER::NEW();
    my $fileMenu = Qt::Menu(this->tr('&File'), this);
    my $openAction = $fileMenu->addAction(this->tr('&Open...'));
    $openAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+O')));
    my $saveAction = $fileMenu->addAction(this->tr('&Save As...'));
    $saveAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+S')));
    my $quitAction = $fileMenu->addAction(this->tr('E&xit'));
    $quitAction->setShortcut(Qt::KeySequence(this->tr('Ctrl+Q')));

    setupModel();
    setupViews();

    this->connect($openAction, SIGNAL 'triggered()', this, SLOT 'openFile()');
    this->connect($saveAction, SIGNAL 'triggered()', this, SLOT 'saveFile()');
    this->connect($quitAction, SIGNAL 'triggered()', Qt::qApp, SLOT 'quit()');

    this->menuBar()->addMenu($fileMenu);
    this->statusBar();

    openFile('qtdata.cht');

    this->setWindowTitle(this->tr('Chart'));
    this->resize(870, 550);
}

sub setupModel {
    my $model = Qt::StandardItemModel(8, 2, this);
    this->{model} = $model;
    $model->setHeaderData(0, Qt::Horizontal(), Qt::Variant(Qt::String(this->tr('Label'))));
    $model->setHeaderData(1, Qt::Horizontal(), Qt::Variant(Qt::String(this->tr('Quantity'))));
}

sub setupViews {
    my $splitter = Qt::Splitter();
    my $table = Qt::TableView();
    my $pieChart = PieView();
    my $model = this->{model};
    this->{pieChart} = $pieChart;
    $splitter->addWidget($table);
    $splitter->addWidget($pieChart);
    $splitter->setStretchFactor(0, 0);
    $splitter->setStretchFactor(1, 1);

    $table->setModel($model);
    $pieChart->setModel($model);

    my $selectionModel = Qt::ItemSelectionModel($model);
    this->{selectionModel} = $selectionModel;
    $table->setSelectionModel($selectionModel);
    $pieChart->setSelectionModel($selectionModel);

    my $headerView = $table->horizontalHeader();
    $headerView->setStretchLastSection(1);

    this->setCentralWidget($splitter);
}

sub openFile {
    my ($path) = @_;
    my $fileName;
    my $model = this->{model};
    if (!$path) {
        $fileName = Qt::FileDialog::getOpenFileName(this, this->tr('Choose a data file'),
                                                '', '*.cht');
    }
    else {
        $fileName = $path;
    }

    if ($fileName) {
        my $file = Qt::File($fileName);

        if ($file->open(Qt::File::ReadOnly() | Qt::File::Text())) {
            my $stream = Qt::TextStream($file);
            my $line;

            $model->removeRows(0, $model->rowCount(Qt::ModelIndex()), Qt::ModelIndex());

            my $row = 0;
            do {
                $line = $stream->readLine();
                if ($line) {

                    $model->insertRows($row, 1, Qt::ModelIndex());

                    my @pieces = grep { $_ } split /,/, $line;
                    $model->setData($model->index($row, 0, Qt::ModelIndex()),
                                   Qt::Variant(Qt::String($pieces[0])));
                    $model->setData($model->index($row, 1, Qt::ModelIndex()),
                                   Qt::Variant(Qt::String($pieces[1])));
                    $model->setData($model->index($row, 0, Qt::ModelIndex()),
                                   Qt::qVariantFromValue(Qt::Color(Qt::String($pieces[2]))), Qt::DecorationRole());
                    $row++;
                }
            } while ($line);

            $file->close();
            this->statusBar()->showMessage(this->tr("Loaded $fileName"), 2000);
        }
    }
}

sub saveFile {
    my $model = this->{model};
    my $fileName = Qt::FileDialog::getSaveFileName(this,
        this->tr('Save file as'), '', '*.cht');

    if ($fileName) {
        my $file = Qt::File($fileName);
        my $stream = Qt::TextStream($file);

        if ($file->open(Qt::File::WriteOnly() | Qt::File::Text())) {
            foreach my $row (0..$model->rowCount(Qt::ModelIndex())-1) {

                my @pieces;

                push @pieces, $model->data($model->index($row, 0, Qt::ModelIndex()),
                                          Qt::DisplayRole())->toString();
                push @pieces, $model->data($model->index($row, 1, Qt::ModelIndex()),
                                          Qt::DisplayRole())->toString();
                push @pieces, $model->data($model->index($row, 0, Qt::ModelIndex()),
                                          Qt::DecorationRole())->toString();

                {
                    no warnings qw(void);
                    $stream << join ( ',', @pieces ) . "\n";
                }
            }
        }

        $file->close();
        this->statusBar()->showMessage(this->tr("Saved $fileName"), 2000);
    }
}

1;