34 #include <QMessageBox> 35 #include <QStandardItemModel> 36 #include <QStandardItem> 45 , mRenderer( nullptr )
46 , mMimeFormat(
"application/x-qgsgraduatedsymbolrendererv2model" )
66 void QgsGraduatedSymbolRendererV2Model::addClass(
QgsSymbolV2* symbol )
68 if ( !mRenderer )
return;
101 return Qt::ItemIsDropEnabled;
104 Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
106 if ( index.
column() == 2 )
108 flags |= Qt::ItemIsEditable;
114 Qt::DropActions QgsGraduatedSymbolRendererV2Model::supportedDropActions()
const 116 return Qt::MoveAction;
119 QVariant QgsGraduatedSymbolRendererV2Model::data(
const QModelIndex &index,
int role )
const 125 if ( role == Qt::CheckStateRole && index.
column() == 0 )
127 return range.
renderState() ? Qt::Checked : Qt::Unchecked;
129 else if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
136 if ( decimalPlaces < 0 ) decimalPlaces = 0;
140 return range.
label();
145 else if ( role == Qt::DecorationRole && index.
column() == 0 && range.
symbol() )
149 else if ( role == Qt::TextAlignmentRole )
151 return ( index.
column() == 0 ) ? Qt::AlignHCenter : Qt::AlignLeft;
153 else if ( role == Qt::EditRole )
159 return range.
label();
168 bool QgsGraduatedSymbolRendererV2Model::setData(
const QModelIndex & index,
const QVariant & value,
int role )
173 if ( index.
column() == 0 && role == Qt::CheckStateRole )
176 emit dataChanged( index, index );
180 if ( role != Qt::EditRole )
194 emit dataChanged( index, index );
198 QVariant QgsGraduatedSymbolRendererV2Model::headerData(
int section, Qt::Orientation orientation,
int role )
const 200 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 3 )
203 lst <<
tr(
"Symbol" ) <<
tr(
"Values" ) <<
tr(
"Legend" );
204 return lst.
value( section );
209 int QgsGraduatedSymbolRendererV2Model::rowCount(
const QModelIndex &
parent )
const 211 if ( parent.
isValid() || !mRenderer )
218 int QgsGraduatedSymbolRendererV2Model::columnCount(
const QModelIndex & index )
const 226 if ( hasIndex( row, column, parent ) )
228 return createIndex( row, column );
239 QStringList QgsGraduatedSymbolRendererV2Model::mimeTypes()
const 242 types << mMimeFormat;
246 QMimeData *QgsGraduatedSymbolRendererV2Model::mimeData(
const QModelIndexList &indexes )
const 251 QDataStream stream( &encodedData, QIODevice::WriteOnly );
259 stream << index.
row();
261 mimeData->
setData( mMimeFormat, encodedData );
265 bool QgsGraduatedSymbolRendererV2Model::dropMimeData(
const QMimeData *data, Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
269 if ( action != Qt::MoveAction )
return true;
271 if ( !data->
hasFormat( mMimeFormat ) )
return false;
274 QDataStream stream( &encodedData, QIODevice::ReadOnly );
277 while ( !stream.atEnd() )
284 int to = parent.
row();
287 if ( to == -1 ) to = mRenderer->
ranges().
size();
288 for (
int i = rows.
size() - 1; i >= 0; i-- )
293 if ( rows[i] < t ) t--;
296 for (
int j = 0; j < i; j++ )
298 if ( to < rows[j] && rows[i] > rows[j] ) rows[j] += 1;
301 if ( rows[i] < to ) to--;
303 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->
ranges().
size(), 0 ) );
308 void QgsGraduatedSymbolRendererV2Model::deleteRows(
QList<int> rows )
310 for (
int i = rows.
size() - 1; i >= 0; i-- )
312 beginRemoveRows(
QModelIndex(), rows[i], rows[i] );
318 void QgsGraduatedSymbolRendererV2Model::removeAllRows()
325 void QgsGraduatedSymbolRendererV2Model::sort(
int column, Qt::SortOrder order )
336 else if ( column == 2 )
341 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->
ranges().
size(), 0 ) );
345 void QgsGraduatedSymbolRendererV2Model::updateSymbology(
bool resetModel )
353 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->
ranges().
size(), 0 ) );
357 void QgsGraduatedSymbolRendererV2Model::updateLabels()
359 emit dataChanged( createIndex( 0, 2 ), createIndex( mRenderer->
ranges().
size(), 2 ) );
363 QgsGraduatedSymbolRendererV2ViewStyle::QgsGraduatedSymbolRendererV2ViewStyle(
QStyle*
style )
367 void QgsGraduatedSymbolRendererV2ViewStyle::drawPrimitive( PrimitiveElement element,
const QStyleOption * option,
QPainter * painter,
const QWidget * widget )
const 369 if ( element == QStyle::PE_IndicatorItemViewItemDrop && !option->rect.isNull() )
372 opt.rect.setLeft( 0 );
374 opt.rect.setHeight( 0 );
375 if ( widget ) opt.rect.setRight( widget->
width() );
418 , mRenderer( nullptr )
436 mModel =
new QgsGraduatedSymbolRendererV2Model(
this );
439 mExpressionWidget->setLayer(
mLayer );
443 cboGraduatedColorRamp->populate(
mStyle );
450 if ( defaultColorRamp !=
"" )
452 int index = cboGraduatedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
454 cboGraduatedColorRamp->setCurrentIndex( index );
458 viewGraduated->setStyle(
new QgsGraduatedSymbolRendererV2ViewStyle( viewGraduated->style() ) );
462 methodComboBox->blockSignals(
true );
463 methodComboBox->addItem(
"Color" );
466 methodComboBox->addItem(
"Size" );
467 minSizeSpinBox->setValue( 1 );
468 maxSizeSpinBox->setValue( 8 );
472 methodComboBox->addItem(
"Size" );
473 minSizeSpinBox->setValue( .1 );
474 maxSizeSpinBox->setValue( 2 );
476 methodComboBox->blockSignals(
false );
487 connect( btnGraduatedAdd, SIGNAL( clicked() ),
this, SLOT(
addClass() ) );
502 btnAdvanced->setMenu( advMenu );
504 mHistogramWidget->setLayer(
mLayer );
505 mHistogramWidget->setRenderer(
mRenderer );
506 connect( mHistogramWidget, SIGNAL( rangesModified(
bool ) ),
this, SLOT(
refreshRanges(
bool ) ) );
507 connect( mExpressionWidget, SIGNAL( fieldChanged(
QString ) ), mHistogramWidget, SLOT( setSourceFieldExp(
QString ) ) );
542 connect( mButtonEditRamp, SIGNAL( clicked() ), cboGraduatedColorRamp, SLOT( editSourceRamp() ) );
562 disconnect( mButtonEditRamp, SIGNAL( clicked() ), cboGraduatedColorRamp, SLOT( editSourceRamp() ) );
586 if ( nclasses && updateCount )
591 mExpressionWidget->setField( attrName );
592 mHistogramWidget->setSourceFieldExp( attrName );
603 viewGraduated->setModel(
mModel );
607 mSizeUnitWidget->blockSignals(
true );
610 mSizeUnitWidget->blockSignals(
false );
614 methodComboBox->blockSignals(
true );
617 methodComboBox->setCurrentIndex( 0 );
624 methodComboBox->setCurrentIndex( 1 );
631 mMethodStackedWidget->setCurrentIndex( methodComboBox->currentIndex() );
632 methodComboBox->blockSignals(
false );
635 txtLegendFormat->setText( labelFormat.
format() );
636 spinPrecision->setValue( labelFormat.
precision() );
639 viewGraduated->resizeColumnToContents( 0 );
640 viewGraduated->resizeColumnToContents( 1 );
641 viewGraduated->resizeColumnToContents( 2 );
643 mHistogramWidget->refresh();
655 mMethodStackedWidget->setCurrentIndex( idx );
663 if ( cboGraduatedColorRamp->count() == 0 )
664 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
684 mModel->updateSymbology( reset );
689 QString attrName = mExpressionWidget->currentField();
691 int nclasses = spinGraduatedClasses->value();
696 if ( cboGraduatedColorRamp->count() == 0 )
697 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
704 if ( cboGraduatedMode->currentIndex() == 0 )
706 else if ( cboGraduatedMode->currentIndex() == 2 )
708 else if ( cboGraduatedMode->currentIndex() == 3 )
710 else if ( cboGraduatedMode->currentIndex() == 4 )
719 if ( QMessageBox::Cancel ==
QMessageBox::question(
this,
tr(
"Warning" ),
tr(
"Natural break classification (Jenks) is O(n2) complexity, your classification may take a long time.\nPress cancel to abort breaks calculation or OK to continue." ), QMessageBox::Cancel, QMessageBox::Ok ) )
728 if ( methodComboBox->currentIndex() == 0 )
734 if ( cboGraduatedColorRamp->count() == 0 )
735 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
750 if ( methodComboBox->currentIndex() == 1 )
783 if ( m && !i.isEmpty() )
803 mSizeUnitWidget->blockSignals(
true );
806 mSizeUnitWidget->blockSignals(
false );
819 btnChangeGraduatedSymbol->setIcon( icon );
823 int QgsRendererV2PropertiesDialog::currentRangeRow()
825 QModelIndex idx = viewGraduated->selectionModel()->currentIndex();
835 QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
850 QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
851 QModelIndexList::const_iterator sIt = selectedRows.
constBegin();
853 for ( ; sIt != selectedRows.constEnd(); ++sIt )
880 if ( m && !selectedIndexes.isEmpty() )
891 Q_FOREACH (
const QModelIndex& idx, selectedIndexes )
895 int rangeIdx = idx.
row();
918 mHistogramWidget->refresh();
929 if ( decimalPlaces < 0 ) decimalPlaces = 0;
933 if ( dialog.
exec() == QDialog::Accepted )
941 if ( cbxLinkBoundaries->isChecked() )
948 if ( rangeIdx < mRenderer->ranges().
size() - 1 )
954 mHistogramWidget->refresh();
960 mHistogramWidget->refresh();
966 mModel->deleteRows( classIndexes );
967 mHistogramWidget->refresh();
973 mHistogramWidget->refresh();
980 for (
int i = 1;i < ranges.
size();++i )
982 if ( ranges[i] < ranges[i-1] )
1001 tr(
"Linked range warning" ),
1002 tr(
"Rows will be reordered before linking boundaries. Continue?" ),
1003 QMessageBox::Ok | QMessageBox::Cancel );
1004 if ( result != QMessageBox::Ok )
1006 cbxLinkBoundaries->setChecked(
false );
1023 if ( item->
column() == 2 )
1026 int idx = item->
row();
1044 txtLegendFormat->text(),
1045 spinPrecision->value(),
1046 cbxTrimTrailingZeroes->isChecked() );
1057 QModelIndexList selectedIndexes = m->
selectedRows( 1 );
1058 if ( m && !selectedIndexes.isEmpty() )
1061 QModelIndexList::const_iterator indexIt = selectedIndexes.
constBegin();
1062 for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
1065 if ( list.
size() < 3 )
1070 double lowerBound = list.
at( 0 ).toDouble();
1071 double upperBound = list.
at( 2 ).toDouble();
1075 selectedSymbols.
append( s );
1089 return it->symbol();
1099 mModel->updateSymbology();
1101 mHistogramWidget->refresh();
1111 viewGraduated->selectionModel()->clear();
1114 cbxLinkBoundaries->setChecked(
false );
1129 if ( event->
key() == Qt::Key_C &&
event->modifiers() == Qt::ControlModifier )
1134 else if ( event->
key() == Qt::Key_V &&
event->modifiers() == Qt::ControlModifier )
1139 mModel->addClass( *rIt );
QList< QgsRendererRangeV2 > QgsRangeList
void setSymbolSizes(double minSize, double maxSize)
set varying symbol size for classes
QByteArray data(const QString &mimeType) const
void setLabelFormat(const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges=false)
Set the label format used to generate default classification labels.
static QgsGraduatedSymbolRendererV2 * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsGraduatedSymbolRendererV2 from an existing renderer.
void updateClasses(QgsVectorLayer *vlayer, Mode mode, int nclasses)
Recalculate classes for a layer.
QString upperValue() const
void append(const T &value)
static QgsExpressionContextScope * atlasScope(const QgsAtlasComposition *atlas)
Creates a new scope which contains variables and functions relating to a QgsAtlasComposition.
void updateSymbols(QgsSymbolV2 *sym)
Update all the symbols but leave breaks and colors.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QString classAttribute() const
virtual QgsSymbolV2 * clone() const =0
bool updateRangeRenderState(int rangeIndex, bool render)
The output shall be in pixels.
const T & at(int i) const
void setSizeScaleField(const QString &fieldOrExpression)
QgsMapUnitScale mapUnitScale() const
double minSymbolSize() const
return the min symbol size when graduated by size
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
void updateColorRamp(QgsVectorColorRampV2 *ramp=nullptr, bool inverted=false)
Update the color ramp used.
void calculateLabelPrecision(bool updateRanges=true)
Reset the label decimal places to a numberbased on the minimum class interval.
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
void setScaleMethod(QgsSymbolV2::ScaleMethod scaleMethod)
void setLowerValue(const QString &val)
double toDouble(bool *ok) const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QString tr(const char *sourceText, const char *disambiguation, int n)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
QgsVectorColorRampV2 * sourceColorRamp()
Returns the source color ramp, from which each classes' color is derived.
void setMapUnitScale(const QgsMapUnitScale &scale)
The QgsMapSettings class contains configuration for rendering of the map.
long featureCount(QgsSymbolV2 *symbol)
Number of features rendered with specified symbol.
double upperValue() const
void setColor(const QColor &color)
QString readEntry(const QString &scope, const QString &key, const QString &def=QString::null, bool *ok=nullptr) const
The output shall be in millimeters.
QString number(int n, int base)
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
int count(const T &value) const
void setGraduatedMethod(GraduatedMethod method)
set the method used for graduation (either size or color)
void append(const T &value)
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
void sortByLabel(Qt::SortOrder order=Qt::AscendingOrder)
StandardButton question(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QModelIndexList selectedRows(int column) const
QgsSymbolV2 * sourceSymbol()
Returns the renderer's source symbol, which is the base symbol used for the each classes' symbol befo...
QgsSymbolV2 * symbol() const
GraduatedMethod graduatedMethod() const
return the method used for graduation (either size or color)
The output shall be in map unitx.
void setOverrideCursor(const QCursor &cursor)
bool updateRangeLowerValue(int rangeIndex, double value)
void restoreOverrideCursor()
QGis::GeometryType geometryType() const
Returns point, line or polygon.
virtual QVariant data(const QModelIndex &index, int role) const =0
Single scope for storing variables and functions for use within a QgsExpressionContext.
const QgsRendererRangeV2LabelFormat & labelFormat() const
Return the label format used to generate default classification labels.
void sortByValue(Qt::SortOrder order=Qt::AscendingOrder)
void moveClass(int from, int to)
Moves the category at index position from to index position to.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
static QgsSymbolV2 * defaultSymbol(QGis::GeometryType geomType)
return new default symbol for specified geometry type
double lowerValue() const
double maxSymbolSize() const
return the max symbol size when graduated by size
const QgsRangeList & ranges() const
void setClassAttribute(const QString &attr)
static QgsProject * instance()
access to canonical QgsProject instance
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the dialog.
void setUpperValue(const QString &val)
bool updateRangeSymbol(int rangeIndex, QgsSymbolV2 *symbol)
QgsSymbolV2::OutputUnit outputUnit() const
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
void setSourceColorRamp(QgsVectorColorRampV2 *ramp)
Sets the source color ramp.
bool updateRangeUpperValue(int rangeIndex, double value)
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
void setData(const QString &mimeType, const QByteArray &data)
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
const_iterator constEnd() const
const_iterator constBegin() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
void deleteClass(int idx)
const QAbstractItemModel * model() const
void setOutputUnit(QgsSymbolV2::OutputUnit u)
QString lowerValue() const
void addClass(QgsSymbolV2 *symbol)
bool updateRangeLabel(int rangeIndex, const QString &label)