QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgslayoutqptdrophandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutqptdrophandler.cpp
3 ------------------------------
4 begin : December 2017
5 copyright : (C) 2017 by nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgslayout.h"
20#include "qgslayoutview.h"
21#include "qgsproject.h"
22#include "qgsreadwritecontext.h"
23
24#include <QMessageBox>
25
26#include "moc_qgslayoutqptdrophandler.cpp"
27
32
33bool QgsLayoutQptDropHandler::handleFileDrop( QgsLayoutDesignerInterface *iface, QPointF, const QString &file )
34{
35 const QFileInfo fi( file );
36 if ( fi.suffix().compare( QLatin1String( "qpt" ), Qt::CaseInsensitive ) != 0 )
37 return false;
38
39 QFile templateFile( file );
40 if ( !templateFile.open( QIODevice::ReadOnly ) )
41 {
42 QMessageBox::warning( iface->view(), tr( "Load from Template" ), tr( "Could not read template file." ) );
43 return true;
44 }
45
46 QDomDocument templateDoc;
47 QgsReadWriteContext context;
48 context.setPathResolver( QgsProject::instance()->pathResolver() );
49 if ( templateDoc.setContent( &templateFile ) )
50 {
51 bool ok = false;
52 const QList<QgsLayoutItem *> items = iface->layout()->loadFromTemplate( templateDoc, context, false, &ok );
53 if ( !ok )
54 {
55 QMessageBox::warning( iface->view(), tr( "Load from Template" ), tr( "Could not read template file." ) );
56 return true;
57 }
58 else
59 {
60 whileBlocking( iface->layout() )->deselectAll();
61 iface->selectItems( items );
62 }
63 }
64
65 return true;
66}
QgsLayoutCustomDropHandler(QObject *parent=nullptr)
Constructor for QgsLayoutCustomDropHandler.
A common interface for layout designer dialogs and widgets.
virtual QgsLayoutView * view()=0
Returns the layout view utilized by the designer.
virtual void selectItems(const QList< QgsLayoutItem * > &items)=0
Selects the specified items.
virtual QgsLayout * layout()=0
Returns the current layout displayed in the designer.
bool handleFileDrop(QgsLayoutDesignerInterface *iface, QPointF point, const QString &file) override
Called when the specified file has been dropped onto a QGIS layout.
QgsLayoutQptDropHandler(QObject *parent=nullptr)
constructor
QList< QgsLayoutItem * > loadFromTemplate(const QDomDocument &document, const QgsReadWriteContext &context, bool clearExisting=true, bool *ok=nullptr)
Load a layout template document.
static QgsProject * instance()
Returns the QgsProject singleton instance.
A container for the context for various read/write operations on objects.
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511