Skip to content
Permalink
Browse files

[FEATURE] Show QPT print templates in browser

Double clicking the template (or dragging and dropping to canvas)
results in a new layout being created from the template
  • Loading branch information
nyalldawson committed Sep 20, 2017
1 parent 15e650d commit fc10b77f45bb14e25a35dcf8ab0d9cc506df19b0
Showing with 147 additions and 0 deletions.
  1. +2 −0 src/app/qgisapp.cpp
  2. +94 −0 src/app/qgsappbrowserproviders.cpp
  3. +51 −0 src/app/qgsappbrowserproviders.h
@@ -979,6 +979,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh

QgsApplication::dataItemProviderRegistry()->addProvider( new QgsQlrDataItemProvider() );
registerCustomDropHandler( new QgsQlrDropHandler() );
QgsApplication::dataItemProviderRegistry()->addProvider( new QgsQptDataItemProvider() );
registerCustomDropHandler( new QgsQptDropHandler() );

mSplash->showMessage( tr( "Starting Python" ), Qt::AlignHCenter | Qt::AlignBottom );
qApp->processEvents();
@@ -78,3 +78,97 @@ void QgsQlrDropHandler::handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri )
QString path = uri.uri;
QgisApp::instance()->openLayerDefinition( path );
}

//
// QgsQptDataItemProvider
//

QString QgsQptDataItemProvider::name()
{
return QStringLiteral( "QPT" );
}

int QgsQptDataItemProvider::capabilities()
{
return QgsDataProvider::File;
}

QgsDataItem *QgsQptDataItemProvider::createDataItem( const QString &path, QgsDataItem *parentItem )
{
QFileInfo fileInfo( path );

if ( fileInfo.suffix().compare( QStringLiteral( "qpt" ), Qt::CaseInsensitive ) == 0 )
{
return new QgsQptDataItem( parentItem, fileInfo.fileName(), path );
}
return nullptr;
}

//
// QgsQptDropHandler
//

QString QgsQptDropHandler::customUriProviderKey() const
{
return QStringLiteral( "qpt" );
}

void QgsQptDropHandler::handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const
{
QString path = uri.uri;
QgisApp::instance()->openTemplate( path );
}

bool QgsQptDropHandler::handleFileDrop( const QString &file )
{
QFileInfo fi( file );
if ( fi.completeSuffix().compare( QStringLiteral( "qpt" ), Qt::CaseInsensitive ) == 0 )
{
QgisApp::instance()->openTemplate( file );
return true;
}
return false;
}

//
// QgsQptDataItem
//

QgsQptDataItem::QgsQptDataItem( QgsDataItem *parent, const QString &name, const QString &path )
: QgsDataItem( QgsDataItem::Custom, parent, name, path )
{
setState( QgsDataItem::Populated ); // no children
setIconName( QStringLiteral( ":/images/icons/qgis-icon-16x16.png" ) );
setToolTip( QDir::toNativeSeparators( path ) );
}

bool QgsQptDataItem::hasDragEnabled() const
{
return true;
}

QgsMimeDataUtils::Uri QgsQptDataItem::mimeUri() const
{
QgsMimeDataUtils::Uri u;
u.layerType = QStringLiteral( "custom" );
u.providerKey = QStringLiteral( "qpt" );
u.name = name();
u.uri = path();
return u;
}

bool QgsQptDataItem::handleDoubleClick()
{
QgisApp::instance()->openTemplate( path() );
return true;
}

QList<QAction *> QgsQptDataItem::actions()
{
QAction *newLayout = new QAction( tr( "New Layout from Template" ), this );
connect( newLayout, &QAction::triggered, this, [ = ]
{
QgisApp::instance()->openTemplate( path() );
} );
return QList<QAction *>() << newLayout;
}
@@ -19,6 +19,9 @@
#include "qgsdataprovider.h"
#include "qgscustomdrophandler.h"

/**
* Custom data item for QLR files.
*/
class QgsQlrDataItem : public QgsLayerItem
{
Q_OBJECT
@@ -31,6 +34,9 @@ class QgsQlrDataItem : public QgsLayerItem

};

/**
* Data item provider for showing QLR layer files in the browser.
*/
class QgsQlrDataItemProvider : public QgsDataItemProvider
{
public:
@@ -39,6 +45,9 @@ class QgsQlrDataItemProvider : public QgsDataItemProvider
QgsDataItem *createDataItem( const QString &path, QgsDataItem *parentItem ) override;
};

/**
* Handles drag and drop of QLR files to app.
*/
class QgsQlrDropHandler : public QgsCustomDropHandler
{
public:
@@ -47,4 +56,46 @@ class QgsQlrDropHandler : public QgsCustomDropHandler
void handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const override;
};

/**
* Custom data item for QPT print template files.
*/
class QgsQptDataItem : public QgsDataItem
{
Q_OBJECT

public:

QgsQptDataItem( QgsDataItem *parent, const QString &name, const QString &path );
bool hasDragEnabled() const override;
QgsMimeDataUtils::Uri mimeUri() const override;
bool handleDoubleClick() override;
QList< QAction * > actions() override;


};

/**
* Data item provider for showing QPT print templates in the browser.
*/
class QgsQptDataItemProvider : public QgsDataItemProvider
{
public:
QString name() override;
int capabilities() override;
QgsDataItem *createDataItem( const QString &path, QgsDataItem *parentItem ) override;
};

/**
* Handles drag and drop of QPT print templates to app.
*/
class QgsQptDropHandler : public QgsCustomDropHandler
{
public:

QString customUriProviderKey() const override;
void handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const override;
bool handleFileDrop( const QString &file ) override;
};


#endif // QGSAPPBROWSERPROVIDERS_H

0 comments on commit fc10b77

Please sign in to comment.
You can’t perform that action at this time.