Skip to content

Commit fc10b77

Browse files
committedSep 20, 2017
[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
1 parent 15e650d commit fc10b77

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
979979

980980
QgsApplication::dataItemProviderRegistry()->addProvider( new QgsQlrDataItemProvider() );
981981
registerCustomDropHandler( new QgsQlrDropHandler() );
982+
QgsApplication::dataItemProviderRegistry()->addProvider( new QgsQptDataItemProvider() );
983+
registerCustomDropHandler( new QgsQptDropHandler() );
982984

983985
mSplash->showMessage( tr( "Starting Python" ), Qt::AlignHCenter | Qt::AlignBottom );
984986
qApp->processEvents();

‎src/app/qgsappbrowserproviders.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,97 @@ void QgsQlrDropHandler::handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri )
7878
QString path = uri.uri;
7979
QgisApp::instance()->openLayerDefinition( path );
8080
}
81+
82+
//
83+
// QgsQptDataItemProvider
84+
//
85+
86+
QString QgsQptDataItemProvider::name()
87+
{
88+
return QStringLiteral( "QPT" );
89+
}
90+
91+
int QgsQptDataItemProvider::capabilities()
92+
{
93+
return QgsDataProvider::File;
94+
}
95+
96+
QgsDataItem *QgsQptDataItemProvider::createDataItem( const QString &path, QgsDataItem *parentItem )
97+
{
98+
QFileInfo fileInfo( path );
99+
100+
if ( fileInfo.suffix().compare( QStringLiteral( "qpt" ), Qt::CaseInsensitive ) == 0 )
101+
{
102+
return new QgsQptDataItem( parentItem, fileInfo.fileName(), path );
103+
}
104+
return nullptr;
105+
}
106+
107+
//
108+
// QgsQptDropHandler
109+
//
110+
111+
QString QgsQptDropHandler::customUriProviderKey() const
112+
{
113+
return QStringLiteral( "qpt" );
114+
}
115+
116+
void QgsQptDropHandler::handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const
117+
{
118+
QString path = uri.uri;
119+
QgisApp::instance()->openTemplate( path );
120+
}
121+
122+
bool QgsQptDropHandler::handleFileDrop( const QString &file )
123+
{
124+
QFileInfo fi( file );
125+
if ( fi.completeSuffix().compare( QStringLiteral( "qpt" ), Qt::CaseInsensitive ) == 0 )
126+
{
127+
QgisApp::instance()->openTemplate( file );
128+
return true;
129+
}
130+
return false;
131+
}
132+
133+
//
134+
// QgsQptDataItem
135+
//
136+
137+
QgsQptDataItem::QgsQptDataItem( QgsDataItem *parent, const QString &name, const QString &path )
138+
: QgsDataItem( QgsDataItem::Custom, parent, name, path )
139+
{
140+
setState( QgsDataItem::Populated ); // no children
141+
setIconName( QStringLiteral( ":/images/icons/qgis-icon-16x16.png" ) );
142+
setToolTip( QDir::toNativeSeparators( path ) );
143+
}
144+
145+
bool QgsQptDataItem::hasDragEnabled() const
146+
{
147+
return true;
148+
}
149+
150+
QgsMimeDataUtils::Uri QgsQptDataItem::mimeUri() const
151+
{
152+
QgsMimeDataUtils::Uri u;
153+
u.layerType = QStringLiteral( "custom" );
154+
u.providerKey = QStringLiteral( "qpt" );
155+
u.name = name();
156+
u.uri = path();
157+
return u;
158+
}
159+
160+
bool QgsQptDataItem::handleDoubleClick()
161+
{
162+
QgisApp::instance()->openTemplate( path() );
163+
return true;
164+
}
165+
166+
QList<QAction *> QgsQptDataItem::actions()
167+
{
168+
QAction *newLayout = new QAction( tr( "New Layout from Template" ), this );
169+
connect( newLayout, &QAction::triggered, this, [ = ]
170+
{
171+
QgisApp::instance()->openTemplate( path() );
172+
} );
173+
return QList<QAction *>() << newLayout;
174+
}

‎src/app/qgsappbrowserproviders.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include "qgsdataprovider.h"
2020
#include "qgscustomdrophandler.h"
2121

22+
/**
23+
* Custom data item for QLR files.
24+
*/
2225
class QgsQlrDataItem : public QgsLayerItem
2326
{
2427
Q_OBJECT
@@ -31,6 +34,9 @@ class QgsQlrDataItem : public QgsLayerItem
3134

3235
};
3336

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

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

59+
/**
60+
* Custom data item for QPT print template files.
61+
*/
62+
class QgsQptDataItem : public QgsDataItem
63+
{
64+
Q_OBJECT
65+
66+
public:
67+
68+
QgsQptDataItem( QgsDataItem *parent, const QString &name, const QString &path );
69+
bool hasDragEnabled() const override;
70+
QgsMimeDataUtils::Uri mimeUri() const override;
71+
bool handleDoubleClick() override;
72+
QList< QAction * > actions() override;
73+
74+
75+
};
76+
77+
/**
78+
* Data item provider for showing QPT print templates in the browser.
79+
*/
80+
class QgsQptDataItemProvider : public QgsDataItemProvider
81+
{
82+
public:
83+
QString name() override;
84+
int capabilities() override;
85+
QgsDataItem *createDataItem( const QString &path, QgsDataItem *parentItem ) override;
86+
};
87+
88+
/**
89+
* Handles drag and drop of QPT print templates to app.
90+
*/
91+
class QgsQptDropHandler : public QgsCustomDropHandler
92+
{
93+
public:
94+
95+
QString customUriProviderKey() const override;
96+
void handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const override;
97+
bool handleFileDrop( const QString &file ) override;
98+
};
99+
100+
50101
#endif // QGSAPPBROWSERPROVIDERS_H

0 commit comments

Comments
 (0)
Please sign in to comment.