QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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
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
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
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
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  tableLevels->setItemDelegate( new SpinBoxDelegate( this ) );
71 
72  chkEnable->setChecked( usingSymbolLevels );
73 
74  connect( chkEnable, SIGNAL( clicked() ), this, SLOT( updateUi() ) );
75 
76  if ( mList.count() > 0 && !mList[0].second )
77  {
78  // remove symbolless entry (probably classifier of categorized renderer)
79  mList.removeFirst();
80  }
81 
82  int maxLayers = 0;
83  tableLevels->setRowCount( mList.count() );
84  for ( int i = 0; i < mList.count(); i++ )
85  {
86  QgsSymbolV2* sym = mList[i].second;
87  QString label = mList[i].first;
88 
89  // set icons for the rows
90  QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( sym, QSize( 16, 16 ) );
91  tableLevels->setVerticalHeaderItem( i, new QTableWidgetItem( icon, label ) );
92 
93  // find out max. number of layers per symbol
94  int layers = sym->symbolLayerCount();
95  if ( layers > maxLayers )
96  maxLayers = layers;
97  }
98 
99  tableLevels->setColumnCount( maxLayers );
100  for ( int i = 0; i < maxLayers; i++ )
101  {
102  QString name = tr( "Layer %1" ).arg( i );
103  tableLevels->setHorizontalHeaderItem( i, new QTableWidgetItem( name ) );
104  }
105 
106  mMaxLayers = maxLayers;
107 
108  updateUi();
109 
110  if ( !usingSymbolLevels )
112 
113  populateTable();
114 
115  connect( tableLevels, SIGNAL( cellChanged( int, int ) ), this, SLOT( renderingPassChanged( int, int ) ) );
116 }
117 
119 {
120  for ( int row = 0; row < mList.count(); row++ )
121  {
122  QgsSymbolV2* sym = mList[row].second;
123  for ( int layer = 0; layer < mMaxLayers; layer++ )
124  {
125  QTableWidgetItem* item;
126  if ( layer >= sym->symbolLayerCount() )
127  {
128  item = new QTableWidgetItem();
129  item->setFlags( Qt::ItemFlags() );
130  }
131  else
132  {
133  QgsSymbolLayerV2* sl = sym->symbolLayer( layer );
135  item = new QTableWidgetItem( icon, QString::number( sl->renderingPass() ) );
136  }
137  tableLevels->setItem( row, layer, item );
138  }
139  }
140 
141 }
142 
144 {
145  tableLevels->setEnabled( chkEnable->isChecked() );
146 }
147 
149 {
150  for ( int i = 0; i < mList.count(); i++ )
151  {
152  QgsSymbolV2* sym = mList[i].second;
153  for ( int layer = 0; layer < sym->symbolLayerCount(); layer++ )
154  {
155  sym->symbolLayer( layer )->setRenderingPass( layer );
156  }
157  }
158 }
159 
160 
162 {
163  return chkEnable->isChecked();
164 }
165 
167 {
168  if ( row < 0 || row >= mList.count() )
169  return;
170  QgsSymbolV2* sym = mList[row].second;
171  if ( column < 0 || column >= sym->symbolLayerCount() )
172  return;
173  sym->symbolLayer( column )->setRenderingPass( tableLevels->item( row, column )->text().toInt() );
174 }
175 
177 {
178  mForceOrderingEnabled = enabled;
179  if ( enabled )
180  {
181  chkEnable->setChecked( true );
182  chkEnable->hide();
183  }
184  else
185  chkEnable->show();
186 }
void renderingPassChanged(int row, int column)
static unsigned index
int mMaxLayers
maximal number of layers from all symbols
static QIcon symbolLayerPreviewIcon(QgsSymbolLayerV2 *layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale &scale=QgsMapUnitScale())
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
static QIcon icon(QString icon)
SpinBoxDelegate(QObject *parent=0)
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
void setRenderingPass(int renderingPass)
int symbolLayerCount()
Definition: qgssymbolv2.h:85
void setEditorData(QWidget *editor, const QModelIndex &index) const
bool mForceOrderingEnabled
whether symbol layers always should be used (default false)
int renderingPass() const
void setForceOrderingEnabled(bool enabled)
QgsSymbolLayerV2 * symbolLayer(int layer)
QList< QPair< QString, QgsSymbolV2 * > > QgsLegendSymbolList
Definition: qgsrendererv2.h:41
QgsSymbolLevelsV2Dialog(QgsLegendSymbolList list, bool usingSymbolLevels, QWidget *parent=NULL)
#define tr(sourceText)