-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.cpp
166 lines (152 loc) · 4.94 KB
/
editor.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "editor.h"
#include "ui_editor.h"
#include "draw.h"
editor::editor(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::editor)
{
ui->setupUi(this);
//jednotlive connecty, pri zmene stavu objektu na ui editor posle hodnotu do drawPlace
connect(ui->lineButton, SIGNAL(clicked()), ui->drawPlace, SLOT(shapeChangedtoLine()));
connect(ui->rectangleButton, SIGNAL(clicked()), ui->drawPlace, SLOT(shapeChangedtoRectangle()));
connect(ui->circleButton, SIGNAL(clicked()), ui->drawPlace, SLOT(shapeChangedtoCircle()));
connect(ui->polygonButton, SIGNAL(clicked()), ui->drawPlace, SLOT(shapeChangedtoPolygon()));
connect(ui->pickButton, SIGNAL(clicked()), ui->drawPlace, SLOT(pickModeOn()));
connect(ui->sizeSlider, SIGNAL(sliderMoved(int)), ui->drawPlace, SLOT(newWidth(int)));
connect(this, SIGNAL(changeColor(QColor)), ui->drawPlace, SLOT(newColor(QColor)));
connect(ui->rotateSlider, SIGNAL(sliderMoved(int)),ui->drawPlace, SLOT(rotate(int)));
connect(ui->fillBox, SIGNAL(toggled(bool)),ui->drawPlace, SLOT(fill(bool)));
//prvok ktory upravuje velkost canvasu je na zaciatku neviditelny
ui->canvasSize->setVisible(false);
}
editor::~editor()
{
delete ui;
}
//dorobeny ColorDialog na vyber farieb
//na zmenu sa vola osobitny changeColor signal
void editor::on_colorButton_clicked()
{
QColor newColor = QColorDialog::getColor();
emit changeColor(newColor);
}
//export do obrazkovych formatov
void editor::on_actionExport_triggered()
{
QString imagePath = QFileDialog::getSaveFileName(this,tr("Export Image as"),"", tr("JPEG (*.jpg *.jpeg);;PNG (*.png)" ));
ui->drawPlace->grab().save(imagePath);
}
//ulozenie do upravovatelneho .edf filu
void editor::on_actionSave_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this,tr("Save File"),"", tr("Editor File (*.edf);;All Files (*)" ));
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Nepodarilo sa otvoriť súbor"),
file.errorString());
return;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_5_7);
out << ui->drawPlace->toDraw.size();
for (auto && x : ui->drawPlace->toDraw) {
x->store(out);
}
file.close();
}
//zase otvorenie .edf filu
void editor::on_actionLoad_triggered()
{
//uvodny dialog
Tool t;
size_t count;
ObjectPtr objekt;
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), "",tr("Editor File (*.edf);;All Files (*)"));
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
//stream in na otvoreny file
QDataStream in(&file);
in.setVersion(QDataStream::Qt_5_7);
//vycisti sa toDraw
ui->drawPlace->toDraw.clear();
//vlozime udaj o pocte objektov
in >> count;
//nasypeme udaje objektoch, vytvorime objekty a ulozime do toDraw
for(size_t i = 0; i < count; i++) {
in >> t;
switch (t) {
case Tool::enRec:
{
Rectangle r;
r.load(in);
objekt = std::make_unique<Rectangle>(r);
ui->drawPlace->toDraw.push_back(std::move(objekt));
break;
}
case Tool::enTr:
{
Polygon p;
p.load(in);
objekt = std::make_unique<Polygon>(p);
ui->drawPlace->toDraw.push_back(std::move(objekt));
break;
}
case Tool::enCir:
{
Circle c;
c.load(in);
objekt = std::make_unique<Circle>(c);
ui->drawPlace->toDraw.push_back(std::move(objekt));
break;
}
case Tool::enLi:
{
Line l;
l.load(in);
objekt = std::make_unique<Line>(l);
ui->drawPlace->toDraw.push_back(std::move(objekt));
break;
}
}
}
file.close();
repaint();
}
//premazanie canvasu
void editor::on_actionClean_triggered()
{
ui->drawPlace->toDraw.clear();
}
//ukazanie okna na zmenu velkosti canvasu
void editor::on_actionSet_Canvas_triggered()
{
ui->canvasSize->setVisible(true);
}
//zase zrusenie na tlacidlo Close
void editor::on_closeButton_clicked()
{
ui->canvasSize->setVisible(false);
}
//nastavenie canvasu na tlacidlo OK
void editor::on_okButton_clicked()
{
std::string width = ui->widthEdit->text().toStdString();
std::string height = ui->heightEdit->text().toStdString();
//kontrola ci boli napisane cisla
if(!is_digit(width) || !is_digit(height)) {
QMessageBox messageBox;
messageBox.critical(0,"Error","Width or height is not a number");
messageBox.setFixedSize(500,200);
return;
}
ui->drawPlace->resize(std::stoi(width),std::stoi(height));
ui->canvasSize->setVisible(false);
}
bool editor::is_digit(std::string s)
{
return std::all_of(s.begin(), s.end(), ::isdigit);
}