/examples/tools/codecs/mainwindow.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 202 lines · 132 code · 31 blank · 39 comment · 14 complexity · 893817a9e6c8f058ff3fe97631a20c5e MD5 · raw file

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