-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmygraphicalview.cpp
More file actions
73 lines (63 loc) · 1.83 KB
/
mygraphicalview.cpp
File metadata and controls
73 lines (63 loc) · 1.83 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
#include "myqgraphicsview.h"
MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
QGraphicsView(parent)
{
scene = new QGraphicsScene();
this->setSceneRect(0, 0, 150, 451);
this->setScene(scene);
}
void MyQGraphicsView::mousePressEvent(QMouseEvent * e)
{
double rad = 10;
QPointF pt = mapToScene(e->pos());
/*scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0,
QPen(), QBrush(Qt::SolidPattern));*/
emit clickedOnMe(pt);
}
void MyQGraphicsView::changeSize(int width, int height)
{
//if (this->scene->height() <= height)
{
this->setSceneRect(0, 0, width-20, height-50);
this->setScene(scene);
}
}
void MyQGraphicsView::clearScene()
{
this->scene->clear();
}
void MyQGraphicsView::addRectangle(QString name, int type, int sectionNumb)
{
QBrush *brush = new QBrush();
if (type == 0) //header table
{
//qDebug() << "type == 0";
brush->setColor(QColor(255, 0, 0, 255));
}
else if (type == 1) //segment
{
brush->setColor(QColor(255, 100, 0, 255));
}
else //section
{
brush->setColor(QColor(255, 204, 0, 255));
}
QGraphicsRectItem * rect = new QGraphicsRectItem;
rect->setPen(QColor("black"));
rect->setRect(50 + type * 20, sectionNumb * 35 + 10, 200, 25);
rect->setBrush(brush->color());
QGraphicsTextItem * txt = new QGraphicsTextItem;
QFont font("Arial", 10, QFont::Bold);
QFontMetrics fm(font);
int pixWidth = fm.width(name);
int posOfText = 100 - pixWidth / 2;
txt->setFont(font);
txt->setPos(50 + type * 20 + posOfText, sectionNumb * 35 + 10);
txt->setPlainText(name);
scene->addItem(rect);
scene->addItem(txt);
}
void MyQGraphicsView::drawLine(int xfrom, int yfrom, int xto, int yto)
{
scene->addLine(xfrom, yfrom, xto, yto, QPen(Qt::black, 2));
}