vnotebookselector.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef VNOTEBOOKSELECTOR_H
  2. #define VNOTEBOOKSELECTOR_H
  3. #include <QComboBox>
  4. #include <QVector>
  5. #include <QString>
  6. #include "vnavigationmode.h"
  7. class VNotebook;
  8. class VNote;
  9. class VEditArea;
  10. class QListWidget;
  11. class QAction;
  12. class QListWidgetItem;
  13. class QLabel;
  14. class VNotebookSelector : public QComboBox, public VNavigationMode
  15. {
  16. Q_OBJECT
  17. public:
  18. explicit VNotebookSelector(VNote *vnote, QWidget *p_parent = 0);
  19. void update();
  20. inline void setEditArea(VEditArea *p_editArea);
  21. // Select notebook @p_notebook.
  22. bool locateNotebook(const VNotebook *p_notebook);
  23. void showPopup() Q_DECL_OVERRIDE;
  24. // Implementations for VNavigationMode.
  25. void registerNavigation(QChar p_majorKey) Q_DECL_OVERRIDE;
  26. void showNavigation() Q_DECL_OVERRIDE;
  27. void hideNavigation() Q_DECL_OVERRIDE;
  28. bool handleKeyNavigation(int p_key, bool &p_succeed) Q_DECL_OVERRIDE;
  29. signals:
  30. void curNotebookChanged(VNotebook *p_notebook);
  31. // Info of current notebook was changed.
  32. void notebookUpdated(const VNotebook *p_notebook);
  33. // Emit after creating a new notebook.
  34. void notebookCreated();
  35. public slots:
  36. bool newNotebook();
  37. protected:
  38. bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE;
  39. private slots:
  40. void handleCurIndexChanged(int p_index);
  41. void handleItemActivated(int p_index);
  42. void requestPopupListContextMenu(QPoint p_pos);
  43. void deleteNotebook();
  44. void editNotebookInfo();
  45. private:
  46. void initActions();
  47. void updateComboBox();
  48. // Return the index of @p_notebook in m_noteboks.
  49. int indexOfNotebook(const VNotebook *p_notebook);
  50. // If @p_import is true, we will use the existing config file.
  51. // If @p_imageFolder is empty, we will use the global one.
  52. void createNotebook(const QString &p_name, const QString &p_path,
  53. bool p_import, const QString &p_imageFolder);
  54. void deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles);
  55. void addNotebookItem(const QString &p_name);
  56. // @p_index is the index of m_notebooks, NOT of QComboBox.
  57. void removeNotebookItem(int p_index);
  58. // @p_index is the index of QComboBox.
  59. void updateComboBoxItem(int p_index, const QString &p_name);
  60. void insertAddNotebookItem();
  61. // @p_index is the index of m_notebooks.
  62. void setCurrentIndexNotebook(int p_index);
  63. int indexOfListItem(const QListWidgetItem *p_item);
  64. // @p_index is the idnex of QComboBox.
  65. inline VNotebook *getNotebookFromComboIndex(int p_index);
  66. void resizeListWidgetToContent();
  67. bool handlePopupKeyPress(QKeyEvent *p_event);
  68. VNote *m_vnote;
  69. QVector<VNotebook *> &m_notebooks;
  70. VEditArea *m_editArea;
  71. QListWidget *m_listWidget;
  72. int m_lastValidIndex;
  73. // Actions
  74. QAction *m_deleteNotebookAct;
  75. QAction *m_notebookInfoAct;
  76. QAction *m_openLocationAct;
  77. // We will add several special action item in the combobox. This is the start index
  78. // of the real notebook items related to m_notebooks.
  79. static const int c_notebookStartIdx;
  80. QLabel *m_naviLabel;
  81. };
  82. inline void VNotebookSelector::setEditArea(VEditArea *p_editArea)
  83. {
  84. m_editArea = p_editArea;
  85. }
  86. inline VNotebook *VNotebookSelector::getNotebookFromComboIndex(int p_index)
  87. {
  88. if (p_index < c_notebookStartIdx) {
  89. return NULL;
  90. }
  91. int nbIdx = p_index - c_notebookStartIdx;
  92. if (nbIdx >= m_notebooks.size()) {
  93. return NULL;
  94. }
  95. return m_notebooks[nbIdx];
  96. }
  97. #endif // VNOTEBOOKSELECTOR_H