Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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")),
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();
50 for (int index = 0; index < list->count(); ++index) {
51 QTextBlock listItem = list->item(index);
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,
60 itemCursor.setBlockFormat(newBlockFormat);
61 /*
63 processListItem(listItem);
65 */
67 }
69 cursor.endEditBlock();
70}
71
73{
74 QTextCursor cursor = editor->textCursor();
75 QTextFrame *frame = cursor.currentFrame();
76
77 if (!frame)
78 return;
79
82 QStringList headerLabels;
83 headerLabels << tr("Lists");
84 treeWidget->setHeaderLabels(headerLabels);
85
86 QTreeWidgetItem *parentItem = nullptr;
88 QTreeWidgetItem *lastItem = nullptr;
89 parentItems.clear();
90 previousItems.clear();
91
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);
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 /*
133 processListItem(list, index);
135 */
137 }
139 }
141 }
143
144 treeWidget->setWindowTitle(tr("List Contents"));
145 treeWidget->show();
146}
147
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
159 listFormat.setIndent(listFormat.indent() + 1);
160 cursor.insertList(listFormat);
161
162 cursor.endEditBlock();
163}
void highlightListItems()
void showList()
void insertList()
The QKeySequence class encapsulates a key sequence as used by shortcuts.
void pop_back() noexcept
Definition qlist.h:679
T & last()
Definition qlist.h:648
value_type takeLast()
Definition qlist.h:567
qsizetype count() const noexcept
Definition qlist.h:398
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
void setCentralWidget(QWidget *widget)
Sets the given widget to be the main window's central widget.
QAction * addMenu(QMenu *menu)
Appends menu to the menu bar.
Definition qmenubar.cpp:768
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3117
\inmodule QtCore
\reentrant
bool isValid() const
Returns true if this text block is valid; otherwise returns false.
QString text() const
Returns the block's contents as plain text.
QTextList * textList() const
If the block represents a list item, returns the list that the item belongs to; otherwise returns \nu...
\reentrant \inmodule QtGui
Definition qtextcursor.h:30
\reentrant \inmodule QtGui
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition qtextedit.h:27
QTextCursor textCursor() const
Returns a copy of the QTextCursor that represents the currently visible cursor.
void setDocument(QTextDocument *document)
void setBackground(const QBrush &brush)
Sets the brush use to paint the document's background to the brush specified.
\reentrant
Definition qtextobject.h:81
void setStyle(Style style)
Sets the list format's style.
void setIndent(int indent)
Sets the list format's indentation.
int indent() const
Returns the list format's indentation.
\reentrant
Definition qtextlist.h:18
The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class.
Definition qtreewidget.h:23
The QTreeWidget class provides a tree view that uses a predefined tree model.
void setColumnCount(int columns)
void setHeaderLabels(const QStringList &labels)
Adds a column in the header for each item in the labels list, and sets the label for each column.
bool close()
Closes this widget.
Definition qwidget.cpp:8562
void setWindowTitle(const QString &)
Definition qwidget.cpp:6105
QCursor cursor
the cursor shape for this widget
Definition qwidget.h:135
QSet< QString >::iterator it
@ lightGray
Definition qnamespace.h:34
GLuint index
[2]
#define tr(X)
QList< int > list
[14]
QGraphicsItem * item
QTreeWidget * treeWidget
[0]
QFrame frame
[0]
QMenuBar * menuBar
[0]