From 1faecafbc6cc74daa8d0a781c0b54f2c778b04b0 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sat, 16 May 2015 23:41:43 +0200 Subject: Add 2d drawing for lines. --- GUI_SS2015.pro | 6 +++-- interactioncanvas.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++-- interactioncanvas.h | 18 +++++++++++++ line.cpp | 52 ++++++++++++++++++++++++++++++++++++ line.h | 22 ++++++++++++++++ 5 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 line.cpp create mode 100644 line.h diff --git a/GUI_SS2015.pro b/GUI_SS2015.pro index 263d369..6db3c44 100644 --- a/GUI_SS2015.pro +++ b/GUI_SS2015.pro @@ -16,12 +16,14 @@ SOURCES += main.cpp\ mainwindow.cpp \ dlgpreferences.cpp \ interactioncanvas.cpp \ - languagedialog.cpp + languagedialog.cpp \ + line.cpp HEADERS += mainwindow.h \ dlgpreferences.h \ interactioncanvas.h \ - languagedialog.h + languagedialog.h \ + line.h FORMS += mainwindow.ui \ dlgpreferences.ui \ diff --git a/interactioncanvas.cpp b/interactioncanvas.cpp index c289c40..2c2b81b 100644 --- a/interactioncanvas.cpp +++ b/interactioncanvas.cpp @@ -4,33 +4,85 @@ InteractionCanvas::InteractionCanvas() { setFocusPolicy(Qt::StrongFocus); setMouseTracking(true); + m_Line = NULL; } InteractionCanvas::~InteractionCanvas() { - + delete tempPainter; + tempPainter = NULL; + delete normalPainter; + normalPainter = NULL; } void InteractionCanvas::mouseMoveEvent(QMouseEvent *mouseEvent) { qDebug() << "InteractionCanvas:" << mouseEvent->pos(); + m_LastMousePosition = mouseEvent->pos(); + + if (m_Line) + { + m_Line->setP2(m_LastMousePosition); + update(); + } } void InteractionCanvas::mousePressEvent(QMouseEvent *mouseEvent) { qDebug() << "InteractionCanvas: Mouse: pressed:" << mouseEvent->button(); + + if (mouseEvent->button() == Qt::LeftButton) + { + if (m_Line) + { + delete m_Line; + m_Line = NULL; + } + + m_Line = new Line(); + m_MousePressPoint = mouseEvent->pos(); + m_LastMousePosition = m_MousePressPoint; + m_Line->setP1(m_MousePressPoint); + m_Line->setP2(m_LastMousePosition); + } + + if (mouseEvent->button() == Qt::RightButton) + { + for(int i = 0; i < m_Lines.size(); i++) + { + m_Lines[i].setSelected(m_Lines[i].isHit(mouseEvent->pos())); + } + } } void InteractionCanvas::mouseReleaseEvent(QMouseEvent *mouseEvent) { - qDebug() << "InteractionCanvas: Mouse: released:"<< mouseEvent->button(); + qDebug() << "InteractionCanvas: Mouse: released:" << mouseEvent->button(); + + if (mouseEvent->button() == Qt::LeftButton) + { + + m_MouseReleasePoint = mouseEvent->pos(); + m_Line->setP2(m_MouseReleasePoint); + m_Line->setSelected(false); + m_Lines.append(*m_Line); + m_Line = NULL; + } + + update(); } void InteractionCanvas::keyPressEvent(QKeyEvent *keyEvent) { qDebug() << "InteractionCanvas: Key: pressed:" << keyEvent->key() << "(" << keyEvent->text() << ")" << (keyEvent->isAutoRepeat() ? "druck" : ""); + + if(!m_Lines.isEmpty() && keyEvent->key() == Qt::Key_Z && keyEvent->modifiers() == Qt::ControlModifier) + { + m_Lines.removeLast(); + update(); + } } void InteractionCanvas::keyReleaseEvent(QKeyEvent *keyEvent) @@ -38,3 +90,20 @@ void InteractionCanvas::keyReleaseEvent(QKeyEvent *keyEvent) qDebug() << "InteractionCanvas: Key: released:" << keyEvent->key() << "(" << keyEvent->text() << ")" << (keyEvent->isAutoRepeat() ? "druck" : ""); } + + +void InteractionCanvas::paintEvent(QPaintEvent *paintEvent) +{ + // Setup the Painters + QPainter painter(this); + + if (m_Line) + { + m_Line->draw(&painter); + } + + foreach (Line line, m_Lines) + { + line.draw(&painter); + } +} diff --git a/interactioncanvas.h b/interactioncanvas.h index efdf9a2..af5d9b6 100644 --- a/interactioncanvas.h +++ b/interactioncanvas.h @@ -4,6 +4,11 @@ #include #include #include +#include +#include +#include + +#include "line.h" class InteractionCanvas : public QLabel { @@ -19,6 +24,19 @@ protected: void keyPressEvent(QKeyEvent *keyEvent); void keyReleaseEvent(QKeyEvent *keyEvent); +private: + QList m_Lines; + Line *m_Line; + QPoint m_MousePressPoint; + QPoint m_MouseReleasePoint; + QPoint m_LastMousePosition; + + QPainter *normalPainter; + QPainter *tempPainter; + + // QWidget interface +protected: + void paintEvent(QPaintEvent *paintEvent); }; #endif // INTERACTIONCANVAS_H diff --git a/line.cpp b/line.cpp new file mode 100644 index 0000000..aa29d43 --- /dev/null +++ b/line.cpp @@ -0,0 +1,52 @@ +#include "line.h" + +Line::Line() + :QLine() +{ + m_selected = true; +} + +Line::Line(const Line &other) + :QLine(other) +{ + m_selected = other.m_selected; +} + +bool Line::isHit(const QPoint &clickPoint) +{ + QVector3D a(p2() - p1()); + QVector3D r1(p1()); + + QVector3D rq(clickPoint); + + float dist = rq.distanceToLine(r1, a.normalized()); + + return dist < 5; +} + +void Line::setSelected(bool selected) +{ + m_selected = selected; +} + +void Line::draw(QPainter *painter) +{ + if(m_selected) + { + QPen penTemp(Qt::DotLine); + penTemp.setColor(Qt::red); + painter->setPen(penTemp); + + painter->drawEllipse(p1(), 5, 5); + painter->drawEllipse(p2(), 5, 5); + } + else + { + QPen penNormal(Qt::SolidLine); + penNormal.setColor(Qt::black); + painter->setPen(penNormal); + } + + painter->drawLine(p1(), p2()); + +} diff --git a/line.h b/line.h new file mode 100644 index 0000000..745f653 --- /dev/null +++ b/line.h @@ -0,0 +1,22 @@ +#ifndef LINE_H +#define LINE_H + +#include +#include +#include + +class Line : public QLine +{ +public: + Line(); + Line(const Line &other); + + bool isHit(const QPoint & clickPoint); + void setSelected(bool selected); + void draw(QPainter * painter); + +private: + bool m_selected; +}; + +#endif // LINE_H -- cgit v1.2.3-70-g09d2