doc/src/snippets/textdocument-listitems/mainwindow.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 8: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 *actionsMenu = new QMenu(tr("&Actions"));
       
    54     actionsMenu->addAction(tr("&Highlight List Items"),
       
    55                         this, SLOT(highlightListItems()));
       
    56     actionsMenu->addAction(tr("&Show Current List"), this, SLOT(showList()));
       
    57 
       
    58     QMenu *insertMenu = new QMenu(tr("&Insert"));
       
    59 
       
    60     insertMenu->addAction(tr("&List"), this, SLOT(insertList()),
       
    61         QKeySequence(tr("Ctrl+L", "Insert|List")));
       
    62 
       
    63     menuBar()->addMenu(fileMenu);
       
    64     menuBar()->addMenu(insertMenu);
       
    65     menuBar()->addMenu(actionsMenu);
       
    66 
       
    67     editor = new QTextEdit(this);
       
    68     document = new QTextDocument(this);
       
    69     editor->setDocument(document);
       
    70 
       
    71     setCentralWidget(editor);
       
    72     setWindowTitle(tr("Text Document List Items"));
       
    73 }
       
    74 
       
    75 void MainWindow::highlightListItems()
       
    76 {
       
    77     QTextCursor cursor = editor->textCursor();
       
    78     QTextList *list = cursor.currentList();
       
    79 
       
    80     if (!list)
       
    81         return;
       
    82 
       
    83     cursor.beginEditBlock();
       
    84 //! [0]
       
    85     for (int index = 0; index < list->count(); ++index) {
       
    86         QTextBlock listItem = list->item(index);
       
    87 //! [0]
       
    88         QTextBlockFormat newBlockFormat = listItem.blockFormat();
       
    89         newBlockFormat.setBackground(Qt::lightGray);
       
    90         QTextCursor itemCursor = cursor;
       
    91         itemCursor.setPosition(listItem.position());
       
    92         //itemCursor.movePosition(QTextCursor::StartOfBlock);
       
    93         itemCursor.movePosition(QTextCursor::EndOfBlock,
       
    94                                 QTextCursor::KeepAnchor);
       
    95         itemCursor.setBlockFormat(newBlockFormat);
       
    96         /*
       
    97 //! [1]
       
    98         processListItem(listItem);
       
    99 //! [1]
       
   100         */
       
   101 //! [2]
       
   102     }
       
   103 //! [2]
       
   104     cursor.endEditBlock();
       
   105 }
       
   106 
       
   107 void MainWindow::showList()
       
   108 {
       
   109     QTextCursor cursor = editor->textCursor();
       
   110     QTextFrame *frame = cursor.currentFrame();
       
   111 
       
   112     if (!frame)
       
   113         return;
       
   114 
       
   115     QTreeWidget *treeWidget = new QTreeWidget;
       
   116     treeWidget->setColumnCount(1);
       
   117     QStringList headerLabels;
       
   118     headerLabels << tr("Lists");
       
   119     treeWidget->setHeaderLabels(headerLabels);
       
   120 
       
   121     QTreeWidgetItem *parentItem = 0;
       
   122     QTreeWidgetItem *item;
       
   123     QTreeWidgetItem *lastItem = 0;
       
   124     parentItems.clear();
       
   125     previousItems.clear();
       
   126 
       
   127 //! [3]
       
   128     QTextFrame::iterator it;
       
   129     for (it = frame->begin(); !(it.atEnd()); ++it) {
       
   130 
       
   131         QTextBlock block = it.currentBlock();
       
   132 
       
   133         if (block.isValid()) {
       
   134 
       
   135             QTextList *list = block.textList();
       
   136 
       
   137             if (list) {
       
   138                 int index = list->itemNumber(block);
       
   139 //! [3]
       
   140                 if (index == 0) {
       
   141                     parentItems.append(parentItem);
       
   142                     previousItems.append(lastItem);
       
   143                     listStructures.append(list);
       
   144                     parentItem = lastItem;
       
   145                     lastItem = 0;
       
   146 
       
   147                     if (parentItem != 0)
       
   148                         item = new QTreeWidgetItem(parentItem, lastItem);
       
   149                     else
       
   150                         item = new QTreeWidgetItem(treeWidget, lastItem);
       
   151 
       
   152                 } else {
       
   153 
       
   154                     while (parentItem != 0 && listStructures.last() != list) {
       
   155                         listStructures.pop_back();
       
   156                         parentItem = parentItems.takeLast();
       
   157                         lastItem = previousItems.takeLast();
       
   158                     }
       
   159                     if (parentItem != 0)
       
   160                         item = new QTreeWidgetItem(parentItem, lastItem);
       
   161                     else
       
   162                         item = new QTreeWidgetItem(treeWidget, lastItem);
       
   163                 }
       
   164                 item->setText(0, block.text());
       
   165                 lastItem = item;
       
   166                 /*
       
   167 //! [4]
       
   168                 processListItem(list, index);
       
   169 //! [4]
       
   170                 */
       
   171 //! [5]
       
   172             }
       
   173 //! [5] //! [6]
       
   174         }
       
   175 //! [6] //! [7]
       
   176     }
       
   177 //! [7]
       
   178 
       
   179     treeWidget->setWindowTitle(tr("List Contents"));
       
   180     treeWidget->show();
       
   181 }
       
   182 
       
   183 void MainWindow::insertList()
       
   184 {
       
   185     QTextCursor cursor = editor->textCursor();
       
   186     cursor.beginEditBlock();
       
   187 
       
   188     QTextList *list = cursor.currentList();
       
   189     QTextListFormat listFormat;
       
   190     if (list)
       
   191         listFormat = list->format();
       
   192 
       
   193     listFormat.setStyle(QTextListFormat::ListDisc);
       
   194     listFormat.setIndent(listFormat.indent() + 1);
       
   195     cursor.insertList(listFormat);
       
   196 
       
   197     cursor.endEditBlock();
       
   198 }