Krita Source Code Documentation
Loading...
Searching...
No Matches
KoTosContainer.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2010 Thomas Zander <zander@kde.org>
3 * SPDX-FileCopyrightText: 2010 KO GmbH <boud@valdyas.org>
4 * SPDX-FileCopyrightText: 2010 Thorsten Zachmann <zachmann@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9#include "KoTosContainer.h"
10
11#include "KoTosContainer_p.h"
12#include "KoShapeRegistry.h"
13#include "KoShapeFactoryBase.h"
15#include "KoTextShapeDataBase.h"
16#include "KoTosContainerModel.h"
17#include "KoXmlNS.h"
18
19#include <FlakeDebug.h>
20
21#include <QTextCursor>
22#include <QTextDocument>
23
25 : QSharedData()
26 , resizeBehavior(KoTosContainer::IndependentSizes)
27{
28}
29
31 : QSharedData()
34 , alignment(rhs.alignment)
35{
36}
37
41
42
48
50 : KoShapeContainer(rhs)
51 , d(rhs.d)
52{
53}
54
59
60void KoTosContainer::paintComponent(QPainter &) const
61{
62}
63
64bool KoTosContainer::loadText(const QDomElement &element)
65{
66 Q_UNUSED(element);
67 return false;
68}
69
70
71void KoTosContainer::setPlainText(const QString &text)
72{
73 KoShape *textShape = this->textShape();
74 if (textShape == 0) {
75 warnFlake << "No text shape present in KoTosContainer";
76 return;
77 }
78 KoTextShapeDataBase *shapeData = qobject_cast<KoTextShapeDataBase*>(textShape->userData());
79 Q_ASSERT(shapeData->document());
80 shapeData->document()->setPlainText(text);
81}
82
84{
85 if (d->resizeBehavior == resizeBehavior) {
86 return;
87 }
88 d->resizeBehavior = resizeBehavior;
89 if (model()) {
91 }
92}
93
95{
96 return d->resizeBehavior;
97}
98
99void KoTosContainer::setTextAlignment(Qt::Alignment alignment)
100{
101 KoShape *textShape = this->textShape();
102 if (textShape == 0) {
103 warnFlake << "No text shape present in KoTosContainer";
104 return;
105 }
106
107 // vertical
108 KoTextShapeDataBase *shapeData = qobject_cast<KoTextShapeDataBase*>(textShape->userData());
109 shapeData->setVerticalAlignment(alignment);
110
111 // horizontal
112 Q_ASSERT(shapeData->document());
113 QTextBlockFormat bf;
114 bf.setAlignment(alignment & Qt::AlignHorizontal_Mask);
115
116 QTextCursor cursor(shapeData->document());
117 cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
118 cursor.mergeBlockFormat(bf);
119
120 d->alignment = alignment;
121}
122
123Qt::Alignment KoTosContainer::textAlignment() const
124{
125 KoShape *textShape = this->textShape();
126 if (textShape == 0) {
127 warnFlake << "No text shape present in KoTosContainer";
128 return Qt::AlignTop;
129 }
130
131 // vertical
132 KoTextShapeDataBase *shapeData = qobject_cast<KoTextShapeDataBase*>(textShape->userData());
133 // the model makes sure it contains a shape that has a KoTextShapeDataBase set so no need to check that
134 Qt::Alignment answer = shapeData->verticalAlignment() & Qt::AlignVertical_Mask;
135
136 // horizontal
137 Q_ASSERT(shapeData->document());
138 QTextCursor cursor(shapeData->document());
139 answer = answer | (cursor.blockFormat().alignment() & Qt::AlignHorizontal_Mask);
140
141 return answer;
142}
143
145{
146 d->preferredTextRect = rect;
147 KoShape *textShape = this->textShape();
148 //debugFlake << rect << textShape << d->resizeBehavior;
149 if (d->resizeBehavior == TextFollowsPreferredTextRect && textShape) {
150 //debugFlake << rect;
151 textShape->setPosition(rect.topLeft());
152 textShape->setSize(rect.size());
153 }
154}
155
157{
158 return d->preferredTextRect;
159}
160
162{
163 if (!documentResources) {
164 warnFlake << "KoDocumentResourceManager not found";
165 return 0;
166 }
167
168 delete textShape();
169 delete model();
170
172
173 QSet<KoShape*> delegates;
174 delegates << this;
175 KoShape *textShape = 0;
176 KoShapeFactoryBase *factory = KoShapeRegistry::instance()->get("TextShapeID");
177 if (factory) { // not installed, that's too bad, but allowed
178 textShape = factory->createDefaultShape(documentResources);
179 Q_ASSERT(textShape); // would be a bug in the text shape;
180 if (d->resizeBehavior == TextFollowsPreferredTextRect) {
181 textShape->setSize(d->preferredTextRect.size());
182 } else {
184 }
185 if (d->resizeBehavior == TextFollowsPreferredTextRect) {
186 textShape->setPosition(d->preferredTextRect.topLeft());
187 } else {
188 textShape->setPosition(QPointF(0, 0));
189 }
190 textShape->setSelectable(false);
192 KoTextShapeDataBase *shapeData = qobject_cast<KoTextShapeDataBase*>(textShape->userData());
193 Q_ASSERT(shapeData); // would be a bug in kotext
194 // TODO check if that is correct depending on the resize mode
195 shapeData->setVerticalAlignment(Qt::AlignVCenter);
197 // textShape->setZIndex(zIndex() + 1); // not needed as there as the text shape is the only sub shape
198 delegates << textShape;
199 } else {
200 warnFlake << "Text shape factory not found";
201 }
202 setToolDelegates(delegates);
203 return textShape;
204}
205
207{
208 const QList<KoShape*> subShapes = shapes();
209 return subShapes.isEmpty() ? 0 : subShapes.at(0);
210}
211
213{
214 Q_UNUSED(shape);
215 if (model() == 0) {
216 return;
217 }
218
219 if (type == SizeChanged || type == ContentChanged) {
220 model()->containerChanged(this, type);
221 }
222 // TODO is this needed?
223#if 0
224 Q_FOREACH (KoShape *shape, model()->shapes())
225 shape->notifyChanged();
226#endif
227}
228
229void KoTosContainer::setRunThrough(short int runThrough)
230{
232 KoShape *textShape = this->textShape();
233 if (textShape) {
235 }
236}
#define warnFlake
Definition FlakeDebug.h:16
T get(const QString &id) const
virtual void containerChanged(KoShapeContainer *container, KoShape::ChangeType type)=0
QList< KoShape * > shapes() const
void setModel(KoShapeContainerModel *model)
void addShape(KoShape *shape)
KoShapeContainerModel * model
virtual KoShape * createDefaultShape(KoDocumentResourceManager *documentResources=0) const
static KoShapeRegistry * instance()
void setToolDelegates(const QSet< KoShape * > &delegates)
Definition KoShape.cpp:1315
KoShapeUserData * userData() const
Definition KoShape.cpp:710
virtual QSizeF size() const
Get the size of the shape in pt.
Definition KoShape.cpp:820
int runThrough() const
Definition KoShape.cpp:962
void setSelectable(bool selectable)
Definition KoShape.cpp:1009
ChangeType
Used by shapeChanged() to select which change was made.
Definition KoShape.h:95
@ ContentChanged
the content of the shape changed e.g. a new image inside a pixmap/text change inside a textshape
Definition KoShape.h:110
@ SizeChanged
used after a setSize()
Definition KoShape.h:100
virtual void setPosition(const QPointF &position)
Set the position of the shape in pt.
Definition KoShape.cpp:295
virtual void setRunThrough(short int runThrough)
Definition KoShape.cpp:967
void notifyChanged()
Definition KoShape.cpp:698
virtual void setSize(const QSizeF &size)
Resize the shape.
Definition KoShape.cpp:276
QTextDocument * document() const
return the document
Qt::Alignment verticalAlignment() const
void setVerticalAlignment(Qt::Alignment alignment)
KoShape * createTextShape(KoDocumentResourceManager *documentResources=0)
void setRunThrough(short int runThrough) override
void setResizeBehavior(ResizeBehavior resizeBehavior)
QSharedDataPointer< Private > d
void setPreferredTextRect(const QRectF &rect)
void shapeChanged(ChangeType type, KoShape *shape=0) override
void paintComponent(QPainter &painter) const override
Paint the component Implement this method to allow the shape to paint itself, just like the KoShape::...
virtual bool loadText(const QDomElement &element)
void setPlainText(const QString &text)
KoShape * textShape() const
ResizeBehavior resizeBehavior() const
QRectF preferredTextRect() const
ResizeBehavior
different kinds of resizing behavior to determine how to treat text overflow
@ TextFollowsPreferredTextRect
The size/position of the text area will follow the preferredTextRect property.
~KoTosContainer() override
void setTextAlignment(Qt::Alignment alignment)
Qt::Alignment textAlignment() const