Пример автоматического алгоритма размещения подписей к точке.
Поддерживает 8 позиций размещения подписи вокруг точки.
Алгоритм делит экран вокруг центра графической сцены на части и представляет для каждой части разную последовательность расположения подписей вокруг точки:
0 | 1 | 2
---+---+---
7 | + | 3
---+---+---
6 | 5 | 4
рис. 1
Для точки, расположенной в левой верхней части графической сцены(0 на рисунке 1), последовательность размещения подписей будет:
0 | 4 | 2
---+---+---
6 | + | 7
---+---+---
3 | 5 | 1
Для точки, расположенной в центральной верхней части графической сцены(1 на рисунке 1), последовательность размещения подписей будет:
6 | 0 | 4
---+---+---
3 | + | 2
---+---+---
5 | 1 | 7
И т.д.
Алгоритм находится в функции paint файла vertexgraphicsitem.cpp.
| C++ (Qt) | 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
| // vertexgraphicsitem.h
#ifndef VERTEXGRAPHICSITEM_H
#define VERTEXGRAPHICSITEM_H
#include <QGraphicsEllipseItem>
#include "graphicsunits.h"
class VertexGraphicsItem : public QGraphicsEllipseItem
{
public:
VertexGraphicsItem(Vertex v, quint16 index, QGraphicsItem *parent = nullptr);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
Vertex vertex() const { return m_vertex; }
quint16 index() const { return m_index; }
void show() { setVisible(true); }
void hide() { setVisible(false); }
void setVisible(bool visible);
private:
Vertex m_vertex;
quint16 m_index;
quint16 m_visibleCount;
qreal m_pointWidth;
};
#endif // VERTEXGRAPHICSITEM_H |
|
| C++ (Qt) | 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
| // vertexgraphicsitem.cpp
#include "vertexgraphicsitem.h"
#include <QPainter>
#include <QGraphicsScene>
#define VertexGraphicsItemName "VertexItem"
VertexGraphicsItem::VertexGraphicsItem(Vertex v, quint16 index, QGraphicsItem *parent)
: QGraphicsEllipseItem (parent)
, m_vertex(v)
, m_index(index)
, m_visibleCount(1)
, m_pointWidth(5.0)
{
setRect(v.point.x() - m_pointWidth/2, v.point.y() - m_pointWidth/2, m_pointWidth, m_pointWidth);
setZValue(1.0);
setData(0, QString(VertexGraphicsItemName));
setData(1, m_index);
}
void VertexGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->save();
QPen pen(Qt::red);
pen.setWidthF(m_pointWidth);
painter->setPen(pen);
painter->drawPoint(m_vertex.point);
int k = 0;
// with how many other vertex collides
QList<QGraphicsItem *> itemsList = collidingItems();
for (QList<QGraphicsItem *>::const_iterator cit = itemsList.cbegin(), cend = itemsList.cend(); cit != cend; ++cit) {
if ((*cit)->data(0).toString() == VertexGraphicsItemName && (*cit)->data(1).toUInt() < m_index)
++k;
}
/*
* left top corner
* sequence of texts
*
* 0 | 4 | 2
* ---+---+---
* 6 | + | 7
* ---+---+---
* 3 | 5 | 1
*
*/
if (k > 7) {
qDebug("Too many bounding Vertices");
// do nothing
} else {
switch (k) {
case 0:
k = 0;
break;
case 1:
k = 4;
break;
case 2:
k = 2;
break;
case 3:
k = 6;
break;
case 4:
k = 1;
break;
case 5:
k = 5;
break;
case 6:
k = 7;
break;
case 7:
k = 3;
break;
}
QRectF scRect = scene()->sceneRect();
QPointF textPoint = vertex().point;
qreal px = scRect.width()/2 - textPoint.x();
qreal py = scRect.height()/2 - textPoint.y();
// rotate matrix clockwise
if (py > 0) {
if (px > 0)
k += 0;
else if (px < 0)
k += 2;
else
k += 1;
} else if (py < 0) {
if (px > 0)
k += 6;
else if (px < 0)
k += 4;
else
k += 5;
} else {
if (px > 0)
k += 7;
else if (px < 0)
k += 3;
}
k = k % 8;
QString text = QString::number(m_index);
qreal indentX = painter->fontMetrics().tightBoundingRect("_").width();
qreal indentY = 3.0;
qreal dx = painter->fontMetrics().tightBoundingRect(text).width();
qreal dy = painter->fontMetrics().tightBoundingRect(text).height();
/*
* matrix of corner calculations
*
* 0 | 1 | 2
* ---+---+---
* 7 | + | 3
* ---+---+---
* 6 | 5 | 4
*/
switch(k) {
case 0:
textPoint.rx() -= dx + m_pointWidth/2 + indentX;
textPoint.ry() -= m_pointWidth/2 + indentY;
break;
case 1:
textPoint.rx() -= dx/2;
textPoint.ry() -= m_pointWidth/2 + indentY;
break;
case 2:
textPoint.rx() += m_pointWidth/2 + indentX;
textPoint.ry() -= m_pointWidth/2 + indentY;
break;
case 3:
textPoint.rx() += m_pointWidth/2 + indentX;
textPoint.ry() += dy/2;
break;
case 4:
textPoint.rx() += m_pointWidth/2 + indentX;
textPoint.ry() += dy + m_pointWidth/2 + indentY;
break;
case 5:
textPoint.rx() -= dx/2;
textPoint.ry() += dy + m_pointWidth/2 + indentY;
break;
case 6:
textPoint.rx() -= dx + m_pointWidth/2 + indentX;
textPoint.ry() += dy + m_pointWidth/2 + indentY;
break;
case 7:
textPoint.rx() -= dx + m_pointWidth/2 + indentX;
textPoint.ry() += dy/2;
break;
}
painter->drawText(textPoint, text);
}
painter->restore();
}
void VertexGraphicsItem::setVisible(bool visible)
{
if (visible) {
if (m_visibleCount == 1000) {
qDebug("Too many times set visible(> 1000)");
return;
}
++m_visibleCount;
if (m_visibleCount == 1)
QGraphicsEllipseItem::setVisible(true);
} else {
if (m_visibleCount == 0)
return;
--m_visibleCount;
if (m_visibleCount == 0)
QGraphicsEllipseItem::setVisible(false);
}
} |
|
graphicsunits.h
| C++ (Qt) | 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
| // graphicsunits.h
#ifndef GRAPHICSUNITS_H
#define GRAPHICSUNITS_H
#include <QDataStream>
#include <QPointF>
#include <QColor>
#include <QDebug>
struct VertexColor {
unsigned char r, g, b, a;
VertexColor() : r(0), g(0), b(0), a(0) {}
VertexColor(const QColor &c)
: r(uchar(qRound(c.redF() * c.alphaF() * 255)))
, g(uchar(qRound(c.greenF() * c.alphaF() * 255)))
, b(uchar(qRound(c.blueF() * c.alphaF() * 255)))
, a(uchar(qRound(c.alphaF() * 255)))
{ }
QString toText() const { return "#" + QByteArray().append((char)r).append((char)g).append((char)b).append((char)a).toHex(); }
};
struct Vertex {
QPointF point;
VertexColor color;
void set(QPointF newPoint, VertexColor newColor) {
point = newPoint; color = newColor;
}
QString toText(int fieldWidth = 6, char fmt = 'f', int prec = 1) const
{ return QString("%1, %2").arg(point.x(), fieldWidth, fmt, prec).arg(point.y(), fieldWidth, fmt, prec); }
};
inline QDataStream& operator>>(QDataStream& s, VertexColor& c)
{
s >> c.r >> c.g >> c.b >> c.a;
return s;
}
inline QDebug& operator<<(QDebug &d, const VertexColor &c)
{
d << c.toText();
return d;
}
inline QDataStream& operator>>(QDataStream& s, Vertex& v)
{
s >> v.point >> v.color;
return s;
}
inline QDebug& operator<<(QDebug &d, const Vertex &v)
{
QDebugStateSaver saver(d);
d.nospace() << "(" << v.point << ", " << v.color << ")";
return d;
}
#endif // GRAPHICSUNITS_H |
|
Графический пример:
|