13
13
* *
14
14
***************************************************************************/
15
15
16
+ #include < QMimeData>
17
+ #include < QTextStream>
18
+
16
19
#include " qgslayertreemodel.h"
17
20
18
21
#include " qgslayertree.h"
19
22
#include " qgslayertreeutils.h"
20
23
#include " qgslayertreemodellegendnode.h"
21
24
#include " qgsproject.h"
22
25
#include " qgsapplication.h"
23
-
24
- #include < QMimeData>
25
- #include < QTextStream>
26
-
27
26
#include " qgsdataitem.h"
28
27
#include " qgsmaphittest.h"
29
28
#include " qgsmaplayer.h"
34
33
#include " qgsrenderer.h"
35
34
#include " qgssymbollayerutils.h"
36
35
#include " qgsvectorlayer.h"
36
+ #include " qgslayerdefinition.h"
37
+
37
38
38
39
QgsLayerTreeModel::QgsLayerTreeModel ( QgsLayerTree *rootNode, QObject *parent )
39
40
: QAbstractItemModel( parent )
@@ -1052,15 +1053,25 @@ QMimeData *QgsLayerTreeModel::mimeData( const QModelIndexList &indexes ) const
1052
1053
1053
1054
QMimeData *mimeData = new QMimeData ();
1054
1055
1055
- QDomDocument doc;
1056
- QDomElement rootElem = doc.createElement ( QStringLiteral ( " layer_tree_model_data" ) );
1056
+ QDomDocument layerTreeDoc;
1057
+ QDomElement rootLayerTreeElem = layerTreeDoc.createElement ( QStringLiteral ( " layer_tree_model_data" ) );
1058
+
1057
1059
for ( QgsLayerTreeNode *node : qgis::as_const ( nodesFinal ) )
1058
- node->writeXml ( rootElem, QgsReadWriteContext () );
1059
- doc.appendChild ( rootElem );
1060
- QString txt = doc.toString ();
1060
+ {
1061
+ node->writeXml ( rootLayerTreeElem, QgsReadWriteContext () );
1062
+ }
1063
+ layerTreeDoc.appendChild ( rootLayerTreeElem );
1061
1064
1062
- mimeData->setData ( QStringLiteral ( " application/qgis.layertreemodeldata" ), txt.toUtf8 () );
1065
+ QString errorMessage;
1066
+ QgsReadWriteContext readWriteContext;
1067
+ QDomDocument layerDefinitionsDoc ( QStringLiteral ( " qgis-layer-definition" ) );
1068
+ QgsLayerDefinition::exportLayerDefinition ( layerDefinitionsDoc, nodesFinal, errorMessage, QgsReadWriteContext () );
1063
1069
1070
+ QString txt = layerDefinitionsDoc.toString ();
1071
+
1072
+ mimeData->setData ( QStringLiteral ( " application/qgis.layertreemodeldata" ), layerTreeDoc.toString ().toUtf8 () );
1073
+ mimeData->setData ( QStringLiteral ( " application/qgis.application.pid" ), QString::number ( QCoreApplication::applicationPid () ).toUtf8 () );
1074
+ mimeData->setData ( QStringLiteral ( " application/qgis.layertree.layerdefinitions" ), txt.toUtf8 () );
1064
1075
mimeData->setData ( QStringLiteral ( " application/x-vnd.qgis.qgis.uri" ), QgsMimeDataUtils::layerTreeNodesToUriList ( nodesFinal ) );
1065
1076
1066
1077
return mimeData;
@@ -1081,36 +1092,54 @@ bool QgsLayerTreeModel::dropMimeData( const QMimeData *data, Qt::DropAction acti
1081
1092
if ( !QgsLayerTree::isGroup ( nodeParent ) )
1082
1093
return false ;
1083
1094
1084
- QByteArray encodedData = data->data ( QStringLiteral ( " application/qgis.layertreemodeldata" ) );
1095
+ if ( parent.isValid () && row == -1 )
1096
+ row = 0 ; // if dropped directly onto group item, insert at first position
1085
1097
1086
- QDomDocument doc;
1087
- if ( !doc.setContent ( QString::fromUtf8 ( encodedData ) ) )
1088
- return false ;
1098
+ // if we are coming from another QGIS instance, we need to add the layers too
1099
+ bool ok = false ;
1100
+ // the application pid is only provided from QGIS 3.14, so do not check to OK before defaulting to moving in the legend
1101
+ int qgisPid = data->data ( QStringLiteral ( " application/qgis.application.pid" ) ).toInt ( &ok );
1089
1102
1090
- QDomElement rootElem = doc.documentElement ();
1091
- if ( rootElem.tagName () != QLatin1String ( " layer_tree_model_data" ) )
1092
- return false ;
1103
+ if ( ok && qgisPid != QString::number ( QCoreApplication::applicationPid () ) )
1104
+ {
1105
+ QByteArray encodedLayerDefinitionData = data->data ( QStringLiteral ( " application/qgis.layertree.layerdefinitions" ) );
1106
+ QDomDocument layerDefinitionDoc;
1107
+ if ( !layerDefinitionDoc.setContent ( QString::fromUtf8 ( encodedLayerDefinitionData ) ) )
1108
+ return false ;
1109
+ QgsReadWriteContext context;
1110
+ QString errorMessage;
1111
+ QgsLayerDefinition::loadLayerDefinition ( layerDefinitionDoc, QgsProject::instance (), QgsLayerTree::toGroup ( nodeParent ), errorMessage, context );
1112
+ emit messageEmitted ( tr ( " New layers added from another QGIS instance" ) );
1113
+ }
1114
+ else
1115
+ {
1116
+ QByteArray encodedLayerTreeData = data->data ( QStringLiteral ( " application/qgis.layertreemodeldata" ) );
1093
1117
1094
- QList<QgsLayerTreeNode *> nodes;
1118
+ QDomDocument layerTreeDoc;
1119
+ if ( !layerTreeDoc.setContent ( QString::fromUtf8 ( encodedLayerTreeData ) ) )
1120
+ return false ;
1095
1121
1096
- QDomElement elem = rootElem.firstChildElement ();
1097
- while ( !elem.isNull () )
1098
- {
1099
- QgsLayerTreeNode *node = QgsLayerTreeNode::readXml ( elem, QgsProject::instance () );
1100
- if ( node )
1101
- nodes << node;
1122
+ QDomElement rootLayerTreeElem = layerTreeDoc.documentElement ();
1123
+ if ( rootLayerTreeElem.tagName () != QLatin1String ( " layer_tree_model_data" ) )
1124
+ return false ;
1102
1125
1103
- elem = elem.nextSiblingElement ();
1104
- }
1126
+ QList<QgsLayerTreeNode *> nodes;
1105
1127
1106
- if ( nodes.isEmpty () )
1107
- return false ;
1128
+ QDomElement elem = rootLayerTreeElem.firstChildElement ();
1129
+ while ( !elem.isNull () )
1130
+ {
1131
+ QgsLayerTreeNode *node = QgsLayerTreeNode::readXml ( elem, QgsProject::instance () );
1132
+ if ( node )
1133
+ nodes << node;
1108
1134
1109
- if ( parent. isValid () && row == - 1 )
1110
- row = 0 ; // if dropped directly onto group item, insert at first position
1135
+ elem = elem. nextSiblingElement ();
1136
+ }
1111
1137
1112
- QgsLayerTree::toGroup ( nodeParent )->insertChildNodes ( row, nodes );
1138
+ if ( nodes.isEmpty () )
1139
+ return false ;
1113
1140
1141
+ QgsLayerTree::toGroup ( nodeParent )->insertChildNodes ( row, nodes );
1142
+ }
1114
1143
return true ;
1115
1144
}
1116
1145
0 commit comments