fb2text.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifndef FB2TEXT_H
  2. #define FB2TEXT_H
  3. #include <QFrame>
  4. #include <QResizeEvent>
  5. #include <QTimer>
  6. #include <QThread>
  7. #include <QWebElement>
  8. #include <QWebView>
  9. #include "fb2temp.hpp"
  10. QT_BEGIN_NAMESPACE
  11. class QDockWidget;
  12. class QToolBar;
  13. class QUndoCommand;
  14. class QWebInspector;
  15. QT_END_NAMESPACE
  16. class FbNoteView;
  17. class FbReadThread;
  18. class FbTextElement;
  19. class FbTextBase : public QWebView
  20. {
  21. Q_OBJECT
  22. public:
  23. FbTextBase(QWidget* parent = 0)
  24. : QWebView(parent)
  25. {
  26. m_timer.setInterval(100);
  27. m_timer.setSingleShot(true);
  28. connect(&m_timer, SIGNAL(timeout()), SLOT(doResize()));
  29. }
  30. void addTools(QToolBar *tool);
  31. protected slots:
  32. void doResize() {
  33. QResizeEvent event(size(), m_size);
  34. QWebView::resizeEvent(&event);
  35. QWebView::update();
  36. }
  37. protected:
  38. void resizeEvent(QResizeEvent* event) {
  39. if (!m_timer.isActive()) m_size = event->oldSize();
  40. m_timer.start();
  41. }
  42. void keyPressEvent(QKeyEvent *event) {
  43. if (event->key() == Qt::Key_Escape) return;
  44. QWebView::keyPressEvent(event);
  45. }
  46. private:
  47. QTimer m_timer;
  48. QSize m_size;
  49. };
  50. class FbTextPage : public QWebPage
  51. {
  52. Q_OBJECT
  53. public:
  54. explicit FbTextPage(QObject *parent = 0);
  55. void push(QUndoCommand * command, const QString &text = QString());
  56. FbTextElement element(const QString &location);
  57. FbTextElement current();
  58. QString location();
  59. QString status();
  60. FbTextElement body();
  61. FbTextElement doc();
  62. void appendSection(const FbTextElement &parent);
  63. public slots:
  64. void insertBody();
  65. void insertTitle();
  66. void insertAnnot();
  67. void insertAuthor();
  68. void insertEpigraph();
  69. void insertSubtitle();
  70. void insertSection();
  71. void insertPoem();
  72. void insertStanza();
  73. void insertDate();
  74. protected:
  75. virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
  76. protected:
  77. static QString div(const QString &style, const QString &text);
  78. static QString p(const QString &text = "<br/>");
  79. void update();
  80. };
  81. class FbTextEdit : public FbTextBase
  82. {
  83. Q_OBJECT
  84. public:
  85. explicit FbTextEdit(QWidget *parent = 0);
  86. virtual ~FbTextEdit();
  87. FbTextPage * page();
  88. FbNetworkAccessManager & files() { return m_files; }
  89. void load(const QString &filename, const QString &xml = QString());
  90. bool save(QIODevice *device, const QString &codec = QString());
  91. bool save(QByteArray *array);
  92. bool save(QString *string);
  93. bool UndoEnabled();
  94. bool RedoEnabled();
  95. bool CutEnabled();
  96. bool CopyEnabled();
  97. bool BoldChecked();
  98. bool ItalicChecked();
  99. bool StrikeChecked();
  100. bool SubChecked();
  101. bool SupChecked();
  102. protected:
  103. virtual void mouseMoveEvent(QMouseEvent *event);
  104. public slots:
  105. void html(QString html);
  106. void data(QString name, QByteArray data);
  107. void linkHovered(const QString &link, const QString &title, const QString &textContent);
  108. void insertImage();
  109. void insertNote();
  110. void insertLink();
  111. void zoomIn();
  112. void zoomOut();
  113. void zoomReset();
  114. void find();
  115. private slots:
  116. void fixContents();
  117. void loadFinished();
  118. private:
  119. void execCommand(const QString &cmd, const QString &arg);
  120. void exec(QUndoCommand *command);
  121. FbTemporaryFile * file(const QString &name);
  122. FbNoteView & noteView();
  123. QWebElement body();
  124. QWebElement doc();
  125. private:
  126. FbNetworkAccessManager m_files;
  127. FbNoteView *m_noteView;
  128. FbReadThread *m_thread;
  129. QPoint m_point;
  130. };
  131. class FbTextFrame : public QFrame
  132. {
  133. Q_OBJECT
  134. public:
  135. explicit FbTextFrame(QWidget* parent = 0);
  136. ~FbTextFrame();
  137. void hideInspector();
  138. FbTextEdit view;
  139. public slots:
  140. void showInspector();
  141. private:
  142. QDockWidget * dock;
  143. };
  144. #endif // FB2TEXT_H