doc/src/snippets/textdocument-selections/mainwindow.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "mainwindow.h"
       
    45 
       
    46 MainWindow::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 
       
    90 void 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 
       
   117 void MainWindow::cutSelection()
       
   118 {
       
   119     QTextCursor cursor = editor->textCursor();
       
   120     if (cursor.hasSelection()) {
       
   121         selection = cursor.selection();
       
   122         cursor.removeSelectedText();
       
   123     }
       
   124 }
       
   125 
       
   126 void MainWindow::copySelection()
       
   127 {
       
   128     QTextCursor cursor = editor->textCursor();
       
   129     if (cursor.hasSelection()) {
       
   130         selection = cursor.selection();
       
   131         cursor.clearSelection();
       
   132     }
       
   133 }
       
   134 
       
   135 void MainWindow::pasteSelection()
       
   136 {
       
   137     QTextCursor cursor = editor->textCursor();
       
   138     cursor.insertFragment(selection);
       
   139 }
       
   140 
       
   141 void 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 
       
   158 void 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 
       
   171 void 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 
       
   184 void 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 
       
   197 void 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 }