1
0

fb2text.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef FB2TEXT_H
  2. #define FB2TEXT_H
  3. #include <QAction>
  4. #include <QFrame>
  5. #include <QResizeEvent>
  6. #include <QTimer>
  7. #include <QThread>
  8. #include <QWebElement>
  9. #include <QWebView>
  10. #include "fb2temp.hpp"
  11. QT_BEGIN_NAMESPACE
  12. class QDockWidget;
  13. class QToolBar;
  14. class QUndoCommand;
  15. class QWebInspector;
  16. QT_END_NAMESPACE
  17. class FbNoteView;
  18. class FbReadThread;
  19. class FbTextElement;
  20. class FbTextLogger : public QObject
  21. {
  22. Q_OBJECT
  23. public:
  24. explicit FbTextLogger(QObject *parent = 0) : QObject(parent) {}
  25. public slots:
  26. void trace(const QString &text);
  27. };
  28. class FbTextBase : public QWebView
  29. {
  30. Q_OBJECT
  31. public:
  32. FbTextBase(QWidget *parent = 0)
  33. : QWebView(parent)
  34. {
  35. m_timer.setInterval(100);
  36. m_timer.setSingleShot(true);
  37. connect(&m_timer, SIGNAL(timeout()), SLOT(doResize()));
  38. }
  39. void addTools(QToolBar *tool);
  40. protected slots:
  41. void doResize() {
  42. QResizeEvent event(size(), m_size);
  43. QWebView::resizeEvent(&event);
  44. QWebView::update();
  45. }
  46. protected:
  47. void resizeEvent(QResizeEvent* event) {
  48. if (!m_timer.isActive()) m_size = event->oldSize();
  49. m_timer.start();
  50. }
  51. void keyPressEvent(QKeyEvent *event) {
  52. if (event->key() == Qt::Key_Escape) return;
  53. QWebView::keyPressEvent(event);
  54. }
  55. private:
  56. QTimer m_timer;
  57. QSize m_size;
  58. };
  59. class FbTextPage : public QWebPage
  60. {
  61. Q_OBJECT
  62. public:
  63. explicit FbTextPage(QObject *parent = 0);
  64. FbNetworkAccessManager *temp();
  65. void push(QUndoCommand * command, const QString &text = QString());
  66. FbTextElement element(const QString &location);
  67. FbTextElement current();
  68. QString location();
  69. QString status();
  70. FbTextElement body();
  71. FbTextElement doc();
  72. FbTextElement appendSection(const FbTextElement &parent);
  73. FbTextElement appendTitle(const FbTextElement &parent);
  74. public slots:
  75. void insertBody();
  76. void insertTitle();
  77. void insertAnnot();
  78. void insertAuthor();
  79. void insertEpigraph();
  80. void insertSubtitle();
  81. void insertSection();
  82. void insertPoem();
  83. void insertStanza();
  84. void insertDate();
  85. void insertText();
  86. void createSection();
  87. void deleteSection();
  88. void createTitle();
  89. protected:
  90. virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
  91. void createBlock(const QString &name);
  92. protected:
  93. static QString block(const QString &name);
  94. static QString block(const QString &name, const QString &text);
  95. static QString p(const QString &text = "<br/>");
  96. void update();
  97. private slots:
  98. void loadFinished();
  99. void fixContents();
  100. private:
  101. FbTextLogger m_logger;
  102. };
  103. class FbTextEdit : public FbTextBase
  104. {
  105. Q_OBJECT
  106. public:
  107. explicit FbTextEdit(QWidget *parent = 0);
  108. virtual ~FbTextEdit();
  109. FbTextPage *page();
  110. FbNetworkAccessManager *files();
  111. void load(const QString &filename, const QString &xml = QString());
  112. bool save(QIODevice *device, const QString &codec = QString());
  113. bool save(QByteArray *array);
  114. bool save(QString *string);
  115. bool actionEnabled(QWebPage::WebAction action);
  116. bool actionChecked(QWebPage::WebAction action);
  117. bool BoldChecked();
  118. bool ItalicChecked();
  119. bool StrikeChecked();
  120. bool SubChecked();
  121. bool SupChecked();
  122. protected:
  123. virtual void mouseMoveEvent(QMouseEvent *event);
  124. public slots:
  125. void html(QString html);
  126. void data(QString name, QByteArray data);
  127. void insertImage();
  128. void insertNote();
  129. void insertLink();
  130. void zoomIn();
  131. void zoomOut();
  132. void zoomReset();
  133. void find();
  134. private slots:
  135. void linkHovered(const QString &link, const QString &title, const QString &textContent);
  136. void contextMenu(const QPoint &pos);
  137. private:
  138. void execCommand(const QString &cmd, const QString &arg);
  139. void exec(QUndoCommand *command);
  140. FbTemporaryFile * file(const QString &name);
  141. FbNoteView & noteView();
  142. QWebElement body();
  143. QWebElement doc();
  144. private:
  145. FbNoteView *m_noteView;
  146. FbReadThread *m_thread;
  147. QPoint m_point;
  148. };
  149. class FbWebFrame : public QFrame
  150. {
  151. Q_OBJECT
  152. public:
  153. explicit FbWebFrame(QWidget *parent = 0);
  154. QWebView * view() { return &m_view; }
  155. private:
  156. QWebView m_view;
  157. };
  158. class FbTextFrame : public QFrame
  159. {
  160. Q_OBJECT
  161. public:
  162. explicit FbTextFrame(QWidget *parent, QAction *action = 0);
  163. ~FbTextFrame();
  164. public:
  165. FbTextEdit * view() { return &m_view; }
  166. public slots:
  167. void showInspector();
  168. void hideInspector();
  169. private slots:
  170. void dockDestroyed();
  171. private:
  172. FbTextEdit m_view;
  173. QDockWidget *m_dock;
  174. };
  175. class FbTextAction : public QAction
  176. {
  177. Q_OBJECT
  178. public:
  179. explicit FbTextAction(const QString &text, QWebPage::WebAction action, QObject* parent);
  180. explicit FbTextAction(const QIcon &icon, const QString &text, QWebPage::WebAction action, QObject* parent);
  181. public slots:
  182. void updateChecked();
  183. void updateEnabled();
  184. private:
  185. QAction * action(QWebPage::WebAction action);
  186. private:
  187. QWebPage::WebAction m_action;
  188. };
  189. #endif // FB2TEXT_H