-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeinterface.cpp
167 lines (140 loc) · 5.41 KB
/
treeinterface.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
#include "treeinterface.h"
#include "ui_treeinterface.h"
#include <QDebug>
#include "TreeUnitTests.h"
TreeInterface::TreeInterface(QWidget *parent) :
QDialog(parent),
ui(new Ui::TreeInterface)
{
QVBoxLayout *layoutPrincipal = new QVBoxLayout;
QGridLayout *layoutName = new QGridLayout();
QGridLayout *layoutPath = new QGridLayout();
QGridLayout *layoutBackupPath = new QGridLayout();
QGridLayout *layoutButtons = new QGridLayout();
ui->setupUi(this);
ui->labelTitle->setText(QObject::tr("Create a genealogic tree"));
ui->labelName->setText(QObject::tr("Name"));
ui->labelPath->setText(QObject::tr("Path"));
ui->checkBoxBackupPath->setText(QObject::tr("BackupPath"));
ui->pushButtonBrowsePath->setText(QObject::tr("Browse"));
ui->pushButtonBrowseBackupPath->setText(QObject::tr("Browse"));
if (TreeUnitTests().getTestInterface()) {
ui->lineEditName->setText("Test");
ui->lineEditPath->setText(QDir::currentPath() + "/Tests Results");
}
connect(ui->checkBoxBackupPath, SIGNAL(clicked()), this, SLOT(enableBackupPath()));
connect(ui->pushButtonBrowsePath, SIGNAL(clicked()), this, SLOT(browsePath()));
connect(ui->pushButtonBrowseBackupPath, SIGNAL(clicked()), this, SLOT(browseBackupPath()));
connect(ui->pushButtonOk, SIGNAL(clicked()), this, SLOT(createTree()));
connect(ui->pushButtonCancel, SIGNAL(clicked()), this, SLOT(close()));
layoutPrincipal->setSpacing(20);
layoutPrincipal->addWidget(ui->labelTitle);
layoutName->addWidget(ui->labelName,1,1);
layoutName->addWidget(ui->lineEditName,1,2);
layoutPrincipal->addLayout(layoutName);
layoutPath->addWidget(ui->labelPath,1,1);
layoutPath->addWidget(ui->lineEditPath,1,2);
layoutPath->addWidget(ui->pushButtonBrowsePath,1,3);
layoutPrincipal->addLayout(layoutPath);
layoutBackupPath->addWidget(ui->checkBoxBackupPath,1,1);
layoutBackupPath->addWidget(ui->lineEditBackupPath,1,2);
layoutBackupPath->addWidget(ui->pushButtonBrowseBackupPath,1,3);
layoutPrincipal->addLayout(layoutBackupPath);
layoutButtons->addWidget(ui->pushButtonOk,1,1);
layoutButtons->addWidget(ui->pushButtonCancel,1,2);
layoutPrincipal->addLayout(layoutButtons,2);
this->setLayout(layoutPrincipal);
}
std::shared_ptr<Tree> TreeInterface::getCurrentTree() {
return currentTree;
}
TreeInterface::~TreeInterface()
{
delete ui;
}
void TreeInterface::createTree(){
treeName = ui->lineEditName->text();
updateDate = QDateTime::currentDateTime();
path = ui->lineEditPath->text();
backupPath = ui->lineEditBackupPath->text();
QString stringError;
if (ui->lineEditName->text() == ""){
stringError += tr("Please fill the name field\n");
}
if(ui->lineEditPath->text() == "" )
{
stringError += tr("Please fill the path field\n");
}
if(ui->lineEditBackupPath->text() == "" && ui->checkBoxBackupPath->isChecked())
{
stringError += tr("Please fill the backup path field or untick the backup path checkbox\n");
}
if (TreeUnitTests().getTestInterface()) {
QFile file(path.path() + "/" + treeName + ".tree");
file.remove();
QFile backupFile(backupPath.path() + "/" + treeName + ".tree");
backupFile.remove();
}
QFileInfo check_file(path.path() + "/" + treeName + ".tree");
// check if file exists and if yes: Is it really a file and no directory?
if (check_file.exists() && check_file.isFile()) {
stringError += tr("A Tree with the same name already exists in this folder, please change the name of your Tree\n");
}
if (backupPath.path() != "") {
QFileInfo check_Backupfile(backupPath.path() + "/" + treeName + ".tree");
// check if file exists and if yes: Is it really a file and no directory?
if (check_Backupfile.exists() && check_Backupfile.isFile()) {
stringError += tr("A Tree with the same name already exists in the backup folder, please change the name of your Tree\n");
}
}
if(stringError != ""){
QMessageBox msgBox;
msgBox.setText(tr("Error"));
msgBox.setInformativeText(stringError);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
else{
currentTree = std::make_shared<Tree>(treeName, updateDate, path, backupPath);
emit signalReturnTree();
this->hide();
}
}
void TreeInterface::enableBackupPath(){
if(ui->checkBoxBackupPath->isChecked()){
ui->lineEditBackupPath->setEnabled(true);
ui->pushButtonBrowseBackupPath->setEnabled(true);
if (TreeUnitTests().getTestInterface()) {
ui->lineEditBackupPath->setText(QDir::currentPath() + "/Tests Results/Backup");
}
}
else {
ui->pushButtonBrowseBackupPath->setEnabled(false);
ui->lineEditBackupPath->setEnabled(false);
if (TreeUnitTests().getTestInterface()) {
ui->lineEditBackupPath->setText("");
}
}
}
void TreeInterface::browsePath() {
browse(true);
}
void TreeInterface::browseBackupPath() {
browse(false);
}
void TreeInterface::browse(bool pathType){
QFileDialog dialog;
dialog.setFileMode(QFileDialog::DirectoryOnly);
dialog.setOption(QFileDialog::ShowDirsOnly, false);
if (dialog.exec())
QStringList fileNames = dialog.selectedFiles();
if(pathType){
path= dialog.directory().path();
ui->lineEditPath->setText(path.path()+"/");
}
else{
backupPath= dialog.directory().path();
ui->lineEditBackupPath->setText(backupPath.path()+"/");
}
}