diff options
| -rw-r--r-- | GUI_SS2015.pro | 10 | ||||
| -rw-r--r-- | ellipse.cpp | 41 | ||||
| -rw-r--r-- | ellipse.h | 18 | ||||
| -rw-r--r-- | interactioncanvas.cpp | 94 | ||||
| -rw-r--r-- | interactioncanvas.h | 16 | ||||
| -rw-r--r-- | line.cpp | 6 | ||||
| -rw-r--r-- | line.h | 7 | ||||
| -rw-r--r-- | mainwindow.cpp | 20 | ||||
| -rw-r--r-- | mainwindow.h | 4 | ||||
| -rw-r--r-- | mainwindow.ui | 42 | ||||
| -rw-r--r-- | polygon.cpp | 47 | ||||
| -rw-r--r-- | polygon.h | 22 | ||||
| -rw-r--r-- | rectangle.cpp | 42 | ||||
| -rw-r--r-- | rectangle.h | 17 |
14 files changed, 355 insertions, 31 deletions
diff --git a/GUI_SS2015.pro b/GUI_SS2015.pro index 6db3c44..49d6038 100644 --- a/GUI_SS2015.pro +++ b/GUI_SS2015.pro @@ -17,13 +17,19 @@ SOURCES += main.cpp\ dlgpreferences.cpp \ interactioncanvas.cpp \ languagedialog.cpp \ - line.cpp + line.cpp \ + rectangle.cpp \ + ellipse.cpp \ + polygon.cpp HEADERS += mainwindow.h \ dlgpreferences.h \ interactioncanvas.h \ languagedialog.h \ - line.h + line.h \ + rectangle.h \ + ellipse.h \ + polygon.h FORMS += mainwindow.ui \ dlgpreferences.ui \ diff --git a/ellipse.cpp b/ellipse.cpp new file mode 100644 index 0000000..0377b66 --- /dev/null +++ b/ellipse.cpp @@ -0,0 +1,41 @@ +#include "ellipse.h" + +Ellipse::Ellipse() +{ + +} + +bool Ellipse::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 Ellipse::draw(QPainter *painter) +{ + if (m_selected) + { + QPen penTemp(Qt::DotLine); + penTemp.setColor(Qt::red); + painter->setPen(penTemp); + + painter->drawEllipse(QPoint(p1().rx() - (p2().rx() - p1().rx()), p1().ry()), 5, 5); + painter->drawEllipse(QPoint(p1().rx() + (p2().rx() - p1().rx()), p1().ry()), 5, 5); + painter->drawEllipse(QPoint(p1().rx(), p1().ry() + (p2().ry() - p1().ry())), 5, 5); + painter->drawEllipse(QPoint(p1().rx(), p1().ry() - (p2().ry() - p1().ry())), 5, 5); + } + else + { + QPen penNormal(Qt::SolidLine); + penNormal.setColor(Qt::black); + painter->setPen(penNormal); + } + + painter->drawEllipse(p1(), p2().rx() - p1().rx(), p2().ry() - p1().ry()); +} diff --git a/ellipse.h b/ellipse.h new file mode 100644 index 0000000..5f3fced --- /dev/null +++ b/ellipse.h @@ -0,0 +1,18 @@ +#ifndef ELLIPSE_H +#define ELLIPSE_H + +#include "rectangle.h" + + +class Ellipse : public Rectangle +{ +public: + Ellipse(); + + // Line interface +public: + virtual bool isHit(const QPoint &clickPoint); + virtual void draw(QPainter *painter); +}; + +#endif // ELLIPSE_H diff --git a/interactioncanvas.cpp b/interactioncanvas.cpp index 7bd8de8..1c4e54f 100644 --- a/interactioncanvas.cpp +++ b/interactioncanvas.cpp @@ -1,6 +1,7 @@ #include "interactioncanvas.h" -InteractionCanvas::InteractionCanvas() +InteractionCanvas::InteractionCanvas(QWidget *parent) + :QLabel(parent) { setFocusPolicy(Qt::StrongFocus); setMouseTracking(true); @@ -10,7 +11,65 @@ InteractionCanvas::InteractionCanvas() InteractionCanvas::~InteractionCanvas() { + for(int i = 0; i < m_Lines.size(); i++) + { + delete m_Lines[i]; + m_Lines.removeAt(i); + } + if(m_NewLine) + { + delete m_NewLine; + m_NewLine = NULL; + } + if(m_SelectedLine) + { + delete m_SelectedLine; + m_SelectedLine = NULL; + } +} +void InteractionCanvas::addLine() +{ + Line *tmpLine = new Line(); + tmpLine->setP1(QPoint(10, 50)); + tmpLine->setP2(QPoint(30, 50)); + addNewLine(tmpLine); + m_NewLine = NULL; + m_Lines.append(tmpLine); + changeSelectedLine(tmpLine); +} + +void InteractionCanvas::addRectangle() +{ + Line *tmpLine = new Rectangle(); + tmpLine->setP1(QPoint(10, 50)); + tmpLine->setP2(QPoint(30, 70)); + addNewLine(tmpLine); + m_NewLine = NULL; + m_Lines.append(tmpLine); + changeSelectedLine(tmpLine); +} + +void InteractionCanvas::addEllipse() +{ + Line *tmpLine = new Ellipse(); + tmpLine->setP1(QPoint(10, 50)); + tmpLine->setP2(QPoint(30, 70)); + addNewLine(tmpLine); + m_NewLine = NULL; + m_Lines.append(tmpLine); + changeSelectedLine(tmpLine); +} + +void InteractionCanvas::addPolygon() +{ + Line *tmpLine = new Polygon(); + tmpLine->setP1(QPoint(10, 50)); + tmpLine->setP2(QPoint(30, 70)); + addNewLine(tmpLine); + m_NewLine = NULL; + m_Lines.append(tmpLine); + changeSelectedLine(tmpLine); } void InteractionCanvas::mouseMoveEvent(QMouseEvent *mouseEvent) @@ -42,15 +101,9 @@ void InteractionCanvas::mousePressEvent(QMouseEvent *mouseEvent) { if(!m_SelectedLine) { - if (m_NewLine) - { - delete m_NewLine; - m_NewLine = NULL; - } - - m_NewLine = new Line(); m_MousePressPoint = mouseEvent->pos(); m_LastMousePosition = m_MousePressPoint; + addNewLine(new Line); m_NewLine->setP1(m_MousePressPoint); m_NewLine->setP2(m_LastMousePosition); } @@ -61,9 +114,9 @@ void InteractionCanvas::mousePressEvent(QMouseEvent *mouseEvent) changeSelectedLine(NULL); for(int i = 0; i < m_Lines.size() && !m_SelectedLine; i++) { - if(m_Lines[i].isHit(mouseEvent->pos())) + if(m_Lines[i]->isHit(mouseEvent->pos())) { - changeSelectedLine(&(m_Lines[i])); + changeSelectedLine(m_Lines[i]); } } } @@ -81,7 +134,7 @@ void InteractionCanvas::mouseReleaseEvent(QMouseEvent *mouseEvent) m_MouseReleasePoint = mouseEvent->pos(); m_NewLine->setP2(m_MouseReleasePoint); m_NewLine->setSelected(false); - m_Lines.append(*m_NewLine); + m_Lines.append(m_NewLine); m_NewLine = NULL; } } @@ -96,6 +149,7 @@ void InteractionCanvas::keyPressEvent(QKeyEvent *keyEvent) if(!m_Lines.isEmpty() && keyEvent->key() == Qt::Key_Z && keyEvent->modifiers() == Qt::ControlModifier) { + delete m_Lines.last(); m_Lines.removeLast(); update(); } @@ -126,6 +180,20 @@ void InteractionCanvas::changeSelectedLine(Line *newSelectedLine) } } +void InteractionCanvas::addNewLine(Line *newLine) +{ + if (m_NewLine) + { + delete m_NewLine; + m_NewLine = NULL; + } + + if(newLine) + { + m_NewLine = new Line(); + } +} + void InteractionCanvas::paintEvent(QPaintEvent *paintEvent) { // Setup the Painters @@ -136,8 +204,8 @@ void InteractionCanvas::paintEvent(QPaintEvent *paintEvent) m_NewLine->draw(&painter); } - foreach (Line line, m_Lines) + foreach (Line *line, m_Lines) { - line.draw(&painter); + line->draw(&painter); } } diff --git a/interactioncanvas.h b/interactioncanvas.h index e380d47..397865d 100644 --- a/interactioncanvas.h +++ b/interactioncanvas.h @@ -9,14 +9,21 @@ #include <QPainter> #include "line.h" +#include "rectangle.h" +#include "ellipse.h" +#include "polygon.h" class InteractionCanvas : public QLabel { public: - InteractionCanvas(); + InteractionCanvas(QWidget *parent); ~InteractionCanvas(); - // QWidget interface + void addLine(); + void addRectangle(); + void addEllipse(); + void addPolygon(); + protected: void mouseMoveEvent(QMouseEvent *mouseEvent); void mousePressEvent(QMouseEvent *mouseEvent); @@ -26,14 +33,15 @@ protected: private: void changeSelectedLine(Line *newSelectedLine); - QList<Line> m_Lines; + void addNewLine(Line *newLine); + + QList<Line *> m_Lines; Line *m_NewLine; Line *m_SelectedLine; QPoint m_MousePressPoint; QPoint m_MouseReleasePoint; QPoint m_LastMousePosition; - // QWidget interface protected: void paintEvent(QPaintEvent *paintEvent); }; @@ -6,12 +6,6 @@ Line::Line() 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()); @@ -9,13 +9,12 @@ class Line : public QLine { public: Line(); - Line(const Line &other); - bool isHit(const QPoint & clickPoint); + virtual bool isHit(const QPoint & clickPoint); void setSelected(bool selected); - void draw(QPainter * painter); + virtual void draw(QPainter * painter); -private: +protected: bool m_selected; }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 8bc0a22..656b8c9 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -6,7 +6,6 @@ MainWindow::MainWindow(QWidget *parent) : ui(new Ui::MainWindow) { ui->setupUi(this); - setCentralWidget(new InteractionCanvas()); } MainWindow::~MainWindow() @@ -76,6 +75,25 @@ void MainWindow::on_actionLanguage_triggered() } } +void MainWindow::on_actionDraw_Line_triggered() +{ + ui->interactionCanvasPlaceholder->addLine(); +} + +void MainWindow::on_actionDraw_Rectangle_triggered() +{ + ui->interactionCanvasPlaceholder->addRectangle(); +} + +void MainWindow::on_actionDraw_Ellipse_triggered() +{ + ui->interactionCanvasPlaceholder->addEllipse(); +} + +void MainWindow::on_actionDraw_Polygon_triggered() +{ + ui->interactionCanvasPlaceholder->addPolygon(); +} void MainWindow::mouseMoveEvent(QMouseEvent *mouseEvent) { diff --git a/mainwindow.h b/mainwindow.h index 10976c4..43dbb29 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -31,6 +31,10 @@ protected: private slots: void on_actionPreferences_triggered(); void on_actionLanguage_triggered(); + void on_actionDraw_Line_triggered(); + void on_actionDraw_Rectangle_triggered(); + void on_actionDraw_Ellipse_triggered(); + void on_actionDraw_Polygon_triggered(); void changeEvent(QEvent *event); private: diff --git a/mainwindow.ui b/mainwindow.ui index 209a4b9..9e55b3a 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -14,7 +14,15 @@ <string>GUIProjekt SS2015_SS</string> </property> <widget class="QWidget" name="centralWidget"> - <layout class="QHBoxLayout" name="horizontalLayout"/> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="InteractionCanvas" name="interactionCanvasPlaceholder"> + <property name="text"> + <string/> + </property> + </widget> + </item> + </layout> </widget> <widget class="QStatusBar" name="statusBar"/> <widget class="QMenuBar" name="menuBar"> @@ -38,6 +46,11 @@ </property> <addaction name="actionPreferences"/> <addaction name="actionLanguage"/> + <addaction name="separator"/> + <addaction name="actionDraw_Line"/> + <addaction name="actionDraw_Rectangle"/> + <addaction name="actionDraw_Ellipse"/> + <addaction name="actionDraw_Polygon"/> </widget> <widget class="QMenu" name="menuHelp"> <property name="title"> @@ -72,8 +85,35 @@ <string>&Language</string> </property> </action> + <action name="actionDraw_Line"> + <property name="text"> + <string>Draw L&ine</string> + </property> + </action> + <action name="actionDraw_Rectangle"> + <property name="text"> + <string>Draw &Rectangle</string> + </property> + </action> + <action name="actionDraw_Ellipse"> + <property name="text"> + <string>Draw &Ellipse</string> + </property> + </action> + <action name="actionDraw_Polygon"> + <property name="text"> + <string>Draw P&olygon</string> + </property> + </action> </widget> <layoutdefault spacing="6" margin="11"/> + <customwidgets> + <customwidget> + <class>InteractionCanvas</class> + <extends>QLabel</extends> + <header>interactioncanvas.h</header> + </customwidget> + </customwidgets> <resources/> <connections> <connection> diff --git a/polygon.cpp b/polygon.cpp new file mode 100644 index 0000000..a292653 --- /dev/null +++ b/polygon.cpp @@ -0,0 +1,47 @@ +#include "polygon.h" + +Polygon::Polygon() + : Line() +{ + m_polygon.append(QPoint(0,0)); + m_polygon.append(QPoint(0,0)); + m_polygon.append(QPoint(0,0)); +} + +bool Polygon::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 Polygon::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); + painter->drawEllipse(QPoint(p1().rx() + 20, p1().ry() - 20), 5, 5); + } + else + { + QPen penNormal(Qt::SolidLine); + penNormal.setColor(Qt::black); + painter->setPen(penNormal); + } + + m_polygon.replace(0, p1()); + m_polygon.replace(1, p2()); + m_polygon.replace(2, QPoint(p1().rx() + 20, p1().ry() - 20)); + + painter->drawPolygon(m_polygon); +} diff --git a/polygon.h b/polygon.h new file mode 100644 index 0000000..54c8c4e --- /dev/null +++ b/polygon.h @@ -0,0 +1,22 @@ +#ifndef POLYGON_H +#define POLYGON_H + +#include <QPolygon> + +#include "line.h" + +class Polygon : public Line +{ +public: + Polygon(); + + // Line interface +public: + virtual bool isHit(const QPoint &clickPoint); + virtual void draw(QPainter *painter); + +private: + QPolygon m_polygon; +}; + +#endif // POLYGON_H diff --git a/rectangle.cpp b/rectangle.cpp new file mode 100644 index 0000000..f07651c --- /dev/null +++ b/rectangle.cpp @@ -0,0 +1,42 @@ +#include "rectangle.h" + +Rectangle::Rectangle() + : Line() +{ + +} + +bool Rectangle::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 Rectangle::draw(QPainter *painter) +{ + if (m_selected) + { + QPen penTemp(Qt::DotLine); + penTemp.setColor(Qt::red); + painter->setPen(penTemp); + + painter->drawEllipse(p1(), 5, 5); + painter->drawEllipse(QPoint(p1().rx() + (p2().rx() - p1().rx()), p1().ry()), 5, 5); + painter->drawEllipse(QPoint(p1().rx(), p1().ry() + (p2().ry() - p1().ry())), 5, 5); + painter->drawEllipse(p2(), 5, 5); + } + else + { + QPen penNormal(Qt::SolidLine); + penNormal.setColor(Qt::black); + painter->setPen(penNormal); + } + + painter->drawRect(QRect(p1(), p2())); +} diff --git a/rectangle.h b/rectangle.h new file mode 100644 index 0000000..5e9445d --- /dev/null +++ b/rectangle.h @@ -0,0 +1,17 @@ +#ifndef RECTANGLE_H +#define RECTANGLE_H + +#include "line.h" + +class Rectangle : public Line +{ +public: + Rectangle(); + + // Line interface +public: + virtual bool isHit(const QPoint &clickPoint); + virtual void draw(QPainter *painter); +}; + +#endif // RECTANGLE_H |
