fb2text.cpp 18 KB

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