QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
qgslayoutqptdrophandler.h
QgsReadWriteContext::setPathResolver
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
Definition: qgsreadwritecontext.cpp:52
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsLayoutDesignerInterface::view
virtual QgsLayoutView * view()=0
Returns the layout view utilized by the designer.
qgsreadwritecontext.h
qgslayoutview.h
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:458
QgsLayoutDesignerInterface::selectItems
virtual void selectItems(const QList< QgsLayoutItem * > &items)=0
Selects the specified items.
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsLayout::loadFromTemplate
QList< QgsLayoutItem * > loadFromTemplate(const QDomDocument &document, const QgsReadWriteContext &context, bool clearExisting=true, bool *ok=nullptr)
Load a layout template document.
Definition: qgslayout.cpp:608
QgsLayoutQptDropHandler::QgsLayoutQptDropHandler
QgsLayoutQptDropHandler(QObject *parent=nullptr)
constructor
Definition: qgslayoutqptdrophandler.cpp:24
QgsLayoutQptDropHandler::handleFileDrop
bool handleFileDrop(QgsLayoutDesignerInterface *iface, QPointF point, const QString &file) override
Called when the specified file has been dropped onto a QGIS layout.
Definition: qgslayoutqptdrophandler.cpp:30
qgslayout.h
QgsLayoutDesignerInterface::layout
virtual QgsLayout * layout()=0
Returns the current layout displayed in the designer.
QgsLayoutCustomDropHandler
Definition: qgslayoutcustomdrophandler.h:33
QgsLayoutDesignerInterface
Definition: qgslayoutdesignerinterface.h:54
qgslayoutdesignerinterface.h
qgsproject.h