-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathhtmleditor.cpp
609 lines (515 loc) · 19.2 KB
/
htmleditor.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information ([email protected])
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at [email protected].
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "htmleditor.h"
#include "highlighter.h"
#include "ui_htmleditor.h"
#include "ui_inserthtmldialog.h"
#include <QtGui>
#include <QtWebKitWidgets>
#define FORWARD_ACTION(action1, action2) \
connect(action1, SIGNAL(triggered()), \
ui->webView->pageAction(action2), SLOT(trigger())); \
connect(ui->webView->pageAction(action2), \
SIGNAL(changed()), SLOT(adjustActions()));
HtmlEditor::HtmlEditor(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui_MainWindow)
, sourceDirty(true)
, highlighter(0)
, ui_dialog(0)
, insertHtmlDialog(0)
{
ui->setupUi(this);
ui->tabWidget->setTabText(0, "Normal View");
ui->tabWidget->setTabText(1, "HTML Source");
connect(ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(changeTab(int)));
resize(600, 600);
highlighter = new Highlighter(ui->plainTextEdit->document());
QWidget *spacer = new QWidget(this);
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
ui->standardToolBar->insertWidget(ui->actionZoomOut, spacer);
zoomLabel = new QLabel;
ui->standardToolBar->insertWidget(ui->actionZoomOut, zoomLabel);
zoomSlider = new QSlider(this);
zoomSlider->setOrientation(Qt::Horizontal);
zoomSlider->setMaximumWidth(150);
zoomSlider->setRange(25, 400);
zoomSlider->setSingleStep(25);
zoomSlider->setPageStep(100);
connect(zoomSlider, SIGNAL(valueChanged(int)), SLOT(changeZoom(int)));
ui->standardToolBar->insertWidget(ui->actionZoomIn, zoomSlider);
connect(ui->actionFileNew, SIGNAL(triggered()), SLOT(fileNew()));
connect(ui->actionFileOpen, SIGNAL(triggered()), SLOT(fileOpen()));
connect(ui->actionFileSave, SIGNAL(triggered()), SLOT(fileSave()));
connect(ui->actionFileSaveAs, SIGNAL(triggered()), SLOT(fileSaveAs()));
connect(ui->actionExit, SIGNAL(triggered()), SLOT(close()));
connect(ui->actionInsertImage, SIGNAL(triggered()), SLOT(insertImage()));
connect(ui->actionCreateLink, SIGNAL(triggered()), SLOT(createLink()));
connect(ui->actionInsertHtml, SIGNAL(triggered()), SLOT(insertHtml()));
connect(ui->actionZoomOut, SIGNAL(triggered()), SLOT(zoomOut()));
connect(ui->actionZoomIn, SIGNAL(triggered()), SLOT(zoomIn()));
// these are forward to internal QWebView
FORWARD_ACTION(ui->actionEditUndo, QWebPage::Undo);
FORWARD_ACTION(ui->actionEditRedo, QWebPage::Redo);
FORWARD_ACTION(ui->actionEditCut, QWebPage::Cut);
FORWARD_ACTION(ui->actionEditCopy, QWebPage::Copy);
FORWARD_ACTION(ui->actionEditPaste, QWebPage::Paste);
FORWARD_ACTION(ui->actionFormatBold, QWebPage::ToggleBold);
FORWARD_ACTION(ui->actionFormatItalic, QWebPage::ToggleItalic);
FORWARD_ACTION(ui->actionFormatUnderline, QWebPage::ToggleUnderline);
// Qt 4.5.0 has a bug: always returns 0 for QWebPage::SelectAll
connect(ui->actionEditSelectAll, SIGNAL(triggered()), SLOT(editSelectAll()));
connect(ui->actionStyleParagraph, SIGNAL(triggered()), SLOT(styleParagraph()));
connect(ui->actionStyleHeading1, SIGNAL(triggered()), SLOT(styleHeading1()));
connect(ui->actionStyleHeading2, SIGNAL(triggered()), SLOT(styleHeading2()));
connect(ui->actionStyleHeading3, SIGNAL(triggered()), SLOT(styleHeading3()));
connect(ui->actionStyleHeading4, SIGNAL(triggered()), SLOT(styleHeading4()));
connect(ui->actionStyleHeading5, SIGNAL(triggered()), SLOT(styleHeading5()));
connect(ui->actionStyleHeading6, SIGNAL(triggered()), SLOT(styleHeading6()));
connect(ui->actionStylePreformatted, SIGNAL(triggered()), SLOT(stylePreformatted()));
connect(ui->actionStyleAddress, SIGNAL(triggered()), SLOT(styleAddress()));
connect(ui->actionFormatFontName, SIGNAL(triggered()), SLOT(formatFontName()));
connect(ui->actionFormatFontSize, SIGNAL(triggered()), SLOT(formatFontSize()));
connect(ui->actionFormatTextColor, SIGNAL(triggered()), SLOT(formatTextColor()));
connect(ui->actionFormatBackgroundColor, SIGNAL(triggered()), SLOT(formatBackgroundColor()));
// no page action exists yet for these, so use execCommand trick
connect(ui->actionFormatStrikethrough, SIGNAL(triggered()), SLOT(formatStrikeThrough()));
connect(ui->actionFormatAlignLeft, SIGNAL(triggered()), SLOT(formatAlignLeft()));
connect(ui->actionFormatAlignCenter, SIGNAL(triggered()), SLOT(formatAlignCenter()));
connect(ui->actionFormatAlignRight, SIGNAL(triggered()), SLOT(formatAlignRight()));
connect(ui->actionFormatAlignJustify, SIGNAL(triggered()), SLOT(formatAlignJustify()));
connect(ui->actionFormatDecreaseIndent, SIGNAL(triggered()), SLOT(formatDecreaseIndent()));
connect(ui->actionFormatIncreaseIndent, SIGNAL(triggered()), SLOT(formatIncreaseIndent()));
connect(ui->actionFormatNumberedList, SIGNAL(triggered()), SLOT(formatNumberedList()));
connect(ui->actionFormatBulletedList, SIGNAL(triggered()), SLOT(formatBulletedList()));
// necessary to sync our actions
connect(ui->webView->page(), SIGNAL(selectionChanged()), SLOT(adjustActions()));
connect(ui->webView->page(), SIGNAL(contentsChanged()), SLOT(adjustSource()));
ui->webView->setFocus();
setCurrentFileName(QString());
QString initialFile = ":/example.html";
const QStringList args = QCoreApplication::arguments();
if (args.count() == 2)
initialFile = args.at(1);
if (!load(initialFile))
fileNew();
adjustActions();
adjustSource();
setWindowModified(false);
changeZoom(100);
}
HtmlEditor::~HtmlEditor()
{
delete ui;
delete ui_dialog;
}
bool HtmlEditor::maybeSave()
{
if (!isWindowModified())
return true;
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("HTML Editor"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard
| QMessageBox::Cancel);
if (ret == QMessageBox::Save)
return fileSave();
else if (ret == QMessageBox::Cancel)
return false;
return true;
}
void HtmlEditor::fileNew()
{
if (maybeSave()) {
ui->webView->setHtml("<p></p>");
ui->webView->setFocus();
ui->webView->page()->setContentEditable(true);
setCurrentFileName(QString());
setWindowModified(false);
// quirk in QWebView: need an initial mouse click to show the cursor
int mx = ui->webView->width() / 2;
int my = ui->webView->height() / 2;
QPoint center = QPoint(mx, my);
QMouseEvent *e1 = new QMouseEvent(QEvent::MouseButtonPress, center,
Qt::LeftButton, Qt::LeftButton,
Qt::NoModifier);
QMouseEvent *e2 = new QMouseEvent(QEvent::MouseButtonRelease, center,
Qt::LeftButton, Qt::LeftButton,
Qt::NoModifier);
QApplication::postEvent(ui->webView, e1);
QApplication::postEvent(ui->webView, e2);
}
}
void HtmlEditor::fileOpen()
{
QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
if (!fn.isEmpty())
load(fn);
}
bool HtmlEditor::fileSave()
{
if (fileName.isEmpty() || fileName.startsWith(QLatin1String(":/")))
return fileSaveAs();
QFile file(fileName);
bool success = file.open(QIODevice::WriteOnly);
if (success) {
// FIXME: here we always use UTF-8 encoding
QString content = ui->webView->page()->mainFrame()->toHtml();
QByteArray data = content.toUtf8();
qint64 c = file.write(data);
success = (c >= data.length());
}
setWindowModified(false);
return success;
}
bool HtmlEditor::fileSaveAs()
{
QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
if (fn.isEmpty())
return false;
if (!(fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)))
fn += ".htm"; // default
setCurrentFileName(fn);
return fileSave();
}
void HtmlEditor::insertImage()
{
QString filters;
filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
filters += tr("Portable Network Graphics (PNG) (*.png);;");
filters += tr("JPEG (*.jpg *.jpeg);;");
filters += tr("Graphics Interchange Format (*.gif);;");
filters += tr("All Files (*)");
QString fn = QFileDialog::getOpenFileName(this, tr("Open image..."),
QString(), filters);
if (fn.isEmpty())
return;
if (!QFile::exists(fn))
return;
QUrl url = QUrl::fromLocalFile(fn);
execCommand("insertImage", url.toString());
}
// shamelessly copied from Qt Demo Browser
static QUrl guessUrlFromString(const QString &string)
{
QString urlStr = string.trimmed();
QRegExp test(QLatin1String("^[a-zA-Z]+\\:.*"));
// Check if it looks like a qualified URL. Try parsing it and see.
bool hasSchema = test.exactMatch(urlStr);
if (hasSchema) {
QUrl url(urlStr, QUrl::TolerantMode);
if (url.isValid())
return url;
}
// Might be a file.
if (QFile::exists(urlStr))
return QUrl::fromLocalFile(urlStr);
// Might be a shorturl - try to detect the schema.
if (!hasSchema) {
int dotIndex = urlStr.indexOf(QLatin1Char('.'));
if (dotIndex != -1) {
QString prefix = urlStr.left(dotIndex).toLower();
QString schema = (prefix == QLatin1String("ftp")) ? prefix : QLatin1String("http");
QUrl url(schema + QLatin1String("://") + urlStr, QUrl::TolerantMode);
if (url.isValid())
return url;
}
}
// Fall back to QUrl's own tolerant parser.
return QUrl(string, QUrl::TolerantMode);
}
void HtmlEditor::createLink()
{
QString link = QInputDialog::getText(this, tr("Create link"),
"Enter URL");
if (!link.isEmpty()) {
QUrl url = guessUrlFromString(link);
if (url.isValid())
execCommand("createLink", url.toString());
}
}
void HtmlEditor::insertHtml()
{
if (!insertHtmlDialog) {
insertHtmlDialog = new QDialog(this);
if (!ui_dialog)
ui_dialog = new Ui_Dialog;
ui_dialog->setupUi(insertHtmlDialog);
connect(ui_dialog->buttonBox, SIGNAL(accepted()),
insertHtmlDialog, SLOT(accept()));
connect(ui_dialog->buttonBox, SIGNAL(rejected()),
insertHtmlDialog, SLOT(reject()));
}
ui_dialog->plainTextEdit->clear();
ui_dialog->plainTextEdit->setFocus();
Highlighter *hilite = new Highlighter(ui_dialog->plainTextEdit->document());
if (insertHtmlDialog->exec() == QDialog::Accepted)
execCommand("insertHTML", ui_dialog->plainTextEdit->toPlainText());
delete hilite;
}
void HtmlEditor::zoomOut()
{
int percent = static_cast<int>(ui->webView->zoomFactor() * 100);
if (percent > 25) {
percent -= 25;
percent = 25 * (int((percent + 25 - 1) / 25));
qreal factor = static_cast<qreal>(percent) / 100;
ui->webView->setZoomFactor(factor);
ui->actionZoomOut->setEnabled(percent > 25);
ui->actionZoomIn->setEnabled(true);
zoomSlider->setValue(percent);
}
}
void HtmlEditor::zoomIn()
{
int percent = static_cast<int>(ui->webView->zoomFactor() * 100);
if (percent < 400) {
percent += 25;
percent = 25 * (int(percent / 25));
qreal factor = static_cast<qreal>(percent) / 100;
ui->webView->setZoomFactor(factor);
ui->actionZoomIn->setEnabled(percent < 400);
ui->actionZoomOut->setEnabled(true);
zoomSlider->setValue(percent);
}
}
void HtmlEditor::editSelectAll()
{
ui->webView->triggerPageAction(QWebPage::SelectAll);
}
void HtmlEditor::execCommand(const QString &cmd)
{
QWebFrame *frame = ui->webView->page()->mainFrame();
QString js = QString("document.execCommand(\"%1\", false, null)").arg(cmd);
frame->evaluateJavaScript(js);
}
void HtmlEditor::execCommand(const QString &cmd, const QString &arg)
{
QWebFrame *frame = ui->webView->page()->mainFrame();
QString js = QString("document.execCommand(\"%1\", false, \"%2\")").arg(cmd).arg(arg);
frame->evaluateJavaScript(js);
}
bool HtmlEditor::queryCommandState(const QString &cmd)
{
QWebFrame *frame = ui->webView->page()->mainFrame();
QString js = QString("document.queryCommandState(\"%1\", false, null)").arg(cmd);
QVariant result = frame->evaluateJavaScript(js);
return result.toString().simplified().toLower() == "true";
}
void HtmlEditor::styleParagraph()
{
execCommand("formatBlock", "p");
}
void HtmlEditor::styleHeading1()
{
execCommand("formatBlock", "h1");
}
void HtmlEditor::styleHeading2()
{
execCommand("formatBlock", "h2");
}
void HtmlEditor::styleHeading3()
{
execCommand("formatBlock", "h3");
}
void HtmlEditor::styleHeading4()
{
execCommand("formatBlock", "h4");
}
void HtmlEditor::styleHeading5()
{
execCommand("formatBlock", "h5");
}
void HtmlEditor::styleHeading6()
{
execCommand("formatBlock", "h6");
}
void HtmlEditor::stylePreformatted()
{
execCommand("formatBlock", "pre");
}
void HtmlEditor::styleAddress()
{
execCommand("formatBlock", "address");
}
void HtmlEditor::formatStrikeThrough()
{
execCommand("strikeThrough");
}
void HtmlEditor::formatAlignLeft()
{
execCommand("justifyLeft");
}
void HtmlEditor::formatAlignCenter()
{
execCommand("justifyCenter");
}
void HtmlEditor::formatAlignRight()
{
execCommand("justifyRight");
}
void HtmlEditor::formatAlignJustify()
{
execCommand("justifyFull");
}
void HtmlEditor::formatIncreaseIndent()
{
execCommand("indent");
}
void HtmlEditor::formatDecreaseIndent()
{
execCommand("outdent");
}
void HtmlEditor::formatNumberedList()
{
execCommand("insertOrderedList");
}
void HtmlEditor::formatBulletedList()
{
execCommand("insertUnorderedList");
}
void HtmlEditor::formatFontName()
{
QStringList families = QFontDatabase().families();
bool ok = false;
QString family = QInputDialog::getItem(this, tr("Font"), tr("Select font:"),
families, 0, false, &ok);
if (ok)
execCommand("fontName", family);
}
void HtmlEditor::formatFontSize()
{
QStringList sizes;
sizes << "xx-small";
sizes << "x-small";
sizes << "small";
sizes << "medium";
sizes << "large";
sizes << "x-large";
sizes << "xx-large";
bool ok = false;
QString size = QInputDialog::getItem(this, tr("Font Size"), tr("Select font size:"),
sizes, sizes.indexOf("medium"), false, &ok);
if (ok)
execCommand("fontSize", QString::number(sizes.indexOf(size)));
}
void HtmlEditor::formatTextColor()
{
QColor color = QColorDialog::getColor(Qt::black, this);
if (color.isValid())
execCommand("foreColor", color.name());
}
void HtmlEditor::formatBackgroundColor()
{
QColor color = QColorDialog::getColor(Qt::white, this);
if (color.isValid())
execCommand("hiliteColor", color.name());
}
#define FOLLOW_ENABLE(a1, a2) a1->setEnabled(ui->webView->pageAction(a2)->isEnabled())
#define FOLLOW_CHECK(a1, a2) a1->setChecked(ui->webView->pageAction(a2)->isChecked())
void HtmlEditor::adjustActions()
{
FOLLOW_ENABLE(ui->actionEditUndo, QWebPage::Undo);
FOLLOW_ENABLE(ui->actionEditRedo, QWebPage::Redo);
FOLLOW_ENABLE(ui->actionEditCut, QWebPage::Cut);
FOLLOW_ENABLE(ui->actionEditCopy, QWebPage::Copy);
FOLLOW_ENABLE(ui->actionEditPaste, QWebPage::Paste);
FOLLOW_CHECK(ui->actionFormatBold, QWebPage::ToggleBold);
FOLLOW_CHECK(ui->actionFormatItalic, QWebPage::ToggleItalic);
FOLLOW_CHECK(ui->actionFormatUnderline, QWebPage::ToggleUnderline);
ui->actionFormatStrikethrough->setChecked(queryCommandState("strikeThrough"));
ui->actionFormatNumberedList->setChecked(queryCommandState("insertOrderedList"));
ui->actionFormatBulletedList->setChecked(queryCommandState("insertUnorderedList"));
}
void HtmlEditor::adjustSource()
{
setWindowModified(true);
sourceDirty = true;
if (ui->tabWidget->currentIndex() == 1)
changeTab(1);
}
void HtmlEditor::changeTab(int index)
{
if (sourceDirty && (index == 1)) {
QString content = ui->webView->page()->mainFrame()->toHtml();
ui->plainTextEdit->setPlainText(content);
sourceDirty = false;
}
}
void HtmlEditor::openLink(const QUrl &url)
{
QString msg = QString(tr("Open %1 ?")).arg(url.toString());
if (QMessageBox::question(this, tr("Open link"), msg,
QMessageBox::Open | QMessageBox::Cancel) ==
QMessageBox::Open)
QDesktopServices::openUrl(url);
}
void HtmlEditor::changeZoom(int percent)
{
ui->actionZoomOut->setEnabled(percent > 25);
ui->actionZoomIn->setEnabled(percent < 400);
qreal factor = static_cast<qreal>(percent) / 100;
ui->webView->setZoomFactor(factor);
zoomLabel->setText(tr(" Zoom: %1% ").arg(percent));
zoomSlider->setValue(percent);
}
void HtmlEditor::closeEvent(QCloseEvent *e)
{
if (maybeSave())
e->accept();
else
e->ignore();
}
bool HtmlEditor::load(const QString &f)
{
if (!QFile::exists(f))
return false;
QFile file(f);
if (!file.open(QFile::ReadOnly))
return false;
QByteArray data = file.readAll();
ui->webView->setContent(data, "text/html");
ui->webView->page()->setContentEditable(true);
ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
connect(ui->webView, SIGNAL(linkClicked(QUrl)), SLOT(openLink(QUrl)));
setCurrentFileName(f);
return true;
}
void HtmlEditor::setCurrentFileName(const QString &fileName)
{
this->fileName = fileName;
QString shownName;
if (fileName.isEmpty())
shownName = "untitled";
else
shownName = QFileInfo(fileName).fileName();
setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("HTML Editor")));
setWindowModified(false);
bool allowSave = true;
if (fileName.isEmpty() || fileName.startsWith(QLatin1String(":/")))
allowSave = false;
ui->actionFileSave->setEnabled(allowSave);
}