fb2tree.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, bool header = false);
  49. private:
  50. QList<Fb2TreeItem*> m_list;
  51. QWebElement m_element;
  52. QString m_name;
  53. QString m_text;
  54. QString m_body;
  55. Fb2TreeItem * m_parent;
  56. int m_number;
  57. };
  58. class Fb2TreeModel: public QAbstractItemModel
  59. {
  60. Q_OBJECT
  61. public:
  62. explicit Fb2TreeModel(Fb2WebView &view, QObject *parent = 0);
  63. virtual ~Fb2TreeModel();
  64. QModelIndex index(const QString &location) const;
  65. Fb2WebView & view() { return m_view; }
  66. void selectText(const QModelIndex &index);
  67. QModelIndex move(const QModelIndex &index, int dx, int dy);
  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. virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
  75. protected:
  76. Fb2TreeItem * item(const QModelIndex &index) const;
  77. private:
  78. Fb2WebView & m_view;
  79. Fb2TreeItem * m_root;
  80. };
  81. class Fb2TreeView : public QTreeView
  82. {
  83. Q_OBJECT
  84. public:
  85. explicit Fb2TreeView(Fb2WebView &view, QWidget *parent = 0);
  86. void initToolbar(QToolBar *toolbar);
  87. public slots:
  88. void updateTree();
  89. private slots:
  90. void activated(const QModelIndex &index);
  91. void contentsChanged();
  92. void selectionChanged();
  93. void selectTree();
  94. void insertNode();
  95. void deleteNode();
  96. void moveUp();
  97. void moveDown();
  98. void moveLeft();
  99. void moveRight();
  100. private:
  101. void moveCurrent(int dx, int dy);
  102. Fb2TreeModel * model();
  103. private:
  104. Fb2WebView & m_view;
  105. QTimer m_timerSelect;
  106. QTimer m_timerUpdate;
  107. };
  108. class Fb2TreeWidget : public QWidget
  109. {
  110. Q_OBJECT
  111. public:
  112. explicit Fb2TreeWidget(Fb2WebView &view, QWidget* parent = 0);
  113. protected:
  114. QToolBar * m_tool;
  115. Fb2TreeView * m_tree;
  116. };
  117. #endif // FB2TREE_HPP