diff --git a/app/gui/oven_control/canvas.cpp b/app/gui/oven_control/canvas.cpp new file mode 100644 index 0000000..4f7ccf3 --- /dev/null +++ b/app/gui/oven_control/canvas.cpp @@ -0,0 +1,83 @@ +#include "canvas.h" + +#include <QMouseEvent> +#include <QPainter> + +Canvas::Canvas(QWidget *parent) : QWidget(parent) +{ + setAttribute(Qt::WA_StaticContents); + scribbling = false; + myPenWidth = 1; + myPenColor = Qt::blue; +} + +void Canvas::clearImage() +{ + image.fill(qRgb(255, 255, 255)); + update(); +} + +void Canvas::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + lastPoint = event->pos(); + scribbling = true; + } +} + +void Canvas::mouseMoveEvent(QMouseEvent *event) +{ + if ((event->buttons() & Qt::LeftButton) && scribbling) + drawLineTo(event->pos()); +} + +void Canvas::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton && scribbling) { + drawLineTo(event->pos()); + scribbling = false; + } +} + +void Canvas::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + QRect dirtyRect = event->rect(); + painter.drawImage(dirtyRect, image, dirtyRect); +} + +void Canvas::resizeEvent(QResizeEvent *event) +{ + if (width() > image.width() || height() > image.height()) { + int newWidth = qMax(width() + 128, image.width()); + int newHeight = qMax(height() + 128, image.height()); + resizeImage(&image, QSize(newWidth, newHeight)); + update(); + } + QWidget::resizeEvent(event); +} + +void Canvas::drawLineTo(const QPoint &endPoint) +{ + QPainter painter(&image); + painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, + Qt::RoundJoin)); + painter.drawLine(lastPoint, endPoint); + + int rad = (myPenWidth / 2) + 2; + update(QRect(lastPoint, endPoint).normalized() + .adjusted(-rad, -rad, +rad, +rad)); + lastPoint = endPoint; +} + +void Canvas::resizeImage(QImage *image, const QSize &newSize) +{ + if (image->size() == newSize) + return; + + QImage newImage(newSize, QImage::Format_RGB32); + newImage.fill(qRgb(255, 255, 255)); + QPainter painter(&newImage); + painter.drawImage(QPoint(0, 0), *image); + *image = newImage; +} diff --git a/app/gui/oven_control/canvas.h b/app/gui/oven_control/canvas.h new file mode 100644 index 0000000..56989c9 --- /dev/null +++ b/app/gui/oven_control/canvas.h @@ -0,0 +1,38 @@ +#ifndef CANVAS_H +#define CANVAS_H + +#include <QWidget> +#include <QImage> +#include <QPoint> + +class Canvas : public QWidget +{ + Q_OBJECT +public: + explicit Canvas(QWidget *parent = 0); + +signals: + +public slots: + void clearImage(); + +protected: + void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; + +private: + void drawLineTo(const QPoint &endPoint); + void resizeImage(QImage *image, const QSize &newSize); + + + bool scribbling; + int myPenWidth; + QColor myPenColor; + QImage image; + QPoint lastPoint; +}; + +#endif // CANVAS_H diff --git a/app/gui/oven_control/oven_control.pro b/app/gui/oven_control/oven_control.pro index 3c7a6b7..909ad75 100644 --- a/app/gui/oven_control/oven_control.pro +++ b/app/gui/oven_control/oven_control.pro @@ -142,7 +142,9 @@ SOURCES += main.cpp\ multicookmanualwindow.cpp \ multicookautowindow.cpp \ multicookbook.cpp \ - haccp.cpp + haccp.cpp \ + canvas.cpp \ + touchtestwindow.cpp HEADERS += mainwindow.h \ @@ -275,7 +277,9 @@ HEADERS += mainwindow.h \ multicookmanualwindow.h \ multicookautowindow.h \ multicookbook.h \ - haccp.h + haccp.h \ + canvas.h \ + touchtestwindow.h FORMS += mainwindow.ui \ manualcookwindow.ui \ @@ -359,7 +363,8 @@ FORMS += mainwindow.ui \ multicookwindow.ui \ multicookselectionwindow.ui \ multicookmanualwindow.ui \ - multicookautowindow.ui + multicookautowindow.ui \ + touchtestwindow.ui RESOURCES += \ resources.qrc diff --git a/app/gui/oven_control/touchtestwindow.cpp b/app/gui/oven_control/touchtestwindow.cpp new file mode 100644 index 0000000..3064c13 --- /dev/null +++ b/app/gui/oven_control/touchtestwindow.cpp @@ -0,0 +1,26 @@ +#include "touchtestwindow.h" +#include "ui_touchtestwindow.h" + +TouchTestWindow::TouchTestWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::TouchTestWindow) +{ + ui->setupUi(this); + + setAttribute(Qt::WA_DeleteOnClose); +} + +TouchTestWindow::~TouchTestWindow() +{ + delete ui; +} + +void TouchTestWindow::on_clearButton_clicked() +{ + ui->canvas->clearImage(); +} + +void TouchTestWindow::on_closeButton_clicked() +{ + close(); +} diff --git a/app/gui/oven_control/touchtestwindow.h b/app/gui/oven_control/touchtestwindow.h new file mode 100644 index 0000000..05ce506 --- /dev/null +++ b/app/gui/oven_control/touchtestwindow.h @@ -0,0 +1,27 @@ +#ifndef TOUCHTESTWINDOW_H +#define TOUCHTESTWINDOW_H + +#include <QMainWindow> + +namespace Ui { +class TouchTestWindow; +} + +class TouchTestWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit TouchTestWindow(QWidget *parent = 0); + ~TouchTestWindow(); + +private slots: + void on_clearButton_clicked(); + + void on_closeButton_clicked(); + +private: + Ui::TouchTestWindow *ui; +}; + +#endif // TOUCHTESTWINDOW_H diff --git a/app/gui/oven_control/touchtestwindow.ui b/app/gui/oven_control/touchtestwindow.ui new file mode 100644 index 0000000..bbc02c9 --- /dev/null +++ b/app/gui/oven_control/touchtestwindow.ui @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>TouchTestWindow</class> + <widget class="QMainWindow" name="TouchTestWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>900</width> + <height>1600</height> + </rect> + </property> + <property name="windowTitle"> + <string>MainWindow</string> + </property> + <widget class="Canvas" name="canvas"> + <widget class="QPushButton" name="closeButton"> + <property name="geometry"> + <rect> + <x>850</x> + <y>0</y> + <width>50</width> + <height>50</height> + </rect> + </property> + <property name="styleSheet"> + <string notr="true">QPushButton { border: 1px solid black; } +QPushButton:pressed { background-color: red; }</string> + </property> + <property name="text"> + <string>X</string> + </property> + </widget> + <widget class="QPushButton" name="clearButton"> + <property name="geometry"> + <rect> + <x>800</x> + <y>0</y> + <width>50</width> + <height>50</height> + </rect> + </property> + <property name="styleSheet"> + <string notr="true">QPushButton { border: 1px solid black; } +QPushButton:pressed { background-color: red; }</string> + </property> + <property name="text"> + <string>C</string> + </property> + </widget> + </widget> + </widget> + <customwidgets> + <customwidget> + <class>Canvas</class> + <extends>QWidget</extends> + <header>canvas.h</header> + <container>1</container> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui>