fb2view.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #include "fb2view.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 <QToolTip>
  17. #include <QUndoCommand>
  18. #include <QUndoStack>
  19. #include <QWebElement>
  20. #include <QWebInspector>
  21. #include <QWebFrame>
  22. #include <QWebPage>
  23. #include <QtDebug>
  24. //---------------------------------------------------------------------------
  25. // Fb2NoteView
  26. //---------------------------------------------------------------------------
  27. class Fb2NoteView : public QWebView
  28. {
  29. public:
  30. explicit Fb2NoteView(QWidget *parent, const QUrl &url);
  31. void hint(const QWebElement element, const QRect &rect);
  32. protected:
  33. void paintEvent(QPaintEvent *event);
  34. const QUrl m_url;
  35. };
  36. Fb2NoteView::Fb2NoteView(QWidget *parent, const QUrl &url)
  37. : QWebView(parent)
  38. , m_url(url)
  39. {
  40. }
  41. void Fb2NoteView::paintEvent(QPaintEvent *event)
  42. {
  43. QWebView::paintEvent(event);
  44. QPainter painter(this);
  45. painter.setPen(Qt::black);
  46. QSize size = geometry().size() - QSize(1, 1);
  47. painter.drawRect( QRect(QPoint(0, 0), size) );
  48. }
  49. void Fb2NoteView::hint(const QWebElement element, const QRect &rect)
  50. {
  51. QString html = element.toOuterXml();
  52. html.prepend(
  53. "<body bgcolor=lightyellow style='overflow:hidden;padding:0;margin:0;margin-top:2;'>"
  54. "<div class=body fb2_name=notes style='padding:0;margin:0;'>"
  55. );
  56. html.append("</div></body>");
  57. setGeometry(rect);
  58. setHtml(html, m_url);
  59. show();
  60. }
  61. //---------------------------------------------------------------------------
  62. // Fb2TextPage
  63. //---------------------------------------------------------------------------
  64. Fb2TextPage::Fb2TextPage(QObject *parent)
  65. : QWebPage(parent)
  66. {
  67. QWebSettings *s = settings();
  68. s->setAttribute(QWebSettings::AutoLoadImages, true);
  69. s->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
  70. s->setAttribute(QWebSettings::JavaEnabled, false);
  71. s->setAttribute(QWebSettings::JavascriptEnabled, true);
  72. s->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
  73. s->setAttribute(QWebSettings::PluginsEnabled, false);
  74. s->setAttribute(QWebSettings::ZoomTextOnly, true);
  75. s->setUserStyleSheetUrl(QUrl::fromLocalFile(":style.css"));
  76. }
  77. bool Fb2TextPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type)
  78. {
  79. Q_UNUSED(frame);
  80. if (type == NavigationTypeLinkClicked) {
  81. qCritical() << request.url().fragment();
  82. return false;
  83. // QToolTip::showText(request.url().fragment());
  84. }
  85. return QWebPage::acceptNavigationRequest(frame, request, type);
  86. }
  87. Fb2TextElement Fb2TextPage::body()
  88. {
  89. return doc().findFirst("body");
  90. }
  91. Fb2TextElement Fb2TextPage::doc()
  92. {
  93. return mainFrame()->documentElement();
  94. }
  95. class Fb2InsertBodyCommand : public QUndoCommand
  96. {
  97. public:
  98. explicit Fb2InsertBodyCommand(Fb2TextPage &page, QUndoCommand *parent = 0) : QUndoCommand(parent), m_page(page) {}
  99. virtual void undo();
  100. virtual void redo();
  101. private:
  102. Fb2TextPage & m_page;
  103. };
  104. void Fb2InsertBodyCommand::undo()
  105. {
  106. m_page.body().lastChild().removeFromDocument();
  107. Fb2TextElement(m_page.body().lastChild()).select();
  108. }
  109. void Fb2InsertBodyCommand::redo()
  110. {
  111. m_page.body().appendInside("<div class=body><div class=section><p>text</p></div></div>");
  112. Fb2TextElement(m_page.body().lastChild()).select();
  113. }
  114. void Fb2TextPage::insertBody()
  115. {
  116. undoStack()->beginMacro("Insert title");
  117. undoStack()->push(new Fb2InsertBodyCommand(*this));
  118. undoStack()->endMacro();
  119. emit contentsChanged();
  120. }
  121. class Fb2InsertSubtitleCmd : public QUndoCommand
  122. {
  123. public:
  124. explicit Fb2InsertSubtitleCmd(Fb2TextPage &page, const QString &location, QUndoCommand *parent = 0)
  125. : QUndoCommand(parent), m_page(page), m_location(location) {}
  126. virtual void undo();
  127. virtual void redo();
  128. private:
  129. Fb2TextElement m_element;
  130. Fb2TextPage & m_page;
  131. QString m_location;
  132. QString m_position;
  133. };
  134. void Fb2InsertSubtitleCmd::redo()
  135. {
  136. QString html = "<div class=subtitle><p><br/></p></div>";
  137. Fb2TextElement element = m_page.element(m_location);
  138. if (m_element.isNull()) {
  139. element.appendOutside(html);
  140. } else {
  141. element.appendOutside(m_element);
  142. }
  143. element = element.nextSibling();
  144. m_position = element.location();
  145. element.select();
  146. QMetaObject::invokeMethod(&m_page, "selectionChanged");
  147. QMetaObject::invokeMethod(&m_page, "contentsChanged");
  148. }
  149. void Fb2InsertSubtitleCmd::undo()
  150. {
  151. Fb2TextElement element = m_page.element(m_position);
  152. Fb2TextElement parent = element.parent();
  153. m_element = element.takeFromDocument();
  154. parent.select();
  155. QMetaObject::invokeMethod(&m_page, "selectionChanged");
  156. QMetaObject::invokeMethod(&m_page, "contentsChanged");
  157. }
  158. void Fb2TextPage::insertSubtitle()
  159. {
  160. Fb2TextElement element = current();
  161. while (!element.isNull()) {
  162. Fb2TextElement parent = element.parent();
  163. if (parent.isSection()) {
  164. Fb2TextElement previous = element.previousSibling();
  165. if (!previous.isNull()) element = previous;
  166. undoStack()->beginMacro("Insert subtitle");
  167. undoStack()->push(new Fb2InsertSubtitleCmd(*this, element.location()));
  168. undoStack()->endMacro();
  169. emit contentsChanged();
  170. break;
  171. }
  172. element = parent;
  173. }
  174. }
  175. Fb2TextElement Fb2TextPage::current()
  176. {
  177. return element(location());
  178. }
  179. Fb2TextElement Fb2TextPage::element(const QString &location)
  180. {
  181. QStringList list = location.split(",");
  182. QStringListIterator iterator(list);
  183. QWebElement result = doc();
  184. while (iterator.hasNext()) {
  185. QString str = iterator.next();
  186. int pos = str.indexOf("=");
  187. QString tag = str.left(pos);
  188. int key = str.mid(pos + 1).toInt();
  189. if (key < 0) break;
  190. result = result.firstChild();
  191. while (0 < key--) result = result.nextSibling();
  192. if (tag == "P") break;
  193. }
  194. return result;
  195. }
  196. QString Fb2TextPage::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 Fb2TextPage::status()
  202. {
  203. static const QString javascript = FB2::read(":/js/get_status.js");
  204. return mainFrame()->evaluateJavaScript(javascript).toString();
  205. }
  206. //---------------------------------------------------------------------------
  207. // Fb2TextEdit
  208. //---------------------------------------------------------------------------
  209. Fb2TextEdit::Fb2TextEdit(QWidget *parent)
  210. : Fb2TextBase(parent)
  211. , m_noteView(0)
  212. , m_thread(0)
  213. {
  214. setPage(new Fb2TextPage(this));
  215. page()->setNetworkAccessManager(new Fb2NetworkAccessManager(*this));
  216. page()->setContentEditable(true);
  217. connect(page(), SIGNAL(contentsChanged()), this, SLOT(fixContents()));
  218. connect(page(), SIGNAL(linkHovered(QString,QString,QString)), this, SLOT(linkHovered(QString,QString,QString)));
  219. connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  220. }
  221. Fb2TextEdit::~Fb2TextEdit()
  222. {
  223. FB2DELETE(m_noteView);
  224. }
  225. Fb2TextPage * Fb2TextEdit::page()
  226. {
  227. return qobject_cast<Fb2TextPage*>(Fb2TextBase::page());
  228. }
  229. Fb2NoteView & Fb2TextEdit::noteView()
  230. {
  231. if (m_noteView) return *m_noteView;
  232. m_noteView = new Fb2NoteView(qobject_cast<QWidget*>(parent()), url());
  233. m_noteView->setPage(new Fb2TextPage(this));
  234. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  235. m_noteView->page()->setContentEditable(false);
  236. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  237. return *m_noteView;
  238. }
  239. QWebElement Fb2TextEdit::body()
  240. {
  241. return doc().findFirst("body");
  242. }
  243. QWebElement Fb2TextEdit::doc()
  244. {
  245. return page()->mainFrame()->documentElement();
  246. }
  247. void Fb2TextEdit::fixContents()
  248. {
  249. foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
  250. span.removeAttribute("style");
  251. }
  252. }
  253. void Fb2TextEdit::mouseMoveEvent(QMouseEvent *event)
  254. {
  255. m_point = event->pos();
  256. QWebView::mouseMoveEvent(event);
  257. }
  258. void Fb2TextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  259. {
  260. Q_UNUSED(title);
  261. Q_UNUSED(textContent);
  262. const QString href = QUrl(link).fragment();
  263. if (href.isEmpty()) {
  264. if (m_noteView) m_noteView->hide();
  265. return;
  266. }
  267. const QString query = QString("DIV#%1").arg(href);
  268. const QWebElement element = doc().findFirst(query);
  269. if (element.isNull()) {
  270. if (m_noteView) m_noteView->hide();
  271. return;
  272. }
  273. QRect rect = geometry();
  274. QSize size = element.geometry().size() + QSize(2, 4);
  275. int center = rect.size().height() / 2;
  276. int h = size.height();
  277. if (h > center) size.setHeight(center - 10);
  278. int x = (rect.size().width() - size.width()) / 2;
  279. int y = m_point.y();
  280. if ( y > h ) y = y - h - 10; else y = y + 10;
  281. QPoint point = QPoint(x, y) + rect.topLeft();
  282. noteView().hint(element, QRect(point, size));
  283. }
  284. void Fb2TextEdit::load(const QString &filename, const QString &xml)
  285. {
  286. if (m_thread) return;
  287. m_thread = new Fb2ReadThread(this, filename, xml);
  288. m_thread->start();
  289. }
  290. bool Fb2TextEdit::save(QIODevice *device, const QString &codec)
  291. {
  292. Fb2SaveWriter writer(*this, device);
  293. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  294. bool ok = Fb2SaveHandler(writer).save();
  295. if (ok) page()->undoStack()->setClean();
  296. return ok;
  297. }
  298. bool Fb2TextEdit::save(QByteArray *array)
  299. {
  300. Fb2SaveWriter writer(*this, array);
  301. return Fb2SaveHandler(writer).save();
  302. }
  303. bool Fb2TextEdit::save(QString *string)
  304. {
  305. // Use class QByteArray instead QString
  306. // to store information about encoding.
  307. QByteArray data;
  308. bool ok = save(&data);
  309. if (ok) *string = QString::fromUtf8(data.data());
  310. return ok;
  311. }
  312. void Fb2TextEdit::data(QString name, QByteArray data)
  313. {
  314. m_files.set(name, data);
  315. }
  316. void Fb2TextEdit::html(QString name, QString html)
  317. {
  318. static int number = 0;
  319. setHtml(html, QUrl(QString("fb2:/%1/").arg(number++)));
  320. if (m_thread) m_thread->deleteLater();
  321. m_thread = 0;
  322. }
  323. void Fb2TextEdit::zoomIn()
  324. {
  325. qreal zoom = zoomFactor();
  326. setZoomFactor(zoom * 1.1);
  327. }
  328. void Fb2TextEdit::zoomOut()
  329. {
  330. qreal zoom = zoomFactor();
  331. setZoomFactor(zoom * 0.9);
  332. }
  333. void Fb2TextEdit::zoomReset()
  334. {
  335. setZoomFactor(1);
  336. }
  337. bool Fb2TextEdit::UndoEnabled()
  338. {
  339. return pageAction(QWebPage::Undo)->isEnabled();
  340. }
  341. bool Fb2TextEdit::RedoEnabled()
  342. {
  343. return pageAction(QWebPage::Redo)->isEnabled();
  344. }
  345. bool Fb2TextEdit::CutEnabled()
  346. {
  347. return pageAction(QWebPage::Cut)->isEnabled();
  348. }
  349. bool Fb2TextEdit::CopyEnabled()
  350. {
  351. return pageAction(QWebPage::Copy)->isEnabled();
  352. }
  353. bool Fb2TextEdit::BoldChecked()
  354. {
  355. return pageAction(QWebPage::ToggleBold)->isChecked();
  356. }
  357. bool Fb2TextEdit::ItalicChecked()
  358. {
  359. return pageAction(QWebPage::ToggleItalic)->isChecked();
  360. }
  361. bool Fb2TextEdit::StrikeChecked()
  362. {
  363. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  364. }
  365. bool Fb2TextEdit::SubChecked()
  366. {
  367. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  368. }
  369. bool Fb2TextEdit::SupChecked()
  370. {
  371. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  372. }
  373. void Fb2TextEdit::find()
  374. {
  375. Fb2TextFindDlg dlg(*this);
  376. dlg.exec();
  377. }
  378. void Fb2TextEdit::insertImage()
  379. {
  380. QString filters;
  381. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
  382. filters += tr("Portable Network Graphics (PNG) (*.png);;");
  383. filters += tr("JPEG (*.jpg *.jpeg);;");
  384. filters += tr("Graphics Interchange Format (*.gif);;");
  385. filters += tr("All Files (*)");
  386. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  387. if (path.isEmpty()) return;
  388. QFile file(path);
  389. if (!file.open(QIODevice::ReadOnly)) return;
  390. QByteArray data = file.readAll();
  391. QString name = m_files.add(path, data);
  392. execCommand("insertImage", name.prepend("#"));
  393. }
  394. void Fb2TextEdit::insertNote()
  395. {
  396. Fb2NoteDlg dlg(*this);
  397. dlg.exec();
  398. }
  399. void Fb2TextEdit::insertLink()
  400. {
  401. }
  402. void Fb2TextEdit::execCommand(const QString &cmd, const QString &arg)
  403. {
  404. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  405. page()->mainFrame()->evaluateJavaScript(javascript);
  406. }
  407. void Fb2TextEdit::loadFinished()
  408. {
  409. Fb2TextElement element = body().findFirst("div.body");
  410. if (element.isNull()) element = body();
  411. element.select();
  412. }
  413. void Fb2TextEdit::insertTitle()
  414. {
  415. page()->undoStack()->beginMacro("Insert title");
  416. static const QString javascript = FB2::read(":/js/insert_title.js");
  417. page()->mainFrame()->evaluateJavaScript(javascript);
  418. page()->undoStack()->endMacro();
  419. }
  420. //---------------------------------------------------------------------------
  421. // Fb2TextFrame
  422. //---------------------------------------------------------------------------
  423. Fb2TextFrame::Fb2TextFrame(QWidget* parent)
  424. : QFrame(parent)
  425. , view(this)
  426. , dock(0)
  427. {
  428. setFrameShape(QFrame::StyledPanel);
  429. setFrameShadow(QFrame::Sunken);
  430. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  431. layout->setSpacing(0);
  432. layout->setMargin(0);
  433. layout->addWidget(&view);
  434. }
  435. Fb2TextFrame::~Fb2TextFrame()
  436. {
  437. if (dock) dock->deleteLater();
  438. }
  439. void Fb2TextFrame::showInspector()
  440. {
  441. if (dock) {
  442. dock->show();
  443. return;
  444. }
  445. QMainWindow * main = qobject_cast<QMainWindow*>(parent());
  446. if (!main) return;
  447. dock = new QDockWidget(tr("Web inspector"), this);
  448. dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  449. main->addDockWidget(Qt::BottomDockWidgetArea, dock);
  450. QWebInspector * inspector = new QWebInspector(this);
  451. inspector->setPage(view.page());
  452. dock->setWidget(inspector);
  453. }
  454. void Fb2TextFrame::hideInspector()
  455. {
  456. if (dock) dock->hide();
  457. }