fb2text.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. setPage(p);
  192. }
  193. FbTextEdit::~FbTextEdit()
  194. {
  195. if (m_noteView) delete m_noteView;
  196. }
  197. FbTextPage *FbTextEdit::page()
  198. {
  199. return qobject_cast<FbTextPage*>(FbTextBase::page());
  200. }
  201. FbStore *FbTextEdit::store()
  202. {
  203. return page()->manager()->store();
  204. }
  205. QAction * FbTextEdit::act(Fb::Actions index) const
  206. {
  207. return m_actions[index];
  208. }
  209. QAction * FbTextEdit::pAct(QWebPage::WebAction index) const
  210. {
  211. return pageAction(index);
  212. }
  213. void FbTextEdit::setAction(Fb::Actions index, QAction *action)
  214. {
  215. m_actions[index] = action;
  216. }
  217. void FbTextEdit::connectActions(QToolBar *tool)
  218. {
  219. m_actions.connect();
  220. foreach (QAction *action, m_actions) {
  221. if (FbTextAction *a = qobject_cast<FbTextAction*>(action)) {
  222. a->connectAction();
  223. }
  224. }
  225. connect(act(Fb::EditFind), SIGNAL(triggered()), SLOT(find()));
  226. connect(act(Fb::InsertImage), SIGNAL(triggered()), SLOT(insertImage()));
  227. connect(act(Fb::InsertLink), SIGNAL(triggered()), SLOT(insertLink()));
  228. connect(act(Fb::InsertNote), SIGNAL(triggered()), SLOT(insertNote()));
  229. connect(act(Fb::ViewContents), SIGNAL(triggered(bool)), SLOT(viewContents(bool)));
  230. connect(act(Fb::ViewPictures), SIGNAL(triggered(bool)), SLOT(viewPictures(bool)));
  231. connect(act(Fb::ViewFootnotes), SIGNAL(triggered(bool)), SLOT(viewFootnotes(bool)));
  232. connect(act(Fb::ViewInspector), SIGNAL(triggered(bool)), SLOT(viewInspector(bool)));
  233. connect(act(Fb::ZoomIn), SIGNAL(triggered()), SLOT(zoomIn()));
  234. connect(act(Fb::ZoomOut), SIGNAL(triggered()), SLOT(zoomOut()));
  235. connect(act(Fb::ZoomReset), SIGNAL(triggered()), SLOT(zoomReset()));
  236. /*
  237. connect(actionTitle, SIGNAL(triggered()), textPage, SLOT(insertTitle()));
  238. connect(actionAnnot, SIGNAL(triggered()), textPage, SLOT(insertAnnot()));
  239. connect(actionAuthor, SIGNAL(triggered()), textPage, SLOT(insertAuthor()));
  240. connect(actionEpigraph, SIGNAL(triggered()), textPage, SLOT(insertEpigraph()));
  241. connect(actionSubtitle, SIGNAL(triggered()), textPage, SLOT(insertSubtitle()));
  242. connect(actionSection, SIGNAL(triggered()), textPage, SLOT(insertSection()));
  243. connect(actionStanza, SIGNAL(triggered()), textPage, SLOT(insertStanza()));
  244. connect(actionPoem, SIGNAL(triggered()), textPage, SLOT(insertPoem()));
  245. connect(actionDate, SIGNAL(triggered()), textPage, SLOT(insertDate()));
  246. connect(actionBody, SIGNAL(triggered()), textPage, SLOT(insertBody()));
  247. connect(actionSectionAdd, SIGNAL(triggered()), textPage, SLOT(createSection()));
  248. connect(actionSectionDel, SIGNAL(triggered()), textPage, SLOT(deleteSection()));
  249. connect(actionTextTitle, SIGNAL(triggered()), textPage, SLOT(createTitle()));
  250. */
  251. tool->clear();
  252. tool->addSeparator();
  253. tool->addAction(act(Fb::EditUndo));
  254. tool->addAction(act(Fb::EditRedo));
  255. tool->addSeparator();
  256. tool->addAction(act(Fb::EditCut));
  257. tool->addAction(act(Fb::EditCopy));
  258. tool->addAction(act(Fb::EditPaste));
  259. tool->addSeparator();
  260. tool->addAction(act(Fb::TextBold));
  261. tool->addAction(act(Fb::TextItalic));
  262. tool->addAction(act(Fb::TextStrike));
  263. tool->addAction(act(Fb::TextSup));
  264. tool->addAction(act(Fb::TextSub));
  265. tool->addSeparator();
  266. tool->addAction(act(Fb::SectionAdd));
  267. tool->addAction(act(Fb::SectionDel));
  268. tool->addAction(act(Fb::TextTitle));
  269. tool->addSeparator();
  270. tool->addAction(act(Fb::InsertImage));
  271. tool->addAction(act(Fb::InsertNote));
  272. tool->addAction(act(Fb::InsertLink));
  273. tool->addAction(act(Fb::InsertSection));
  274. }
  275. void FbTextEdit::disconnectActions()
  276. {
  277. m_actions.disconnect();
  278. foreach (QAction *action, m_actions) {
  279. if (FbTextAction *a = qobject_cast<FbTextAction*>(action)) {
  280. a->disconnectAction();
  281. }
  282. }
  283. viewContents(false);
  284. viewPictures(false);
  285. viewInspector(false);
  286. }
  287. void FbTextEdit::viewContents(bool show)
  288. {
  289. if (show) {
  290. if (dockTree) { dockTree->show(); return; }
  291. dockTree = new FbDockWidget(tr("Contents"), this);
  292. dockTree->setWidget(new FbTreeWidget(this, m_owner));
  293. connect(dockTree, SIGNAL(visibilityChanged(bool)), act(Fb::ViewContents), SLOT(setChecked(bool)));
  294. connect(dockTree, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  295. m_owner->addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  296. } else if (dockTree) {
  297. dockTree->deleteLater();
  298. dockTree = 0;
  299. }
  300. }
  301. void FbTextEdit::viewPictures(bool show)
  302. {
  303. if (show) {
  304. if (dockImgs) { dockImgs->show(); return; }
  305. dockImgs = new FbDockWidget(tr("Pictures"), this);
  306. dockImgs->setWidget(new FbImgsWidget(this, m_owner));
  307. connect(dockImgs, SIGNAL(visibilityChanged(bool)), act(Fb::ViewPictures), SLOT(setChecked(bool)));
  308. connect(dockImgs, SIGNAL(destroyed()), SLOT(imgsDestroyed()));
  309. m_owner->addDockWidget(Qt::RightDockWidgetArea, dockImgs);
  310. } else if (dockImgs) {
  311. dockImgs->deleteLater();
  312. dockImgs = 0;
  313. }
  314. }
  315. void FbTextEdit::viewFootnotes(bool show)
  316. {
  317. if (show) {
  318. if (dockNote) { dockNote->show(); return; }
  319. dockNote = new FbDockWidget(tr("Footnotes"), this);
  320. dockNote->setWidget(new FbNotesWidget(this, m_owner));
  321. connect(dockNote, SIGNAL(visibilityChanged(bool)), act(Fb::ViewFootnotes), SLOT(setChecked(bool)));
  322. connect(dockNote, SIGNAL(destroyed()), SLOT(noteDestroyed()));
  323. m_owner->addDockWidget(Qt::RightDockWidgetArea, dockNote);
  324. } else if (dockNote) {
  325. dockNote->deleteLater();
  326. dockNote = 0;
  327. }
  328. }
  329. void FbTextEdit::viewInspector(bool show)
  330. {
  331. if (show) {
  332. if (dockInsp) { dockInsp->show(); return; }
  333. QWebInspector *inspector = new QWebInspector(this);
  334. inspector->setPage(page());
  335. dockInsp = new QDockWidget(tr("Web inspector"), this);
  336. dockInsp->setFeatures(QDockWidget::AllDockWidgetFeatures);
  337. dockInsp->setWidget(inspector);
  338. connect(dockInsp, SIGNAL(visibilityChanged(bool)), act(Fb::ViewInspector), SLOT(setChecked(bool)));
  339. m_owner->addDockWidget(Qt::BottomDockWidgetArea, dockInsp);
  340. } else if (dockInsp) {
  341. dockInsp->hide();
  342. }
  343. }
  344. void FbTextEdit::hideDocks()
  345. {
  346. return;
  347. if (dockTree) {
  348. dockTree->deleteLater();
  349. dockTree = 0;
  350. }
  351. if (dockImgs) {
  352. dockImgs->deleteLater();
  353. dockImgs = 0;
  354. }
  355. if (dockInsp) {
  356. dockInsp->hide();
  357. }
  358. }
  359. void FbTextEdit::treeDestroyed()
  360. {
  361. m_actions[Fb::ViewContents]->setChecked(false);
  362. dockTree = 0;
  363. }
  364. void FbTextEdit::imgsDestroyed()
  365. {
  366. m_actions[Fb::ViewPictures]->setChecked(false);
  367. dockImgs = 0;
  368. }
  369. void FbTextEdit::noteDestroyed()
  370. {
  371. m_actions[Fb::ViewFootnotes]->setChecked(false);
  372. dockNote = 0;
  373. }
  374. FbNoteView & FbTextEdit::noteView()
  375. {
  376. if (m_noteView) return *m_noteView;
  377. m_noteView = new FbNoteView(qobject_cast<QWidget*>(parent()), url());
  378. m_noteView->setPage(new FbTextPage(this));
  379. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  380. m_noteView->page()->setContentEditable(false);
  381. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  382. return *m_noteView;
  383. }
  384. QWebElement FbTextEdit::body()
  385. {
  386. return doc().findFirst("body");
  387. }
  388. QWebElement FbTextEdit::doc()
  389. {
  390. return page()->mainFrame()->documentElement();
  391. }
  392. void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
  393. {
  394. m_point = event->pos();
  395. QWebView::mouseMoveEvent(event);
  396. }
  397. void FbTextEdit::contextMenu(const QPoint &pos)
  398. {
  399. QMenu menu, *submenu;
  400. submenu = menu.addMenu(tr("Fo&rmat"));
  401. submenu->addAction(pageAction(QWebPage::RemoveFormat));
  402. submenu->addSeparator();
  403. submenu->addAction(pageAction(QWebPage::ToggleBold));
  404. submenu->addAction(pageAction(QWebPage::ToggleItalic));
  405. submenu->addAction(pageAction(QWebPage::ToggleStrikethrough));
  406. submenu->addAction(pageAction(QWebPage::ToggleSuperscript));
  407. submenu->addAction(pageAction(QWebPage::ToggleSubscript));
  408. menu.addSeparator();
  409. menu.addAction(pageAction(QWebPage::Cut));
  410. menu.addAction(pageAction(QWebPage::Copy));
  411. menu.addAction(pageAction(QWebPage::Paste));
  412. menu.addAction(pageAction(QWebPage::PasteAndMatchStyle));
  413. menu.exec(mapToGlobal(pos));
  414. }
  415. void FbTextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  416. {
  417. Q_UNUSED(title);
  418. Q_UNUSED(textContent);
  419. const QString href = QUrl(link).fragment();
  420. if (href.isEmpty()) {
  421. if (m_noteView) m_noteView->hide();
  422. return;
  423. }
  424. const QString query = QString("fb\\:section#%1").arg(href);
  425. const QWebElement element = doc().findFirst(query);
  426. if (element.isNull()) {
  427. if (m_noteView) m_noteView->hide();
  428. return;
  429. }
  430. QRect rect = geometry();
  431. QSize size = element.geometry().size() + QSize(2, 4);
  432. int center = rect.size().height() / 2;
  433. int h = size.height();
  434. if (h > center) size.setHeight(center - 10);
  435. int x = (rect.size().width() - size.width()) / 2;
  436. int y = m_point.y();
  437. if ( y > h ) y = y - h - 10; else y = y + 10;
  438. QPoint point = QPoint(x, y) + rect.topLeft();
  439. noteView().hint(element, QRect(point, size));
  440. }
  441. bool FbTextEdit::save(QIODevice *device, const QString &codec)
  442. {
  443. FbSaveWriter writer(*this, device);
  444. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  445. bool ok = FbSaveHandler(writer).save();
  446. if (ok) page()->undoStack()->setClean();
  447. return ok;
  448. }
  449. bool FbTextEdit::save(QByteArray *array)
  450. {
  451. FbSaveWriter writer(*this, array);
  452. return FbSaveHandler(writer).save();
  453. }
  454. bool FbTextEdit::save(QString *string)
  455. {
  456. // Use class QByteArray instead QString
  457. // to store information about encoding.
  458. QByteArray data;
  459. bool ok = save(&data);
  460. if (ok) *string = QString::fromUtf8(data.data());
  461. return ok;
  462. }
  463. QString FbTextEdit::toHtml()
  464. {
  465. return page()->mainFrame()->toHtml();
  466. }
  467. void FbTextEdit::zoomIn()
  468. {
  469. qreal zoom = zoomFactor();
  470. setZoomFactor(zoom * 1.1);
  471. }
  472. void FbTextEdit::zoomOut()
  473. {
  474. qreal zoom = zoomFactor();
  475. setZoomFactor(zoom * 0.9);
  476. }
  477. void FbTextEdit::zoomReset()
  478. {
  479. setZoomFactor(1);
  480. }
  481. bool FbTextEdit::actionEnabled(QWebPage::WebAction action)
  482. {
  483. QAction *act = pageAction(action);
  484. return act ? act->isEnabled() : false;
  485. }
  486. bool FbTextEdit::actionChecked(QWebPage::WebAction action)
  487. {
  488. QAction *act = pageAction(action);
  489. return act ? act->isChecked() : false;
  490. }
  491. bool FbTextEdit::BoldChecked()
  492. {
  493. return pageAction(QWebPage::ToggleBold)->isChecked();
  494. }
  495. bool FbTextEdit::ItalicChecked()
  496. {
  497. return pageAction(QWebPage::ToggleItalic)->isChecked();
  498. }
  499. bool FbTextEdit::StrikeChecked()
  500. {
  501. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  502. }
  503. bool FbTextEdit::SubChecked()
  504. {
  505. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  506. }
  507. bool FbTextEdit::SupChecked()
  508. {
  509. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  510. }
  511. void FbTextEdit::find()
  512. {
  513. FbTextFindDlg dlg(*this);
  514. dlg.exec();
  515. }
  516. void FbTextEdit::insertImage()
  517. {
  518. FbImageDlg dlg(this);
  519. if (dlg.exec()) {
  520. QString name = dlg.result();
  521. if (name.isEmpty()) return;
  522. QUrl url; url.setFragment(name);
  523. execCommand("insertImage", url.toString());
  524. }
  525. }
  526. void FbTextEdit::insertNote()
  527. {
  528. FbNoteDlg dlg(this);
  529. dlg.exec();
  530. }
  531. void FbTextEdit::insertLink()
  532. {
  533. bool ok;
  534. QString text = QInputDialog::getText(this, tr("Insert hyperlink"), tr("URL:"), QLineEdit::Normal, QString(), &ok);
  535. if (ok && !text.isEmpty()) execCommand("CreateLink", text);
  536. }
  537. void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
  538. {
  539. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  540. page()->mainFrame()->evaluateJavaScript(javascript);
  541. }
  542. //---------------------------------------------------------------------------
  543. // FbTextFrame
  544. //---------------------------------------------------------------------------
  545. FbTextFrame::FbTextFrame(QWidget *parent)
  546. : QFrame(parent)
  547. {
  548. setFrameShape(QFrame::StyledPanel);
  549. setFrameShadow(QFrame::Sunken);
  550. QLayout * layout = new QVBoxLayout(this);
  551. layout->setSpacing(0);
  552. layout->setMargin(0);
  553. setLayout(layout);
  554. }