|
| 1 | +/*************************************************************************** |
| 2 | + qgslayerorder.cpp - description |
| 3 | + ------------------- |
| 4 | + begin : 2011-11-09 |
| 5 | + copyright : (C) 2011 by Juergen E. Fischer, norBIT GmbH |
| 6 | + email : jef at norbit dot de |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgslayerorder.h" |
| 19 | +#include "qgsmapcanvas.h" |
| 20 | +#include "qgsmaplayer.h" |
| 21 | +#include "qgslogger.h" |
| 22 | +#include "qgslegend.h" |
| 23 | +#include "qgslegendlayer.h" |
| 24 | +#include "qgsproject.h" |
| 25 | + |
| 26 | +#include <QMouseEvent> |
| 27 | +#include <QTreeWidgetItem> |
| 28 | + |
| 29 | +QgsLayerOrder::QgsLayerOrder( QgsLegend *legend, QWidget * parent, const char *name ) |
| 30 | + : QListWidget( parent ) |
| 31 | + , mLegend( legend ) |
| 32 | + , mPressItem( 0 ) |
| 33 | +{ |
| 34 | + setObjectName( name ); |
| 35 | + |
| 36 | + // track visibility changed in legend |
| 37 | + connect( mLegend, SIGNAL( itemChanged( QTreeWidgetItem *, int ) ), |
| 38 | + this, SLOT( legendItemChanged( QTreeWidgetItem *, int ) ) ); |
| 39 | + |
| 40 | + // track if legend mode changes |
| 41 | + connect( mLegend, SIGNAL( updateDrawingOrderChanged( bool ) ), |
| 42 | + this, SLOT( updateDrawingOrderChanged( bool ) ) ); |
| 43 | + |
| 44 | + connect( mLegend, SIGNAL( zOrderChanged() ), |
| 45 | + this, SLOT( refreshLayerList() ) ); |
| 46 | + |
| 47 | + connect( mLegend->canvas(), SIGNAL( layersChanged() ), |
| 48 | + this, SLOT( refreshLayerList() ) ); |
| 49 | + |
| 50 | + // change visibility |
| 51 | + connect( this, SIGNAL( itemChanged( QListWidgetItem * ) ), |
| 52 | + this, SLOT( itemChanged( QListWidgetItem * ) ) ); |
| 53 | + |
| 54 | + // Initialise the line indicator widget. |
| 55 | + mInsertionLine = new QWidget( viewport() ); |
| 56 | + hideLine(); |
| 57 | + mInsertionLine->setAutoFillBackground( true ); |
| 58 | + QPalette pal = mInsertionLine->palette(); |
| 59 | + pal.setColor( mInsertionLine->backgroundRole(), Qt::blue ); |
| 60 | + mInsertionLine->setPalette( pal ); |
| 61 | + mInsertRow = -1; |
| 62 | + |
| 63 | + setSortingEnabled( false ); |
| 64 | + setSelectionMode( QAbstractItemView::ExtendedSelection ); |
| 65 | + setDragEnabled( false ); |
| 66 | + setAutoScroll( true ); |
| 67 | + QFont f( "Arial", 12, QFont::Bold ); |
| 68 | + setFont( f ); |
| 69 | + QPalette palette; |
| 70 | + palette.setColor( backgroundRole(), QColor( 192, 192, 192 ) ); |
| 71 | + setPalette( palette ); |
| 72 | + |
| 73 | + updateDrawingOrderChanged( mLegend->updateDrawingOrder() ); |
| 74 | +} |
| 75 | + |
| 76 | +QgsLayerOrder::~QgsLayerOrder() |
| 77 | +{ |
| 78 | + delete mInsertionLine; |
| 79 | +} |
| 80 | + |
| 81 | +void QgsLayerOrder::refreshLayerList() |
| 82 | +{ |
| 83 | + clear(); |
| 84 | + |
| 85 | + foreach( QgsMapLayer *layer, mLegend->layers() ) |
| 86 | + { |
| 87 | + QListWidgetItem *item = new QListWidgetItem( layer->name() ); |
| 88 | + item->setData( Qt::UserRole, qVariantFromValue( qobject_cast<QObject*>( layer ) ) ); |
| 89 | + item->setCheckState( mLegend->layerCheckState( layer ) ); |
| 90 | + QgsDebugMsg( QString( "add item=%1 at %2" ).arg( item ? item->text() : "(null item)" ).arg( count() ) ); |
| 91 | + addItem( item ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +QListWidgetItem *QgsLayerOrder::layerItem( QgsMapLayer *layer ) const |
| 96 | +{ |
| 97 | + for ( int i = 0; i < count(); i++ ) |
| 98 | + { |
| 99 | + QListWidgetItem *lwi = item( i ); |
| 100 | + |
| 101 | + if ( layer == qobject_cast<QgsMapLayer *>( lwi->data( Qt::UserRole ).value<QObject*>() ) ) |
| 102 | + { |
| 103 | + return lwi; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +void QgsLayerOrder::itemChanged( QListWidgetItem *item ) |
| 111 | +{ |
| 112 | + QgsDebugMsg( "Entering." ); |
| 113 | + QgsDebugMsg( QString( "item=%1" ).arg( item ? item->text() : "(null item)" ) ); |
| 114 | + |
| 115 | + QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( item->data( Qt::UserRole ).value<QObject*>() ); |
| 116 | + mLegend->setLayerVisible( layer, item->checkState() == Qt::Checked ); |
| 117 | + |
| 118 | + updateLayerOrder(); |
| 119 | +} |
| 120 | + |
| 121 | +void QgsLayerOrder::legendItemChanged( QTreeWidgetItem *item, int col ) |
| 122 | +{ |
| 123 | + QgsDebugMsg( "Entering." ); |
| 124 | + |
| 125 | + if ( col != 0 ) |
| 126 | + return; |
| 127 | + |
| 128 | + QgsDebugMsg( QString( "legendItem changed=%1" ).arg( item ? item->text( 0 ) : "(null item)" ) ); |
| 129 | + |
| 130 | + QgsLegendLayer *ll = dynamic_cast< QgsLegendLayer * >( item ); |
| 131 | + if ( !ll ) |
| 132 | + return; |
| 133 | + |
| 134 | + QListWidgetItem *lwi = layerItem( ll->layer() ); |
| 135 | + if ( !lwi ) |
| 136 | + return; |
| 137 | + |
| 138 | + lwi->setCheckState( item->checkState( col ) ); |
| 139 | +} |
| 140 | + |
| 141 | +void QgsLayerOrder::mousePressEvent( QMouseEvent * e ) |
| 142 | +{ |
| 143 | + QgsDebugMsg( "Entering." ); |
| 144 | + |
| 145 | + if ( e->button() == Qt::LeftButton ) |
| 146 | + { |
| 147 | + mPressItem = itemAt( e->pos() ); |
| 148 | + } |
| 149 | + |
| 150 | + QListWidget::mousePressEvent( e ); |
| 151 | +} |
| 152 | + |
| 153 | +void QgsLayerOrder::mouseMoveEvent( QMouseEvent * e ) |
| 154 | +{ |
| 155 | + if ( !mPressItem || count() == 0 ) |
| 156 | + return; |
| 157 | + |
| 158 | + // start moving when cursor leaves the current item |
| 159 | + if ( itemAt( e->pos() ) == mPressItem ) |
| 160 | + return; |
| 161 | + |
| 162 | + hideLine(); |
| 163 | + |
| 164 | + if ( mItemsBeingMoved.isEmpty() && !selectedItems().isEmpty() ) |
| 165 | + { |
| 166 | + for ( int i = 0; i < count(); i++ ) |
| 167 | + { |
| 168 | + QListWidgetItem *lwi = item( i ); |
| 169 | + if ( lwi->isSelected() ) |
| 170 | + { |
| 171 | + QgsDebugMsg( QString( "Take item %1:%2" ).arg( i ).arg( lwi->text() ) ); |
| 172 | + mItemsBeingMoved << takeItem( i-- ); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + setCursor( Qt::SizeVerCursor ); |
| 177 | + } |
| 178 | + |
| 179 | + if ( mItemsBeingMoved.isEmpty() ) |
| 180 | + { |
| 181 | + setCursor( Qt::ArrowCursor ); |
| 182 | + mInsertRow = -1; |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + QListWidgetItem *rowItem = itemAt( e->pos() ); |
| 187 | + |
| 188 | + if ( rowItem ) |
| 189 | + { |
| 190 | + mInsertRow = row( rowItem ); |
| 191 | + QgsDebugMsg( QString( "Move to row %1" ).arg( mInsertRow ) ); |
| 192 | + } |
| 193 | + else if ( e->pos().y() < visualItemRect( item( 0 ) ).bottom() ) |
| 194 | + { |
| 195 | + mInsertRow = 0; |
| 196 | + QgsDebugMsg( QString( "Insert top row %1" ).arg( mInsertRow ) ); |
| 197 | + } |
| 198 | + else |
| 199 | + { |
| 200 | + mInsertRow = count(); |
| 201 | + QgsDebugMsg( QString( "Append bottom row %1" ).arg( mInsertRow ) ); |
| 202 | + } |
| 203 | + |
| 204 | + int y; |
| 205 | + if ( mInsertRow < count() ) |
| 206 | + { |
| 207 | + y = visualItemRect( rowItem ).top(); |
| 208 | + } |
| 209 | + else |
| 210 | + { |
| 211 | + y = visualItemRect( item( count() - 1 ) ).bottom(); |
| 212 | + } |
| 213 | + |
| 214 | + mInsertionLine->setGeometry( visualItemRect( item( 0 ) ).left(), y, viewport()->width(), 2 ); |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +void QgsLayerOrder::mouseReleaseEvent( QMouseEvent * e ) |
| 219 | +{ |
| 220 | + QgsDebugMsg( "Entering." ); |
| 221 | + QListWidget::mouseReleaseEvent( e ); |
| 222 | + mPressItem = 0; |
| 223 | + hideLine(); |
| 224 | + |
| 225 | + if ( mItemsBeingMoved.isEmpty() ) |
| 226 | + return; |
| 227 | + |
| 228 | + setCursor( QCursor( Qt::ArrowCursor ) ); |
| 229 | + |
| 230 | + foreach( QListWidgetItem *item, mItemsBeingMoved ) |
| 231 | + { |
| 232 | + if ( mInsertRow >= count() ) |
| 233 | + { |
| 234 | + QgsDebugMsg( QString( "Adding item at %1:%2" ).arg( mInsertRow ).arg( item->text() ) ); |
| 235 | + addItem( item ); |
| 236 | + } |
| 237 | + else |
| 238 | + { |
| 239 | + QgsDebugMsg( QString( "Inserting item at %1:%2" ).arg( mInsertRow ).arg( item->text() ) ); |
| 240 | + insertItem( mInsertRow, item ); |
| 241 | + } |
| 242 | + |
| 243 | + mInsertRow++; |
| 244 | + } |
| 245 | + |
| 246 | + mItemsBeingMoved.clear(); |
| 247 | + mInsertRow = -1; |
| 248 | + |
| 249 | + updateLayerOrder(); |
| 250 | +} |
| 251 | + |
| 252 | +void QgsLayerOrder::updateLayerOrder() |
| 253 | +{ |
| 254 | + if ( !isEnabled() ) |
| 255 | + return; |
| 256 | + |
| 257 | + QList<QgsMapLayer *> layers; |
| 258 | + |
| 259 | + for ( int i = 0; i < count(); i++ ) |
| 260 | + { |
| 261 | + layers << qobject_cast<QgsMapLayer *>( item( i )->data( Qt::UserRole ).value<QObject*>() ); |
| 262 | + } |
| 263 | + |
| 264 | + mLegend->setDrawingOrder( layers ); |
| 265 | +} |
| 266 | + |
| 267 | +void QgsLayerOrder::hideLine() |
| 268 | +{ |
| 269 | + mInsertionLine->setGeometry( 0, -100, 1, 1 ); |
| 270 | +} |
| 271 | + |
| 272 | +void QgsLayerOrder::updateDrawingOrderChanged( bool enabled ) |
| 273 | +{ |
| 274 | + setDisabled( enabled ); |
| 275 | +} |
0 commit comments