Repository navigation
Expand file tree
/
Copy pathContactDialog.cpp
More file actions
259 lines (208 loc) · 9.37 KB
/
Copy pathContactDialog.cpp
File metadata and controls
259 lines (208 loc) · 9.37 KB
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
#include "ContactDialog.h"
#include "validation.h"
#include <QHBoxLayout>
#include <QFormLayout>
#include <QHeaderView>
#include <QMessageBox>
ContactDialog::ContactDialog(Phonebook& phonebook, Contact* contactToEdit, QWidget* parent)
: QDialog(parent), phonebook(phonebook), contactToEdit(contactToEdit) {
isEditMode = contactToEdit != nullptr;
setupUi();
if (isEditMode) {
setWindowTitle("Edit contact");
loadContactData();
} else {
setWindowTitle("Add contact");
}
}
void ContactDialog::setupUi() {
const auto mainLayout = new QVBoxLayout(this);
QLabel* infoLabel = new QLabel("Fields marked with an asterisk (*) are mandatory.");
mainLayout->addWidget(infoLabel);
QFormLayout* formLayout = new QFormLayout();
editSurname = new QLineEdit();
editForename = new QLineEdit();
editPatronymic = new QLineEdit();
editAddress = new QLineEdit();
formLayout->addRow("Surname*:", editSurname);
formLayout->addRow("Forename*:", editForename);
formLayout->addRow("Patronymic:", editPatronymic);
formLayout->addRow("Address:", editAddress);
QHBoxLayout* dateLayout = new QHBoxLayout();
editBirthDate = new QDateEdit();
editBirthDate->setDisplayFormat("dd.MM.yyyy");
editBirthDate->setCalendarPopup(true);
editBirthDate->setMaximumDate(QDate::currentDate());
editBirthDate->setMinimumDate(QDate(1900, 1, 1));
editBirthDate->setDate(QDate::currentDate());
editBirthDate->setEnabled(false);
cbDateEnabled = new QCheckBox("Set date");
cbDateEnabled->setChecked(false);
dateLayout->addWidget(editBirthDate);
dateLayout->addWidget(cbDateEnabled);
dateLayout->addStretch();
formLayout->addRow("Birth date:", dateLayout);
editEmail = new QLineEdit();
formLayout->addRow("Email*:", editEmail);
mainLayout->addLayout(formLayout);
mainLayout->addWidget(new QLabel("Phone numbers* (at least one):"));
tablePhones = new QTableWidget();
tablePhones->setColumnCount(2);
tablePhones->setHorizontalHeaderLabels({"Type", "Number"});
tablePhones->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tablePhones->setSelectionBehavior(QAbstractItemView::SelectRows);
tablePhones->setSelectionMode(QAbstractItemView::SingleSelection);
mainLayout->addWidget(tablePhones);
const auto phoneBtnLayout = new QHBoxLayout();
btnAddPhone = new QPushButton("Add phone");
btnDeletePhone = new QPushButton("Delete selected");
phoneBtnLayout->addWidget(btnAddPhone);
phoneBtnLayout->addWidget(btnDeletePhone);
mainLayout->addLayout(phoneBtnLayout);
mainLayout->addStretch();
const auto btnLayout = new QHBoxLayout();
btnSave = new QPushButton("Save");
btnSave->setDefault(true);
btnCancel = new QPushButton("Cancel");
btnLayout->addWidget(btnSave);
btnLayout->addWidget(btnCancel);
mainLayout->addLayout(btnLayout);
connect(btnAddPhone, &QPushButton::clicked, this, &ContactDialog::onAddPhoneClicked);
connect(btnDeletePhone, &QPushButton::clicked, this, &ContactDialog::onDeletePhoneClicked);
connect(btnSave, &QPushButton::clicked, this, &ContactDialog::onSaveClicked);
connect(btnCancel, &QPushButton::clicked, this, &QDialog::reject);
connect(cbDateEnabled, &QCheckBox::toggled, editBirthDate, &QWidget::setEnabled);
}
void ContactDialog::onAddPhoneClicked() const {
const int row = tablePhones->rowCount();
tablePhones->insertRow(row);
tablePhones->setItem(row, 0, new QTableWidgetItem(""));
tablePhones->setItem(row, 1, new QTableWidgetItem(""));
}
void ContactDialog::onDeletePhoneClicked() {
const int currentRow = tablePhones->currentRow();
if (currentRow != -1) {
tablePhones->removeRow(currentRow);
} else {
QMessageBox::warning(this, "Error", "Select a number to delete.");
}
}
void ContactDialog::loadContactData() const {
editSurname->setText(QString::fromStdString(contactToEdit->getSurname()));
editForename->setText(QString::fromStdString(contactToEdit->getForename()));
editPatronymic->setText(QString::fromStdString(contactToEdit->getPatronymic()));
editAddress->setText(QString::fromStdString(contactToEdit->getAddress()));
const Date birthDate = contactToEdit->getBirthDate();
if (birthDate.day != 0) {
cbDateEnabled->setChecked(true);
editBirthDate->setDate(QDate(birthDate.year, birthDate.month, birthDate.day));
}
editEmail->setText(QString::fromStdString(contactToEdit->getEmail()));
for (const auto& phone : contactToEdit->getPhoneNumbers()) {
const int row = tablePhones->rowCount();
tablePhones->insertRow(row);
tablePhones->setItem(row, 0, new QTableWidgetItem(QString::fromStdString(phone.type)));
tablePhones->setItem(row, 1, new QTableWidgetItem(QString::fromStdString(phone.number)));
}
}
void ContactDialog::onSaveClicked() {
const std::string surname = validation::trim(editSurname->text().toStdString());
if (!validation::isValidName(surname)) {
QMessageBox::warning(this, "Error", "Invalid surname.");
editSurname->setFocus();
return;
}
const std::string forename = validation::trim(editForename->text().toStdString());
if (!validation::isValidName(forename)) {
QMessageBox::warning(this, "Error", "Invalid forename.");
editForename->setFocus();
return;
}
const std::string patronymic = validation::trim(editPatronymic->text().toStdString());
if (!patronymic.empty() && !validation::isValidName(patronymic)) {
QMessageBox::warning(this, "Error", "Invalid patronymic.");
editPatronymic->setFocus();
return;
}
const std::string address = validation::trim(editAddress->text().toStdString());
if (!validation::isValidAddress(address)) {
QMessageBox::warning(this, "Error", "Invalid address.");
editAddress->setFocus();
return;
}
const std::string email = validation::normalizeEmail(editEmail->text().toStdString());
if (!validation::isValidEmail(email)) {
QMessageBox::warning(this, "Error", "Invalid email.");
editEmail->setFocus();
return;
}
if (!validation::isForenameInEmail(email, forename)) {
QMessageBox::warning(this, "Error",
"Email must contain the forename '" + QString::fromStdString(forename) + "'.\n"
"Update email or forename.");
editForename->setFocus();
return;
}
const int ignoreId = isEditMode ? contactToEdit->getId() : 0;
if (!phonebook.isEmailUnique(email, ignoreId)) {
QMessageBox::warning(this, "Error", "Email already in use.");
editEmail->setFocus();
return;
}
if (tablePhones->rowCount() == 0) {
QMessageBox::warning(this, "Error", "Add at least one phone number.");
btnAddPhone->setFocus();
return;
}
std::vector<std::string> currentPhones;
for (int i = 0; i < tablePhones->rowCount(); ++i) {
std::string type = validation::trim(tablePhones->item(i, 0)->text().toStdString());
std::string number = validation::trim(tablePhones->item(i, 1)->text().toStdString());
if (!validation::isValidPhoneType(type)) {
QMessageBox::warning(this, "Error", "Invalid phone type in row " + QString::number(i+1));
tablePhones->setCurrentCell(i, 0);
return;
}
if (!validation::isValidPhoneNumber(number)) {
QMessageBox::warning(this, "Error", "Invalid phone number in row " + QString::number(i+1));
tablePhones->setCurrentCell(i, 1);
return;
}
std::string normalizedNumber = validation::normalizePhoneNumber(number);
if (!phonebook.isPhoneNumberUnique(number, ignoreId)) {
QMessageBox::warning(this, "Error", "Phone number in row " + QString::number(i+1) + " is already in use by other contact.");
tablePhones->setCurrentCell(i, 1);
return;
}
for (const auto& existing : currentPhones) {
if (existing == normalizedNumber) {
QMessageBox::warning(this, "Error", "Duplicate phone number inside this contact.");
tablePhones->setCurrentCell(i, 1);
return;
}
}
currentPhones.push_back(normalizedNumber);
}
accept();
}
Contact ContactDialog::getContact() const {
Contact contact;
contact.setSurname(editSurname->text().toStdString());
contact.setForename(editForename->text().toStdString());
contact.setPatronymic(editPatronymic->text().toStdString());
contact.setAddress(editAddress->text().toStdString());
if (cbDateEnabled->isChecked()) {
const QDate date = editBirthDate->date();
contact.setBirthDate(Date(date.day(), date.month(), date.year()));
} else {
contact.setBirthDate(Date());
}
contact.setEmail(editEmail->text().toStdString());
for (int i = 0; i < tablePhones->rowCount(); ++i) {
contact.addPhoneNumber(
tablePhones->item(i, 0)->text().toStdString(),
tablePhones->item(i, 1)->text().toStdString()
);
}
return contact;
}