Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mainwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include "mainwindow.h"
4
5#include <QMenu>
6#include <QMenuBar>
7#include <QTextEdit>
8#include <QTextList>
9#include <QTreeWidget>
10
12{
13 QMenu *fileMenu = new QMenu(tr("&File"));
14
15 fileMenu->addAction(tr("E&xit"), QKeySequence(tr("Ctrl+Q", "File|Exit")),
16 this, &QWidget::close);
17
18 QMenu *actionsMenu = new QMenu(tr("&Actions"));
19 actionsMenu->addAction(tr("&Highlight List Items"),
21 actionsMenu->addAction(tr("&Show Current List"), this, &MainWindow::showList);
22
23 QMenu *insertMenu = new QMenu(tr("&Insert"));
24
25 insertMenu->addAction(tr("&List"), QKeySequence(tr("Ctrl+L", "Insert|List")),
26 this, &MainWindow::insertList);
27
28 menuBar()->addMenu(fileMenu);
29 menuBar()->addMenu(insertMenu);
30 menuBar()->addMenu(actionsMenu);
31
32 editor = new QTextEdit(this);
33 document = new QTextDocument(this);
34 editor->setDocument(document);
35
36 setCentralWidget(editor);
37 setWindowTitle(tr("Text Document List Items"));
38}
39
41{
42 QTextCursor cursor = editor->textCursor();
43 QTextList *list = cursor.currentList();
44
45 if (!list)
46 return;
47
48 cursor.beginEditBlock();
49//! [0]
50 for (int index = 0; index < list->count(); ++index) {
51 QTextBlock listItem = list->item(index);
52//! [0]
53 QTextBlockFormat newBlockFormat = listItem.blockFormat();
54 newBlockFormat.setBackground(Qt::lightGray);
55 QTextCursor itemCursor = cursor;
56 itemCursor.setPosition(listItem.position());
57 //itemCursor.movePosition(QTextCursor::StartOfBlock);
58 itemCursor.movePosition(QTextCursor::EndOfBlock,
59 QTextCursor::KeepAnchor);
60 itemCursor.setBlockFormat(newBlockFormat);
61 /*
62//! [1]
63 processListItem(listItem);
64//! [1]
65 */
66//! [2]
67 }
68//! [2]
69 cursor.endEditBlock();
70}
71
73{
74 QTextCursor cursor = editor->textCursor();
75 QTextFrame *frame = cursor.currentFrame();
76
77 if (!frame)
78 return;
79
80 QTreeWidget *treeWidget = new QTreeWidget;
81 treeWidget->setColumnCount(1);
82 QStringList headerLabels;
83 headerLabels << tr("Lists");
84 treeWidget->setHeaderLabels(headerLabels);
85
86 QTreeWidgetItem *parentItem = nullptr;
87 QTreeWidgetItem *item;
88 QTreeWidgetItem *lastItem = nullptr;
89 parentItems.clear();
90 previousItems.clear();
91
92//! [3]
93 QTextFrame::iterator it;
94 for (it = frame->begin(); !(it.atEnd()); ++it) {
95
96 QTextBlock block = it.currentBlock();
97
98 if (block.isValid()) {
99
100 QTextList *list = block.textList();
101
102 if (list) {
103 int index = list->itemNumber(block);
104//! [3]
105 if (index == 0) {
106 parentItems.append(parentItem);
107 previousItems.append(lastItem);
108 listStructures.append(list);
109 parentItem = lastItem;
110 lastItem = 0;
111
112 if (parentItem != 0)
113 item = new QTreeWidgetItem(parentItem, lastItem);
114 else
115 item = new QTreeWidgetItem(treeWidget, lastItem);
116
117 } else {
118
119 while (parentItem != 0 && listStructures.last() != list) {
120 listStructures.pop_back();
121 parentItem = parentItems.takeLast();
122 lastItem = previousItems.takeLast();
123 }
124 if (parentItem != 0)
125 item = new QTreeWidgetItem(parentItem, lastItem);
126 else
127 item = new QTreeWidgetItem(treeWidget, lastItem);
128 }
129 item->setText(0, block.text());
130 lastItem = item;
131 /*
132//! [4]
133 processListItem(list, index);
134//! [4]
135 */
136//! [5]
137 }
138//! [5] //! [6]
139 }
140//! [6] //! [7]
141 }
142//! [7]
143
144 treeWidget->setWindowTitle(tr("List Contents"));
145 treeWidget->show();
146}
147
148void MainWindow::insertList()
149{
150 QTextCursor cursor = editor->textCursor();
151 cursor.beginEditBlock();
152
153 QTextList *list = cursor.currentList();
154 QTextListFormat listFormat;
155 if (list)
156 listFormat = list->format();
157
158 listFormat.setStyle(QTextListFormat::ListDisc);
159 listFormat.setIndent(listFormat.indent() + 1);
160 cursor.insertList(listFormat);
161
162 cursor.endEditBlock();
163}
void highlightListItems()
void showList()