-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
111 lines (93 loc) · 2.8 KB
/
widget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "widget.h"
#include <QDebug>
#include <QEvent>
#include <QFileDialog>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPainter>
#include <QString>
#include <cereal/archives/json.hpp>
#include <cereal/types/string.hpp>
#include <cereal/types/vector.hpp>
#include <fstream>
#include "./ui_widget.h"
void Widget::Render(QPaintDevice *PaintDevice) {
QPainter painter(PaintDevice);
painter.setRenderHint(QPainter::Antialiasing, true);
auto OrigPen = painter.pen();
if (DrawText) {
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 30));
painter.drawText(rect(), Qt::AlignCenter, "Qt");
}
painter.setPen(OrigPen);
if (Path.size() >= 2) {
for (int q = 0; q < Path.size() - 1; q++) {
painter.drawLine(Path.at(q), Path.at(q + 1));
}
}
if (Path.size() > 0) {
painter.drawLine(Path.back(), QPoint(CoordX, CoordY));
}
}
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
ui->setupUi(this);
setMouseTracking(true);
installEventFilter(this);
}
Widget::~Widget() { delete ui; }
bool Widget::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::MouseMove) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
CoordX = mouseEvent->pos().x();
CoordY = mouseEvent->pos().y();
}
update();
return false;
}
void Widget::mousePressEvent(QMouseEvent *event) {
Path.push_back(event->pos());
}
void Widget::mouseReleaseEvent(QMouseEvent *event) { update(); }
void Widget::paintEvent(QPaintEvent *event) { Render(this); }
void Widget::on_pushButton_clicked() {
DrawText = true;
update();
}
void Widget::on_saveImageButton_clicked() {
QString fileName = QFileDialog::getSaveFileName(
this, tr("Save File"),
"/home/ujoimro/doc/university/sje/2020_21_osz/c++ programozás",
tr("Images (*.png)"));
if (!fileName.isEmpty()) {
QImage image(1024, 1024, QImage::Format_RGB32);
image.fill(QColor(255, 255, 255));
Render(&image);
image.save(fileName);
}
}
void Widget::on_SaveJsonButton_clicked() {
auto fileName = QFileDialog::getSaveFileName(
this, tr("Save Image Json"), "/home/ujoimro", tr("Image Files (*.json)"));
if (!fileName.isEmpty()) {
std::ofstream ofs(fileName.toStdString());
cereal::JSONOutputArchive archive(ofs);
archive(*this);
}
}
void Widget::on_LoadJsonButton_clicked() {
auto fileName = QFileDialog::getOpenFileName(
this, tr("Save Image Json"), "/home/ujoimro", tr("Image Files (*.json)"));
if (!fileName.isEmpty()) {
std::ifstream ifs(fileName.toStdString());
cereal::JSONInputArchive archive(ifs);
archive(*this);
}
Render(this);
}
void Widget::on_aboutButton_clicked() {
QMessageBox msgBox;
msgBox.setText("Created by SJE.");
msgBox.exec();
}
void Widget::on_guitButton_clicked() { QApplication::exit(); }