fb2text.cpp 24 KB

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