fb2tree.hpp 3.9 KB

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