-
Notifications
You must be signed in to change notification settings - Fork 0
/
notedialog.cpp
259 lines (213 loc) · 6.91 KB
/
notedialog.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
#include "notedialog.h"
#include "note.h"
#include "mainwindow.h"
NoteDialog::NoteDialog(Note *note) : QWidget()
{
setWindowTitle(note == 0 ? "Nouvelle note" : note->title());
// Récupération des options
m_settings = new QSettings("yellownote.ini", QSettings::IniFormat);
// Création des champs title et content
m_noteEdit = new NoteEdit(m_settings, this);
m_note = note;
// Assignation du dialog à la note
attachToNote();
// Si on est en modification, on remplit les champs
if (note != 0)
{
m_noteEdit->update(note->title(), note->content());
}
QAction *actionInfos = new QAction("&Infos", this);
actionInfos->setIcon(QIcon(":/note/infos"));
actionInfos->setIconText("Infos");
connect(actionInfos, SIGNAL(triggered()), this, SLOT(infos()));
m_actionDelete = new QAction("&Supprimer", this);
m_actionDelete->setIcon(QIcon(":/note/delete"));
m_actionDelete->setIconText("Supprimer");
connect(m_actionDelete, SIGNAL(triggered()), this, SLOT(deleteMe()));
QWidget *spacerWidget = new QWidget(this);
spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
spacerWidget->setVisible(true);
QToolBar *toolBar = new QToolBar;
toolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBar->addWidget(spacerWidget);
toolBar->addAction(actionInfos);
toolBar->addAction(m_actionDelete);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
mainLayout->addWidget(toolBar);
mainLayout->addWidget(m_noteEdit);
setLayout(mainLayout);
// Réassignation de la taille de la fenêtre
if (m_settings->contains("note/size"))
{
resize(m_settings->value("note/size").toSize());
}
// Donne le focus à la fenêtre (quand elle est créée
// depuis un raccouri clavier global)
setFocus();
}
void NoteDialog::setFocus()
{
showNormal();
raise();
activateWindow();
}
QString NoteDialog::content() const
{
return m_noteEdit->content();
}
QString NoteDialog::title() const
{
return m_noteEdit->title();
}
Note * NoteDialog::note()
{
return m_note;
}
void NoteDialog::setNote(Note* note)
{
m_note = note;
attachToNote();
}
void NoteDialog::changeEvent(QEvent *event)
{
QWidget::changeEvent(event);
// Perte du focus
// (cette méthode fonctionne mieux que de surcharger focusOutEvent)
if (event->type() == QEvent::ActivationChange && ! this->isActiveWindow())
{
save();
}
}
void NoteDialog::closeEvent(QCloseEvent *event)
{
save();
if (m_note)
{
delete m_note->noteDialog();
m_note->setNoteDialog(0);
}
event->accept();
}
void NoteDialog::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
m_settings->setValue("note/size", size());
}
bool NoteDialog::save()
{
if (m_noteEdit->hasChanged())
{
// Si c'est un ajout
if (m_note == 0)
{
// Création de la note
Note *note = new Note(title(), content());
note->addToDb();
setNote(note);
// Ajout de la note dans la liste
emit newNote(note);
}
// Modification
else
{
// Mise à jour de la note elle-même
m_note->setTitle(title());
m_note->setContent(content());
m_note->setToSync(true);
m_note->editInDb();
// Mise à jour de la note dans la liste
m_note->item()->update();
// Si la note est dans le panneau, on le met à jour
if (m_note->notePanel())
{
m_note->notePanel()->update();
}
}
// Mise à jour du titre (après génération automatique)
m_noteEdit->setTitle(m_note->title());
setWindowTitle(m_note->title());
// Enregistrement terminée, il n'y a donc plus de changements en attente
m_noteEdit->setNoChange();
return true;
}
return false;
}
void NoteDialog::infos()
{
if (!m_note)
{
QMessageBox::information(this, "Informations sur la note",
"Aucune information pour l'instant.<br>La note n'est pas encore enregistrée.");
return;
}
QDialog *dialog = new QDialog(this, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint);
dialog->setWindowTitle("Informations sur la note");
QLabel *creation = new QLabel(m_note->createdAt().toString("d MMM yyyy à hh:mm:ss"));
QLabel *update = new QLabel(m_note->updatedAt().toString("d MMM yyyy à hh:mm:ss"));
QLabel *sync = new QLabel(m_note->toSync() ? "Synchronisation nécessaire" :
"Synchronisé le " + m_note->syncedAt().toString("d MMM yyyy à hh:mm:ss"));
QLabel *chars = new QLabel(QString::number(m_noteEdit->contentChars()));
QLabel *words = new QLabel(QString::number(m_noteEdit->contentWords()));
QLabel *lines = new QLabel(QString::number(m_noteEdit->contentLines()));
creation->setAlignment(Qt::AlignRight);
update->setAlignment(Qt::AlignRight);
sync->setAlignment(Qt::AlignRight);
chars->setAlignment(Qt::AlignRight);
words->setAlignment(Qt::AlignRight);
lines->setAlignment(Qt::AlignRight);
QFormLayout *noteLayout = new QFormLayout;
noteLayout->addRow("Créée le", creation);
noteLayout->addRow("Mise à jour le", update);
noteLayout->addRow("Statut", sync);
QFormLayout *textLayout = new QFormLayout;
textLayout->addRow("Caractères", chars);
textLayout->addRow("Mots", words);
textLayout->addRow("Lignes", lines);
QGroupBox *noteGroupBox = new QGroupBox("Note");
noteGroupBox->setLayout(noteLayout);
QGroupBox *textGroupBox = new QGroupBox("Texte");
textGroupBox->setLayout(textLayout);
QPushButton *close = new QPushButton("&Fermer", dialog);
connect(close, SIGNAL(clicked()), dialog, SLOT(accept()));
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addWidget(noteGroupBox);
mainLayout->addWidget(textGroupBox);
mainLayout->addWidget(close, 0, Qt::AlignRight);
dialog->setLayout(mainLayout);
dialog->exec();
}
void NoteDialog::deleteMe()
{
// close() va lancer l'évènement de fermeture et donc save() va être appelé
// pour éviter que la note soit sauvegardée, on fait comme si la note n'avait pas été modifiée
// c'est important dans le cas où on veut supprimer une note qui n'est pas encore enregistrée
m_noteEdit->setNoChange();
// signal de suppression
if (m_note)
{
emit deletionRequested(m_note);
m_note->setNoteDialog(0);
}
close();
}
// Mise à jour d'affichage
void NoteDialog::update()
{
if (m_note)
{
m_noteEdit->update(m_note->title(), m_note->content());
}
else
{
m_noteEdit->clear();
}
}
void NoteDialog::attachToNote()
{
if (m_note)
{
m_note->setNoteDialog(this);
}
}