-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockinstance.cpp
More file actions
75 lines (59 loc) · 2.38 KB
/
blockinstance.cpp
File metadata and controls
75 lines (59 loc) · 2.38 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
#include "blockinstance.h"
#define BORDER_MARGIN 12
#define ICON_OFFSET 25
#define HEADER_HEIGHT 50
#define ENTRY_HEIGHT 25
#define FOOTER_HEIGHT 15
BlockInstance::BlockInstance(const QString &title, const model_linkable_t *linkable, const QPixmap &icon, const QColor &color) :
QGraphicsItem(), title(title), icon(icon), color(color), linkable(linkable)
{
data = (QPair<QPointF, QList<Entry *> > *)model_userdata(model_object(linkable));
QGraphicsSimpleTextItem *name = new QGraphicsSimpleTextItem(title, this);
name->setFont(QFont("sans-serif", 11, QFont::Bold));
name->setPos(ICON_OFFSET, 0);
int width = QFontMetrics(name->font()).width(title) + ICON_OFFSET * 2;
QSet<QString> ios;
foreach (Entry *e, data->second)
{
ios.insert(e->getName());
}
QList<QString> ios_list = ios.toList();
qSort(ios_list);
int offset = HEADER_HEIGHT;
foreach (QString n, ios_list)
{
QGraphicsSimpleTextItem *entry = new QGraphicsSimpleTextItem(n, this);
entry->setPos( width / 2 - entry->boundingRect().width() / 2, offset + ENTRY_HEIGHT / 2 - entry->boundingRect().height() / 2);
foreach (Entry *e, data->second)
{
if (e->getName() == n)
{
Entry::Handle *handle = e->makeHandle();
handle->setParentItem(this);
handle->setPos(e->getType() == Entry::Input? 0 : width, offset + ENTRY_HEIGHT / 2);
}
}
offset += ENTRY_HEIGHT;
}
bounding = QRectF(0, 0, width, offset + FOOTER_HEIGHT);
setPos(data->first);
setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
}
QRectF BlockInstance::boundingRect() const
{
return QRectF(bounding.x() - BORDER_MARGIN, bounding.y() - BORDER_MARGIN, bounding.width() + BORDER_MARGIN * 2, bounding.height() + BORDER_MARGIN * 2);
}
void BlockInstance::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->save();
painter->setRenderHints(QPainter::Antialiasing);
painter->setPen(QPen(Qt::black, hasFocus()? 3 : 1));
QPainterPath bg;
bg.addRoundedRect(bounding, 4, 4);
painter->fillPath(bg, QBrush(color));
painter->drawPath(bg);
painter->drawPixmap(3, 3, icon);
painter->restore();
}