fb2text.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. }
  283. void FbTextEdit::viewContents(bool show)
  284. {
  285. if (show) {
  286. if (dockTree) { dockTree->show(); return; }
  287. dockTree = new FbDockWidget(tr("Contents"), this);
  288. dockTree->setWidget(new FbTreeWidget(this, m_owner));
  289. connect(dockTree, SIGNAL(visibilityChanged(bool)), act(Fb::ViewContents), SLOT(setChecked(bool)));
  290. connect(dockTree, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  291. m_owner->addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  292. } else {
  293. dockTree->deleteLater();
  294. dockTree = 0;
  295. }
  296. }
  297. void FbTextEdit::viewPictures(bool show)
  298. {
  299. if (show) {
  300. if (dockImgs) { dockImgs->show(); return; }
  301. dockImgs = new FbDockWidget(tr("Pictures"), this);
  302. dockImgs->setWidget(new FbListWidget(this, m_owner));
  303. connect(dockImgs, SIGNAL(visibilityChanged(bool)), act(Fb::ViewPictures), SLOT(setChecked(bool)));
  304. connect(dockImgs, SIGNAL(destroyed()), SLOT(imgsDestroyed()));
  305. m_owner->addDockWidget(Qt::RightDockWidgetArea, dockImgs);
  306. } else {
  307. dockImgs->deleteLater();
  308. dockImgs = 0;
  309. }
  310. }
  311. void FbTextEdit::viewInspector(bool show)
  312. {
  313. if (show) {
  314. if (dockInsp) { dockInsp->show(); return; }
  315. QWebInspector *inspector = new QWebInspector(this);
  316. inspector->setPage(page());
  317. dockInsp = new QDockWidget(tr("Web inspector"), this);
  318. dockInsp->setFeatures(QDockWidget::AllDockWidgetFeatures);
  319. dockInsp->setWidget(inspector);
  320. connect(dockInsp, SIGNAL(visibilityChanged(bool)), act(Fb::ViewInspector), SLOT(setChecked(bool)));
  321. m_owner->addDockWidget(Qt::BottomDockWidgetArea, dockInsp);
  322. } else {
  323. dockImgs->hide();
  324. }
  325. }
  326. void FbTextEdit::hideDocks()
  327. {
  328. if (dockTree) {
  329. dockTree->deleteLater();
  330. dockTree = 0;
  331. }
  332. if (dockImgs) {
  333. dockImgs->deleteLater();
  334. dockImgs = 0;
  335. }
  336. if (dockInsp) {
  337. dockInsp->hide();
  338. }
  339. }
  340. void FbTextEdit::treeDestroyed()
  341. {
  342. m_actions[Fb::ViewContents]->setChecked(false);
  343. dockTree = 0;
  344. }
  345. void FbTextEdit::imgsDestroyed()
  346. {
  347. m_actions[Fb::ViewPictures]->setChecked(false);
  348. dockImgs = 0;
  349. }
  350. FbNoteView & FbTextEdit::noteView()
  351. {
  352. if (m_noteView) return *m_noteView;
  353. m_noteView = new FbNoteView(qobject_cast<QWidget*>(parent()), url());
  354. m_noteView->setPage(new FbTextPage(this));
  355. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  356. m_noteView->page()->setContentEditable(false);
  357. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  358. return *m_noteView;
  359. }
  360. QWebElement FbTextEdit::body()
  361. {
  362. return doc().findFirst("body");
  363. }
  364. QWebElement FbTextEdit::doc()
  365. {
  366. return page()->mainFrame()->documentElement();
  367. }
  368. void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
  369. {
  370. m_point = event->pos();
  371. QWebView::mouseMoveEvent(event);
  372. }
  373. void FbTextEdit::contextMenu(const QPoint &pos)
  374. {
  375. QMenu menu, *submenu;
  376. submenu = menu.addMenu(tr("Fo&rmat"));
  377. submenu->addAction(pageAction(QWebPage::RemoveFormat));
  378. submenu->addSeparator();
  379. submenu->addAction(pageAction(QWebPage::ToggleBold));
  380. submenu->addAction(pageAction(QWebPage::ToggleItalic));
  381. submenu->addAction(pageAction(QWebPage::ToggleStrikethrough));
  382. submenu->addAction(pageAction(QWebPage::ToggleSuperscript));
  383. submenu->addAction(pageAction(QWebPage::ToggleSubscript));
  384. menu.addSeparator();
  385. menu.addAction(pageAction(QWebPage::Cut));
  386. menu.addAction(pageAction(QWebPage::Copy));
  387. menu.addAction(pageAction(QWebPage::Paste));
  388. menu.addAction(pageAction(QWebPage::PasteAndMatchStyle));
  389. menu.exec(mapToGlobal(pos));
  390. }
  391. void FbTextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  392. {
  393. Q_UNUSED(title);
  394. Q_UNUSED(textContent);
  395. const QString href = QUrl(link).fragment();
  396. if (href.isEmpty()) {
  397. if (m_noteView) m_noteView->hide();
  398. return;
  399. }
  400. const QString query = QString("fb\\:section#%1").arg(href);
  401. const QWebElement element = doc().findFirst(query);
  402. if (element.isNull()) {
  403. if (m_noteView) m_noteView->hide();
  404. return;
  405. }
  406. QRect rect = geometry();
  407. QSize size = element.geometry().size() + QSize(2, 4);
  408. int center = rect.size().height() / 2;
  409. int h = size.height();
  410. if (h > center) size.setHeight(center - 10);
  411. int x = (rect.size().width() - size.width()) / 2;
  412. int y = m_point.y();
  413. if ( y > h ) y = y - h - 10; else y = y + 10;
  414. QPoint point = QPoint(x, y) + rect.topLeft();
  415. noteView().hint(element, QRect(point, size));
  416. }
  417. void FbTextEdit::html(QString html)
  418. {
  419. /*
  420. if (!m_thread) return;
  421. static int number = 0;
  422. QWebSettings::clearMemoryCaches();
  423. QUrl url(QString("fb2:/%1/").arg(number++));
  424. FbTextPage *page = m_thread->page();
  425. setPage(page);
  426. page->setParent(this);
  427. page->temp()->setPath(url.path());
  428. setHtml(html, url);
  429. connect(page, SIGNAL(linkHovered(QString,QString,QString)), SLOT(linkHovered(QString,QString,QString)));
  430. m_thread->deleteLater();
  431. m_thread = 0;
  432. */
  433. }
  434. bool FbTextEdit::save(QIODevice *device, const QString &codec)
  435. {
  436. FbSaveWriter writer(*this, device);
  437. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  438. bool ok = FbSaveHandler(writer).save();
  439. if (ok) page()->undoStack()->setClean();
  440. return ok;
  441. }
  442. bool FbTextEdit::save(QByteArray *array)
  443. {
  444. FbSaveWriter writer(*this, array);
  445. return FbSaveHandler(writer).save();
  446. }
  447. bool FbTextEdit::save(QString *string)
  448. {
  449. // Use class QByteArray instead QString
  450. // to store information about encoding.
  451. QByteArray data;
  452. bool ok = save(&data);
  453. if (ok) *string = QString::fromUtf8(data.data());
  454. return ok;
  455. }
  456. void FbTextEdit::data(QString name, QByteArray data)
  457. {
  458. files()->data(name, data);
  459. }
  460. void FbTextEdit::zoomIn()
  461. {
  462. qreal zoom = zoomFactor();
  463. setZoomFactor(zoom * 1.1);
  464. }
  465. void FbTextEdit::zoomOut()
  466. {
  467. qreal zoom = zoomFactor();
  468. setZoomFactor(zoom * 0.9);
  469. }
  470. void FbTextEdit::zoomReset()
  471. {
  472. setZoomFactor(1);
  473. }
  474. bool FbTextEdit::actionEnabled(QWebPage::WebAction action)
  475. {
  476. QAction *act = pageAction(action);
  477. return act ? act->isEnabled() : false;
  478. }
  479. bool FbTextEdit::actionChecked(QWebPage::WebAction action)
  480. {
  481. QAction *act = pageAction(action);
  482. return act ? act->isChecked() : false;
  483. }
  484. bool FbTextEdit::BoldChecked()
  485. {
  486. return pageAction(QWebPage::ToggleBold)->isChecked();
  487. }
  488. bool FbTextEdit::ItalicChecked()
  489. {
  490. return pageAction(QWebPage::ToggleItalic)->isChecked();
  491. }
  492. bool FbTextEdit::StrikeChecked()
  493. {
  494. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  495. }
  496. bool FbTextEdit::SubChecked()
  497. {
  498. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  499. }
  500. bool FbTextEdit::SupChecked()
  501. {
  502. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  503. }
  504. void FbTextEdit::find()
  505. {
  506. FbTextFindDlg dlg(*this);
  507. dlg.exec();
  508. }
  509. void FbTextEdit::insertImage()
  510. {
  511. QString filters;
  512. filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
  513. filters += tr("Portable Network Graphics (PNG) (*.png);;");
  514. filters += tr("JPEG (*.jpg *.jpeg);;");
  515. filters += tr("Graphics Interchange Format (*.gif);;");
  516. filters += tr("All Files (*)");
  517. QString path = QFileDialog::getOpenFileName(this, tr("Insert image..."), QString(), filters);
  518. if (path.isEmpty()) return;
  519. QFile file(path);
  520. if (!file.open(QIODevice::ReadOnly)) return;
  521. QByteArray data = file.readAll();
  522. QString name = files()->add(path, data);
  523. execCommand("insertImage", name.prepend("#"));
  524. }
  525. void FbTextEdit::insertNote()
  526. {
  527. FbNoteDlg dlg(*this);
  528. dlg.exec();
  529. }
  530. void FbTextEdit::insertLink()
  531. {
  532. bool ok;
  533. QString text = QInputDialog::getText(this, tr("Insert hyperlink"), tr("URL:"), QLineEdit::Normal, QString(), &ok);
  534. if (ok && !text.isEmpty()) execCommand("CreateLink", text);
  535. }
  536. void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
  537. {
  538. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  539. page()->mainFrame()->evaluateJavaScript(javascript);
  540. }
  541. //---------------------------------------------------------------------------
  542. // FbTextFrame
  543. //---------------------------------------------------------------------------
  544. FbTextFrame::FbTextFrame(QWidget *parent)
  545. : QFrame(parent)
  546. {
  547. setFrameShape(QFrame::StyledPanel);
  548. setFrameShadow(QFrame::Sunken);
  549. QLayout * layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
  550. layout->setSpacing(0);
  551. layout->setMargin(0);
  552. setLayout(layout);
  553. }