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
|
/* This file is part of the KDE project
* Copyright (C) 2011 C. Boemann <cbo@boemann.dk>
*
* 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 "SimpleRootAreaProvider.h"
#include "TextShape.h"
#include <KoBorder.h>
#include <KoTextLayoutRootArea.h>
#include <KoTextLayoutObstruction.h>
SimpleRootAreaProvider::SimpleRootAreaProvider(KoTextShapeData *data, TextShape *textshape)
: m_textShape(textshape)
, m_area(0)
, m_textShapeData(data)
, m_fixAutogrow(false)
{
}
KoTextLayoutRootArea *SimpleRootAreaProvider::provide(KoTextDocumentLayout *documentLayout, const RootAreaConstraint &, int requestedPosition, bool *isNewRootArea)
{
if (m_area == 0) {
*isNewRootArea = true;
m_area = new KoTextLayoutRootArea(documentLayout);
m_area->setAssociatedShape(m_textShape);
m_textShapeData->setRootArea(m_area);
return m_area;
}
if (requestedPosition == 0) {
*isNewRootArea = false;
return m_area;
}
return 0;
}
void SimpleRootAreaProvider::releaseAllAfter(KoTextLayoutRootArea *afterThis)
{
Q_UNUSED(afterThis);
}
void SimpleRootAreaProvider::doPostLayout(KoTextLayoutRootArea *rootArea, bool isNewRootArea)
{
Q_UNUSED(isNewRootArea);
m_textShape->update(m_textShape->outlineRect());
QSizeF newSize = m_textShape->size()
- QSizeF(m_textShapeData->leftPadding() + m_textShapeData->rightPadding(),
m_textShapeData->topPadding() + m_textShapeData->bottomPadding());
KoBorder *border = m_textShape->border();
if (border) {
newSize -= QSizeF(border->borderWidth(KoBorder::LeftBorder) + border->borderWidth(KoBorder::RightBorder), border->borderWidth(KoBorder::TopBorder) + border->borderWidth(KoBorder::BottomBorder));
}
if (m_textShapeData->verticalAlignment() & Qt::AlignBottom) {
// Do nothing
}
if (m_textShapeData->verticalAlignment() & Qt::AlignVCenter) {
// Do nothing
}
if (m_textShapeData->resizeMethod() == KoTextShapeData::AutoGrowWidthAndHeight
|| m_textShapeData->resizeMethod() == KoTextShapeData::AutoGrowHeight) {
qreal height = rootArea->bottom() - rootArea->top();
if (height > newSize.height()) {
newSize.setHeight(height);
}
if (m_textShape->shapeId() == "AnnotationTextShapeID") {
if (height < newSize.height()) {
newSize.setHeight(rootArea->bottom() - rootArea->top());
}
}
}
if (m_textShapeData->resizeMethod() == KoTextShapeData::AutoGrowWidthAndHeight
|| m_textShapeData->resizeMethod() == KoTextShapeData::AutoGrowWidth) {
qreal width = rootArea->right() - rootArea->left();
if (width > newSize.width()) {
newSize.setWidth(rootArea->right() - rootArea->left());
}
}
qreal newBottom = rootArea->top() + newSize.height();
KoFlake::Position sizeAnchor = KoFlake::TopLeftCorner;
if (m_textShapeData->verticalAlignment() & Qt::AlignBottom) {
if (true /*FIXME test no page based shapes interfering*/) {
rootArea->setVerticalAlignOffset(newBottom - rootArea->bottom());
sizeAnchor = KoFlake::BottomLeftCorner;
}
}
if (m_textShapeData->verticalAlignment() & Qt::AlignVCenter) {
if (true /*FIXME test no page based shapes interfering*/) {
rootArea->setVerticalAlignOffset((newBottom - rootArea->bottom()) / 2);
sizeAnchor = KoFlake::CenteredPosition;
}
}
newSize += QSizeF(m_textShapeData->leftPadding() + m_textShapeData->rightPadding(),
m_textShapeData->topPadding() + m_textShapeData->bottomPadding());
if (border) {
newSize += QSizeF(border->borderWidth(KoBorder::LeftBorder) + border->borderWidth(KoBorder::RightBorder), border->borderWidth(KoBorder::TopBorder) + border->borderWidth(KoBorder::BottomBorder));
}
if (newSize != m_textShape->size()) {
// OO grows to both sides so when to small the initial layouting needs
// to keep that into account.
if (m_fixAutogrow) {
m_fixAutogrow = false;
QSizeF tmpSize = m_textShape->size();
tmpSize.setWidth(newSize.width());
QPointF centerpos = rootArea->associatedShape()->absolutePosition(KoFlake::CenteredPosition);
m_textShape->setSize(tmpSize);
m_textShape->setAbsolutePosition(centerpos, KoFlake::CenteredPosition);
centerpos = rootArea->associatedShape()->absolutePosition(sizeAnchor);
m_textShape->setSize(newSize);
m_textShape->setAbsolutePosition(centerpos, sizeAnchor);
}
m_textShape->setSize(newSize);
}
m_textShape->update(m_textShape->outlineRect());
}
void SimpleRootAreaProvider::updateAll()
{
if (m_area && m_area->associatedShape()) {
m_area->associatedShape()->update();
}
}
QRectF SimpleRootAreaProvider::suggestRect(KoTextLayoutRootArea *rootArea)
{
//Come up with a rect, but actually we don't need the height, as we set it to infinite below
// Still better keep it for completeness sake
QRectF rect(QPointF(), m_textShape->size());
rect.adjust(m_textShapeData->leftPadding(), m_textShapeData->topPadding(), -m_textShapeData->rightPadding(), - m_textShapeData->bottomPadding());
KoBorder *border = m_textShape->border();
if (border) {
rect.adjust(border->borderWidth(KoBorder::LeftBorder), border->borderWidth(KoBorder::TopBorder),
-border->borderWidth(KoBorder::RightBorder), - border->borderWidth(KoBorder::BottomBorder));
}
// In simple cases we always set height way too high so that we have no breaking
// If the shape grows afterwards or not is handled in doPostLayout()
rect.setHeight(1E6);
if (m_textShapeData->resizeMethod() == KoTextShapeData::AutoGrowWidthAndHeight
|| m_textShapeData->resizeMethod() == KoTextShapeData::AutoGrowWidth) {
rootArea->setNoWrap(1E6);
}
// Make sure the size is not negative due to padding and border with
// This can happen on vertical lines containing text on shape.
if (rect.width() < 0) {
rect.setWidth(0);
}
return rect;
}
QList<KoTextLayoutObstruction *> SimpleRootAreaProvider::relevantObstructions(KoTextLayoutRootArea *rootArea)
{
Q_UNUSED(rootArea);
QList<KoTextLayoutObstruction *> obstructions;
/*
m_textShape->boundingRect();
QList<KoShape *> shapes;
shapes = manager->shapesAt(canvasRect):
*/
return obstructions;
}
|