-
Notifications
You must be signed in to change notification settings - Fork 0
/
questionsgui.cpp
116 lines (97 loc) · 4.29 KB
/
questionsgui.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
//
// Created by bia on 05/07/2022.
//
// You may need to build the project (run Qt uic code generator) to get "ui_QuestionsGUI.h" resolved
#include "questionsgui.h"
#include "ui_QuestionsGUI.h"
#include <QMessageBox>
#include <iostream>
QuestionsGUI::QuestionsGUI(User* u, UserRepo& urepo, QuestionRepo& repo, AnswerRepo& arepo, QWidget *parent) :
QWidget(parent), ui(new Ui::QuestionsGUI), u(u), urepo(urepo), repo(repo), arepo(arepo) {
ui->setupUi(this);
this->repo.addObserver(this);
this->setWindowTitle(QString::fromStdString(u->getName()));
this->populate();
this->connect();
}
QuestionsGUI::~QuestionsGUI() {
delete ui;
}
void QuestionsGUI::populate() {
this->ui->listWidget->clear();
this->ui->spinBox->clear();
this->repo.sort();
for(auto q : this->repo.getAll()) {
auto *i = new QListWidgetItem(QString::fromStdString(std::to_string(q.getId()) + " | " + q.getText()
+ " | Posted by: " + q.getCreator() + " | Answers : " +
std::to_string(q.getAnswers())));
if (q.getCreator() == u->getName()) {
i->setBackground(QBrush(QColor(212,175,55)));
}
this->ui->listWidget->addItem(i);
}
}
void QuestionsGUI::connect() {
QObject::connect(this->ui->pushButton, &QPushButton::pressed, this, [&]() {
try {
std::string text = this->ui->lineEdit->text().toStdString();
if(text.empty())
throw std::exception();
Question q{(int) this->repo.getAll().size() + 1, text, u->getName()};
this->repo.add(q);
// this->repo.writeToFile();
} catch (std::exception& e){
auto* popup = new QMessageBox;
popup->setText("Description can't be emtpy!");
popup->show();
}
});
QObject::connect(this->ui->listWidget, &QListWidget::itemSelectionChanged, this, [&]() {
std::string line = this->ui->listWidget->item(this->ui->listWidget->currentRow())->text().toStdString();
int id = (int)line[0] - 48;
if(this->repo[id].getCreator() == u->getName())
this->ui->pushButton_2->setDisabled(true);
else this->ui->pushButton_2->setEnabled(true);
this->ui->listWidget_2->clear();
for(auto a: this->arepo.getById(id)) {
auto *i = new QListWidgetItem(QString::fromStdString(
std::to_string(a.getId()) + " | " + a.getText() + " | Posted by: " +
a.getAName() + " | Upvotes: " + std::to_string(a.getVotes())));
if (a.getAName() == u->getName()) {
i->setBackground(QBrush(QColor(126,176,76)));
}
this->ui->listWidget_2->addItem(i);
}
});
QObject::connect(this->ui->pushButton_2, &QPushButton::pressed, this, [&]() {
try {
std::string text = this->ui->lineEdit_2->text().toStdString();
if(text.empty())
throw std::exception();
Answer a{(int) this->repo.getAll().size() + 1, this->ui->listWidget->currentRow() + 1, u->getName(), text, 0};
this->arepo.add(a);
this->repo[this->ui->listWidget->currentRow()].setAnswers(this->repo[this->ui->listWidget->currentRow()].getAnswers() + 1);
this->repo.notify();
// this->arepo.writeToFile();
} catch (std::exception& e){
auto* popup = new QMessageBox;
popup->setText("Description can't be emtpy!");
popup->show();
}
});
QObject::connect(this->ui->listWidget_2, &QListWidget::itemSelectionChanged, this, [&]() {
this->ui->spinBox->clear();
std::string line = this->ui->listWidget_2->item(this->ui->listWidget_2->currentRow())->text().toStdString();
int aId = (int)line[0] - 48;
this->ui->spinBox->setValue(this->arepo[aId - 1].getVotes());
});
QObject::connect(this->ui->spinBox, &QSpinBox::valueChanged, this, [&]() {
std::string line = this->ui->listWidget_2->item(this->ui->listWidget_2->currentRow())->text().toStdString();
int aId = (int)line[0] - 48;
this->arepo[aId - 1].setVotes(this->ui->spinBox->value());
this->repo.notify();
});
}
void QuestionsGUI::update() {
this->populate();
}