fb2text.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. // Fb2NoteView
  27. //---------------------------------------------------------------------------
  28. class Fb2NoteView : public QWebView
  29. {
  30. public:
  31. explicit Fb2NoteView(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. Fb2NoteView::Fb2NoteView(QWidget *parent, const QUrl &url)
  38. : QWebView(parent)
  39. , m_url(url)
  40. {
  41. }
  42. void Fb2NoteView::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 Fb2NoteView::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. // Fb2TextPage
  64. //---------------------------------------------------------------------------
  65. Fb2TextPage::Fb2TextPage(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 Fb2TextPage::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. Fb2TextElement Fb2TextPage::body()
  89. {
  90. return doc().findFirst("body");
  91. }
  92. Fb2TextElement Fb2TextPage::doc()
  93. {
  94. return mainFrame()->documentElement();
  95. }
  96. void Fb2TextPage::insertBody()
  97. {
  98. undoStack()->beginMacro("Append body");
  99. undoStack()->push(new Fb2AddBodyCmd(*this));
  100. undoStack()->endMacro();
  101. }
  102. void Fb2TextPage::update()
  103. {
  104. emit contentsChanged();
  105. emit selectionChanged();
  106. }
  107. void Fb2TextPage::insertTitle()
  108. {
  109. Fb2TextElement element = current();
  110. while (!element.isNull()) {
  111. Fb2TextElement parent = element.parent();
  112. if (parent.isSection() && !parent.hasTitle()) {
  113. undoStack()->beginMacro("Insert title");
  114. undoStack()->push(new Fb2TitleCmd(*this, parent));
  115. undoStack()->endMacro();
  116. break;
  117. }
  118. element = parent;
  119. }
  120. }
  121. void Fb2TextPage::insertSubtitle()
  122. {
  123. Fb2TextElement element = current();
  124. while (!element.isNull()) {
  125. Fb2TextElement parent = element.parent();
  126. if (parent.isSection()) {
  127. Fb2TextElement previous = element.previousSibling();
  128. if (!previous.isNull()) element = previous;
  129. undoStack()->beginMacro("Insert subtitle");
  130. undoStack()->push(new Fb2SubtitleCmd(*this, element.location()));
  131. undoStack()->endMacro();
  132. break;
  133. }
  134. element = parent;
  135. }
  136. }
  137. Fb2TextElement Fb2TextPage::current()
  138. {
  139. return element(location());
  140. }
  141. Fb2TextElement Fb2TextPage::element(const QString &location)
  142. {
  143. QStringList list = location.split(",");
  144. QStringListIterator iterator(list);
  145. QWebElement result = doc();
  146. while (iterator.hasNext()) {
  147. QString str = iterator.next();
  148. int pos = str.indexOf("=");
  149. QString tag = str.left(pos);
  150. int key = str.mid(pos + 1).toInt();
  151. if (key < 0) break;
  152. result = result.firstChild();
  153. while (0 < key--) result = result.nextSibling();
  154. }
  155. return result;
  156. }
  157. QString Fb2TextPage::location()
  158. {
  159. static const QString javascript = FB2::read(":/js/get_location.js").prepend("var element=document.getSelection().anchorNode;");
  160. return mainFrame()->evaluateJavaScript(javascript).toString();
  161. }
  162. QString Fb2TextPage::status()
  163. {
  164. static const QString javascript = FB2::read(":/js/get_status.js");
  165. return mainFrame()->evaluateJavaScript(javascript).toString();
  166. }
  167. //---------------------------------------------------------------------------
  168. // Fb2TextBase
  169. //---------------------------------------------------------------------------
  170. void Fb2TextBase::addTools(QToolBar *tool)
  171. {
  172. QAction *act;
  173. act = pageAction(QWebPage::Undo);
  174. act->setIcon(Fb2Icon("edit-undo"));
  175. act->setText(QObject::tr("&Undo"));
  176. act->setPriority(QAction::LowPriority);
  177. act->setShortcut(QKeySequence::Undo);
  178. tool->addAction(act);
  179. act = pageAction(QWebPage::Redo);
  180. act->setIcon(Fb2Icon("edit-redo"));
  181. act->setText(QObject::tr("&Redo"));
  182. act->setPriority(QAction::LowPriority);
  183. act->setShortcut(QKeySequence::Redo);
  184. tool->addAction(act);
  185. tool->addSeparator();
  186. act = pageAction(QWebPage::Cut);
  187. act->setIcon(Fb2Icon("edit-cut"));
  188. act->setText(QObject::tr("Cu&t"));
  189. act->setPriority(QAction::LowPriority);
  190. act->setShortcuts(QKeySequence::Cut);
  191. act->setStatusTip(QObject::tr("Cut the current selection's contents to the clipboard"));
  192. tool->addAction(act);
  193. act = pageAction(QWebPage::Copy);
  194. act->setIcon(Fb2Icon("edit-copy"));
  195. act->setText(QObject::tr("&Copy"));
  196. act->setPriority(QAction::LowPriority);
  197. act->setShortcuts(QKeySequence::Copy);
  198. act->setStatusTip(QObject::tr("Copy the current selection's contents to the clipboard"));
  199. tool->addAction(act);
  200. act = pageAction(QWebPage::Paste);
  201. act->setIcon(Fb2Icon("edit-paste"));
  202. act->setText(QObject::tr("&Paste"));
  203. act->setPriority(QAction::LowPriority);
  204. act->setShortcuts(QKeySequence::Paste);
  205. act->setStatusTip(QObject::tr("Paste the clipboard's contents into the current selection"));
  206. tool->addAction(act);
  207. tool->addSeparator();
  208. act = pageAction(QWebPage::ToggleBold);
  209. act->setIcon(Fb2Icon("format-text-bold"));
  210. act->setText(QObject::tr("&Bold"));
  211. tool->addAction(act);
  212. act = pageAction(QWebPage::ToggleItalic);
  213. act->setIcon(Fb2Icon("format-text-italic"));
  214. act->setText(QObject::tr("&Italic"));
  215. tool->addAction(act);
  216. act = pageAction(QWebPage::ToggleStrikethrough);
  217. act->setIcon(Fb2Icon("format-text-strikethrough"));
  218. act->setText(QObject::tr("&Strikethrough"));
  219. tool->addAction(act);
  220. act = pageAction(QWebPage::ToggleSuperscript);
  221. act->setIcon(Fb2Icon("format-text-superscript"));
  222. act->setText(QObject::tr("Su&perscript"));
  223. tool->addAction(act);
  224. act = pageAction(QWebPage::ToggleSubscript);
  225. act->setIcon(Fb2Icon("format-text-subscript"));
  226. act->setText(QObject::tr("Su&bscript"));
  227. tool->addAction(act);
  228. }
  229. //---------------------------------------------------------------------------
  230. // Fb2TextEdit
  231. //---------------------------------------------------------------------------
  232. Fb2TextEdit::Fb2TextEdit(QWidget *parent)
  233. : Fb2TextBase(parent)
  234. , m_noteView(0)
  235. , m_thread(0)
  236. {
  237. setPage(new Fb2TextPage(this));
  238. page()->setNetworkAccessManager(new Fb2NetworkAccessManager(*this));
  239. page()->setContentEditable(true);
  240. connect(page(), SIGNAL(contentsChanged()), this, SLOT(fixContents()));
  241. connect(page(), SIGNAL(linkHovered(QString,QString,QString)), this, SLOT(linkHovered(QString,QString,QString)));
  242. connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  243. }
  244. Fb2TextEdit::~Fb2TextEdit()
  245. {
  246. if (m_noteView) delete m_noteView;
  247. }
  248. Fb2TextPage * Fb2TextEdit::page()
  249. {
  250. return qobject_cast<Fb2TextPage*>(Fb2TextBase::page());
  251. }
  252. Fb2NoteView & Fb2TextEdit::noteView()
  253. {
  254. if (m_noteView) return *m_noteView;
  255. m_noteView = new Fb2NoteView(qobject_cast<QWidget*>(parent()), url());
  256. m_noteView->setPage(new Fb2TextPage(this));
  257. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  258. m_noteView->page()->setContentEditable(false);
  259. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  260. return *m_noteView;
  261. }
  262. QWebElement Fb2TextEdit::body()
  263. {
  264. return doc().findFirst("body");
  265. }
  266. QWebElement Fb2TextEdit::doc()
  267. {
  268. return page()->mainFrame()->documentElement();
  269. }
  270. void Fb2TextEdit::fixContents()
  271. {
  272. foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
  273. span.removeAttribute("style");
  274. }
  275. }
  276. void Fb2TextEdit::mouseMoveEvent(QMouseEvent *event)
  277. {
  278. m_point = event->pos();
  279. QWebView::mouseMoveEvent(event);
  280. }
  281. void Fb2TextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  282. {
  283. Q_UNUSED(title);
  284. Q_UNUSED(textContent);
  285. const QString href = QUrl(link).fragment();
  286. if (href.isEmpty()) {
  287. if (m_noteView) m_noteView->hide();
  288. return;
  289. }
  290. const QString query = QString("DIV#%1").arg(href);
  291. const QWebElement element = doc().findFirst(query);
  292. if (element.isNull()) {
  293. if (m_noteView) m_noteView->hide();
  294. return;
  295. }
  296. QRect rect = geometry();
  297. QSize size = element.geometry().size() + QSize(2, 4);
  298. int center = rect.size().height() / 2;
  299. int h = size.height();
  300. if (h > center) size.setHeight(center - 10);
  301. int x = (rect.size().width() - size.width()) / 2;
  302. int y = m_point.y();
  303. if ( y > h ) y = y - h - 10; else y = y + 10;
  304. QPoint point = QPoint(x, y) + rect.topLeft();
  305. noteView().hint(element, QRect(point, size));
  306. }
  307. void Fb2TextEdit::load(const QString &filename, const QString &xml)
  308. {
  309. if (m_thread) return;
  310. m_thread = new Fb2ReadThread(this, filename, xml);
  311. m_thread->start();
  312. }
  313. bool Fb2TextEdit::save(QIODevice *device, const QString &codec)
  314. {
  315. Fb2SaveWriter writer(*this, device);
  316. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  317. bool ok = Fb2SaveHandler(writer).save();
  318. if (ok) page()->undoStack()->setClean();
  319. return ok;
  320. }
  321. bool Fb2TextEdit::save(QByteArray *array)
  322. {
  323. Fb2SaveWriter writer(*this, array);
  324. return Fb2SaveHandler(writer).save();
  325. }
  326. bool Fb2TextEdit::save(QString *string)
  327. {
  328. // Use class QByteArray instead QString
  329. // to store information about encoding.
  330. QByteArray data;
  331. bool ok = save(&data);
  332. if (ok) *string = QString::fromUtf8(data.data());
  333. return ok;
  334. }
  335. void Fb2TextEdit::data(QString name, QByteArray data)
  336. {
  337. m_files.set(name, data);
  338. }
  339. void Fb2TextEdit::html(QString name, QString html)
  340. {
  341. static int number = 0;
  342. setHtml(html, QUrl(QString("fb2:/%1/").arg(number++)));
  343. if (m_thread) m_thread->deleteLater();
  344. m_thread = 0;
  345. }
  346. void Fb2TextEdit::zoomIn()
  347. {
  348. qreal zoom = zoomFactor();
  349. setZoomFactor(zoom * 1.1);
  350. }
  351. void Fb2TextEdit::zoomOut()
  352. {
  353. qreal zoom = zoomFactor();
  354. setZoomFactor(zoom * 0.9);
  355. }
  356. void Fb2TextEdit::zoomReset()
  357. {
  358. setZoomFactor(1);
  359. }
  360. bool Fb2TextEdit::UndoEnabled()
  361. {
  362. return pageAction(QWebPage::Undo)->isEnabled();
  363. }
  364. bool Fb2TextEdit::RedoEnabled()
  365. {
  366. return pageAction(QWebPage::Redo)->isEnabled();
  367. }
  368. bool Fb2TextEdit::CutEnabled()
  369. {
  370. return pageAction(QWebPage::Cut)->isEnabled();
  371. }
  372. bool Fb2TextEdit::CopyEnabled()
  373. {
  374. return pageAction(QWebPage::Copy)->isEnabled();
  375. }
  376. bool Fb2TextEdit::BoldChecked()
  377. {
  378. return pageAction(QWebPage::ToggleBold)->isChecked();
  379. }
  380. bool Fb2TextEdit::ItalicChecked()
  381. {
  382. return pageAction(QWebPage::ToggleItalic)->isChecked();
  383. }
  384. bool Fb2TextEdit::StrikeChecked()
  385. {
  386. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  387. }
  388. bool Fb2TextEdit::SubChecked()
  389. {
  390. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  391. }
  392. bool Fb2TextEdit::SupChecked()
  393. {
  394. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  395. }
  396. void Fb2TextEdit::find()
  397. {
  398. Fb2TextFindDlg dlg(*this);
  399. dlg.exec();
  400. }
  401. void Fb2TextEdit::insertImage()
  402. {
  403. QString filters;
  404. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
  405. filters += tr("Portable Network Graphics (PNG) (*.png);;");
  406. filters += tr("JPEG (*.jpg *.jpeg);;");
  407. filters += tr("Graphics Interchange Format (*.gif);;");
  408. filters += tr("All Files (*)");
  409. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  410. if (path.isEmpty()) return;
  411. QFile file(path);
  412. if (!file.open(QIODevice::ReadOnly)) return;
  413. QByteArray data = file.readAll();
  414. QString name = m_files.add(path, data);
  415. execCommand("insertImage", name.prepend("#"));
  416. }
  417. void Fb2TextEdit::insertNote()
  418. {
  419. Fb2NoteDlg dlg(*this);
  420. dlg.exec();
  421. }
  422. void Fb2TextEdit::insertLink()
  423. {
  424. }
  425. void Fb2TextEdit::execCommand(const QString &cmd, const QString &arg)
  426. {
  427. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  428. page()->mainFrame()->evaluateJavaScript(javascript);
  429. }
  430. void Fb2TextEdit::loadFinished()
  431. {
  432. Fb2TextElement element = body().findFirst("div.body");
  433. if (element.isNull()) element = body();
  434. element.select();
  435. }
  436. //---------------------------------------------------------------------------
  437. // Fb2TextFrame
  438. //---------------------------------------------------------------------------
  439. Fb2TextFrame::Fb2TextFrame(QWidget* parent)
  440. : QFrame(parent)
  441. , view(this)
  442. , dock(0)
  443. {
  444. setFrameShape(QFrame::StyledPanel);
  445. setFrameShadow(QFrame::Sunken);
  446. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  447. layout->setSpacing(0);
  448. layout->setMargin(0);
  449. layout->addWidget(&view);
  450. }
  451. Fb2TextFrame::~Fb2TextFrame()
  452. {
  453. if (dock) dock->deleteLater();
  454. }
  455. void Fb2TextFrame::showInspector()
  456. {
  457. if (dock) {
  458. dock->show();
  459. return;
  460. }
  461. QMainWindow * main = qobject_cast<QMainWindow*>(parent());
  462. if (!main) return;
  463. dock = new QDockWidget(tr("Web inspector"), this);
  464. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  465. main->addDockWidget(Qt::BottomDockWidgetArea, dock);
  466. QWebInspector * inspector = new QWebInspector(this);
  467. inspector->setPage(view.page());
  468. dock->setWidget(inspector);
  469. }
  470. void Fb2TextFrame::hideInspector()
  471. {
  472. if (dock) dock->hide();
  473. }