fb2text.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include "fb2text.hpp"
  2. #include <QAction>
  3. #include <QBoxLayout>
  4. #include <QDockWidget>
  5. #include <QFileDialog>
  6. #include <QInputDialog>
  7. #include <QMainWindow>
  8. #include <QMenu>
  9. #include <QNetworkRequest>
  10. #include <QStyle>
  11. #include <QStyleOptionFrame>
  12. #include <QToolBar>
  13. #include <QToolTip>
  14. #include <QUndoCommand>
  15. #include <QUndoStack>
  16. #include <QWebElement>
  17. #include <QWebInspector>
  18. #include <QWebFrame>
  19. #include <QWebPage>
  20. #include <QtDebug>
  21. #include "fb2dlgs.hpp"
  22. #include "fb2page.hpp"
  23. #include "fb2save.hpp"
  24. #include "fb2tree.hpp"
  25. #include "fb2utils.h"
  26. //---------------------------------------------------------------------------
  27. // FbDockWidget
  28. //---------------------------------------------------------------------------
  29. FbDockWidget::FbDockWidget(const QString &title, QWidget *parent, Qt::WindowFlags flags)
  30. : QDockWidget(title, parent, flags)
  31. {
  32. setFeatures(QDockWidget::AllDockWidgetFeatures);
  33. setAttribute(Qt::WA_DeleteOnClose);
  34. }
  35. //---------------------------------------------------------------------------
  36. // FbNoteView
  37. //---------------------------------------------------------------------------
  38. class FbNoteView : public QWebView
  39. {
  40. public:
  41. explicit FbNoteView(QWidget *parent, const QUrl &url);
  42. void hint(const QWebElement element, const QRect &rect);
  43. protected:
  44. void paintEvent(QPaintEvent *event);
  45. const QUrl m_url;
  46. };
  47. FbNoteView::FbNoteView(QWidget *parent, const QUrl &url)
  48. : QWebView(parent)
  49. , m_url(url)
  50. {
  51. }
  52. void FbNoteView::paintEvent(QPaintEvent *event)
  53. {
  54. QWebView::paintEvent(event);
  55. QPainter painter(this);
  56. painter.setPen(Qt::black);
  57. QSize size = geometry().size() - QSize(1, 1);
  58. painter.drawRect( QRect(QPoint(0, 0), size) );
  59. }
  60. void FbNoteView::hint(const QWebElement element, const QRect &rect)
  61. {
  62. QString html = element.toOuterXml();
  63. html.prepend(
  64. "<body bgcolor=lightyellow style='overflow:hidden;padding:0;margin:0;margin-top:2;'>"
  65. "<fb:body name=notes style='padding:0;margin:0;'>"
  66. );
  67. html.append("</fb:body></body>");
  68. setGeometry(rect);
  69. setHtml(html, m_url);
  70. show();
  71. }
  72. //---------------------------------------------------------------------------
  73. // FbTextBase
  74. //---------------------------------------------------------------------------
  75. void FbTextBase::addTools(QToolBar *tool)
  76. {
  77. QAction *act;
  78. act = pageAction(QWebPage::Undo);
  79. act->setIcon(FbIcon("edit-undo"));
  80. act->setText(QObject::tr("&Undo"));
  81. act->setPriority(QAction::LowPriority);
  82. act->setShortcut(QKeySequence::Undo);
  83. tool->addAction(act);
  84. act = pageAction(QWebPage::Redo);
  85. act->setIcon(FbIcon("edit-redo"));
  86. act->setText(QObject::tr("&Redo"));
  87. act->setPriority(QAction::LowPriority);
  88. act->setShortcut(QKeySequence::Redo);
  89. tool->addAction(act);
  90. tool->addSeparator();
  91. act = pageAction(QWebPage::Cut);
  92. act->setIcon(FbIcon("edit-cut"));
  93. act->setText(QObject::tr("Cu&t"));
  94. act->setPriority(QAction::LowPriority);
  95. act->setShortcuts(QKeySequence::Cut);
  96. act->setStatusTip(QObject::tr("Cut the current selection's contents to the clipboard"));
  97. tool->addAction(act);
  98. act = pageAction(QWebPage::Copy);
  99. act->setIcon(FbIcon("edit-copy"));
  100. act->setText(QObject::tr("&Copy"));
  101. act->setPriority(QAction::LowPriority);
  102. act->setShortcuts(QKeySequence::Copy);
  103. act->setStatusTip(QObject::tr("Copy the current selection's contents to the clipboard"));
  104. tool->addAction(act);
  105. act = pageAction(QWebPage::Paste);
  106. act->setIcon(FbIcon("edit-paste"));
  107. act->setText(QObject::tr("&Paste"));
  108. act->setPriority(QAction::LowPriority);
  109. act->setShortcuts(QKeySequence::Paste);
  110. act->setStatusTip(QObject::tr("Paste the clipboard's contents into the current selection"));
  111. tool->addAction(act);
  112. tool->addSeparator();
  113. act = pageAction(QWebPage::RemoveFormat);
  114. act->setText(QObject::tr("Clear format"));
  115. act->setIcon(FbIcon("edit-clear"));
  116. act = pageAction(QWebPage::ToggleBold);
  117. act->setIcon(FbIcon("format-text-bold"));
  118. act->setText(QObject::tr("&Bold"));
  119. tool->addAction(act);
  120. act = pageAction(QWebPage::ToggleItalic);
  121. act->setIcon(FbIcon("format-text-italic"));
  122. act->setText(QObject::tr("&Italic"));
  123. tool->addAction(act);
  124. act = pageAction(QWebPage::ToggleStrikethrough);
  125. act->setIcon(FbIcon("format-text-strikethrough"));
  126. act->setText(QObject::tr("&Strikethrough"));
  127. tool->addAction(act);
  128. act = pageAction(QWebPage::ToggleSuperscript);
  129. act->setIcon(FbIcon("format-text-superscript"));
  130. act->setText(QObject::tr("Su&perscript"));
  131. tool->addAction(act);
  132. act = pageAction(QWebPage::ToggleSubscript);
  133. act->setIcon(FbIcon("format-text-subscript"));
  134. act->setText(QObject::tr("Su&bscript"));
  135. tool->addAction(act);
  136. }
  137. //---------------------------------------------------------------------------
  138. // FbTextEdit
  139. //---------------------------------------------------------------------------
  140. FbTextEdit::FbTextEdit(QWidget *parent, QObject *owner)
  141. : FbTextBase(parent)
  142. , m_owner(qobject_cast<QMainWindow*>(owner))
  143. , m_noteView(0)
  144. , m_thread(0)
  145. , dockTree(0)
  146. , dockImgs(0)
  147. {
  148. setContextMenuPolicy(Qt::CustomContextMenu);
  149. connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
  150. setPage(new FbTextPage(this));
  151. }
  152. FbTextEdit::~FbTextEdit()
  153. {
  154. if (m_noteView) delete m_noteView;
  155. }
  156. FbTextPage *FbTextEdit::page()
  157. {
  158. return qobject_cast<FbTextPage*>(FbTextBase::page());
  159. }
  160. FbNetworkAccessManager *FbTextEdit::files()
  161. {
  162. return page()->temp();
  163. }
  164. QAction * FbTextEdit::act(Fb::Actions index) const
  165. {
  166. return m_actions[index];
  167. }
  168. void FbTextEdit::setAction(Fb::Actions index, QAction *action)
  169. {
  170. m_actions[index] = action;
  171. }
  172. void FbTextEdit::connectActions(QToolBar *tool)
  173. {
  174. connect(act(Fb::ViewContents), SIGNAL(triggered()), this, SLOT(viewTree()));
  175. connect(act(Fb::ViewPictures), SIGNAL(triggered()), this, SLOT(viewImgs()));
  176. connect(act(Fb::ViewPictures), SIGNAL(triggered()), this, SLOT(viewInsp()));
  177. /*
  178. connect(textPage->undoStack(), SIGNAL(cleanChanged(bool)), SLOT(cleanChanged(bool)));
  179. connect(textPage->undoStack(), SIGNAL(canUndoChanged(bool)), SLOT(canUndoChanged(bool)));
  180. connect(textPage->undoStack(), SIGNAL(canRedoChanged(bool)), SLOT(canRedoChanged(bool)));
  181. connect(textPage, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
  182. connect(actionUndo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Undo), SIGNAL(triggered()));
  183. connect(actionRedo, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Redo), SIGNAL(triggered()));
  184. connect(actionCut, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Cut), SIGNAL(triggered()));
  185. connect(actionCopy, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Copy), SIGNAL(triggered()));
  186. connect(actionPaste, SIGNAL(triggered()), textEdit->pageAction(QWebPage::Paste), SIGNAL(triggered()));
  187. connect(actionPasteText, SIGNAL(triggered()), textEdit->pageAction(QWebPage::PasteAndMatchStyle), SIGNAL(triggered()));
  188. connect(actionClearFormat, SIGNAL(triggered()), textEdit->pageAction(QWebPage::RemoveFormat), SIGNAL(triggered()));
  189. connect(actionTextBold, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(triggered()));
  190. connect(actionTextItalic, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(triggered()));
  191. connect(actionTextStrike, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(triggered()));
  192. connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
  193. connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
  194. connect(textEdit->pageAction(QWebPage::RemoveFormat), SIGNAL(changed()), actionClearFormat, SLOT(updateEnabled()));
  195. connect(textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(changed()), actionTextBold, SLOT(updateChecked()));
  196. connect(textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(changed()), actionTextItalic, SLOT(updateChecked()));
  197. connect(textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(changed()), actionTextStrike, SLOT(updateChecked()));
  198. connect(textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(changed()), actionTextSub, SLOT(updateChecked()));
  199. connect(textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(changed()), actionTextSup, SLOT(updateChecked()));
  200. connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
  201. connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
  202. connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
  203. connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
  204. connect(actionTitle, SIGNAL(triggered()), textPage, SLOT(insertTitle()));
  205. connect(actionAnnot, SIGNAL(triggered()), textPage, SLOT(insertAnnot()));
  206. connect(actionAuthor, SIGNAL(triggered()), textPage, SLOT(insertAuthor()));
  207. connect(actionEpigraph, SIGNAL(triggered()), textPage, SLOT(insertEpigraph()));
  208. connect(actionSubtitle, SIGNAL(triggered()), textPage, SLOT(insertSubtitle()));
  209. connect(actionSection, SIGNAL(triggered()), textPage, SLOT(insertSection()));
  210. connect(actionStanza, SIGNAL(triggered()), textPage, SLOT(insertStanza()));
  211. connect(actionPoem, SIGNAL(triggered()), textPage, SLOT(insertPoem()));
  212. connect(actionDate, SIGNAL(triggered()), textPage, SLOT(insertDate()));
  213. connect(actionBody, SIGNAL(triggered()), textPage, SLOT(insertBody()));
  214. connect(actionSimpleText, SIGNAL(triggered()), textPage, SLOT(insertText()));
  215. connect(actionParaSeparator, SIGNAL(triggered()), textEdit->pageAction(QWebPage::InsertParagraphSeparator), SIGNAL(triggered()));
  216. connect(actionLineSeparator, SIGNAL(triggered()), textEdit->pageAction(QWebPage::InsertLineSeparator), SIGNAL(triggered()));
  217. connect(actionSectionAdd, SIGNAL(triggered()), textPage, SLOT(createSection()));
  218. connect(actionSectionDel, SIGNAL(triggered()), textPage, SLOT(deleteSection()));
  219. connect(actionTextTitle, SIGNAL(triggered()), textPage, SLOT(createTitle()));
  220. connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
  221. connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
  222. connect(actionZoomReset, SIGNAL(triggered()), textEdit, SLOT(zoomReset()));
  223. tool->clear();
  224. tool->addSeparator();
  225. tool->addAction(actionSectionAdd);
  226. tool->addAction(actionSectionDel);
  227. tool->addAction(actionTextTitle);
  228. tool->addSeparator();
  229. tool->addAction(actionImage);
  230. tool->addAction(actionNote);
  231. tool->addAction(actionLink);
  232. tool->addAction(actionSection);
  233. */
  234. }
  235. void FbTextEdit::disconnectActions()
  236. {
  237. foreach (QAction *action, m_actions) {
  238. action->setDisabled(true);
  239. action->setChecked(false);
  240. disconnect(action);
  241. }
  242. }
  243. void FbTextEdit::createTree()
  244. {
  245. dockTree = new FbDockWidget(tr("Contents"), this);
  246. dockTree->setWidget(new FbTreeWidget(this, m_owner));
  247. // connect(dockTree, SIGNAL(visibilityChanged(bool)), actionContents, SLOT(setChecked(bool)));
  248. // connect(dockTree, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  249. m_owner->addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  250. }
  251. void FbTextEdit::createImgs()
  252. {
  253. dockImgs = new FbDockWidget(tr("Pictures"), this);
  254. dockImgs->setWidget(new FbListWidget(this, m_owner));
  255. // connect(dockImgs, SIGNAL(visibilityChanged(bool)), actionPictures, SLOT(setChecked(bool)));
  256. // connect(dockImgs, SIGNAL(destroyed()), SLOT(imgsDestroyed()));
  257. m_owner->addDockWidget(Qt::RightDockWidgetArea, dockImgs);
  258. }
  259. void FbTextEdit::treeDestroyed()
  260. {
  261. m_actions[Fb::ViewContents]->setChecked(false);
  262. dockTree = 0;
  263. }
  264. void FbTextEdit::imgsDestroyed()
  265. {
  266. m_actions[Fb::ViewPictures]->setChecked(false);
  267. dockImgs = 0;
  268. }
  269. FbNoteView & FbTextEdit::noteView()
  270. {
  271. if (m_noteView) return *m_noteView;
  272. m_noteView = new FbNoteView(qobject_cast<QWidget*>(parent()), url());
  273. m_noteView->setPage(new FbTextPage(this));
  274. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  275. m_noteView->page()->setContentEditable(false);
  276. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  277. return *m_noteView;
  278. }
  279. QWebElement FbTextEdit::body()
  280. {
  281. return doc().findFirst("body");
  282. }
  283. QWebElement FbTextEdit::doc()
  284. {
  285. return page()->mainFrame()->documentElement();
  286. }
  287. void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
  288. {
  289. m_point = event->pos();
  290. QWebView::mouseMoveEvent(event);
  291. }
  292. void FbTextEdit::contextMenu(const QPoint &pos)
  293. {
  294. QMenu menu, *submenu;
  295. submenu = menu.addMenu(tr("Fo&rmat"));
  296. submenu->addAction(pageAction(QWebPage::RemoveFormat));
  297. submenu->addSeparator();
  298. submenu->addAction(pageAction(QWebPage::ToggleBold));
  299. submenu->addAction(pageAction(QWebPage::ToggleItalic));
  300. submenu->addAction(pageAction(QWebPage::ToggleStrikethrough));
  301. submenu->addAction(pageAction(QWebPage::ToggleSuperscript));
  302. submenu->addAction(pageAction(QWebPage::ToggleSubscript));
  303. menu.addSeparator();
  304. menu.addAction(pageAction(QWebPage::Cut));
  305. menu.addAction(pageAction(QWebPage::Copy));
  306. menu.addAction(pageAction(QWebPage::Paste));
  307. menu.addAction(pageAction(QWebPage::PasteAndMatchStyle));
  308. menu.exec(mapToGlobal(pos));
  309. }
  310. void FbTextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  311. {
  312. Q_UNUSED(title);
  313. Q_UNUSED(textContent);
  314. const QString href = QUrl(link).fragment();
  315. if (href.isEmpty()) {
  316. if (m_noteView) m_noteView->hide();
  317. return;
  318. }
  319. const QString query = QString("fb\\:section#%1").arg(href);
  320. const QWebElement element = doc().findFirst(query);
  321. if (element.isNull()) {
  322. if (m_noteView) m_noteView->hide();
  323. return;
  324. }
  325. QRect rect = geometry();
  326. QSize size = element.geometry().size() + QSize(2, 4);
  327. int center = rect.size().height() / 2;
  328. int h = size.height();
  329. if (h > center) size.setHeight(center - 10);
  330. int x = (rect.size().width() - size.width()) / 2;
  331. int y = m_point.y();
  332. if ( y > h ) y = y - h - 10; else y = y + 10;
  333. QPoint point = QPoint(x, y) + rect.topLeft();
  334. noteView().hint(element, QRect(point, size));
  335. }
  336. void FbTextEdit::html(QString html)
  337. {
  338. /*
  339. if (!m_thread) return;
  340. static int number = 0;
  341. QWebSettings::clearMemoryCaches();
  342. QUrl url(QString("fb2:/%1/").arg(number++));
  343. FbTextPage *page = m_thread->page();
  344. setPage(page);
  345. page->setParent(this);
  346. page->temp()->setPath(url.path());
  347. setHtml(html, url);
  348. connect(page, SIGNAL(linkHovered(QString,QString,QString)), SLOT(linkHovered(QString,QString,QString)));
  349. m_thread->deleteLater();
  350. m_thread = 0;
  351. */
  352. }
  353. bool FbTextEdit::save(QIODevice *device, const QString &codec)
  354. {
  355. FbSaveWriter writer(*this, device);
  356. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  357. bool ok = FbSaveHandler(writer).save();
  358. if (ok) page()->undoStack()->setClean();
  359. return ok;
  360. }
  361. bool FbTextEdit::save(QByteArray *array)
  362. {
  363. FbSaveWriter writer(*this, array);
  364. return FbSaveHandler(writer).save();
  365. }
  366. bool FbTextEdit::save(QString *string)
  367. {
  368. // Use class QByteArray instead QString
  369. // to store information about encoding.
  370. QByteArray data;
  371. bool ok = save(&data);
  372. if (ok) *string = QString::fromUtf8(data.data());
  373. return ok;
  374. }
  375. void FbTextEdit::data(QString name, QByteArray data)
  376. {
  377. files()->data(name, data);
  378. }
  379. void FbTextEdit::zoomIn()
  380. {
  381. qreal zoom = zoomFactor();
  382. setZoomFactor(zoom * 1.1);
  383. }
  384. void FbTextEdit::zoomOut()
  385. {
  386. qreal zoom = zoomFactor();
  387. setZoomFactor(zoom * 0.9);
  388. }
  389. void FbTextEdit::zoomReset()
  390. {
  391. setZoomFactor(1);
  392. }
  393. bool FbTextEdit::actionEnabled(QWebPage::WebAction action)
  394. {
  395. QAction *act = pageAction(action);
  396. return act ? act->isEnabled() : false;
  397. }
  398. bool FbTextEdit::actionChecked(QWebPage::WebAction action)
  399. {
  400. QAction *act = pageAction(action);
  401. return act ? act->isChecked() : false;
  402. }
  403. bool FbTextEdit::BoldChecked()
  404. {
  405. return pageAction(QWebPage::ToggleBold)->isChecked();
  406. }
  407. bool FbTextEdit::ItalicChecked()
  408. {
  409. return pageAction(QWebPage::ToggleItalic)->isChecked();
  410. }
  411. bool FbTextEdit::StrikeChecked()
  412. {
  413. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  414. }
  415. bool FbTextEdit::SubChecked()
  416. {
  417. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  418. }
  419. bool FbTextEdit::SupChecked()
  420. {
  421. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  422. }
  423. void FbTextEdit::find()
  424. {
  425. FbTextFindDlg dlg(*this);
  426. dlg.exec();
  427. }
  428. void FbTextEdit::insertImage()
  429. {
  430. QString filters;
  431. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
  432. filters += tr("Portable Network Graphics (PNG) (*.png);;");
  433. filters += tr("JPEG (*.jpg *.jpeg);;");
  434. filters += tr("Graphics Interchange Format (*.gif);;");
  435. filters += tr("All Files (*)");
  436. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  437. if (path.isEmpty()) return;
  438. QFile file(path);
  439. if (!file.open(QIODevice::ReadOnly)) return;
  440. QByteArray data = file.readAll();
  441. QString name = files()->add(path, data);
  442. execCommand("insertImage", name.prepend("#"));
  443. }
  444. void FbTextEdit::insertNote()
  445. {
  446. FbNoteDlg dlg(*this);
  447. dlg.exec();
  448. }
  449. void FbTextEdit::insertLink()
  450. {
  451. bool ok;
  452. QString text = QInputDialog::getText(this, tr("Insert hyperlink"), tr("URL:"), QLineEdit::Normal, QString(), &ok);
  453. if (ok && !text.isEmpty()) execCommand("CreateLink", text);
  454. }
  455. void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
  456. {
  457. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  458. page()->mainFrame()->evaluateJavaScript(javascript);
  459. }
  460. //---------------------------------------------------------------------------
  461. // FbWebFrame
  462. //---------------------------------------------------------------------------
  463. FbWebFrame::FbWebFrame(QWidget *parent)
  464. : QFrame(parent)
  465. {
  466. setFrameShape(QFrame::StyledPanel);
  467. setFrameShadow(QFrame::Sunken);
  468. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  469. layout->setSpacing(0);
  470. layout->setMargin(0);
  471. setLayout(layout);
  472. }
  473. //---------------------------------------------------------------------------
  474. // FbTextFrame
  475. //---------------------------------------------------------------------------
  476. FbTextFrame::FbTextFrame(QWidget *parent, QAction *action)
  477. : QFrame(parent)
  478. , m_view(this, parent->parent())
  479. , m_dock(0)
  480. {
  481. setFrameShape(QFrame::StyledPanel);
  482. setFrameShadow(QFrame::Sunken);
  483. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  484. layout->setSpacing(0);
  485. layout->setMargin(0);
  486. layout->addWidget(&m_view);
  487. connect(action, SIGNAL(triggered()), SLOT(showInspector()));
  488. }
  489. FbTextFrame::~FbTextFrame()
  490. {
  491. if (m_dock) m_dock->deleteLater();
  492. }
  493. void FbTextFrame::showInspector()
  494. {
  495. if (m_dock) {
  496. m_dock->setVisible(m_dock->isHidden());
  497. return;
  498. }
  499. QMainWindow *main = qobject_cast<QMainWindow*>(parent());
  500. if (!main) return;
  501. m_dock = new QDockWidget(tr("Web inspector"), this);
  502. m_dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
  503. main->addDockWidget(Qt::BottomDockWidgetArea, m_dock);
  504. QWebInspector *inspector = new QWebInspector(this);
  505. inspector->setPage(m_view.page());
  506. m_dock->setWidget(inspector);
  507. connect(m_dock, SIGNAL(visibilityChanged(bool)), main, SIGNAL(showInspectorChecked(bool)));
  508. connect(m_dock, SIGNAL(destroyed()), SLOT(dockDestroyed()));
  509. }
  510. void FbTextFrame::hideInspector()
  511. {
  512. if (m_dock) m_dock->hide();
  513. }
  514. void FbTextFrame::dockDestroyed()
  515. {
  516. m_dock = 0;
  517. }
  518. //---------------------------------------------------------------------------
  519. // FbTextAction
  520. //---------------------------------------------------------------------------
  521. FbTextAction::FbTextAction(const QString &text, QWebPage::WebAction action, QObject* parent)
  522. : QAction(text, parent)
  523. , m_action(action)
  524. {
  525. }
  526. FbTextAction::FbTextAction(const QIcon &icon, const QString &text, QWebPage::WebAction action, QObject* parent)
  527. : QAction(icon, text, parent)
  528. , m_action(action)
  529. {
  530. }
  531. void FbTextAction::updateChecked()
  532. {
  533. // if (QAction * act = action(m_action)) setChecked(act->isChecked());
  534. }
  535. void FbTextAction::updateEnabled()
  536. {
  537. // if (QAction * act = action(m_action)) setEnabled(act->isEnabled());
  538. }