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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
package MainWindow;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::MainWindow );
use QtCore4::slots
open => [],
save => [],
about => [],
aboutToShowSaveAsMenu => [];
use PreviewForm;
sub textEdit() {
return this->{textEdit};
}
sub previewForm() {
return this->{previewForm};
}
sub codecs() {
return this->{codecs};
}
sub fileMenu() {
return this->{fileMenu};
}
sub helpMenu() {
return this->{helpMenu};
}
sub saveAsMenu() {
return this->{saveAsMenu};
}
sub openAct() {
return this->{openAct};
}
sub saveAsActs() {
return this->{saveAsActs};
}
sub exitAct() {
return this->{exitAct};
}
sub aboutAct() {
return this->{aboutAct};
}
sub aboutQtAct() {
return this->{aboutQtAct};
}
sub NEW
{
my ($class) = @_;
$class->SUPER::NEW();
this->{saveAsActs} = [];
this->{textEdit} = Qt::TextEdit();
textEdit->setLineWrapMode(Qt::TextEdit::NoWrap());
setCentralWidget(textEdit);
findCodecs();
this->{previewForm} = PreviewForm(this);
previewForm->setCodecList(codecs);
createActions();
createMenus();
setWindowTitle(this->tr('Codecs'));
resize(500, 400);
}
sub open
{
my $fileName = Qt::FileDialog::getOpenFileName(this);
if ($fileName) {
my $file = Qt::File($fileName);
if (!$file->open(Qt::File::ReadOnly())) {
Qt::MessageBox::warning(this, this->tr('Codecs'),
sprintf this->tr("Cannot read file %s:\n%s"),
$fileName,
$file.errorString());
return;
}
my $data = $file->readAll();
previewForm->setEncodedData($data);
if (previewForm->exec()) {
textEdit->setPlainText(previewForm->decodedString());
}
}
}
sub save
{
my $fileName = Qt::FileDialog::getSaveFileName(this);
if ($fileName) {
my $file = Qt::File($fileName);
if (!$file->open(Qt::File::WriteOnly() | Qt::File::Text())) {
Qt::MessageBox::warning(this, this->tr('Codecs'),
sprintf this->tr("Cannot write file %s:\n%s"),
$fileName,
$file.errorString());
return;
}
my $action = sender();
my $codecName = $action->data()->toByteArray();
my $out = Qt::TextStream($file);
$out->setCodec($codecName->constData());
no warnings qw(void);
$out << textEdit->toPlainText();
$file->close();
}
}
sub about
{
Qt::MessageBox::about(this, this->tr('About Codecs'),
this->tr('The <b>Codecs</b> example demonstrates how to read and write ' .
'files using various encodings.'));
}
sub aboutToShowSaveAsMenu
{
my $currentText = textEdit->toPlainText();
foreach my $action ( @{saveAsActs()} ) {
my $codecName = $action->data()->toByteArray();
my $codec = Qt::TextCodec::codecForName($codecName);
$action->setVisible($codec && $codec->canEncode($currentText));
}
}
sub findCodecs
{
my %codecMap;
my $iso8859RegExp = Qt::RegExp('ISO[- ]8859-([0-9]+).*');
foreach my $mib ( @{Qt::TextCodec::availableMibs()} ) {
my $codec = Qt::TextCodec::codecForMib($mib);
my $sortKey = $codec->name()->toUpper();
my $rank;
if ($sortKey->startsWith('UTF-8')) {
$rank = 1;
} elsif ($sortKey->startsWith('UTF-16')) {
$rank = 2;
} elsif ($iso8859RegExp->exactMatch($sortKey->constData())) {
if (length $iso8859RegExp->cap(1) == 1) {
$rank = 3;
}
else {
$rank = 4;
}
} else {
$rank = 5;
}
$sortKey->prepend(Qt::CString($rank));
$codecMap{$sortKey->constData} = $codec;
}
this->{codecs} = [map{ $codecMap{$_} } sort keys %codecMap];
}
sub createActions
{
this->{openAct} = Qt::Action(this->tr('&Open...'), this);
openAct->setShortcut(Qt::KeySequence(Qt::KeySequence::Open()));
this->connect(openAct, SIGNAL 'triggered()', this, SLOT 'open()');
foreach my $codec ( @{codecs() } ) {
my $text = sprintf this->tr('%s...'), $codec->name()->constData();
my $action = Qt::Action($text, this);
$action->setData(Qt::Variant($codec->name()));
this->connect($action, SIGNAL 'triggered()', this, SLOT 'save()');
push @{this->{saveAsActs}}, $action;
}
this->{exitAct} = Qt::Action(this->tr('E&xit'), this);
exitAct->setShortcut(Qt::KeySequence(Qt::KeySequence::Quit()));
this->connect(exitAct, SIGNAL 'triggered()', this, SLOT 'close()');
this->{aboutAct} = Qt::Action(this->tr('&About'), this);
this->connect(aboutAct, SIGNAL 'triggered()', this, SLOT 'about()');
this->{aboutQtAct} = Qt::Action(this->tr('About &Qt'), this);
this->connect(aboutQtAct, SIGNAL 'triggered()', qApp, SLOT 'aboutQt()');
}
sub createMenus
{
this->{saveAsMenu} = Qt::Menu(this->tr('&Save As'), this);
foreach my $action ( @{saveAsActs()} ) {
saveAsMenu->addAction($action);
}
this->connect(saveAsMenu, SIGNAL 'aboutToShow()',
this, SLOT 'aboutToShowSaveAsMenu()');
this->{fileMenu} = Qt::Menu(this->tr('&File'), this);
fileMenu->addAction(openAct);
fileMenu->addMenu(saveAsMenu);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
this->{helpMenu} = Qt::Menu(this->tr('&Help'), this);
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
menuBar()->addMenu(fileMenu);
menuBar()->addSeparator();
menuBar()->addMenu(helpMenu);
}
1;
|