fb2tree.hpp 3.2 KB

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