~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/gui/symbology-ng/qgssymbolv2propertiesdialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "qgssymbolv2propertiesdialog.h"
 
3
 
 
4
#include <QFile>
 
5
#include <QStandardItem>
 
6
#include <QKeyEvent>
 
7
 
 
8
#include "qgssymbollayerv2.h"
 
9
#include "qgssymbolv2.h"
 
10
#include "qgssymbollayerv2registry.h"
 
11
 
 
12
#include "qgsapplication.h"
 
13
 
 
14
#include "qgssymbollayerv2widget.h"
 
15
 
 
16
static const int SymbolLayerItemType = QStandardItem::UserType + 1;
 
17
 
 
18
class SymbolLayerItem : public QStandardItem
 
19
{
 
20
  public:
 
21
    SymbolLayerItem( QgsSymbolLayerV2* layer )
 
22
    {
 
23
      setLayer( layer );
 
24
    }
 
25
 
 
26
    void setLayer( QgsSymbolLayerV2* layer )
 
27
    {
 
28
      mLayer = layer;
 
29
      updatePreview();
 
30
    }
 
31
 
 
32
    void updatePreview()
 
33
    {
 
34
      QIcon icon = QgsSymbolLayerV2Utils::symbolLayerPreviewIcon( mLayer, QSize( 16, 16 ) );
 
35
      setIcon( icon );
 
36
    }
 
37
 
 
38
    int type() const { return SymbolLayerItemType; }
 
39
 
 
40
    QVariant data( int role ) const
 
41
    {
 
42
      if ( role == Qt::DisplayRole )
 
43
        return QVariant( mLayer->layerType() );
 
44
      if ( role == Qt::SizeHintRole )
 
45
        return QVariant( QSize( 32, 32 ) );
 
46
      if ( role == Qt::CheckStateRole )
 
47
        return QVariant(); // could be true/false
 
48
      return QStandardItem::data( role );
 
49
    }
 
50
 
 
51
  protected:
 
52
    QgsSymbolLayerV2* mLayer;
 
53
};
 
54
 
 
55
//////////
 
56
 
 
57
static QString iconPath( QString iconFile )
 
58
{
 
59
  // try active theme
 
60
  QString path = QgsApplication::activeThemePath();
 
61
  if ( QFile::exists( path + iconFile ) )
 
62
    return path + iconFile;
 
63
 
 
64
  // use default theme
 
65
  return QgsApplication::defaultThemePath() + iconFile;
 
66
}
 
67
 
 
68
//////////
 
69
 
 
70
QgsSymbolV2PropertiesDialog::QgsSymbolV2PropertiesDialog( QgsSymbolV2* symbol, QWidget* parent )
 
71
    : QDialog( parent ), mSymbol( symbol )
 
72
{
 
73
  setupUi( this );
 
74
 
 
75
  // setup icons
 
76
  btnAddLayer->setIcon( QIcon( iconPath( "symbologyAdd.png" ) ) );
 
77
  btnRemoveLayer->setIcon( QIcon( iconPath( "symbologyRemove.png" ) ) );
 
78
  btnLock->setIcon( QIcon( iconPath( "symbologyLock.png" ) ) );
 
79
  btnUp->setIcon( QIcon( iconPath( "symbologyUp.png" ) ) );
 
80
  btnDown->setIcon( QIcon( iconPath( "symbologyDown.png" ) ) );
 
81
 
 
82
  // set widget functions
 
83
  // (should be probably moved somewhere else)
 
84
  QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "SimpleLine", QgsSimpleLineSymbolLayerV2Widget::create );
 
85
  QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "MarkerLine", QgsMarkerLineSymbolLayerV2Widget::create );
 
86
  QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "LineDecoration", QgsLineDecorationSymbolLayerV2Widget::create );
 
87
 
 
88
  QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "SimpleMarker", QgsSimpleMarkerSymbolLayerV2Widget::create );
 
89
  QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "SvgMarker", QgsSvgMarkerSymbolLayerV2Widget::create );
 
90
 
 
91
  QgsSymbolLayerV2Registry::instance()->setLayerTypeWidgetFunction( "SimpleFill", QgsSimpleFillSymbolLayerV2Widget::create );
 
92
 
 
93
  loadSymbol();
 
94
 
 
95
  connect( btnUp, SIGNAL( clicked() ), this, SLOT( moveLayerUp() ) );
 
96
  connect( btnDown, SIGNAL( clicked() ), this, SLOT( moveLayerDown() ) );
 
97
  connect( btnAddLayer, SIGNAL( clicked() ), this, SLOT( addLayer() ) );
 
98
  connect( btnRemoveLayer, SIGNAL( clicked() ), this, SLOT( removeLayer() ) );
 
99
  connect( btnLock, SIGNAL( clicked() ), this, SLOT( lockLayer() ) );
 
100
 
 
101
  populateLayerTypes();
 
102
  connect( cboLayerType, SIGNAL( currentIndexChanged( int ) ), this, SLOT( layerTypeChanged() ) );
 
103
 
 
104
  loadPropertyWidgets();
 
105
 
 
106
  updateUi();
 
107
 
 
108
  // set first layer as active
 
109
  QModelIndex newIndex = listLayers->model()->index( 0, 0 );
 
110
  listLayers->setCurrentIndex( newIndex );
 
111
}
 
112
 
 
113
 
 
114
void QgsSymbolV2PropertiesDialog::loadSymbol()
 
115
{
 
116
  QStandardItemModel* model = new QStandardItemModel( this );
 
117
  listLayers->setModel( model );
 
118
 
 
119
  QItemSelectionModel* selModel = listLayers->selectionModel();
 
120
  connect( selModel, SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ), this, SLOT( layerChanged() ) );
 
121
 
 
122
  int count = mSymbol->symbolLayerCount();
 
123
  for ( int i = count - 1; i >= 0; i-- )
 
124
  {
 
125
    model->appendRow( new SymbolLayerItem( mSymbol->symbolLayer( i ) ) );
 
126
  }
 
127
 
 
128
  updatePreview();
 
129
}
 
130
 
 
131
 
 
132
void QgsSymbolV2PropertiesDialog::populateLayerTypes()
 
133
{
 
134
  QStringList types = QgsSymbolLayerV2Registry::instance()->symbolLayersForType( mSymbol->type() );
 
135
 
 
136
  cboLayerType->clear();
 
137
  for ( int i = 0; i < types.count(); i++ )
 
138
    cboLayerType->addItem( types[i] );
 
139
}
 
140
 
 
141
 
 
142
void QgsSymbolV2PropertiesDialog::updateUi()
 
143
{
 
144
  int row = currentRowIndex();
 
145
  btnUp->setEnabled( row > 0 );
 
146
  btnDown->setEnabled( row < listLayers->model()->rowCount() - 1 && row != -1 );
 
147
  btnRemoveLayer->setEnabled( row != -1 );
 
148
}
 
149
 
 
150
void QgsSymbolV2PropertiesDialog::updatePreview()
 
151
{
 
152
  QImage preview = mSymbol->bigSymbolPreviewImage();
 
153
  lblPreview->setPixmap( QPixmap::fromImage( preview ) );
 
154
}
 
155
 
 
156
void QgsSymbolV2PropertiesDialog::updateLayerPreview()
 
157
{
 
158
  // get current layer item and update its icon
 
159
  SymbolLayerItem* item = currentLayerItem();
 
160
  if ( item )
 
161
    item->updatePreview();
 
162
 
 
163
  // update also preview of the whole symbol
 
164
  updatePreview();
 
165
}
 
166
 
 
167
void QgsSymbolV2PropertiesDialog::updateSymbolLayerWidget( QgsSymbolLayerV2* layer )
 
168
{
 
169
  QString layerType = layer->layerType();
 
170
 
 
171
  // stop updating from the original widget
 
172
  if ( stackedWidget->currentWidget() != pageDummy )
 
173
    disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( updateLayerPreview() ) );
 
174
 
 
175
  // update active properties widget
 
176
  if ( mWidgets.contains( layerType ) )
 
177
  {
 
178
    stackedWidget->setCurrentWidget( mWidgets[layerType] );
 
179
    mWidgets[layerType]->setSymbolLayer( layer );
 
180
 
 
181
    // start recieving updates from widget
 
182
    connect( mWidgets[layerType], SIGNAL( changed() ), this, SLOT( updateLayerPreview() ) );
 
183
  }
 
184
  else
 
185
  {
 
186
    // use dummy widget instead
 
187
    stackedWidget->setCurrentWidget( pageDummy );
 
188
  }
 
189
}
 
190
 
 
191
void QgsSymbolV2PropertiesDialog::loadPropertyWidgets()
 
192
{
 
193
  QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance();
 
194
 
 
195
  QStringList layerTypes = pReg->symbolLayersForType( mSymbol->type() );
 
196
 
 
197
  for ( int i = 0; i < layerTypes.count(); i++ )
 
198
  {
 
199
    QString layerType = layerTypes[i];
 
200
    QgsSymbolLayerV2WidgetFunc f = pReg->symbolLayerMetadata( layerType ).widgetFunction();
 
201
    if ( f == NULL ) // check whether the function is assigned
 
202
      continue;
 
203
 
 
204
    QgsSymbolLayerV2Widget* w = f();
 
205
    if ( w == NULL ) // check whether the function returns correct widget
 
206
      continue;
 
207
 
 
208
    mWidgets[layerType] = w;
 
209
    stackedWidget->addWidget( w );
 
210
  }
 
211
}
 
212
 
 
213
int QgsSymbolV2PropertiesDialog::currentRowIndex()
 
214
{
 
215
  QModelIndex idx = listLayers->selectionModel()->currentIndex();
 
216
  if ( !idx.isValid() )
 
217
    return -1;
 
218
  return idx.row();
 
219
}
 
220
 
 
221
int QgsSymbolV2PropertiesDialog::currentLayerIndex()
 
222
{
 
223
  return listLayers->model()->rowCount() - currentRowIndex() - 1;
 
224
}
 
225
 
 
226
SymbolLayerItem* QgsSymbolV2PropertiesDialog::currentLayerItem()
 
227
{
 
228
  int index = currentRowIndex();
 
229
  if ( index < 0 )
 
230
    return NULL;
 
231
 
 
232
  QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listLayers->model() );
 
233
  if ( model == NULL )
 
234
    return NULL;
 
235
  QStandardItem* item = model->item( index );
 
236
  if ( item->type() != SymbolLayerItemType )
 
237
    return NULL;
 
238
  return static_cast<SymbolLayerItem*>( item );
 
239
}
 
240
 
 
241
QgsSymbolLayerV2* QgsSymbolV2PropertiesDialog::currentLayer()
 
242
{
 
243
  int idx = currentLayerIndex();
 
244
  if ( idx < 0 )
 
245
    return NULL;
 
246
 
 
247
  return mSymbol->symbolLayer( idx );
 
248
}
 
249
 
 
250
 
 
251
void QgsSymbolV2PropertiesDialog::layerChanged()
 
252
{
 
253
  updateUi();
 
254
 
 
255
  // get layer info
 
256
  QgsSymbolLayerV2* layer = currentLayer();
 
257
  if ( layer == NULL )
 
258
    return;
 
259
 
 
260
  // update layer type combo box
 
261
  int idx = cboLayerType->findText( layer->layerType() );
 
262
  cboLayerType->setCurrentIndex( idx );
 
263
 
 
264
  updateSymbolLayerWidget( layer );
 
265
 
 
266
  updateLockButton();
 
267
}
 
268
 
 
269
 
 
270
void QgsSymbolV2PropertiesDialog::updateLockButton()
 
271
{
 
272
  QgsSymbolLayerV2* layer = currentLayer();
 
273
  if ( layer == NULL ) return;
 
274
 
 
275
  btnLock->setChecked( layer->isLocked() );
 
276
}
 
277
 
 
278
 
 
279
void QgsSymbolV2PropertiesDialog::layerTypeChanged()
 
280
{
 
281
  QgsSymbolLayerV2* layer = currentLayer();
 
282
  if ( layer == NULL ) return;
 
283
 
 
284
  QString newLayerType = cboLayerType->currentText();
 
285
  if ( layer->layerType() == newLayerType )
 
286
    return;
 
287
 
 
288
  // get creation function for new layer from registry
 
289
  QgsSymbolLayerV2Registry* pReg = QgsSymbolLayerV2Registry::instance();
 
290
  QgsSymbolLayerV2CreateFunc f = pReg->symbolLayerMetadata( newLayerType ).createFunction();
 
291
  if ( f == NULL ) // check whether the function is assigned
 
292
    return;
 
293
 
 
294
  // change layer to a new (with different type)
 
295
  QgsSymbolLayerV2* newLayer = f( QgsStringMap() );
 
296
  mSymbol->changeSymbolLayer( currentLayerIndex(), newLayer );
 
297
 
 
298
  updateSymbolLayerWidget( newLayer );
 
299
 
 
300
  // update symbol layer item
 
301
  SymbolLayerItem* item = currentLayerItem();
 
302
  item->setLayer( newLayer );
 
303
  item->updatePreview();
 
304
 
 
305
  updatePreview();
 
306
}
 
307
 
 
308
 
 
309
void QgsSymbolV2PropertiesDialog::addLayer()
 
310
{
 
311
  QgsSymbolLayerV2* newLayer = QgsSymbolLayerV2Registry::instance()->defaultSymbolLayer( mSymbol->type() );
 
312
 
 
313
  mSymbol->appendSymbolLayer( newLayer );
 
314
 
 
315
  loadSymbol();
 
316
 
 
317
  QModelIndex newIndex = listLayers->model()->index( 0, 0 );
 
318
  listLayers->setCurrentIndex( newIndex );
 
319
 
 
320
  updateUi();
 
321
}
 
322
 
 
323
 
 
324
void QgsSymbolV2PropertiesDialog::removeLayer()
 
325
{
 
326
  int idx = currentLayerIndex();
 
327
  if ( idx < 0 ) return;
 
328
  mSymbol->deleteSymbolLayer( idx );
 
329
 
 
330
  loadSymbol();
 
331
 
 
332
  updateUi();
 
333
}
 
334
 
 
335
 
 
336
void QgsSymbolV2PropertiesDialog::moveLayerDown()
 
337
{
 
338
  moveLayerByOffset( +1 );
 
339
}
 
340
 
 
341
void QgsSymbolV2PropertiesDialog::moveLayerUp()
 
342
{
 
343
  moveLayerByOffset( -1 );
 
344
}
 
345
 
 
346
void QgsSymbolV2PropertiesDialog::moveLayerByOffset( int offset )
 
347
{
 
348
  int rowIdx = currentRowIndex();
 
349
  int layerIdx = currentLayerIndex();
 
350
 
 
351
  // switch layers
 
352
  QgsSymbolLayerV2* tmpLayer = mSymbol->takeSymbolLayer( layerIdx );
 
353
  mSymbol->insertSymbolLayer( layerIdx - offset, tmpLayer );
 
354
 
 
355
  loadSymbol();
 
356
 
 
357
  QModelIndex newIndex = listLayers->model()->index( rowIdx + offset, 0 );
 
358
  listLayers->setCurrentIndex( newIndex );
 
359
 
 
360
  updateUi();
 
361
}
 
362
 
 
363
 
 
364
void QgsSymbolV2PropertiesDialog::lockLayer()
 
365
{
 
366
  QgsSymbolLayerV2* layer = currentLayer();
 
367
  if ( layer == NULL ) return;
 
368
 
 
369
  layer->setLocked( btnLock->isChecked() );
 
370
}
 
371
 
 
372
#include "qgslogger.h"
 
373
 
 
374
void QgsSymbolV2PropertiesDialog::keyPressEvent( QKeyEvent * e )
 
375
{
 
376
  // Ignore the ESC key to avoid close the dialog without the properties window
 
377
  if ( !isWindow() && e->key() == Qt::Key_Escape )
 
378
  {
 
379
    e->ignore();
 
380
  }
 
381
  else
 
382
  {
 
383
    QDialog::keyPressEvent( e );
 
384
  }
 
385
}