fb2tree.hpp 3.2 KB

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