doc/src/snippets/textdocument-lists/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("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 
       
    90 void MainWindow::cutSelection()
       
    91 {
       
    92     QTextCursor cursor = editor->textCursor();
       
    93     if (cursor.hasSelection()) {
       
    94         selection = cursor.selection();
       
    95         cursor.removeSelectedText();
       
    96     }
       
    97 }
       
    98 
       
    99 void MainWindow::copySelection()
       
   100 {
       
   101     QTextCursor cursor = editor->textCursor();
       
   102     if (cursor.hasSelection()) {
       
   103         selection = cursor.selection();
       
   104         cursor.clearSelection();
       
   105     }
       
   106 }
       
   107 
       
   108 void MainWindow::pasteSelection()
       
   109 {
       
   110     QTextCursor cursor = editor->textCursor();
       
   111     cursor.insertFragment(selection);
       
   112 }
       
   113 
       
   114 void 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 
       
   127 void 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 
       
   140 void 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 
       
   153 void 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 
       
   166 void 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 
       
   186 void 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 }