36 #include <QMessageBox>
37 #include <QStandardItemModel>
38 #include <QStandardItem>
41 #include <QFileDialog>
45 , mMimeFormat(
"application/x-qgscategorizedsymbolrendererv2model" )
67 if ( !mRenderer )
return;
81 int row = index.
row();
82 if ( row >= catList.
size() )
86 return catList.
at( row );
94 return Qt::ItemIsDropEnabled;
97 Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
100 flags |= Qt::ItemIsEditable;
107 return Qt::MoveAction;
112 if ( !index.
isValid() || !mRenderer )
117 if ( role == Qt::CheckStateRole && index.
column() == 0 )
119 return category.
renderState() ? Qt::Checked : Qt::Unchecked;
121 else if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
126 case 2:
return category.
label();
130 else if ( role == Qt::DecorationRole && index.
column() == 0 && category.
symbol() )
134 else if ( role == Qt::TextAlignmentRole )
136 return ( index.
column() == 0 ) ? Qt::AlignHCenter : Qt::AlignLeft;
138 else if ( role == Qt::EditRole )
142 case 1:
return category.
value();
143 case 2:
return category.
label();
156 if ( index.
column() == 0 && role == Qt::CheckStateRole )
163 if ( role != Qt::EditRole )
177 case QVariant::Double:
200 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 3 )
203 return lst.
value( section );
210 if ( parent.
isValid() || !mRenderer )
225 if (
hasIndex( row, column, parent ) )
241 types << mMimeFormat;
250 QDataStream stream( &encodedData, QIODevice::WriteOnly );
258 stream << index.
row();
260 mimeData->
setData( mMimeFormat, encodedData );
268 if ( action != Qt::MoveAction )
return true;
270 if ( !data->
hasFormat( mMimeFormat ) )
return false;
273 QDataStream stream( &encodedData, QIODevice::ReadOnly );
276 while ( !stream.
atEnd() )
283 int to = parent.
row();
287 for (
int i = rows.
size() - 1; i >= 0; i-- )
292 if ( rows[i] < t ) t--;
295 for (
int j = 0; j < i; j++ )
297 if ( to < rows[j] && rows[i] > rows[j] ) rows[j] += 1;
300 if ( rows[i] < to ) to--;
309 for (
int i = rows.
size() - 1; i >= 0; i-- )
335 else if ( column == 2 )
355 if ( element == QStyle::PE_IndicatorItemViewItemDrop && !option->rect.isNull() )
358 opt.rect.setLeft( 0 );
360 opt.rect.setHeight( 0 );
361 if ( widget ) opt.rect.setRight( widget->
width() );
417 mOldClassificationAttribute = attrName;
422 mExpressionWidget->setLayer(
mLayer );
424 cboCategorizedColorRamp->populate(
mStyle );
425 int randomIndex = cboCategorizedColorRamp->findText(
tr(
"Random colors" ) );
426 if ( randomIndex != -1 )
428 cboCategorizedColorRamp->setCurrentIndex( randomIndex );
433 if ( defaultColorRamp !=
"" )
435 int index = cboCategorizedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
437 cboCategorizedColorRamp->setCurrentIndex( index );
448 viewCategories->setModel( mModel );
449 viewCategories->resizeColumnToContents( 0 );
450 viewCategories->resizeColumnToContents( 1 );
451 viewCategories->resizeColumnToContents( 2 );
468 connect( cboCategorizedColorRamp, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT(
applyColorRamp() ) );
470 connect( mButtonEditRamp, SIGNAL( clicked() ), cboCategorizedColorRamp, SLOT( editSourceRamp() ) );
479 btnAdvanced->setMenu( advMenu );
503 mExpressionWidget->setField( attrName );
531 if ( selectedCats.
size() > 0 )
542 Q_FOREACH (
int idx, selectedCats )
559 if ( m && i.size() > 0 )
586 btnChangeCategorizedSymbol->setIcon( icon );
612 symbol = symbol->
clone();
635 int num = values.
count();
637 bool hasNull =
false;
639 for (
int i = 0; i < num; i++ )
664 if ( cboCategorizedColorRamp->count() == 0 )
665 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
666 else if ( !cboCategorizedColorRamp->createNewColorRampSelected() )
675 QString attrName = mExpressionWidget->currentField();
688 expression->
prepare( &context );
695 if ( unique_vals.
contains( value ) )
697 unique_vals << value;
706 if ( unique_vals.
size() >= 1000 )
709 tr(
"Classification would yield %1 entries which might not be expected. Continue?" ).arg( unique_vals.
size() ),
710 QMessageBox::Ok | QMessageBox::Cancel,
711 QMessageBox::Cancel );
712 if ( res == QMessageBox::Cancel )
719 DlgAddCategories dlg(
mStyle, createDefaultSymbol(), unique_vals,
this );
726 bool deleteExisting =
false;
728 if ( !mOldClassificationAttribute.
isEmpty() &&
729 attrName != mOldClassificationAttribute &&
733 tr(
"Confirm Delete" ),
734 tr(
"The classification field was changed from '%1' to '%2'.\n"
735 "Should the existing classes be deleted before classification?" )
736 .arg( mOldClassificationAttribute, attrName ),
737 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel );
738 if ( res == QMessageBox::Cancel )
743 deleteExisting = ( res == QMessageBox::Yes );
747 bool keepExistingColors =
false;
748 if ( !deleteExisting )
751 keepExistingColors = prevCats.
size() > 0;
752 for (
int i = 0; i < cats.
size(); ++i )
754 bool contains =
false;
756 for (
int j = 0; j < prevCats.
size() && !contains; ++j )
758 if ( prevCats.
at( j ).value() == value )
771 mOldClassificationAttribute = attrName;
818 QModelIndex idx = viewCategories->selectionModel()->currentIndex();
827 QModelIndexList selectedRows = viewCategories->selectionModel()->selectedRows();
875 if ( m && selectedIndexes.size() > 0 )
878 QModelIndexList::const_iterator indexIt = selectedIndexes.
constBegin();
879 for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
881 int row = ( *indexIt ).row();
885 selectedSymbols.
append( s );
899 if ( m && selectedIndexes.size() > 0 )
901 QModelIndexList::const_iterator indexIt = selectedIndexes.constBegin();
902 for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
917 viewCategories->selectionModel()->clear();
926 tr(
"Matched %1 categories to symbols." ).arg( matched ) );
931 tr(
"No categories could be matched to symbols in library." ) );
964 tr(
"XML files (*.xml *XML)" ) );
974 if ( !importedStyle.
importXML( fileName ) )
977 tr(
"An error occured reading file:\n%1" ).arg( importedStyle.
errorString() ) );
985 tr(
"Matched %1 categories to symbols from file." ).arg( matched ) );
990 tr(
"No categories could be matched to symbols in file." ) );
1001 if ( event->
key() == Qt::Key_C &&
event->modifiers() == Qt::ControlModifier )
1003 mCopyBuffer.
clear();
1006 else if ( event->
key() == Qt::Key_V &&
event->modifiers() == Qt::ControlModifier )
1009 for ( ; rIt != mCopyBuffer.
constEnd(); ++rIt )
bool hasIndex(int row, int column, const QModelIndex &parent) const
static void sortVariantList(QList< QVariant > &list, Qt::SortOrder order)
Sorts the passed list in requested order.
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
QgsRendererCategoryV2 category(const QModelIndex &index)
QByteArray data(const QString &mimeType) const
const QgsCategoryList & categories() const
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
bool importXML(const QString &filename)
Imports the symbols and colorramps into the default style database from the given XML file...
QgsVectorColorRampV2 * sourceColorRamp()
void append(const T &value)
static QgsExpressionContextScope * atlasScope(const QgsAtlasComposition *atlas)
Creates a new scope which contains variables and functions relating to a QgsAtlasComposition.
Q_DECL_DEPRECATED bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
QVariant data(const QModelIndex &index, int role) const override
void uniqueValues(int index, QList< QVariant > &uniqueValues, int limit=-1)
Returns unique values for column.
void setSourceSymbol(QgsSymbolV2 *sym)
QStringList mimeTypes() const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
virtual QgsSymbolV2 * clone() const =0
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
const T & at(int i) const
bool updateCategoryRenderState(int catIndex, bool render)
void addCategory(const QgsRendererCategoryV2 &cat)
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
void moveCategory(int from, int to)
Moves the category at index position from to index position to.
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QString tr(const char *sourceText, const char *disambiguation, int n)
void setClassAttribute(const QString &attr)
void setSourceColorRamp(QgsVectorColorRampV2 *ramp)
Sets the source color ramp.
The QgsMapSettings class contains configuration for rendering of the map.
void setValue(const QString &key, const QVariant &value)
void sortByLabel(Qt::SortOrder order=Qt::AscendingOrder)
void updateSymbols(QgsSymbolV2 *sym)
void setColor(const QColor &color)
void deleteAllCategories()
QList< QgsRendererCategoryV2 > QgsCategoryList
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
int count(const T &value) const
void append(const T &value)
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
int toInt(bool *ok) const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
StandardButton question(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
virtual QgsVectorColorRampV2 * clone() const =0
bool updateCategoryLabel(int catIndex, const QString &label)
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static QgsStyleV2 * defaultStyle()
return default application-wide style
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndexList selectedRows(int column) const
QMimeData * mimeData(const QModelIndexList &indexes) const override
QgsCategorizedSymbolRendererV2Model(QObject *parent=0)
QGis::GeometryType geometryType() const
Returns point, line or polygon.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsCategorizedSymbolRendererV2ViewStyle(QStyle *style=0)
void setScaleMethod(QgsSymbolV2::ScaleMethod scaleMethod)
static QgsCategorizedSymbolRendererV2 * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsCategorizedSymbolRendererV2 from an existing renderer.
QModelIndex createIndex(int row, int column, void *ptr) const
void updateColorRamp(QgsVectorColorRampV2 *ramp, bool inverted=false)
Update the color ramp used and all symbols colors.
QgsSymbolV2 * symbol(const QString &name)
return a NEW copy of symbol
bool contains(const T &value) const
void beginInsertRows(const QModelIndex &parent, int first, int last)
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget=0) const override
QVariant value(const QString &key, const QVariant &defaultValue) const
QgsSymbolV2 * sourceSymbol()
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
QString readEntry(const QString &scope, const QString &key, const QString &def=QString::null, bool *ok=0) const
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
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setRenderer(QgsCategorizedSymbolRendererV2 *renderer)
bool updateCategoryValue(int catIndex, const QVariant &value)
bool deleteCategory(int catIndex)
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
static QgsProject * instance()
access to canonical QgsProject instance
QString sizeScaleField() const
bool updateCategorySymbol(int catIndex, QgsSymbolV2 *symbol)
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the dialog.
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
Qt::DropActions supportedDropActions() const override
QString classAttribute() const
int columnCount(const QModelIndex &=QModelIndex()) const override
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
double toDouble(bool *ok) const
void deleteRows(QList< int > rows)
void setData(const QString &mimeType, const QByteArray &data)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
QgsSymbolV2::ScaleMethod scaleMethod() const
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
const_iterator constEnd() const
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
bool nextFeature(QgsFeature &f)
const_iterator constBegin() const
Qt::ItemFlags flags(const QModelIndex &index) const override
QString absolutePath() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void addCategory(const QgsRendererCategoryV2 &category)
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
void sortByValue(Qt::SortOrder order=Qt::AscendingOrder)
void setSizeScaleField(const QString &fieldOrExpression)
QString errorString()
return last error from load/save operation
QgsSymbolV2 * symbol() const
void setInvertedColorRamp(bool inverted)