Skip to content

Commit

Permalink
[FEATURE] Drag qpt to QGIS to create new composer from template
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 17, 2016
1 parent 8194de9 commit 17e318b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -5120,6 +5120,47 @@ void QgisApp::openLayerDefinition( const QString & path )
}
}

void QgisApp::openTemplate( const QString& fileName )
{
QFile templateFile;
templateFile.setFileName( fileName );

if ( !templateFile.open( QIODevice::ReadOnly ) )
{
messageBar()->pushMessage( tr( "Load template" ), tr( "Could not read template file" ), QgsMessageBar::WARNING );
return;
}

QString title;
if ( !uniqueComposerTitle( this, title, true ) )
{
return;
}

QgsComposer* newComposer = createNewComposer( title );
if ( !newComposer )
{
messageBar()->pushMessage( tr( "Load template" ), tr( "Could not create composer" ), QgsMessageBar::WARNING );
return;
}

bool loadedOk = false;
QDomDocument templateDoc;
if ( templateDoc.setContent( &templateFile, false ) )
{
loadedOk = newComposer->loadFromTemplate( templateDoc, true );
newComposer->activate();
}

if ( !loadedOk )
{
newComposer->close();
deleteComposer( newComposer );
newComposer = nullptr;
messageBar()->pushMessage( tr( "Load template" ), tr( "Could not load template file" ), QgsMessageBar::WARNING );
}
}

// Open the project file corresponding to the
// path at the given index in mRecentProjectPaths
void QgisApp::openProject( QAction *action )
Expand Down Expand Up @@ -5246,6 +5287,10 @@ void QgisApp::openFile( const QString & fileName )
{
openLayerDefinition( fileName );
}
else if ( fi.completeSuffix() == "qpt" )
{
openTemplate( fileName );
}
else if ( fi.completeSuffix() == "py" )
{
runScript( fileName );
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -203,6 +203,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void openProject( const QString & fileName );

void openLayerDefinition( const QString & filename );

//! Open a composer template file and create a new composition
void openTemplate( const QString& fileName );

/** Opens a qgis project file
@returns false if unable to open the project
*/
Expand Down

0 comments on commit 17e318b

Please sign in to comment.