Skip to content

Commit

Permalink
Use native file selector dialog
Browse files Browse the repository at this point in the history
Fix #232
Fix #82
  • Loading branch information
m-kuhn committed Mar 25, 2018
1 parent cb29637 commit bc3d4d4
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/androidplatformutilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "androidplatformutilities.h"
#include "androidpicturesource.h"
#include "androidprojectsource.h"

#include <QMap>
#include <QString>
Expand Down Expand Up @@ -127,3 +128,28 @@ void AndroidPlatformUtilities::open( const QString& data, const QString& type )

QtAndroid::startActivity( intent.object<jobject>(), 102 );
}

ProjectSource *AndroidPlatformUtilities::openProject()
{
QAndroidJniObject actionOpenDocument = QAndroidJniObject::getStaticObjectField( "android/content/Intent", "ACTION_OPEN_DOCUMENT", "Ljava/lang/String;" );
QAndroidJniObject categoryOpenable = QAndroidJniObject::getStaticObjectField( "android/content/Intent", "CATEGORY_OPENABLE", "Ljava/lang/String;" );

QAndroidJniObject intent = QAndroidJniObject( "android/content/Intent", "(Ljava/lang/String;)V", actionOpenDocument.object<jstring>() );
intent.callObjectMethod( "addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", categoryOpenable.object<jstring>() );
QAndroidJniObject mimeType = QAndroidJniObject::fromString( QStringLiteral( "application/*" ) );
intent.callObjectMethod( "setType", "(Ljava/lang/String;)Landroid/content/Intent;", mimeType.object<jstring>() );

AndroidProjectSource* projectSource = nullptr;

if ( intent.isValid() )
{
projectSource = new AndroidProjectSource();
QtAndroid::startActivity( intent.object<jobject>(), 103, projectSource );
}
else
{
qDebug() << "Something went wrong creating the project intent";
}

return projectSource;
}
1 change: 1 addition & 0 deletions src/androidplatformutilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AndroidPlatformUtilities : public PlatformUtilities
virtual QString qgsProject() const override;
virtual PictureSource* getPicture( const QString &prefix ) override;
virtual void open( const QString& data, const QString& type );
virtual ProjectSource *openProject() override;

private:
QString getIntentExtra( const QString&, QAndroidJniObject = 0 ) const;
Expand Down
49 changes: 49 additions & 0 deletions src/androidprojectsource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/***************************************************************************
androidprojectsource.cpp - AndroidProjectSource
---------------------
begin : 19.3.2018
copyright : (C) 2018 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "androidprojectsource.h"
#include <QDebug>

AndroidProjectSource::AndroidProjectSource()
: ProjectSource( nullptr )
, QAndroidActivityResultReceiver()
{
}

void AndroidProjectSource::handleActivityResult( int receiverRequestCode, int resultCode, const QAndroidJniObject& data )
{
jint RESULT_OK = QAndroidJniObject::getStaticField<jint>( "android/app/Activity", "RESULT_OK" );
if ( receiverRequestCode == 103 && resultCode == RESULT_OK )
{
QAndroidJniObject uri = data.callObjectMethod( "getData", "()Landroid/net/Uri;" );
QAndroidJniObject path = uri.callObjectMethod( "getPath", "()Ljava/lang/String;" );
QAndroidJniObject file = QAndroidJniObject( "java/io/File", "(Ljava/lang/String;)V", path.object<jstring>() );
QString absolutePath = file.callObjectMethod( "getAbsolutePath", "()Ljava/lang/String;" ).toString();

if ( absolutePath.startsWith( QStringLiteral( "/document/primary:" ) ) )
{
QAndroidJniObject extStorage = QAndroidJniObject::callStaticObjectMethod( "android/os/Environment", "getExternalStorageDirectory", "()Ljava/io/File;" );
QString extStoragePath = extStorage.callObjectMethod( "getAbsolutePath", "()Ljava/lang/String;" ).toString();

extStoragePath += '/';

absolutePath.replace( QStringLiteral( "/document/primary:" ), extStoragePath );
}

emit projectOpened( absolutePath );
}
}
33 changes: 33 additions & 0 deletions src/androidprojectsource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/***************************************************************************
androidprojectsource.h - AndroidProjectSource
---------------------
begin : 19.3.2018
copyright : (C) 2018 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#ifndef ANDROIDPROJECTSOURCE_H
#define ANDROIDPROJECTSOURCE_H

#include <QAndroidActivityResultReceiver>
#include "projectsource.h"

class AndroidProjectSource : public ProjectSource, public QAndroidActivityResultReceiver
{
public:
AndroidProjectSource();

void handleActivityResult( int receiverRequestCode, int resultCode, const QAndroidJniObject& data ) override;

};

#endif // ANDROIDPROJECTSOURCE_H
5 changes: 5 additions & 0 deletions src/platformutilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ void PlatformUtilities::open( const QString& data, const QString& type )
Q_UNUSED( type )
}

ProjectSource *PlatformUtilities::openProject()
{
return nullptr;
}

2 changes: 2 additions & 0 deletions src/platformutilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QObject>

class PictureSource;
class ProjectSource;

class PlatformUtilities : public QObject
{
Expand All @@ -48,5 +49,6 @@ class PlatformUtilities : public QObject

Q_INVOKABLE virtual void open( const QString& data, const QString& type );

Q_INVOKABLE virtual ProjectSource *openProject();
};
#endif // PLATFORMUTILITIES_H
22 changes: 22 additions & 0 deletions src/projectsource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/***************************************************************************
projectsource.cpp - ProjectSource
---------------------
begin : 19.3.2018
copyright : (C) 2018 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "projectsource.h"
#include <QDebug>

ProjectSource::ProjectSource( QObject *parent ) : QObject( parent )
{
}
45 changes: 45 additions & 0 deletions src/projectsource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/***************************************************************************
projectsource.h - ProjectSource
---------------------
begin : 19.3.2018
copyright : (C) 2018 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef PROJECTSOURCE_H
#define PROJECTSOURCE_H

#include <QObject>

/**
* This class represents an ongoing open project intent.
* It will notify the system with the projectOpened() signal
* once the user has selected a project to open.
*
* The default implementation does nothing. You probably
* want to have a look at the AndroidProjectSource subclass.
*/
class ProjectSource : public QObject
{
Q_OBJECT
public:
explicit ProjectSource( QObject *parent = nullptr );

virtual ~ProjectSource() = default;

signals:
/**
* This signal communitcates, when a project has been opened.
*/
void projectOpened( const QString& path );
};

#endif // PROJECTSOURCE_H
2 changes: 2 additions & 0 deletions src/qgismobileapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include "distancearea.h"
#include "coordinatetransformer.h"
#include "picturesource.h"
#include "projectsource.h"

QgisMobileapp::QgisMobileapp( QgsApplication* app, QObject* parent )
: QQmlApplicationEngine( parent )
Expand Down Expand Up @@ -148,6 +149,7 @@ void QgisMobileapp::initDeclarative()
qmlRegisterType<Rubberband>( "org.qgis", 1, 0, "Rubberband" );
qmlRegisterType<RubberbandModel>( "org.qgis", 1, 0, "RubberbandModel" );
qmlRegisterType<PictureSource>( "org.qgis", 1, 0, "PictureSource" );
qmlRegisterType<ProjectSource>( "org.qgis", 1, 0, "ProjectSource" );
qmlRegisterType<MessageLogModel>( "org.qgis", 1, 0, "MessageLogModel" );
qmlRegisterType<AttributeFormModel>( "org.qfield", 1, 0, "AttributeFormModel" );
qmlRegisterType<FeatureModel>( "org.qfield", 1, 0, "FeatureModel" );
Expand Down
16 changes: 15 additions & 1 deletion src/qml/qgismobileapp.qml
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,16 @@ ApplicationWindow {
}

Controls.MenuItem {
id: openProjectMenuItem
property ProjectSource __projectSource

text: qsTr( "Open Project" )
iconSource: Style.getThemeIcon( "ic_map_white_24dp" )
onTriggered: {
openProjectDialog.visible = true
__projectSource = platformUtilities.openProject()

if (!__projectSource)
openProjectDialog.visible = true
}
}

Expand Down Expand Up @@ -904,4 +910,12 @@ ApplicationWindow {
alreadyCloseRequested = false
}
}

Connections {
target: openProjectMenuItem.__projectSource

onProjectOpened: {
iface.loadProject( path )
}
}
}
12 changes: 8 additions & 4 deletions src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ QT += widgets concurrent xml positioning printsupport svg sql opengl sensors qui
android {
QT += androidextras
HEADERS += androidplatformutilities.h \
androidpicturesource.h
androidpicturesource.h\
androidprojectsource.h
SOURCES += androidplatformutilities.cpp \
androidpicturesource.cpp
androidpicturesource.cpp \
androidprojectsource.cpp
}
include( ../qfield.pri )
include( ../qgis.pri )
Expand Down Expand Up @@ -54,7 +56,8 @@ HEADERS += \
featurelistmodel.h \
distancearea.h \
coordinatetransformer.h \
expressioncontextutils.h
expressioncontextutils.h \
projectsource.h

SOURCES += \
appinterface.cpp \
Expand Down Expand Up @@ -93,7 +96,8 @@ SOURCES += \
featurelistmodel.cpp \
distancearea.cpp \
coordinatetransformer.cpp \
expressioncontextutils.cpp
expressioncontextutils.cpp \
projectsource.cpp

INCLUDEPATH += ../3rdparty/tessellate
LIBS += ../3rdparty/tessellate/libtessellate.a
Expand Down

0 comments on commit bc3d4d4

Please sign in to comment.