1 #include <QPainter> |
1 #include <QPainter> |
2 #include <QPen> |
2 #include <QPen> |
3 #include "pixlabel.h" |
3 #include "pixlabel.h" |
4 |
4 |
5 PixLabel::PixLabel() |
5 PixLabel::PixLabel() |
6 : QLabel(0) |
6 : QLabel(0) |
7 { |
7 { |
8 |
8 |
9 } |
9 } |
10 |
10 |
11 void PixLabel::paintEvent(QPaintEvent * event) |
11 void PixLabel::paintEvent(QPaintEvent * event) |
12 { |
12 { |
13 QLabel::paintEvent(event); |
13 QLabel::paintEvent(event); |
14 QPainter p(this); |
14 QPainter p(this); |
15 |
15 |
16 p.fillRect(QRect(0, 0, 1024, 512), QBrush(Qt::black)); |
16 p.fillRect(QRect(0, 0, 1024, 512), QBrush(Qt::black)); |
17 |
17 |
18 if (rects.size()) |
18 if (rects.size()) |
19 { |
19 { |
20 p.setPen(QPen(Qt::lightGray)); |
20 p.setPen(QPen(Qt::lightGray)); |
21 QVector<QPoint> centers; |
21 QVector<QPoint> centers; |
22 for(QList<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it) |
22 for(QList<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it) |
23 centers.push_back((*it).center()); |
23 centers.push_back((*it).center()); |
24 p.drawPolyline(QPolygon(centers)); |
24 p.drawPolyline(QPolygon(centers)); |
25 |
25 |
26 p.setPen(QPen(Qt::white)); |
26 p.setPen(QPen(Qt::white)); |
27 p.drawRects(rects.toVector()); |
27 p.drawRects(rects.toVector()); |
28 |
28 |
29 p.setPen(QPen(Qt::yellow)); |
29 p.setPen(QPen(Qt::yellow)); |
30 p.drawRect(rects.last()); |
30 p.drawRect(rects.last()); |
31 } |
31 } |
32 } |
32 } |
33 |
33 |
34 void PixLabel::mousePressEvent(QMouseEvent * e) |
34 void PixLabel::mousePressEvent(QMouseEvent * e) |
35 { |
35 { |
36 if (!rects.empty()) |
36 if (!rects.empty()) |
37 { |
37 { |
38 if (e->button() == Qt::LeftButton) |
38 if (e->button() == Qt::LeftButton) |
39 rects[rects.size() - 1].moveTopLeft(QPoint(e->x(), e->y())); |
39 rects[rects.size() - 1].moveTopLeft(QPoint(e->x(), e->y())); |
40 else |
40 else |
41 if (e->button() == Qt::RightButton) |
41 if (e->button() == Qt::RightButton) |
42 rects[rects.size() - 1].setBottomRight(QPoint(e->x(), e->y())); |
42 rects[rects.size() - 1].setBottomRight(QPoint(e->x(), e->y())); |
43 repaint(); |
43 repaint(); |
44 } |
44 } |
45 } |
45 } |
46 |
46 |
47 void PixLabel::AddRect() |
47 void PixLabel::AddRect() |
48 { |
48 { |
49 rects.push_back(QRect(0, 0, 1, 1)); |
49 rects.push_back(QRect(0, 0, 1, 1)); |
50 repaint(); |
50 repaint(); |
51 } |
51 } |