FileBrowser.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * FileBrowser.h - include file for FileBrowser
  3. *
  4. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef FILE_BROWSER_H
  25. #define FILE_BROWSER_H
  26. #include <QCheckBox>
  27. #include <QtCore/QDir>
  28. #include <QtCore/QMutex>
  29. #include <QTreeWidget>
  30. #include "SideBarWidget.h"
  31. class QLineEdit;
  32. class FileItem;
  33. class InstrumentTrack;
  34. class FileBrowserTreeWidget;
  35. class PlayHandle;
  36. class TrackContainer;
  37. class FileBrowser : public SideBarWidget
  38. {
  39. Q_OBJECT
  40. public:
  41. /**
  42. Create a file browser side bar widget
  43. @param directories '*'-separated list of directories to search for.
  44. If a directory of factory files should be in the list it
  45. must be the last one (for the factory files delimiter to work)
  46. @param filter Filter as used in QDir::match
  47. @param recurse *to be documented*
  48. */
  49. FileBrowser( const QString & directories, const QString & filter,
  50. const QString & title, const QPixmap & pm,
  51. QWidget * parent, bool dirs_as_items = false, bool recurse = false,
  52. const QString& userDir = "",
  53. const QString& factoryDir = "");
  54. virtual ~FileBrowser() = default;
  55. private slots:
  56. void reloadTree( void );
  57. void expandItems( QTreeWidgetItem * item=nullptr, QList<QString> expandedDirs = QList<QString>() );
  58. // call with item=NULL to filter the entire tree
  59. bool filterItems( const QString & filter, QTreeWidgetItem * item=nullptr );
  60. void giveFocusToFilter();
  61. private:
  62. void keyPressEvent( QKeyEvent * ke ) override;
  63. void addItems( const QString & path );
  64. FileBrowserTreeWidget * m_fileBrowserTreeWidget;
  65. QLineEdit * m_filterEdit;
  66. QString m_directories; //!< Directories to search, split with '*'
  67. QString m_filter; //!< Filter as used in QDir::match()
  68. bool m_dirsAsItems;
  69. bool m_recurse;
  70. void addContentCheckBox();
  71. QCheckBox* m_showUserContent = nullptr;
  72. QCheckBox* m_showFactoryContent = nullptr;
  73. QString m_userDir;
  74. QString m_factoryDir;
  75. } ;
  76. class FileBrowserTreeWidget : public QTreeWidget
  77. {
  78. Q_OBJECT
  79. public:
  80. FileBrowserTreeWidget( QWidget * parent );
  81. virtual ~FileBrowserTreeWidget() = default;
  82. //! This method returns a QList with paths (QString's) of all directories
  83. //! that are expanded in the tree.
  84. QList<QString> expandedDirs( QTreeWidgetItem * item = nullptr ) const;
  85. protected:
  86. void contextMenuEvent( QContextMenuEvent * e ) override;
  87. void mousePressEvent( QMouseEvent * me ) override;
  88. void mouseMoveEvent( QMouseEvent * me ) override;
  89. void mouseReleaseEvent( QMouseEvent * me ) override;
  90. void keyPressEvent( QKeyEvent * ke ) override;
  91. void keyReleaseEvent( QKeyEvent * ke ) override;
  92. void hideEvent( QHideEvent * he ) override;
  93. void focusOutEvent( QFocusEvent * fe ) override;
  94. private:
  95. //! Start a preview of a file item
  96. void previewFileItem(FileItem* file);
  97. //! If a preview is playing, stop it.
  98. void stopPreview();
  99. void handleFile( FileItem * fi, InstrumentTrack * it );
  100. void openInNewInstrumentTrack( TrackContainer* tc, FileItem* item );
  101. bool m_mousePressed;
  102. QPoint m_pressPos;
  103. //! This should only be accessed or modified when m_pphMutex is held
  104. PlayHandle* m_previewPlayHandle;
  105. QMutex m_pphMutex;
  106. QList<QAction*> getContextActions(FileItem* item, bool songEditor);
  107. private slots:
  108. void activateListItem( QTreeWidgetItem * item, int column );
  109. void openInNewInstrumentTrack( FileItem* item, bool songEditor );
  110. bool openInNewSampleTrack( FileItem* item );
  111. void sendToActiveInstrumentTrack( FileItem* item );
  112. void updateDirectory( QTreeWidgetItem * item );
  113. void openContainingFolder( FileItem* item );
  114. } ;
  115. class Directory : public QTreeWidgetItem
  116. {
  117. public:
  118. Directory( const QString & filename, const QString & path,
  119. const QString & filter );
  120. void update( void );
  121. inline QString fullName( QString path = QString() )
  122. {
  123. if( path.isEmpty() )
  124. {
  125. path = m_directories[0];
  126. }
  127. if( ! path.isEmpty() )
  128. {
  129. path += QDir::separator();
  130. }
  131. return( QDir::cleanPath( path + text( 0 ) ) +
  132. QDir::separator() );
  133. }
  134. inline void addDirectory( const QString & dir )
  135. {
  136. m_directories.push_back( dir );
  137. }
  138. private:
  139. void initPixmaps( void );
  140. bool addItems( const QString & path );
  141. static QPixmap * s_folderPixmap;
  142. static QPixmap * s_folderOpenedPixmap;
  143. static QPixmap * s_folderLockedPixmap;
  144. //! Directories that lead here
  145. //! Initially, this is just set to the current path of a directory
  146. //! If, however, you have e.g. 'TripleOscillator/xyz' in two of the
  147. //! file browser's search directories 'a' and 'b', this will have two
  148. //! entries 'a/TripleOscillator' and 'b/TripleOscillator'
  149. //! and 'xyz' in the tree widget
  150. QStringList m_directories;
  151. //! Filter as used in QDir::match()
  152. QString m_filter;
  153. int m_dirCount;
  154. } ;
  155. class FileItem : public QTreeWidgetItem
  156. {
  157. public:
  158. enum FileTypes
  159. {
  160. ProjectFile,
  161. PresetFile,
  162. SampleFile,
  163. SoundFontFile,
  164. PatchFile,
  165. MidiFile,
  166. VstPluginFile,
  167. UnknownFile,
  168. NumFileTypes
  169. } ;
  170. enum FileHandling
  171. {
  172. NotSupported,
  173. LoadAsProject,
  174. LoadAsPreset,
  175. LoadByPlugin,
  176. ImportAsProject
  177. } ;
  178. FileItem( QTreeWidget * parent, const QString & name,
  179. const QString & path );
  180. FileItem( const QString & name, const QString & path );
  181. QString fullName() const
  182. {
  183. return QFileInfo(m_path, text(0)).absoluteFilePath();
  184. }
  185. inline FileTypes type( void ) const
  186. {
  187. return( m_type );
  188. }
  189. inline FileHandling handling( void ) const
  190. {
  191. return( m_handling );
  192. }
  193. inline bool isTrack( void ) const
  194. {
  195. return m_handling == LoadAsPreset || m_handling == LoadByPlugin;
  196. }
  197. QString extension( void );
  198. static QString extension( const QString & file );
  199. private:
  200. void initPixmaps( void );
  201. void determineFileType( void );
  202. static QPixmap * s_projectFilePixmap;
  203. static QPixmap * s_presetFilePixmap;
  204. static QPixmap * s_sampleFilePixmap;
  205. static QPixmap * s_soundfontFilePixmap;
  206. static QPixmap * s_vstPluginFilePixmap;
  207. static QPixmap * s_midiFilePixmap;
  208. static QPixmap * s_unknownFilePixmap;
  209. QString m_path;
  210. FileTypes m_type;
  211. FileHandling m_handling;
  212. } ;
  213. #endif