filebrowser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #ifndef FILEBROWSER_H
  2. #define FILEBROWSER_H
  3. #include "extras/listviewdelegate.h"
  4. #include <QListView>
  5. #include <QStack>
  6. #include <QDirModel>
  7. #include <QFile>
  8. #include <QFileSystemModel>
  9. #include <QMenu>
  10. #include <QUrl>
  11. #include <QTimer>
  12. #include <QDesktopWidget>
  13. #include <QDesktopServices>
  14. #include <QMap>
  15. #include <QApplication>
  16. #include <QAction>
  17. #include <QModelIndex>
  18. #include "contextmenu.h"
  19. #include "ddeviceinformation.h"
  20. QT_BEGIN_NAMESPACE
  21. class QGestureEvent;
  22. class QTapAndHoldGesture;
  23. class QTapGesture;
  24. class QPinchGesture;
  25. class QSwipeGesture;
  26. QT_END_NAMESPACE
  27. /*! \class FileBrowser
  28. \brief This class provides the list view widget for the browser and the data model
  29. */
  30. class FileBrowser : public QWidget
  31. {
  32. Q_OBJECT
  33. public:
  34. //! Types of view using listview or tableview
  35. enum ViewMode{icon,list,detail};
  36. //! Class constructor
  37. /*!
  38. Defines user's home directory as the base path of the file browser
  39. \param parent Parent object
  40. */
  41. explicit FileBrowser( QWidget *parent = 0);
  42. //! Class constructor
  43. /*!
  44. Defines homedir as the base path of the file browser
  45. \param homedir Base path of the file browser
  46. \param parent Parent object
  47. */
  48. explicit FileBrowser( QString homedir, QWidget *parent = 0);
  49. //void changeDir(QModelIndex index, bool remember=true);
  50. //! Returns the current path
  51. /*!
  52. Returns the current path of the file browser
  53. */
  54. QString getCurrentPath();
  55. //! Returns the data model
  56. /*!
  57. Returns the data model used in the file browser
  58. */
  59. QFileSystemModel *getDirModel();
  60. void setContextMenu(ContextMenu * menu);
  61. //! Returns the mode of the view
  62. /*!
  63. Returns the mode of the view. Gives access to ListView::viewMode
  64. */
  65. int viewMode();
  66. //! Returns a pointer to the listview widget
  67. /*!
  68. Returns a pointer to the listview widget
  69. */
  70. QWidget * listViewWidget();
  71. //! Returns a pointer to the tableview widget
  72. /*!
  73. Returns a pointer to the tableview widget
  74. */
  75. QWidget * tableViewWidget();
  76. //! Returns the current index shown in the file browser
  77. /*!
  78. Gets the current index of the widget that is being used
  79. */
  80. QModelIndex getCurrentIndex();
  81. private:
  82. //! File browser data moodel
  83. QFileSystemModel *dirModel;
  84. //! Progress bar object to show directory copy progress
  85. QProgressBar *copyProgress;
  86. //! Listview object to show the data model
  87. QListView *listView;
  88. //! TableView object to show the data model
  89. QTableView *tableView;
  90. //! Base path, i.e. associated to the home button
  91. QString basePath;
  92. //! Current path of the file browser. The path of the current index in the data model
  93. QString currentPath;
  94. //! Stack of paths for the previous button. Allows go back action
  95. QStack<QString> history;
  96. //! Stack of paths for the next button. Allows go next action
  97. QStack<QString> future;
  98. //! Context menu for any file in the file borwser
  99. ContextMenu *fbMenu;
  100. QShortcut * scCopy;
  101. //! Delegated class for the view.Designed for overlaying icons. TODO: this shouldn't be used by default
  102. ListViewDelegate *listdelegate;
  103. //! Stores the current view mode
  104. ViewMode currentViewMode;
  105. //! Stores the index in the data model of a copied file or directory
  106. QModelIndex copied_resource;
  107. //! Determines if we have to delete the copied (cut) item
  108. bool cutResource;
  109. //! Grants access to the operative system clipboard
  110. const QClipboard *clipboard;
  111. //! Mime data associated to the current item in the clipboard
  112. const QMimeData *mimeData;
  113. //! Initializes the class
  114. /*!
  115. Defines homedir as the base path of the file browser, assigns the data model, the view delagate and the context menu to be used
  116. \param homedir Base path of the file browser
  117. */
  118. void init(QString homedir= QDir::homePath());
  119. //! Emits signals for updating some widgets
  120. /*!
  121. Emits signals for updating some widgets like back, next and up buttons, setting them enabled or not
  122. */
  123. void updateActions();
  124. //! Copies a resource from the clipboard
  125. /*!
  126. Copies (phisically) a file or directory stored in the clipboard.
  127. */
  128. bool copyFromClipboard();
  129. //! Returns the root index shown in the file browser
  130. /*!
  131. Gets the current root of the widget that is being used
  132. */
  133. QModelIndex getRootIndex();
  134. bool gestureEvent(QGestureEvent *);
  135. void tapAndHoldTriggered(QTapAndHoldGesture* );
  136. void tapTriggered(QTapGesture* );
  137. protected:
  138. bool event(QEvent *event);
  139. public Q_SLOTS:
  140. //! Sets the current path to home (base path)
  141. void goHome();
  142. //! Goes one level up in the directory tree
  143. void goUp();
  144. //! Goes to the previous path stored in the history stack
  145. void goBack();
  146. //! Goes to the next path stored in the future stack
  147. void goForward();
  148. //! Forces the refreshing of the data model to the current path
  149. void refresh();
  150. //! Sets the root path to path
  151. /*!
  152. Sets the root path to path. In other words, changes the current directory of the file browser to path
  153. \param path The path to be used
  154. \param remember Determines if is required to push the current path to the histoy stack
  155. */
  156. void setRootPath(const QString& path= QString(), bool remember=true);
  157. //! Changes the current directory to the value of index in the data model
  158. /*!
  159. Changes the current directory to the value of index in the data model
  160. \param index The index in the data model to be used
  161. \param remember Determines if is required to push the current path to the histoy stack
  162. */
  163. void changeDir(const QModelIndex& index, bool remember=true);
  164. //! Changes the view mode of the listview widget
  165. /*!
  166. Changes the view mode of the listview widget
  167. \param view Type of view to be used.
  168. \sa QListView::ViewMode
  169. */
  170. void changeView(int view);
  171. //! Configures some widgets for being used with the icon view mode
  172. void setIconViewConfiguration();
  173. //! Configures some widgets for being used with the list view mode
  174. void setListViewConfiguration();
  175. //! Configures some widgets for being used with the list view mode
  176. void setDetailViewConfiguration();
  177. //! Slot to force treeview column adaptation to content
  178. void adaptTreeViewColumns ();
  179. //! Calls context menu
  180. /*!
  181. Calls context menu and performs some internal control regarding the visibility of context menu items
  182. \param point Current position of the mouse pointer
  183. */
  184. void menu(QPoint point);
  185. //! Opens the directory or the file using the propper application
  186. /*!
  187. This method opens the current resource (index) in the data model. If the selected resource is a directory,
  188. it is assigned as the new root of the file browser. If the selected resource is a file, QDesktopServices::openUrl is called
  189. \sa FileBrowser::changeDir
  190. */
  191. void openResource();
  192. //! Deletes the selected file or directory
  193. /*!
  194. Deletes the selected file or directory after user confirmation
  195. \param index Resource to be deleted
  196. */
  197. void deleteResource(QModelIndex index);
  198. //! Requests conformation before deletion
  199. /*!
  200. Requests conformation before deletion of the current selected item
  201. */
  202. void deleteResourceRequest();
  203. //! Deletes the resorce thah has been cut
  204. /*!
  205. Deletes the resorce thah has been cut
  206. */
  207. void deleteCutResource();
  208. //! Renames the selected file or directory
  209. /*!
  210. Renames the selected file or directory after user input
  211. */
  212. void renameResource();
  213. //! Copies the selected file or directory to the system clipboard
  214. /*!
  215. Copies the selected file or directory to the system clipboard
  216. */
  217. void copyResourceToBuffer();
  218. //! Cuts the selected file or directory to the system clipboard
  219. /*!
  220. Copies the selected file or directory to the system clipboard and sets the flag FileBrowser::cutResource to true
  221. */
  222. void cutResourceToBuffer();
  223. //! Pastes the content of the system clipboard
  224. /*!
  225. Pastes the content of the system clipboard using FileBrowser::copyFromClipboard
  226. \sa FileBrowser::copyFromClipboard
  227. */
  228. void pasteResource();
  229. //! Copies the source folder into dest
  230. /*!
  231. Copies recursively the source folder into dest
  232. \param source Source directory
  233. \param dest Destination directory
  234. */
  235. bool copyFolder(QString source, QString dest);
  236. //! Deletes a folder recursively
  237. /*!
  238. Deletes a folder recursively
  239. \param sourceFolder Directory to be deleted
  240. */
  241. bool rmFolder(const QString sourceFolder);
  242. //! Creates a new folder in the current directory
  243. /*!
  244. Creates a new folder in the current directory
  245. */
  246. bool createFolder();
  247. //! Sets basePath to path
  248. /*!
  249. Sets basePath variable to path, that is an alternative path to user's home directory. Used for home button
  250. \param path Path to be used as base path
  251. */
  252. void setBasePath(QString path);
  253. //! Sets a widget for an item in the list view
  254. /*!
  255. Encapsultes Listview's setItemWidget but using as a parameter a path instead of an index in the data model.
  256. The path is converted then to an index in the data model.
  257. \param path Path of the item in the list view
  258. \param widget The widget to be used for that element of the list view
  259. */
  260. void setItemWidget(QString path, QWidget * widget);
  261. //TODO: decide if this should be extracted from this class
  262. //! Encapsulates ListViewDelegate::setOverlayStatus
  263. void setOverlayStatus(QString path, int status=ListViewDelegate::FileModified);
  264. //! Encapsulates ListViewDelegate::removeOverlayStatus
  265. void removeOverlayStatus(QString path);
  266. //! Encapsulates ListViewDelegate::setMap
  267. void setMap(QMap <qint64, int>*map);
  268. Q_SIGNALS:
  269. //void elementDropped(QString);
  270. //void strOutput(QString);
  271. //void maximizeWindow();
  272. //! Emitted when the current directory is changed
  273. void changeDirectory(QString);
  274. //! Emitted when the current directory is changed in order to enable or disable the button \sa FileBrowser::updateActions
  275. void setBackButtonEnabled(bool);
  276. //! Emitted when the current directory is changed in order to enable or disable the button \sa FileBrowser::updateActions
  277. void setNextButtonEnabled(bool);
  278. //! Emitted when the current directory is changed in order to enable or disable the button \sa FileBrowser::updateActions
  279. void setUpButtonEnabled(bool);
  280. };
  281. #endif // FILEBROWSER_H