examples/tools/codecs/mainwindow.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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->setShortcuts(QKeySequence::Open);
       
   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->setShortcuts(QKeySequence::Quit);
       
   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 }