fb2text.hpp 3.7 KB

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