~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/tools/codecs/mainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "mainwindow.h"
 
32
#include "previewform.h"
 
33
 
 
34
MainWindow::MainWindow()
 
35
{
 
36
    textEdit = new QTextEdit;
 
37
    textEdit->setLineWrapMode(QTextEdit::NoWrap);
 
38
    setCentralWidget(textEdit);
 
39
 
 
40
    findCodecs();
 
41
 
 
42
    previewForm = new PreviewForm(this);
 
43
    previewForm->setCodecList(codecs);
 
44
 
 
45
    createActions();
 
46
    createMenus();
 
47
 
 
48
    setWindowTitle(tr("Codecs"));
 
49
    resize(500, 400);
 
50
}
 
51
 
 
52
void MainWindow::open()
 
53
{
 
54
    QString fileName = QFileDialog::getOpenFileName(this);
 
55
    if (!fileName.isEmpty()) {
 
56
        QFile file(fileName);
 
57
        if (!file.open(QFile::ReadOnly)) {
 
58
            QMessageBox::warning(this, tr("Codecs"),
 
59
                                 tr("Cannot read file %1:\n%2")
 
60
                                 .arg(fileName)
 
61
                                 .arg(file.errorString()));
 
62
            return;
 
63
        }
 
64
 
 
65
        QByteArray data = file.readAll();
 
66
 
 
67
        previewForm->setEncodedData(data);
 
68
        if (previewForm->exec())
 
69
            textEdit->setPlainText(previewForm->decodedString());
 
70
    }
 
71
}
 
72
 
 
73
void MainWindow::save()
 
74
{
 
75
    QString fileName = QFileDialog::getSaveFileName(this);
 
76
    if (!fileName.isEmpty()) {
 
77
        QFile file(fileName);
 
78
        if (!file.open(QFile::WriteOnly)) {
 
79
            QMessageBox::warning(this, tr("Codecs"),
 
80
                                 tr("Cannot write file %1:\n%2")
 
81
                                 .arg(fileName)
 
82
                                 .arg(file.errorString()));
 
83
            return;
 
84
        }
 
85
 
 
86
        QAction *action = qobject_cast<QAction *>(sender());
 
87
        QByteArray codecName = action->data().toByteArray();
 
88
 
 
89
        QTextStream out(&file);
 
90
        out.setCodec(codecName);
 
91
        out << textEdit->toPlainText();
 
92
    }
 
93
}
 
94
 
 
95
void MainWindow::about()
 
96
{
 
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."));
 
100
}
 
101
 
 
102
void MainWindow::aboutToShowSaveAsMenu()
 
103
{
 
104
    QString currentText = textEdit->toPlainText();
 
105
 
 
106
    foreach (QAction *action, saveAsActs) {
 
107
        QByteArray codecName = action->data().toByteArray();
 
108
        QTextCodec *codec = QTextCodec::codecForName(codecName);
 
109
        action->setVisible(codec && codec->canEncode(currentText));
 
110
    }
 
111
}
 
112
 
 
113
void MainWindow::findCodecs()
 
114
{
 
115
    QMap<QString, QTextCodec *> codecMap;
 
116
    QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*");
 
117
 
 
118
    foreach (int mib, QTextCodec::availableMibs()) {
 
119
        QTextCodec *codec = QTextCodec::codecForMib(mib);
 
120
 
 
121
        QString sortKey = codec->name().toUpper();
 
122
        int rank;
 
123
 
 
124
        if (sortKey.startsWith("UTF-8")) {
 
125
            rank = 1;
 
126
        } else if (sortKey.startsWith("UTF-16")) {
 
127
            rank = 2;
 
128
        } else if (iso8859RegExp.exactMatch(sortKey)) {
 
129
            if (iso8859RegExp.cap(1).size() == 1)
 
130
                rank = 3;
 
131
            else
 
132
                rank = 4;
 
133
        } else {
 
134
            rank = 5;
 
135
        }
 
136
        sortKey.prepend(QChar('0' + rank));
 
137
 
 
138
        codecMap.insert(sortKey, codec);
 
139
    }
 
140
    codecs = codecMap.values();
 
141
}
 
142
 
 
143
void MainWindow::createActions()
 
144
{
 
145
    openAct = new QAction(tr("&Open..."), this);
 
146
    openAct->setShortcut(tr("Ctrl+O"));
 
147
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
148
 
 
149
    foreach (QTextCodec *codec, codecs) {
 
150
        QString text = tr("%1...").arg(QString(codec->name()));
 
151
 
 
152
        QAction *action = new QAction(text, this);
 
153
        action->setData(codec->name());
 
154
        connect(action, SIGNAL(triggered()), this, SLOT(save()));
 
155
        saveAsActs.append(action);
 
156
    }
 
157
 
 
158
    exitAct = new QAction(tr("E&xit"), this);
 
159
    exitAct->setShortcut(tr("Ctrl+Q"));
 
160
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
161
 
 
162
    aboutAct = new QAction(tr("&About"), this);
 
163
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
164
 
 
165
    aboutQtAct = new QAction(tr("About &Qt"), this);
 
166
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
167
}
 
168
 
 
169
void MainWindow::createMenus()
 
170
{
 
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()));
 
176
 
 
177
    fileMenu = new QMenu(tr("&File"), this);
 
178
    fileMenu->addAction(openAct);
 
179
    fileMenu->addMenu(saveAsMenu);
 
180
    fileMenu->addSeparator();
 
181
    fileMenu->addAction(exitAct);
 
182
 
 
183
    helpMenu = new QMenu(tr("&Help"), this);
 
184
    helpMenu->addAction(aboutAct);
 
185
    helpMenu->addAction(aboutQtAct);
 
186
 
 
187
    menuBar()->addMenu(fileMenu);
 
188
    menuBar()->addSeparator();
 
189
    menuBar()->addMenu(helpMenu);
 
190
}