[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 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.
|
---|
[2] | 25 | **
|
---|
[846] | 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."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "mainwindow.h"
|
---|
| 44 | #include "xmlwriter.h"
|
---|
| 45 |
|
---|
| 46 | MainWindow::MainWindow()
|
---|
| 47 | {
|
---|
| 48 | QMenu *fileMenu = new QMenu(tr("&File"));
|
---|
| 49 |
|
---|
| 50 | QAction *saveAction = fileMenu->addAction(tr("&Save..."));
|
---|
| 51 | saveAction->setShortcut(tr("Ctrl+S"));
|
---|
| 52 |
|
---|
| 53 | QAction *quitAction = fileMenu->addAction(tr("E&xit"));
|
---|
| 54 | quitAction->setShortcut(tr("Ctrl+Q"));
|
---|
| 55 |
|
---|
| 56 | menuBar()->addMenu(fileMenu);
|
---|
| 57 | editor = new QTextEdit();
|
---|
| 58 |
|
---|
| 59 | QTextCursor cursor(editor->textCursor());
|
---|
| 60 | cursor.movePosition(QTextCursor::Start);
|
---|
| 61 |
|
---|
| 62 | QTextFrame *mainFrame = cursor.currentFrame();
|
---|
| 63 |
|
---|
| 64 | QTextCharFormat plainCharFormat;
|
---|
| 65 | QTextCharFormat boldCharFormat;
|
---|
| 66 | boldCharFormat.setFontWeight(QFont::Bold);
|
---|
| 67 | /* main frame
|
---|
| 68 | //! [0]
|
---|
| 69 | QTextFrame *mainFrame = cursor.currentFrame();
|
---|
| 70 | cursor.insertText(...);
|
---|
| 71 | //! [0]
|
---|
| 72 | */
|
---|
| 73 | cursor.insertText("Text documents are represented by the "
|
---|
| 74 | "QTextDocument class, rather than by QString objects. "
|
---|
| 75 | "Each QTextDocument object contains information about "
|
---|
| 76 | "the document's internal representation, its structure, "
|
---|
| 77 | "and keeps track of modifications to provide undo/redo "
|
---|
| 78 | "facilities. This approach allows features such as the "
|
---|
| 79 | "layout management to be delegated to specialized "
|
---|
| 80 | "classes, but also provides a focus for the framework.",
|
---|
| 81 | plainCharFormat);
|
---|
| 82 |
|
---|
| 83 | //! [1]
|
---|
| 84 | QTextFrameFormat frameFormat;
|
---|
| 85 | frameFormat.setMargin(32);
|
---|
| 86 | frameFormat.setPadding(8);
|
---|
| 87 | frameFormat.setBorder(4);
|
---|
| 88 | //! [1]
|
---|
| 89 | cursor.insertFrame(frameFormat);
|
---|
| 90 |
|
---|
| 91 | /* insert frame
|
---|
| 92 | //! [2]
|
---|
| 93 | cursor.insertFrame(frameFormat);
|
---|
| 94 | cursor.insertText(...);
|
---|
| 95 | //! [2]
|
---|
| 96 | */
|
---|
| 97 | cursor.insertText("Documents are either converted from external sources "
|
---|
| 98 | "or created from scratch using Qt. The creation process "
|
---|
| 99 | "can done by an editor widget, such as QTextEdit, or by "
|
---|
| 100 | "explicit calls to the Scribe API.", boldCharFormat);
|
---|
| 101 |
|
---|
| 102 | cursor = mainFrame->lastCursorPosition();
|
---|
| 103 | /* last cursor
|
---|
| 104 | //! [3]
|
---|
| 105 | cursor = mainFrame->lastCursorPosition();
|
---|
| 106 | cursor.insertText(...);
|
---|
| 107 | //! [3]
|
---|
| 108 | */
|
---|
| 109 | cursor.insertText("There are two complementary ways to visualize the "
|
---|
| 110 | "contents of a document: as a linear buffer that is "
|
---|
| 111 | "used by editors to modify the contents, and as an "
|
---|
| 112 | "object hierarchy containing structural information "
|
---|
| 113 | "that is useful to layout engines. In the hierarchical "
|
---|
| 114 | "model, the objects generally correspond to visual "
|
---|
| 115 | "elements such as frames, tables, and lists. At a lower "
|
---|
| 116 | "level, these elements describe properties such as the "
|
---|
| 117 | "style of text used and its alignment. The linear "
|
---|
| 118 | "representation of the document is used for editing and "
|
---|
| 119 | "manipulation of the document's contents.",
|
---|
| 120 | plainCharFormat);
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
|
---|
| 124 | connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
|
---|
| 125 |
|
---|
| 126 | setCentralWidget(editor);
|
---|
| 127 | setWindowTitle(tr("Text Document Frames"));
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void MainWindow::saveFile()
|
---|
| 131 | {
|
---|
| 132 | QString fileName = QFileDialog::getSaveFileName(this,
|
---|
| 133 | tr("Save document as:"), "", tr("XML (*.xml)"));
|
---|
| 134 |
|
---|
| 135 | if (!fileName.isEmpty()) {
|
---|
| 136 | if (writeXml(fileName))
|
---|
| 137 | setWindowTitle(fileName);
|
---|
| 138 | else
|
---|
| 139 | QMessageBox::warning(this, tr("Warning"),
|
---|
| 140 | tr("Failed to save the document."), QMessageBox::Cancel,
|
---|
| 141 | QMessageBox::NoButton);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | bool MainWindow::writeXml(const QString &fileName)
|
---|
| 145 | {
|
---|
| 146 | XmlWriter documentWriter(editor->document());
|
---|
| 147 |
|
---|
| 148 | QDomDocument *domDocument = documentWriter.toXml();
|
---|
| 149 | QFile file(fileName);
|
---|
| 150 |
|
---|
| 151 | if (file.open(QFile::WriteOnly)) {
|
---|
| 152 | QTextStream textStream(&file);
|
---|
| 153 | textStream.setCodec(QTextCodec::codecForName("UTF-8"));
|
---|
| 154 |
|
---|
| 155 | textStream << domDocument->toString(1).toUtf8();
|
---|
| 156 | file.close();
|
---|
| 157 | return true;
|
---|
| 158 | }
|
---|
| 159 | else
|
---|
| 160 | return false;
|
---|
| 161 | }
|
---|