fb2tree.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef FB2TREE_HPP
  2. #define FB2TREE_HPP
  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 Fb2TextEdit;
  13. class Fb2TreeModel;
  14. class Fb2TreeItem: public QObject
  15. {
  16. Q_OBJECT
  17. public:
  18. explicit Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent = 0, int index = 0);
  19. virtual ~Fb2TreeItem();
  20. Fb2TreeItem * item(const QModelIndex &index) const;
  21. Fb2TreeItem * item(int row) const;
  22. int index(Fb2TreeItem * child) const {
  23. return m_list.indexOf(child);
  24. }
  25. void insert(Fb2TreeItem * child, int row) {
  26. m_list.insert(row, child);
  27. child->m_parent = this;
  28. }
  29. Fb2TreeItem * takeAt(int row) {
  30. return m_list.takeAt(row);
  31. }
  32. int count() const {
  33. return m_list.size();
  34. }
  35. Fb2TextElement element() const {
  36. return m_element;
  37. }
  38. Fb2TreeItem * parent() const {
  39. return m_parent;
  40. }
  41. QString text() const;
  42. const QString & name() const {
  43. return m_name;
  44. }
  45. QPoint pos() const {
  46. return m_element.geometry().topLeft();
  47. }
  48. QString selector() const;
  49. Fb2TreeItem * content(const Fb2TreeModel &model, int number, QModelIndex &index) const;
  50. private:
  51. QString static title(const QWebElement &element);
  52. void addChildren(QWebElement &parent, bool direct = true, bool header = false);
  53. private:
  54. QList<Fb2TreeItem*> m_list;
  55. QWebElement m_element;
  56. QString m_name;
  57. QString m_text;
  58. QString m_body;
  59. Fb2TreeItem * m_parent;
  60. int m_number;
  61. };
  62. class Fb2TreeModel: public QAbstractItemModel
  63. {
  64. Q_OBJECT
  65. public:
  66. explicit Fb2TreeModel(Fb2TextEdit &view, QObject *parent = 0);
  67. virtual ~Fb2TreeModel();
  68. QModelIndex index(const QString &location) const;
  69. Fb2TextEdit & view() { return m_view; }
  70. void selectText(const QModelIndex &index);
  71. QModelIndex move(const QModelIndex &index, int dx, int dy);
  72. QModelIndex append(const QModelIndex &parent);
  73. Fb2TreeItem * item(const QModelIndex &index) const;
  74. public:
  75. virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
  76. virtual QModelIndex parent(const QModelIndex &child) const;
  77. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
  78. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
  79. virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  80. virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
  81. private:
  82. Fb2TextEdit & m_view;
  83. Fb2TreeItem * m_root;
  84. };
  85. class Fb2TreeView : public QTreeView
  86. {
  87. Q_OBJECT
  88. public:
  89. explicit Fb2TreeView(Fb2TextEdit &view, QWidget *parent = 0);
  90. void initActions(QToolBar *toolbar);
  91. public slots:
  92. void updateTree();
  93. private slots:
  94. void activated(const QModelIndex &index);
  95. void contextMenu(const QPoint &pos);
  96. void contentsChanged();
  97. void selectionChanged();
  98. void selectTree();
  99. void insertNode();
  100. void deleteNode();
  101. void moveUp();
  102. void moveDown();
  103. void moveLeft();
  104. void moveRight();
  105. protected:
  106. void keyPressEvent(QKeyEvent *event);
  107. private:
  108. void moveCurrent(int dx, int dy);
  109. Fb2TreeModel * model();
  110. private:
  111. Fb2TextEdit & m_view;
  112. QTimer m_timerSelect;
  113. QTimer m_timerUpdate;
  114. QMenu m_menu;
  115. QAction
  116. *actionCut,
  117. *actionCopy,
  118. *actionPaste;
  119. };
  120. class Fb2TreeWidget : public QWidget
  121. {
  122. Q_OBJECT
  123. public:
  124. explicit Fb2TreeWidget(Fb2TextEdit &view, QWidget* parent = 0);
  125. protected:
  126. QToolBar * m_tool;
  127. Fb2TreeView * m_tree;
  128. };
  129. #endif // FB2TREE_HPP