fb2text.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 <QMenu>
  15. #include <QNetworkRequest>
  16. #include <QStyle>
  17. #include <QStyleOptionFrame>
  18. #include <QToolBar>
  19. #include <QToolTip>
  20. #include <QUndoCommand>
  21. #include <QUndoStack>
  22. #include <QWebElement>
  23. #include <QWebInspector>
  24. #include <QWebFrame>
  25. #include <QWebPage>
  26. #include <QtDebug>
  27. //---------------------------------------------------------------------------
  28. // FbTextLogger
  29. //---------------------------------------------------------------------------
  30. void FbTextLogger::trace(const QString &text)
  31. {
  32. qCritical() << text;
  33. }
  34. //---------------------------------------------------------------------------
  35. // FbNoteView
  36. //---------------------------------------------------------------------------
  37. class FbNoteView : public QWebView
  38. {
  39. public:
  40. explicit FbNoteView(QWidget *parent, const QUrl &url);
  41. void hint(const QWebElement element, const QRect &rect);
  42. protected:
  43. void paintEvent(QPaintEvent *event);
  44. const QUrl m_url;
  45. };
  46. FbNoteView::FbNoteView(QWidget *parent, const QUrl &url)
  47. : QWebView(parent)
  48. , m_url(url)
  49. {
  50. }
  51. void FbNoteView::paintEvent(QPaintEvent *event)
  52. {
  53. QWebView::paintEvent(event);
  54. QPainter painter(this);
  55. painter.setPen(Qt::black);
  56. QSize size = geometry().size() - QSize(1, 1);
  57. painter.drawRect( QRect(QPoint(0, 0), size) );
  58. }
  59. void FbNoteView::hint(const QWebElement element, const QRect &rect)
  60. {
  61. QString html = element.toOuterXml();
  62. html.prepend(
  63. "<body bgcolor=lightyellow style='overflow:hidden;padding:0;margin:0;margin-top:2;'>"
  64. "<fb:body name=notes style='padding:0;margin:0;'>"
  65. );
  66. html.append("</fb:body></body>");
  67. setGeometry(rect);
  68. setHtml(html, m_url);
  69. show();
  70. }
  71. //---------------------------------------------------------------------------
  72. // FbTextPage
  73. //---------------------------------------------------------------------------
  74. FbTextPage::FbTextPage(QObject *parent)
  75. : QWebPage(parent)
  76. , m_logger(this)
  77. {
  78. QWebSettings *s = settings();
  79. s->setAttribute(QWebSettings::AutoLoadImages, true);
  80. s->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
  81. s->setAttribute(QWebSettings::JavaEnabled, false);
  82. s->setAttribute(QWebSettings::JavascriptEnabled, true);
  83. s->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
  84. s->setAttribute(QWebSettings::PluginsEnabled, false);
  85. s->setAttribute(QWebSettings::ZoomTextOnly, true);
  86. s->setUserStyleSheetUrl(QUrl::fromLocalFile(":style.css"));
  87. setContentEditable(true);
  88. setNetworkAccessManager(new FbNetworkAccessManager(this));
  89. connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  90. connect(this, SIGNAL(contentsChanged()), SLOT(fixContents()));
  91. }
  92. FbNetworkAccessManager *FbTextPage::temp()
  93. {
  94. return qobject_cast<FbNetworkAccessManager*>(networkAccessManager());
  95. }
  96. void FbTextPage::binary(const QString &name, const QByteArray &data)
  97. {
  98. if (FbNetworkAccessManager *t = temp()) t->data(name, data);
  99. }
  100. bool FbTextPage::load(const QString &filename, const QString &xml)
  101. {
  102. QXmlInputSource source;
  103. if (xml.isEmpty()) {
  104. QFile file(filename);
  105. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  106. qCritical() << QObject::tr("Cannot read file %1: %2.").arg(filename).arg(file.errorString());
  107. return false;
  108. }
  109. source.setData(file.readAll());
  110. } else {
  111. source.setData(xml);
  112. }
  113. bool ok = FbReadHandler::load(this, source, m_html);
  114. if (ok) QTimer::singleShot(1000, this, SLOT(onTimer()));
  115. return ok;
  116. }
  117. void FbTextPage::onTimer()
  118. {
  119. static int number = 0;
  120. QUrl url(QString("fb2:/%1/").arg(number++));
  121. temp()->setPath(url.path());
  122. mainFrame()->setHtml(m_html, url);
  123. }
  124. void FbTextPage::html(const QString &html, const QUrl &url)
  125. {
  126. mainFrame()->setHtml(html, url);
  127. temp()->setPath(url.path());
  128. }
  129. bool FbTextPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type)
  130. {
  131. Q_UNUSED(frame);
  132. if (type == NavigationTypeLinkClicked) {
  133. qCritical() << request.url().fragment();
  134. return false;
  135. }
  136. return QWebPage::acceptNavigationRequest(frame, request, type);
  137. }
  138. QString FbTextPage::block(const QString &name)
  139. {
  140. return block(name, p());
  141. }
  142. QString FbTextPage::block(const QString &name, const QString &text)
  143. {
  144. return QString("<fb:%1>%2</fb:%1>").arg(name).arg(text);
  145. }
  146. QString FbTextPage::p(const QString &text)
  147. {
  148. return QString("<p>%1</p>").arg(text);
  149. }
  150. FbTextElement FbTextPage::body()
  151. {
  152. return doc().findFirst("body");
  153. }
  154. FbTextElement FbTextPage::doc()
  155. {
  156. return mainFrame()->documentElement();
  157. }
  158. void FbTextPage::push(QUndoCommand * command, const QString &text)
  159. {
  160. undoStack()->beginMacro(text);
  161. undoStack()->push(command);
  162. undoStack()->endMacro();
  163. }
  164. void FbTextPage::update()
  165. {
  166. emit contentsChanged();
  167. emit selectionChanged();
  168. }
  169. FbTextElement FbTextPage::appendSection(const FbTextElement &parent)
  170. {
  171. QString html = block("section", block("title", p()) + p());
  172. FbTextElement element = parent;
  173. element.appendInside(html);
  174. element = parent.lastChild();
  175. QUndoCommand * command = new FbInsertCmd(element);
  176. push(command, tr("Append section"));
  177. return element;
  178. }
  179. FbTextElement FbTextPage::appendTitle(const FbTextElement &parent)
  180. {
  181. QString html = block("title", p());
  182. FbTextElement element = parent;
  183. element.prependInside(html);
  184. element = parent.firstChild();
  185. QUndoCommand * command = new FbInsertCmd(element);
  186. push(command, tr("Append section"));
  187. return element;
  188. }
  189. void FbTextPage::insertBody()
  190. {
  191. QString html = block("body", block("title", p()) + block("section", block("title", p()) + p()));
  192. FbTextElement element = body();
  193. element.appendInside(html);
  194. element = element.lastChild();
  195. QUndoCommand * command = new FbInsertCmd(element);
  196. push(command, tr("Append body"));
  197. }
  198. void FbTextPage::insertSection()
  199. {
  200. FbTextElement element = current();
  201. while (!element.isNull()) {
  202. if (element.isSection() || element.isBody()) {
  203. appendSection(element);
  204. break;
  205. }
  206. element = element.parent();
  207. }
  208. }
  209. void FbTextPage::insertTitle()
  210. {
  211. FbTextElement element = current();
  212. while (!element.isNull()) {
  213. FbTextElement parent = element.parent();
  214. if ((parent.isSection() || parent.isBody()) && !parent.hasTitle()) {
  215. QString html = block("title", p());
  216. parent.prependInside(html);
  217. element = parent.firstChild();
  218. QUndoCommand * command = new FbInsertCmd(element);
  219. push(command, tr("Insert title"));
  220. break;
  221. }
  222. element = parent;
  223. }
  224. }
  225. void FbTextPage::insertSubtitle()
  226. {
  227. FbTextElement element = current();
  228. while (!element.isNull()) {
  229. FbTextElement parent = element.parent();
  230. if (parent.isSection()) {
  231. QString html = block("subtitle", p());
  232. if (element.isTitle()) {
  233. element.appendOutside(html);
  234. element = element.nextSibling();
  235. } else {
  236. element.prependOutside(html);
  237. element = element.previousSibling();
  238. }
  239. QUndoCommand * command = new FbInsertCmd(element);
  240. push(command, tr("Insert subtitle"));
  241. break;
  242. }
  243. element = parent;
  244. }
  245. }
  246. void FbTextPage::insertPoem()
  247. {
  248. FbTextElement element = current();
  249. while (!element.isNull()) {
  250. FbTextElement parent = element.parent();
  251. if (parent.isSection()) {
  252. QString html = block("poem", block("stanza", p()));
  253. if (element.isTitle()) {
  254. element.appendOutside(html);
  255. element = element.nextSibling();
  256. } else {
  257. element.prependOutside(html);
  258. element = element.previousSibling();
  259. }
  260. QUndoCommand * command = new FbInsertCmd(element);
  261. push(command, tr("Insert poem"));
  262. break;
  263. }
  264. element = parent;
  265. }
  266. }
  267. void FbTextPage::insertStanza()
  268. {
  269. FbTextElement element = current();
  270. while (!element.isNull()) {
  271. if (element.isStanza()) {
  272. QString html = block("stanza", p());
  273. element.appendOutside(html);
  274. element = element.nextSibling();
  275. QUndoCommand * command = new FbInsertCmd(element);
  276. push(command, tr("Append stanza"));
  277. break;
  278. }
  279. element = element.parent();
  280. }
  281. }
  282. void FbTextPage::insertAnnot()
  283. {
  284. }
  285. void FbTextPage::insertAuthor()
  286. {
  287. }
  288. void FbTextPage::insertEpigraph()
  289. {
  290. const QString type = "epigraph";
  291. FbTextElement element = current();
  292. while (!element.isNull()) {
  293. if (element.hasSubtype(type)) {
  294. QString html = block("epigraph", p());
  295. element = element.insertInside(type, html);
  296. QUndoCommand * command = new FbInsertCmd(element);
  297. push(command, tr("Insert epigraph"));
  298. break;
  299. }
  300. element = element.parent();
  301. }
  302. }
  303. void FbTextPage::insertDate()
  304. {
  305. }
  306. void FbTextPage::insertText()
  307. {
  308. }
  309. void FbTextPage::createBlock(const QString &name)
  310. {
  311. QString style = name;
  312. QString js1 = jScript("section_get.js");
  313. QString result = mainFrame()->evaluateJavaScript(js1).toString();
  314. QStringList list = result.split("|");
  315. if (list.count() < 2) return;
  316. const QString location = list[0];
  317. const QString position = list[1];
  318. if (style == "title" && position.left(2) != "0,") style.prepend("sub");
  319. FbTextElement original = element(location);
  320. FbTextElement duplicate = original.clone();
  321. original.appendOutside(duplicate);
  322. original.takeFromDocument();
  323. QString js2 = jScript("section_new.js") + ";f(this,'fb:%1',%2)";
  324. duplicate.evaluateJavaScript(js2.arg(style).arg(position));
  325. QUndoCommand * command = new FbReplaceCmd(original, duplicate);
  326. push(command, tr("Create <%1>").arg(style));
  327. }
  328. void FbTextPage::createSection()
  329. {
  330. createBlock("section");
  331. }
  332. void FbTextPage::deleteSection()
  333. {
  334. FbTextElement element = current();
  335. while (!element.isNull()) {
  336. if (element.isSection()) {
  337. if (element.parent().isBody()) return;
  338. FbTextElement original = element.parent();
  339. FbTextElement duplicate = original.clone();
  340. int index = element.index();
  341. original.appendOutside(duplicate);
  342. original.takeFromDocument();
  343. element = duplicate.child(index);
  344. if (index) {
  345. FbTextElement title = element.firstChild();
  346. if (title.isTitle()) {
  347. title.removeClass("title");
  348. title.addClass("subtitle");
  349. }
  350. }
  351. QString xml = element.toInnerXml();
  352. element.setOuterXml(xml);
  353. QUndoCommand * command = new FbReplaceCmd(original, duplicate);
  354. push(command, tr("Remove section"));
  355. element.select();
  356. break;
  357. }
  358. element = element.parent();
  359. }
  360. }
  361. void FbTextPage::createTitle()
  362. {
  363. createBlock("title");
  364. }
  365. FbTextElement FbTextPage::current()
  366. {
  367. return element(location());
  368. }
  369. FbTextElement FbTextPage::element(const QString &location)
  370. {
  371. if (location.isEmpty()) return FbTextElement();
  372. QStringList list = location.split(",", QString::SkipEmptyParts);
  373. QStringListIterator iterator(list);
  374. QWebElement result = doc();
  375. while (iterator.hasNext()) {
  376. QString str = iterator.next();
  377. int pos = str.indexOf("=");
  378. QString tag = str.left(pos);
  379. int key = str.mid(pos + 1).toInt();
  380. if (key < 0) break;
  381. result = result.firstChild();
  382. while (0 < key--) result = result.nextSibling();
  383. }
  384. return result;
  385. }
  386. QString FbTextPage::location()
  387. {
  388. QString javascript = "location(document.getSelection().anchorNode)";
  389. return mainFrame()->evaluateJavaScript(javascript).toString();
  390. }
  391. QString FbTextPage::status()
  392. {
  393. QString javascript = jScript("get_status.js");
  394. QString status = mainFrame()->evaluateJavaScript(javascript).toString();
  395. return status.replace("FB:", "");
  396. }
  397. void FbTextPage::loadFinished()
  398. {
  399. mainFrame()->addToJavaScriptWindowObject("logger", &m_logger);
  400. FbTextElement element = body().findFirst("fb\\:section");
  401. if (element.isNull()) element = body().findFirst("fb\\:body");
  402. if (element.isNull()) element = body();
  403. FbTextElement child = element.firstChild();
  404. if (child.isTitle()) child = child.nextSibling();
  405. if (!child.isNull()) element = child;
  406. element.select();
  407. }
  408. void FbTextPage::fixContents()
  409. {
  410. foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
  411. span.removeAttribute("style");
  412. }
  413. foreach (QWebElement span, doc().findAll("[style]")) {
  414. span.removeAttribute("style");
  415. }
  416. }
  417. //---------------------------------------------------------------------------
  418. // FbTextBase
  419. //---------------------------------------------------------------------------
  420. void FbTextBase::addTools(QToolBar *tool)
  421. {
  422. QAction *act;
  423. act = pageAction(QWebPage::Undo);
  424. act->setIcon(FbIcon("edit-undo"));
  425. act->setText(QObject::tr("&Undo"));
  426. act->setPriority(QAction::LowPriority);
  427. act->setShortcut(QKeySequence::Undo);
  428. tool->addAction(act);
  429. act = pageAction(QWebPage::Redo);
  430. act->setIcon(FbIcon("edit-redo"));
  431. act->setText(QObject::tr("&Redo"));
  432. act->setPriority(QAction::LowPriority);
  433. act->setShortcut(QKeySequence::Redo);
  434. tool->addAction(act);
  435. tool->addSeparator();
  436. act = pageAction(QWebPage::Cut);
  437. act->setIcon(FbIcon("edit-cut"));
  438. act->setText(QObject::tr("Cu&t"));
  439. act->setPriority(QAction::LowPriority);
  440. act->setShortcuts(QKeySequence::Cut);
  441. act->setStatusTip(QObject::tr("Cut the current selection's contents to the clipboard"));
  442. tool->addAction(act);
  443. act = pageAction(QWebPage::Copy);
  444. act->setIcon(FbIcon("edit-copy"));
  445. act->setText(QObject::tr("&Copy"));
  446. act->setPriority(QAction::LowPriority);
  447. act->setShortcuts(QKeySequence::Copy);
  448. act->setStatusTip(QObject::tr("Copy the current selection's contents to the clipboard"));
  449. tool->addAction(act);
  450. act = pageAction(QWebPage::Paste);
  451. act->setIcon(FbIcon("edit-paste"));
  452. act->setText(QObject::tr("&Paste"));
  453. act->setPriority(QAction::LowPriority);
  454. act->setShortcuts(QKeySequence::Paste);
  455. act->setStatusTip(QObject::tr("Paste the clipboard's contents into the current selection"));
  456. tool->addAction(act);
  457. tool->addSeparator();
  458. act = pageAction(QWebPage::RemoveFormat);
  459. act->setText(QObject::tr("Clear format"));
  460. act->setIcon(FbIcon("edit-clear"));
  461. act = pageAction(QWebPage::ToggleBold);
  462. act->setIcon(FbIcon("format-text-bold"));
  463. act->setText(QObject::tr("&Bold"));
  464. tool->addAction(act);
  465. act = pageAction(QWebPage::ToggleItalic);
  466. act->setIcon(FbIcon("format-text-italic"));
  467. act->setText(QObject::tr("&Italic"));
  468. tool->addAction(act);
  469. act = pageAction(QWebPage::ToggleStrikethrough);
  470. act->setIcon(FbIcon("format-text-strikethrough"));
  471. act->setText(QObject::tr("&Strikethrough"));
  472. tool->addAction(act);
  473. act = pageAction(QWebPage::ToggleSuperscript);
  474. act->setIcon(FbIcon("format-text-superscript"));
  475. act->setText(QObject::tr("Su&perscript"));
  476. tool->addAction(act);
  477. act = pageAction(QWebPage::ToggleSubscript);
  478. act->setIcon(FbIcon("format-text-subscript"));
  479. act->setText(QObject::tr("Su&bscript"));
  480. tool->addAction(act);
  481. }
  482. //---------------------------------------------------------------------------
  483. // FbTextEdit
  484. //---------------------------------------------------------------------------
  485. FbTextEdit::FbTextEdit(QWidget *parent)
  486. : FbTextBase(parent)
  487. , m_noteView(0)
  488. , m_thread(0)
  489. {
  490. setContextMenuPolicy(Qt::CustomContextMenu);
  491. connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
  492. setPage(new FbTextPage(this));
  493. }
  494. FbTextEdit::~FbTextEdit()
  495. {
  496. if (m_noteView) delete m_noteView;
  497. }
  498. FbTextPage *FbTextEdit::page()
  499. {
  500. return qobject_cast<FbTextPage*>(FbTextBase::page());
  501. }
  502. FbNetworkAccessManager *FbTextEdit::files()
  503. {
  504. return page()->temp();
  505. }
  506. FbNoteView & FbTextEdit::noteView()
  507. {
  508. if (m_noteView) return *m_noteView;
  509. m_noteView = new FbNoteView(qobject_cast<QWidget*>(parent()), url());
  510. m_noteView->setPage(new FbTextPage(this));
  511. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  512. m_noteView->page()->setContentEditable(false);
  513. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  514. return *m_noteView;
  515. }
  516. QWebElement FbTextEdit::body()
  517. {
  518. return doc().findFirst("body");
  519. }
  520. QWebElement FbTextEdit::doc()
  521. {
  522. return page()->mainFrame()->documentElement();
  523. }
  524. void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
  525. {
  526. m_point = event->pos();
  527. QWebView::mouseMoveEvent(event);
  528. }
  529. void FbTextEdit::contextMenu(const QPoint &pos)
  530. {
  531. QMenu menu, *submenu;
  532. submenu = menu.addMenu(tr("Fo&rmat"));
  533. submenu->addAction(pageAction(QWebPage::RemoveFormat));
  534. submenu->addSeparator();
  535. submenu->addAction(pageAction(QWebPage::ToggleBold));
  536. submenu->addAction(pageAction(QWebPage::ToggleItalic));
  537. submenu->addAction(pageAction(QWebPage::ToggleStrikethrough));
  538. submenu->addAction(pageAction(QWebPage::ToggleSuperscript));
  539. submenu->addAction(pageAction(QWebPage::ToggleSubscript));
  540. menu.addSeparator();
  541. menu.addAction(pageAction(QWebPage::Cut));
  542. menu.addAction(pageAction(QWebPage::Copy));
  543. menu.addAction(pageAction(QWebPage::Paste));
  544. menu.addAction(pageAction(QWebPage::PasteAndMatchStyle));
  545. menu.exec(mapToGlobal(pos));
  546. }
  547. void FbTextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  548. {
  549. Q_UNUSED(title);
  550. Q_UNUSED(textContent);
  551. const QString href = QUrl(link).fragment();
  552. if (href.isEmpty()) {
  553. if (m_noteView) m_noteView->hide();
  554. return;
  555. }
  556. const QString query = QString("fb\\:section#%1").arg(href);
  557. const QWebElement element = doc().findFirst(query);
  558. if (element.isNull()) {
  559. if (m_noteView) m_noteView->hide();
  560. return;
  561. }
  562. QRect rect = geometry();
  563. QSize size = element.geometry().size() + QSize(2, 4);
  564. int center = rect.size().height() / 2;
  565. int h = size.height();
  566. if (h > center) size.setHeight(center - 10);
  567. int x = (rect.size().width() - size.width()) / 2;
  568. int y = m_point.y();
  569. if ( y > h ) y = y - h - 10; else y = y + 10;
  570. QPoint point = QPoint(x, y) + rect.topLeft();
  571. noteView().hint(element, QRect(point, size));
  572. }
  573. void FbTextEdit::html(QString html)
  574. {
  575. /*
  576. if (!m_thread) return;
  577. static int number = 0;
  578. QWebSettings::clearMemoryCaches();
  579. QUrl url(QString("fb2:/%1/").arg(number++));
  580. FbTextPage *page = m_thread->page();
  581. setPage(page);
  582. page->setParent(this);
  583. page->temp()->setPath(url.path());
  584. setHtml(html, url);
  585. connect(page, SIGNAL(linkHovered(QString,QString,QString)), SLOT(linkHovered(QString,QString,QString)));
  586. m_thread->deleteLater();
  587. m_thread = 0;
  588. */
  589. }
  590. bool FbTextEdit::save(QIODevice *device, const QString &codec)
  591. {
  592. FbSaveWriter writer(*this, device);
  593. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  594. bool ok = FbSaveHandler(writer).save();
  595. if (ok) page()->undoStack()->setClean();
  596. return ok;
  597. }
  598. bool FbTextEdit::save(QByteArray *array)
  599. {
  600. FbSaveWriter writer(*this, array);
  601. return FbSaveHandler(writer).save();
  602. }
  603. bool FbTextEdit::save(QString *string)
  604. {
  605. // Use class QByteArray instead QString
  606. // to store information about encoding.
  607. QByteArray data;
  608. bool ok = save(&data);
  609. if (ok) *string = QString::fromUtf8(data.data());
  610. return ok;
  611. }
  612. void FbTextEdit::data(QString name, QByteArray data)
  613. {
  614. files()->data(name, data);
  615. }
  616. void FbTextEdit::zoomIn()
  617. {
  618. qreal zoom = zoomFactor();
  619. setZoomFactor(zoom * 1.1);
  620. }
  621. void FbTextEdit::zoomOut()
  622. {
  623. qreal zoom = zoomFactor();
  624. setZoomFactor(zoom * 0.9);
  625. }
  626. void FbTextEdit::zoomReset()
  627. {
  628. setZoomFactor(1);
  629. }
  630. bool FbTextEdit::actionEnabled(QWebPage::WebAction action)
  631. {
  632. QAction *act = pageAction(action);
  633. return act ? act->isEnabled() : false;
  634. }
  635. bool FbTextEdit::actionChecked(QWebPage::WebAction action)
  636. {
  637. QAction *act = pageAction(action);
  638. return act ? act->isChecked() : false;
  639. }
  640. bool FbTextEdit::BoldChecked()
  641. {
  642. return pageAction(QWebPage::ToggleBold)->isChecked();
  643. }
  644. bool FbTextEdit::ItalicChecked()
  645. {
  646. return pageAction(QWebPage::ToggleItalic)->isChecked();
  647. }
  648. bool FbTextEdit::StrikeChecked()
  649. {
  650. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  651. }
  652. bool FbTextEdit::SubChecked()
  653. {
  654. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  655. }
  656. bool FbTextEdit::SupChecked()
  657. {
  658. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  659. }
  660. void FbTextEdit::find()
  661. {
  662. FbTextFindDlg dlg(*this);
  663. dlg.exec();
  664. }
  665. void FbTextEdit::insertImage()
  666. {
  667. QString filters;
  668. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
  669. filters += tr("Portable Network Graphics (PNG) (*.png);;");
  670. filters += tr("JPEG (*.jpg *.jpeg);;");
  671. filters += tr("Graphics Interchange Format (*.gif);;");
  672. filters += tr("All Files (*)");
  673. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  674. if (path.isEmpty()) return;
  675. QFile file(path);
  676. if (!file.open(QIODevice::ReadOnly)) return;
  677. QByteArray data = file.readAll();
  678. QString name = files()->add(path, data);
  679. execCommand("insertImage", name.prepend("#"));
  680. }
  681. void FbTextEdit::insertNote()
  682. {
  683. FbNoteDlg dlg(*this);
  684. dlg.exec();
  685. }
  686. void FbTextEdit::insertLink()
  687. {
  688. bool ok;
  689. QString text = QInputDialog::getText(this, tr("Insert hyperlink"), tr("URL:"), QLineEdit::Normal, QString(), &ok);
  690. if (ok && !text.isEmpty()) execCommand("CreateLink", text);
  691. }
  692. void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
  693. {
  694. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  695. page()->mainFrame()->evaluateJavaScript(javascript);
  696. }
  697. //---------------------------------------------------------------------------
  698. // FbWebFrame
  699. //---------------------------------------------------------------------------
  700. FbWebFrame::FbWebFrame(QWidget *parent)
  701. : QFrame(parent)
  702. , m_view(this)
  703. {
  704. setFrameShape(QFrame::StyledPanel);
  705. setFrameShadow(QFrame::Sunken);
  706. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  707. layout->setSpacing(0);
  708. layout->setMargin(0);
  709. layout->addWidget(&m_view);
  710. }
  711. //---------------------------------------------------------------------------
  712. // FbTextFrame
  713. //---------------------------------------------------------------------------
  714. FbTextFrame::FbTextFrame(QWidget *parent, QAction *action)
  715. : QFrame(parent)
  716. , m_view(this)
  717. , m_dock(0)
  718. {
  719. setFrameShape(QFrame::StyledPanel);
  720. setFrameShadow(QFrame::Sunken);
  721. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  722. layout->setSpacing(0);
  723. layout->setMargin(0);
  724. layout->addWidget(&m_view);
  725. connect(action, SIGNAL(triggered()), SLOT(showInspector()));
  726. }
  727. FbTextFrame::~FbTextFrame()
  728. {
  729. if (m_dock) m_dock->deleteLater();
  730. }
  731. void FbTextFrame::showInspector()
  732. {
  733. if (m_dock) {
  734. m_dock->setVisible(m_dock->isHidden());
  735. return;
  736. }
  737. QMainWindow *main = qobject_cast<QMainWindow*>(parent());
  738. if (!main) return;
  739. m_dock = new QDockWidget(tr("Web inspector"), this);
  740. m_dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  741. main->addDockWidget(Qt::BottomDockWidgetArea, m_dock);
  742. QWebInspector *inspector = new QWebInspector(this);
  743. inspector->setPage(m_view.page());
  744. m_dock->setWidget(inspector);
  745. connect(m_dock, SIGNAL(visibilityChanged(bool)), main, SIGNAL(showInspectorChecked(bool)));
  746. connect(m_dock, SIGNAL(destroyed()), SLOT(dockDestroyed()));
  747. }
  748. void FbTextFrame::hideInspector()
  749. {
  750. if (m_dock) m_dock->hide();
  751. }
  752. void FbTextFrame::dockDestroyed()
  753. {
  754. m_dock = 0;
  755. }
  756. //---------------------------------------------------------------------------
  757. // FbTextAction
  758. //---------------------------------------------------------------------------
  759. FbTextAction::FbTextAction(const QString &text, QWebPage::WebAction action, QObject* parent)
  760. : QAction(text, parent)
  761. , m_action(action)
  762. {
  763. }
  764. FbTextAction::FbTextAction(const QIcon &icon, const QString &text, QWebPage::WebAction action, QObject* parent)
  765. : QAction(icon, text, parent)
  766. , m_action(action)
  767. {
  768. }
  769. void FbTextAction::updateChecked()
  770. {
  771. if (QAction * act = action(m_action)) setChecked(act->isChecked());
  772. }
  773. void FbTextAction::updateEnabled()
  774. {
  775. if (QAction * act = action(m_action)) setEnabled(act->isEnabled());
  776. }