|
| 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 | + |
| 16 | +#include "qgslayoutqptdrophandler.h" |
| 17 | +#include "qgslayoutdesignerinterface.h" |
| 18 | +#include "qgslayout.h" |
| 19 | +#include "qgsreadwritecontext.h" |
| 20 | +#include "qgsproject.h" |
| 21 | +#include "qgslayoutview.h" |
| 22 | +#include <QMessageBox> |
| 23 | + |
| 24 | +QgsLayoutQptDropHandler::QgsLayoutQptDropHandler( QObject *parent ) |
| 25 | + : QgsLayoutCustomDropHandler( parent ) |
| 26 | +{ |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +bool QgsLayoutQptDropHandler::handleFileDrop( QgsLayoutDesignerInterface *iface, 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 | +} |
0 commit comments