1
0

fb2tree.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef FB2TREE_H
  2. #define FB2TREE_H
  3. #include <QAbstractItemModel>
  4. #include <QMenu>
  5. #include <QTreeView>
  6. #include <QTimer>
  7. #include <QToolBar>
  8. #include "fb2html.h"
  9. QT_BEGIN_NAMESPACE
  10. class QAction;
  11. QT_END_NAMESPACE
  12. class FbTextEdit;
  13. class FbTreeItem;
  14. class FbTreeModel;
  15. typedef QList<FbTreeItem*> FbTreeList;
  16. class FbTreeItem: public QObject
  17. {
  18. Q_OBJECT
  19. public:
  20. explicit FbTreeItem(QWebElement &element, FbTreeItem *parent = 0, int index = 0);
  21. virtual ~FbTreeItem();
  22. FbTreeItem * item(const QModelIndex &index) const;
  23. FbTreeItem * item(int row) const;
  24. FbTreeItem & operator=(const QWebElement &element) {
  25. m_element = element;
  26. return *this;
  27. }
  28. int index(FbTreeItem * child) const {
  29. return m_list.indexOf(child);
  30. }
  31. void insert(FbTreeItem * child, int row) {
  32. m_list.insert(row, child);
  33. child->m_parent = this;
  34. }
  35. FbTreeItem * takeAt(int row) {
  36. return m_list.takeAt(row);
  37. }
  38. bool hasChildren() {
  39. return m_list.size();
  40. }
  41. int count() const {
  42. return m_list.size();
  43. }
  44. FbTextElement element() const {
  45. return m_element;
  46. }
  47. FbTreeItem * parent() const {
  48. return m_parent;
  49. }
  50. const QString & name() const {
  51. return m_name;
  52. }
  53. QPoint pos() const {
  54. return m_element.geometry().topLeft();
  55. }
  56. FbTreeItem * content(int number) const;
  57. QString selector() const;
  58. QString text() const;
  59. void init();
  60. private:
  61. QString title();
  62. private:
  63. FbTreeList m_list;
  64. QWebElement m_element;
  65. QString m_name;
  66. QString m_text;
  67. QString m_body;
  68. FbTreeItem * m_parent;
  69. int m_number;
  70. };
  71. class FbTreeModel: public QAbstractItemModel
  72. {
  73. Q_OBJECT
  74. public:
  75. explicit FbTreeModel(FbTextEdit &view, QObject *parent = 0);
  76. virtual ~FbTreeModel();
  77. QModelIndex index(FbTreeItem *item, int column = 0) const;
  78. QModelIndex index(const QString &location) const;
  79. FbTextEdit & view() { return m_view; }
  80. void selectText(const QModelIndex &index);
  81. QModelIndex move(const QModelIndex &index, int dx, int dy);
  82. QModelIndex append(const QModelIndex &parent, FbTextElement element);
  83. FbTreeItem * item(const QModelIndex &index) const;
  84. void update();
  85. public:
  86. virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
  87. virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
  88. virtual QModelIndex parent(const QModelIndex &child) const;
  89. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
  90. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
  91. virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  92. virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
  93. private:
  94. void update(FbTreeItem &item);
  95. private:
  96. FbTextEdit & m_view;
  97. FbTreeItem * m_root;
  98. };
  99. class FbTreeView : public QTreeView
  100. {
  101. Q_OBJECT
  102. public:
  103. explicit FbTreeView(FbTextEdit &view, QWidget *parent = 0);
  104. void initActions(QToolBar *toolbar);
  105. private slots:
  106. void connectPage();
  107. void updateTree();
  108. void activated(const QModelIndex &index);
  109. void contextMenu(const QPoint &pos);
  110. void contentsChanged();
  111. void selectionChanged();
  112. void selectTree();
  113. void insertSection();
  114. void insertTitle();
  115. void insertAuthor();
  116. void insertEpigraph();
  117. void insertImage();
  118. void insertAnnot();
  119. void insertStanza();
  120. void insertDate();
  121. void insertText();
  122. void deleteNode();
  123. void moveUp();
  124. void moveDown();
  125. void moveLeft();
  126. void moveRight();
  127. protected:
  128. void keyPressEvent(QKeyEvent *event);
  129. private:
  130. void append(const QModelIndex &parent, FbTextElement element);
  131. void moveCurrent(int dx, int dy);
  132. FbTreeModel * model() const;
  133. private:
  134. FbTextEdit & m_view;
  135. QTimer m_timerSelect;
  136. QTimer m_timerUpdate;
  137. QAction
  138. *actionSection,
  139. *actionDelete,
  140. *actionTitle,
  141. *actionAuthor,
  142. *actionEpigraph,
  143. *actionStanza,
  144. *actionImage,
  145. *actionAnnot,
  146. *actionText,
  147. *actionDate,
  148. *actionCut,
  149. *actionCopy,
  150. *actionPaste,
  151. *actionMoveUp,
  152. *actionMoveDown,
  153. *actionMoveLeft,
  154. *actionMoveRight;
  155. };
  156. class FbTreeWidget : public QWidget
  157. {
  158. Q_OBJECT
  159. public:
  160. explicit FbTreeWidget(FbTextEdit *view, QWidget* parent = 0);
  161. protected:
  162. QToolBar * m_tool;
  163. FbTreeView * m_tree;
  164. };
  165. #endif // FB2TREE_H