35 #include <QMessageBox> 36 #include <QStandardItemModel> 37 #include <QStandardItem> 46 , mRenderer( nullptr )
47 , mMimeFormat(
"application/x-qgsgraduatedsymbolrendererv2model" )
67 void QgsGraduatedSymbolRendererV2Model::addClass(
QgsSymbolV2* symbol )
69 if ( !mRenderer )
return;
102 return Qt::ItemIsDropEnabled;
105 Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
107 if ( index.
column() == 2 )
109 flags |= Qt::ItemIsEditable;
115 Qt::DropActions QgsGraduatedSymbolRendererV2Model::supportedDropActions()
const 117 return Qt::MoveAction;
120 QVariant QgsGraduatedSymbolRendererV2Model::data(
const QModelIndex &index,
int role )
const 126 if ( role == Qt::CheckStateRole && index.
column() == 0 )
128 return range.
renderState() ? Qt::Checked : Qt::Unchecked;
130 else if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
137 if ( decimalPlaces < 0 ) decimalPlaces = 0;
141 return range.
label();
146 else if ( role == Qt::DecorationRole && index.
column() == 0 && range.
symbol() )
150 else if ( role == Qt::TextAlignmentRole )
152 return ( index.
column() == 0 ) ? Qt::AlignHCenter : Qt::AlignLeft;
154 else if ( role == Qt::EditRole )
160 return range.
label();
169 bool QgsGraduatedSymbolRendererV2Model::setData(
const QModelIndex & index,
const QVariant & value,
int role )
174 if ( index.
column() == 0 && role == Qt::CheckStateRole )
177 emit dataChanged( index, index );
181 if ( role != Qt::EditRole )
195 emit dataChanged( index, index );
199 QVariant QgsGraduatedSymbolRendererV2Model::headerData(
int section, Qt::Orientation orientation,
int role )
const 201 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 3 )
204 lst <<
tr(
"Symbol" ) <<
tr(
"Values" ) <<
tr(
"Legend" );
205 return lst.
value( section );
210 int QgsGraduatedSymbolRendererV2Model::rowCount(
const QModelIndex &
parent )
const 212 if ( parent.
isValid() || !mRenderer )
219 int QgsGraduatedSymbolRendererV2Model::columnCount(
const QModelIndex & index )
const 227 if ( hasIndex( row, column, parent ) )
229 return createIndex( row, column );
240 QStringList QgsGraduatedSymbolRendererV2Model::mimeTypes()
const 243 types << mMimeFormat;
247 QMimeData *QgsGraduatedSymbolRendererV2Model::mimeData(
const QModelIndexList &indexes )
const 252 QDataStream stream( &encodedData, QIODevice::WriteOnly );
260 stream << index.
row();
262 mimeData->
setData( mMimeFormat, encodedData );
266 bool QgsGraduatedSymbolRendererV2Model::dropMimeData(
const QMimeData *data, Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
270 if ( action != Qt::MoveAction )
return true;
272 if ( !data->
hasFormat( mMimeFormat ) )
return false;
275 QDataStream stream( &encodedData, QIODevice::ReadOnly );
278 while ( !stream.atEnd() )
285 int to = parent.
row();
288 if ( to == -1 ) to = mRenderer->
ranges().
size();
289 for (
int i = rows.
size() - 1; i >= 0; i-- )
294 if ( rows[i] < t ) t--;
297 for (
int j = 0; j < i; j++ )
299 if ( to < rows[j] && rows[i] > rows[j] ) rows[j] += 1;
302 if ( rows[i] < to ) to--;
304 emit dataChanged( createIndex( 0, 0 ), createIndex( mRenderer->
ranges().
size(), 0 ) );
309 void QgsGraduatedSymbolRendererV2Model::deleteRows(
QList<int> rows )
311 for (
int i = rows.
size() - 1; i >= 0; i-- )
313 beginRemoveRows(
QModelIndex(), rows[i], rows[i] );
319 void QgsGraduatedSymbolRendererV2Model::removeAllRows()
326 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 )
437 mModel =
new QgsGraduatedSymbolRendererV2Model(
this );
440 mExpressionWidget->setLayer(
mLayer );
444 cboGraduatedColorRamp->populate(
mStyle );
451 if ( defaultColorRamp !=
"" )
453 int index = cboGraduatedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
455 cboGraduatedColorRamp->setCurrentIndex( index );
459 viewGraduated->setStyle(
new QgsGraduatedSymbolRendererV2ViewStyle( viewGraduated->style() ) );
463 methodComboBox->blockSignals(
true );
464 methodComboBox->addItem(
"Color" );
467 methodComboBox->addItem(
"Size" );
468 minSizeSpinBox->setValue( 1 );
469 maxSizeSpinBox->setValue( 8 );
473 methodComboBox->addItem(
"Size" );
474 minSizeSpinBox->setValue( .1 );
475 maxSizeSpinBox->setValue( 2 );
477 methodComboBox->blockSignals(
false );
488 connect( btnGraduatedAdd, SIGNAL( clicked() ),
this, SLOT(
addClass() ) );
503 btnAdvanced->setMenu( advMenu );
505 mHistogramWidget->setLayer(
mLayer );
506 mHistogramWidget->setRenderer(
mRenderer );
507 connect( mHistogramWidget, SIGNAL( rangesModified(
bool ) ),
this, SLOT(
refreshRanges(
bool ) ) );
508 connect( mExpressionWidget, SIGNAL( fieldChanged(
QString ) ), mHistogramWidget, SLOT( setSourceFieldExp(
QString ) ) );
543 connect( mButtonEditRamp, SIGNAL( clicked() ), cboGraduatedColorRamp, SLOT( editSourceRamp() ) );
563 disconnect( mButtonEditRamp, SIGNAL( clicked() ), cboGraduatedColorRamp, SLOT( editSourceRamp() ) );
587 if ( nclasses && updateCount )
592 mExpressionWidget->setField( attrName );
593 mHistogramWidget->setSourceFieldExp( attrName );
604 viewGraduated->setModel(
mModel );
608 mSizeUnitWidget->blockSignals(
true );
611 mSizeUnitWidget->blockSignals(
false );
615 methodComboBox->blockSignals(
true );
618 methodComboBox->setCurrentIndex( 0 );
625 methodComboBox->setCurrentIndex( 1 );
632 mMethodStackedWidget->setCurrentIndex( methodComboBox->currentIndex() );
633 methodComboBox->blockSignals(
false );
636 txtLegendFormat->setText( labelFormat.
format() );
637 spinPrecision->setValue( labelFormat.
precision() );
640 viewGraduated->resizeColumnToContents( 0 );
641 viewGraduated->resizeColumnToContents( 1 );
642 viewGraduated->resizeColumnToContents( 2 );
644 mHistogramWidget->refresh();
657 mMethodStackedWidget->setCurrentIndex( idx );
665 if ( cboGraduatedColorRamp->count() == 0 )
666 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
686 mModel->updateSymbology( reset );
690 void QgsGraduatedSymbolRendererV2Widget::cleanUpSymbolSelector(
QgsPanelWidget *container )
699 void QgsGraduatedSymbolRendererV2Widget::updateSymbolsFromWidget()
705 mSizeUnitWidget->blockSignals(
true );
706 mSizeUnitWidget->setUnit( mGraduatedSymbol->outputUnit() );
708 mSizeUnitWidget->blockSignals(
false );
712 if ( m && !selectedIndexes.isEmpty() )
714 Q_FOREACH (
const QModelIndex& idx, selectedIndexes )
718 int rangeIdx = idx.
row();
720 if ( selectedIndexes.count() > 1 )
742 QString attrName = mExpressionWidget->currentField();
744 int nclasses = spinGraduatedClasses->value();
749 if ( cboGraduatedColorRamp->count() == 0 )
750 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
757 if ( cboGraduatedMode->currentIndex() == 0 )
759 else if ( cboGraduatedMode->currentIndex() == 2 )
761 else if ( cboGraduatedMode->currentIndex() == 3 )
763 else if ( cboGraduatedMode->currentIndex() == 4 )
772 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 ) )
781 if ( methodComboBox->currentIndex() == 0 )
787 if ( cboGraduatedColorRamp->count() == 0 )
788 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
803 if ( methodComboBox->currentIndex() == 1 )
848 btnChangeGraduatedSymbol->setIcon( icon );
852 int QgsRendererV2PropertiesDialog::currentRangeRow()
854 QModelIndex idx = viewGraduated->selectionModel()->currentIndex();
864 QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
879 QModelIndexList selectedRows = viewGraduated->selectionModel()->selectedRows();
880 QModelIndexList::const_iterator sIt = selectedRows.
constBegin();
882 for ( ; sIt != selectedRows.constEnd(); ++sIt )
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();
961 mHistogramWidget->refresh();
967 mModel->deleteRows( classIndexes );
968 mHistogramWidget->refresh();
974 mHistogramWidget->refresh();
981 for (
int i = 1;i < ranges.
size();++i )
983 if ( ranges[i] < ranges[i-1] )
1002 tr(
"Linked range warning" ),
1003 tr(
"Rows will be reordered before linking boundaries. Continue?" ),
1004 QMessageBox::Ok | QMessageBox::Cancel );
1005 if ( result != QMessageBox::Ok )
1007 cbxLinkBoundaries->setChecked(
false );
1024 if ( item->
column() == 2 )
1027 int idx = item->
row();
1045 txtLegendFormat->text(),
1046 spinPrecision->value(),
1047 cbxTrimTrailingZeroes->isChecked() );
1058 QModelIndexList selectedIndexes = m->
selectedRows( 1 );
1059 if ( m && !selectedIndexes.isEmpty() )
1062 QModelIndexList::const_iterator indexIt = selectedIndexes.
constBegin();
1063 for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
1066 if ( list.
size() < 3 )
1071 double lowerBound = list.
at( 0 ).toDouble();
1072 double upperBound = list.
at( 2 ).toDouble();
1076 selectedSymbols.
append( s );
1086 if ( decimalPlaces < 0 )
1088 double precision = 1.0 / qPow( 10, decimalPlaces );
1094 return it->symbol();
1104 mModel->updateSymbology();
1106 mHistogramWidget->refresh();
1117 viewGraduated->selectionModel()->clear();
1120 cbxLinkBoundaries->setChecked(
false );
1137 if ( event->
key() == Qt::Key_C &&
event->modifiers() == Qt::ControlModifier )
1142 else if ( event->
key() == Qt::Key_V &&
event->modifiers() == Qt::ControlModifier )
1147 mModel->addClass( *rIt );
QList< QgsRendererRangeV2 > QgsRangeList
void setSymbolSizes(double minSize, double maxSize)
set varying symbol size for classes
QByteArray data(const QString &mimeType) const
QString readEntry(const QString &scope, const QString &key, const QString &def=QString::null, bool *ok=nullptr) 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.
void append(const T &value)
static QgsExpressionContextScope * atlasScope(const QgsAtlasComposition *atlas)
Creates a new scope which contains variables and functions relating to a QgsAtlasComposition.
QgsSymbolV2 * symbol() const
void updateSymbols(QgsSymbolV2 *sym)
Update all the symbols but leave breaks and colors.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
GraduatedMethod graduatedMethod() const
return the method used for graduation (either size or color)
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)
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)
Compare two doubles (but allow some difference)
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.
void setColor(const QColor &color)
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...
QString classAttribute() const
void sortByLabel(Qt::SortOrder order=Qt::AscendingOrder)
StandardButton question(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
const QgsRangeList & ranges() const
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...
The output shall be in map unitx.
void setOverrideCursor(const QCursor &cursor)
bool updateRangeLowerValue(int rangeIndex, double value)
void restoreOverrideCursor()
virtual QVariant data(const QModelIndex &index, int role) const=0
QGis::GeometryType geometryType() const
Returns point, line or polygon.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsMapUnitScale mapUnitScale() const
void sortByValue(Qt::SortOrder order=Qt::AscendingOrder)
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
QgsSymbolV2::OutputUnit outputUnit() const
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.
const QgsRendererRangeV2LabelFormat & labelFormat() const
Return the label format used to generate default classification labels.
static QgsSymbolV2 * defaultSymbol(QGis::GeometryType geomType)
return new default symbol for specified geometry type
void setClassAttribute(const QString &attr)
double minSymbolSize() const
return the min symbol size when graduated by size
static QgsProject * instance()
Returns the QgsProject singleton instance.
void setUpperValue(const QString &val)
bool updateRangeSymbol(int rangeIndex, QgsSymbolV2 *symbol)
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
double maxSymbolSize() const
return the max symbol size when graduated by size
void setSourceColorRamp(QgsVectorColorRampV2 *ramp)
Sets the source color ramp.
QString lowerValue() const
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)
QString upperValue() const
double lowerValue() const
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)
Abstract base class for color ramps.
const QAbstractItemModel * model() const
void setOutputUnit(QgsSymbolV2::OutputUnit u)
void addClass(QgsSymbolV2 *symbol)
double upperValue() const
bool updateRangeLabel(int rangeIndex, const QString &label)