/
addressbook.cpp
331 lines (279 loc) · 11.3 KB
/
addressbook.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
#include "addressbook.h"
#include "finddialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QGridLayout>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QString>
#include <QMap>
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>
#include <QDataStream>
AddressBook::AddressBook(QWidget *parent)
: QWidget(parent)
{
rechercheDialog = new FindDialog;
QLabel *labelNom = new QLabel (tr("Nom :"));
nomLineEdit = new QLineEdit;
nomLineEdit->setReadOnly(true);
QLabel *labelAdresse = new QLabel(tr("Adresse :"));
adresseTextEdit = new QTextEdit;
adresseTextEdit->setReadOnly(true);
QGridLayout *gridLayout = new QGridLayout;
QVBoxLayout *layoutBoutons = new QVBoxLayout;
QHBoxLayout *layoutBoutonsNavigation = new QHBoxLayout;
ajouterBouton = new QPushButton (tr ("Ajouter"));
soumettreBouton = new QPushButton (tr ("Soumettre"));
annulerBouton = new QPushButton (tr("Annuler"));
previousBouton = new QPushButton (tr ("Précédent"));
nextBouton = new QPushButton (tr ("Suivant"));
editerBouton = new QPushButton (tr ("&Editer"));
supprimerBouton = new QPushButton (tr ("&Supprimer"));
chercherBouton = new QPushButton (tr("Chercher"));
chargerBouton = new QPushButton (tr("Charger..."));
chargerBouton->setToolTip(tr("Charger depuis un fichier"));
sauverBouton= new QPushButton (tr ("Sauver..."));
sauverBouton->setToolTip(tr("Sauvegarder dans fichier"));
layoutBoutons->addWidget(ajouterBouton, Qt::AlignTop);
layoutBoutons->addWidget(soumettreBouton);
layoutBoutons->addWidget(annulerBouton);
layoutBoutons->addWidget(editerBouton);
layoutBoutons->addWidget(supprimerBouton);
layoutBoutons->addWidget(chercherBouton);
layoutBoutons->addWidget(chargerBouton);
layoutBoutons->addWidget(sauverBouton);
layoutBoutons->addStretch();
layoutBoutonsNavigation->addWidget(previousBouton);
layoutBoutonsNavigation->addWidget(nextBouton);
gridLayout->addWidget(labelNom , 0 , 0);
gridLayout->addWidget(nomLineEdit , 0 , 1);
gridLayout->addWidget(labelAdresse , 1 , 0);
gridLayout->addWidget(adresseTextEdit , 1 , 1);
gridLayout->addLayout(layoutBoutons , 1 , 2);
gridLayout->addLayout(layoutBoutonsNavigation , 2 , 1);
setLayout(gridLayout);
setWindowTitle(tr("Carnet d'adresses"));
updateInterface(NavigationMode);
// Connecteurs
QObject::connect(ajouterBouton , SIGNAL(clicked()) , this , SLOT (ajouterContact()));
QObject::connect(soumettreBouton , SIGNAL (clicked()) , this , SLOT (soumettreContact()));
QObject::connect(annulerBouton , SIGNAL(clicked()) , this , SLOT(annuler()));
QObject::connect(previousBouton , SIGNAL(clicked()) , this ,SLOT (previous()));
QObject::connect(nextBouton , SIGNAL(clicked()) , this , SLOT(next()));
QObject::connect(editerBouton , SIGNAL(clicked()) ,this , SLOT(editerContact()));
QObject::connect(supprimerBouton , SIGNAL(clicked()) , this , SLOT(supprimerContact()));
QObject::connect(chercherBouton , SIGNAL(clicked()) , this , SLOT(trouverContact()));
QObject::connect(chargerBouton , SIGNAL(clicked()) , this , SLOT(chargerFichier()));
QObject::connect(sauverBouton , SIGNAL(clicked()) , this , SLOT(sauverFichier()));
}
AddressBook::~AddressBook()
{
}
/////////// Slots //////////
void AddressBook::ajouterContact() {
oldNom = nomLineEdit->text();
oldAdresse = adresseTextEdit->toPlainText();
nomLineEdit->clear();
adresseTextEdit->clear();
updateInterface(AddingMode);
}
void AddressBook::editerContact() {
oldNom = nomLineEdit->text();
oldAdresse = adresseTextEdit->toPlainText();
updateInterface(EditingMode);
}
void AddressBook::supprimerContact() {
QString nom = nomLineEdit->text();
QString adresse = adresseTextEdit->toPlainText();
if (listeContacts.contains(nom)) {
int bouton = QMessageBox::question (this ,
tr ("Suppression de contact") ,
tr("Etes vous surs de vouloir supprimer le contact \"%1\" ?").arg(nom) ,
QMessageBox::Yes | QMessageBox::No);
if (bouton == QMessageBox::Yes) {
previous();
listeContacts.remove(nom);
QMessageBox::information(this ,
tr ("Suppression effectuée") ,
tr ("Le contact \"%1\" a été supprimé.").arg(nom));
}
}
updateInterface(NavigationMode);
}
void AddressBook::soumettreContact() {
QString nom (nomLineEdit->text());
QString adresse (adresseTextEdit->toPlainText());
if (nom.isEmpty() || adresse.isEmpty()) {
QMessageBox::information(this ,
tr("Champs obligatoires") ,
tr ("Veuillez remplir le nom et l'adresse."));
return;
}
if (currentMode == AddingMode) {
if (! listeContacts.contains(nom)) {
listeContacts.insert(nom , adresse);
QMessageBox::information(this ,
tr("Contact ajouté") ,
tr ("Le contact \"%1\" a bien été ajouté").arg(nom));
} else {
QMessageBox::information (this ,
tr ("Contact existant") ,
tr ("Un contact portant ce nom existe déjà"));
return;
}
} else if (currentMode == EditingMode) {
if (oldNom != nom) {
if (!listeContacts.contains(nom)) {
QMessageBox::information(this ,
tr("Contact modifié") ,
tr("Modification du contact \"%1\"").arg(oldNom));
listeContacts.remove(oldNom);
listeContacts.insert(nom , adresse);
} else {
QMessageBox::information (this ,
tr ("Erreur modification") ,
tr ("\"%1\" figure déjà dans vos contacts.").arg(nom));
}
} else if (oldAdresse != adresse){
QMessageBox::information(this ,
tr("Contact modifié") ,
tr("Modification du contact \"%1\"").arg(nom));
listeContacts[nom] = adresse;
}
}
updateInterface(NavigationMode);
}
void AddressBook::annuler() {
nomLineEdit->setText(oldNom);
nomLineEdit->setReadOnly(true);
updateInterface(NavigationMode);
}
void AddressBook::previous () {
QString nom = nomLineEdit->text();
QMap<QString , QString>::Iterator i = listeContacts.find(nom);
if ( i == listeContacts.end()) {
nomLineEdit->clear();
adresseTextEdit->clear();
return;
}
if ( i == listeContacts.begin())
i = listeContacts.end();
i--;
nomLineEdit->setText(i.key());
adresseTextEdit->setText(i.value());
}
void AddressBook::next () {
QString nom = nomLineEdit->text();
QMap<QString , QString>::Iterator i = listeContacts.find(nom);
if ( i != listeContacts.end())
i++;
if ( i == listeContacts.end())
i = listeContacts.begin();
nomLineEdit->setText(i.key());
adresseTextEdit->setText(i.value());
}
void AddressBook::trouverContact() {
rechercheDialog->show();
if (rechercheDialog->exec() == QDialog::Accepted) {
QString recherche = rechercheDialog->getRecherche();
if (listeContacts.contains(recherche)) {
QMap<QString,QString>::Iterator i = listeContacts.find(recherche);
nomLineEdit->setText(i.key());
adresseTextEdit->setText(i.value());
} else {
QMessageBox::information (this ,
tr ("Pas de résultat") ,
tr ("Le contact \"%1\" n'existe pas.").arg(recherche));
return;
}
}
updateInterface(NavigationMode);
}
void AddressBook::chargerFichier() {
QString nomFichier = QFileDialog::getOpenFileName(this,
tr ("Charger depuis un fichier"),
"",
tr ("Carnet d'adresse (*.abk);;Tous les fichiers (*)"));
if (nomFichier.isEmpty())
return;
else {
QFile fichier (nomFichier);
if (!fichier.open(QIODevice::ReadOnly)) {
QMessageBox::information(this,
tr ("Erreur de chargement"),
fichier.errorString());
return;
}
QDataStream in (&fichier);
in.setVersion(QDataStream::Qt_4_7);
listeContacts.empty();
in >> listeContacts;
if (listeContacts.isEmpty()) {
QMessageBox::information (this ,
tr ("Pas de contacts"),
tr ("Aucun contact à importer dans le fichier"));
} else {
QMap<QString , QString>::iterator i = listeContacts.begin();
nomLineEdit->setText(i.key());
adresseTextEdit->setText(i.value());
}
}
updateInterface(NavigationMode);
}
void AddressBook::sauverFichier() {
QString nomFichier = QFileDialog::getSaveFileName(this ,
tr("Sauvegarder dans un fichier") ,
"" ,
tr("Carnet d'adresse (*.abk);;Tous les fichiers (*)"));
if (nomFichier.isEmpty())
return;
else {
QFile fichier (nomFichier);
if (!fichier.open(QIODevice::WriteOnly)) {
QMessageBox::information(this,
tr ("Erreur d'écriture"),
fichier.errorString());
return;
}
QDataStream out (&fichier);
out.setVersion(QDataStream::Qt_4_7);
out << listeContacts;
}
}
void AddressBook::updateInterface(Mode mode) {
currentMode = mode;
switch (currentMode) {
case AddingMode:
case EditingMode:
nomLineEdit->setReadOnly(false);
nomLineEdit->setFocus(Qt::OtherFocusReason);
adresseTextEdit->setReadOnly(false);
ajouterBouton->setEnabled(false);
editerBouton->setEnabled(false);
supprimerBouton->setEnabled(false);
nextBouton->setEnabled(false);
previousBouton->setEnabled(false);
soumettreBouton->show();
annulerBouton->show();
break;
case NavigationMode:
if (listeContacts.isEmpty()) {
nomLineEdit->clear();
adresseTextEdit->clear();
}
nomLineEdit->setReadOnly(true);
adresseTextEdit->setReadOnly(true);
ajouterBouton->setEnabled(true);
int nombre = listeContacts.size();
nextBouton->setEnabled(nombre > 1);
previousBouton->setEnabled(nombre > 1);
supprimerBouton->setEnabled(nombre >= 1);
editerBouton->setEnabled(nombre >= 1);
soumettreBouton->hide();
annulerBouton->hide();
}
}