10 QMenu *fileMenu =
new QMenu(tr(
"&File"));
12 QAction *saveAction = fileMenu->addAction(tr(
"&Save..."));
13 saveAction->setShortcut(tr(
"Ctrl+S"));
15 QAction *quitAction = fileMenu->addAction(tr(
"E&xit"));
16 quitAction->setShortcut(tr(
"Ctrl+Q"));
18 menuBar()->addMenu(fileMenu);
19 editor =
new QTextEdit;
22 QTextDocument *editorDocument = editor->document();
23 QTextFrame *root = editorDocument->rootFrame();
27 QTextCursor cursor(editor->textCursor());
28 cursor.movePosition(QTextCursor::Start);
30 QTextFrame *mainFrame = cursor.currentFrame();
32 QTextCharFormat plainCharFormat;
33 QTextCharFormat boldCharFormat;
34 boldCharFormat.setFontWeight(QFont::Bold);
36
37
38
39
40
41 cursor.insertText(
"Text documents are represented by the "
42 "QTextDocument class, rather than by QString objects. "
43 "Each QTextDocument object contains information about "
44 "the document's internal representation, its structure, "
45 "and keeps track of modifications to provide undo/redo "
46 "facilities. This approach allows features such as the "
47 "layout management to be delegated to specialized "
48 "classes, but also provides a focus for the framework.",
52 QTextFrameFormat frameFormat;
53 frameFormat.setMargin(32);
54 frameFormat.setPadding(8);
55 frameFormat.setBorder(4);
57 cursor.insertFrame(frameFormat);
60
61
62
63
64
65 cursor.insertText(
"Documents are either converted from external sources "
66 "or created from scratch using Qt. The creation process "
67 "can done by an editor widget, such as QTextEdit, or by "
68 "explicit calls to the Scribe API.", boldCharFormat);
70 cursor = mainFrame->lastCursorPosition();
72
73
74
75
76
77 cursor.insertText(
"There are two complementary ways to visualize the "
78 "contents of a document: as a linear buffer that is "
79 "used by editors to modify the contents, and as an "
80 "object hierarchy containing structural information "
81 "that is useful to layout engines. In the hierarchical "
82 "model, the objects generally correspond to visual "
83 "elements such as frames, tables, and lists. At a lower "
84 "level, these elements describe properties such as the "
85 "style of text used and its alignment. The linear "
86 "representation of the document is used for editing and "
87 "manipulation of the document's contents.",
91 connect(saveAction, &QAction::triggered,
this, &MainWindow::saveFile);
92 connect(quitAction, &QAction::triggered,
this, &MainWindow::close);
94 setCentralWidget(editor);
95 setWindowTitle(tr(
"Text Document Frames"));
100 QString fileName = QFileDialog::getSaveFileName(
this,
101 tr(
"Save document as:"),
"", tr(
"XML (*.xml)"));
103 if (!fileName.isEmpty()) {
104 if (writeXml(fileName))
105 setWindowTitle(fileName);
107 QMessageBox::warning(
this, tr(
"Warning"),
108 tr(
"Failed to save the document."), QMessageBox::Cancel,
109 QMessageBox::NoButton);
117void MainWindow::processFrame(QTextFrame *frame)
120 QTextFrame::iterator it;
121 for (it = frame->begin(); !(it.atEnd()); ++it) {
123 QTextFrame *childFrame = it.currentFrame();
124 QTextBlock childBlock = it.currentBlock();
127 processFrame(childFrame);
128 else if (childBlock.isValid())
129 processBlock(childBlock);