-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.cpp
More file actions
118 lines (103 loc) · 3.28 KB
/
interface.cpp
File metadata and controls
118 lines (103 loc) · 3.28 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
#include <QApplication>
#include <QFontMetrics>
#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QPixmapCache>
#include "interface.h"
namespace CutePathSim
{
/**
* \class Interface
* The Interface class represents an interface on a Component (either an input or an output), so that it can be displayed and manipulated on a ComponentGraph.
*
* This class doesn't directly manage inputs, outputs, or their connections, as that is handled by the Component::Input and Component::Output classes. The Interface class is mainly responsible for graphically drawing the interfaces, and handling drag events from the user.
*/
/**
* Constructs an Interface class.
*/
Interface::Interface(const QString &name, QGraphicsItem *parent) : QGraphicsItem(parent)
{
m_name = name;
/*
// add a drop shadow effect
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
shadow->setBlurRadius(5);
shadow->setOffset(2, 3);
setGraphicsEffect(shadow);
*/
// set up the font
if(m_font == 0)
{
m_font = new QFont("Helvetica");
m_font->setPixelSize(FONT_SIZE);
}
m_textWidth = QFontMetrics(*m_font).size(Qt::TextSingleLine, m_name).width();
setAcceptHoverEvents(true);
setAcceptDrops(true);
m_drawHover = false;
m_selected = false;
}
Interface::~Interface()
{
}
/**
* \fn Interface::name()
* Returns the name of the interface.
*/
QRectF Interface::boundingRect() const
{
return QRect(-LEFT_MARGIN - m_textWidth / 2, -TOP_MARGIN - FONT_SIZE / 2, LEFT_MARGIN + m_textWidth + RIGHT_MARGIN, TOP_MARGIN + FONT_SIZE + BOTTOM_MARGIN);
}
void Interface::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
// draw a gradient background
QLinearGradient gradient(0, 0, 0, boundingRect().height());
if(m_drawHover)
{
gradient.setColorAt(0, color().lighter(110));
}
else
{
gradient.setColorAt(0, color());
}
int penWidth;
if(m_selected)
{
penWidth = SELECTED_PEN_WIDTH;
painter->setPen(QPen(QBrush(Qt::SolidPattern), penWidth));
}
else
{
penWidth = BORDER_PEN_WIDTH;
painter->setPen(QPen(QBrush(Qt::SolidPattern), penWidth));
}
gradient.setColorAt(1, Qt::white);
QBrush gradientBrush(gradient);
painter->setBrush(gradientBrush);
QRect drawingRect(boundingRect().x() + penWidth / 2, boundingRect().y() + penWidth / 2, boundingRect().width() - penWidth, boundingRect().height() - penWidth);
painter->drawRoundedRect(drawingRect, 5, 5);
// draw the name
// FIXME: figure out how to make the font hinting look optimal
painter->setPen(QPen());
painter->setFont(*m_font);
painter->drawText(drawingRect, Qt::AlignCenter, name());
}
void Interface::hoverEnterEvent(QGraphicsSceneHoverEvent *)
{
m_drawHover = true;
update();
}
void Interface::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{
m_drawHover = false;
update();
}
const qreal Interface::BORDER_PEN_WIDTH;
const qreal Interface::SELECTED_PEN_WIDTH;
const qreal Interface::LEFT_MARGIN;
const qreal Interface::RIGHT_MARGIN;
const qreal Interface::TOP_MARGIN;
const qreal Interface::BOTTOM_MARGIN;
const qreal Interface::FONT_SIZE;
QFont *Interface::m_font = 0;
}