Skip to content

Commit f5726be

Browse files
committed
FileReader plugin: added possibility to drag&drop files
1 parent a14d5fd commit f5726be

4 files changed

Lines changed: 90 additions & 7 deletions

File tree

Source/Processors/FileReader/FileReader.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,35 @@ void FileReader::enabledState (bool t)
124124
}
125125

126126

127+
bool FileReader::isFileSupported (const String& fileName) const
128+
{
129+
const File file (fileName);
130+
String ext = file.getFileExtension().toLowerCase().substring (1);
131+
132+
return isFileExtensionSupported (ext);
133+
}
134+
135+
136+
bool FileReader::isFileExtensionSupported (const String& ext) const
137+
{
138+
const int index = supportedExtensions[ext] - 1;
139+
const bool isExtensionSupported = index >= 0;
140+
141+
return isExtensionSupported;
142+
}
143+
144+
127145
bool FileReader::setFile (String fullpath)
128146
{
129147
File file (fullpath);
130148

131149
String ext = file.getFileExtension().toLowerCase().substring (1);
132-
133150
const int index = supportedExtensions[ext] - 1;
134-
const bool isSupportedFileExtension = index >= 0;
151+
const bool isExtensionSupported = index >= 0;
135152

136-
if (isSupportedFileExtension)
153+
if (isExtensionSupported)
137154
{
155+
const int index = supportedExtensions[ext] - 1;
138156
Plugin::FileSourceInfo sourceInfo = AccessClass::getPluginManager()->getFileSourceInfo (index);
139157
input = sourceInfo.creator();
140158
}

Source/Processors/FileReader/FileReader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class FileReader : public GenericProcessor
7070
String getFile() const;
7171
bool setFile (String fullpath);
7272

73+
bool isFileSupported (const String& filename) const;
74+
bool isFileExtensionSupported (const String& ext) const;
75+
7376

7477
private:
7578
void setActiveRecording (int index);

Source/Processors/FileReader/FileReaderEditor.cpp

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ static const Font FONT_LABEL ("Small Text", 10, Font::plain);
3333
FileReaderEditor::FileReaderEditor (GenericProcessor* parentNode, bool useDefaultParameterEditors = true)
3434
: GenericEditor (parentNode, useDefaultParameterEditors)
3535
, fileReader (static_cast<FileReader*> (parentNode))
36-
, recTotalTime (0)
36+
, recTotalTime (0)
37+
, m_isFileDragAndDropActive (false)
3738
{
3839
lastFilePath = File::getCurrentWorkingDirectory();
3940

@@ -91,6 +92,17 @@ void FileReaderEditor::setFile (String file)
9192
}
9293

9394

95+
void FileReaderEditor::paintOverChildren (Graphics& g)
96+
{
97+
// Draw a frame around component if files are drag&dropping now
98+
if (m_isFileDragAndDropActive)
99+
{
100+
g.setColour (Colours::aqua);
101+
g.drawRect (getLocalBounds(), 2.f);
102+
}
103+
}
104+
105+
94106
void FileReaderEditor::buttonEvent (Button* button)
95107
{
96108
if (! acquisitionIsActive)
@@ -251,6 +263,45 @@ void FileReaderEditor::loadCustomParameters (XmlElement* xml)
251263
}
252264

253265

266+
bool FileReaderEditor::isInterestedInFileDrag (const StringArray& files)
267+
{
268+
if (! acquisitionIsActive)
269+
{
270+
const bool isExtensionSupported = fileReader->isFileSupported (files[0]);
271+
m_isFileDragAndDropActive = true;
272+
273+
return isExtensionSupported;
274+
}
275+
276+
return false;
277+
}
278+
279+
280+
void FileReaderEditor::fileDragExit (const StringArray& files)
281+
{
282+
m_isFileDragAndDropActive = false;
283+
284+
repaint();
285+
}
286+
287+
288+
void FileReaderEditor::fileDragEnter (const StringArray& files, int x, int y)
289+
{
290+
m_isFileDragAndDropActive = true;
291+
292+
repaint();
293+
}
294+
295+
296+
void FileReaderEditor::filesDropped (const StringArray& files, int x, int y)
297+
{
298+
setFile (files[0]);
299+
300+
m_isFileDragAndDropActive = false;
301+
repaint();
302+
}
303+
304+
254305
// DualTimeComponent
255306
// ================================================================================
256307
DualTimeComponent::DualTimeComponent (FileReaderEditor* e, bool editable)
@@ -298,15 +349,14 @@ DualTimeComponent::~DualTimeComponent()
298349

299350
void DualTimeComponent::paint (Graphics& g)
300351
{
301-
g.setFont (FONT_LABEL);
302-
g.setColour (Colours::darkgrey);
303-
304352
String sep;
305353
if (isEditable)
306354
sep = "-";
307355
else
308356
sep = "/";
309357

358+
g.setFont (FONT_LABEL);
359+
g.setColour (Colours::darkgrey);
310360
g.drawText (sep, 78, 0, 5, 20, Justification::centred, false);
311361
}
312362

Source/Processors/FileReader/FileReaderEditor.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,27 @@ class FileSource;
4141
*/
4242

4343
class FileReaderEditor : public GenericEditor
44+
, public FileDragAndDropTarget
4445
, public ComboBox::Listener
4546
{
4647
public:
4748
FileReaderEditor (GenericProcessor* parentNode, bool useDefaultParameterEditors);
4849
virtual ~FileReaderEditor();
4950

51+
void paintOverChildren (Graphics& g) override;
52+
5053
void buttonEvent (Button* button) override;
5154

5255
void saveCustomParameters (XmlElement*) override;
5356
void loadCustomParameters (XmlElement*) override;
5457

58+
// FileDragAndDropTarget methods
59+
// ============================================
60+
bool isInterestedInFileDrag (const StringArray& files) override;
61+
void fileDragExit (const StringArray& files) override;
62+
void filesDropped (const StringArray& files, int x, int y) override;
63+
void fileDragEnter (const StringArray& files, int x, int y) override;
64+
5565
bool setPlaybackStartTime (unsigned int ms);
5666
bool setPlaybackStopTime (unsigned int ms);
5767
void setTotalTime (unsigned int ms);
@@ -79,6 +89,8 @@ class FileReaderEditor : public GenericEditor
7989
FileReader* fileReader;
8090
unsigned int recTotalTime;
8191

92+
bool m_isFileDragAndDropActive;
93+
8294
File lastFilePath;
8395

8496
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileReaderEditor);

0 commit comments

Comments
 (0)