source: trunk/doc/src/snippets/textdocument-lists/mainwindow.cpp@ 568

Last change on this file since 568 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 5.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 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:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "mainwindow.h"
45
46MainWindow::MainWindow()
47{
48 QMenu *fileMenu = new QMenu(tr("&File"));
49
50 fileMenu->addAction(tr("E&xit"), this, SLOT(close()),
51 QKeySequence(tr("Ctrl+Q", "File|Exit")));
52
53 QMenu *editMenu = new QMenu(tr("&Edit"));
54
55 cutAction = editMenu->addAction(tr("Cu&t"), this, SLOT(cutSelection()),
56 QKeySequence(tr("Ctrl+X", "Edit|Cut")));
57 copyAction = editMenu->addAction(tr("&Copy"), this, SLOT(copySelection()),
58 QKeySequence(tr("Ctrl+C", "Edit|Copy")));
59 pasteAction = editMenu->addAction(tr("&Paste"), this,
60 SLOT(pasteSelection()), QKeySequence(tr("Ctrl+V", "Edit|Paste")));
61
62 QMenu *selectMenu = new QMenu(tr("&Select"));
63 selectMenu->addAction(tr("&Word"), this, SLOT(selectWord()));
64 selectMenu->addAction(tr("&Line"), this, SLOT(selectLine()));
65 selectMenu->addAction(tr("&Block"), this, SLOT(selectBlock()));
66 selectMenu->addAction(tr("&Frame"), this, SLOT(selectFrame()));
67
68 QMenu *insertMenu = new QMenu(tr("&Insert"));
69
70 insertMenu->addAction(tr("&List"), this, SLOT(insertList()),
71 QKeySequence(tr("Ctrl+L", "Insert|List")));
72
73 menuBar()->addMenu(fileMenu);
74 menuBar()->addMenu(editMenu);
75 menuBar()->addMenu(selectMenu);
76 menuBar()->addMenu(insertMenu);
77
78 editor = new QTextEdit(this);
79 document = new QTextDocument(this);
80 editor->setDocument(document);
81
82 connect(editor, SIGNAL(selectionChanged()), this, SLOT(updateMenus()));
83
84 updateMenus();
85
86 setCentralWidget(editor);
87 setWindowTitle(tr("Text Document Writer"));
88}
89
90void MainWindow::cutSelection()
91{
92 QTextCursor cursor = editor->textCursor();
93 if (cursor.hasSelection()) {
94 selection = cursor.selection();
95 cursor.removeSelectedText();
96 }
97}
98
99void MainWindow::copySelection()
100{
101 QTextCursor cursor = editor->textCursor();
102 if (cursor.hasSelection()) {
103 selection = cursor.selection();
104 cursor.clearSelection();
105 }
106}
107
108void MainWindow::pasteSelection()
109{
110 QTextCursor cursor = editor->textCursor();
111 cursor.insertFragment(selection);
112}
113
114void MainWindow::selectWord()
115{
116 QTextCursor cursor = editor->textCursor();
117 QTextBlock block = cursor.block();
118
119 cursor.beginEditBlock();
120 cursor.movePosition(QTextCursor::StartOfWord);
121 cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
122 cursor.endEditBlock();
123
124 editor->setTextCursor(cursor);
125}
126
127void MainWindow::selectLine()
128{
129 QTextCursor cursor = editor->textCursor();
130 QTextBlock block = cursor.block();
131
132 cursor.beginEditBlock();
133 cursor.movePosition(QTextCursor::StartOfLine);
134 cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
135 cursor.endEditBlock();
136
137 editor->setTextCursor(cursor);
138}
139
140void MainWindow::selectBlock()
141{
142 QTextCursor cursor = editor->textCursor();
143 QTextBlock block = cursor.block();
144
145 cursor.beginEditBlock();
146 cursor.movePosition(QTextCursor::StartOfBlock);
147 cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
148 cursor.endEditBlock();
149
150 editor->setTextCursor(cursor);
151}
152
153void MainWindow::selectFrame()
154{
155 QTextCursor cursor = editor->textCursor();
156 QTextFrame *frame = cursor.currentFrame();
157
158 cursor.beginEditBlock();
159 cursor.setPosition(frame->firstPosition());
160 cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor);
161 cursor.endEditBlock();
162
163 editor->setTextCursor(cursor);
164}
165
166void MainWindow::insertList()
167{
168 QTextCursor cursor = editor->textCursor();
169 cursor.beginEditBlock();
170
171 QTextList *list = cursor.currentList();
172//! [0]
173 QTextListFormat listFormat;
174 if (list) {
175 listFormat = list->format();
176 listFormat.setIndent(listFormat.indent() + 1);
177 }
178
179 listFormat.setStyle(QTextListFormat::ListDisc);
180 cursor.insertList(listFormat);
181//! [0]
182
183 cursor.endEditBlock();
184}
185
186void MainWindow::updateMenus()
187{
188 QTextCursor cursor = editor->textCursor();
189 cutAction->setEnabled(cursor.hasSelection());
190 copyAction->setEnabled(cursor.hasSelection());
191
192 pasteAction->setEnabled(!selection.isEmpty());
193}
Note: See TracBrowser for help on using the repository browser.