| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtGui> | 
|---|
| 43 |  | 
|---|
| 44 | #include "mainwindow.h" | 
|---|
| 45 | #include "previewform.h" | 
|---|
| 46 |  | 
|---|
| 47 | MainWindow::MainWindow() | 
|---|
| 48 | { | 
|---|
| 49 | textEdit = new QTextEdit; | 
|---|
| 50 | textEdit->setLineWrapMode(QTextEdit::NoWrap); | 
|---|
| 51 | setCentralWidget(textEdit); | 
|---|
| 52 |  | 
|---|
| 53 | findCodecs(); | 
|---|
| 54 |  | 
|---|
| 55 | previewForm = new PreviewForm(this); | 
|---|
| 56 | previewForm->setCodecList(codecs); | 
|---|
| 57 |  | 
|---|
| 58 | createActions(); | 
|---|
| 59 | createMenus(); | 
|---|
| 60 |  | 
|---|
| 61 | setWindowTitle(tr("Codecs")); | 
|---|
| 62 | resize(500, 400); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void MainWindow::open() | 
|---|
| 66 | { | 
|---|
| 67 | QString fileName = QFileDialog::getOpenFileName(this); | 
|---|
| 68 | if (!fileName.isEmpty()) { | 
|---|
| 69 | QFile file(fileName); | 
|---|
| 70 | if (!file.open(QFile::ReadOnly)) { | 
|---|
| 71 | QMessageBox::warning(this, tr("Codecs"), | 
|---|
| 72 | tr("Cannot read file %1:\n%2") | 
|---|
| 73 | .arg(fileName) | 
|---|
| 74 | .arg(file.errorString())); | 
|---|
| 75 | return; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | QByteArray data = file.readAll(); | 
|---|
| 79 |  | 
|---|
| 80 | previewForm->setEncodedData(data); | 
|---|
| 81 | if (previewForm->exec()) | 
|---|
| 82 | textEdit->setPlainText(previewForm->decodedString()); | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | void MainWindow::save() | 
|---|
| 87 | { | 
|---|
| 88 | QString fileName = QFileDialog::getSaveFileName(this); | 
|---|
| 89 | if (!fileName.isEmpty()) { | 
|---|
| 90 | QFile file(fileName); | 
|---|
| 91 | if (!file.open(QFile::WriteOnly | QFile::Text)) { | 
|---|
| 92 | QMessageBox::warning(this, tr("Codecs"), | 
|---|
| 93 | tr("Cannot write file %1:\n%2") | 
|---|
| 94 | .arg(fileName) | 
|---|
| 95 | .arg(file.errorString())); | 
|---|
| 96 | return; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | QAction *action = qobject_cast<QAction *>(sender()); | 
|---|
| 100 | QByteArray codecName = action->data().toByteArray(); | 
|---|
| 101 |  | 
|---|
| 102 | QTextStream out(&file); | 
|---|
| 103 | out.setCodec(codecName); | 
|---|
| 104 | out << textEdit->toPlainText(); | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | void MainWindow::about() | 
|---|
| 109 | { | 
|---|
| 110 | QMessageBox::about(this, tr("About Codecs"), | 
|---|
| 111 | tr("The <b>Codecs</b> example demonstrates how to read and write " | 
|---|
| 112 | "files using various encodings.")); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | void MainWindow::aboutToShowSaveAsMenu() | 
|---|
| 116 | { | 
|---|
| 117 | QString currentText = textEdit->toPlainText(); | 
|---|
| 118 |  | 
|---|
| 119 | foreach (QAction *action, saveAsActs) { | 
|---|
| 120 | QByteArray codecName = action->data().toByteArray(); | 
|---|
| 121 | QTextCodec *codec = QTextCodec::codecForName(codecName); | 
|---|
| 122 | action->setVisible(codec && codec->canEncode(currentText)); | 
|---|
| 123 | } | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | void MainWindow::findCodecs() | 
|---|
| 127 | { | 
|---|
| 128 | QMap<QString, QTextCodec *> codecMap; | 
|---|
| 129 | QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*"); | 
|---|
| 130 |  | 
|---|
| 131 | foreach (int mib, QTextCodec::availableMibs()) { | 
|---|
| 132 | QTextCodec *codec = QTextCodec::codecForMib(mib); | 
|---|
| 133 |  | 
|---|
| 134 | QString sortKey = codec->name().toUpper(); | 
|---|
| 135 | int rank; | 
|---|
| 136 |  | 
|---|
| 137 | if (sortKey.startsWith("UTF-8")) { | 
|---|
| 138 | rank = 1; | 
|---|
| 139 | } else if (sortKey.startsWith("UTF-16")) { | 
|---|
| 140 | rank = 2; | 
|---|
| 141 | } else if (iso8859RegExp.exactMatch(sortKey)) { | 
|---|
| 142 | if (iso8859RegExp.cap(1).size() == 1) | 
|---|
| 143 | rank = 3; | 
|---|
| 144 | else | 
|---|
| 145 | rank = 4; | 
|---|
| 146 | } else { | 
|---|
| 147 | rank = 5; | 
|---|
| 148 | } | 
|---|
| 149 | sortKey.prepend(QChar('0' + rank)); | 
|---|
| 150 |  | 
|---|
| 151 | codecMap.insert(sortKey, codec); | 
|---|
| 152 | } | 
|---|
| 153 | codecs = codecMap.values(); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | void MainWindow::createActions() | 
|---|
| 157 | { | 
|---|
| 158 | openAct = new QAction(tr("&Open..."), this); | 
|---|
| 159 | openAct->setShortcut(tr("Ctrl+O")); | 
|---|
| 160 | connect(openAct, SIGNAL(triggered()), this, SLOT(open())); | 
|---|
| 161 |  | 
|---|
| 162 | foreach (QTextCodec *codec, codecs) { | 
|---|
| 163 | QString text = tr("%1...").arg(QString(codec->name())); | 
|---|
| 164 |  | 
|---|
| 165 | QAction *action = new QAction(text, this); | 
|---|
| 166 | action->setData(codec->name()); | 
|---|
| 167 | connect(action, SIGNAL(triggered()), this, SLOT(save())); | 
|---|
| 168 | saveAsActs.append(action); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | exitAct = new QAction(tr("E&xit"), this); | 
|---|
| 172 | exitAct->setShortcut(tr("Ctrl+Q")); | 
|---|
| 173 | connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); | 
|---|
| 174 |  | 
|---|
| 175 | aboutAct = new QAction(tr("&About"), this); | 
|---|
| 176 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); | 
|---|
| 177 |  | 
|---|
| 178 | aboutQtAct = new QAction(tr("About &Qt"), this); | 
|---|
| 179 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | void MainWindow::createMenus() | 
|---|
| 183 | { | 
|---|
| 184 | saveAsMenu = new QMenu(tr("&Save As"), this); | 
|---|
| 185 | foreach (QAction *action, saveAsActs) | 
|---|
| 186 | saveAsMenu->addAction(action); | 
|---|
| 187 | connect(saveAsMenu, SIGNAL(aboutToShow()), | 
|---|
| 188 | this, SLOT(aboutToShowSaveAsMenu())); | 
|---|
| 189 |  | 
|---|
| 190 | fileMenu = new QMenu(tr("&File"), this); | 
|---|
| 191 | fileMenu->addAction(openAct); | 
|---|
| 192 | fileMenu->addMenu(saveAsMenu); | 
|---|
| 193 | fileMenu->addSeparator(); | 
|---|
| 194 | fileMenu->addAction(exitAct); | 
|---|
| 195 |  | 
|---|
| 196 | helpMenu = new QMenu(tr("&Help"), this); | 
|---|
| 197 | helpMenu->addAction(aboutAct); | 
|---|
| 198 | helpMenu->addAction(aboutQtAct); | 
|---|
| 199 |  | 
|---|
| 200 | menuBar()->addMenu(fileMenu); | 
|---|
| 201 | menuBar()->addSeparator(); | 
|---|
| 202 | menuBar()->addMenu(helpMenu); | 
|---|
| 203 | } | 
|---|