~kubuntu-members/perlkde/4.11

« back to all changes in this revision

Viewing changes to qt/qtgui/examples/tools/codecs/MainWindow.pm

  • Committer: Arno Rehn
  • Date: 2011-01-06 17:49:41 UTC
  • Revision ID: git-v1:ada9e4b459cf6fabd0f3b9b164387469cb19d9bc
A patch by Ian Monroe and myself to modularize the repository.
See README.MODULARIZATION for details.

svn path=/trunk/KDE/kdebindings/perl/; revision=1212365

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package MainWindow;
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
use QtCore4;
 
6
use QtGui4;
 
7
use QtCore4::isa qw( Qt::MainWindow );
 
8
use QtCore4::slots
 
9
    open => [],
 
10
    save => [],
 
11
    about => [],
 
12
    aboutToShowSaveAsMenu => [];
 
13
use PreviewForm;
 
14
 
 
15
sub textEdit() {
 
16
    return this->{textEdit};
 
17
}
 
18
 
 
19
sub previewForm() {
 
20
    return this->{previewForm};
 
21
}
 
22
 
 
23
sub codecs() {
 
24
    return this->{codecs};
 
25
}
 
26
 
 
27
sub fileMenu() {
 
28
    return this->{fileMenu};
 
29
}
 
30
 
 
31
sub helpMenu() {
 
32
    return this->{helpMenu};
 
33
}
 
34
 
 
35
sub saveAsMenu() {
 
36
    return this->{saveAsMenu};
 
37
}
 
38
 
 
39
sub openAct() {
 
40
    return this->{openAct};
 
41
}
 
42
 
 
43
sub saveAsActs() {
 
44
    return this->{saveAsActs};
 
45
}
 
46
 
 
47
sub exitAct() {
 
48
    return this->{exitAct};
 
49
}
 
50
 
 
51
sub aboutAct() {
 
52
    return this->{aboutAct};
 
53
}
 
54
 
 
55
sub aboutQtAct() {
 
56
    return this->{aboutQtAct};
 
57
}
 
58
 
 
59
sub NEW
 
60
{
 
61
    my ($class) = @_;
 
62
    $class->SUPER::NEW();
 
63
    this->{saveAsActs} = [];
 
64
    this->{textEdit} = Qt::TextEdit();
 
65
    textEdit->setLineWrapMode(Qt::TextEdit::NoWrap());
 
66
    setCentralWidget(textEdit);
 
67
 
 
68
    findCodecs();
 
69
 
 
70
    this->{previewForm} = PreviewForm(this);
 
71
    previewForm->setCodecList(codecs);
 
72
 
 
73
    createActions();
 
74
    createMenus();
 
75
 
 
76
    setWindowTitle(this->tr('Codecs'));
 
77
    resize(500, 400);
 
78
}
 
79
 
 
80
sub open
 
81
{
 
82
    my $fileName = Qt::FileDialog::getOpenFileName(this);
 
83
    if ($fileName) {
 
84
        my $file = Qt::File($fileName);
 
85
        if (!$file->open(Qt::File::ReadOnly())) {
 
86
            Qt::MessageBox::warning(this, this->tr('Codecs'),
 
87
                         sprintf this->tr("Cannot read file %s:\n%s"),
 
88
                                 $fileName,
 
89
                                 $file.errorString());
 
90
            return;
 
91
        }
 
92
 
 
93
        my $data = $file->readAll();
 
94
 
 
95
        previewForm->setEncodedData($data);
 
96
        if (previewForm->exec()) {
 
97
            textEdit->setPlainText(previewForm->decodedString());
 
98
        }
 
99
    }
 
100
}
 
101
 
 
102
sub save
 
103
{
 
104
    my $fileName = Qt::FileDialog::getSaveFileName(this);
 
105
    if ($fileName) {
 
106
        my $file = Qt::File($fileName);
 
107
        if (!$file->open(Qt::File::WriteOnly() | Qt::File::Text())) {
 
108
            Qt::MessageBox::warning(this, this->tr('Codecs'),
 
109
                         sprintf this->tr("Cannot write file %s:\n%s"),
 
110
                                 $fileName,
 
111
                                 $file.errorString());
 
112
            return;
 
113
        }
 
114
 
 
115
        my $action = sender();
 
116
        my $codecName = $action->data()->toByteArray();
 
117
 
 
118
        my $out = Qt::TextStream($file);
 
119
        $out->setCodec($codecName->constData());
 
120
        no warnings qw(void);
 
121
        $out << textEdit->toPlainText();
 
122
        $file->close();
 
123
    }
 
124
}
 
125
 
 
126
sub about
 
127
{
 
128
    Qt::MessageBox::about(this, this->tr('About Codecs'),
 
129
            this->tr('The <b>Codecs</b> example demonstrates how to read and write ' .
 
130
               'files using various encodings.'));
 
131
}
 
132
 
 
133
sub aboutToShowSaveAsMenu
 
134
{
 
135
    my $currentText = textEdit->toPlainText();
 
136
 
 
137
    foreach my $action ( @{saveAsActs()} ) {
 
138
        my $codecName = $action->data()->toByteArray();
 
139
        my $codec = Qt::TextCodec::codecForName($codecName);
 
140
        $action->setVisible($codec && $codec->canEncode($currentText));
 
141
    }
 
142
}
 
143
 
 
144
sub findCodecs
 
145
{
 
146
    my %codecMap;
 
147
    my $iso8859RegExp = Qt::RegExp('ISO[- ]8859-([0-9]+).*');
 
148
 
 
149
    foreach my $mib ( @{Qt::TextCodec::availableMibs()} ) {
 
150
        my $codec = Qt::TextCodec::codecForMib($mib);
 
151
 
 
152
        my $sortKey = $codec->name()->toUpper();
 
153
        my $rank;
 
154
 
 
155
        if ($sortKey->startsWith('UTF-8')) {
 
156
            $rank = 1;
 
157
        } elsif ($sortKey->startsWith('UTF-16')) {
 
158
            $rank = 2;
 
159
        } elsif ($iso8859RegExp->exactMatch($sortKey->constData())) {
 
160
            if (length $iso8859RegExp->cap(1) == 1) {
 
161
                $rank = 3;
 
162
            }
 
163
            else {
 
164
                $rank = 4;
 
165
            }
 
166
        } else {
 
167
            $rank = 5;
 
168
        }
 
169
        $sortKey->prepend(Qt::CString($rank));
 
170
 
 
171
        $codecMap{$sortKey->constData} = $codec;
 
172
    }
 
173
    this->{codecs} = [map{ $codecMap{$_} } sort keys %codecMap];
 
174
}
 
175
 
 
176
sub createActions
 
177
{
 
178
    this->{openAct} = Qt::Action(this->tr('&Open...'), this);
 
179
    openAct->setShortcut(Qt::KeySequence(Qt::KeySequence::Open()));
 
180
    this->connect(openAct, SIGNAL 'triggered()', this, SLOT 'open()');
 
181
 
 
182
    foreach my $codec ( @{codecs() } ) {
 
183
        my $text = sprintf this->tr('%s...'), $codec->name()->constData();
 
184
 
 
185
        my $action = Qt::Action($text, this);
 
186
        $action->setData(Qt::Variant($codec->name()));
 
187
        this->connect($action, SIGNAL 'triggered()', this, SLOT 'save()');
 
188
        push @{this->{saveAsActs}}, $action;
 
189
    }
 
190
 
 
191
    this->{exitAct} = Qt::Action(this->tr('E&xit'), this);
 
192
    exitAct->setShortcut(Qt::KeySequence(Qt::KeySequence::Quit()));
 
193
    this->connect(exitAct, SIGNAL 'triggered()', this, SLOT 'close()');
 
194
 
 
195
    this->{aboutAct} = Qt::Action(this->tr('&About'), this);
 
196
    this->connect(aboutAct, SIGNAL 'triggered()', this, SLOT 'about()');
 
197
 
 
198
    this->{aboutQtAct} = Qt::Action(this->tr('About &Qt'), this);
 
199
    this->connect(aboutQtAct, SIGNAL 'triggered()', qApp, SLOT 'aboutQt()');
 
200
}
 
201
 
 
202
sub createMenus
 
203
{
 
204
    this->{saveAsMenu} = Qt::Menu(this->tr('&Save As'), this);
 
205
    foreach my $action ( @{saveAsActs()} ) {
 
206
        saveAsMenu->addAction($action);
 
207
    }
 
208
    this->connect(saveAsMenu, SIGNAL 'aboutToShow()',
 
209
            this, SLOT 'aboutToShowSaveAsMenu()');
 
210
 
 
211
    this->{fileMenu} = Qt::Menu(this->tr('&File'), this);
 
212
    fileMenu->addAction(openAct);
 
213
    fileMenu->addMenu(saveAsMenu);
 
214
    fileMenu->addSeparator();
 
215
    fileMenu->addAction(exitAct);
 
216
 
 
217
    this->{helpMenu} = Qt::Menu(this->tr('&Help'), this);
 
218
    helpMenu->addAction(aboutAct);
 
219
    helpMenu->addAction(aboutQtAct);
 
220
 
 
221
    menuBar()->addMenu(fileMenu);
 
222
    menuBar()->addSeparator();
 
223
    menuBar()->addMenu(helpMenu);
 
224
}
 
225
 
 
226
1;