fb2text.cpp 20 KB

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