-
Notifications
You must be signed in to change notification settings - Fork 4
/
square.cpp
139 lines (110 loc) · 2.94 KB
/
square.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
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
#include "square.h"
#include "view.h"
#include <iostream>
#include <QtWidgets>
Square::Square(const QColor &color, int x, int y)
{
this->x = x;
this->y = y;
this->color = color;
this->dispText = "Square";
setZValue((x + y) % 2);
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}
Square::Square(const QColor &color, int x, int y, QString text)
{
this->x = x;
this->y = y;
this->color = color;
this->dispText = text;
setZValue((x + y) % 2);
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}
/*
void Square::popUpCombo()
{
w = new QWidget;
w->setWindowTitle("Select Color");
t = new QHBoxLayout;
w->setLayout(t);
combo1 = new QComboBox;
combo1->addItem("blue");
combo1->addItem("red");
combo1->addItem("green");
combo1->addItem("gray");
combo1->addItem("yellow");
t->addWidget(combo1);
w->show();
b = new QToolButton;
b->setText("OK");
t->addWidget(b);
//connect(b, SIGNAL(triggered()), this, SLOT(openFile()));
//connect(b, SIGNAL(clicked()), this, SLOT(setColor()));
}*/
QRectF Square::boundingRect() const
{
return QRectF(0, 0, 110, 70);
}
int Square::type() const
{
return 0;
}
QPainterPath Square::shape() const
{
QPainterPath path;
path.addRect(14, 14, 82, 42);
return path;
}
void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
QColor fillColor = color;
QPen oldPen = painter->pen();
QPen pen = oldPen;
int width = 0;
if (option->state & QStyle::State_Selected)
width += 2;
pen.setWidth(width);
QBrush b = painter->brush();
painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
painter->drawRect(QRect(14, 14, 79, 39));
painter->setBrush(b);
painter->setPen(QPen(Qt::gray, 1));
painter->drawLine(15, 54, 94, 54);
painter->drawLine(94, 53, 94, 15);
painter->setPen(QPen(Qt::black, 0));
QFont font("Times", 10);
font.setStyleStrategy(QFont::ForceOutline);
painter->setFont(font);
painter->save();
painter->drawText(20,40, dispText);
painter->restore();
}
void Square::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
std::cout << "you right-clicked on Square" << std::endl;
//popUpCombo();
//setColor(0);
return;
}
QGraphicsItem::mousePressEvent(event);
//std::cout <<"Start (" << this->pos().x() << ", " << this->pos().y() << ") ";
update();
}
void Square::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (event->modifiers() & Qt::ShiftModifier) {
stuff << event->pos();
update();
return;
}
QGraphicsItem::mouseMoveEvent(event);
}
void Square::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseReleaseEvent(event);
update();
}