fb2text.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. element.prependOutside(html);
  168. element = element.previousSibling();
  169. QUndoCommand * command = new FbInsertCmd(element);
  170. push(command, tr("Insert subtitle"));
  171. break;
  172. }
  173. element = parent;
  174. }
  175. }
  176. FbTextElement FbTextPage::current()
  177. {
  178. return element(location());
  179. }
  180. FbTextElement FbTextPage::element(const QString &location)
  181. {
  182. QStringList list = location.split(",");
  183. QStringListIterator iterator(list);
  184. QWebElement result = doc();
  185. while (iterator.hasNext()) {
  186. QString str = iterator.next();
  187. int pos = str.indexOf("=");
  188. QString tag = str.left(pos);
  189. int key = str.mid(pos + 1).toInt();
  190. if (key < 0) break;
  191. result = result.firstChild();
  192. while (0 < key--) result = result.nextSibling();
  193. }
  194. return result;
  195. }
  196. QString FbTextPage::location()
  197. {
  198. static const QString javascript = FB2::read(":/js/get_location.js").prepend("var element=document.getSelection().anchorNode;");
  199. return mainFrame()->evaluateJavaScript(javascript).toString();
  200. }
  201. QString FbTextPage::status()
  202. {
  203. static const QString javascript = FB2::read(":/js/get_status.js");
  204. return mainFrame()->evaluateJavaScript(javascript).toString();
  205. }
  206. //---------------------------------------------------------------------------
  207. // FbTextBase
  208. //---------------------------------------------------------------------------
  209. void FbTextBase::addTools(QToolBar *tool)
  210. {
  211. QAction *act;
  212. act = pageAction(QWebPage::Undo);
  213. act->setIcon(FbIcon("edit-undo"));
  214. act->setText(QObject::tr("&Undo"));
  215. act->setPriority(QAction::LowPriority);
  216. act->setShortcut(QKeySequence::Undo);
  217. tool->addAction(act);
  218. act = pageAction(QWebPage::Redo);
  219. act->setIcon(FbIcon("edit-redo"));
  220. act->setText(QObject::tr("&Redo"));
  221. act->setPriority(QAction::LowPriority);
  222. act->setShortcut(QKeySequence::Redo);
  223. tool->addAction(act);
  224. tool->addSeparator();
  225. act = pageAction(QWebPage::Cut);
  226. act->setIcon(FbIcon("edit-cut"));
  227. act->setText(QObject::tr("Cu&t"));
  228. act->setPriority(QAction::LowPriority);
  229. act->setShortcuts(QKeySequence::Cut);
  230. act->setStatusTip(QObject::tr("Cut the current selection's contents to the clipboard"));
  231. tool->addAction(act);
  232. act = pageAction(QWebPage::Copy);
  233. act->setIcon(FbIcon("edit-copy"));
  234. act->setText(QObject::tr("&Copy"));
  235. act->setPriority(QAction::LowPriority);
  236. act->setShortcuts(QKeySequence::Copy);
  237. act->setStatusTip(QObject::tr("Copy the current selection's contents to the clipboard"));
  238. tool->addAction(act);
  239. act = pageAction(QWebPage::Paste);
  240. act->setIcon(FbIcon("edit-paste"));
  241. act->setText(QObject::tr("&Paste"));
  242. act->setPriority(QAction::LowPriority);
  243. act->setShortcuts(QKeySequence::Paste);
  244. act->setStatusTip(QObject::tr("Paste the clipboard's contents into the current selection"));
  245. tool->addAction(act);
  246. tool->addSeparator();
  247. act = pageAction(QWebPage::ToggleBold);
  248. act->setIcon(FbIcon("format-text-bold"));
  249. act->setText(QObject::tr("&Bold"));
  250. tool->addAction(act);
  251. act = pageAction(QWebPage::ToggleItalic);
  252. act->setIcon(FbIcon("format-text-italic"));
  253. act->setText(QObject::tr("&Italic"));
  254. tool->addAction(act);
  255. act = pageAction(QWebPage::ToggleStrikethrough);
  256. act->setIcon(FbIcon("format-text-strikethrough"));
  257. act->setText(QObject::tr("&Strikethrough"));
  258. tool->addAction(act);
  259. act = pageAction(QWebPage::ToggleSuperscript);
  260. act->setIcon(FbIcon("format-text-superscript"));
  261. act->setText(QObject::tr("Su&perscript"));
  262. tool->addAction(act);
  263. act = pageAction(QWebPage::ToggleSubscript);
  264. act->setIcon(FbIcon("format-text-subscript"));
  265. act->setText(QObject::tr("Su&bscript"));
  266. tool->addAction(act);
  267. }
  268. //---------------------------------------------------------------------------
  269. // FbTextEdit
  270. //---------------------------------------------------------------------------
  271. FbTextEdit::FbTextEdit(QWidget *parent)
  272. : FbTextBase(parent)
  273. , m_noteView(0)
  274. , m_thread(0)
  275. {
  276. setPage(new FbTextPage(this));
  277. page()->setNetworkAccessManager(new FbNetworkAccessManager(*this));
  278. page()->setContentEditable(true);
  279. connect(page(), SIGNAL(contentsChanged()), this, SLOT(fixContents()));
  280. connect(page(), SIGNAL(linkHovered(QString,QString,QString)), this, SLOT(linkHovered(QString,QString,QString)));
  281. connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  282. }
  283. FbTextEdit::~FbTextEdit()
  284. {
  285. if (m_noteView) delete m_noteView;
  286. }
  287. FbTextPage * FbTextEdit::page()
  288. {
  289. return qobject_cast<FbTextPage*>(FbTextBase::page());
  290. }
  291. FbNoteView & FbTextEdit::noteView()
  292. {
  293. if (m_noteView) return *m_noteView;
  294. m_noteView = new FbNoteView(qobject_cast<QWidget*>(parent()), url());
  295. m_noteView->setPage(new FbTextPage(this));
  296. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  297. m_noteView->page()->setContentEditable(false);
  298. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  299. return *m_noteView;
  300. }
  301. QWebElement FbTextEdit::body()
  302. {
  303. return doc().findFirst("body");
  304. }
  305. QWebElement FbTextEdit::doc()
  306. {
  307. return page()->mainFrame()->documentElement();
  308. }
  309. void FbTextEdit::fixContents()
  310. {
  311. foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
  312. span.removeAttribute("style");
  313. }
  314. }
  315. void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
  316. {
  317. m_point = event->pos();
  318. QWebView::mouseMoveEvent(event);
  319. }
  320. void FbTextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  321. {
  322. Q_UNUSED(title);
  323. Q_UNUSED(textContent);
  324. const QString href = QUrl(link).fragment();
  325. if (href.isEmpty()) {
  326. if (m_noteView) m_noteView->hide();
  327. return;
  328. }
  329. const QString query = QString("DIV#%1").arg(href);
  330. const QWebElement element = doc().findFirst(query);
  331. if (element.isNull()) {
  332. if (m_noteView) m_noteView->hide();
  333. return;
  334. }
  335. QRect rect = geometry();
  336. QSize size = element.geometry().size() + QSize(2, 4);
  337. int center = rect.size().height() / 2;
  338. int h = size.height();
  339. if (h > center) size.setHeight(center - 10);
  340. int x = (rect.size().width() - size.width()) / 2;
  341. int y = m_point.y();
  342. if ( y > h ) y = y - h - 10; else y = y + 10;
  343. QPoint point = QPoint(x, y) + rect.topLeft();
  344. noteView().hint(element, QRect(point, size));
  345. }
  346. void FbTextEdit::load(const QString &filename, const QString &xml)
  347. {
  348. if (m_thread) return;
  349. m_thread = new FbReadThread(this, filename, xml);
  350. m_thread->start();
  351. }
  352. bool FbTextEdit::save(QIODevice *device, const QString &codec)
  353. {
  354. FbSaveWriter writer(*this, device);
  355. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  356. bool ok = FbSaveHandler(writer).save();
  357. if (ok) page()->undoStack()->setClean();
  358. return ok;
  359. }
  360. bool FbTextEdit::save(QByteArray *array)
  361. {
  362. FbSaveWriter writer(*this, array);
  363. return FbSaveHandler(writer).save();
  364. }
  365. bool FbTextEdit::save(QString *string)
  366. {
  367. // Use class QByteArray instead QString
  368. // to store information about encoding.
  369. QByteArray data;
  370. bool ok = save(&data);
  371. if (ok) *string = QString::fromUtf8(data.data());
  372. return ok;
  373. }
  374. void FbTextEdit::data(QString name, QByteArray data)
  375. {
  376. m_files.set(name, data);
  377. }
  378. void FbTextEdit::html(QString name, QString html)
  379. {
  380. static int number = 0;
  381. setHtml(html, QUrl(QString("fb2:/%1/").arg(number++)));
  382. if (m_thread) m_thread->deleteLater();
  383. m_thread = 0;
  384. }
  385. void FbTextEdit::zoomIn()
  386. {
  387. qreal zoom = zoomFactor();
  388. setZoomFactor(zoom * 1.1);
  389. }
  390. void FbTextEdit::zoomOut()
  391. {
  392. qreal zoom = zoomFactor();
  393. setZoomFactor(zoom * 0.9);
  394. }
  395. void FbTextEdit::zoomReset()
  396. {
  397. setZoomFactor(1);
  398. }
  399. bool FbTextEdit::UndoEnabled()
  400. {
  401. return pageAction(QWebPage::Undo)->isEnabled();
  402. }
  403. bool FbTextEdit::RedoEnabled()
  404. {
  405. return pageAction(QWebPage::Redo)->isEnabled();
  406. }
  407. bool FbTextEdit::CutEnabled()
  408. {
  409. return pageAction(QWebPage::Cut)->isEnabled();
  410. }
  411. bool FbTextEdit::CopyEnabled()
  412. {
  413. return pageAction(QWebPage::Copy)->isEnabled();
  414. }
  415. bool FbTextEdit::BoldChecked()
  416. {
  417. return pageAction(QWebPage::ToggleBold)->isChecked();
  418. }
  419. bool FbTextEdit::ItalicChecked()
  420. {
  421. return pageAction(QWebPage::ToggleItalic)->isChecked();
  422. }
  423. bool FbTextEdit::StrikeChecked()
  424. {
  425. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  426. }
  427. bool FbTextEdit::SubChecked()
  428. {
  429. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  430. }
  431. bool FbTextEdit::SupChecked()
  432. {
  433. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  434. }
  435. void FbTextEdit::find()
  436. {
  437. FbTextFindDlg dlg(*this);
  438. dlg.exec();
  439. }
  440. void FbTextEdit::insertImage()
  441. {
  442. QString filters;
  443. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
  444. filters += tr("Portable Network Graphics (PNG) (*.png);;");
  445. filters += tr("JPEG (*.jpg *.jpeg);;");
  446. filters += tr("Graphics Interchange Format (*.gif);;");
  447. filters += tr("All Files (*)");
  448. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  449. if (path.isEmpty()) return;
  450. QFile file(path);
  451. if (!file.open(QIODevice::ReadOnly)) return;
  452. QByteArray data = file.readAll();
  453. QString name = m_files.add(path, data);
  454. execCommand("insertImage", name.prepend("#"));
  455. }
  456. void FbTextEdit::insertNote()
  457. {
  458. FbNoteDlg dlg(*this);
  459. dlg.exec();
  460. }
  461. void FbTextEdit::insertLink()
  462. {
  463. }
  464. void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
  465. {
  466. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  467. page()->mainFrame()->evaluateJavaScript(javascript);
  468. }
  469. void FbTextEdit::loadFinished()
  470. {
  471. FbTextElement element = body().findFirst("div.body");
  472. if (element.isNull()) element = body();
  473. element.select();
  474. }
  475. //---------------------------------------------------------------------------
  476. // FbTextFrame
  477. //---------------------------------------------------------------------------
  478. FbTextFrame::FbTextFrame(QWidget* parent)
  479. : QFrame(parent)
  480. , view(this)
  481. , dock(0)
  482. {
  483. setFrameShape(QFrame::StyledPanel);
  484. setFrameShadow(QFrame::Sunken);
  485. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  486. layout->setSpacing(0);
  487. layout->setMargin(0);
  488. layout->addWidget(&view);
  489. }
  490. FbTextFrame::~FbTextFrame()
  491. {
  492. if (dock) dock->deleteLater();
  493. }
  494. void FbTextFrame::showInspector()
  495. {
  496. if (dock) {
  497. dock->show();
  498. return;
  499. }
  500. QMainWindow * main = qobject_cast<QMainWindow*>(parent());
  501. if (!main) return;
  502. dock = new QDockWidget(tr("Web inspector"), this);
  503. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  504. main->addDockWidget(Qt::BottomDockWidgetArea, dock);
  505. QWebInspector * inspector = new QWebInspector(this);
  506. inspector->setPage(view.page());
  507. dock->setWidget(inspector);
  508. }
  509. void FbTextFrame::hideInspector()
  510. {
  511. if (dock) dock->hide();
  512. }