fb2tree.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef FB2TREE_H
  2. #define FB2TREE_H
  3. #include <QAbstractItemModel>
  4. #include <QWebElement>
  5. #include <QWebView>
  6. QT_BEGIN_NAMESPACE
  7. class QTreeView;
  8. QT_END_NAMESPACE
  9. class Fb2TreeItem: public QObject
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent = 0);
  14. virtual ~Fb2TreeItem();
  15. Fb2TreeItem * item(const QModelIndex &index) const;
  16. Fb2TreeItem * item(int row) const;
  17. int index(Fb2TreeItem * child) const {
  18. return m_list.indexOf(child);
  19. }
  20. int count() const {
  21. return m_list.size();
  22. }
  23. Fb2TreeItem * parent() const {
  24. return m_parent;
  25. }
  26. QString text() const;
  27. const QString & name() const {
  28. return m_name;
  29. }
  30. QPoint pos() const {
  31. return m_element.geometry().topLeft();
  32. }
  33. private:
  34. QString static title(const QWebElement &element);
  35. void addChildren(QWebElement &parent);
  36. private:
  37. QList<Fb2TreeItem*> m_list;
  38. QWebElement m_element;
  39. QString m_name;
  40. QString m_text;
  41. Fb2TreeItem * m_parent;
  42. };
  43. class Fb2TreeModel: public QAbstractItemModel
  44. {
  45. Q_OBJECT
  46. public:
  47. explicit Fb2TreeModel(QWebView &view, QObject *parent = 0);
  48. virtual ~Fb2TreeModel();
  49. void select(const QModelIndex &index);
  50. void expand(QTreeView *view);
  51. public:
  52. virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
  53. virtual QModelIndex parent(const QModelIndex &child) const;
  54. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
  55. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
  56. virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  57. protected:
  58. Fb2TreeItem * item(const QModelIndex &index) const;
  59. private:
  60. QWebView & m_view;
  61. Fb2TreeItem * m_root;
  62. };
  63. #endif // FB2TREE_H