diff --git a/sources/qetapp.cpp b/sources/qetapp.cpp index b0e864985..55ebca244 100644 --- a/sources/qetapp.cpp +++ b/sources/qetapp.cpp @@ -526,6 +526,38 @@ QString QETApp::symbolicPath(const QString &real_path) { return(chemin); } +/** + @return the list of file extensions QElectroTech is able to open and + supposed to handle. Note they are provided with no leading point. +*/ +QStringList QETApp::handledFileExtensions() { + static QStringList ext; + if (!ext.count()) { + ext << "qet"; + ext << "elmt"; + ext << QString(TITLEBLOCKS_FILE_EXTENSION).remove(QRegExp("^\.")); + } + return(ext); +} + +/** + @param an URLs list + @return the list of filepaths QElectroTech is able to open. +*/ +QStringList QETApp::handledFiles(const QList &urls) { + QList filepaths; + foreach (QUrl url, urls) { + if (!url.isLocalFile()) continue; + QString local_path = url.toLocalFile(); + QFileInfo local_path_info(local_path); + QString local_path_ext = local_path_info.completeSuffix(); + if (handledFileExtensions().contains(local_path_ext)) { + filepaths << local_path; + } + } + return(filepaths); +} + /** @param filepath Un chemin de fichier Note : si filepath est une chaine vide, cette methode retourne 0. diff --git a/sources/qetapp.h b/sources/qetapp.h index 195148df0..4eb45f181 100644 --- a/sources/qetapp.h +++ b/sources/qetapp.h @@ -95,6 +95,8 @@ class QETApp : public QETSingleApplication { static QString languagesPath(); static QString realPath(const QString &); static QString symbolicPath(const QString &); + static QStringList handledFileExtensions(); + static QStringList handledFiles(const QList &); static RecentFiles *projectsRecentFiles(); static RecentFiles *elementsRecentFiles(); #ifdef QET_ALLOW_OVERRIDE_CED_OPTION diff --git a/sources/qetmainwindow.cpp b/sources/qetmainwindow.cpp index d5b6ba39e..9f3a4bf23 100644 --- a/sources/qetmainwindow.cpp +++ b/sources/qetmainwindow.cpp @@ -29,6 +29,8 @@ QETMainWindow::QETMainWindow(QWidget *widget, Qt::WindowFlags flags) : { initCommonActions(); initCommonMenus(); + + setAcceptDrops(true); } /** @@ -162,3 +164,31 @@ bool QETMainWindow::event(QEvent *e) { */ void QETMainWindow::firstActivation(QEvent *) { } + + +/** + Accept or refuse drag'n drop events depending on the dropped mime type; + especially, accepts only URLs to local files that we could open. + @param e le QDragEnterEvent correspondant au drag'n drop tente +*/ +void QETMainWindow::dragEnterEvent(QDragEnterEvent *e) { + if (e -> mimeData() -> hasUrls()) { + if (QETApp::handledFiles(e -> mimeData() -> urls()).count()) { + e -> acceptProposedAction(); + } + } +} + +/** + Handle drops accepted on main windows; more specifically, open dropped files + as long as they are handled by QElectrotech. + @param e the QDropEvent describing the current drag'n drop +*/ +void QETMainWindow::dropEvent(QDropEvent *e) { + if (e -> mimeData() -> hasUrls()) { + QStringList filepaths = QETApp::handledFiles(e -> mimeData() -> urls()); + if (filepaths.count()) { + QETApp::instance() -> openFiles(QETArguments(filepaths)); + } + } +} diff --git a/sources/qetmainwindow.h b/sources/qetmainwindow.h index 2c36cb95e..803755ac5 100644 --- a/sources/qetmainwindow.h +++ b/sources/qetmainwindow.h @@ -40,6 +40,8 @@ class QETMainWindow : public QMainWindow { protected: virtual bool event(QEvent *); + virtual void dragEnterEvent(QDragEnterEvent *e); + virtual void dropEvent(QDropEvent *e); virtual void firstActivation(QEvent *); // slots