7
use QtCore4::isa qw( Qt::MainWindow );
12
aboutToShowSaveAsMenu => [];
16
return this->{textEdit};
20
return this->{previewForm};
24
return this->{codecs};
28
return this->{fileMenu};
32
return this->{helpMenu};
36
return this->{saveAsMenu};
40
return this->{openAct};
44
return this->{saveAsActs};
48
return this->{exitAct};
52
return this->{aboutAct};
56
return this->{aboutQtAct};
63
this->{saveAsActs} = [];
64
this->{textEdit} = Qt::TextEdit();
65
textEdit->setLineWrapMode(Qt::TextEdit::NoWrap());
66
setCentralWidget(textEdit);
70
this->{previewForm} = PreviewForm(this);
71
previewForm->setCodecList(codecs);
76
setWindowTitle(this->tr('Codecs'));
82
my $fileName = Qt::FileDialog::getOpenFileName(this);
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"),
93
my $data = $file->readAll();
95
previewForm->setEncodedData($data);
96
if (previewForm->exec()) {
97
textEdit->setPlainText(previewForm->decodedString());
104
my $fileName = Qt::FileDialog::getSaveFileName(this);
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"),
111
$file.errorString());
115
my $action = sender();
116
my $codecName = $action->data()->toByteArray();
118
my $out = Qt::TextStream($file);
119
$out->setCodec($codecName->constData());
120
no warnings qw(void);
121
$out << textEdit->toPlainText();
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.'));
133
sub aboutToShowSaveAsMenu
135
my $currentText = textEdit->toPlainText();
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));
147
my $iso8859RegExp = Qt::RegExp('ISO[- ]8859-([0-9]+).*');
149
foreach my $mib ( @{Qt::TextCodec::availableMibs()} ) {
150
my $codec = Qt::TextCodec::codecForMib($mib);
152
my $sortKey = $codec->name()->toUpper();
155
if ($sortKey->startsWith('UTF-8')) {
157
} elsif ($sortKey->startsWith('UTF-16')) {
159
} elsif ($iso8859RegExp->exactMatch($sortKey->constData())) {
160
if (length $iso8859RegExp->cap(1) == 1) {
169
$sortKey->prepend(Qt::CString($rank));
171
$codecMap{$sortKey->constData} = $codec;
173
this->{codecs} = [map{ $codecMap{$_} } sort keys %codecMap];
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()');
182
foreach my $codec ( @{codecs() } ) {
183
my $text = sprintf this->tr('%s...'), $codec->name()->constData();
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;
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()');
195
this->{aboutAct} = Qt::Action(this->tr('&About'), this);
196
this->connect(aboutAct, SIGNAL 'triggered()', this, SLOT 'about()');
198
this->{aboutQtAct} = Qt::Action(this->tr('About &Qt'), this);
199
this->connect(aboutQtAct, SIGNAL 'triggered()', qApp, SLOT 'aboutQt()');
204
this->{saveAsMenu} = Qt::Menu(this->tr('&Save As'), this);
205
foreach my $action ( @{saveAsActs()} ) {
206
saveAsMenu->addAction($action);
208
this->connect(saveAsMenu, SIGNAL 'aboutToShow()',
209
this, SLOT 'aboutToShowSaveAsMenu()');
211
this->{fileMenu} = Qt::Menu(this->tr('&File'), this);
212
fileMenu->addAction(openAct);
213
fileMenu->addMenu(saveAsMenu);
214
fileMenu->addSeparator();
215
fileMenu->addAction(exitAct);
217
this->{helpMenu} = Qt::Menu(this->tr('&Help'), this);
218
helpMenu->addAction(aboutAct);
219
helpMenu->addAction(aboutQtAct);
221
menuBar()->addMenu(fileMenu);
222
menuBar()->addSeparator();
223
menuBar()->addMenu(helpMenu);