Skip to content

Commit

Permalink
[FEATURE] Port new memory layer plugin to core
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 4, 2014
1 parent a435441 commit 486d408
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 0 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -155,6 +155,7 @@
<file>themes/default/mActionComposerManager.svg</file>
<file>themes/default/mActionContextHelp.png</file>
<file>themes/default/mActionCopySelected.png</file>
<file>themes/default/mActionCreateMemory.png</file>
<file>themes/default/mActionCustomProjection.svg</file>
<file>themes/default/mActionDecreaseBrightness.svg</file>
<file>themes/default/mActionDecreaseContrast.svg</file>
Expand Down
Binary file added images/themes/default/mActionCreateMemory.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -86,6 +86,7 @@
%Include qgsmessagelogviewer.sip
%Include qgsmessageviewer.sip
%Include qgsnewhttpconnection.sip
%Include qgsnewmemorylayerdialog.sip
%Include qgsnewvectorlayerdialog.sip
%Include qgsnumericsortlistviewitem.sip
%Include qgsowssourceselect.sip
Expand Down
30 changes: 30 additions & 0 deletions python/gui/qgsnewmemorylayerdialog.sip
@@ -0,0 +1,30 @@
class QgsNewMemoryLayerDialog : QDialog
{
%TypeHeaderCode
#include <qgsnewmemorylayerdialog.h>
%End

public:

/**Runs the dialoag and creates a new memory layer
* @param parent parent widget
* @returns new memory layer
*/
static QgsVectorLayer* runAndCreateLayer( QWidget* parent = 0 );

QgsNewMemoryLayerDialog( QWidget *parent /TransferThis/ = 0, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
~QgsNewMemoryLayerDialog();

/**Returns the selected geometry type*/
QGis::WkbType selectedType() const;

/**Returns the selected crs id*/
QString selectedCrsId() const;

/**Returns the layer name*/
QString layerName() const;

protected slots:

void on_mChangeSrsButton_clicked();
};
21 changes: 21 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -158,6 +158,7 @@
#include "qgsmessagelog.h"
#include "qgsmultibandcolorrenderer.h"
#include "qgsnewvectorlayerdialog.h"
#include "qgsnewmemorylayerdialog.h"
#include "qgsoptions.h"
#include "qgspluginlayer.h"
#include "qgspluginlayerregistry.h"
Expand Down Expand Up @@ -1102,6 +1103,7 @@ void QgisApp::createActions()

connect( mActionNewVectorLayer, SIGNAL( triggered() ), this, SLOT( newVectorLayer() ) );
connect( mActionNewSpatiaLiteLayer, SIGNAL( triggered() ), this, SLOT( newSpatialiteLayer() ) );
connect( mActionNewMemoryLayer, SIGNAL( triggered() ), this, SLOT( newMemoryLayer() ) );
connect( mActionShowRasterCalculator, SIGNAL( triggered() ), this, SLOT( showRasterCalculator() ) );
connect( mActionEmbedLayers, SIGNAL( triggered() ), this, SLOT( embedLayers() ) );
connect( mActionAddLayerDefinition, SIGNAL( triggered() ), this, SLOT( addLayerDefinition() ) );
Expand Down Expand Up @@ -1608,12 +1610,14 @@ void QgisApp::createToolBars()
bt->setPopupMode( QToolButton::MenuButtonPopup );
bt->addAction( mActionNewSpatiaLiteLayer );
bt->addAction( mActionNewVectorLayer );
bt->addAction( mActionNewMemoryLayer );

QAction* defNewLayerAction = mActionNewVectorLayer;
switch ( settings.value( "/UI/defaultNewLayer", 1 ).toInt() )
{
case 0: defNewLayerAction = mActionNewSpatiaLiteLayer; break;
case 1: defNewLayerAction = mActionNewVectorLayer; break;
case 2: defNewLayerAction = mActionNewMemoryLayer; break;
}
bt->setDefaultAction( defNewLayerAction );
QAction* newLayerAction = mLayerToolBar->addWidget( bt );
Expand Down Expand Up @@ -1839,6 +1843,7 @@ void QgisApp::setTheme( QString theThemeName )
mActionSetLayerCRS->setIcon( QgsApplication::getThemeIcon( "/mActionSetLayerCRS.png" ) );
mActionSetProjectCRSFromLayer->setIcon( QgsApplication::getThemeIcon( "/mActionSetProjectCRSFromLayer.png" ) );
mActionNewVectorLayer->setIcon( QgsApplication::getThemeIcon( "/mActionNewVectorLayer.svg" ) );
mActionNewMemoryLayer->setIcon( QgsApplication::getThemeIcon( "/mActionCreateMemory.png" ) );
mActionAddAllToOverview->setIcon( QgsApplication::getThemeIcon( "/mActionAddAllToOverview.svg" ) );
mActionHideAllLayers->setIcon( QgsApplication::getThemeIcon( "/mActionHideAllLayers.png" ) );
mActionShowAllLayers->setIcon( QgsApplication::getThemeIcon( "/mActionShowAllLayers.png" ) );
Expand Down Expand Up @@ -3708,6 +3713,20 @@ void QgisApp::newVectorLayer()
}
}

void QgisApp::newMemoryLayer()
{
QgsVectorLayer* newLayer = QgsNewMemoryLayerDialog::runAndCreateLayer( this );

if ( newLayer )
{
//then add the layer to the view
QList< QgsMapLayer* > layers;
layers << newLayer;

QgsMapLayerRegistry::instance()->addMapLayers( layers );
}
}

void QgisApp::newSpatialiteLayer()
{
QgsNewSpatialiteLayerDialog spatialiteDialog( this );
Expand Down Expand Up @@ -10155,6 +10174,8 @@ void QgisApp::toolButtonActionTriggered( QAction *action )
settings.setValue( "/UI/defaultNewLayer", 0 );
else if ( action == mActionNewVectorLayer )
settings.setValue( "/UI/defaultNewLayer", 1 );
else if ( action == mActionNewMemoryLayer )
settings.setValue( "/UI/defaultNewLayer", 2 );
bt->setDefaultAction( action );
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -882,6 +882,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

//! Create a new empty vector layer
void newVectorLayer();
//! Create a new memory layer
void newMemoryLayer();
//! Create a new empty spatialite layer
void newSpatialiteLayer();
//! Print the current map view frame
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -195,6 +195,7 @@ qgsmessagebaritem.cpp
qgsmessagelogviewer.cpp
qgsmessageviewer.cpp
qgsnewhttpconnection.cpp
qgsnewmemorylayerdialog.cpp
qgsnewvectorlayerdialog.cpp
qgsnumericsortlistviewitem.cpp
qgsoptionsdialogbase.cpp
Expand Down Expand Up @@ -392,6 +393,7 @@ qgsmessagebar.h
qgsmessagelogviewer.h
qgsmessageviewer.h
qgsnewhttpconnection.h
qgsnewmemorylayerdialog.h
qgsnewvectorlayerdialog.h
qgsoptionsdialogbase.h
qgsowssourceselect.h
Expand Down
153 changes: 153 additions & 0 deletions src/gui/qgsnewmemorylayerdialog.cpp
@@ -0,0 +1,153 @@
/***************************************************************************
qgsnewmemorylayerdialog.cpp
-------------------
begin : September 2014
copyright : (C) 2014 by Nyall Dawson, Marco Hugentobler
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* 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 "qgsnewmemorylayerdialog.h"
#include "qgsapplication.h"
#include "qgis.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsgenericprojectionselector.h"
#include "qgsproviderregistry.h"
#include "qgsvectordataprovider.h"

#include <QPushButton>
#include <QComboBox>
#include <QLibrary>
#include <QSettings>
#include <QFileDialog>

QgsVectorLayer *QgsNewMemoryLayerDialog::runAndCreateLayer( QWidget *parent )
{
QgsNewMemoryLayerDialog dialog( parent );
if ( dialog.exec() == QDialog::Rejected )
{
return 0;
}

QGis::WkbType geometrytype = dialog.selectedType();
QString crsId = dialog.selectedCrsId();

QString geomType;
switch ( geometrytype )
{
case QGis::WKBPoint:
geomType = "point";
break;
case QGis::WKBLineString:
geomType = "linestring";
break;
case QGis::WKBPolygon:
geomType = "polygon";
break;
case QGis::WKBMultiPoint:
geomType = "multipoint";
break;
case QGis::WKBMultiLineString:
geomType = "multilinestring";
break;
case QGis::WKBMultiPolygon:
geomType = "multipolygon";
break;
default:
geomType = "point";
}

QString layerProperties = geomType + QString( "?crs=%1" ).arg( crsId );
QString name = dialog.layerName().isEmpty() ? tr( "New scratch layer" ) : dialog.layerName();
QgsVectorLayer* newLayer = new QgsVectorLayer( layerProperties, name, QString( "memory" ) );
return newLayer;
}

QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
{
setupUi( this );

QSettings settings;
restoreGeometry( settings.value( "/Windows/NewMemoryLayer/geometry" ).toByteArray() );

mPointRadioButton->setChecked( true );

QgsCoordinateReferenceSystem srs;
srs.createFromOgcWmsCrs( settings.value( "/Projections/layerDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString() );
srs.validate();
mCrsId = srs.authid();
mSpatialRefSysEdit->setText( srs.authid() + " - " + srs.description() );

mNameLineEdit->setText( tr( "New scratch layer" ) );
}

QgsNewMemoryLayerDialog::~QgsNewMemoryLayerDialog()
{
QSettings settings;
settings.setValue( "/Windows/NewMemoryLayer/geometry", saveGeometry() );
}

QGis::WkbType QgsNewMemoryLayerDialog::selectedType() const
{
if ( mPointRadioButton->isChecked() )
{
return QGis::WKBPoint;
}
else if ( mLineRadioButton->isChecked() )
{
return QGis::WKBLineString;
}
else if ( mPolygonRadioButton->isChecked() )
{
return QGis::WKBPolygon;
}
else if ( mMultiPointRadioButton->isChecked() )
{
return QGis::WKBMultiPoint;
}
else if ( mMultiLineRadioButton->isChecked() )
{
return QGis::WKBMultiLineString;
}
else if ( mMultiPolygonRadioButton->isChecked() )
{
return QGis::WKBMultiPolygon;
}
return QGis::WKBUnknown;
}

QString QgsNewMemoryLayerDialog::layerName() const
{
return mNameLineEdit->text();
}

QString QgsNewMemoryLayerDialog::selectedCrsId() const
{
return mCrsId;
}

void QgsNewMemoryLayerDialog::on_mChangeSrsButton_clicked()
{
QgsGenericProjectionSelector *selector = new QgsGenericProjectionSelector( this );
selector->setMessage();
selector->setSelectedAuthId( mCrsId );
if ( selector->exec() )
{
mCrsId = selector->selectedAuthId();
mSpatialRefSysEdit->setText( mCrsId );
}
else
{
QApplication::restoreOverrideCursor();
}
delete selector;
}
57 changes: 57 additions & 0 deletions src/gui/qgsnewmemorylayerdialog.h
@@ -0,0 +1,57 @@
/***************************************************************************
qgsnewmemorylayerdialog.h
-------------------
begin : September 2014
copyright : (C) 2014 by Nyall Dawson, Marco Hugentobler
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* 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 QGSNEWMEMORYLAYERDIALOG_H
#define QGSNEWMEMORYLAYERDIALOG_H

#include "ui_qgsnewmemorylayerdialogbase.h"
#include "qgisgui.h"
#include "qgis.h"
#include "qgsvectorlayer.h"

class GUI_EXPORT QgsNewMemoryLayerDialog: public QDialog, private Ui::QgsNewMemoryLayerDialogBase
{
Q_OBJECT

public:

/**Runs the dialoag and creates a new memory layer
* @param parent parent widget
* @returns new memory layer
*/
static QgsVectorLayer* runAndCreateLayer( QWidget* parent = 0 );

QgsNewMemoryLayerDialog( QWidget *parent = 0, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
~QgsNewMemoryLayerDialog();

/**Returns the selected geometry type*/
QGis::WkbType selectedType() const;

/**Returns the selected crs id*/
QString selectedCrsId() const;

/**Returns the layer name*/
QString layerName() const;

protected slots:

void on_mChangeSrsButton_clicked();

private:
QString mCrsId;
};

#endif //QGSNEWMEMORYLAYERDIALOG_H
13 changes: 13 additions & 0 deletions src/ui/qgisapp.ui
Expand Up @@ -131,6 +131,7 @@
</property>
<addaction name="mActionNewVectorLayer"/>
<addaction name="mActionNewSpatiaLiteLayer"/>
<addaction name="mActionNewMemoryLayer"/>
</widget>
<widget class="QMenu" name="mAddLayerMenu">
<property name="title">
Expand Down Expand Up @@ -2270,6 +2271,18 @@ Acts on currently active editable layer</string>
<string>Hide Selected Layers</string>
</property>
</action>
<action name="mActionNewMemoryLayer">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionCreateMemory.png</normaloff>:/images/themes/default/mActionCreateMemory.png</iconset>
</property>
<property name="text">
<string>New Temporary Scratch Layer...</string>
</property>
<property name="toolTip">
<string>New temporary scratch layer</string>
</property>
</action>
</widget>
<resources>
<include location="../../images/images.qrc"/>
Expand Down

0 comments on commit 486d408

Please sign in to comment.