fb2tree.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. int count() const {
  39. return m_list.size();
  40. }
  41. FbTextElement element() const {
  42. return m_element;
  43. }
  44. FbTreeItem * parent() const {
  45. return m_parent;
  46. }
  47. const QString & name() const {
  48. return m_name;
  49. }
  50. QPoint pos() const {
  51. return m_element.geometry().topLeft();
  52. }
  53. FbTreeItem * content(int number) const;
  54. QString selector() const;
  55. QString text() const;
  56. void init();
  57. private:
  58. QString title();
  59. private:
  60. FbTreeList m_list;
  61. QWebElement m_element;
  62. QString m_name;
  63. QString m_text;
  64. QString m_body;
  65. FbTreeItem * m_parent;
  66. int m_number;
  67. };
  68. class FbTreeModel: public QAbstractItemModel
  69. {
  70. Q_OBJECT
  71. public:
  72. explicit FbTreeModel(FbTextEdit &view, QObject *parent = 0);
  73. virtual ~FbTreeModel();
  74. QModelIndex index(FbTreeItem *item, int column = 0) const;
  75. QModelIndex index(const QString &location) const;
  76. FbTextEdit & view() { return m_view; }
  77. void selectText(const QModelIndex &index);
  78. QModelIndex move(const QModelIndex &index, int dx, int dy);
  79. QModelIndex append(const QModelIndex &parent, FbTextElement element);
  80. FbTreeItem * item(const QModelIndex &index) const;
  81. void update();
  82. public:
  83. virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
  84. virtual QModelIndex parent(const QModelIndex &child) const;
  85. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
  86. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
  87. virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  88. virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
  89. private:
  90. void update(FbTreeItem &item);
  91. private:
  92. FbTextEdit & m_view;
  93. FbTreeItem * m_root;
  94. };
  95. class FbTreeView : public QTreeView
  96. {
  97. Q_OBJECT
  98. public:
  99. explicit FbTreeView(FbTextEdit &view, QWidget *parent = 0);
  100. void initActions(QToolBar *toolbar);
  101. public slots:
  102. void updateTree();
  103. private slots:
  104. void activated(const QModelIndex &index);
  105. void contextMenu(const QPoint &pos);
  106. void contentsChanged();
  107. void selectionChanged();
  108. void selectTree();
  109. void insertNode();
  110. void deleteNode();
  111. void moveUp();
  112. void moveDown();
  113. void moveLeft();
  114. void moveRight();
  115. protected:
  116. void keyPressEvent(QKeyEvent *event);
  117. private:
  118. void moveCurrent(int dx, int dy);
  119. FbTreeModel * model();
  120. private:
  121. FbTextEdit & m_view;
  122. QTimer m_timerSelect;
  123. QTimer m_timerUpdate;
  124. QMenu m_menu;
  125. QAction
  126. *actionCut,
  127. *actionCopy,
  128. *actionPaste;
  129. };
  130. class FbTreeWidget : public QWidget
  131. {
  132. Q_OBJECT
  133. public:
  134. explicit FbTreeWidget(FbTextEdit &view, QWidget* parent = 0);
  135. protected:
  136. QToolBar * m_tool;
  137. FbTreeView * m_tree;
  138. };
  139. #endif // FB2TREE_H