diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:18:55 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:18:55 (GMT) |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /examples/tools/codecs | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/tools/codecs')
-rw-r--r-- | examples/tools/codecs/codecs.pro | 11 | ||||
-rw-r--r-- | examples/tools/codecs/encodedfiles/.gitattributes | 2 | ||||
-rw-r--r-- | examples/tools/codecs/encodedfiles/iso-8859-1.txt | 6 | ||||
-rw-r--r-- | examples/tools/codecs/encodedfiles/iso-8859-15.txt | 8 | ||||
-rw-r--r-- | examples/tools/codecs/encodedfiles/utf-16.txt | bin | 0 -> 162 bytes | |||
-rw-r--r-- | examples/tools/codecs/encodedfiles/utf-16be.txt | bin | 0 -> 160 bytes | |||
-rw-r--r-- | examples/tools/codecs/encodedfiles/utf-16le.txt | bin | 0 -> 160 bytes | |||
-rw-r--r-- | examples/tools/codecs/encodedfiles/utf-8.txt | 6 | ||||
-rw-r--r-- | examples/tools/codecs/main.cpp | 52 | ||||
-rw-r--r-- | examples/tools/codecs/mainwindow.cpp | 203 | ||||
-rw-r--r-- | examples/tools/codecs/mainwindow.h | 88 | ||||
-rw-r--r-- | examples/tools/codecs/previewform.cpp | 102 | ||||
-rw-r--r-- | examples/tools/codecs/previewform.h | 80 |
13 files changed, 558 insertions, 0 deletions
diff --git a/examples/tools/codecs/codecs.pro b/examples/tools/codecs/codecs.pro new file mode 100644 index 0000000..6ae225a --- /dev/null +++ b/examples/tools/codecs/codecs.pro @@ -0,0 +1,11 @@ +HEADERS += mainwindow.h \ + previewform.h +SOURCES += main.cpp \ + mainwindow.cpp \ + previewform.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/tools/codecs +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS encodedfiles codecs.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/tools/codecs +INSTALLS += target sources diff --git a/examples/tools/codecs/encodedfiles/.gitattributes b/examples/tools/codecs/encodedfiles/.gitattributes new file mode 100644 index 0000000..996aab2 --- /dev/null +++ b/examples/tools/codecs/encodedfiles/.gitattributes @@ -0,0 +1,2 @@ +iso-8859-15.txt -crlf +iso-8859-1.txt -crlf diff --git a/examples/tools/codecs/encodedfiles/iso-8859-1.txt b/examples/tools/codecs/encodedfiles/iso-8859-1.txt new file mode 100644 index 0000000..4a7ebe3 --- /dev/null +++ b/examples/tools/codecs/encodedfiles/iso-8859-1.txt @@ -0,0 +1,6 @@ +Paulo Coelho: O Gênio e as Rosas
+Anna Hallström, Urban Östberg: Svår svenska
+Darrell Huff: How to Lie with Statistics
+Franz Kafka: Das Schloß
+Walter Moers: Die 13½ Leben des Käpt'n Blaubär
+Dag Solstad: Forsøk på å beskrive det ugjennomtrengelige
diff --git a/examples/tools/codecs/encodedfiles/iso-8859-15.txt b/examples/tools/codecs/encodedfiles/iso-8859-15.txt new file mode 100644 index 0000000..cd43ea3 --- /dev/null +++ b/examples/tools/codecs/encodedfiles/iso-8859-15.txt @@ -0,0 +1,8 @@ +Paulo Coelho: O Gênio e as Rosas
+Jean-Pierre Coffe: À table en famille avec 15 ¤ par jour
+Anna Hallström, Urban Östberg: Svår svenska
+Darrell Huff: How to Lie with Statistics
+Franz Kafka: Das Schloß
+Helena Lehecková: T¨ekkiä suomalaisille
+Arthur Rimbaud: ¼uvres complètes
+Dag Solstad: Forsøk på å beskrive det ugjennomtrengelige
diff --git a/examples/tools/codecs/encodedfiles/utf-16.txt b/examples/tools/codecs/encodedfiles/utf-16.txt Binary files differnew file mode 100644 index 0000000..371f06f --- /dev/null +++ b/examples/tools/codecs/encodedfiles/utf-16.txt diff --git a/examples/tools/codecs/encodedfiles/utf-16be.txt b/examples/tools/codecs/encodedfiles/utf-16be.txt Binary files differnew file mode 100644 index 0000000..d8ae642 --- /dev/null +++ b/examples/tools/codecs/encodedfiles/utf-16be.txt diff --git a/examples/tools/codecs/encodedfiles/utf-16le.txt b/examples/tools/codecs/encodedfiles/utf-16le.txt Binary files differnew file mode 100644 index 0000000..3779264 --- /dev/null +++ b/examples/tools/codecs/encodedfiles/utf-16le.txt diff --git a/examples/tools/codecs/encodedfiles/utf-8.txt b/examples/tools/codecs/encodedfiles/utf-8.txt new file mode 100644 index 0000000..a5e4ae6f --- /dev/null +++ b/examples/tools/codecs/encodedfiles/utf-8.txt @@ -0,0 +1,6 @@ +SprÃ¥k: Norsk +Γλώσσα: Ελληνικά +Язык: РуÑÑкий +언어 : í•œêµì–´ +言語: 日本語 +Langage : Français diff --git a/examples/tools/codecs/main.cpp b/examples/tools/codecs/main.cpp new file mode 100644 index 0000000..4856752 --- /dev/null +++ b/examples/tools/codecs/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + return app.exec(); +} diff --git a/examples/tools/codecs/mainwindow.cpp b/examples/tools/codecs/mainwindow.cpp new file mode 100644 index 0000000..1e6fa69 --- /dev/null +++ b/examples/tools/codecs/mainwindow.cpp @@ -0,0 +1,203 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "mainwindow.h" +#include "previewform.h" + +MainWindow::MainWindow() +{ + textEdit = new QTextEdit; + textEdit->setLineWrapMode(QTextEdit::NoWrap); + setCentralWidget(textEdit); + + findCodecs(); + + previewForm = new PreviewForm(this); + previewForm->setCodecList(codecs); + + createActions(); + createMenus(); + + setWindowTitle(tr("Codecs")); + resize(500, 400); +} + +void MainWindow::open() +{ + QString fileName = QFileDialog::getOpenFileName(this); + if (!fileName.isEmpty()) { + QFile file(fileName); + if (!file.open(QFile::ReadOnly)) { + QMessageBox::warning(this, tr("Codecs"), + tr("Cannot read file %1:\n%2") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QByteArray data = file.readAll(); + + previewForm->setEncodedData(data); + if (previewForm->exec()) + textEdit->setPlainText(previewForm->decodedString()); + } +} + +void MainWindow::save() +{ + QString fileName = QFileDialog::getSaveFileName(this); + if (!fileName.isEmpty()) { + QFile file(fileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Codecs"), + tr("Cannot write file %1:\n%2") + .arg(fileName) + .arg(file.errorString())); + return; + } + + QAction *action = qobject_cast<QAction *>(sender()); + QByteArray codecName = action->data().toByteArray(); + + QTextStream out(&file); + out.setCodec(codecName); + out << textEdit->toPlainText(); + } +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Codecs"), + tr("The <b>Codecs</b> example demonstrates how to read and write " + "files using various encodings.")); +} + +void MainWindow::aboutToShowSaveAsMenu() +{ + QString currentText = textEdit->toPlainText(); + + foreach (QAction *action, saveAsActs) { + QByteArray codecName = action->data().toByteArray(); + QTextCodec *codec = QTextCodec::codecForName(codecName); + action->setVisible(codec && codec->canEncode(currentText)); + } +} + +void MainWindow::findCodecs() +{ + QMap<QString, QTextCodec *> codecMap; + QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*"); + + foreach (int mib, QTextCodec::availableMibs()) { + QTextCodec *codec = QTextCodec::codecForMib(mib); + + QString sortKey = codec->name().toUpper(); + int rank; + + if (sortKey.startsWith("UTF-8")) { + rank = 1; + } else if (sortKey.startsWith("UTF-16")) { + rank = 2; + } else if (iso8859RegExp.exactMatch(sortKey)) { + if (iso8859RegExp.cap(1).size() == 1) + rank = 3; + else + rank = 4; + } else { + rank = 5; + } + sortKey.prepend(QChar('0' + rank)); + + codecMap.insert(sortKey, codec); + } + codecs = codecMap.values(); +} + +void MainWindow::createActions() +{ + openAct = new QAction(tr("&Open..."), this); + openAct->setShortcut(tr("Ctrl+O")); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + foreach (QTextCodec *codec, codecs) { + QString text = tr("%1...").arg(QString(codec->name())); + + QAction *action = new QAction(text, this); + action->setData(codec->name()); + connect(action, SIGNAL(triggered()), this, SLOT(save())); + saveAsActs.append(action); + } + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcut(tr("Ctrl+Q")); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + aboutAct = new QAction(tr("&About"), this); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} + +void MainWindow::createMenus() +{ + saveAsMenu = new QMenu(tr("&Save As"), this); + foreach (QAction *action, saveAsActs) + saveAsMenu->addAction(action); + connect(saveAsMenu, SIGNAL(aboutToShow()), + this, SLOT(aboutToShowSaveAsMenu())); + + fileMenu = new QMenu(tr("&File"), this); + fileMenu->addAction(openAct); + fileMenu->addMenu(saveAsMenu); + fileMenu->addSeparator(); + fileMenu->addAction(exitAct); + + helpMenu = new QMenu(tr("&Help"), this); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); + + menuBar()->addMenu(fileMenu); + menuBar()->addSeparator(); + menuBar()->addMenu(helpMenu); +} diff --git a/examples/tools/codecs/mainwindow.h b/examples/tools/codecs/mainwindow.h new file mode 100644 index 0000000..1702d5c --- /dev/null +++ b/examples/tools/codecs/mainwindow.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QList> +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QAction; +class QMenu; +class QTextCodec; +class QTextEdit; +QT_END_NAMESPACE +class PreviewForm; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void open(); + void save(); + void about(); + void aboutToShowSaveAsMenu(); + +private: + void findCodecs(); + void createActions(); + void createMenus(); + + QTextEdit *textEdit; + PreviewForm *previewForm; + QList<QTextCodec *> codecs; + + QMenu *fileMenu; + QMenu *helpMenu; + QMenu *saveAsMenu; + QAction *openAct; + QList<QAction *> saveAsActs; + QAction *exitAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; + +#endif diff --git a/examples/tools/codecs/previewform.cpp b/examples/tools/codecs/previewform.cpp new file mode 100644 index 0000000..cbe084f --- /dev/null +++ b/examples/tools/codecs/previewform.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "previewform.h" + +PreviewForm::PreviewForm(QWidget *parent) + : QDialog(parent) +{ + encodingComboBox = new QComboBox; + + encodingLabel = new QLabel(tr("&Encoding:")); + encodingLabel->setBuddy(encodingComboBox); + + textEdit = new QTextEdit; + textEdit->setLineWrapMode(QTextEdit::NoWrap); + textEdit->setReadOnly(true); + + buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok + | QDialogButtonBox::Cancel); + + connect(encodingComboBox, SIGNAL(activated(int)), + this, SLOT(updateTextEdit())); + connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); + connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(encodingLabel, 0, 0); + mainLayout->addWidget(encodingComboBox, 0, 1); + mainLayout->addWidget(textEdit, 1, 0, 1, 2); + mainLayout->addWidget(buttonBox, 2, 0, 1, 2); + setLayout(mainLayout); + + setWindowTitle(tr("Choose Encoding")); + resize(400, 300); +} + +void PreviewForm::setCodecList(const QList<QTextCodec *> &list) +{ + encodingComboBox->clear(); + foreach (QTextCodec *codec, list) + encodingComboBox->addItem(codec->name(), codec->mibEnum()); +} + +void PreviewForm::setEncodedData(const QByteArray &data) +{ + encodedData = data; + updateTextEdit(); +} + +void PreviewForm::updateTextEdit() +{ + int mib = encodingComboBox->itemData( + encodingComboBox->currentIndex()).toInt(); + QTextCodec *codec = QTextCodec::codecForMib(mib); + + QTextStream in(&encodedData); + in.setAutoDetectUnicode(false); + in.setCodec(codec); + decodedStr = in.readAll(); + + textEdit->setPlainText(decodedStr); +} diff --git a/examples/tools/codecs/previewform.h b/examples/tools/codecs/previewform.h new file mode 100644 index 0000000..130cb79 --- /dev/null +++ b/examples/tools/codecs/previewform.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PREVIEWFORM_H +#define PREVIEWFORM_H + +#include <QDialog> +#include <QList> + +QT_BEGIN_NAMESPACE +class QComboBox; +class QDialogButtonBox; +class QLabel; +class QTextCodec; +class QTextEdit; +QT_END_NAMESPACE + +class PreviewForm : public QDialog +{ + Q_OBJECT + +public: + PreviewForm(QWidget *parent = 0); + + void setCodecList(const QList<QTextCodec *> &list); + void setEncodedData(const QByteArray &data); + QString decodedString() const { return decodedStr; } + +private slots: + void updateTextEdit(); + +private: + QByteArray encodedData; + QString decodedStr; + + QComboBox *encodingComboBox; + QLabel *encodingLabel; + QTextEdit *textEdit; + QDialogButtonBox *buttonBox; +}; + +#endif |