vsnippetlist.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef VSNIPPETLIST_H
  2. #define VSNIPPETLIST_H
  3. #include <QWidget>
  4. #include <QVector>
  5. #include <QPoint>
  6. #include "vsnippet.h"
  7. #include "vnavigationmode.h"
  8. class QPushButton;
  9. class VListWidget;
  10. class QListWidgetItem;
  11. class QLabel;
  12. class QKeyEvent;
  13. class QFocusEvent;
  14. class VSnippetList : public QWidget, public VNavigationMode
  15. {
  16. Q_OBJECT
  17. public:
  18. explicit VSnippetList(QWidget *p_parent = nullptr);
  19. const QVector<VSnippet> &getSnippets() const;
  20. const VSnippet *getSnippet(const QString &p_name) const;
  21. // Implementations for VNavigationMode.
  22. void showNavigation() Q_DECL_OVERRIDE;
  23. bool handleKeyNavigation(int p_key, bool &p_succeed) Q_DECL_OVERRIDE;
  24. protected:
  25. void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE;
  26. void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE;
  27. void focusInEvent(QFocusEvent *p_event) Q_DECL_OVERRIDE;
  28. private slots:
  29. void newSnippet();
  30. void handleContextMenuRequested(QPoint p_pos);
  31. void handleItemActivated(QListWidgetItem *p_item);
  32. void snippetInfo();
  33. void deleteSelectedItems();
  34. void sortItems();
  35. private:
  36. void setupUI();
  37. void init();
  38. void initShortcuts();
  39. void makeSureFolderExist() const;
  40. // Update list of snippets according to m_snippets.
  41. void updateContent();
  42. // Add @p_snippet.
  43. bool addSnippet(const VSnippet &p_snippet, QString *p_errMsg = nullptr);
  44. // Write m_snippets to config file.
  45. bool writeSnippetsToConfig() const;
  46. // Read from config file to m_snippets.
  47. bool readSnippetsFromConfig();
  48. // Get the snippet index in m_snippets of @p_item.
  49. int getSnippetIndex(QListWidgetItem *p_item) const;
  50. VSnippet *getSnippet(QListWidgetItem *p_item);
  51. // Write the content of @p_snippet to file.
  52. bool writeSnippetFile(const VSnippet &p_snippet, QString *p_errMsg = nullptr);
  53. QString getSnippetFilePath(const VSnippet &p_snippet) const;
  54. // Sort m_snippets according to @p_sortedIdx.
  55. bool sortSnippets(const QVector<int> &p_sortedIdx, QString *p_errMsg = nullptr);
  56. bool deleteSnippets(const QList<QString> &p_snippets, QString *p_errMsg = nullptr);
  57. bool deleteSnippetFile(const VSnippet &p_snippet, QString *p_errMsg = nullptr);
  58. void updateNumberLabel() const;
  59. bool m_initialized;
  60. bool m_uiInitialized;
  61. QPushButton *m_addBtn;
  62. QPushButton *m_locateBtn;
  63. QLabel *m_numLabel;
  64. VListWidget *m_snippetList;
  65. QVector<VSnippet> m_snippets;
  66. static const QString c_infoShortcutSequence;
  67. };
  68. inline const QVector<VSnippet> &VSnippetList::getSnippets() const
  69. {
  70. const_cast<VSnippetList *>(this)->init();
  71. return m_snippets;
  72. }
  73. inline const VSnippet *VSnippetList::getSnippet(const QString &p_name) const
  74. {
  75. const_cast<VSnippetList *>(this)->init();
  76. for (auto const & snip : m_snippets) {
  77. if (snip.getName() == p_name) {
  78. return &snip;
  79. }
  80. }
  81. return NULL;
  82. }
  83. #endif // VSNIPPETLIST_H