fb2text.cpp 20 KB

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