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 
MainWindow()55 MainWindow::MainWindow()
56 {
57     QMenu *fileMenu = new QMenu(tr("&File"));
58 
59     fileMenu->addAction(tr("E&xit"), this, SLOT(close()),
60         QKeySequence(tr("Ctrl+Q", "File|Exit")));
61 
62     QMenu *editMenu = new QMenu(tr("&Edit"));
63 
64     cutAction = editMenu->addAction(tr("Cu&t"), this, SLOT(cutSelection()),
65         QKeySequence(tr("Ctrl+X", "Edit|Cut")));
66     copyAction = editMenu->addAction(tr("&Copy"), this, SLOT(copySelection()),
67         QKeySequence(tr("Ctrl+C", "Edit|Copy")));
68     pasteAction = editMenu->addAction(tr("&Paste"), this,
69         SLOT(pasteSelection()), QKeySequence(tr("Ctrl+V", "Edit|Paste")));
70 
71     QMenu *selectMenu = new QMenu(tr("&Select"));
72     selectMenu->addAction(tr("&Word"), this, SLOT(selectWord()));
73     selectMenu->addAction(tr("&Line"), this, SLOT(selectLine()));
74     selectMenu->addAction(tr("&Block"), this, SLOT(selectBlock()));
75     selectMenu->addAction(tr("&Frame"), this, SLOT(selectFrame()));
76 
77     QMenu *insertMenu = new QMenu(tr("&Insert"));
78 
79     insertMenu->addAction(tr("&List"), this, SLOT(insertList()),
80         QKeySequence(tr("Ctrl+L", "Insert|List")));
81 
82     menuBar()->addMenu(fileMenu);
83     menuBar()->addMenu(editMenu);
84     menuBar()->addMenu(selectMenu);
85     menuBar()->addMenu(insertMenu);
86 
87     editor = new QTextEdit(this);
88     document = new QTextDocument(this);
89     editor->setDocument(document);
90 
91     connect(editor, &QTextEdit::selectionChanged,
92             this, &MainWindow::updateMenus);
93 
94     updateMenus();
95 
96     setCentralWidget(editor);
97     setWindowTitle(tr("Text Document Writer"));
98 }
99 
cutSelection()100 void MainWindow::cutSelection()
101 {
102     QTextCursor cursor = editor->textCursor();
103     if (cursor.hasSelection()) {
104         selection = cursor.selection();
105         cursor.removeSelectedText();
106     }
107 }
108 
copySelection()109 void MainWindow::copySelection()
110 {
111     QTextCursor cursor = editor->textCursor();
112     if (cursor.hasSelection()) {
113         selection = cursor.selection();
114         cursor.clearSelection();
115     }
116 }
117 
pasteSelection()118 void MainWindow::pasteSelection()
119 {
120     QTextCursor cursor = editor->textCursor();
121     cursor.insertFragment(selection);
122 }
123 
selectWord()124 void MainWindow::selectWord()
125 {
126     QTextCursor cursor = editor->textCursor();
127     QTextBlock block = cursor.block();
128 
129     cursor.beginEditBlock();
130     cursor.movePosition(QTextCursor::StartOfWord);
131     cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
132     cursor.endEditBlock();
133 
134     editor->setTextCursor(cursor);
135 }
136 
selectLine()137 void MainWindow::selectLine()
138 {
139     QTextCursor cursor = editor->textCursor();
140     QTextBlock block = cursor.block();
141 
142     cursor.beginEditBlock();
143     cursor.movePosition(QTextCursor::StartOfLine);
144     cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
145     cursor.endEditBlock();
146 
147     editor->setTextCursor(cursor);
148 }
149 
selectBlock()150 void MainWindow::selectBlock()
151 {
152     QTextCursor cursor = editor->textCursor();
153     QTextBlock block = cursor.block();
154 
155     cursor.beginEditBlock();
156     cursor.movePosition(QTextCursor::StartOfBlock);
157     cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
158     cursor.endEditBlock();
159 
160     editor->setTextCursor(cursor);
161 }
162 
selectFrame()163 void MainWindow::selectFrame()
164 {
165     QTextCursor cursor = editor->textCursor();
166     QTextFrame *frame = cursor.currentFrame();
167 
168     cursor.beginEditBlock();
169     cursor.setPosition(frame->firstPosition());
170     cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor);
171     cursor.endEditBlock();
172 
173     editor->setTextCursor(cursor);
174 }
175 
insertList()176 void MainWindow::insertList()
177 {
178     QTextCursor cursor = editor->textCursor();
179     cursor.beginEditBlock();
180 
181     QTextList *list = cursor.currentList();
182 //! [0]
183     QTextListFormat listFormat;
184     if (list) {
185         listFormat = list->format();
186         listFormat.setIndent(listFormat.indent() + 1);
187     }
188 
189     listFormat.setStyle(QTextListFormat::ListDisc);
190     cursor.insertList(listFormat);
191 //! [0]
192 
193     cursor.endEditBlock();
194 }
195 
updateMenus()196 void MainWindow::updateMenus()
197 {
198     QTextCursor cursor = editor->textCursor();
199     cutAction->setEnabled(cursor.hasSelection());
200     copyAction->setEnabled(cursor.hasSelection());
201 
202     pasteAction->setEnabled(!selection.isEmpty());
203 }
204