QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgslayoutqptdrophandler.cpp"
19#include "qgslayout.h"
20#include "qgsreadwritecontext.h"
21#include "qgsproject.h"
22#include "qgslayoutview.h"
23#include <QMessageBox>
24
30
31bool QgsLayoutQptDropHandler::handleFileDrop( QgsLayoutDesignerInterface *iface, QPointF, const QString &file )
32{
33 const QFileInfo fi( file );
34 if ( fi.suffix().compare( QLatin1String( "qpt" ), Qt::CaseInsensitive ) != 0 )
35 return false;
36
37 QFile templateFile( file );
38 if ( !templateFile.open( QIODevice::ReadOnly ) )
39 {
40 QMessageBox::warning( iface->view(), tr( "Load from Template" ), tr( "Could not read template file." ) );
41 return true;
42 }
43
44 QDomDocument templateDoc;
45 QgsReadWriteContext context;
46 context.setPathResolver( QgsProject::instance()->pathResolver() );
47 if ( templateDoc.setContent( &templateFile ) )
48 {
49 bool ok = false;
50 const QList< QgsLayoutItem * > items = iface->layout()->loadFromTemplate( templateDoc, context, false, &ok );
51 if ( !ok )
52 {
53 QMessageBox::warning( iface->view(), tr( "Load from Template" ), tr( "Could not read template file." ) );
54 return true;
55 }
56 else
57 {
58 whileBlocking( iface->layout() )->deselectAll();
59 iface->selectItems( items );
60 }
61 }
62
63 return true;
64}
Abstract base class that may be implemented to handle new types of data to be dropped or pasted in QG...
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.
The class is used as a container of context for various read/write operations on other 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:5862