-
Notifications
You must be signed in to change notification settings - Fork 0
/
guiUser.cpp
99 lines (80 loc) · 3.42 KB
/
guiUser.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
//
// Created by Misan on 5/17/2021.
//
#include <QVBoxLayout>
#include <QFormLayout>
#include "guiUser.h"
GUIADM::GUIADM(Service &serv) : _serv{serv}{
this->setUpGUI();
this->populateDogList();
this->populateAdoptList();
this->connectSignalAndSlots();
}
void GUIADM::setUpGUI() {
this->dogsListWidget = new QListWidget{};
this->adoptionListWidget = new QListWidget{};
this->breedLineEdit = new QLineEdit{};
this->nameLineEdit = new QLineEdit{};
this->ageLineEdit = new QLineEdit{};
this->photographLineEdit= new QLineEdit{};
this->adoptButton = new QPushButton{"Adopt"};
QVBoxLayout * mainLayout = new QVBoxLayout { this };
mainLayout->addWidget(this->dogsListWidget);
mainLayout->addWidget(this->adoptionListWidget);
QFormLayout* dogsDetails = new QFormLayout{};
dogsDetails->addRow("Breed", this->breedLineEdit);
dogsDetails->addRow("Name", this->nameLineEdit);
dogsDetails->addRow("Age", this->ageLineEdit);
dogsDetails->addRow("Photograph", this->photographLineEdit);
mainLayout->addLayout(dogsDetails);
QGridLayout* buttonsLayout = new QGridLayout{};
buttonsLayout->addWidget(this->adoptButton, 0, 0);
mainLayout->addLayout(buttonsLayout);
}
void GUIADM::populateDogList() {
this->dogsListWidget->clear();
vector<Dog> listDogs = this->_serv.getDog();
for(Dog& d: listDogs)
this->dogsListWidget->addItem(QString::fromStdString(d.getBreedDog() + " " + d.getNameDog() + " " + to_string(d.getAgeDog()) + " " + d.getPhotoDog()));
}
void GUIADM::populateAdoptList() {
this->adoptionListWidget->clear();
vector<Dog> listDogsAdopt = this->_serv.getDogsAdoption();
for(Dog& d: listDogsAdopt)
this->adoptionListWidget->addItem(QString::fromStdString(d.getBreedDog() + " " + d.getNameDog() + " " + to_string(d.getAgeDog()) + " " + d.getPhotoDog()));
}
void GUIADM::connectSignalAndSlots() {
QObject::connect(this->dogsListWidget, &QListWidget::itemSelectionChanged, [this](){
int selectedDog = this->getSelectedIndex();
if(selectedDog < 0) return;
Dog d = this->_serv.getDog()[selectedDog];
this->breedLineEdit->setText(QString::fromStdString(d.getBreedDog()));
this->nameLineEdit->setText(QString::fromStdString(d.getNameDog()));
this->ageLineEdit->setText(QString::fromStdString(to_string(d.getAgeDog())));
this->photographLineEdit->setText(QString::fromStdString(d.getPhotoDog()));
});
QObject::connect(this->adoptButton, &QPushButton::clicked, this, &GUIADM::adoptDog);
}
int GUIADM::getSelectedIndex() {
QModelIndexList selectedInd = this->dogsListWidget->selectionModel()->selectedIndexes();
if(selectedInd.size() == 0){
this->breedLineEdit->clear();
this->nameLineEdit->clear();
this->ageLineEdit->clear();
this->photographLineEdit->clear();
return -1;
}
int selInd = selectedInd.at(0).row();
return selInd;
}
void GUIADM::adoptDog() {
string breed = this->breedLineEdit->text().toStdString();
string name = this->nameLineEdit->text().toStdString();
int age = this->ageLineEdit->text().toInt();
string photo = this->photographLineEdit->text().toStdString();
this->_serv.AddDogAdoptionList(breed, name, age, photo);
this->populateAdoptList();
this->populateDogList();
int lastElem = this->_serv.getDogsAdoption().size() -1;
this->adoptionListWidget->setCurrentRow(lastElem);
}