QGIS API Documentation  2.13.0-Master
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 ), mList( list ), mForceOrderingEnabled( false )
32 {
33  setupUi( this );
34 
35  QSettings settings;
36  restoreGeometry( settings.value( "/Windows/symbolLevelsDlg/geometry" ).toByteArray() );
37 
38  tableLevels->setItemDelegate( new SpinBoxDelegate( this ) );
39 
40  chkEnable->setChecked( usingSymbolLevels );
41 
42  connect( chkEnable, SIGNAL( clicked() ), this, SLOT( updateUi() ) );
43 
44  if ( mList.count() > 0 && !mList[0].second )
45  {
46  // remove symbolless entry (probably classifier of categorized renderer)
48  }
49 
50  int maxLayers = 0;
51  tableLevels->setRowCount( mList.count() );
52  for ( int i = 0; i < mList.count(); i++ )
53  {
54  QgsSymbolV2* sym = mList.at( i ).second;
55 
56  // set icons for the rows
58  tableLevels->setVerticalHeaderItem( i, new QTableWidgetItem( icon, QString() ) );
59 
60  // find out max. number of layers per symbol
61  int layers = sym->symbolLayerCount();
62  if ( layers > maxLayers )
63  maxLayers = layers;
64  }
65 
66  tableLevels->setColumnCount( maxLayers + 1 );
67  tableLevels->setHorizontalHeaderItem( 0, new QTableWidgetItem( QString() ) );
68  for ( int i = 0; i < maxLayers; i++ )
69  {
70  QString name = tr( "Layer %1" ).arg( i );
71  tableLevels->setHorizontalHeaderItem( i + 1, new QTableWidgetItem( name ) );
72  }
73 
74  mMaxLayers = maxLayers;
75 
76  updateUi();
77 
78  if ( !usingSymbolLevels )
80 
81  populateTable();
82 
83  connect( tableLevels, SIGNAL( cellChanged( int, int ) ), this, SLOT( renderingPassChanged( int, int ) ) );
84 }
85 
87 {
88  QSettings settings;
89  settings.setValue( "/Windows/symbolLevelsDlg/geometry", saveGeometry() );
90 }
91 
93 {
94  for ( int row = 0; row < mList.count(); row++ )
95  {
96  QgsSymbolV2* sym = mList.at( row ).second;
97  QString label = mList.at( row ).first;
98  QTableWidgetItem *itemLabel = new QTableWidgetItem( label );
99  itemLabel->setFlags( itemLabel->flags() ^ Qt::ItemIsEditable );
100  tableLevels->setItem( row, 0, itemLabel );
101  for ( int layer = 0; layer < mMaxLayers; layer++ )
102  {
103  QTableWidgetItem* item;
104  if ( layer >= sym->symbolLayerCount() )
105  {
106  item = new QTableWidgetItem();
107  item->setFlags( Qt::ItemFlags() );
108  }
109  else
110  {
111  QgsSymbolLayerV2* sl = sym->symbolLayer( layer );
113  item = new QTableWidgetItem( icon, QString::number( sl->renderingPass() ) );
114  }
115  tableLevels->setItem( row, layer + 1, item );
116  tableLevels->resizeColumnToContents( 0 );
117  }
118  }
119 
120 }
121 
123 {
124  tableLevels->setEnabled( chkEnable->isChecked() );
125 }
126 
128 {
129  for ( int i = 0; i < mList.count(); i++ )
130  {
131  QgsSymbolV2* sym = mList.at( i ).second;
132  for ( int layer = 0; layer < sym->symbolLayerCount(); layer++ )
133  {
134  sym->symbolLayer( layer )->setRenderingPass( layer );
135  }
136  }
137 }
138 
140 {
141  return chkEnable->isChecked();
142 }
143 
145 {
146  if ( row < 0 || row >= mList.count() )
147  return;
148  QgsSymbolV2* sym = mList.at( row ).second;
149  if ( column < 0 || column > sym->symbolLayerCount() )
150  return;
151  sym->symbolLayer( column - 1 )->setRenderingPass( tableLevels->item( row, column )->text().toInt() );
152 }
153 
155 {
157  if ( enabled )
158  {
159  chkEnable->setChecked( true );
160  chkEnable->hide();
161  }
162  else
163  chkEnable->show();
164 }
165 
166 
168 
169 QWidget* SpinBoxDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& ) const
170 {
171  QSpinBox *editor = new QSpinBox( parent );
172  editor->setMinimum( 0 );
173  editor->setMaximum( 999 );
174  return editor;
175 }
176 
177 void SpinBoxDelegate::setEditorData( QWidget* editor, const QModelIndex& index ) const
178 {
179  int value = index.model()->data( index, Qt::EditRole ).toInt();
180  QSpinBox *spinBox = static_cast<QSpinBox*>( editor );
181  spinBox->setValue( value );
182 }
183 
184 void SpinBoxDelegate::setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const
185 {
186  QSpinBox *spinBox = static_cast<QSpinBox*>( editor );
187  spinBox->interpretText();
188  int value = spinBox->value();
189 
190  model->setData( index, value, Qt::EditRole );
191 }
192 
193 void SpinBoxDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& ) const
194 {
195  editor->setGeometry( option.rect );
196 }
197 
198 
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:61
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:128
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