1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
43 #include "mainwindow.h"
44 #include "previewform.h"
46 MainWindow::MainWindow()
48 textEdit = new QTextEdit;
49 textEdit->setLineWrapMode(QTextEdit::NoWrap);
50 setCentralWidget(textEdit);
54 previewForm = new PreviewForm(this);
55 previewForm->setCodecList(codecs);
60 setWindowTitle(tr("Codecs"));
64 void MainWindow::open()
66 QString fileName = QFileDialog::getOpenFileName(this);
67 if (!fileName.isEmpty()) {
69 if (!file.open(QFile::ReadOnly)) {
70 QMessageBox::warning(this, tr("Codecs"),
71 tr("Cannot read file %1:\n%2")
73 .arg(file.errorString()));
77 QByteArray data = file.readAll();
79 previewForm->setEncodedData(data);
80 if (previewForm->exec())
81 textEdit->setPlainText(previewForm->decodedString());
85 void MainWindow::save()
87 QString fileName = QFileDialog::getSaveFileName(this);
88 if (!fileName.isEmpty()) {
90 if (!file.open(QFile::WriteOnly | QFile::Text)) {
91 QMessageBox::warning(this, tr("Codecs"),
92 tr("Cannot write file %1:\n%2")
94 .arg(file.errorString()));
98 QAction *action = qobject_cast<QAction *>(sender());
99 QByteArray codecName = action->data().toByteArray();
101 QTextStream out(&file);
102 out.setCodec(codecName.constData());
103 out << textEdit->toPlainText();
107 void MainWindow::about()
109 QMessageBox::about(this, tr("About Codecs"),
110 tr("The <b>Codecs</b> example demonstrates how to read and write "
111 "files using various encodings."));
114 void MainWindow::aboutToShowSaveAsMenu()
116 QString currentText = textEdit->toPlainText();
118 foreach (QAction *action, saveAsActs) {
119 QByteArray codecName = action->data().toByteArray();
120 QTextCodec *codec = QTextCodec::codecForName(codecName);
121 action->setVisible(codec && codec->canEncode(currentText));
125 void MainWindow::findCodecs()
127 QMap<QString, QTextCodec *> codecMap;
128 QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*");
130 foreach (int mib, QTextCodec::availableMibs()) {
131 QTextCodec *codec = QTextCodec::codecForMib(mib);
133 QString sortKey = codec->name().toUpper();
136 if (sortKey.startsWith("UTF-8")) {
138 } else if (sortKey.startsWith("UTF-16")) {
140 } else if (iso8859RegExp.exactMatch(sortKey)) {
141 if (iso8859RegExp.cap(1).size() == 1)
148 sortKey.prepend(QChar('0' + rank));
150 codecMap.insert(sortKey, codec);
152 codecs = codecMap.values();
155 void MainWindow::createActions()
157 openAct = new QAction(tr("&Open..."), this);
158 openAct->setShortcuts(QKeySequence::Open);
159 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
161 foreach (QTextCodec *codec, codecs) {
162 QString text = tr("%1...").arg(QString(codec->name()));
164 QAction *action = new QAction(text, this);
165 action->setData(codec->name());
166 connect(action, SIGNAL(triggered()), this, SLOT(save()));
167 saveAsActs.append(action);
170 exitAct = new QAction(tr("E&xit"), this);
171 exitAct->setShortcuts(QKeySequence::Quit);
172 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
174 aboutAct = new QAction(tr("&About"), this);
175 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
177 aboutQtAct = new QAction(tr("About &Qt"), this);
178 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
181 void MainWindow::createMenus()
183 saveAsMenu = new QMenu(tr("&Save As"), this);
184 foreach (QAction *action, saveAsActs)
185 saveAsMenu->addAction(action);
186 connect(saveAsMenu, SIGNAL(aboutToShow()),
187 this, SLOT(aboutToShowSaveAsMenu()));
189 fileMenu = new QMenu(tr("&File"), this);
190 fileMenu->addAction(openAct);
191 fileMenu->addMenu(saveAsMenu);
192 fileMenu->addSeparator();
193 fileMenu->addAction(exitAct);
195 helpMenu = new QMenu(tr("&Help"), this);
196 helpMenu->addAction(aboutAct);
197 helpMenu->addAction(aboutQtAct);
199 menuBar()->addMenu(fileMenu);
200 menuBar()->addSeparator();
201 menuBar()->addMenu(helpMenu);