Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgCreateTextStrategy Class Reference

#include <SvgCreateTextStrategy.h>

+ Inheritance diagram for SvgCreateTextStrategy:

Public Member Functions

void cancelInteraction () override
 
KUndo2CommandcreateCommand () override
 
bool draggingInlineSize ()
 
void finishInteraction (Qt::KeyboardModifiers modifiers) override
 
void handleMouseMove (const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override
 
void paint (QPainter &painter, const KoViewConverter &converter) override
 
 SvgCreateTextStrategy (SvgTextTool *tool, const QPointF &clicked)
 
 ~SvgCreateTextStrategy () override=default
 
- Public Member Functions inherited from KoInteractionStrategy
 KoInteractionStrategy (KoToolBase *parent)
 constructor
 
KoToolBasetool () const
 
virtual ~KoInteractionStrategy ()
 Destructor.
 

Private Attributes

QPointF m_dragEnd
 
QPointF m_dragStart
 
QSizeF m_minSizeInline
 
Qt::KeyboardModifiers m_modifiers
 

Additional Inherited Members

- Protected Member Functions inherited from KoInteractionStrategy
uint decorationThickness () const
 
uint grabSensitivity () const
 Convenience function to get the global grab sensitivity.
 
uint handleRadius () const
 Convenience function to get the global handle radius.
 
 KoInteractionStrategy (KoInteractionStrategyPrivate &)
 constructor
 
- Protected Attributes inherited from KoInteractionStrategy
KoInteractionStrategyPrivated_ptr
 

Detailed Description

Definition at line 19 of file SvgCreateTextStrategy.h.

Constructor & Destructor Documentation

◆ SvgCreateTextStrategy()

SvgCreateTextStrategy::SvgCreateTextStrategy ( SvgTextTool * tool,
const QPointF & clicked )

Definition at line 26 of file SvgCreateTextStrategy.cpp.

28 , m_dragStart(clicked)
29 , m_dragEnd(clicked)
30{
31 KoSvgTextProperties properties = tool->propertiesForNewText();
33 const KoSvgText::FontMetrics fontMetrics = properties.metrics(true);
34 const qreal ftMultiplier = properties.fontSize().value / fontMetrics.fontSize;
35 const double lineHeight = (fontMetrics.lineGap+fontMetrics.ascender+fontMetrics.descender)*ftMultiplier;
36 m_minSizeInline = {lineHeight, lineHeight};
37}
KoInteractionStrategy(KoToolBase *parent)
constructor
static const KoSvgTextProperties & defaultProperties()
KoSvgText::FontMetrics metrics(const bool withResolvedLineHeight=true) const
metrics Return the metrics of the first available font.
KoSvgText::CssLengthPercentage fontSize() const
void inheritFrom(const KoSvgTextProperties &parentProperties, bool resolve=false)
The FontMetrics class A class to keep track of a variety of font metrics. Note that values are in Fre...
Definition KoSvgText.h:327
qint32 lineGap
additional linegap between consecutive lines.
Definition KoSvgText.h:341
qint32 fontSize
Currently set size, CSS unit 'em'.
Definition KoSvgText.h:329
qint32 descender
distance for origin to bottom.
Definition KoSvgText.h:340
qint32 ascender
distance from origin to top.
Definition KoSvgText.h:339

References KoSvgText::FontMetrics::ascender, KoSvgTextProperties::defaultProperties(), KoSvgText::FontMetrics::descender, KoSvgText::FontMetrics::fontSize, KoSvgTextProperties::fontSize(), KoSvgTextProperties::inheritFrom(), KoSvgText::FontMetrics::lineGap, m_minSizeInline, KoSvgTextProperties::metrics(), KoInteractionStrategy::tool(), and KoSvgText::CssLengthPercentage::value.

◆ ~SvgCreateTextStrategy()

SvgCreateTextStrategy::~SvgCreateTextStrategy ( )
overridedefault

Member Function Documentation

◆ cancelInteraction()

void SvgCreateTextStrategy::cancelInteraction ( )
overridevirtual

This method will undo frames based interactions by calling createCommand() and unexecuting that.

Reimplemented from KoInteractionStrategy.

Definition at line 149 of file SvgCreateTextStrategy.cpp.

150{
151 tool()->canvas()->snapGuide()->reset();
152 const QRectF updateRect = QRectF(m_dragStart, m_dragEnd).normalized();
153 tool()->canvas()->updateCanvas(updateRect);
154}
KoSnapGuide * snapGuide
virtual void updateCanvas(const QRectF &rc)=0
void reset()
Resets the snap guide.
KoCanvasBase * canvas() const
Returns the canvas the tool is working on.

References KoToolBase::canvas(), m_dragEnd, m_dragStart, KoSnapGuide::reset(), KoCanvasBase::snapGuide, KoInteractionStrategy::tool(), and KoCanvasBase::updateCanvas().

◆ createCommand()

KUndo2Command * SvgCreateTextStrategy::createCommand ( )
overridevirtual

For interactions that are undo-able this method should be implemented to return such a command. Implementations should return 0 otherwise.

Returns
a command, or 0.

Implements KoInteractionStrategy.

Definition at line 58 of file SvgCreateTextStrategy.cpp.

59{
60 SvgTextTool *const tool = qobject_cast<SvgTextTool *>(this->tool());
61
62 QRectF rectangle = QRectF(m_dragStart, m_dragEnd).normalized();
63
64 KoSvgTextProperties properties = tool->propertiesForNewText();
65 KoSvgTextProperties resolvedProperties = properties;
66 resolvedProperties.inheritFrom(KoSvgTextProperties::defaultProperties(), true);
67
68 const KoSvgText::FontMetrics fontMetrics = properties.metrics(true);
69 const qreal ftMultiplier = resolvedProperties.fontSize().value / fontMetrics.fontSize;
70 double ascender = fontMetrics.ascender;
71 ascender += fontMetrics.lineGap/2;
72 ascender *= ftMultiplier;
73 const double lineHeight = m_minSizeInline.width();
75
76 bool unwrappedText = m_modifiers.testFlag(Qt::ControlModifier);
77 if (rectangle.width() < m_minSizeInline.width() && rectangle.height() < m_minSizeInline.height()) {
78 unwrappedText = true;
79 }
80 if (!unwrappedText) {
82 val.isAuto = false;
83 val.customValue = writingMode == KoSvgText::HorizontalTB? rectangle.width(): rectangle.height();
84 properties.setProperty(KoSvgTextProperties::InlineSizeId, QVariant::fromValue(val));
85 }
86 if (writingMode != KoSvgText::HorizontalTB) {
88 }
89 // Ensure white space is set to pre-wrap if unspecified.
92 }
95 }
96
97 KoShapeFactoryBase *factory = KoShapeRegistry::instance()->value("KoSvgTextShapeID");
98 KoProperties *params = new KoProperties();//Fill these with "svgText", "defs" and "shapeRect"
99 params->setProperty("defs", QVariant(tool->generateDefs(properties)));
100
101 QPointF origin = rectangle.topLeft();
102
103 {
106
107 if (writingMode == KoSvgText::HorizontalTB) {
108 origin.setY(rectangle.top() + ascender);
109 if (halign == KoSvgText::AnchorMiddle) {
110 origin.setX(rectangle.center().x());
111 } else if ((halign == KoSvgText::AnchorEnd && !isRtl) || (halign == KoSvgText::AnchorStart && isRtl)) {
112 origin.setX(rectangle.right());
113 }
114 } else {
115 if (writingMode == KoSvgText::VerticalRL) {
116 origin.setX(rectangle.right() - (lineHeight*0.5));
117 } else {
118 origin.setX(rectangle.left() + (lineHeight*0.5));
119 }
120
121 if (halign == KoSvgText::AnchorMiddle) {
122 origin.setY(rectangle.center().y());
123 } else if (halign == KoSvgText::AnchorEnd) {
124 origin.setY(rectangle.bottom());
125 }
126 }
127 }
128 if (!rectangle.contains(origin) && unwrappedText) {
129 origin = writingMode == KoSvgText::HorizontalTB? QPointF(origin.x(), rectangle.bottom()): QPointF(rectangle.center().x(), origin.y());
130 }
131 params->setProperty("shapeRect", QVariant(rectangle));
132 params->setProperty("origin", QVariant(origin));
133
134 KoShape *textShape = factory->createShape( params, tool->canvas()->shapeController()->resourceManager());
135
136 KUndo2Command *parentCommand = new KUndo2Command();
137
138 new KoKeepShapesSelectedCommand(tool->koSelection()->selectedShapes(), {}, tool->canvas()->selectedShapesProxy(), false, parentCommand);
139
140 KUndo2Command *cmd = tool->canvas()->shapeController()->addShape(textShape, 0, parentCommand);
141 parentCommand->setText(cmd->text());
142
143 new KoKeepShapesSelectedCommand({}, {textShape}, tool->canvas()->selectedShapesProxy(), true, parentCommand);
144 tool->canvas()->snapGuide()->reset();
145
146 return parentCommand;
147}
void setText(const KUndo2MagicString &text)
KUndo2MagicString text() const
QPointer< KoShapeController > shapeController
virtual KoSelectedShapesProxy * selectedShapesProxy() const =0
selectedShapesProxy() is a special interface for keeping a persistent connections to selectionChanged...
const T value(const QString &id) const
void setProperty(const QString &name, const QVariant &value)
virtual KoShape * createShape(const KoProperties *params, KoDocumentResourceManager *documentResources=0) const
static KoShapeRegistry * instance()
@ TextAnchorId
KoSvgText::TextAnchor.
@ InlineSizeId
KoSvgText::AutoValue.
@ TextOrientationId
KoSvgText::TextOrientation.
@ TextCollapseId
KoSvgText::TextSpaceCollapse.
@ WritingModeId
KoSvgText::WritingMode.
@ DirectionId
KoSvgText::Direction.
@ TextWrapId
KoSvgText::TextWrap.
bool hasProperty(PropertyId id) const
void setProperty(PropertyId id, const QVariant &value)
QVariant propertyOrDefault(PropertyId id) const
Qt::KeyboardModifiers m_modifiers
TextAnchor
Where the text is anchored for SVG 1.1 text and 'inline-size'.
Definition KoSvgText.h:79
@ AnchorEnd
Anchor right for LTR, left for RTL.
Definition KoSvgText.h:82
@ AnchorStart
Anchor left for LTR, right for RTL.
Definition KoSvgText.h:80
@ AnchorMiddle
Anchor to the middle.
Definition KoSvgText.h:81
Direction
Base direction used by Bidi algorithm.
Definition KoSvgText.h:48
@ DirectionRightToLeft
Definition KoSvgText.h:50
@ HorizontalTB
Definition KoSvgText.h:38
@ OrientationUpright
Set all characters upright.
Definition KoSvgText.h:73
@ Preserve
Do not collapse any space.
Definition KoSvgText.h:99

References KoSvgText::AnchorEnd, KoSvgText::AnchorMiddle, KoSvgText::AnchorStart, KoSvgText::FontMetrics::ascender, KoToolBase::canvas(), KoShapeFactoryBase::createShape(), KoSvgText::AutoValue::customValue, KoSvgTextProperties::defaultProperties(), KoSvgTextProperties::DirectionId, KoSvgText::DirectionRightToLeft, KoSvgText::FontMetrics::fontSize, KoSvgTextProperties::fontSize(), KoSvgTextProperties::hasProperty(), KoSvgText::HorizontalTB, KoSvgTextProperties::inheritFrom(), KoSvgTextProperties::InlineSizeId, KoShapeRegistry::instance(), KoSvgText::AutoValue::isAuto, KoSvgText::FontMetrics::lineGap, m_dragEnd, m_dragStart, m_minSizeInline, m_modifiers, KoSvgTextProperties::metrics(), KoSvgText::OrientationUpright, KoSvgText::Preserve, KoSvgTextProperties::propertyOrDefault(), KoSnapGuide::reset(), KoCanvasBase::selectedShapesProxy(), KoProperties::setProperty(), KoSvgTextProperties::setProperty(), KUndo2Command::setText(), KoCanvasBase::shapeController, KoCanvasBase::snapGuide, KUndo2Command::text(), KoSvgTextProperties::TextAnchorId, KoSvgTextProperties::TextCollapseId, KoSvgTextProperties::TextOrientationId, KoSvgTextProperties::TextWrapId, KoInteractionStrategy::tool(), KoSvgText::CssLengthPercentage::value, KoGenericRegistry< T >::value(), KoSvgText::VerticalRL, KoSvgText::Wrap, and KoSvgTextProperties::WritingModeId.

◆ draggingInlineSize()

bool SvgCreateTextStrategy::draggingInlineSize ( )

Definition at line 161 of file SvgCreateTextStrategy.cpp.

162{
163 QRectF rectangle = QRectF(m_dragStart, m_dragEnd).normalized();
164 return (rectangle.width() >= m_minSizeInline.width() || rectangle.height() >= m_minSizeInline.height()) && !m_modifiers.testFlag(Qt::ControlModifier);
165}

References m_dragEnd, m_dragStart, m_minSizeInline, and m_modifiers.

◆ finishInteraction()

void SvgCreateTextStrategy::finishInteraction ( Qt::KeyboardModifiers modifiers)
overridevirtual

Override to make final changes to the data on the end of an interaction.

Implements KoInteractionStrategy.

Definition at line 156 of file SvgCreateTextStrategy.cpp.

157{
158 m_modifiers = modifiers;
159}

References m_modifiers.

◆ handleMouseMove()

void SvgCreateTextStrategy::handleMouseMove ( const QPointF & mouseLocation,
Qt::KeyboardModifiers modifiers )
overridevirtual

Extending classes should implement this method to update the selectedShapes based on the new mouse position.

Parameters
mouseLocationthe new location in pt
modifiersOR-ed set of keys pressed.

Implements KoInteractionStrategy.

Definition at line 50 of file SvgCreateTextStrategy.cpp.

51{
52 m_dragEnd = this->tool()->canvas()->snapGuide()->snap(mouseLocation, modifiers);
53 m_modifiers = modifiers;
54 const QRectF updateRect = QRectF(m_dragStart, m_dragEnd).normalized();
55 tool()->canvas()->updateCanvas(kisGrowRect(updateRect, 100));
56}
QPointF snap(const QPointF &mousePosition, Qt::KeyboardModifiers modifiers)
snaps the mouse position, returns if mouse was snapped
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186

References KoToolBase::canvas(), kisGrowRect(), m_dragEnd, m_dragStart, m_modifiers, KoSnapGuide::snap(), KoCanvasBase::snapGuide, KoInteractionStrategy::tool(), and KoCanvasBase::updateCanvas().

◆ paint()

void SvgCreateTextStrategy::paint ( QPainter & painter,
const KoViewConverter & converter )
overridevirtual

Reimplement this if the action needs to draw a "blob" on the canvas; that is, a transient decoration like a rubber band.

Reimplemented from KoInteractionStrategy.

Definition at line 39 of file SvgCreateTextStrategy.cpp.

40{
41 const QTransform originalPainterTransform = painter.transform();
42 painter.setTransform(converter.documentToView(), true);
43 KisHandlePainterHelper handlePainter(&painter, originalPainterTransform, 0.0, decorationThickness());
44
45 const QPolygonF poly(QRectF(m_dragStart, m_dragEnd));
46 handlePainter.setHandleStyle(KisHandleStyle::primarySelection());
47 handlePainter.drawRubberLine(poly);
48}
The KisHandlePainterHelper class is a special helper for painting handles around objects....
static KisHandleStyle & primarySelection()
virtual QPointF documentToView(const QPointF &documentPoint) const

References KoInteractionStrategy::decorationThickness(), KoViewConverter::documentToView(), KisHandlePainterHelper::drawRubberLine(), m_dragEnd, m_dragStart, KisHandleStyle::primarySelection(), and KisHandlePainterHelper::setHandleStyle().

Member Data Documentation

◆ m_dragEnd

QPointF SvgCreateTextStrategy::m_dragEnd
private

Definition at line 35 of file SvgCreateTextStrategy.h.

◆ m_dragStart

QPointF SvgCreateTextStrategy::m_dragStart
private

Definition at line 34 of file SvgCreateTextStrategy.h.

◆ m_minSizeInline

QSizeF SvgCreateTextStrategy::m_minSizeInline
private

Definition at line 36 of file SvgCreateTextStrategy.h.

◆ m_modifiers

Qt::KeyboardModifiers SvgCreateTextStrategy::m_modifiers
private

Definition at line 37 of file SvgCreateTextStrategy.h.


The documentation for this class was generated from the following files: