forked from Haroooold/motaGroup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
90 lines (77 loc) · 2.32 KB
/
Button.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
/*
* File: Button.cpp
* --------------------
* This file implements Button.h interface.
*/
#include "Button.h"
Button::Button(QString name, int sizeX, int sizeY, QGraphicsItem* parent): QGraphicsRectItem (parent)
{
// draw the button frame
setRect(0, 0, sizeX, sizeY); // position(0,0) & size(200,40)
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
setBrush(brush);
// draw the text
textContent = name;
text = new QGraphicsTextItem(textContent, this);
double xPos = rect().width()/2 - text->boundingRect().width()/2;
double yPos = rect().height()/2 - text->boundingRect().height()/2;
text->setPos(xPos, yPos);
// set hover event
setAcceptHoverEvents(true);
}
/*
* Implementation notes: mousePressEvent()
* -------------------------------
* Send <code>clicked()</code> signal and connect.
* Use to start, load, close, restart, skip, back, set BGM.
*/
void Button::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
setBrush(brush); // change color & style
emit clicked();
}
/*
* Implementation notes: hoverEnterEvent, hoverLeaveEvent
* ------------------------------------------------
* Change color whether mouse hover over the button or not.
*/
void Button::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::cyan);
setBrush(brush); // change color & style
}
void Button::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
setBrush(brush); // change color & style
}
/*
* Implementation notes: setButtonText(QString textContent)
* --------------------------------------------------------
* Change text content on the <code>Button</code>.
*/
void Button::setButtonText(QString textContent)
{
text->setPlainText(textContent);
double xPos = rect().width()/2 - text->boundingRect().width()/2;
double yPos = rect().height()/2 - text->boundingRect().height()/2;
text->setPos(xPos, yPos);
}
/*
* Implementation notes: getButtonText()
* -------------------------------------
* Return name on the Button.
*/
QString Button::getButtonText()
{
return textContent;
}