1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/* This file is part of the KDE project
* Copyright (C) 2010 Thomas Zander <zander@kde.org>
* Copyright (C) 2010 Jean Nicolas Artaud <jean.nicolas.artaud@kogmbh.com>
* Copyright (C) 2011 Pavol Korinek <pavol.korinek@ixonos.com>
* Copyright (C) 2011 Lukáš Tvrdý <lukas.tvrdy@ixonos.com>
* Copyright (C) 2011 Ko GmbH <cbo@kogmbh.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "ToCGenerator.h"
#include <klocalizedstring.h>
#include "KoTextDocumentLayout.h"
#include "KoTextLayoutRootArea.h"
#include "DummyDocumentLayout.h"
#include <KoParagraphStyle.h>
#include <KoTextPage.h>
#include <KoTextDocument.h>
#include <KoTextBlockData.h>
#include <KoStyleManager.h>
#include <KoTableOfContentsGeneratorInfo.h>
#include <QTextDocument>
#include <TextLayoutDebug.h>
#include <KoBookmark.h>
#include <KoTextRangeManager.h>
#include <algorithm>
static const QString INVALID_HREF_TARGET = "INVALID_HREF";
ToCGenerator::ToCGenerator(QTextDocument *tocDocument, KoTableOfContentsGeneratorInfo *tocInfo)
: QObject(tocDocument)
, m_ToCDocument(tocDocument)
, m_ToCInfo(tocInfo)
, m_document(0)
, m_documentLayout(0)
{
Q_ASSERT(tocDocument);
Q_ASSERT(tocInfo);
tocDocument->setUndoRedoEnabled(false);
tocDocument->setDocumentLayout(new DummyDocumentLayout(tocDocument));
KoTextDocument(tocDocument).setRelativeTabs(tocInfo->m_relativeTabStopPosition);
}
ToCGenerator::~ToCGenerator()
{
delete m_ToCInfo;
}
void ToCGenerator::setBlock(const QTextBlock &block)
{
m_block = block;
m_documentLayout = static_cast<KoTextDocumentLayout *>(m_block.document()->documentLayout());
m_document = m_documentLayout->document();
}
QString ToCGenerator::fetchBookmarkRef(const QTextBlock &block, KoTextRangeManager *textRangeManager)
{
QHash<int, KoTextRange *> ranges = textRangeManager->textRangesChangingWithin(block.document(), block.position(), block.position() + block.length(), block.position(), block.position() + block.length());
foreach (KoTextRange *range, ranges) {
KoBookmark *bookmark = dynamic_cast<KoBookmark *>(range);
if (bookmark) {
return bookmark->name();
}
}
return QString();
}
static QString removeWhitespacePrefix(const QString& text)
{
int firstNonWhitespaceCharIndex = 0;
const int length = text.length();
while (firstNonWhitespaceCharIndex < length && text.at(firstNonWhitespaceCharIndex).isSpace()) {
firstNonWhitespaceCharIndex++;
}
return text.right(length - firstNonWhitespaceCharIndex);
}
bool ToCGenerator::generate()
{
if (!m_ToCInfo)
return true;
m_preservePagebreak = m_ToCDocument->begin().blockFormat().intProperty(KoParagraphStyle::BreakBefore) & KoText::PageBreak;
m_success = true;
QTextCursor cursor = m_ToCDocument->rootFrame()->lastCursorPosition();
cursor.setPosition(m_ToCDocument->rootFrame()->firstPosition(), QTextCursor::KeepAnchor);
cursor.beginEditBlock();
cursor.insertBlock(QTextBlockFormat(), QTextCharFormat());
KoStyleManager *styleManager = KoTextDocument(m_document).styleManager();
if (!m_ToCInfo->m_indexTitleTemplate.text.isEmpty()) {
KoParagraphStyle *titleStyle = styleManager->paragraphStyle(m_ToCInfo->m_indexTitleTemplate.styleId);
// titleStyle == 0? then it might be in unused styles
if (!titleStyle) {
titleStyle = styleManager->unusedStyle(m_ToCInfo->m_indexTitleTemplate.styleId); // this should return true only for ToC template preview
}
if (!titleStyle) {
titleStyle = styleManager->defaultTableOfcontentsTitleStyle();
}
QTextBlock titleTextBlock = cursor.block();
titleStyle->applyStyle(titleTextBlock);
cursor.insertText(m_ToCInfo->m_indexTitleTemplate.text);
if (m_preservePagebreak) {
QTextBlockFormat blockFormat;
blockFormat.setProperty(KoParagraphStyle::BreakBefore, KoText::PageBreak);
cursor.mergeBlockFormat(blockFormat);
m_preservePagebreak = false;
}
cursor.insertBlock(QTextBlockFormat(), QTextCharFormat());
}
// Add TOC
// Iterate through all blocks to generate TOC
QTextBlock block = m_document->rootFrame()->firstCursorPosition().block();
int blockId = 0;
for (; block.isValid(); block = block.next()) {
// Choose only TOC blocks
if (m_ToCInfo->m_useOutlineLevel) {
if (block.blockFormat().hasProperty(KoParagraphStyle::OutlineLevel)) {
int level = block.blockFormat().intProperty(KoParagraphStyle::OutlineLevel);
generateEntry(level, cursor, block, blockId);
continue;
}
}
if (m_ToCInfo->m_useIndexSourceStyles) {
bool inserted = false;
foreach (const IndexSourceStyles &indexSourceStyles, m_ToCInfo->m_indexSourceStyles) {
foreach (const IndexSourceStyle &indexStyle, indexSourceStyles.styles) {
if (indexStyle.styleId == block.blockFormat().intProperty(KoParagraphStyle::StyleId)) {
generateEntry(indexSourceStyles.outlineLevel, cursor, block, blockId);
inserted = true;
break;
}
}
if (inserted)
break;
}
if (inserted)
continue;
}
if (m_ToCInfo->m_useIndexMarks) {
if (false) {
generateEntry(1, cursor, block, blockId);
continue;
}
}
}
cursor.endEditBlock();
m_documentLayout->documentChanged(m_block.position(),1,1);
return m_success;
}
static bool compareTab(const QVariant &tab1, const QVariant &tab2)
{
return tab1.value<KoText::Tab>().position < tab2.value<KoText::Tab>().position;
}
void ToCGenerator::generateEntry(int outlineLevel, QTextCursor &cursor, QTextBlock &block, int &blockId)
{
KoStyleManager *styleManager = KoTextDocument(m_document).styleManager();
QString tocEntryText = block.text();
tocEntryText.remove(QChar::ObjectReplacementCharacter);
// some headings contain tabs, replace all occurrences with spaces
tocEntryText.replace('\t',' ').remove(0x200B);
tocEntryText = removeWhitespacePrefix(tocEntryText);
// Add only blocks with text
if (!tocEntryText.isEmpty()) {
KoParagraphStyle *tocTemplateStyle = 0;
if (outlineLevel >= 1 && (outlineLevel-1) < m_ToCInfo->m_entryTemplate.size()
&& outlineLevel <= m_ToCInfo->m_outlineLevel) {
// List's index starts with 0, outline level starts with 0
const TocEntryTemplate *tocEntryTemplate = &m_ToCInfo->m_entryTemplate.at(outlineLevel - 1);
// ensure that we fetched correct entry template
Q_ASSERT(tocEntryTemplate->outlineLevel == outlineLevel);
if (tocEntryTemplate->outlineLevel != outlineLevel) {
qDebug() << "TOC outline level not found correctly " << outlineLevel;
}
tocTemplateStyle = styleManager->paragraphStyle(tocEntryTemplate->styleId);
if (tocTemplateStyle == 0) {
tocTemplateStyle = styleManager->defaultTableOfContentsEntryStyle(outlineLevel);
}
QTextBlockFormat blockFormat;
if (m_preservePagebreak) {
blockFormat.setProperty(KoParagraphStyle::BreakBefore, KoText::PageBreak);
m_preservePagebreak = false;
}
cursor.insertBlock(blockFormat, QTextCharFormat());
QTextBlock tocEntryTextBlock = cursor.block();
tocTemplateStyle->applyStyle( tocEntryTextBlock );
KoTextBlockData bd(block);
// save the current style due to hyperlinks
QTextCharFormat savedCharFormat = cursor.charFormat();
foreach (IndexEntry * entry, tocEntryTemplate->indexEntries) {
switch(entry->name) {
case IndexEntry::LINK_START: {
//IndexEntryLinkStart *linkStart = static_cast<IndexEntryLinkStart*>(entry);
QString target = fetchBookmarkRef(block, m_documentLayout->textRangeManager());
if (target.isNull()) {
// generate unique name for the bookmark
target = tocEntryText + "|outline" + QString::number(blockId);
blockId++;
// insert new KoBookmark
QTextCursor blockCursor(block);
KoBookmark *bookmark = new KoBookmark(blockCursor);
bookmark->setName(target);
m_documentLayout->textRangeManager()->insert(bookmark);
}
if (!target.isNull()) {
// copy it to alter subset of properties
QTextCharFormat linkCf(savedCharFormat);
linkCf.setAnchor(true);
linkCf.setProperty(KoCharacterStyle::AnchorType, KoCharacterStyle::Anchor);
linkCf.setAnchorHref('#'+ target);
QBrush foreground = linkCf.foreground();
foreground.setColor(Qt::blue);
linkCf.setForeground(foreground);
linkCf.setProperty(KoCharacterStyle::UnderlineStyle, KoCharacterStyle::SolidLine);
linkCf.setProperty(KoCharacterStyle::UnderlineType, KoCharacterStyle::SingleLine);
cursor.setCharFormat(linkCf);
}
break;
}
case IndexEntry::CHAPTER: {
//IndexEntryChapter *chapter = static_cast<IndexEntryChapter*>(entry);
cursor.insertText(bd.counterText());
break;
}
case IndexEntry::SPAN: {
IndexEntrySpan *span = static_cast<IndexEntrySpan*>(entry);
cursor.insertText(span->text);
break;
}
case IndexEntry::TEXT: {
//IndexEntryText *text = static_cast<IndexEntryText*>(entry);
cursor.insertText(tocEntryText);
break;
}
case IndexEntry::TAB_STOP: {
IndexEntryTabStop *tabEntry = static_cast<IndexEntryTabStop*>(entry);
cursor.insertText("\t");
QTextBlockFormat blockFormat = cursor.blockFormat();
QList<QVariant> tabList = (blockFormat.property(KoParagraphStyle::TabPositions)).value<QList<QVariant> >();
if (tabEntry->m_position.isEmpty()) {
tabEntry->tab.position = KoTextLayoutArea::MaximumTabPos;
} // else the position is already parsed into tab.position
tabList.append(QVariant::fromValue<KoText::Tab>(tabEntry->tab));
std::sort(tabList.begin(), tabList.end(), compareTab);
blockFormat.setProperty(KoParagraphStyle::TabPositions, QVariant::fromValue<QList<QVariant> >(tabList));
cursor.setBlockFormat(blockFormat);
break;
}
case IndexEntry::PAGE_NUMBER: {
//IndexEntryPageNumber *pageNumber = static_cast<IndexEntryPageNumber*>(entry);
cursor.insertText(resolvePageNumber(block));
break;
}
case IndexEntry::LINK_END: {
//IndexEntryLinkEnd *linkEnd = static_cast<IndexEntryLinkEnd*>(entry);
cursor.setCharFormat(savedCharFormat);
break;
}
default:{
qDebug() << "New or unknown index entry";
break;
}
}
}// foreach
cursor.setCharFormat(savedCharFormat); // restore the cursor char format
}
}
}
QString ToCGenerator::resolvePageNumber(const QTextBlock &headingBlock)
{
KoTextDocumentLayout *layout = qobject_cast<KoTextDocumentLayout*>(m_document->documentLayout());
KoTextLayoutRootArea *rootArea = layout->rootAreaForPosition(headingBlock.position());
if (rootArea) {
if (rootArea->page()) {
return QString::number(rootArea->page()->visiblePageNumber());
}
else qDebug()<<"had root but no page";
}
m_success = false;
return "###";
}
|