fb2text.cpp 12 KB

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