fb2text.cpp 21 KB

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