-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
407 lines (358 loc) · 14.5 KB
/
mainwindow.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QInputDialog>
#include "xmlshapesreader.h"
#include "xmlshapeswriter.h"
#include "shape.h"
#include <QFile>
#include "box.h"
#include "Cone.h"
#include "Ellipsoid.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mLinkedList = new std::list<Shape*>;
//connect(ui->MainWindow.actionExit(),signals(MainWindow.),slots(MainWindow.on_emptyInput()));
connect(ui->actionExit,SIGNAL(triggered(bool)),SLOT(close()));
//connect(Shape, SIGNAL(on_emptyInput(QString,double&)),SLOT(on_emptyInput(QString,double&)));
//connect(sender,SIGNAL(signal_changed_linkedList(Shape*)),this,SLOT(on_changed_linkedList(Shape*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox *msgbox = new QMessageBox;
QString message{"Assignment - 4 - FunWithShape \nCreated by: Jacob Tovar \nUniversity: BYU"};
//(*msgbox).text() = message;
msgbox->setText(message);
(*msgbox).show();
}
void MainWindow::on_actionOpen_triggered()
{
QFileDialog *browse = new QFileDialog;
QString selectedFile = browse->getOpenFileName(this,tr("Open File"),"/home",tr("Xml (*.xml)"));
// ReadXmlLocation = selectedFile;
// QString outputText = "";
// QStringList addressList = selectedFile.split("/");
// QString fileName = addressList.last();
// ui->outputDockPlainText->appendPlainText(fileName);
if (selectedFile != NULL)
{
//This code came from the last assignment
QFile file(selectedFile);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
ui->outputDockPlainText->appendPlainText("Cannot read file - " + (selectedFile.split("/")).last() + file.errorString());
//return false;
}
else
{
//std::list<Shape*> linkedList{NULL};
//Notify user what file is being read.
QString startText = "Reading File - " + selectedFile;
ui->outputDockPlainText->appendPlainText(startText);
XmlShapesReader shape_reader(mLinkedList);
if (!shape_reader.read(&file))
{
QMessageBox *msgbox = new QMessageBox;
QString message{"Parse error in file\nPlease review Xml File\n " + shape_reader.errorString()};
msgbox->setText(message);
ui->outputDockPlainText->appendPlainText( "Parse error in file. Please review Xml File\n " + shape_reader.errorString());
msgbox->show();
}
if (mLinkedList!= nullptr && mLinkedList->size() >0)
{
ui->outputDockPlainText->appendPlainText( QString::number(mLinkedList->size()));
for(Shape *s: *mLinkedList) //for(int s=0;s<size;s++)
{
ui->outputDockPlainText->appendPlainText( s->print());
QString ListItem = "Id: " + QString::number(s->get_id()) + "\t Shape: ...";
ui->ListListWidget->addItem(ListItem);
ui->lineEdit_Id_2->setText(QString::number((mLinkedList->size())+1));
//Shape* shape{nullptr};
//mLinkedList->get_at(s,shape);
//shape->print();
}
ui->outputDockPlainText->appendPlainText("- - - - - - - - End of File - - - - - - - - - \n");
}
}
}
}
void MainWindow::on_actionSave_triggered()
{
//Use the workingFile to try and open the file. If not able throw and error on the Output Dock.
QFile file(workingFile);
if(!file.open(QFile::WriteOnly | QFile::Text))
{
ui->outputDockPlainText->appendPlainText( "Cannot Save file");// << file.errorString().data();
}
//Create a xmlWriter and wrtie the file.
XmlShapesWriter shape_writer(mLinkedList);
shape_writer.write(&file);
ui->outputDockPlainText->appendPlainText("Wrote file" + (workingFile.split("/")).last());
}
void MainWindow::on_actionSave_As_triggered()
{
//Open file dialog for the user to select the location and name for the saved XML document
QFileDialog *saveDialog = new QFileDialog;
QString saveFile = saveDialog->getSaveFileName(this,tr("Save File"), "/home", tr("Xml (*.xml)"));
// QFileDialog *browse = new QFileDialog;
// QString selectedFile = browse->getOpenFileName(this,tr("Save File"),"/home",tr("Xml (*.xml)"));
// QString outputText = "";
//QStringList addressList = saveFile.split("/");
//QString fileName = addressList.last();
//ui->outputDockPlainText->appendPlainText(fileName);
QFile file(saveFile);
if(!file.open(QFile::WriteOnly | QFile::Text))
{
ui->outputDockPlainText->appendPlainText( "Cannot Write file");// << file.errorString().data();
}
//Create a XmlWriter and write the file. Notify user when done
XmlShapesWriter shape_writer(mLinkedList);
shape_writer.write(&file);
ui->outputDockPlainText->appendPlainText("Wrote new file" + (saveFile.split("/")).last());
}
void MainWindow::on_actionExit_triggered()
{
//close();
}
void MainWindow::print(QString output)
{
QString message = "Hello World!";
QPlainTextEdit *outputTextBox = ui->outputDockPlainText;
outputTextBox->setPlainText(message);
}
void MainWindow::clear_window()
{
ui->outputDockPlainText->clear();
}
void MainWindow::close()
{
ui->outputDockPlainText->appendPlainText("close functoin.");
QCoreApplication::quit();
}
void MainWindow::on_emptyInput(QString XmlProperty, double &xmlValue)
{
bool ok;
//Code to get text input from the user. Useful reference Don't delete
// QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
// tr("User name:"), QLineEdit::Normal,
// QDir::home().dirName(), &ok);
//Get numeric input from the user.
double value = QInputDialog::getDouble(this,tr("Missing Xml Property!"),
tr("Your file is missing a property\nPlease enter a valid value of property:"),0,0,10000,5,&ok);
//If user hit ok use the value from the user.
if (ok)
ui->outputDockPlainText->appendPlainText(QString::number(value));
}
void MainWindow::add_shape_to_UIlist(Shape *s)
{
QString ListItem = "Id: " + QString::number(s->get_id()) + "\t Shape: ???";
ui->ListListWidget->addItem(ListItem);
}
void MainWindow::on_addShapeButton_2_clicked()
{
//Create the message box with text notification incase there is a incorrect property
QMessageBox *msgbox = new QMessageBox;
QString message{"Missing property!\n Please enter all properties and add shape."};
msgbox->setText(message);
//(*msgbox).show();
//Add the values of the current property to the new shape.
double x,y,z;
bool valid{true};
int id = ui->lineEdit_Id_2->text().toInt();
if(id >0)
{
//Create string to add to the Listwidget.
QString ListItem = "Id: " + QString::number(id) + "\t Shape: ";
//If we are talking about the dimensions, find out what type of shape we are creating.
if(ui->boxRadioButton_2->isChecked())
{
Box *newBox = new Box(id);
double h = ui->lineEdit_DimX->text().toDouble();
double w = ui->lineEdit_DimY->text().toDouble();
double d = ui->lineEdit_DimZ->text().toDouble();
if(h==0 || w==0 || d==0)
{
//double value = QInputDialog::getDouble(this,tr("Missing Shape Property!"),
//tr("You have not entered a property.\nPlease enter a valid value of property:"),0,0,10000,5,&ok);
valid = false;
(*msgbox).show();
}
else
{
newBox->set_size(h,w,d);
newShape = newBox;
ListItem.append("Box");
//valid = true;
}
}
else if (ui->coneRadioButton_2->isChecked())
{
Cone *newCone = new Cone(id);
double h = ui->lineEdit_DimX->text().toDouble();
double radX = ui->lineEdit_DimY->text().toDouble();
double radY = ui->lineEdit_DimZ->text().toDouble();
if(h==0 || radX==0 || radY==0)
{
//double value = QInputDialog::getDouble(this,tr("Missing Shape Property!"),
//tr("You have not entered a property.\nPlease enter a valid value of property:"),0,0,10000,5,&ok);
valid = false;
(*msgbox).show();
}
else
{
newCone->set_size(h,radX,radY);
newShape = newCone;
ListItem.append("Cone");
}
}
else if (ui->ellipsoidRadioButton_2->isChecked())
{
Ellipsoid *newEllipsoid = new Ellipsoid(id);
double radX = ui->lineEdit_DimX->text().toDouble();
double radY = ui->lineEdit_DimY->text().toDouble();
double radZ = ui->lineEdit_DimZ->text().toDouble();
if(radX==0 || radY==0 || radZ==0)
{
//double value = QInputDialog::getDouble(this,tr("Missing Shape Property!"),
//tr("You have not entered a property.\nPlease enter a valid value of property:"),0,0,10000,5,&ok);
valid = false;
(*msgbox).show();
}
else
{
newEllipsoid->set_size(radX,radY,radZ);
newShape = newEllipsoid;
ListItem.append("Ellipsoid");
}
}
//Get and Set color properties
double r = ui->lineEdit_ColorX->text().toDouble();
double g = ui->lineEdit_ColorY->text().toDouble();
double b = ui->lineEdit_ColorZ->text().toDouble();
if(r==0 || g==0 || b==0)
{
//double value = QInputDialog::getDouble(this,tr("Missing Shape Property!"),
//tr("You have not entered a property.\nPlease enter a valid value of property:"),0,0,10000,5,&ok);
valid = false;
(*msgbox).show();
}
else
newShape->set_color(r,g,b);
//Get Translation values from GUI and set them in the newShape
x = ui->lineEdit_TranX->text().toDouble();
y = ui->lineEdit_TranY->text().toDouble();
z = ui->lineEdit_TranZ->text().toDouble();
if(x==0 || y==0 || z==0)
{
//double value = QInputDialog::getDouble(this,tr("Missing Shape Property!"),
//tr("You have not entered a property.\nPlease enter a valid value of property:"),0,0,10000,5,&ok);
valid = false;
(*msgbox).show();
}
else
newShape->set_translation(x,y,z);
//Get and set Rotation values from the GUI into the newShape
x = ui->lineEdit_RotX->text().toDouble();
y = ui->lineEdit_RotY->text().toDouble();
z = ui->lineEdit_RotZ->text().toDouble();
if(x==0 || y==0 || z==0)
{
//double value = QInputDialog::getDouble(this,tr("Missing Shape Property!"),
//tr("You have not entered a property.\nPlease enter a valid value of property:") 0,0,10000,5,&ok);
valid = false;
(*msgbox).show();
}
else
newShape->set_rotation(x,y,z);
//Get and set Scale
x = ui->lineEdit_ScaleX->text().toDouble();
y = ui->lineEdit_ScaleY->text().toDouble();
z = ui->lineEdit_ScaleZ->text().toDouble();
if(x==0 || y==0 || z==0)
{
//double value = QInputDialog::getDouble(this,tr("Missing Shape Property!"),
//tr("You have not entered a property.\nPlease enter a valid value of property:"),0,0,10000,5,&ok);
valid = false;
(*msgbox).show();
}
else
newShape->set_scale(x,y,z);
if (valid)
{
//Add the shape to the linked list and output the info to the user
mLinkedList->push_back(newShape);
ui->outputDockPlainText->appendPlainText(newShape->print());
//Create a new listWidget item with the shape information.
ui->ListListWidget->addItem(ListItem);
ui->ListListWidget->repaint();
ui->ListListWidget->update();
//Increment the shapes id for usability
ui->lineEdit_Id_2->setText(QString::number(id+1));
}
}
}
void MainWindow::on_boxRadioButton_2_toggled(bool checked)
{
if(checked)
{
ui->label_sizeX->setText(QString("Height:"));
ui->label_sizeY->setText(QString("Width:"));
ui->label_sizeZ->setText(QString("Depth:"));
}
}
void MainWindow::on_coneRadioButton_2_toggled(bool checked)
{
if(checked)
{
ui->label_sizeX->setText(QString("Height:"));
ui->label_sizeY->setText(QString("Radius X:"));
ui->label_sizeZ->setText(QString("Radius Y:"));
}
}
void MainWindow::on_ellipsoidRadioButton_2_toggled(bool checked)
{
if(checked)
{
ui->label_sizeX->setText(QString("Radius X:"));
ui->label_sizeY->setText(QString("Radius Y:"));
ui->label_sizeZ->setText(QString("Radius Z:"));
}
}
void MainWindow::on_clearButton_2_clicked()
{
ui->lineEdit_Id_2->setText(QString(""));
ui->lineEdit_ColorX->setText(QString(""));
ui->lineEdit_ColorY->setText(QString(""));
ui->lineEdit_ColorZ->setText(QString(""));
ui->lineEdit_DimX->setText(QString(""));
ui->lineEdit_DimY->setText(QString(""));
ui->lineEdit_DimZ->setText(QString(""));
ui->lineEdit_TranX->setText(QString(""));
ui->lineEdit_TranY->setText(QString(""));
ui->lineEdit_TranZ->setText(QString(""));
ui->lineEdit_RotX->setText(QString(""));
ui->lineEdit_RotY->setText(QString(""));
ui->lineEdit_RotZ->setText(QString(""));
ui->lineEdit_ScaleX->setText(QString(""));
ui->lineEdit_ScaleY->setText(QString(""));
ui->lineEdit_ScaleZ->setText(QString(""));
//ui->addShapeButton_2->setDisabled(true);
}
void MainWindow::on_actionOpen_Create_Shape_Dialog_triggered()
{
//ui->creatShapeDockWidget = new QDockWidget;
ui->creatShapeDockWidget->show();
}
void MainWindow::on_actionOpen_Output_Dock_triggered()
{
//ui->outputDockPlainText = new QDockWidget;
ui->outputDockWidget->show();
}