fb2text.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. FbTextElement FbTextPage::current()
  252. {
  253. return element(location());
  254. }
  255. FbTextElement FbTextPage::element(const QString &location)
  256. {
  257. QStringList list = location.split(",");
  258. QStringListIterator iterator(list);
  259. QWebElement result = doc();
  260. while (iterator.hasNext()) {
  261. QString str = iterator.next();
  262. int pos = str.indexOf("=");
  263. QString tag = str.left(pos);
  264. int key = str.mid(pos + 1).toInt();
  265. if (key < 0) break;
  266. result = result.firstChild();
  267. while (0 < key--) result = result.nextSibling();
  268. }
  269. return result;
  270. }
  271. QString FbTextPage::location()
  272. {
  273. static const QString javascript = FB2::read(":/js/get_location.js").prepend("var element=document.getSelection().anchorNode;");
  274. return mainFrame()->evaluateJavaScript(javascript).toString();
  275. }
  276. QString FbTextPage::status()
  277. {
  278. static const QString javascript = FB2::read(":/js/get_status.js");
  279. return mainFrame()->evaluateJavaScript(javascript).toString();
  280. }
  281. void FbTextPage::loadFinished()
  282. {
  283. FbTextElement element = body().findFirst("div.body");
  284. if (element.isNull()) element = body();
  285. element.select();
  286. }
  287. void FbTextPage::fixContents()
  288. {
  289. foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
  290. span.removeAttribute("style");
  291. }
  292. }
  293. //---------------------------------------------------------------------------
  294. // FbTextBase
  295. //---------------------------------------------------------------------------
  296. void FbTextBase::addTools(QToolBar *tool)
  297. {
  298. QAction *act;
  299. act = pageAction(QWebPage::Undo);
  300. act->setIcon(FbIcon("edit-undo"));
  301. act->setText(QObject::tr("&Undo"));
  302. act->setPriority(QAction::LowPriority);
  303. act->setShortcut(QKeySequence::Undo);
  304. tool->addAction(act);
  305. act = pageAction(QWebPage::Redo);
  306. act->setIcon(FbIcon("edit-redo"));
  307. act->setText(QObject::tr("&Redo"));
  308. act->setPriority(QAction::LowPriority);
  309. act->setShortcut(QKeySequence::Redo);
  310. tool->addAction(act);
  311. tool->addSeparator();
  312. act = pageAction(QWebPage::Cut);
  313. act->setIcon(FbIcon("edit-cut"));
  314. act->setText(QObject::tr("Cu&t"));
  315. act->setPriority(QAction::LowPriority);
  316. act->setShortcuts(QKeySequence::Cut);
  317. act->setStatusTip(QObject::tr("Cut the current selection's contents to the clipboard"));
  318. tool->addAction(act);
  319. act = pageAction(QWebPage::Copy);
  320. act->setIcon(FbIcon("edit-copy"));
  321. act->setText(QObject::tr("&Copy"));
  322. act->setPriority(QAction::LowPriority);
  323. act->setShortcuts(QKeySequence::Copy);
  324. act->setStatusTip(QObject::tr("Copy the current selection's contents to the clipboard"));
  325. tool->addAction(act);
  326. act = pageAction(QWebPage::Paste);
  327. act->setIcon(FbIcon("edit-paste"));
  328. act->setText(QObject::tr("&Paste"));
  329. act->setPriority(QAction::LowPriority);
  330. act->setShortcuts(QKeySequence::Paste);
  331. act->setStatusTip(QObject::tr("Paste the clipboard's contents into the current selection"));
  332. tool->addAction(act);
  333. tool->addSeparator();
  334. act = pageAction(QWebPage::RemoveFormat);
  335. act->setText(QObject::tr("Clear format"));
  336. act->setIcon(FbIcon("edit-clear"));
  337. act = pageAction(QWebPage::ToggleBold);
  338. act->setIcon(FbIcon("format-text-bold"));
  339. act->setText(QObject::tr("&Bold"));
  340. tool->addAction(act);
  341. act = pageAction(QWebPage::ToggleItalic);
  342. act->setIcon(FbIcon("format-text-italic"));
  343. act->setText(QObject::tr("&Italic"));
  344. tool->addAction(act);
  345. act = pageAction(QWebPage::ToggleStrikethrough);
  346. act->setIcon(FbIcon("format-text-strikethrough"));
  347. act->setText(QObject::tr("&Strikethrough"));
  348. tool->addAction(act);
  349. act = pageAction(QWebPage::ToggleSuperscript);
  350. act->setIcon(FbIcon("format-text-superscript"));
  351. act->setText(QObject::tr("Su&perscript"));
  352. tool->addAction(act);
  353. act = pageAction(QWebPage::ToggleSubscript);
  354. act->setIcon(FbIcon("format-text-subscript"));
  355. act->setText(QObject::tr("Su&bscript"));
  356. tool->addAction(act);
  357. }
  358. //---------------------------------------------------------------------------
  359. // FbTextEdit
  360. //---------------------------------------------------------------------------
  361. FbTextEdit::FbTextEdit(QWidget *parent)
  362. : FbTextBase(parent)
  363. , m_noteView(0)
  364. , m_thread(0)
  365. {
  366. setContextMenuPolicy(Qt::CustomContextMenu);
  367. connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
  368. setPage(new FbTextPage(this));
  369. }
  370. FbTextEdit::~FbTextEdit()
  371. {
  372. if (m_noteView) delete m_noteView;
  373. }
  374. FbTextPage *FbTextEdit::page()
  375. {
  376. return qobject_cast<FbTextPage*>(FbTextBase::page());
  377. }
  378. FbNetworkAccessManager *FbTextEdit::files()
  379. {
  380. return page()->temp();
  381. }
  382. FbNoteView & FbTextEdit::noteView()
  383. {
  384. if (m_noteView) return *m_noteView;
  385. m_noteView = new FbNoteView(qobject_cast<QWidget*>(parent()), url());
  386. m_noteView->setPage(new FbTextPage(this));
  387. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  388. m_noteView->page()->setContentEditable(false);
  389. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  390. return *m_noteView;
  391. }
  392. QWebElement FbTextEdit::body()
  393. {
  394. return doc().findFirst("body");
  395. }
  396. QWebElement FbTextEdit::doc()
  397. {
  398. return page()->mainFrame()->documentElement();
  399. }
  400. void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
  401. {
  402. m_point = event->pos();
  403. QWebView::mouseMoveEvent(event);
  404. }
  405. void FbTextEdit::contextMenu(const QPoint &pos)
  406. {
  407. QMenu menu, *submenu;
  408. submenu = menu.addMenu(tr("Fo&rmat"));
  409. submenu->addAction(pageAction(QWebPage::RemoveFormat));
  410. submenu->addSeparator();
  411. submenu->addAction(pageAction(QWebPage::ToggleBold));
  412. submenu->addAction(pageAction(QWebPage::ToggleItalic));
  413. submenu->addAction(pageAction(QWebPage::ToggleStrikethrough));
  414. submenu->addAction(pageAction(QWebPage::ToggleSuperscript));
  415. submenu->addAction(pageAction(QWebPage::ToggleSubscript));
  416. menu.addSeparator();
  417. menu.addAction(pageAction(QWebPage::Cut));
  418. menu.addAction(pageAction(QWebPage::Copy));
  419. menu.addAction(pageAction(QWebPage::Paste));
  420. menu.addAction(pageAction(QWebPage::PasteAndMatchStyle));
  421. menu.exec(mapToGlobal(pos));
  422. }
  423. void FbTextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  424. {
  425. Q_UNUSED(title);
  426. Q_UNUSED(textContent);
  427. const QString href = QUrl(link).fragment();
  428. if (href.isEmpty()) {
  429. if (m_noteView) m_noteView->hide();
  430. return;
  431. }
  432. const QString query = QString("DIV#%1").arg(href);
  433. const QWebElement element = doc().findFirst(query);
  434. if (element.isNull()) {
  435. if (m_noteView) m_noteView->hide();
  436. return;
  437. }
  438. QRect rect = geometry();
  439. QSize size = element.geometry().size() + QSize(2, 4);
  440. int center = rect.size().height() / 2;
  441. int h = size.height();
  442. if (h > center) size.setHeight(center - 10);
  443. int x = (rect.size().width() - size.width()) / 2;
  444. int y = m_point.y();
  445. if ( y > h ) y = y - h - 10; else y = y + 10;
  446. QPoint point = QPoint(x, y) + rect.topLeft();
  447. noteView().hint(element, QRect(point, size));
  448. }
  449. void FbTextEdit::load(const QString &filename, const QString &xml)
  450. {
  451. if (m_thread) return;
  452. m_thread = new FbReadThread(this, filename, xml);
  453. FbTextPage *page = new FbTextPage(m_thread);
  454. m_thread->setPage(page);
  455. m_thread->setTemp(page->temp());
  456. m_thread->start();
  457. }
  458. void FbTextEdit::html(QString html)
  459. {
  460. if (!m_thread) return;
  461. static int number = 0;
  462. QWebSettings::clearMemoryCaches();
  463. QUrl url(QString("fb2:/%1/").arg(number++));
  464. FbTextPage *page = m_thread->page();
  465. setPage(page);
  466. page->setParent(this);
  467. page->temp()->setPath(url.path());
  468. setHtml(html, url);
  469. connect(page, SIGNAL(linkHovered(QString,QString,QString)), SLOT(linkHovered(QString,QString,QString)));
  470. m_thread->deleteLater();
  471. m_thread = 0;
  472. }
  473. bool FbTextEdit::save(QIODevice *device, const QString &codec)
  474. {
  475. FbSaveWriter writer(*this, device);
  476. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  477. bool ok = FbSaveHandler(writer).save();
  478. if (ok) page()->undoStack()->setClean();
  479. return ok;
  480. }
  481. bool FbTextEdit::save(QByteArray *array)
  482. {
  483. FbSaveWriter writer(*this, array);
  484. return FbSaveHandler(writer).save();
  485. }
  486. bool FbTextEdit::save(QString *string)
  487. {
  488. // Use class QByteArray instead QString
  489. // to store information about encoding.
  490. QByteArray data;
  491. bool ok = save(&data);
  492. if (ok) *string = QString::fromUtf8(data.data());
  493. return ok;
  494. }
  495. void FbTextEdit::data(QString name, QByteArray data)
  496. {
  497. files()->data(name, data);
  498. }
  499. void FbTextEdit::zoomIn()
  500. {
  501. qreal zoom = zoomFactor();
  502. setZoomFactor(zoom * 1.1);
  503. }
  504. void FbTextEdit::zoomOut()
  505. {
  506. qreal zoom = zoomFactor();
  507. setZoomFactor(zoom * 0.9);
  508. }
  509. void FbTextEdit::zoomReset()
  510. {
  511. setZoomFactor(1);
  512. }
  513. bool FbTextEdit::actionEnabled(QWebPage::WebAction action)
  514. {
  515. QAction *act = pageAction(action);
  516. return act ? act->isEnabled() : false;
  517. }
  518. bool FbTextEdit::actionChecked(QWebPage::WebAction action)
  519. {
  520. QAction *act = pageAction(action);
  521. return act ? act->isChecked() : false;
  522. }
  523. bool FbTextEdit::BoldChecked()
  524. {
  525. return pageAction(QWebPage::ToggleBold)->isChecked();
  526. }
  527. bool FbTextEdit::ItalicChecked()
  528. {
  529. return pageAction(QWebPage::ToggleItalic)->isChecked();
  530. }
  531. bool FbTextEdit::StrikeChecked()
  532. {
  533. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  534. }
  535. bool FbTextEdit::SubChecked()
  536. {
  537. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  538. }
  539. bool FbTextEdit::SupChecked()
  540. {
  541. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  542. }
  543. void FbTextEdit::find()
  544. {
  545. FbTextFindDlg dlg(*this);
  546. dlg.exec();
  547. }
  548. void FbTextEdit::insertImage()
  549. {
  550. QString filters;
  551. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
  552. filters += tr("Portable Network Graphics (PNG) (*.png);;");
  553. filters += tr("JPEG (*.jpg *.jpeg);;");
  554. filters += tr("Graphics Interchange Format (*.gif);;");
  555. filters += tr("All Files (*)");
  556. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  557. if (path.isEmpty()) return;
  558. QFile file(path);
  559. if (!file.open(QIODevice::ReadOnly)) return;
  560. QByteArray data = file.readAll();
  561. QString name = files()->add(path, data);
  562. execCommand("insertImage", name.prepend("#"));
  563. }
  564. void FbTextEdit::insertNote()
  565. {
  566. FbNoteDlg dlg(*this);
  567. dlg.exec();
  568. }
  569. void FbTextEdit::insertLink()
  570. {
  571. bool ok;
  572. QString text = QInputDialog::getText(this, tr("Insert hyperlink"), tr("URL:"), QLineEdit::Normal, QString(), &ok);
  573. if (ok && !text.isEmpty()) execCommand("CreateLink", text);
  574. }
  575. void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
  576. {
  577. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  578. page()->mainFrame()->evaluateJavaScript(javascript);
  579. }
  580. //---------------------------------------------------------------------------
  581. // FbTextFrame
  582. //---------------------------------------------------------------------------
  583. FbTextFrame::FbTextFrame(QWidget *parent, QAction *action)
  584. : QFrame(parent)
  585. , m_view(this)
  586. , m_dock(0)
  587. {
  588. setFrameShape(QFrame::StyledPanel);
  589. setFrameShadow(QFrame::Sunken);
  590. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  591. layout->setSpacing(0);
  592. layout->setMargin(0);
  593. layout->addWidget(&m_view);
  594. connect(action, SIGNAL(triggered()), SLOT(showInspector()));
  595. }
  596. FbTextFrame::~FbTextFrame()
  597. {
  598. if (m_dock) m_dock->deleteLater();
  599. }
  600. void FbTextFrame::showInspector()
  601. {
  602. if (m_dock) {
  603. m_dock->setVisible(m_dock->isHidden());
  604. return;
  605. }
  606. QMainWindow *main = qobject_cast<QMainWindow*>(parent());
  607. if (!main) return;
  608. m_dock = new QDockWidget(tr("Web inspector"), this);
  609. m_dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  610. main->addDockWidget(Qt::BottomDockWidgetArea, m_dock);
  611. QWebInspector *inspector = new QWebInspector(this);
  612. inspector->setPage(m_view.page());
  613. m_dock->setWidget(inspector);
  614. connect(m_dock, SIGNAL(visibilityChanged(bool)), main, SIGNAL(showInspectorChecked(bool)));
  615. connect(m_dock, SIGNAL(destroyed()), SLOT(dockDestroyed()));
  616. }
  617. void FbTextFrame::hideInspector()
  618. {
  619. if (m_dock) m_dock->hide();
  620. }
  621. void FbTextFrame::dockDestroyed()
  622. {
  623. m_dock = 0;
  624. }
  625. //---------------------------------------------------------------------------
  626. // FbTextAction
  627. //---------------------------------------------------------------------------
  628. FbTextAction::FbTextAction(const QString &text, QWebPage::WebAction action, QObject* parent)
  629. : QAction(text, parent)
  630. , m_action(action)
  631. {
  632. }
  633. FbTextAction::FbTextAction(const QIcon &icon, const QString &text, QWebPage::WebAction action, QObject* parent)
  634. : QAction(icon, text, parent)
  635. , m_action(action)
  636. {
  637. }
  638. void FbTextAction::updateChecked()
  639. {
  640. if (QAction * act = action(m_action)) setChecked(act->isChecked());
  641. }
  642. void FbTextAction::updateEnabled()
  643. {
  644. if (QAction * act = action(m_action)) setEnabled(act->isEnabled());
  645. }