fb2text.cpp 19 KB

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