vdirectorytree.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef VDIRECTORYTREE_H
  2. #define VDIRECTORYTREE_H
  3. #include <QJsonObject>
  4. #include <QPointer>
  5. #include <QVector>
  6. #include <QMap>
  7. #include <QList>
  8. #include <QHash>
  9. #include "vtreewidget.h"
  10. #include "vdirectory.h"
  11. #include "vnotebook.h"
  12. #include "vnavigationmode.h"
  13. #include "vconstants.h"
  14. class VEditArea;
  15. class QLabel;
  16. class VDirectoryTree : public VTreeWidget, public VNavigationMode
  17. {
  18. Q_OBJECT
  19. public:
  20. explicit VDirectoryTree(QWidget *parent = 0);
  21. void setEditArea(VEditArea *p_editArea);
  22. // Locate to the item representing @p_directory.
  23. bool locateDirectory(const VDirectory *p_directory);
  24. const VNotebook *currentNotebook() const;
  25. VDirectory *currentDirectory() const;
  26. // Implementations for VNavigationMode.
  27. void showNavigation() Q_DECL_OVERRIDE;
  28. bool handleKeyNavigation(int p_key, bool &p_succeed) Q_DECL_OVERRIDE;
  29. signals:
  30. void currentDirectoryChanged(VDirectory *p_directory);
  31. void directoryUpdated(const VDirectory *p_directory, UpdateAction p_act);
  32. public slots:
  33. // Set directory tree to display a given notebook @p_notebook.
  34. void setNotebook(VNotebook *p_notebook);
  35. // Create a root folder.
  36. void newRootDirectory();
  37. // View and edit info about directory.
  38. void editDirectoryInfo();
  39. // Clear and re-build the whole directory tree.
  40. // Do not load all the sub-directories at once.
  41. void updateDirectoryTree();
  42. private slots:
  43. // Set the state of expansion of the directory.
  44. void handleItemExpanded(QTreeWidgetItem *p_item);
  45. // Set the state of expansion of the directory.
  46. void handleItemCollapsed(QTreeWidgetItem *p_item);
  47. void contextMenuRequested(QPoint pos);
  48. // Directory selected folder.
  49. // Currently only support single selected item.
  50. void deleteSelectedDirectory();
  51. // Create sub-directory of current item's directory.
  52. void newSubDirectory();
  53. // Current tree item changed.
  54. void currentDirectoryItemChanged(QTreeWidgetItem *currentItem);
  55. // Copy selected directories.
  56. // Will put a Json string into the clipboard which contains the information
  57. // about copied directories.
  58. void copySelectedDirectories(bool p_isCut = false);
  59. void cutSelectedDirectories();
  60. // Paste directories from clipboard as sub-directories of current item.
  61. void pasteDirectoriesFromClipboard();
  62. // Open the folder's parent directory in system's file browser.
  63. void openDirectoryLocation() const;
  64. // Reload the content of current directory.
  65. void reloadFromDisk();
  66. // Sort sub-folders of current item's folder.
  67. void sortItems();
  68. // Pin selected directory to History.
  69. void pinDirectoryToHistory();
  70. protected:
  71. void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  72. void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
  73. QStringList mimeTypes() const Q_DECL_OVERRIDE;
  74. Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE;
  75. // Will be called inside dropEvent().
  76. bool dropMimeData(QTreeWidgetItem *p_parent,
  77. int p_index,
  78. const QMimeData *p_data,
  79. Qt::DropAction p_action) Q_DECL_OVERRIDE;
  80. // Drop the data.
  81. void dropEvent(QDropEvent *p_event) Q_DECL_OVERRIDE;
  82. private:
  83. // Build the subtree of @p_parent recursively to the depth @p_depth.
  84. // @p_depth: negative - infinite levels.
  85. // Will expand the item if the corresponding directory was expanded before.
  86. // Will treat items with children as items having been built before.
  87. void buildSubTree(QTreeWidgetItem *p_parent, int p_depth);
  88. // Fill the content of a tree item according to @p_directory.
  89. void fillTreeItem(QTreeWidgetItem *p_item, VDirectory *p_directory);
  90. void initShortcuts();
  91. // Update @p_item's direct children only: deleted, added, renamed.
  92. void updateItemDirectChildren(QTreeWidgetItem *p_item);
  93. // Find the corresponding item of @p_dir;
  94. // Return's NULL if no item is found and it is the root directory if @p_widget is true.
  95. QTreeWidgetItem *findVDirectory(const VDirectory *p_dir, bool *p_widget = NULL);
  96. QPointer<VDirectory> getVDirectory(QTreeWidgetItem *p_item) const;
  97. // Paste @p_dirs as sub-directory of @p_destDir.
  98. void pasteDirectories(VDirectory *p_destDir,
  99. const QVector<QString> &p_dirs,
  100. bool p_isCut);
  101. // Build the subtree of @p_item's children if it has not been built yet.
  102. // We need to fill the children before showing a item to get a correct render.
  103. void buildChildren(QTreeWidgetItem *p_item);
  104. // Expand/create the directory tree nodes to @p_directory.
  105. QTreeWidgetItem *expandToVDirectory(const VDirectory *p_directory);
  106. // Expand the currently-built subtree of @p_item according to VDirectory.isExpanded().
  107. void expandSubTree(QTreeWidgetItem *p_item);
  108. // We use a map to save and restore current directory of each notebook.
  109. // Try to restore current directory after changing notebook.
  110. // Return false if no cache item found for current notebook.
  111. bool restoreCurrentItem();
  112. // Generate new magic to m_magicForClipboard.
  113. int getNewMagic();
  114. // Check if @p_magic equals to m_magicForClipboard.
  115. bool checkMagic(int p_magic) const;
  116. // Check if clipboard contains valid info to paste as directories.
  117. bool pasteAvailable() const;
  118. // Sort sub-directories of @p_dir.
  119. void sortItems(VDirectory *p_dir);
  120. QPointer<VNotebook> m_notebook;
  121. VEditArea *m_editArea;
  122. // Each notebook's current item's VDirectory.
  123. QHash<VNotebook *, VDirectory *> m_notebookCurrentDirMap;
  124. // Magic number for clipboard operations.
  125. int m_magicForClipboard;
  126. };
  127. inline QPointer<VDirectory> VDirectoryTree::getVDirectory(QTreeWidgetItem *p_item) const
  128. {
  129. Q_ASSERT(p_item);
  130. return p_item->data(0, Qt::UserRole).value<VDirectory *>();
  131. }
  132. inline void VDirectoryTree::setEditArea(VEditArea *p_editArea)
  133. {
  134. m_editArea = p_editArea;
  135. }
  136. inline const VNotebook *VDirectoryTree::currentNotebook() const
  137. {
  138. return m_notebook;
  139. }
  140. #endif // VDIRECTORYTREE_H