1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include <QtWidgets>
52
53 #include "mainwindow.h"
54 #include "xmlwriter.h"
55
MainWindow()56 MainWindow::MainWindow()
57 {
58 QMenu *fileMenu = new QMenu(tr("&File"));
59
60 QAction *saveAction = fileMenu->addAction(tr("&Save..."));
61 saveAction->setShortcut(tr("Ctrl+S"));
62
63 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
64 quitAction->setShortcut(tr("Ctrl+Q"));
65
66 menuBar()->addMenu(fileMenu);
67 editor = new QTextEdit;
68
69 QTextCursor cursor(editor->textCursor());
70 cursor.movePosition(QTextCursor::Start);
71
72 QTextFrame *mainFrame = cursor.currentFrame();
73
74 QTextCharFormat plainCharFormat;
75 QTextCharFormat boldCharFormat;
76 boldCharFormat.setFontWeight(QFont::Bold);
77 /* main frame
78 //! [0]
79 QTextFrame *mainFrame = cursor.currentFrame();
80 cursor.insertText(...);
81 //! [0]
82 */
83 cursor.insertText("Text documents are represented by the "
84 "QTextDocument class, rather than by QString objects. "
85 "Each QTextDocument object contains information about "
86 "the document's internal representation, its structure, "
87 "and keeps track of modifications to provide undo/redo "
88 "facilities. This approach allows features such as the "
89 "layout management to be delegated to specialized "
90 "classes, but also provides a focus for the framework.",
91 plainCharFormat);
92
93 //! [1]
94 QTextFrameFormat frameFormat;
95 frameFormat.setMargin(32);
96 frameFormat.setPadding(8);
97 frameFormat.setBorder(4);
98 //! [1]
99 cursor.insertFrame(frameFormat);
100
101 /* insert frame
102 //! [2]
103 cursor.insertFrame(frameFormat);
104 cursor.insertText(...);
105 //! [2]
106 */
107 cursor.insertText("Documents are either converted from external sources "
108 "or created from scratch using Qt. The creation process "
109 "can done by an editor widget, such as QTextEdit, or by "
110 "explicit calls to the Scribe API.", boldCharFormat);
111
112 cursor = mainFrame->lastCursorPosition();
113 /* last cursor
114 //! [3]
115 cursor = mainFrame->lastCursorPosition();
116 cursor.insertText(...);
117 //! [3]
118 */
119 cursor.insertText("There are two complementary ways to visualize the "
120 "contents of a document: as a linear buffer that is "
121 "used by editors to modify the contents, and as an "
122 "object hierarchy containing structural information "
123 "that is useful to layout engines. In the hierarchical "
124 "model, the objects generally correspond to visual "
125 "elements such as frames, tables, and lists. At a lower "
126 "level, these elements describe properties such as the "
127 "style of text used and its alignment. The linear "
128 "representation of the document is used for editing and "
129 "manipulation of the document's contents.",
130 plainCharFormat);
131
132
133 connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);
134 connect(quitAction, &QAction::triggered, this, &MainWindow::close);
135
136 setCentralWidget(editor);
137 setWindowTitle(tr("Text Document Frames"));
138 }
139
saveFile()140 void MainWindow::saveFile()
141 {
142 QString fileName = QFileDialog::getSaveFileName(this,
143 tr("Save document as:"), "", tr("XML (*.xml)"));
144
145 if (!fileName.isEmpty()) {
146 if (writeXml(fileName))
147 setWindowTitle(fileName);
148 else
149 QMessageBox::warning(this, tr("Warning"),
150 tr("Failed to save the document."), QMessageBox::Cancel,
151 QMessageBox::NoButton);
152 }
153 }
writeXml(const QString & fileName)154 bool MainWindow::writeXml(const QString &fileName)
155 {
156 XmlWriter documentWriter(editor->document());
157
158 QDomDocument *domDocument = documentWriter.toXml();
159 QFile file(fileName);
160
161 if (file.open(QFile::WriteOnly)) {
162 QTextStream textStream(&file);
163 textStream.setCodec(QTextCodec::codecForName("UTF-8"));
164
165 textStream << domDocument->toString(1).toUtf8();
166 file.close();
167 return true;
168 }
169 else
170 return false;
171 }
172