source: trunk/doc/src/snippets/textdocument-selections/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: 6.4 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("&Open..."), this, SLOT(openFile()),
51 QKeySequence(tr("Ctrl+O", "File|Open")));
52
53 QAction *quitAction = fileMenu->addAction(tr("E&xit"), this, SLOT(close()));
54 quitAction->setShortcut(tr("Ctrl+Q"));
55
56 QMenu *editMenu = new QMenu(tr("&Edit"));
57
58 cutAction = editMenu->addAction(tr("Cu&t"), this, SLOT(cutSelection()));
59 cutAction->setShortcut(tr("Ctrl+X"));
60 cutAction->setEnabled(false);
61
62 copyAction = editMenu->addAction(tr("&Copy"), this, SLOT(copySelection()));
63 copyAction->setShortcut(tr("Ctrl+C"));
64 copyAction->setEnabled(false);
65
66 pasteAction = editMenu->addAction(tr("&Paste"), this, SLOT(pasteSelection()));
67 pasteAction->setShortcut(tr("Ctrl+V"));
68 pasteAction->setEnabled(false);
69
70 QMenu *selectMenu = new QMenu(tr("&Select"));
71 selectMenu->addAction(tr("&Word"), this, SLOT(selectWord()));
72 selectMenu->addAction(tr("&Line"), this, SLOT(selectLine()));
73 selectMenu->addAction(tr("&Block"), this, SLOT(selectBlock()));
74 selectMenu->addAction(tr("&Frame"), this, SLOT(selectFrame()));
75
76 menuBar()->addMenu(fileMenu);
77 menuBar()->addMenu(editMenu);
78 menuBar()->addMenu(selectMenu);
79
80 editor = new QTextEdit(this);
81 document = new QTextDocument(this);
82 editor->setDocument(document);
83
84 connect(editor, SIGNAL(selectionChanged()), this, SLOT(updateMenus()));
85
86 setCentralWidget(editor);
87 setWindowTitle(tr("Text Document Writer"));
88}
89
90void MainWindow::openFile()
91{
92 QString fileName = QFileDialog::getOpenFileName(this,
93 tr("Open file"), currentFile, "HTML files (*.html);;Text files (*.txt)");
94
95 if (!fileName.isEmpty()) {
96 QFileInfo info(fileName);
97 if (info.completeSuffix() == "html") {
98 QFile file(fileName);
99
100 if (file.open(QFile::ReadOnly)) {
101 editor->setHtml(QString(file.readAll()));
102 file.close();
103 currentFile = fileName;
104 }
105 } else if (info.completeSuffix() == "txt") {
106 QFile file(fileName);
107
108 if (file.open(QFile::ReadOnly)) {
109 editor->setPlainText(file.readAll());
110 file.close();
111 currentFile = fileName;
112 }
113 }
114 }
115}
116
117void MainWindow::cutSelection()
118{
119 QTextCursor cursor = editor->textCursor();
120 if (cursor.hasSelection()) {
121 selection = cursor.selection();
122 cursor.removeSelectedText();
123 }
124}
125
126void MainWindow::copySelection()
127{
128 QTextCursor cursor = editor->textCursor();
129 if (cursor.hasSelection()) {
130 selection = cursor.selection();
131 cursor.clearSelection();
132 }
133}
134
135void MainWindow::pasteSelection()
136{
137 QTextCursor cursor = editor->textCursor();
138 cursor.insertFragment(selection);
139}
140
141void MainWindow::selectWord()
142{
143 QTextCursor cursor = editor->textCursor();
144 QTextBlock block = cursor.block();
145
146//! [0]
147 cursor.beginEditBlock();
148//! [1]
149 cursor.movePosition(QTextCursor::StartOfWord);
150 cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
151//! [1]
152 cursor.endEditBlock();
153//! [0]
154
155 editor->setTextCursor(cursor);
156}
157
158void MainWindow::selectLine()
159{
160 QTextCursor cursor = editor->textCursor();
161 QTextBlock block = cursor.block();
162
163 cursor.beginEditBlock();
164 cursor.movePosition(QTextCursor::StartOfLine);
165 cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
166 cursor.endEditBlock();
167
168 editor->setTextCursor(cursor);
169}
170
171void MainWindow::selectBlock()
172{
173 QTextCursor cursor = editor->textCursor();
174 QTextBlock block = cursor.block();
175
176 cursor.beginEditBlock();
177 cursor.movePosition(QTextCursor::StartOfBlock);
178 cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
179 cursor.endEditBlock();
180
181 editor->setTextCursor(cursor);
182}
183
184void MainWindow::selectFrame()
185{
186 QTextCursor cursor = editor->textCursor();
187 QTextFrame *frame = cursor.currentFrame();
188
189 cursor.beginEditBlock();
190 cursor.setPosition(frame->firstPosition());
191 cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor);
192 cursor.endEditBlock();
193
194 editor->setTextCursor(cursor);
195}
196
197void MainWindow::updateMenus()
198{
199 QTextCursor cursor = editor->textCursor();
200 cutAction->setEnabled(cursor.hasSelection());
201 copyAction->setEnabled(cursor.hasSelection());
202
203 pasteAction->setEnabled(!selection.isEmpty());
204}
Note: See TracBrowser for help on using the repository browser.