/**************************************************************************** ** ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). ** Contact: Qt Software Information (qt-info@nokia.com) ** ** This file is part of the documentation of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** No Commercial Usage ** This file contains pre-release code and may not be distributed. ** You may use this file in accordance with the terms and conditions ** contained in the either Technology Preview License Agreement or the ** Beta Release License Agreement. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain ** additional rights. These rights are described in the Nokia Qt LGPL ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this ** package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** If you are unsure which license is appropriate for your use, please ** contact the sales department at qt-sales@nokia.com. ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include "mainwindow.h" MainWindow::MainWindow() { QMenu *fileMenu = new QMenu(tr("&File")); fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence(tr("Ctrl+Q", "File|Exit"))); QMenu *actionsMenu = new QMenu(tr("&Actions")); actionsMenu->addAction(tr("&Highlight List Items"), this, SLOT(highlightListItems())); actionsMenu->addAction(tr("&Show Current List"), this, SLOT(showList())); QMenu *insertMenu = new QMenu(tr("&Insert")); insertMenu->addAction(tr("&List"), this, SLOT(insertList()), QKeySequence(tr("Ctrl+L", "Insert|List"))); menuBar()->addMenu(fileMenu); menuBar()->addMenu(insertMenu); menuBar()->addMenu(actionsMenu); editor = new QTextEdit(this); document = new QTextDocument(this); editor->setDocument(document); setCentralWidget(editor); setWindowTitle(tr("Text Document List Items")); } void MainWindow::highlightListItems() { QTextCursor cursor = editor->textCursor(); QTextList *list = cursor.currentList(); if (!list) return; cursor.beginEditBlock(); //! [0] for (int index = 0; index < list->count(); ++index) { QTextBlock listItem = list->item(index); //! [0] QTextBlockFormat newBlockFormat = listItem.blockFormat(); newBlockFormat.setBackground(Qt::lightGray); QTextCursor itemCursor = cursor; itemCursor.setPosition(listItem.position()); //itemCursor.movePosition(QTextCursor::StartOfBlock); itemCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); itemCursor.setBlockFormat(newBlockFormat); /* //! [1] processListItem(listItem); //! [1] */ //! [2] } //! [2] cursor.endEditBlock(); } void MainWindow::showList() { QTextCursor cursor = editor->textCursor(); QTextFrame *frame = cursor.currentFrame(); if (!frame) return; QTreeWidget *treeWidget = new QTreeWidget; treeWidget->setColumnCount(1); QStringList headerLabels; headerLabels << tr("Lists"); treeWidget->setHeaderLabels(headerLabels); QTreeWidgetItem *parentItem = 0; QTreeWidgetItem *item; QTreeWidgetItem *lastItem = 0; parentItems.clear(); previousItems.clear(); //! [3] QTextFrame::iterator it; for (it = frame->begin(); !(it.atEnd()); ++it) { QTextBlock block = it.currentBlock(); if (block.isValid()) { QTextList *list = block.textList(); if (list) { int index = list->itemNumber(block); //! [3] if (index == 0) { parentItems.append(parentItem); previousItems.append(lastItem); listStructures.append(list); parentItem = lastItem; lastItem = 0; if (parentItem != 0) item = new QTreeWidgetItem(parentItem, lastItem); else item = new QTreeWidgetItem(treeWidget, lastItem); } else { while (parentItem != 0 && listStructures.last() != list) { listStructures.pop_back(); parentItem = parentItems.takeLast(); lastItem = previousItems.takeLast(); } if (parentItem != 0) item = new QTreeWidgetItem(parentItem, lastItem); else item = new QTreeWidgetItem(treeWidget, lastItem); } item->setText(0, block.text()); lastItem = item; /* //! [4] processListItem(list, index); //! [4] */ //! [5] } //! [5] //! [6] } //! [6] //! [7] } //! [7] treeWidget->setWindowTitle(tr("List Contents")); treeWidget->show(); } void MainWindow::insertList() { QTextCursor cursor = editor->textCursor(); cursor.beginEditBlock(); QTextList *list = cursor.currentList(); QTextListFormat listFormat; if (list) listFormat = list->format(); listFormat.setStyle(QTextListFormat::ListDisc); listFormat.setIndent(listFormat.indent() + 1); cursor.insertList(listFormat); cursor.endEditBlock(); }