-
Notifications
You must be signed in to change notification settings - Fork 0
/
textedit.cpp
415 lines (353 loc) · 12.9 KB
/
textedit.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "mainwindow.h"
#include "textedit.h"
#include <QApplication>
#include <QButtonGroup>
#include <QColorDialog>
#include <QFileDialog>
#include <QGridLayout>
#include <QDebug>
//#include <hunspell/hunspell.hxx>
TextEdit::TextEdit(MainWindow *parent)
: QWidget(parent)
{
QGridLayout * layout = new QGridLayout;
setLayout(layout);
const QSize iconSize(32,32);
const QIcon boldIcon(":/images/bold.png");
actionTextBold = new QPushButton(this);
actionTextBold->setIcon(boldIcon);
actionTextBold->setIconSize(iconSize);
actionTextBold->setToolTip(tr("Gras"));
connect(actionTextBold,SIGNAL(clicked(bool)), this, SLOT(textBold()));
actionTextBold->setCheckable(true);
layout->addWidget(actionTextBold,0,0);
const QIcon italicIcon(":/images/italic.png");
actionTextItalic = new QPushButton(this);
actionTextItalic->setIcon(italicIcon);
actionTextItalic->setIconSize(iconSize);
actionTextItalic->setToolTip(tr("Italique"));
connect(actionTextItalic,SIGNAL(clicked(bool)), this, SLOT(textItalic()));
actionTextItalic->setCheckable(true);
layout->addWidget(actionTextItalic,0,1);
const QIcon underlineIcon(":/images/underline.png");
actionTextUnderline = new QPushButton(this);
actionTextUnderline->setIcon(underlineIcon);
actionTextUnderline->setIconSize(iconSize);
actionTextUnderline->setToolTip( tr("Souligné"));
connect(actionTextUnderline,SIGNAL(clicked(bool)), this, SLOT(textUnderline()));
actionTextUnderline->setCheckable(true);
layout->addWidget(actionTextUnderline,0,2);
QButtonGroup * alignGroup = new QButtonGroup(this);
const QIcon leftIcon(":/images/textleft.png");
actionAlignLeft = new QPushButton(this);
actionAlignLeft->setIcon(leftIcon);
actionAlignLeft->setIconSize(iconSize);
actionAlignLeft->setToolTip(tr("Aligné à gauche"));
actionAlignLeft->setCheckable(true);
connect(actionAlignLeft,SIGNAL(clicked(bool)), this, SLOT(textAlign()));
layout->addWidget(actionAlignLeft,0,3);
alignGroup->addButton(actionAlignLeft);
const QIcon centerIcon(":/images/textcenter.png");
actionAlignCenter = new QPushButton(this);
actionAlignCenter->setIcon(centerIcon);
actionAlignCenter->setIconSize(iconSize);
actionAlignCenter->setToolTip(tr("Centré"));
actionAlignCenter->setCheckable(true);
connect(actionAlignCenter,SIGNAL(clicked(bool)), this, SLOT(textAlign()));
layout->addWidget(actionAlignCenter,0,4);
alignGroup->addButton(actionAlignCenter);
const QIcon rightIcon(":/images/textright.png");
actionAlignRight = new QPushButton(this);
actionAlignRight->setIcon(rightIcon);
actionAlignRight->setIconSize(iconSize);
actionAlignRight->setToolTip(tr("Aligné à droite"));
actionAlignRight->setCheckable(true);
connect(actionAlignRight,SIGNAL(clicked(bool)), this, SLOT(textAlign()));
layout->addWidget(actionAlignRight,0,5);
alignGroup->addButton(actionAlignRight);
const QIcon fillIcon(":/images/textjustify.png");
actionAlignJustify = new QPushButton(this);
actionAlignJustify->setIcon(fillIcon);
actionAlignJustify->setIconSize(iconSize);
actionAlignJustify->setToolTip(tr("Justifié"));
actionAlignJustify->setCheckable(true);
connect(actionAlignJustify,SIGNAL(clicked(bool)), this, SLOT(textAlign()));
layout->addWidget(actionAlignJustify,0,6);
alignGroup->addButton(actionAlignJustify);
QPixmap pix(32,32);
pix.fill(Qt::black);
actionTextColor = new QPushButton(this);
actionTextColor->setIcon(QIcon(pix));
actionTextColor->setIconSize(iconSize);
actionTextColor->setToolTip(tr("Couleur"));
connect(actionTextColor,SIGNAL(clicked(bool)), this, SLOT(textColor()));
layout->addWidget(actionTextColor,0,7);
comboFont = new QFontComboBox(this);
connect(comboFont, QOverload<const QString &>::of(&QComboBox::activated), this, &TextEdit::textFamily);
layout->addWidget(comboFont,0,8);
comboSize = new QComboBox(this);
comboSize->setObjectName("comboSize");
comboSize->setEditable(true);
const QList<int> standardSizes = QFontDatabase::standardSizes();
foreach (int size, standardSizes)
comboSize->addItem(QString::number(size));
comboSize->setCurrentIndex(standardSizes.indexOf(QApplication::font().pointSize()));
connect(comboSize, QOverload<const QString &>::of(&QComboBox::activated), this, &TextEdit::textSize);
layout->addWidget(comboSize,0,9);
QPushButton * image = new QPushButton(this);
image->setIcon(QIcon(":/images/image.png"));
image->setIconSize(iconSize);
image->setToolTip(tr("Insérer une image..."));
layout->addWidget(image,0,10);
connect(image,SIGNAL(clicked(bool)),this,SLOT(insertImage()));
textEdit = new QTextEdit(this);
connect(textEdit, &QTextEdit::currentCharFormatChanged,
this, &TextEdit::currentCharFormatChanged);
connect(textEdit, &QTextEdit::cursorPositionChanged,
this, &TextEdit::cursorPositionChanged);
connect(textEdit,SIGNAL(textChanged()),this,SLOT(contentChanged()));
textEdit->setUndoRedoEnabled(true);
layout->addWidget(textEdit,1,0,1,11);
QFont textFont("Helvetica");
textFont.setStyleHint(QFont::SansSerif);
textEdit->setFont(textFont);
fontChanged(textEdit->font());
colorChanged(textEdit->textColor());
alignmentChanged(textEdit->alignment());
textEdit->setFocus();
/*
textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
connect(textEdit, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(showContextMenu(const QPoint &)));
*/
//hunspell = parent->getSpellChecker();
//codec = parent->getTextCodec();
}
QString TextEdit::toHtml()
{
return textEdit->toHtml();
}
void TextEdit::setHtml(QString html)
{
textEdit->setHtml(html);
}
void TextEdit::textBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void TextEdit::textUnderline()
{
QTextCharFormat fmt;
fmt.setFontUnderline(actionTextUnderline->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void TextEdit::textItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(actionTextItalic->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void TextEdit::textFamily(const QString &f)
{
QTextCharFormat fmt;
fmt.setFontFamily(f);
mergeFormatOnWordOrSelection(fmt);
}
void TextEdit::textSize(const QString &p)
{
qreal pointSize = p.toFloat();
if (p.toFloat() > 0) {
QTextCharFormat fmt;
fmt.setFontPointSize(pointSize);
mergeFormatOnWordOrSelection(fmt);
}
}
void TextEdit::textColor()
{
QColor col = QColorDialog::getColor(textEdit->textColor(), this);
if (!col.isValid())
return;
QTextCharFormat fmt;
fmt.setForeground(col);
mergeFormatOnWordOrSelection(fmt);
colorChanged(col);
}
void TextEdit::textAlign()
{
QPushButton * a=(QPushButton*)sender();
if (a == actionAlignLeft)
textEdit->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
else if (a == actionAlignCenter)
textEdit->setAlignment(Qt::AlignHCenter);
else if (a == actionAlignRight)
textEdit->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
else if (a == actionAlignJustify)
textEdit->setAlignment(Qt::AlignJustify);
}
void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
{
fontChanged(format.font());
colorChanged(format.foreground().color());
}
void TextEdit::cursorPositionChanged()
{
alignmentChanged(textEdit->alignment());
}
void TextEdit::clipboardDataChanged()
{
}
void TextEdit::contentChanged()
{
emit textChanged();
/*
QString text = textEdit->toPlainText();
text = text.replace('.',' ');
text = text.replace(',',' ');
text = text.replace('-',' ');
text = text.replace('"',' ');
text = text.replace('?',' ');
text = text.replace('!',' ');
text = text.replace(';',' ');
text = text.replace(':',' ');
text = text.replace("'"," ");
QStringList list = text.split(' ',QString::SkipEmptyParts);
QStringList misspelled_words;
QStringList goodspelled_words;
for (int i = 0 ; i < list.count() ; ++i)
if ((list[i].length()>3)&&(!isSpellingCorrect(list[i])))
misspelled_words << list[i] ;
else
goodspelled_words << list [i] ;
disconnect(textEdit,SIGNAL(textChanged()),this,SLOT(contentChanged()));
QTextDocument * doc = textEdit->document();
QTextCharFormat format;
format.setFontUnderline(false);
for (int i = 0 ; i < goodspelled_words.count() ; ++i)
{
QTextCursor cursor = doc->find(goodspelled_words[i]);
do
{
cursor.mergeCharFormat(format);
cursor = doc->find(goodspelled_words[i],cursor);
}
while(cursor!=QTextCursor());
}
format.setUnderlineColor(QColor(Qt::red));
format.setFontUnderline(true);
for (int i = 0 ; i < misspelled_words.count() ; ++i)
{
QTextCursor cursor = doc->find(misspelled_words[i]);
do
{
cursor.mergeCharFormat(format);
cursor = doc->find(misspelled_words[i],cursor);
}
while(cursor!=QTextCursor());
}
connect(textEdit,SIGNAL(textChanged()),this,SLOT(contentChanged()));
*/
}
/*
bool TextEdit::isSpellingCorrect(const QString &word) const {
bool isOk = false;
try {
isOk = hunspell->spell(codec->fromUnicode(word).constData());
}
catch (...) {
isOk = false;
}
return isOk;
}
*/
/*
void TextEdit::showContextMenu(const QPoint & pos)
{
QMenu* stdMenu= textEdit->createStandardContextMenu();
tc = textEdit->cursorForPosition(pos);
tc.select(QTextCursor::WordUnderCursor);
QString word = tc.selectedText();
if (!isSpellingCorrect(word))
{
QStringList suggestions = suggestCorrections(word);
QAction *first = stdMenu->actions().first();
for (int i = 0 ; i < suggestions.count() ; ++i)
{
QAction * action = new QAction(suggestions[i],stdMenu) ;
stdMenu->insertAction(first,action);
connect(action,SIGNAL(triggered()),this,SLOT(correct()));
}
stdMenu->insertSeparator(first);
}
stdMenu->popup(textEdit->viewport()->mapToGlobal(pos));
}
*/
/*
QStringList TextEdit::suggestCorrections(const QString &word) {
QStringList suggestions;
char **suggestWordList = NULL;
try {
// Encode from Unicode to the encoding used by current dictionary
int count = hunspell->suggest(&suggestWordList, codec->fromUnicode(word).constData());
for (int i = 0; i < count; ++i) {
QString suggestion = codec->toUnicode(suggestWordList[i]);
suggestions << suggestion;
free(suggestWordList[i]);
}
}
catch (...) {
qDebug() << "Error for keyword:" << word;
}
return suggestions;
}
void TextEdit::correct(void)
{
QAction * action = (QAction*)sender();
tc.insertText(action->text(),QTextCharFormat());
}
*/
void TextEdit::insertImage()
{
const QString fileName = QFileDialog::getOpenFileName(this,tr("Sélectionnez l'image que vous désirez insérer."),QDir::homePath(),"*.png *.jpg");
if (fileName!=QString())
{
QString html("<IMG src=\"");
html.append(fileName);
html.append("\" />");
textEdit->insertHtml(html);
}
}
void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextCursor cursor = textEdit->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
textEdit->mergeCurrentCharFormat(format);
}
void TextEdit::fontChanged(const QFont &f)
{
comboFont->setCurrentIndex(comboFont->findText(QFontInfo(f).family()));
comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
actionTextBold->setChecked(f.bold());
actionTextItalic->setChecked(f.italic());
actionTextUnderline->setChecked(f.underline());
}
void TextEdit::colorChanged(const QColor &c)
{
QPixmap pix(32, 32);
pix.fill(c);
actionTextColor->setIcon(pix);
}
void TextEdit::alignmentChanged(Qt::Alignment a)
{
if (a & Qt::AlignLeft)
actionAlignLeft->setChecked(true);
else if (a & Qt::AlignHCenter)
actionAlignCenter->setChecked(true);
else if (a & Qt::AlignRight)
actionAlignRight->setChecked(true);
else if (a & Qt::AlignJustify)
actionAlignJustify->setChecked(true);
}