[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 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.
|
---|
[2] | 25 | **
|
---|
[846] | 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."
|
---|
[2] | 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("&Open..."), this, SLOT(openFile()),
|
---|
| 50 | QKeySequence(tr("Ctrl+O", "File|Open")));
|
---|
| 51 |
|
---|
| 52 | QAction *quitAction = fileMenu->addAction(tr("E&xit"), this, SLOT(close()));
|
---|
| 53 | quitAction->setShortcut(tr("Ctrl+Q"));
|
---|
| 54 |
|
---|
| 55 | QMenu *editMenu = new QMenu(tr("&Edit"));
|
---|
| 56 |
|
---|
| 57 | cutAction = editMenu->addAction(tr("Cu&t"), this, SLOT(cutSelection()));
|
---|
| 58 | cutAction->setShortcut(tr("Ctrl+X"));
|
---|
| 59 | cutAction->setEnabled(false);
|
---|
| 60 |
|
---|
| 61 | copyAction = editMenu->addAction(tr("&Copy"), this, SLOT(copySelection()));
|
---|
| 62 | copyAction->setShortcut(tr("Ctrl+C"));
|
---|
| 63 | copyAction->setEnabled(false);
|
---|
| 64 |
|
---|
| 65 | pasteAction = editMenu->addAction(tr("&Paste"), this, SLOT(pasteSelection()));
|
---|
| 66 | pasteAction->setShortcut(tr("Ctrl+V"));
|
---|
| 67 | pasteAction->setEnabled(false);
|
---|
| 68 |
|
---|
| 69 | QMenu *selectMenu = new QMenu(tr("&Select"));
|
---|
| 70 | selectMenu->addAction(tr("&Word"), this, SLOT(selectWord()));
|
---|
| 71 | selectMenu->addAction(tr("&Line"), this, SLOT(selectLine()));
|
---|
| 72 | selectMenu->addAction(tr("&Block"), this, SLOT(selectBlock()));
|
---|
| 73 | selectMenu->addAction(tr("&Frame"), this, SLOT(selectFrame()));
|
---|
| 74 |
|
---|
| 75 | menuBar()->addMenu(fileMenu);
|
---|
| 76 | menuBar()->addMenu(editMenu);
|
---|
| 77 | menuBar()->addMenu(selectMenu);
|
---|
| 78 |
|
---|
| 79 | editor = new QTextEdit(this);
|
---|
| 80 | document = new QTextDocument(this);
|
---|
| 81 | editor->setDocument(document);
|
---|
| 82 |
|
---|
| 83 | connect(editor, SIGNAL(selectionChanged()), this, SLOT(updateMenus()));
|
---|
| 84 |
|
---|
| 85 | setCentralWidget(editor);
|
---|
| 86 | setWindowTitle(tr("Text Document Writer"));
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void MainWindow::openFile()
|
---|
| 90 | {
|
---|
| 91 | QString fileName = QFileDialog::getOpenFileName(this,
|
---|
| 92 | tr("Open file"), currentFile, "HTML files (*.html);;Text files (*.txt)");
|
---|
| 93 |
|
---|
| 94 | if (!fileName.isEmpty()) {
|
---|
| 95 | QFileInfo info(fileName);
|
---|
| 96 | if (info.completeSuffix() == "html") {
|
---|
| 97 | QFile file(fileName);
|
---|
| 98 |
|
---|
| 99 | if (file.open(QFile::ReadOnly)) {
|
---|
| 100 | editor->setHtml(QString(file.readAll()));
|
---|
| 101 | file.close();
|
---|
| 102 | currentFile = fileName;
|
---|
| 103 | }
|
---|
| 104 | } else if (info.completeSuffix() == "txt") {
|
---|
| 105 | QFile file(fileName);
|
---|
| 106 |
|
---|
| 107 | if (file.open(QFile::ReadOnly)) {
|
---|
| 108 | editor->setPlainText(file.readAll());
|
---|
| 109 | file.close();
|
---|
| 110 | currentFile = fileName;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void MainWindow::cutSelection()
|
---|
| 117 | {
|
---|
| 118 | QTextCursor cursor = editor->textCursor();
|
---|
| 119 | if (cursor.hasSelection()) {
|
---|
| 120 | selection = cursor.selection();
|
---|
| 121 | cursor.removeSelectedText();
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | void MainWindow::copySelection()
|
---|
| 126 | {
|
---|
| 127 | QTextCursor cursor = editor->textCursor();
|
---|
| 128 | if (cursor.hasSelection()) {
|
---|
| 129 | selection = cursor.selection();
|
---|
| 130 | cursor.clearSelection();
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void MainWindow::pasteSelection()
|
---|
| 135 | {
|
---|
| 136 | QTextCursor cursor = editor->textCursor();
|
---|
| 137 | cursor.insertFragment(selection);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void MainWindow::selectWord()
|
---|
| 141 | {
|
---|
| 142 | QTextCursor cursor = editor->textCursor();
|
---|
| 143 | QTextBlock block = cursor.block();
|
---|
| 144 |
|
---|
| 145 | //! [0]
|
---|
| 146 | cursor.beginEditBlock();
|
---|
| 147 | //! [1]
|
---|
| 148 | cursor.movePosition(QTextCursor::StartOfWord);
|
---|
| 149 | cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
---|
| 150 | //! [1]
|
---|
| 151 | cursor.endEditBlock();
|
---|
| 152 | //! [0]
|
---|
| 153 |
|
---|
| 154 | editor->setTextCursor(cursor);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | void MainWindow::selectLine()
|
---|
| 158 | {
|
---|
| 159 | QTextCursor cursor = editor->textCursor();
|
---|
| 160 | QTextBlock block = cursor.block();
|
---|
| 161 |
|
---|
| 162 | cursor.beginEditBlock();
|
---|
| 163 | cursor.movePosition(QTextCursor::StartOfLine);
|
---|
| 164 | cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
|
---|
| 165 | cursor.endEditBlock();
|
---|
| 166 |
|
---|
| 167 | editor->setTextCursor(cursor);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | void MainWindow::selectBlock()
|
---|
| 171 | {
|
---|
| 172 | QTextCursor cursor = editor->textCursor();
|
---|
| 173 | QTextBlock block = cursor.block();
|
---|
| 174 |
|
---|
| 175 | cursor.beginEditBlock();
|
---|
| 176 | cursor.movePosition(QTextCursor::StartOfBlock);
|
---|
| 177 | cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
---|
| 178 | cursor.endEditBlock();
|
---|
| 179 |
|
---|
| 180 | editor->setTextCursor(cursor);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void MainWindow::selectFrame()
|
---|
| 184 | {
|
---|
| 185 | QTextCursor cursor = editor->textCursor();
|
---|
| 186 | QTextFrame *frame = cursor.currentFrame();
|
---|
| 187 |
|
---|
| 188 | cursor.beginEditBlock();
|
---|
| 189 | cursor.setPosition(frame->firstPosition());
|
---|
| 190 | cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor);
|
---|
| 191 | cursor.endEditBlock();
|
---|
| 192 |
|
---|
| 193 | editor->setTextCursor(cursor);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 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 | }
|
---|