KoTextDocumentLayout.cpp 12.6 KB
Newer Older
1
/* This file is part of the KDE project
2
 * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
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
 *
 * 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 "KoTextDocumentLayout.h"
#include "KoTextShapeData.h"
#include "styles/KoParagraphStyle.h"
#include "styles/KoCharacterStyle.h"
#include "styles/KoListStyle.h"
#include "styles/KoStyleManager.h"
#include "KoTextBlockData.h"
#include "KoTextBlockBorderData.h"
28
#include "KoInlineTextObjectManager.h"
29 30

#include <KoInsets.h>
Thomas Zander's avatar
Thomas Zander committed
31
#include <KoPostscriptPaintDevice.h>
32 33 34 35 36 37 38
#include <KoShape.h>

#include <kdebug.h>
#include <QTextBlock>
#include <QTextList>
#include <QTimer>

39
class LayoutStateDummy : public KoTextDocumentLayout::LayoutState {
40
public:
41
    LayoutStateDummy() : m_styleManager(0) {}
42 43 44 45 46 47 48 49 50 51
    bool start() { return false; }
    void end() {}
    void reset() {}
    bool interrupted() { return false; }
    double width() { return 0; }
    double x() { return 0; }
    double y() { return 0; }
    double docOffsetInShape() const { return 0; }
    bool addLine(QTextLine &) { return false; }
    bool nextParag() { return false; }
52
    bool previousParag() { return false; }
53
    double documentOffsetInShape() { return 0; }
54
    void draw(QPainter *, const KoTextDocumentLayout::PaintContext &) {}
55 56
    KoStyleManager *styleManager() const { return m_styleManager; }
    void setStyleManager(KoStyleManager *sm) { m_styleManager = sm; }
57
    bool setFollowupShape(KoShape *) { return false; }
58
    void clearTillEnd() {}
59
    int cursorPosition() const { return 0; }
60
    void registerInlineObject(const QTextInlineObject &) {}
61

62
    KoStyleManager *m_styleManager;
63 64
};

65 66
class KoTextDocumentLayout::Private {
public:
67
    Private(KoTextDocumentLayout *parent_)
68
        : inlineTextObjectManager(0),
69 70
        scheduled(false),
        parent(parent_)
71 72 73
    {
    }

74 75 76 77 78
    void relayoutPrivate() {
        scheduled = false;
        parent->relayout();
    }

79 80 81
    QList<KoShape *> shapes;
    KoInlineTextObjectManager *inlineTextObjectManager;
    bool scheduled;
82
    KoTextDocumentLayout *parent;
83
};
84 85

// ------------------- KoTextDocumentLayout --------------------
86
KoTextDocumentLayout::KoTextDocumentLayout(QTextDocument *doc, KoTextDocumentLayout::LayoutState *layout)
87
    : QAbstractTextDocumentLayout(doc),
88
    m_state(layout),
89
    d(new Private(this))
90
{
Thomas Zander's avatar
Thomas Zander committed
91
    setPaintDevice( new KoPostscriptPaintDevice() );
92 93
    if(m_state == 0)
        m_state = new LayoutStateDummy();
94 95 96 97
}

KoTextDocumentLayout::~KoTextDocumentLayout() {
    delete m_state;
98 99 100 101 102
    m_state = 0;
}

void KoTextDocumentLayout::setLayout(LayoutState *layout) {
    Q_ASSERT(layout);
103 104 105
    KoStyleManager *sm = 0;
    if(m_state)
        sm = m_state->styleManager();
106 107
    delete m_state;
    m_state = layout;
108
    m_state->setStyleManager(sm);
109
    scheduleLayout();
110 111
}

112 113 114 115 116 117 118
bool KoTextDocumentLayout::hasLayouter() const {
    if(dynamic_cast<LayoutStateDummy*> (m_state) != 0)
        return false;
    if(m_state == 0)
        return false;
    return true;
}
119

120
void KoTextDocumentLayout::addShape(KoShape *shape) {
121
    d->shapes.append(shape);
122 123 124 125 126 127 128 129

    KoTextShapeData *data = dynamic_cast<KoTextShapeData*> (shape->userData());
    if(data) {
        data->faul();
        m_state->reset();
    }
}

130 131
void KoTextDocumentLayout::setStyleManager(KoStyleManager *sm) {
    m_state->setStyleManager(sm);
132 133 134 135 136
    if(document()->isEmpty()) {
        QTextBlock block = document()->begin();
        if(block.blockFormat().intProperty(KoParagraphStyle::StyleId) < 100)
            sm->defaultParagraphStyle()->applyStyle(block);
    }
137 138 139
}

void KoTextDocumentLayout::setInlineObjectTextManager(KoInlineTextObjectManager *iom) {
140
    d->inlineTextObjectManager = iom;
141 142
}

143
KoInlineTextObjectManager *KoTextDocumentLayout::inlineObjectTextManager() {
144
    return d->inlineTextObjectManager;
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159
QRectF KoTextDocumentLayout::blockBoundingRect(const QTextBlock &block) const {
    // nobody calls this code and I have no way of implementing it anyway...
    Q_UNUSED(block);
    kWarning() << "KoTextDocumentLayout::blockBoundingRect is not implemented"<< endl;
    return QRectF(0, 0, 10, 10);
}

QSizeF KoTextDocumentLayout::documentSize() const {
    // nobody calls this code and I have no way of implementing it anyway...
    kWarning() << "KoTextDocumentLayout::documentSize is not implemented"<< endl;
    return QSizeF(10, 10);
}

160 161 162 163
void KoTextDocumentLayout::draw(QPainter *painter, const QAbstractTextDocumentLayout::PaintContext &context) {
    PaintContext pc;
    pc.textContext = context;
    m_state->draw(painter, pc);
Thomas Zander's avatar
Thomas Zander committed
164 165
}

166 167
void KoTextDocumentLayout::draw ( QPainter * painter, const KoTextDocumentLayout::PaintContext & context) {
    m_state->draw(painter, context);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

QRectF KoTextDocumentLayout::frameBoundingRect(QTextFrame *frame) const {
    Q_UNUSED(frame);
    // nobody calls this code and I have no way of implementing it anyway...
    kWarning() << "KoTextDocumentLayout::frameBoundingRect is not implemented"<< endl;
    return QRectF(0, 0, 10, 10);
}

int KoTextDocumentLayout::hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const {
    // kDebug() << "hitTest[" << point.x() << "," << point.y() << "]" << endl;
    QTextBlock block = document()->begin();
    int position = -1;
    while(block.isValid()) {
        QTextLayout *layout = block.layout();
        if(point.y() > layout->boundingRect().bottom()) {
184
            position = block.position() + block.length() -1;
185 186 187 188 189
            block = block.next();
            continue;
        }
        for(int i=0; i < layout->lineCount(); i++) {
            QTextLine line = layout->lineAt(i);
190
            updateTabsForLine(block, i);
191 192 193 194 195 196 197 198 199 200
            // kDebug() << " + line[" << line.textStart() << "]: " << line.y() << "-" << line.height() << endl;
            if(point.y() > line.y() + line.height()) {
                position = line.textStart() + line.textLength();
                continue;
            }
            if(accuracy == Qt::ExactHit && point.y() < line.y()) // between lines
                return -1;
            if(accuracy == Qt::ExactHit && // left or right of line
                    (point.x() < line.x() || point.x() > line.x() + line.width()))
                return -1;
201 202 203 204
            if(point.x() > line.width() && layout->textOption().textDirection() == Qt::RightToLeft) {
                // totally right of RTL text means the position is the start of the text.
                return block.position() + line.textStart();
            }
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
            return block.position() + line.xToCursor(point.x());
        }
        block = block.next();
    }
    if(accuracy == Qt::ExactHit)
        return -1;
    return position;
}

int KoTextDocumentLayout::pageCount () const {
    return 1;
}

void KoTextDocumentLayout::documentChanged(int position, int charsRemoved, int charsAdded) {
    Q_UNUSED(charsAdded);
    Q_UNUSED(charsRemoved);
    if(shapes().count() == 0) // nothing to do.
        return;

    foreach(KoShape *shape, shapes()) {
        KoTextShapeData *data = dynamic_cast<KoTextShapeData*> (shape->userData());
        Q_ASSERT(data);
        if(data && data->position() <= position && data->endPosition() >= position) {
            // found our (first) shape to re-layout
            data->faul();
            m_state->reset();
            scheduleLayout();
            return;
        }
    }
    // if still here; then the change was not in any frame, lets relayout the last for now.
    KoShape *shape = shapes().last();
    KoTextShapeData *data = dynamic_cast<KoTextShapeData*> (shape->userData());
    Q_ASSERT(data);
    data->faul();
    m_state->reset();
    scheduleLayout();
}

void KoTextDocumentLayout::drawInlineObject(QPainter *painter, const QRectF &rect, QTextInlineObject object, int position, const QTextFormat &format) {
245
    Q_ASSERT(format.isCharFormat());
246
    if(d->inlineTextObjectManager == 0)
247
        return;
248
    QTextCharFormat cf = format.toCharFormat();
249
    KoInlineObject *obj = d->inlineTextObjectManager->inlineTextObject(cf);
250
    if(obj)
251
        obj->paint(*painter, paintDevice(), document(), rect, object, position, cf);
252 253 254
}

void KoTextDocumentLayout::positionInlineObject(QTextInlineObject item, int position, const QTextFormat &format) {
255
    Q_ASSERT(format.isCharFormat());
256
    if(d->inlineTextObjectManager == 0)
257
        return;
258
    QTextCharFormat cf = format.toCharFormat();
259
    KoInlineObject *obj = d->inlineTextObjectManager->inlineTextObject(cf);
260
    if(obj)
261
        obj->updatePosition(document(), item, position, cf);
262 263 264
}

void KoTextDocumentLayout::resizeInlineObject(QTextInlineObject item, int position, const QTextFormat &format) {
265
    Q_ASSERT(format.isCharFormat());
266
    if(d->inlineTextObjectManager == 0)
267
        return;
268
    QTextCharFormat cf = format.toCharFormat();
269
    KoInlineObject *obj = d->inlineTextObjectManager->inlineTextObject(cf);
270
    if(obj) {
271
        obj->resize(document(), item, position, cf, paintDevice());
272 273
        m_state->registerInlineObject(item);
    }
274 275 276
}

void KoTextDocumentLayout::scheduleLayout() {
277
    if(d->scheduled)
278
        return;
279
    interruptLayout();
280
    d->scheduled = true;
281
    QTimer::singleShot(0, this, SLOT(relayoutPrivate()));
282 283 284 285 286 287 288 289 290 291 292
}

void KoTextDocumentLayout::relayout() {
    layout();
}

void KoTextDocumentLayout::interruptLayout() {
    m_state->reset();
}

void KoTextDocumentLayout::layout() {
293
    d->scheduled = false;
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 337 338 339 340
//kDebug() << "KoTextDocumentLayout::layout" << endl;
    class End {
    public:
        End(LayoutState *state) { m_state = state; }
        ~End() { m_state->end(); }
    private:
        LayoutState *m_state;
    };
    End ender(m_state); // poor mans finally{}

    if(! m_state->start())
        return;
    bool newParagraph = true;
    while(m_state->shape) {
        QTextLine line = m_state->layout->createLine();
        if (!line.isValid()) { // end of parag
            double posY = m_state->y();
            bool moreText = m_state->nextParag();
            if(m_state->shape && m_state->y() > posY)
                m_state->shape->repaint(QRectF(0, posY,
                            m_state->shape->size().width(), m_state->y() - posY));

            if(! moreText)
                return; // done!
            newParagraph = true;
            continue;
        }
        newParagraph = false;
        line.setLineWidth(m_state->width());
        line.setPosition(QPointF(m_state->x(), m_state->y()));
        while(m_state->addLine(line)) {
            if(m_state->shape == 0) { // shape is full!
                line.setPosition(QPointF(0, m_state->y()+20));
                return; // done!
            }
            line.setLineWidth(m_state->width());
            line.setPosition(QPointF(m_state->x(), m_state->y()));
        }

        QRectF repaintRect = line.rect();
        repaintRect.moveTop(repaintRect.y() - m_state->docOffsetInShape());
        repaintRect.setX(0.0); // just take full width since we can't force a repaint of
        repaintRect.setWidth(m_state->shape->size().width()); // where lines were before layout.
        m_state->shape->repaint(repaintRect);
    }
}

341 342 343 344 345 346 347 348
QList<KoShape*> KoTextDocumentLayout::shapes() const {
    return d->shapes;
}

KoStyleManager *KoTextDocumentLayout::styleManager() const {
    return m_state->styleManager();
}

349 350 351 352 353 354 355 356 357 358 359
KoShape* KoTextDocumentLayout::shapeForPosition(int position) const {
    foreach(KoShape *shape, shapes()) {
        KoTextShapeData *data = dynamic_cast<KoTextShapeData*> (shape->userData());
        if(data == 0)
            continue;
        if(data->position() <= position && (data->endPosition() == -1 || data->endPosition() > position))
            return shape;
    }
    return 0;
}

360 361 362 363 364 365 366 367 368 369 370 371 372 373
// static
void KoTextDocumentLayout::updateTabsForLine(const QTextBlock &block, int lineNumber) {
    if(! block.isValid()) return;
    KoTextBlockData *blockData = dynamic_cast<KoTextBlockData*> (block.userData());
    if(blockData == 0) return;
    QList<KoTextBlockData::TabLineData> tabsData = blockData->tabLineData();
    if(tabsData.count() <= lineNumber) return;

    KoTextBlockData::TabLineData tabs = tabsData[lineNumber];
    QTextOption textOption = block.layout()->textOption();
    textOption.setTabArray(tabs.tabs);
    block.layout()->setTextOption(textOption);
}

374
#include "KoTextDocumentLayout.moc"