fb2text.cpp 19 KB

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