QGIS API Documentation  2.15.0-Master (af20121)
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 
27 
29 
30 QgsSymbolLevelsV2Dialog::QgsSymbolLevelsV2Dialog( const QgsLegendSymbolList& list, bool usingSymbolLevels, QWidget* parent )
31  : QDialog( parent )
32  , mList( list )
33  , mForceOrderingEnabled( false )
34 {
35  setupUi( this );
36 
37  QSettings settings;
38  restoreGeometry( settings.value( "/Windows/symbolLevelsDlg/geometry" ).toByteArray() );
39 
40  tableLevels->setItemDelegate( new SpinBoxDelegate( this ) );
41 
42  chkEnable->setChecked( usingSymbolLevels );
43 
44  connect( chkEnable, SIGNAL( clicked() ), this, SLOT( updateUi() ) );
45 
46  if ( mList.count() > 0 && !mList[0].second )
47  {
48  // remove symbolless entry (probably classifier of categorized renderer)
50  }
51 
52  int maxLayers = 0;
53  tableLevels->setRowCount( mList.count() );
54  for ( int i = 0; i < mList.count(); i++ )
55  {
56  QgsSymbolV2* sym = mList.at( i ).second;
57 
58  // set icons for the rows
60  tableLevels->setVerticalHeaderItem( i, new QTableWidgetItem( icon, QString() ) );
61 
62  // find out max. number of layers per symbol
63  int layers = sym->symbolLayerCount();
64  if ( layers > maxLayers )
65  maxLayers = layers;
66  }
67 
68  tableLevels->setColumnCount( maxLayers + 1 );
69  tableLevels->setHorizontalHeaderItem( 0, new QTableWidgetItem( QString() ) );
70  for ( int i = 0; i < maxLayers; i++ )
71  {
72  QString name = tr( "Layer %1" ).arg( i );
73  tableLevels->setHorizontalHeaderItem( i + 1, new QTableWidgetItem( name ) );
74  }
75 
76  mMaxLayers = maxLayers;
77 
78  updateUi();
79 
80  if ( !usingSymbolLevels )
82 
83  populateTable();
84 
85  connect( tableLevels, SIGNAL( cellChanged( int, int ) ), this, SLOT( renderingPassChanged( int, int ) ) );
86 }
87 
89 {
90  QSettings settings;
91  settings.setValue( "/Windows/symbolLevelsDlg/geometry", saveGeometry() );
92 }
93 
95 {
96  for ( int row = 0; row < mList.count(); row++ )
97  {
98  QgsSymbolV2* sym = mList.at( row ).second;
99  QString label = mList.at( row ).first;
100  QTableWidgetItem *itemLabel = new QTableWidgetItem( label );
101  itemLabel->setFlags( itemLabel->flags() ^ Qt::ItemIsEditable );
102  tableLevels->setItem( row, 0, itemLabel );
103  for ( int layer = 0; layer < mMaxLayers; layer++ )
104  {
105  QTableWidgetItem* item;
106  if ( layer >= sym->symbolLayerCount() )
107  {
108  item = new QTableWidgetItem();
109  item->setFlags( Qt::ItemFlags() );
110  }
111  else
112  {
113  QgsSymbolLayerV2* sl = sym->symbolLayer( layer );
115  item = new QTableWidgetItem( icon, QString::number( sl->renderingPass() ) );
116  }
117  tableLevels->setItem( row, layer + 1, item );
118  tableLevels->resizeColumnToContents( 0 );
119  }
120  }
121 
122 }
123 
125 {
126  tableLevels->setEnabled( chkEnable->isChecked() );
127 }
128 
130 {
131  for ( int i = 0; i < mList.count(); i++ )
132  {
133  QgsSymbolV2* sym = mList.at( i ).second;
134  for ( int layer = 0; layer < sym->symbolLayerCount(); layer++ )
135  {
136  sym->symbolLayer( layer )->setRenderingPass( layer );
137  }
138  }
139 }
140 
142 {
143  return chkEnable->isChecked();
144 }
145 
147 {
148  if ( row < 0 || row >= mList.count() )
149  return;
150  QgsSymbolV2* sym = mList.at( row ).second;
151  if ( column < 0 || column > sym->symbolLayerCount() )
152  return;
153  sym->symbolLayer( column - 1 )->setRenderingPass( tableLevels->item( row, column )->text().toInt() );
154 }
155 
157 {
159  if ( enabled )
160  {
161  chkEnable->setChecked( true );
162  chkEnable->hide();
163  }
164  else
165  chkEnable->show();
166 }
167 
168 
170 
171 QWidget* SpinBoxDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& ) const
172 {
173  QSpinBox *editor = new QSpinBox( parent );
174  editor->setMinimum( 0 );
175  editor->setMaximum( 999 );
176  return editor;
177 }
178 
179 void SpinBoxDelegate::setEditorData( QWidget* editor, const QModelIndex& index ) const
180 {
181  int value = index.model()->data( index, Qt::EditRole ).toInt();
182  QSpinBox *spinBox = static_cast<QSpinBox*>( editor );
183  spinBox->setValue( value );
184 }
185 
186 void SpinBoxDelegate::setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const
187 {
188  QSpinBox *spinBox = static_cast<QSpinBox*>( editor );
189  spinBox->interpretText();
190  int value = spinBox->value();
191 
192  model->setData( index, value, Qt::EditRole );
193 }
194 
195 void SpinBoxDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& ) const
196 {
197  editor->setGeometry( option.rect );
198 }
199 
200 
void renderingPassChanged(int row, int column)
QByteArray toByteArray() const
void setMinimum(int min)
static unsigned index
void setupUi(QWidget *widget)
int mMaxLayers
maximal number of layers from all symbols
static QIcon symbolLayerPreviewIcon(QgsSymbolLayerV2 *layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale &scale=QgsMapUnitScale())
void removeFirst()
const T & at(int i) const
const QPixmap * icon() const
QgsSymbolLevelsV2Dialog(const QgsLegendSymbolList &list, bool usingSymbolLevels, QWidget *parent=nullptr)
QString tr(const char *sourceText, const char *disambiguation, int n)
void setGeometry(int x, int y, int w, int h)
void setValue(const QString &key, const QVariant &value)
const char * name() const
The output shall be in millimeters.
Definition: qgssymbolv2.h:64
QString number(int n, int base)
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
int count(const T &value) const
int toInt(bool *ok) const
bool restoreGeometry(const QByteArray &geometry)
void setRenderingPass(int renderingPass)
int symbolLayerCount()
Returns total number of symbol layers contained in the symbol.
Definition: qgssymbolv2.h:131
virtual QVariant data(const QModelIndex &index, int role) const =0
bool mForceOrderingEnabled
whether symbol layers always should be used (default false)
int renderingPass() const
void setMaximum(int max)
void setForceOrderingEnabled(bool enabled)
QVariant value(const QString &key, const QVariant &defaultValue) const
void setFlags(QFlags< Qt::ItemFlag > flags)
const QAbstractItemModel * model() const
QByteArray saveGeometry() const
void setValue(int val)
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
QgsSymbolLayerV2 * symbolLayer(int layer)
Returns a specific symbol layers contained in the symbol.
Qt::ItemFlags flags() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
typedef ItemFlags