1
/****************************************************************************
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "mainwindow.h"
32
#include "previewform.h"
34
MainWindow::MainWindow()
36
textEdit = new QTextEdit;
37
textEdit->setLineWrapMode(QTextEdit::NoWrap);
38
setCentralWidget(textEdit);
42
previewForm = new PreviewForm(this);
43
previewForm->setCodecList(codecs);
48
setWindowTitle(tr("Codecs"));
52
void MainWindow::open()
54
QString fileName = QFileDialog::getOpenFileName(this);
55
if (!fileName.isEmpty()) {
57
if (!file.open(QFile::ReadOnly)) {
58
QMessageBox::warning(this, tr("Codecs"),
59
tr("Cannot read file %1:\n%2")
61
.arg(file.errorString()));
65
QByteArray data = file.readAll();
67
previewForm->setEncodedData(data);
68
if (previewForm->exec())
69
textEdit->setPlainText(previewForm->decodedString());
73
void MainWindow::save()
75
QString fileName = QFileDialog::getSaveFileName(this);
76
if (!fileName.isEmpty()) {
78
if (!file.open(QFile::WriteOnly)) {
79
QMessageBox::warning(this, tr("Codecs"),
80
tr("Cannot write file %1:\n%2")
82
.arg(file.errorString()));
86
QAction *action = qobject_cast<QAction *>(sender());
87
QByteArray codecName = action->data().toByteArray();
89
QTextStream out(&file);
90
out.setCodec(codecName);
91
out << textEdit->toPlainText();
95
void MainWindow::about()
97
QMessageBox::about(this, tr("About Codecs"),
98
tr("The <b>Codecs</b> example demonstrates how to read and write "
99
"files using various encodings."));
102
void MainWindow::aboutToShowSaveAsMenu()
104
QString currentText = textEdit->toPlainText();
106
foreach (QAction *action, saveAsActs) {
107
QByteArray codecName = action->data().toByteArray();
108
QTextCodec *codec = QTextCodec::codecForName(codecName);
109
action->setVisible(codec && codec->canEncode(currentText));
113
void MainWindow::findCodecs()
115
QMap<QString, QTextCodec *> codecMap;
116
QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*");
118
foreach (int mib, QTextCodec::availableMibs()) {
119
QTextCodec *codec = QTextCodec::codecForMib(mib);
121
QString sortKey = codec->name().toUpper();
124
if (sortKey.startsWith("UTF-8")) {
126
} else if (sortKey.startsWith("UTF-16")) {
128
} else if (iso8859RegExp.exactMatch(sortKey)) {
129
if (iso8859RegExp.cap(1).size() == 1)
136
sortKey.prepend(QChar('0' + rank));
138
codecMap.insert(sortKey, codec);
140
codecs = codecMap.values();
143
void MainWindow::createActions()
145
openAct = new QAction(tr("&Open..."), this);
146
openAct->setShortcut(tr("Ctrl+O"));
147
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
149
foreach (QTextCodec *codec, codecs) {
150
QString text = tr("%1...").arg(QString(codec->name()));
152
QAction *action = new QAction(text, this);
153
action->setData(codec->name());
154
connect(action, SIGNAL(triggered()), this, SLOT(save()));
155
saveAsActs.append(action);
158
exitAct = new QAction(tr("E&xit"), this);
159
exitAct->setShortcut(tr("Ctrl+Q"));
160
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
162
aboutAct = new QAction(tr("&About"), this);
163
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
165
aboutQtAct = new QAction(tr("About &Qt"), this);
166
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
169
void MainWindow::createMenus()
171
saveAsMenu = new QMenu(tr("&Save As"), this);
172
foreach (QAction *action, saveAsActs)
173
saveAsMenu->addAction(action);
174
connect(saveAsMenu, SIGNAL(aboutToShow()),
175
this, SLOT(aboutToShowSaveAsMenu()));
177
fileMenu = new QMenu(tr("&File"), this);
178
fileMenu->addAction(openAct);
179
fileMenu->addMenu(saveAsMenu);
180
fileMenu->addSeparator();
181
fileMenu->addAction(exitAct);
183
helpMenu = new QMenu(tr("&Help"), this);
184
helpMenu->addAction(aboutAct);
185
helpMenu->addAction(aboutQtAct);
187
menuBar()->addMenu(fileMenu);
188
menuBar()->addSeparator();
189
menuBar()->addMenu(helpMenu);