QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgssymbollevelsv2dialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbollevelsv2dialog.cpp
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk 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 
17 
18 #include "qgssymbollayerv2utils.h"
19 #include "qgssymbollayerv2.h"
20 #include "qgssymbolv2.h"
21 
22 #include <QTableWidgetItem>
23 #include <QItemDelegate>
24 #include <QSpinBox>
25 
26 // delegate used from Qt Spin Box example
27 class SpinBoxDelegate : public QItemDelegate
28 {
29  public:
30  SpinBoxDelegate( QObject *parent = 0 ) : QItemDelegate( parent ) {}
31 
32  QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &/*index*/ ) const override
33  {
34  QSpinBox *editor = new QSpinBox( parent );
35  editor->setMinimum( 0 );
36  editor->setMaximum( 999 );
37  return editor;
38  }
39 
40  void setEditorData( QWidget *editor, const QModelIndex &index ) const override
41  {
42  int value = index.model()->data( index, Qt::EditRole ).toInt();
43  QSpinBox *spinBox = static_cast<QSpinBox*>( editor );
44  spinBox->setValue( value );
45  }
46 
47  void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override
48  {
49  QSpinBox *spinBox = static_cast<QSpinBox*>( editor );
50  spinBox->interpretText();
51  int value = spinBox->value();
52 
53  model->setData( index, value, Qt::EditRole );
54  }
55 
56  void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/ ) const override
57  {
58  editor->setGeometry( option.rect );
59  }
60 
61 };
62 
64 
65 QgsSymbolLevelsV2Dialog::QgsSymbolLevelsV2Dialog( QgsLegendSymbolList list, bool usingSymbolLevels, QWidget* parent )
66  : QDialog( parent ), mList( list ), mForceOrderingEnabled( false )
67 {
68  setupUi( this );
69 
70  QSettings settings;
71  restoreGeometry( settings.value( "/Windows/symbolLevelsDlg/geometry" ).toByteArray() );
72 
73  tableLevels->setItemDelegate( new SpinBoxDelegate( this ) );
74 
75  chkEnable->setChecked( usingSymbolLevels );
76 
77  connect( chkEnable, SIGNAL( clicked() ), this, SLOT( updateUi() ) );
78 
79  if ( mList.count() > 0 && !mList[0].second )
80  {
81  // remove symbolless entry (probably classifier of categorized renderer)
82  mList.removeFirst();
83  }
84 
85  int maxLayers = 0;
86  tableLevels->setRowCount( mList.count() );
87  for ( int i = 0; i < mList.count(); i++ )
88  {
89  QgsSymbolV2* sym = mList[i].second;
90 
91  // set icons for the rows
92  QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( sym, QSize( 16, 16 ) );
93  tableLevels->setVerticalHeaderItem( i, new QTableWidgetItem( icon, QString() ) );
94 
95  // find out max. number of layers per symbol
96  int layers = sym->symbolLayerCount();
97  if ( layers > maxLayers )
98  maxLayers = layers;
99  }
100 
101  tableLevels->setColumnCount( maxLayers + 1 );
102  tableLevels->setHorizontalHeaderItem( 0, new QTableWidgetItem( QString() ) );
103  for ( int i = 0; i < maxLayers; i++ )
104  {
105  QString name = tr( "Layer %1" ).arg( i );
106  tableLevels->setHorizontalHeaderItem( i + 1, new QTableWidgetItem( name ) );
107  }
108 
109  mMaxLayers = maxLayers;
110 
111  updateUi();
112 
113  if ( !usingSymbolLevels )
115 
116  populateTable();
117 
118  connect( tableLevels, SIGNAL( cellChanged( int, int ) ), this, SLOT( renderingPassChanged( int, int ) ) );
119 }
120 
122 {
123  QSettings settings;
124  settings.setValue( "/Windows/symbolLevelsDlg/geometry", saveGeometry() );
125 }
126 
128 {
129  for ( int row = 0; row < mList.count(); row++ )
130  {
131  QgsSymbolV2* sym = mList[row].second;
132  QString label = mList[row].first;
133  QTableWidgetItem *itemLabel = new QTableWidgetItem( label );
134  itemLabel->setFlags( itemLabel->flags() ^ Qt::ItemIsEditable );
135  tableLevels->setItem( row, 0, itemLabel );
136  for ( int layer = 0; layer < mMaxLayers; layer++ )
137  {
138  QTableWidgetItem* item;
139  if ( layer >= sym->symbolLayerCount() )
140  {
141  item = new QTableWidgetItem();
142  item->setFlags( Qt::ItemFlags() );
143  }
144  else
145  {
146  QgsSymbolLayerV2* sl = sym->symbolLayer( layer );
147  QIcon icon = QgsSymbolLayerV2Utils::symbolLayerPreviewIcon( sl, QgsSymbolV2::MM, QSize( 16, 16 ) );
148  item = new QTableWidgetItem( icon, QString::number( sl->renderingPass() ) );
149  }
150  tableLevels->setItem( row, layer + 1, item );
151  tableLevels->resizeColumnToContents( 0 );
152  }
153  }
154 
155 }
156 
158 {
159  tableLevels->setEnabled( chkEnable->isChecked() );
160 }
161 
163 {
164  for ( int i = 0; i < mList.count(); i++ )
165  {
166  QgsSymbolV2* sym = mList[i].second;
167  for ( int layer = 0; layer < sym->symbolLayerCount(); layer++ )
168  {
169  sym->symbolLayer( layer )->setRenderingPass( layer );
170  }
171  }
172 }
173 
175 {
176  return chkEnable->isChecked();
177 }
178 
180 {
181  if ( row < 0 || row >= mList.count() )
182  return;
183  QgsSymbolV2* sym = mList[row].second;
184  if ( column < 0 || column > sym->symbolLayerCount() )
185  return;
186  sym->symbolLayer( column - 1 )->setRenderingPass( tableLevels->item( row, column )->text().toInt() );
187 }
188 
190 {
191  mForceOrderingEnabled = enabled;
192  if ( enabled )
193  {
194  chkEnable->setChecked( true );
195  chkEnable->hide();
196  }
197  else
198  chkEnable->show();
199 }