fb2text.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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()
  35. {
  36. return m_parent->pageAction(m_action);
  37. }
  38. void FbTextAction::updateAction()
  39. {
  40. if (QAction * act = action()) {
  41. if (isCheckable()) setChecked(act->isChecked());
  42. setEnabled(act->isEnabled());
  43. }
  44. }
  45. void FbTextAction::connectAction()
  46. {
  47. if (QAction * act = 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();
  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::DockWidgetClosable|QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable);
  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. for (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. for (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. #ifdef QT_DEBUG
  289. void FbTextEdit::exportHtml()
  290. {
  291. FbSaveDialog dlg(this, tr("Save As..."));
  292. dlg.selectFile("filename.htm");
  293. if (!dlg.exec()) return;
  294. QString fileName = dlg.fileName();
  295. if (fileName.isEmpty()) return;
  296. QFile file(fileName);
  297. if (!file.open(QFile::WriteOnly)) return;
  298. QTextStream out(&file);
  299. out.setCodec("UTF-8");
  300. out << toHtml();
  301. QFileInfo fileInfo(fileName);
  302. QString dirName = fileInfo.path() + "/" + fileInfo.completeBaseName() + "/";
  303. QDir().mkpath(dirName);
  304. FbNetworkAccessManager *m = qobject_cast<FbNetworkAccessManager*>(page()->networkAccessManager());
  305. if (!m) return;
  306. int count = m->count();
  307. for (int i = 0; i < count; ++i) {
  308. QFile file(dirName + m->info(i, 0).toString());
  309. if (file.open(QFile::WriteOnly)) {
  310. file.write(m->data(i));
  311. }
  312. }
  313. }
  314. #endif // QT_DEBUG
  315. void FbTextEdit::viewContents(bool show)
  316. {
  317. if (show) {
  318. if (dockTree) { dockTree->show(); return; }
  319. dockTree = new FbDockWidget(tr("Contents"), this);
  320. dockTree->setWidget(new FbTreeWidget(this, m_owner));
  321. connect(dockTree, SIGNAL(visibilityChanged(bool)), act(Fb::ViewContents), SLOT(setChecked(bool)));
  322. connect(dockTree, SIGNAL(destroyed()), SLOT(treeDestroyed()));
  323. m_owner->addDockWidget(Qt::LeftDockWidgetArea, dockTree);
  324. } else if (dockTree) {
  325. dockTree->deleteLater();
  326. dockTree = 0;
  327. }
  328. }
  329. void FbTextEdit::viewPictures(bool show)
  330. {
  331. if (show) {
  332. if (dockImgs) { dockImgs->show(); return; }
  333. dockImgs = new FbDockWidget(tr("Pictures"), this);
  334. dockImgs->setWidget(new FbImgsWidget(this, m_owner));
  335. connect(dockImgs, SIGNAL(visibilityChanged(bool)), act(Fb::ViewPictures), SLOT(setChecked(bool)));
  336. connect(dockImgs, SIGNAL(destroyed()), SLOT(imgsDestroyed()));
  337. m_owner->addDockWidget(Qt::RightDockWidgetArea, dockImgs);
  338. } else if (dockImgs) {
  339. dockImgs->deleteLater();
  340. dockImgs = 0;
  341. }
  342. }
  343. void FbTextEdit::viewFootnotes(bool show)
  344. {
  345. if (show) {
  346. if (dockNote) { dockNote->show(); return; }
  347. dockNote = new FbDockWidget(tr("Footnotes"), this);
  348. dockNote->setWidget(new FbNotesWidget(this, m_owner));
  349. connect(dockNote, SIGNAL(visibilityChanged(bool)), act(Fb::ViewFootnotes), SLOT(setChecked(bool)));
  350. connect(dockNote, SIGNAL(destroyed()), SLOT(noteDestroyed()));
  351. m_owner->addDockWidget(Qt::RightDockWidgetArea, dockNote);
  352. } else if (dockNote) {
  353. dockNote->deleteLater();
  354. dockNote = 0;
  355. }
  356. }
  357. void FbTextEdit::viewInspector(bool show)
  358. {
  359. if (show) {
  360. if (dockInsp) { dockInsp->show(); return; }
  361. QWebInspector *inspector = new QWebInspector(this);
  362. inspector->setPage(page());
  363. dockInsp = new QDockWidget(tr("Web inspector"), this);
  364. dockInsp->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable);
  365. dockInsp->setWidget(inspector);
  366. connect(dockInsp, SIGNAL(visibilityChanged(bool)), act(Fb::ViewInspector), SLOT(setChecked(bool)));
  367. m_owner->addDockWidget(Qt::BottomDockWidgetArea, dockInsp);
  368. } else if (dockInsp) {
  369. dockInsp->hide();
  370. }
  371. }
  372. void FbTextEdit::hideDocks()
  373. {
  374. return;
  375. if (dockTree) {
  376. dockTree->deleteLater();
  377. dockTree = 0;
  378. }
  379. if (dockImgs) {
  380. dockImgs->deleteLater();
  381. dockImgs = 0;
  382. }
  383. if (dockInsp) {
  384. dockInsp->hide();
  385. }
  386. }
  387. void FbTextEdit::treeDestroyed()
  388. {
  389. m_actions[Fb::ViewContents]->setChecked(false);
  390. dockTree = 0;
  391. }
  392. void FbTextEdit::imgsDestroyed()
  393. {
  394. m_actions[Fb::ViewPictures]->setChecked(false);
  395. dockImgs = 0;
  396. }
  397. void FbTextEdit::noteDestroyed()
  398. {
  399. m_actions[Fb::ViewFootnotes]->setChecked(false);
  400. dockNote = 0;
  401. }
  402. FbNoteView & FbTextEdit::noteView()
  403. {
  404. if (m_noteView) return *m_noteView;
  405. m_noteView = new FbNoteView(qobject_cast<QWidget*>(parent()), url());
  406. m_noteView->setPage(new FbTextPage(this));
  407. m_noteView->page()->setNetworkAccessManager(page()->networkAccessManager());
  408. m_noteView->page()->setContentEditable(false);
  409. m_noteView->setGeometry(QRect(100, 100, 400, 200));
  410. return *m_noteView;
  411. }
  412. QWebElement FbTextEdit::body()
  413. {
  414. return doc().findFirst("body");
  415. }
  416. QWebElement FbTextEdit::doc()
  417. {
  418. return page()->mainFrame()->documentElement();
  419. }
  420. void FbTextEdit::mouseMoveEvent(QMouseEvent *event)
  421. {
  422. m_point = event->pos();
  423. QWebView::mouseMoveEvent(event);
  424. }
  425. void FbTextEdit::cleanChanged(bool clean)
  426. {
  427. emit modificationChanged(!clean);
  428. }
  429. void FbTextEdit::contextMenu(const QPoint &pos)
  430. {
  431. QMenu menu, *submenu;
  432. submenu = menu.addMenu(tr("Fo&rmat"));
  433. submenu->addAction(pageAction(QWebPage::RemoveFormat));
  434. submenu->addSeparator();
  435. submenu->addAction(pageAction(QWebPage::ToggleBold));
  436. submenu->addAction(pageAction(QWebPage::ToggleItalic));
  437. submenu->addAction(pageAction(QWebPage::ToggleStrikethrough));
  438. submenu->addAction(pageAction(QWebPage::ToggleSuperscript));
  439. submenu->addAction(pageAction(QWebPage::ToggleSubscript));
  440. menu.addSeparator();
  441. menu.addAction(pageAction(QWebPage::Cut));
  442. menu.addAction(pageAction(QWebPage::Copy));
  443. menu.addAction(pageAction(QWebPage::Paste));
  444. menu.addAction(pageAction(QWebPage::PasteAndMatchStyle));
  445. menu.exec(mapToGlobal(pos));
  446. }
  447. void FbTextEdit::linkHovered(const QString &link, const QString &title, const QString &textContent)
  448. {
  449. Q_UNUSED(title);
  450. Q_UNUSED(textContent);
  451. const QString href = QUrl(link).fragment();
  452. if (href.isEmpty()) {
  453. if (m_noteView) m_noteView->hide();
  454. return;
  455. }
  456. const QString query = QString("fb\\:section#%1").arg(href);
  457. const QWebElement element = doc().findFirst(query);
  458. if (element.isNull()) {
  459. if (m_noteView) m_noteView->hide();
  460. return;
  461. }
  462. QRect rect = geometry();
  463. QSize size = element.geometry().size() + QSize(2, 4);
  464. int center = rect.size().height() / 2;
  465. int h = size.height();
  466. if (h > center) size.setHeight(center - 10);
  467. int x = (rect.size().width() - size.width()) / 2;
  468. int y = m_point.y();
  469. if ( y > h ) y = y - h - 10; else y = y + 10;
  470. QPoint point = QPoint(x, y) + rect.topLeft();
  471. noteView().hint(element, QRect(point, size));
  472. }
  473. bool FbTextEdit::save(QIODevice *device, const QString &codec)
  474. {
  475. FbSaveWriter writer(*this, device);
  476. if (!codec.isEmpty()) writer.setCodec(codec.toLatin1());
  477. bool ok = FbSaveHandler(writer).save();
  478. if (ok) page()->undoStack()->setClean();
  479. return ok;
  480. }
  481. bool FbTextEdit::save(QByteArray *array)
  482. {
  483. FbSaveWriter writer(*this, array);
  484. return FbSaveHandler(writer).save();
  485. }
  486. bool FbTextEdit::save(QString *string, int &anchor, int &focus)
  487. {
  488. FbSaveWriter writer(*this, string);
  489. bool ok = FbSaveHandler(writer).save();
  490. anchor = writer.anchor();
  491. focus = writer.focus();
  492. return ok;
  493. }
  494. QString FbTextEdit::toHtml()
  495. {
  496. return page()->mainFrame()->toHtml();
  497. }
  498. void FbTextEdit::zoomIn()
  499. {
  500. qreal zoom = zoomFactor();
  501. setZoomFactor(zoom * 1.1);
  502. }
  503. void FbTextEdit::zoomOut()
  504. {
  505. qreal zoom = zoomFactor();
  506. setZoomFactor(zoom * 0.9);
  507. }
  508. void FbTextEdit::zoomReset()
  509. {
  510. setZoomFactor(1);
  511. }
  512. bool FbTextEdit::actionEnabled(QWebPage::WebAction action)
  513. {
  514. QAction *act = pageAction(action);
  515. return act ? act->isEnabled() : false;
  516. }
  517. bool FbTextEdit::actionChecked(QWebPage::WebAction action)
  518. {
  519. QAction *act = pageAction(action);
  520. return act ? act->isChecked() : false;
  521. }
  522. bool FbTextEdit::BoldChecked()
  523. {
  524. return pageAction(QWebPage::ToggleBold)->isChecked();
  525. }
  526. bool FbTextEdit::ItalicChecked()
  527. {
  528. return pageAction(QWebPage::ToggleItalic)->isChecked();
  529. }
  530. bool FbTextEdit::StrikeChecked()
  531. {
  532. return pageAction(QWebPage::ToggleStrikethrough)->isChecked();
  533. }
  534. bool FbTextEdit::SubChecked()
  535. {
  536. return pageAction(QWebPage::ToggleSubscript)->isChecked();
  537. }
  538. bool FbTextEdit::SupChecked()
  539. {
  540. return pageAction(QWebPage::ToggleSuperscript)->isChecked();
  541. }
  542. void FbTextEdit::find()
  543. {
  544. FbTextFindDlg dlg(*this);
  545. dlg.exec();
  546. }
  547. void FbTextEdit::insertImage()
  548. {
  549. FbImageDlg dlg(this);
  550. if (dlg.exec()) {
  551. QString name = dlg.result();
  552. if (name.isEmpty()) return;
  553. QUrl url; url.setFragment(name);
  554. execCommand("insertImage", url.toString());
  555. }
  556. }
  557. void FbTextEdit::insertNote()
  558. {
  559. FbNoteDlg dlg(this);
  560. dlg.exec();
  561. }
  562. void FbTextEdit::insertLink()
  563. {
  564. bool ok;
  565. QString text = QInputDialog::getText(this, tr("Insert hyperlink"), tr("URL:"), QLineEdit::Normal, QString(), &ok);
  566. if (ok && !text.isEmpty()) execCommand("CreateLink", text);
  567. }
  568. void FbTextEdit::execCommand(const QString &cmd, const QString &arg)
  569. {
  570. QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
  571. page()->mainFrame()->evaluateJavaScript(javascript);
  572. }
  573. //---------------------------------------------------------------------------
  574. // FbTextFrame
  575. //---------------------------------------------------------------------------
  576. FbTextFrame::FbTextFrame(QWidget *parent)
  577. : QFrame(parent)
  578. {
  579. setFrameShape(QFrame::StyledPanel);
  580. setFrameShadow(QFrame::Sunken);
  581. QLayout * layout = new QVBoxLayout(this);
  582. layout->setSpacing(0);
  583. layout->setMargin(0);
  584. setLayout(layout);
  585. }