fb2text.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. #include "fb2text.hpp"
  2. #include "fb2dlgs.hpp"
  3. #include "fb2read.hpp"
  4. #include "fb2save.hpp"
  5. #include "fb2utils.h"
  6. #include "fb2html.h"
  7. #include "fb2xml2.h"
  8. #include <QAction>
  9. #include <QBoxLayout>
  10. #include <QDockWidget>
  11. #include <QFileDialog>
  12. #include <QMainWindow>
  13. #include <QNetworkRequest>
  14. #include <QStyle>
  15. #include <QStyleOptionFrame>
  16. #include <QToolBar>
  17. #include <QToolTip>
  18. #include <QUndoCommand>
  19. #include <QUndoStack>
  20. #include <QWebElement>
  21. #include <QWebInspector>
  22. #include <QWebFrame>
  23. #include <QWebPage>
  24. #include <QtDebug>
  25. //---------------------------------------------------------------------------
  26. // FbNoteView
  27. //---------------------------------------------------------------------------
  28. class FbNoteView : public QWebView
  29. {
  30. public:
  31. explicit FbNoteView(QWidget *parent, const QUrl &url);
  32. void hint(const QWebElement element, const QRect &rect);
  33. protected:
  34. void paintEvent(QPaintEvent *event);
  35. const QUrl m_url;
  36. };
  37. FbNoteView::FbNoteView(QWidget *parent, const QUrl &url)
  38. : QWebView(parent)
  39. , m_url(url)
  40. {
  41. }
  42. void FbNoteView::paintEvent(QPaintEvent *event)
  43. {
  44. QWebView::paintEvent(event);
  45. QPainter painter(this);
  46. painter.setPen(Qt::black);
  47. QSize size = geometry().size() - QSize(1, 1);
  48. painter.drawRect( QRect(QPoint(0, 0), size) );
  49. }
  50. void FbNoteView::hint(const QWebElement element, const QRect &rect)
  51. {
  52. QString html = element.toOuterXml();
  53. html.prepend(
  54. "<body bgcolor=lightyellow style='overflow:hidden;padding:0;margin:0;margin-top:2;'>"
  55. "<div class=body fb2_name=notes style='padding:0;margin:0;'>"
  56. );
  57. html.append("</div></body>");
  58. setGeometry(rect);
  59. setHtml(html, m_url);
  60. show();
  61. }
  62. //---------------------------------------------------------------------------
  63. // FbTextPage
  64. //---------------------------------------------------------------------------
  65. FbTextPage::FbTextPage(QObject *parent)
  66. : QWebPage(parent)
  67. {
  68. QWebSettings *s = settings();
  69. s->setAttribute(QWebSettings::AutoLoadImages, true);
  70. s->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
  71. s->setAttribute(QWebSettings::JavaEnabled, false);
  72. s->setAttribute(QWebSettings::JavascriptEnabled, true);
  73. s->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
  74. s->setAttribute(QWebSettings::PluginsEnabled, false);
  75. s->setAttribute(QWebSettings::ZoomTextOnly, true);
  76. s->setUserStyleSheetUrl(QUrl::fromLocalFile(":style.css"));
  77. }
  78. bool FbTextPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type)
  79. {
  80. Q_UNUSED(frame);
  81. if (type == NavigationTypeLinkClicked) {
  82. qCritical() << request.url().fragment();
  83. return false;
  84. // QToolTip::showText(request.url().fragment());
  85. }
  86. return QWebPage::acceptNavigationRequest(frame, request, type);
  87. }
  88. QString FbTextPage::div(const QString &style, const QString &text)
  89. {
  90. return QString("<div class=%1>%2</div>").arg(style).arg(text);
  91. }
  92. QString FbTextPage::p(const QString &text)
  93. {
  94. return QString("<p>%1</p>").arg(text);
  95. }
  96. FbTextElement FbTextPage::body()
  97. {
  98. return doc().findFirst("body");
  99. }
  100. FbTextElement FbTextPage::doc()
  101. {
  102. return mainFrame()->documentElement();
  103. }
  104. void FbTextPage::push(QUndoCommand * command, const QString &text)
  105. {
  106. undoStack()->beginMacro(text);
  107. undoStack()->push(command);
  108. undoStack()->endMacro();
  109. }
  110. void FbTextPage::update()
  111. {
  112. emit contentsChanged();
  113. emit selectionChanged();
  114. }
  115. void FbTextPage::appendSection(const FbTextElement &parent)
  116. {
  117. QString html = div("section", div("title", p()) + p());
  118. FbTextElement element = parent;
  119. element.appendInside(html);
  120. element = parent.lastChild();
  121. QUndoCommand * command = new FbInsertCmd(element);
  122. push(command, tr("Append section"));
  123. }
  124. void FbTextPage::insertBody()
  125. {
  126. QString html = div("body", div("title", p()) + div("section", div("title", p()) + p()));
  127. FbTextElement element = body();
  128. element.appendInside(html);
  129. element = element.lastChild();
  130. QUndoCommand * command = new FbInsertCmd(element);
  131. push(command, tr("Append body"));
  132. }
  133. void FbTextPage::insertSection()
  134. {
  135. FbTextElement element = current();
  136. while (!element.isNull()) {
  137. if (element.isSection() || element.isBody()) {
  138. appendSection(element);
  139. break;
  140. }
  141. element = element.parent();
  142. }
  143. }
  144. void FbTextPage::insertTitle()
  145. {
  146. FbTextElement element = current();
  147. while (!element.isNull()) {
  148. FbTextElement parent = element.parent();
  149. if ((parent.isSection() || parent.isBody()) && !parent.hasTitle()) {
  150. QString html = div("title", p());
  151. parent.prependInside(html);
  152. element = parent.firstChild();
  153. QUndoCommand * command = new FbInsertCmd(element);
  154. push(command, tr("Insert title"));
  155. break;
  156. }
  157. element = parent;
  158. }
  159. }
  160. void FbTextPage::insertSubtitle()
  161. {
  162. FbTextElement element = current();
  163. while (!element.isNull()) {
  164. FbTextElement parent = element.parent();
  165. if (parent.isSection()) {
  166. QString html = div("subtitle", p());
  167. if (element.isTitle()) {
  168. element.appendOutside(html);
  169. element = element.nextSibling();
  170. } else {
  171. element.prependOutside(html);
  172. element = element.previousSibling();
  173. }
  174. QUndoCommand * command = new FbInsertCmd(element);
  175. push(command, tr("Insert subtitle"));
  176. break;
  177. }
  178. element = parent;
  179. }
  180. }
  181. void FbTextPage::insertPoem()
  182. {
  183. FbTextElement element = current();
  184. while (!element.isNull()) {
  185. FbTextElement parent = element.parent();
  186. if (parent.isSection()) {
  187. QString html = div("poem", div("stanza", p()));
  188. if (element.isTitle()) {
  189. element.appendOutside(html);
  190. element = element.nextSibling();
  191. } else {
  192. element.prependOutside(html);
  193. element = element.previousSibling();
  194. }
  195. QUndoCommand * command = new FbInsertCmd(element);
  196. push(command, tr("Insert poem"));
  197. break;
  198. }
  199. element = parent;
  200. }
  201. }
  202. void FbTextPage::insertStanza()
  203. {
  204. FbTextElement element = current();
  205. while (!element.isNull()) {
  206. if (element.isStanza()) {
  207. QString html = div("stanza", p());
  208. element.appendOutside(html);
  209. element = element.nextSibling();
  210. QUndoCommand * command = new FbInsertCmd(element);
  211. push(command, tr("Append stanza"));
  212. break;
  213. }
  214. element = element.parent();
  215. }
  216. }
  217. void FbTextPage::insertAnnot()
  218. {
  219. }
  220. void FbTextPage::insertAuthor()
  221. {
  222. }
  223. void FbTextPage::insertEpigraph()
  224. {
  225. const QString type = "epigraph";
  226. FbTextElement element = current();
  227. while (!element.isNull()) {
  228. if (element.hasSubtype(type)) {
  229. QString html = div("epigraph", p());
  230. element = element.insertInside(type, html);
  231. QUndoCommand * command = new FbInsertCmd(element);
  232. push(command, tr("Insert epigraph"));
  233. break;
  234. }
  235. element = element.parent();
  236. }
  237. }
  238. void FbTextPage::insertDate()
  239. {
  240. }
  241. FbTextElement FbTextPage::current()
  242. {
  243. return element(location());
  244. }
  245. FbTextElement FbTextPage::element(const QString &location)
  246. {
  247. QStringList list = location.split(",");
  248. QStringListIterator iterator(list);
  249. QWebElement result = doc();
  250. while (iterator.hasNext()) {
  251. QString str = iterator.next();
  252. int pos = str.indexOf("=");
  253. QString tag = str.left(pos);
  254. int key = str.mid(pos + 1).toInt();
  255. if (key < 0) break;
  256. result = result.firstChild();
  257. while (0 < key--) result = result.nextSibling();
  258. }
  259. return result;
  260. }
  261. QString FbTextPage::location()
  262. {
  263. static const QString javascript = FB2::read(":/js/get_location.js").prepend("var element=document.getSelection().anchorNode;");
  264. return mainFrame()->evaluateJavaScript(javascript).toString();
  265. }
  266. QString FbTextPage::status()
  267. {
  268. static const QString javascript = FB2::read(":/js/get_status.js");
  269. return mainFrame()->evaluateJavaScript(javascript).toString();
  270. }
  271. //---------------------------------------------------------------------------
  272. // FbTextBase
  273. //---------------------------------------------------------------------------
  274. void FbTextBase::addTools(QToolBar *tool)
  275. {
  276. QAction *act;
  277. act = pageAction(QWebPage::Undo);
  278. act->setIcon(FbIcon("edit-undo"));
  279. act->setText(QObject::tr("&Undo"));
  280. act->setPriority(QAction::LowPriority);
  281. act->setShortcut(QKeySequence::Undo);
  282. tool->addAction(act);
  283. act = pageAction(QWebPage::Redo);
  284. act->setIcon(FbIcon("edit-redo"));
  285. act->setText(QObject::tr("&Redo"));
  286. act->setPriority(QAction::LowPriority);
  287. act->setShortcut(QKeySequence::Redo);
  288. tool->addAction(act);
  289. tool->addSeparator();
  290. act = pageAction(QWebPage::Cut);
  291. act->setIcon(FbIcon("edit-cut"));
  292. act->setText(QObject::tr("Cu&t"));
  293. act->setPriority(QAction::LowPriority);
  294. act->setShortcuts(QKeySequence::Cut);
  295. act->setStatusTip(QObject::tr("Cut the current selection's contents to the clipboard"));
  296. tool->addAction(act);
  297. act = pageAction(QWebPage::Copy);
  298. act->setIcon(FbIcon("edit-copy"));
  299. act->setText(QObject::tr("&Copy"));
  300. act->setPriority(QAction::LowPriority);
  301. act->setShortcuts(QKeySequence::Copy);
  302. act->setStatusTip(QObject::tr("Copy the current selection's contents to the clipboard"));
  303. tool->addAction(act);
  304. act = pageAction(QWebPage::Paste);
  305. act->setIcon(FbIcon("edit-paste"));
  306. act->setText(QObject::tr("&Paste"));
  307. act->setPriority(QAction::LowPriority);
  308. act->setShortcuts(QKeySequence::Paste);
  309. act->setStatusTip(QObject::tr("Paste the clipboard's contents into the current selection"));
  310. tool->addAction(act);
  311. tool->addSeparator();
  312. act = pageAction(QWebPage::ToggleBold);
  313. act->setIcon(FbIcon("format-text-bold"));
  314. act->setText(QObject::tr("&Bold"));
  315. tool->addAction(act);
  316. act = pageAction(QWebPage::ToggleItalic);
  317. act->setIcon(FbIcon("format-text-italic"));
  318. act->setText(QObject::tr("&Italic"));
  319. tool->addAction(act);
  320. act = pageAction(QWebPage::ToggleStrikethrough);
  321. act->setIcon(FbIcon("format-text-strikethrough"));
  322. act->setText(QObject::tr("&Strikethrough"));
  323. tool->addAction(act);
  324. act = pageAction(QWebPage::ToggleSuperscript);
  325. act->setIcon(FbIcon("format-text-superscript"));
  326. act->setText(QObject::tr("Su&perscript"));
  327. tool->addAction(act);
  328. act = pageAction(QWebPage::ToggleSubscript);
  329. act->setIcon(FbIcon("format-text-subscript"));
  330. act->setText(QObject::tr("Su&bscript"));
  331. tool->addAction(act);
  332. }
  333. //---------------------------------------------------------------------------
  334. // FbTextEdit
  335. //---------------------------------------------------------------------------
  336. FbTextEdit::FbTextEdit(QWidget *parent)
  337. : FbTextBase(parent)
  338. , m_noteView(0)
  339. , m_thread(0)
  340. {
  341. setPage(new FbTextPage(this));
  342. page()->setNetworkAccessManager(new FbNetworkAccessManager(*this));
  343. page()->setContentEditable(true);
  344. connect(page(), SIGNAL(contentsChanged()), this, SLOT(fixContents()));
  345. connect(page(), SIGNAL(linkHovered(QString,QString,QString)), this, SLOT(linkHovered(QString,QString,QString)));
  346. connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  347. }
  348. FbTextEdit::~FbTextEdit()
  349. {
  350. if (m_noteView) delete m_noteView;
  351. }
  352. FbTextPage * FbTextEdit::page()
  353. {
  354. return qobject_cast<FbTextPage*>(FbTextBase::page());
  355. }
  356. FbNoteView & FbTextEdit::noteView()
  357. {
  358. if (m_noteView) return *m_noteView;
  359. m_noteView = new FbNoteView(qobject_cast<QWidget*>(parent()), url());
  360. m_noteView->setPage(new FbTextPage(this));
  361. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  362. m_noteView->page()->setContentEditable(false);
  363. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  364. return *m_noteView;
  365. }
  366. QWebElement FbTextEdit::body()
  367. {
  368. return doc().findFirst("body");
  369. }
  370. QWebElement FbTextEdit::doc()
  371. {
  372. return page()->mainFrame()->documentElement();
  373. }
  374. void FbTextEdit::fixContents()
  375. {
  376. foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
  377. span.removeAttribute("style");
  378. }
  379. }
  380. void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
  381. {
  382. m_point = event->pos();
  383. QWebView::mouseMoveEvent(event);
  384. }
  385. void FbTextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  386. {
  387. Q_UNUSED(title);
  388. Q_UNUSED(textContent);
  389. const QString href = QUrl(link).fragment();
  390. if (href.isEmpty()) {
  391. if (m_noteView) m_noteView->hide();
  392. return;
  393. }
  394. const QString query = QString("DIV#%1").arg(href);
  395. const QWebElement element = doc().findFirst(query);
  396. if (element.isNull()) {
  397. if (m_noteView) m_noteView->hide();
  398. return;
  399. }
  400. QRect rect = geometry();
  401. QSize size = element.geometry().size() + QSize(2, 4);
  402. int center = rect.size().height() / 2;
  403. int h = size.height();
  404. if (h > center) size.setHeight(center - 10);
  405. int x = (rect.size().width() - size.width()) / 2;
  406. int y = m_point.y();
  407. if ( y > h ) y = y - h - 10; else y = y + 10;
  408. QPoint point = QPoint(x, y) + rect.topLeft();
  409. noteView().hint(element, QRect(point, size));
  410. }
  411. void FbTextEdit::load(const QString &filename, const QString &xml)
  412. {
  413. if (m_thread) return;
  414. m_thread = new FbReadThread(this, filename, xml);
  415. m_thread->start();
  416. }
  417. bool FbTextEdit::save(QIODevice *device, const QString &codec)
  418. {
  419. FbSaveWriter writer(*this, device);
  420. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  421. bool ok = FbSaveHandler(writer).save();
  422. if (ok) page()->undoStack()->setClean();
  423. return ok;
  424. }
  425. bool FbTextEdit::save(QByteArray *array)
  426. {
  427. FbSaveWriter writer(*this, array);
  428. return FbSaveHandler(writer).save();
  429. }
  430. bool FbTextEdit::save(QString *string)
  431. {
  432. // Use class QByteArray instead QString
  433. // to store information about encoding.
  434. QByteArray data;
  435. bool ok = save(&data);
  436. if (ok) *string = QString::fromUtf8(data.data());
  437. return ok;
  438. }
  439. void FbTextEdit::data(QString name, QByteArray data)
  440. {
  441. m_files.set(name, data);
  442. }
  443. void FbTextEdit::html(QString html)
  444. {
  445. static int number = 0;
  446. setHtml(html, QUrl(QString("fb2:/%1/").arg(number++)));
  447. if (m_thread) m_thread->deleteLater();
  448. m_thread = 0;
  449. }
  450. void FbTextEdit::zoomIn()
  451. {
  452. qreal zoom = zoomFactor();
  453. setZoomFactor(zoom * 1.1);
  454. }
  455. void FbTextEdit::zoomOut()
  456. {
  457. qreal zoom = zoomFactor();
  458. setZoomFactor(zoom * 0.9);
  459. }
  460. void FbTextEdit::zoomReset()
  461. {
  462. setZoomFactor(1);
  463. }
  464. bool FbTextEdit::UndoEnabled()
  465. {
  466. return pageAction(QWebPage::Undo)->isEnabled();
  467. }
  468. bool FbTextEdit::RedoEnabled()
  469. {
  470. return pageAction(QWebPage::Redo)->isEnabled();
  471. }
  472. bool FbTextEdit::CutEnabled()
  473. {
  474. return pageAction(QWebPage::Cut)->isEnabled();
  475. }
  476. bool FbTextEdit::CopyEnabled()
  477. {
  478. return pageAction(QWebPage::Copy)->isEnabled();
  479. }
  480. bool FbTextEdit::BoldChecked()
  481. {
  482. return pageAction(QWebPage::ToggleBold)->isChecked();
  483. }
  484. bool FbTextEdit::ItalicChecked()
  485. {
  486. return pageAction(QWebPage::ToggleItalic)->isChecked();
  487. }
  488. bool FbTextEdit::StrikeChecked()
  489. {
  490. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  491. }
  492. bool FbTextEdit::SubChecked()
  493. {
  494. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  495. }
  496. bool FbTextEdit::SupChecked()
  497. {
  498. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  499. }
  500. void FbTextEdit::find()
  501. {
  502. FbTextFindDlg dlg(*this);
  503. dlg.exec();
  504. }
  505. void FbTextEdit::insertImage()
  506. {
  507. QString filters;
  508. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
  509. filters += tr("Portable Network Graphics (PNG) (*.png);;");
  510. filters += tr("JPEG (*.jpg *.jpeg);;");
  511. filters += tr("Graphics Interchange Format (*.gif);;");
  512. filters += tr("All Files (*)");
  513. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  514. if (path.isEmpty()) return;
  515. QFile file(path);
  516. if (!file.open(QIODevice::ReadOnly)) return;
  517. QByteArray data = file.readAll();
  518. QString name = m_files.add(path, data);
  519. execCommand("insertImage", name.prepend("#"));
  520. }
  521. void FbTextEdit::insertNote()
  522. {
  523. FbNoteDlg dlg(*this);
  524. dlg.exec();
  525. }
  526. void FbTextEdit::insertLink()
  527. {
  528. }
  529. void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
  530. {
  531. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  532. page()->mainFrame()->evaluateJavaScript(javascript);
  533. }
  534. void FbTextEdit::loadFinished()
  535. {
  536. FbTextElement element = body().findFirst("div.body");
  537. if (element.isNull()) element = body();
  538. element.select();
  539. }
  540. //---------------------------------------------------------------------------
  541. // FbTextFrame
  542. //---------------------------------------------------------------------------
  543. FbTextFrame::FbTextFrame(QWidget* parent)
  544. : QFrame(parent)
  545. , view(this)
  546. , dock(0)
  547. {
  548. setFrameShape(QFrame::StyledPanel);
  549. setFrameShadow(QFrame::Sunken);
  550. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  551. layout->setSpacing(0);
  552. layout->setMargin(0);
  553. layout->addWidget(&view);
  554. }
  555. FbTextFrame::~FbTextFrame()
  556. {
  557. if (dock) dock->deleteLater();
  558. }
  559. void FbTextFrame::showInspector()
  560. {
  561. if (dock) {
  562. dock->show();
  563. return;
  564. }
  565. QMainWindow * main = qobject_cast<QMainWindow*>(parent());
  566. if (!main) return;
  567. dock = new QDockWidget(tr("Web inspector"), this);
  568. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  569. main->addDockWidget(Qt::BottomDockWidgetArea, dock);
  570. QWebInspector * inspector = new QWebInspector(this);
  571. inspector->setPage(view.page());
  572. dock->setWidget(inspector);
  573. }
  574. void FbTextFrame::hideInspector()
  575. {
  576. if (dock) dock->hide();
  577. }