Created
June 29, 2012 05:58
-
-
Save 40/3016116 to your computer and use it in GitHub Desktop.
Browse File System on BB10 Cascades Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FILEBROWSEDIALOG_HPP_ | |
#define FILEBROWSEDIALOG_HPP_ | |
#include <QThread> | |
#include <QVariant> | |
#include <bps/dialog.h> | |
/* | |
* The file browse dialog displays a dialog to browse and select | |
* files from shared folders on the system. | |
*/ | |
class FileBrowseDialog : public QThread | |
{ | |
Q_OBJECT | |
/* | |
* QML property to allow multiple selection | |
*/ | |
Q_PROPERTY(bool multiselect READ getMultiSelect WRITE setMultiSelect) | |
/* | |
* QML property to read the selected filenames | |
*/ | |
Q_PROPERTY(QVariant filepaths READ getFilePaths) | |
/* | |
* QML property to set or get the file filters. This is an | |
* list array variant. | |
*/ | |
Q_PROPERTY(QVariant filters READ getFilters WRITE setFilters) | |
public: | |
/* | |
* Ctor and Dtor | |
*/ | |
FileBrowseDialog(QObject* parent = 0); | |
virtual ~FileBrowseDialog(); | |
/* | |
* Exposed to QML to start the run loop which creates and displays the dialog. | |
* The dialog is shown until a button is clicked. | |
*/ | |
Q_INVOKABLE void show(); | |
public: | |
/* | |
* Getter for the selected filenames QML property | |
*/ | |
QVariant getFilePaths() const; | |
/* | |
* Setter and Getter for the filters QML property | |
*/ | |
QVariant getFilters() const; | |
void setFilters(QVariant const& value); | |
/* | |
* Getter and Setter for the multiselect QML property | |
*/ | |
bool getMultiSelect() const; | |
void setMultiSelect(bool value); | |
signals: | |
/* | |
* Signal emitted when the OK button has been clicked on the browse dialog | |
* The OK button is not enabled unless a file is selected | |
*/ | |
void selectionCompleted(); | |
/* | |
* Signal emitted when the cancel button has been clicked on the browse dialog | |
*/ | |
void selectionCancelled(); | |
protected: | |
/* | |
* Implements the run loop. Dialog stays open until a button is clicked. | |
*/ | |
virtual void run(); | |
protected: | |
dialog_instance_t m_dialog; | |
bool m_multiSelect; | |
QVariantList m_filePaths; | |
QVariantList m_filters; | |
}; | |
#endif /* FILEBROWSEDIALOG_HPP_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "FileBrowseDialog.hpp" | |
#include <bps/bps.h> | |
#include <QDebug> | |
FileBrowseDialog::FileBrowseDialog(QObject* parent) | |
: QThread(parent) | |
, m_multiSelect(false) | |
{ | |
m_filters.push_back(QString("*.*")); | |
} | |
FileBrowseDialog::~FileBrowseDialog() | |
{ | |
} | |
void FileBrowseDialog::show() | |
{ | |
if (!isRunning()) | |
{ | |
m_filePaths.clear(); | |
start(); | |
} | |
} | |
QVariant FileBrowseDialog::getFilePaths() const | |
{ | |
return m_filePaths; | |
} | |
bool FileBrowseDialog::getMultiSelect() const | |
{ | |
return m_multiSelect; | |
} | |
void FileBrowseDialog::setMultiSelect(bool value) | |
{ | |
m_multiSelect = value; | |
} | |
QVariant FileBrowseDialog::getFilters() const | |
{ | |
return m_filters; | |
} | |
void FileBrowseDialog::setFilters(QVariant const& value) | |
{ | |
m_filters = value.toList(); | |
qDebug() << "filter count: " << m_filters.count(); | |
} | |
void FileBrowseDialog::run() | |
{ | |
bps_initialize(); | |
//request all dialog events | |
dialog_request_events(0); | |
if (dialog_create_filebrowse(&m_dialog) != BPS_SUCCESS) | |
{ | |
qDebug() << "Failed to create file browse dialog."; | |
emit selectionCancelled(); | |
return; | |
} | |
//set the selection filters | |
if (m_filters.count() > 0) | |
{ | |
char** ext = (char**)new char[m_filters.count()*sizeof(char*)]; | |
int i = 0; | |
for (QVariantList::iterator it = m_filters.begin(); it != m_filters.end(); ++it, ++i) | |
{ | |
QString filter = it->toString(); | |
if (!filter.trimmed().isEmpty()) | |
{ | |
int length = (filter.length() + 1) * sizeof(char); | |
ext[i] = new char[length]; | |
strncpy(ext[i], filter.toAscii(), length); | |
} | |
} | |
if (dialog_set_filebrowse_filter(m_dialog, (const char**)ext, m_filters.count()) != BPS_SUCCESS) | |
{ | |
qDebug() << "unable to set file browse dialog extensions"; | |
} | |
for (i = 0; i < m_filters.count(); i++) | |
{ | |
delete ext[i]; | |
} | |
delete ext; | |
} | |
if (dialog_show(m_dialog) != BPS_SUCCESS) | |
{ | |
qDebug() << "Failed to show file browse dialog."; | |
dialog_destroy(m_dialog); | |
m_dialog = 0; | |
emit selectionCancelled(); | |
return; | |
} | |
bool shutdown = false; | |
while (!shutdown) | |
{ | |
bps_event_t* event = NULL; | |
bps_get_event(&event, -1); // -1 means that the function waits | |
// for an event before returning | |
if (event) | |
{ | |
if (bps_event_get_domain(event) == dialog_get_domain()) | |
{ | |
//0=ok, 1=cancel | |
int selectedIndex = dialog_event_get_selected_index(event); | |
if (selectedIndex == 1) | |
{ | |
int count; | |
char** filepaths; | |
if (BPS_SUCCESS == dialog_event_get_filebrowse_filepaths(event, &filepaths, &count)) | |
{ | |
for (int i = 0; i < count; i++) | |
{ | |
qDebug() << "selected file: " << filepaths[i]; | |
m_filePaths.push_back(QString(filepaths[i])); | |
} | |
bps_free(filepaths); | |
} | |
emit selectionCompleted(); | |
} | |
else | |
{ | |
emit selectionCancelled(); | |
} | |
qDebug() << "Got file browse dialog click"; | |
shutdown = true; | |
} | |
} | |
} | |
if (m_dialog) | |
{ | |
dialog_destroy(m_dialog); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bb.cascades 1.0 | |
import Dialog.FileBrowse 1.0 | |
Page { | |
content: Container { | |
Label { id: filebrowseDialogLabel } | |
Button { | |
text : "File Browse Dialog" | |
onClicked: { | |
filebrowseDialog.show(); | |
} | |
} | |
attachedObjects: [ | |
FileBrowseDialog { | |
id: filebrowseDialog | |
multiselect : true | |
filters : ["*.doc","*.jpg","*.txt"] | |
onSelectionCompleted: { | |
if(filebrowseDialog.filepaths.length>0) | |
filebrowseDialogLabel.text = filebrowseDialog.filepaths[0]; | |
else | |
filebrowseDialogLabel.text = "no file selected"; | |
} | |
onSelectionCancelled: { | |
filebrowseDialogLabel.text = "file browse dialog was cancelled"; | |
} | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment