voutline.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef VOUTLINE_H
  2. #define VOUTLINE_H
  3. #include <QWidget>
  4. #include <QVector>
  5. #include <QMap>
  6. #include <QChar>
  7. #include "vtableofcontent.h"
  8. #include "vnavigationmode.h"
  9. class QLabel;
  10. class VTreeWidget;
  11. class QPushButton;
  12. class QTimer;
  13. // Display table of content as a tree and enable user to click an item to
  14. // jump to that header.
  15. class VOutline : public QWidget, public VNavigationMode
  16. {
  17. Q_OBJECT
  18. public:
  19. VOutline(QWidget *parent = 0);
  20. // Implementations for VNavigationMode.
  21. void showNavigation() Q_DECL_OVERRIDE;
  22. bool handleKeyNavigation(int p_key, bool &p_succeed) Q_DECL_OVERRIDE;
  23. // Update tree according to outline.
  24. static void updateTreeFromOutline(QTreeWidget *p_treeWidget, const VTableOfContent &p_outline);
  25. // Set the item corresponding to @p_header as current item.
  26. static void selectHeader(QTreeWidget *p_treeWidget,
  27. const VTableOfContent &p_outline,
  28. const VHeaderPointer &p_header);
  29. // Return NULL if no corresponding header in outline.
  30. static const VTableOfContentItem *getHeaderFromItem(QTreeWidgetItem *p_item,
  31. const VTableOfContent &p_outline);
  32. signals:
  33. // Emit when current item changed by user and header of that item is not empty.
  34. // Do not worry about infinite recursion.
  35. void outlineItemActivated(const VHeaderPointer &p_header);
  36. public slots:
  37. // Called to update outline and the tree.
  38. // Just clear the tree if @p_outline is empty.
  39. void updateOutline(const VTableOfContent &p_outline);
  40. // Called to update current header in the tree.
  41. // Will not emit outlineItemActivated().
  42. void updateCurrentHeader(const VHeaderPointer &p_header);
  43. protected:
  44. void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
  45. void focusInEvent(QFocusEvent *p_event) Q_DECL_OVERRIDE;
  46. private:
  47. void setupUI();
  48. void expandTree(int p_expandedLevel = 6);
  49. void expandTreeOne(QTreeWidgetItem *p_item, int p_levelToBeExpanded);
  50. void updateButtonsState();
  51. // Do not response if m_muted is true.
  52. void activateItem(QTreeWidgetItem *p_item, bool p_focusEditArea = false);
  53. // @index: the index in @headers.
  54. static void updateTreeByLevel(QTreeWidget *p_treeWidget,
  55. const QVector<VTableOfContentItem> &p_headers,
  56. int &p_index,
  57. QTreeWidgetItem *p_parent,
  58. QTreeWidgetItem *p_last,
  59. int p_level);
  60. // Fill the info of @p_item.
  61. static void fillItem(QTreeWidgetItem *p_item, const VTableOfContentItem &p_header);
  62. static bool selectHeaderOne(QTreeWidget *p_treeWidget,
  63. QTreeWidgetItem *p_item,
  64. const VTableOfContent &p_outline,
  65. const VHeaderPointer &p_header);
  66. VTableOfContent m_outline;
  67. VHeaderPointer m_currentHeader;
  68. // When true, won't emit outlineItemActivated().
  69. bool m_muted;
  70. QTimer *m_expandTimer;
  71. QPushButton *m_deLevelBtn;
  72. QPushButton *m_inLevelBtn;
  73. VTreeWidget *m_tree;
  74. };
  75. #endif // VOUTLINE_H