-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextelement.cpp
167 lines (127 loc) · 4.03 KB
/
textelement.cpp
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
#include "textelement.h"
#include "elementpicker.h"
#include "basespace.h"
#include <QGraphicsTextItem>
#include <QDebug>
#include <QTextDocument>
#include <QPainter>
#include <QTextCursor>
#include <QKeyEvent>
class TextItem_private : public QGraphicsTextItem
{
TextElement * m_parent;
bool m_selecting;
public:
TextItem_private(TextElement * parent)
:QGraphicsTextItem(parent),m_parent(parent),m_selecting(false){
QTextDocument * doc = new QTextDocument(this);
this->setDocument(doc);
//this->setFlag(QGraphicsItem::ItemSendsGeometryChanges,true);
//this->setFlag(QGraphicsItem::ItemSendsScenePositionChanges,true);
connect(this->document(),&QTextDocument::contentsChanged,this,[this](){
//m_parent->setRect(this->boundingRect());
setTextWidth(-1);
setTextWidth(boundingRect().width());
QRectF r = this->mapRectToParent(this->boundingRect());
m_parent->setRect(r);
});
}
protected:
virtual void keyReleaseEvent(QKeyEvent *event) override;
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
};
void TextItem_private::keyReleaseEvent(QKeyEvent *event)
{
QGraphicsTextItem::keyReleaseEvent(event);
m_parent->emitCursorChanged();
event->accept();;
}
void TextItem_private::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsTextItem::mouseMoveEvent(event);
if (m_selecting){
m_parent->emitCursorChanged();
}
}
void TextItem_private::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsTextItem::mousePressEvent(event);
m_selecting = true;
m_parent->emitCursorChanged();
}
void TextItem_private::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsTextItem::mouseReleaseEvent(event);
m_selecting = false;
m_parent->emitCursorChanged();
}
TextElement::TextElement(BaseElement *parent, BaseSpace *space, QRectF initRect):
BaseElement(parent,space,initRect)
{
setElementName(m_space->nameElementAuto(this->type()));
setResizable(false);
m_picker = createPicker();
m_textItem = new TextItem_private(this);
m_textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
connect(this,&TextElement::rectChanged,this,[this](const QRectF & arg1){
m_textItem->setPos(arg1.topLeft());
//this->m_picker->show();
});
}
QGraphicsTextItem *TextElement::textItem() const
{
return qobject_cast<QGraphicsTextItem *>(m_textItem);
}
void TextElement::emitCursorChanged()
{
emit cursorPositionChanged(m_textItem->textCursor());
}
void TextElement::setAlignment(Qt::Alignment alignment)
{
QTextBlockFormat format;
format.setAlignment(alignment);
QTextCursor cursor = m_textItem->textCursor();
cursor.mergeBlockFormat(format);
m_textItem->setTextCursor(cursor);
}
void TextElement::startDraw(const QPointF &p)
{
QPointF p1 = this->mapToScene(p);
this->setPos(p1);
this->m_textItem->setPos(this->boundingRect().topLeft());
//this->m_picker->show();
}
void TextElement::doDrawing(const QPointF &p)
{
Q_UNUSED(p)
}
void TextElement::clickDraw(const QPointF &)
{
if (this->m_textItem->toPlainText().size()==0){
this->abortDraw();
}
}
void TextElement::doubleClickDraw(const QPointF &)
{
}
BasePicker *TextElement::createPicker()
{
return new ElementPicker(this);
}
void TextElement::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
Q_UNUSED(painter)
BaseElement::paint(painter,option,widget);
}
void TextElement::keyPressEvent(QKeyEvent *event)
{
event->accept();
}
void TextElement::keyReleaseEvent(QKeyEvent *event)
{
event->accept();
}