1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "mainwindow.h"
|
---|
44 |
|
---|
45 | MainWindow::MainWindow()
|
---|
46 | {
|
---|
47 | QMenu *fileMenu = new QMenu(tr("&File"));
|
---|
48 |
|
---|
49 | fileMenu->addAction(tr("E&xit"), this, SLOT(close()),
|
---|
50 | QKeySequence(tr("Ctrl+Q", "File|Exit")));
|
---|
51 |
|
---|
52 | QMenu *editMenu = new QMenu(tr("&Edit"));
|
---|
53 |
|
---|
54 | cutAction = editMenu->addAction(tr("Cu&t"), this, SLOT(cutSelection()),
|
---|
55 | QKeySequence(tr("Ctrl+X", "Edit|Cut")));
|
---|
56 | copyAction = editMenu->addAction(tr("&Copy"), this, SLOT(copySelection()),
|
---|
57 | QKeySequence(tr("Ctrl+C", "Edit|Copy")));
|
---|
58 | pasteAction = editMenu->addAction(tr("&Paste"), this,
|
---|
59 | SLOT(pasteSelection()), QKeySequence(tr("Ctrl+V", "Edit|Paste")));
|
---|
60 |
|
---|
61 | QMenu *selectMenu = new QMenu(tr("&Select"));
|
---|
62 | selectMenu->addAction(tr("&Word"), this, SLOT(selectWord()));
|
---|
63 | selectMenu->addAction(tr("&Line"), this, SLOT(selectLine()));
|
---|
64 | selectMenu->addAction(tr("&Block"), this, SLOT(selectBlock()));
|
---|
65 | selectMenu->addAction(tr("&Frame"), this, SLOT(selectFrame()));
|
---|
66 |
|
---|
67 | QMenu *insertMenu = new QMenu(tr("&Insert"));
|
---|
68 |
|
---|
69 | insertMenu->addAction(tr("&List"), this, SLOT(insertList()),
|
---|
70 | QKeySequence(tr("Ctrl+L", "Insert|List")));
|
---|
71 |
|
---|
72 | menuBar()->addMenu(fileMenu);
|
---|
73 | menuBar()->addMenu(editMenu);
|
---|
74 | menuBar()->addMenu(selectMenu);
|
---|
75 | menuBar()->addMenu(insertMenu);
|
---|
76 |
|
---|
77 | editor = new QTextEdit(this);
|
---|
78 | document = new QTextDocument(this);
|
---|
79 | editor->setDocument(document);
|
---|
80 |
|
---|
81 | connect(editor, SIGNAL(selectionChanged()), this, SLOT(updateMenus()));
|
---|
82 |
|
---|
83 | updateMenus();
|
---|
84 |
|
---|
85 | setCentralWidget(editor);
|
---|
86 | setWindowTitle(tr("Text Document Writer"));
|
---|
87 | }
|
---|
88 |
|
---|
89 | void MainWindow::cutSelection()
|
---|
90 | {
|
---|
91 | QTextCursor cursor = editor->textCursor();
|
---|
92 | if (cursor.hasSelection()) {
|
---|
93 | selection = cursor.selection();
|
---|
94 | cursor.removeSelectedText();
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | void MainWindow::copySelection()
|
---|
99 | {
|
---|
100 | QTextCursor cursor = editor->textCursor();
|
---|
101 | if (cursor.hasSelection()) {
|
---|
102 | selection = cursor.selection();
|
---|
103 | cursor.clearSelection();
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | void MainWindow::pasteSelection()
|
---|
108 | {
|
---|
109 | QTextCursor cursor = editor->textCursor();
|
---|
110 | cursor.insertFragment(selection);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void MainWindow::selectWord()
|
---|
114 | {
|
---|
115 | QTextCursor cursor = editor->textCursor();
|
---|
116 | QTextBlock block = cursor.block();
|
---|
117 |
|
---|
118 | cursor.beginEditBlock();
|
---|
119 | cursor.movePosition(QTextCursor::StartOfWord);
|
---|
120 | cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
---|
121 | cursor.endEditBlock();
|
---|
122 |
|
---|
123 | editor->setTextCursor(cursor);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void MainWindow::selectLine()
|
---|
127 | {
|
---|
128 | QTextCursor cursor = editor->textCursor();
|
---|
129 | QTextBlock block = cursor.block();
|
---|
130 |
|
---|
131 | cursor.beginEditBlock();
|
---|
132 | cursor.movePosition(QTextCursor::StartOfLine);
|
---|
133 | cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
|
---|
134 | cursor.endEditBlock();
|
---|
135 |
|
---|
136 | editor->setTextCursor(cursor);
|
---|
137 | }
|
---|
138 |
|
---|
139 | void MainWindow::selectBlock()
|
---|
140 | {
|
---|
141 | QTextCursor cursor = editor->textCursor();
|
---|
142 | QTextBlock block = cursor.block();
|
---|
143 |
|
---|
144 | cursor.beginEditBlock();
|
---|
145 | cursor.movePosition(QTextCursor::StartOfBlock);
|
---|
146 | cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
---|
147 | cursor.endEditBlock();
|
---|
148 |
|
---|
149 | editor->setTextCursor(cursor);
|
---|
150 | }
|
---|
151 |
|
---|
152 | void MainWindow::selectFrame()
|
---|
153 | {
|
---|
154 | QTextCursor cursor = editor->textCursor();
|
---|
155 | QTextFrame *frame = cursor.currentFrame();
|
---|
156 |
|
---|
157 | cursor.beginEditBlock();
|
---|
158 | cursor.setPosition(frame->firstPosition());
|
---|
159 | cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor);
|
---|
160 | cursor.endEditBlock();
|
---|
161 |
|
---|
162 | editor->setTextCursor(cursor);
|
---|
163 | }
|
---|
164 |
|
---|
165 | void MainWindow::insertList()
|
---|
166 | {
|
---|
167 | QTextCursor cursor = editor->textCursor();
|
---|
168 | cursor.beginEditBlock();
|
---|
169 |
|
---|
170 | QTextList *list = cursor.currentList();
|
---|
171 | //! [0]
|
---|
172 | QTextListFormat listFormat;
|
---|
173 | if (list) {
|
---|
174 | listFormat = list->format();
|
---|
175 | listFormat.setIndent(listFormat.indent() + 1);
|
---|
176 | }
|
---|
177 |
|
---|
178 | listFormat.setStyle(QTextListFormat::ListDisc);
|
---|
179 | cursor.insertList(listFormat);
|
---|
180 | //! [0]
|
---|
181 |
|
---|
182 | cursor.endEditBlock();
|
---|
183 | }
|
---|
184 |
|
---|
185 | void MainWindow::updateMenus()
|
---|
186 | {
|
---|
187 | QTextCursor cursor = editor->textCursor();
|
---|
188 | cutAction->setEnabled(cursor.hasSelection());
|
---|
189 | copyAction->setEnabled(cursor.hasSelection());
|
---|
190 |
|
---|
191 | pasteAction->setEnabled(!selection.isEmpty());
|
---|
192 | }
|
---|