QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
18 #include "qgslayout.h"
19 #include "qgsreadwritecontext.h"
20 #include "qgsproject.h"
21 #include "qgslayoutview.h"
22 #include <QMessageBox>
23 
25  : QgsLayoutCustomDropHandler( parent )
26 {
27 
28 }
29 
30 bool QgsLayoutQptDropHandler::handleFileDrop( QgsLayoutDesignerInterface *iface, QPointF, const QString &file )
31 {
32  QFileInfo fi( file );
33  if ( fi.suffix().compare( QLatin1String( "qpt" ), Qt::CaseInsensitive ) != 0 )
34  return false;
35 
36  QFile templateFile( file );
37  if ( !templateFile.open( QIODevice::ReadOnly ) )
38  {
39  QMessageBox::warning( iface->view(), tr( "Load from Template" ), tr( "Could not read template file." ) );
40  return true;
41  }
42 
43  QDomDocument templateDoc;
44  QgsReadWriteContext context;
45  context.setPathResolver( QgsProject::instance()->pathResolver() );
46  if ( templateDoc.setContent( &templateFile ) )
47  {
48  bool ok = false;
49  QList< QgsLayoutItem * > items = iface->layout()->loadFromTemplate( templateDoc, context, false, &ok );
50  if ( !ok )
51  {
52  QMessageBox::warning( iface->view(), tr( "Load from Template" ), tr( "Could not read template file." ) );
53  return true;
54  }
55  else
56  {
57  whileBlocking( iface->layout() )->deselectAll();
58  iface->selectItems( items );
59  }
60  }
61 
62  return true;
63 }
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 QgsLayout * layout()=0
Returns the current layout displayed in the designer.
virtual void selectItems(const QList< QgsLayoutItem * > &items)=0
Selects the specified items.
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.
Definition: qgslayout.cpp:609
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:467
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:537