| 1 | #include "rvuwidget.h" | 
| 2 |  | 
| 3 | #include <QApplication> | 
| 4 | #include <QCursor> | 
| 5 | #include <QColor> | 
| 6 | #include <QPainter> | 
| 7 | #include <QPixmap> | 
| 8 | #include <QMouseEvent> | 
| 9 |  | 
| 10 | RvuWidget::RvuWidget(QWidget* parent) : QWidget(parent), m_x(0), m_y(0) | 
| 11 | { | 
| 12 | m_image = new QPixmap(200, 200); | 
| 13 | m_image->fill(QColor(0, 0, 0)); | 
| 14 | m_painter = new QPainter(m_image); | 
| 15 | m_do_pick = false; | 
| 16 | } | 
| 17 |  | 
| 18 | RvuWidget::~RvuWidget() | 
| 19 | { | 
| 20 | m_painter->end(); | 
| 21 | delete m_painter; | 
| 22 | delete m_image; | 
| 23 | } | 
| 24 |  | 
| 25 | void RvuWidget::resizeImage(int X, int Y) | 
| 26 | { | 
| 27 | m_painter->end(); | 
| 28 | delete m_image; | 
| 29 | m_image =  new QPixmap(X, Y); | 
| 30 | m_image->fill(QColor(0, 0, 0)); | 
| 31 | m_painter->begin(m_image); | 
| 32 | m_painter->setPen(Qt::NoPen); | 
| 33 | } | 
| 34 |  | 
| 35 | void RvuWidget::drawRect(int x, int y, int width, int height, | 
| 36 | const QColor &color) | 
| 37 | { | 
| 38 | m_painter->fillRect(x, y, width, height, color); | 
| 39 | } | 
| 40 |  | 
| 41 | void RvuWidget::getPosition(int *x, int *y) | 
| 42 | { | 
| 43 | this->m_do_pick = true; | 
| 44 | while(this->m_do_pick) | 
| 45 | { | 
| 46 | QApplication::processEvents(); | 
| 47 | } | 
| 48 | *x = m_x; | 
| 49 | *y = m_y; | 
| 50 | } | 
| 51 |  | 
| 52 | void RvuWidget::paintEvent(QPaintEvent *) | 
| 53 | { | 
| 54 | QPainter painter(this); | 
| 55 | // Draw QImage | 
| 56 | painter.drawPixmap(0, 0, *m_image); | 
| 57 |  | 
| 58 | // Draw the crosshairs | 
| 59 | painter.setPen(QColor(0, 255, 0)); | 
| 60 | painter.drawLine(m_x - 10, m_y, m_x + 10, m_y); | 
| 61 | painter.drawLine(m_x, m_y - 10, m_x, m_y + 10); | 
| 62 | } | 
| 63 |  | 
| 64 | void RvuWidget::mousePressEvent(QMouseEvent *event) | 
| 65 | { | 
| 66 | if (event->button() == Qt::LeftButton) | 
| 67 | { | 
| 68 | this->m_do_pick = false; | 
| 69 | // Set the cursor position | 
| 70 | m_x = event->x(); | 
| 71 | m_y = event->y(); | 
| 72 | } | 
| 73 | this->repaint(); | 
| 74 | } | 
| 75 |  | 
| 76 | void RvuWidget::setPosition(int x, int y) | 
| 77 | { | 
| 78 | m_x = x; | 
| 79 | m_y = y; | 
| 80 | this->repaint(); | 
| 81 | } |