-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextShape.cpp
More file actions
123 lines (101 loc) · 2.42 KB
/
TextShape.cpp
File metadata and controls
123 lines (101 loc) · 2.42 KB
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
/**
* @file TextShape.cpp
* @brief Implementation of text shape class
* @author Ehcochwy
* @date 2025-05-10
*/
#include "TextShape.h"
#include <QFontMetrics>
#include <QPainter>
TextShape::TextShape()
: DiagramShape(Text)
, size(QSizeF(100, 30))
, textColor(Qt::black)
{
font = QFont("Arial", 10);
shapeColor = Qt::transparent; // Default transparent background
}
void TextShape::paint(QPainter* painter)
{
painter->save();
QRectF rect(position, size);
// Draw background if not transparent
if (shapeColor != Qt::transparent) {
painter->setBrush(shapeColor);
painter->setPen(Qt::NoPen);
painter->drawRect(rect);
}
// Draw text
painter->setFont(font);
painter->setPen(textColor);
painter->drawText(rect, Qt::AlignCenter | Qt::TextWordWrap, m_text);
// Draw selection handles if selected
if (isSelected) {
paintSelectionHandles(painter, rect);
}
painter->restore();
}
bool TextShape::contains(const QPointF& point) const
{
return QRectF(position, size).contains(point);
}
QRectF TextShape::boundingRect() const
{
return QRectF(position, size);
}
void TextShape::moveBy(const QPointF& delta)
{
position += delta;
}
void TextShape::setSize(const QSizeF& newSize)
{
size = newSize;
}
QSizeF TextShape::getSize() const
{
return size;
}
void TextShape::setFont(const QFont& newFont)
{
font = newFont;
// Optionally auto-resize based on new font
if (!m_text.isEmpty()) {
size = calculateTextSize();
}
}
QFont TextShape::getFont() const
{
return font;
}
void TextShape::setTextColor(const QColor& color)
{
textColor = color;
}
QColor TextShape::getTextColor() const
{
return textColor;
}
QSizeF TextShape::calculateTextSize() const
{
if (m_text.isEmpty()) {
return QSizeF(100, 30); // Default size
}
QFontMetrics fm(font);
QRect textRect = fm.boundingRect(QRect(0, 0, 1000, 1000), Qt::TextWordWrap, m_text);
// Add some padding
return QSizeF(textRect.width() + 20, textRect.height() + 10);
}
void TextShape::save(QDataStream& out) const
{
DiagramShape::save(out);
out << size;
out << font;
out << textColor;
}
void TextShape::load(QDataStream& in)
{
DiagramShape::load(in);
in >> size;
in >> font;
in >> textColor;
}