author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include "mainwindow.h" |
|
44 |
#include "previewform.h" |
|
45 |
||
46 |
MainWindow::MainWindow() |
|
47 |
{ |
|
48 |
textEdit = new QTextEdit; |
|
49 |
textEdit->setLineWrapMode(QTextEdit::NoWrap); |
|
50 |
setCentralWidget(textEdit); |
|
51 |
||
52 |
findCodecs(); |
|
53 |
||
54 |
previewForm = new PreviewForm(this); |
|
55 |
previewForm->setCodecList(codecs); |
|
56 |
||
57 |
createActions(); |
|
58 |
createMenus(); |
|
59 |
||
60 |
setWindowTitle(tr("Codecs")); |
|
61 |
resize(500, 400); |
|
62 |
} |
|
63 |
||
64 |
void MainWindow::open() |
|
65 |
{ |
|
66 |
QString fileName = QFileDialog::getOpenFileName(this); |
|
67 |
if (!fileName.isEmpty()) { |
|
68 |
QFile file(fileName); |
|
69 |
if (!file.open(QFile::ReadOnly)) { |
|
70 |
QMessageBox::warning(this, tr("Codecs"), |
|
71 |
tr("Cannot read file %1:\n%2") |
|
72 |
.arg(fileName) |
|
73 |
.arg(file.errorString())); |
|
74 |
return; |
|
75 |
} |
|
76 |
||
77 |
QByteArray data = file.readAll(); |
|
78 |
||
79 |
previewForm->setEncodedData(data); |
|
80 |
if (previewForm->exec()) |
|
81 |
textEdit->setPlainText(previewForm->decodedString()); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
void MainWindow::save() |
|
86 |
{ |
|
87 |
QString fileName = QFileDialog::getSaveFileName(this); |
|
88 |
if (!fileName.isEmpty()) { |
|
89 |
QFile file(fileName); |
|
90 |
if (!file.open(QFile::WriteOnly | QFile::Text)) { |
|
91 |
QMessageBox::warning(this, tr("Codecs"), |
|
92 |
tr("Cannot write file %1:\n%2") |
|
93 |
.arg(fileName) |
|
94 |
.arg(file.errorString())); |
|
95 |
return; |
|
96 |
} |
|
97 |
||
98 |
QAction *action = qobject_cast<QAction *>(sender()); |
|
99 |
QByteArray codecName = action->data().toByteArray(); |
|
100 |
||
101 |
QTextStream out(&file); |
|
102 |
out.setCodec(codecName); |
|
103 |
out << textEdit->toPlainText(); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
void MainWindow::about() |
|
108 |
{ |
|
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.")); |
|
112 |
} |
|
113 |
||
114 |
void MainWindow::aboutToShowSaveAsMenu() |
|
115 |
{ |
|
116 |
QString currentText = textEdit->toPlainText(); |
|
117 |
||
118 |
foreach (QAction *action, saveAsActs) { |
|
119 |
QByteArray codecName = action->data().toByteArray(); |
|
120 |
QTextCodec *codec = QTextCodec::codecForName(codecName); |
|
121 |
action->setVisible(codec && codec->canEncode(currentText)); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
void MainWindow::findCodecs() |
|
126 |
{ |
|
127 |
QMap<QString, QTextCodec *> codecMap; |
|
128 |
QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*"); |
|
129 |
||
130 |
foreach (int mib, QTextCodec::availableMibs()) { |
|
131 |
QTextCodec *codec = QTextCodec::codecForMib(mib); |
|
132 |
||
133 |
QString sortKey = codec->name().toUpper(); |
|
134 |
int rank; |
|
135 |
||
136 |
if (sortKey.startsWith("UTF-8")) { |
|
137 |
rank = 1; |
|
138 |
} else if (sortKey.startsWith("UTF-16")) { |
|
139 |
rank = 2; |
|
140 |
} else if (iso8859RegExp.exactMatch(sortKey)) { |
|
141 |
if (iso8859RegExp.cap(1).size() == 1) |
|
142 |
rank = 3; |
|
143 |
else |
|
144 |
rank = 4; |
|
145 |
} else { |
|
146 |
rank = 5; |
|
147 |
} |
|
148 |
sortKey.prepend(QChar('0' + rank)); |
|
149 |
||
150 |
codecMap.insert(sortKey, codec); |
|
151 |
} |
|
152 |
codecs = codecMap.values(); |
|
153 |
} |
|
154 |
||
155 |
void MainWindow::createActions() |
|
156 |
{ |
|
157 |
openAct = new QAction(tr("&Open..."), this); |
|
158 |
openAct->setShortcuts(QKeySequence::Open); |
|
159 |
connect(openAct, SIGNAL(triggered()), this, SLOT(open())); |
|
160 |
||
161 |
foreach (QTextCodec *codec, codecs) { |
|
162 |
QString text = tr("%1...").arg(QString(codec->name())); |
|
163 |
||
164 |
QAction *action = new QAction(text, this); |
|
165 |
action->setData(codec->name()); |
|
166 |
connect(action, SIGNAL(triggered()), this, SLOT(save())); |
|
167 |
saveAsActs.append(action); |
|
168 |
} |
|
169 |
||
170 |
exitAct = new QAction(tr("E&xit"), this); |
|
171 |
exitAct->setShortcuts(QKeySequence::Quit); |
|
172 |
connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); |
|
173 |
||
174 |
aboutAct = new QAction(tr("&About"), this); |
|
175 |
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); |
|
176 |
||
177 |
aboutQtAct = new QAction(tr("About &Qt"), this); |
|
178 |
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
|
179 |
} |
|
180 |
||
181 |
void MainWindow::createMenus() |
|
182 |
{ |
|
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())); |
|
188 |
||
189 |
fileMenu = new QMenu(tr("&File"), this); |
|
190 |
fileMenu->addAction(openAct); |
|
191 |
fileMenu->addMenu(saveAsMenu); |
|
192 |
fileMenu->addSeparator(); |
|
193 |
fileMenu->addAction(exitAct); |
|
194 |
||
195 |
helpMenu = new QMenu(tr("&Help"), this); |
|
196 |
helpMenu->addAction(aboutAct); |
|
197 |
helpMenu->addAction(aboutQtAct); |
|
198 |
||
199 |
menuBar()->addMenu(fileMenu); |
|
200 |
menuBar()->addSeparator(); |
|
201 |
menuBar()->addMenu(helpMenu); |
|
202 |
} |