22 #include <QTextStream>
35 : QAbstractItemModel( parent )
36 , mRootNode( rootNode )
37 , mFlags( ShowLegend | AllowLegendChangeState )
38 , mAutoCollapseLegendNodesCount( -1 )
39 , mLegendFilterByScale( 0 )
40 , mLegendMapViewMupp( 0 )
41 , mLegendMapViewDpi( 0 )
42 , mLegendMapViewScale( 0 )
56 if ( !index.isValid() )
59 QObject* obj =
reinterpret_cast<QObject*
>( index.internalPointer() );
92 if ( column < 0 || column >=
columnCount( parent ) ||
93 row < 0 || row >=
rowCount( parent ) )
101 return QModelIndex();
108 return createIndex( row, column, static_cast<QObject*>( n->
children().at( row ) ) );
114 if ( !child.isValid() )
115 return QModelIndex();
128 return QModelIndex();
136 Q_ASSERT( parentNode );
139 if ( !grandParentNode )
140 return QModelIndex();
142 int row = grandParentNode->
children().indexOf( parentNode );
143 Q_ASSERT( row >= 0 );
145 return createIndex( row, 0, static_cast<QObject*>( parentNode ) );
151 if ( !index.isValid() || index.column() > 1 )
158 if ( role == Qt::DisplayRole || role == Qt::EditRole )
167 if ( nodeLayer->
customProperty(
"showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole )
176 else if ( role == Qt::DecorationRole && index.column() == 0 )
227 QPixmap pixmap( icon.pixmap( 16, 16 ) );
229 QPainter painter( &pixmap );
233 icon = QIcon( pixmap );
239 else if ( role == Qt::CheckStateRole )
260 else if ( role == Qt::FontRole )
266 f.setUnderline(
true );
269 else if ( role == Qt::ToolTipRole )
274 return layer->publicSource();
284 if ( !index.isValid() )
286 Qt::ItemFlags rootFlags = 0;
288 rootFlags |= Qt::ItemIsDropEnabled;
295 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
298 f |= Qt::ItemIsEditable;
307 f |= Qt::ItemIsDragEnabled;
311 f |= Qt::ItemIsUserCheckable;
314 f |= Qt::ItemIsDropEnabled;
326 bool res = sym->
setData( value, role );
328 emit dataChanged( index, index );
336 if ( role == Qt::CheckStateRole )
344 layer->
setVisible(( Qt::CheckState )value.toInt() );
351 group->
setVisible(( Qt::CheckState )value.toInt() );
357 else if ( role == Qt::EditRole )
366 emit dataChanged( index, index );
371 emit dataChanged( index, index );
381 return QModelIndex();
386 Q_ASSERT( row >= 0 );
387 return index( row, 0, parentIndex );
396 if ( child->
parent() == node )
416 QList<QgsLayerTreeNode*> nodes;
417 foreach ( QModelIndex
index, list )
430 QList<QgsLayerTreeNode*> nodesFinal;
448 return symNode ? symNode->
layerNode() : 0;
475 emit dataChanged( idx, idx );
479 beginRemoveRows( idx, 0, oldNodeCount - 1 );
501 if ( oldIndex.isValid() )
502 emit dataChanged( oldIndex, oldIndex );
503 if ( currentIndex.isValid() )
504 emit dataChanged( currentIndex, currentIndex );
608 beginInsertRows(
node2index( node ), indexFrom, indexTo );
613 QList<QgsLayerTreeNode*> children = node->
children();
614 QList<QgsLayerTreeLayer*> newLayerNodes;
615 for (
int i = indexFrom; i <= indexTo; ++i )
623 return newLayerNodes;
640 beginRemoveRows(
node2index( node ), indexFrom, indexTo );
657 emit dataChanged( index, index );
687 connect( nodeLayer, SIGNAL( layerLoaded() ),
this, SLOT(
nodeLayerLoaded() ) );
717 emit dataChanged( index, index );
731 if ( index.isValid() )
732 emit dataChanged( index, index );
738 if ( !nodeLayer->
layer() )
742 connect( nodeLayer, SIGNAL( layerLoaded() ),
this, SLOT(
nodeLayerLoaded() ) );
762 connect( layer, SIGNAL( legendChanged() ),
this, SLOT(
layerLegendChanged() ), Qt::UniqueConnection );
769 connect( layer, SIGNAL( editingStarted() ),
this, SLOT(
layerNeedsUpdate() ), Qt::UniqueConnection );
770 connect( layer, SIGNAL( editingStopped() ),
this, SLOT(
layerNeedsUpdate() ), Qt::UniqueConnection );
771 connect( layer, SIGNAL( layerModified() ),
this, SLOT(
layerNeedsUpdate() ), Qt::UniqueConnection );
772 connect( layer, SIGNAL( layerNameChanged() ),
this, SLOT(
layerNeedsUpdate() ), Qt::UniqueConnection );
797 disconnect( nodeLayer, 0,
this, 0 );
799 if ( !nodeLayer->
layer() )
810 disconnect( nodeLayer->
layer(), 0,
this, 0 );
864 int count = node->
children().count();
867 emit dataChanged(
index( 0, 0, idx ),
index( count - 1, 0, idx ) );
868 for (
int i = 0; i < count; ++i )
875 return Qt::MoveAction;
881 types <<
"application/qgis.layertreemodeldata";
888 QList<QgsLayerTreeNode*> nodesFinal =
indexes2nodes( indexes,
true );
890 if ( nodesFinal.count() == 0 )
893 QMimeData *
mimeData =
new QMimeData();
896 QDomElement rootElem = doc.createElement(
"layer_tree_model_data" );
899 doc.appendChild( rootElem );
900 QString txt = doc.toString();
902 mimeData->setData(
"application/qgis.layertreemodeldata", txt.toUtf8() );
908 if ( action == Qt::IgnoreAction )
911 if ( !data->hasFormat(
"application/qgis.layertreemodeldata" ) )
921 QByteArray encodedData = data->data(
"application/qgis.layertreemodeldata" );
924 if ( !doc.setContent( QString::fromUtf8( encodedData ) ) )
927 QDomElement rootElem = doc.documentElement();
928 if ( rootElem.tagName() !=
"layer_tree_model_data" )
931 QList<QgsLayerTreeNode*> nodes;
933 QDomElement elem = rootElem.firstChildElement();
934 while ( !elem.isNull() )
940 elem = elem.nextSiblingElement();
943 if ( nodes.count() == 0 )
946 if ( parent.isValid() && row == -1 )
985 return mFlags.testFlag( f );
1000 QList<QgsLayerTreeModelLegendNode*> filtered;
1053 if (
mLegend.contains( nodeLayer ) )
1055 qDeleteAll(
mLegend[nodeLayer].originalNodes );
1056 delete mLegend[nodeLayer].tree;
1064 if ( !nodeL->
layer() )
1076 QList<QgsLayerTreeModelLegendNode*> filteredLstNew =
filterLegendNodes( lstNew );
1078 bool isEmbedded = filteredLstNew.count() == 1 && filteredLstNew[0]->isEmbeddedInParent();
1082 n->setParent(
this );
1095 int count = data.
tree ? data.
tree->
children[0].count() : filteredLstNew.count();
1097 if ( ! isEmbedded ) beginInsertRows(
node2index( nodeL ), 0, count - 1 );
1101 if ( ! isEmbedded ) endInsertRows();
1108 bool hasParentKeys =
false;
1113 hasParentKeys =
true;
1117 if ( !hasParentKeys )
1121 QHash<QString, QgsLayerTreeModelLegendNode*> rule2node;
1122 rule2node[QString()] = 0;
1126 if ( ruleKey.isEmpty() )
1128 if ( rule2node.contains( ruleKey ) )
1130 rule2node[ruleKey] = n;
1159 int row = data.
tree->
children[parentLegendNode].indexOf( legendNode );
1160 return index( row, 0, parentIndex );
1165 int row = data.
tree->
children[0].indexOf( legendNode );
1166 return index( row, 0, parentIndex );
1171 Q_ASSERT( parentIndex.isValid() );
1174 return QModelIndex();
1175 return index( row, 0, parentIndex );
1194 if ( !
mLegend.contains( nL ) )
1207 Q_ASSERT(
mLegend.contains( nL ) );
1210 return createIndex( row, column, static_cast<QObject*>( data.tree->children[0].at( row ) ) );
1212 return createIndex( row, column, static_cast<QObject*>( data.activeNodes.at( row ) ) );
1220 return createIndex( row, column, static_cast<QObject*>( data.
tree->
children[node].at( row ) ) );
1222 return QModelIndex();
1235 int row = data.
tree->
children[grandParentNode].indexOf( parentNode );
1236 return createIndex( row, 0, static_cast<QObject*>( parentNode ) );
1250 return node->
data( role );
1256 Qt::ItemFlags f = node->
flags();
1258 f &= ~Qt::ItemIsUserCheckable;
1272 return QIcon( qvariant_cast<QPixmap>(
mLegend[nodeLayer].activeNodes[0]->
data( Qt::DecorationRole ) ) );
1278 return mLegend.value( nodeLayer ).activeNodes;