35 #include <QMessageBox>
36 #include <QStandardItemModel>
37 #include <QStandardItem>
40 #include <QFileDialog>
44 , mMimeFormat(
"application/x-qgscategorizedsymbolrendererv2model" )
66 if ( !mRenderer )
return;
80 int row = index.
row();
81 if ( row >= catList.
size() )
85 return catList.
at( row );
93 return Qt::ItemIsDropEnabled;
96 Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
99 flags |= Qt::ItemIsEditable;
106 return Qt::MoveAction;
111 if ( !index.
isValid() || !mRenderer )
116 if ( role == Qt::CheckStateRole && index.
column() == 0 )
118 return category.
renderState() ? Qt::Checked : Qt::Unchecked;
120 else if ( role == Qt::DisplayRole || role == Qt::ToolTipRole )
125 case 2:
return category.
label();
129 else if ( role == Qt::DecorationRole && index.
column() == 0 && category.
symbol() )
133 else if ( role == Qt::TextAlignmentRole )
135 return ( index.
column() == 0 ) ? Qt::AlignHCenter : Qt::AlignLeft;
137 else if ( role == Qt::EditRole )
141 case 1:
return category.
value();
142 case 2:
return category.
label();
155 if ( index.
column() == 0 && role == Qt::CheckStateRole )
162 if ( role != Qt::EditRole )
176 case QVariant::Double:
199 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 3 )
202 return lst.
value( section );
209 if ( parent.
isValid() || !mRenderer )
224 if (
hasIndex( row, column, parent ) )
240 types << mMimeFormat;
249 QDataStream stream( &encodedData, QIODevice::WriteOnly );
257 stream << index.
row();
259 mimeData->
setData( mMimeFormat, encodedData );
267 if ( action != Qt::MoveAction )
return true;
269 if ( !data->
hasFormat( mMimeFormat ) )
return false;
272 QDataStream stream( &encodedData, QIODevice::ReadOnly );
275 while ( !stream.
atEnd() )
282 int to = parent.
row();
286 for (
int i = rows.
size() - 1; i >= 0; i-- )
291 if ( rows[i] < t ) t--;
294 for (
int j = 0; j < i; j++ )
296 if ( to < rows[j] && rows[i] > rows[j] ) rows[j] += 1;
299 if ( rows[i] < to ) to--;
308 for (
int i = rows.
size() - 1; i >= 0; i-- )
334 else if ( column == 2 )
354 if ( element == QStyle::PE_IndicatorItemViewItemDrop && !option->rect.isNull() )
357 opt.rect.setLeft( 0 );
359 opt.rect.setHeight( 0 );
360 if ( widget ) opt.rect.setRight( widget->
width() );
391 mOldClassificationAttribute = attrName;
396 mExpressionWidget->setLayer(
mLayer );
398 cboCategorizedColorRamp->populate(
mStyle );
399 int randomIndex = cboCategorizedColorRamp->findText(
tr(
"Random colors" ) );
400 if ( randomIndex != -1 )
402 cboCategorizedColorRamp->setCurrentIndex( randomIndex );
407 if ( defaultColorRamp !=
"" )
409 int index = cboCategorizedColorRamp->findText( defaultColorRamp, Qt::MatchCaseSensitive );
411 cboCategorizedColorRamp->setCurrentIndex( index );
422 viewCategories->setModel( mModel );
423 viewCategories->resizeColumnToContents( 0 );
424 viewCategories->resizeColumnToContents( 1 );
425 viewCategories->resizeColumnToContents( 2 );
442 connect( cboCategorizedColorRamp, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT(
applyColorRamp() ) );
456 btnAdvanced->setMenu( advMenu );
477 mExpressionWidget->setField( attrName );
505 if ( selectedCats.
size() > 0 )
515 foreach (
const int idx, selectedCats )
532 if ( m && i.size() > 0 )
557 btnChangeCategorizedSymbol->setIcon( icon );
583 symbol = symbol->
clone();
605 int num = values.
count();
607 bool hasNull =
false;
609 for (
int i = 0; i < num; i++ )
634 if ( cboCategorizedColorRamp->count() == 0 )
635 QMessageBox::critical(
this,
tr(
"Error" ),
tr(
"There are no available color ramps. You can add them in Style Manager." ) );
636 else if ( !cboCategorizedColorRamp->createNewColorRampSelected() )
645 QString attrName = mExpressionWidget->currentField();
658 if ( unique_vals.
contains( value ) )
660 unique_vals << value;
669 if ( unique_vals.
size() >= 1000 )
672 tr(
"Classification would yield %1 entries which might not be expected. Continue?" ).arg( unique_vals.
size() ),
673 QMessageBox::Ok | QMessageBox::Cancel,
674 QMessageBox::Cancel );
675 if ( res == QMessageBox::Cancel )
682 DlgAddCategories dlg(
mStyle, createDefaultSymbol(), unique_vals,
this );
689 bool deleteExisting =
false;
691 if ( !mOldClassificationAttribute.
isEmpty() &&
692 attrName != mOldClassificationAttribute &&
696 tr(
"Confirm Delete" ),
697 tr(
"The classification field was changed from '%1' to '%2'.\n"
698 "Should the existing classes be deleted before classification?" )
699 .arg( mOldClassificationAttribute ).arg( attrName ),
700 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel );
701 if ( res == QMessageBox::Cancel )
706 deleteExisting = ( res == QMessageBox::Yes );
710 bool keepExistingColors =
false;
711 if ( !deleteExisting )
714 keepExistingColors = prevCats.
size() > 0;
715 for (
int i = 0; i < cats.
size(); ++i )
717 bool contains =
false;
719 for (
int j = 0; j < prevCats.
size() && !contains; ++j )
721 if ( prevCats.
at( j ).value() == value )
734 mOldClassificationAttribute = attrName;
781 QModelIndex idx = viewCategories->selectionModel()->currentIndex();
790 QModelIndexList selectedRows = viewCategories->selectionModel()->selectedRows();
843 if ( m && selectedIndexes.size() > 0 )
846 QModelIndexList::const_iterator indexIt = selectedIndexes.
constBegin();
847 for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
849 int row = ( *indexIt ).row();
853 selectedSymbols.
append( s );
867 if ( m && selectedIndexes.size() > 0 )
869 QModelIndexList::const_iterator indexIt = selectedIndexes.constBegin();
870 for ( ; indexIt != selectedIndexes.constEnd(); ++indexIt )
885 viewCategories->selectionModel()->clear();
894 tr(
"Matched %1 categories to symbols." ).arg( matched ) );
899 tr(
"No categories could be matched to symbols in library." ) );
932 tr(
"XML files (*.xml *XML)" ) );
942 if ( !importedStyle.
importXML( fileName ) )
945 tr(
"An error occured reading file:\n%1" ).arg( importedStyle.
errorString() ) );
953 tr(
"Matched %1 categories to symbols from file." ).arg( matched ) );
958 tr(
"No categories could be matched to symbols in file." ) );
969 if ( event->
key() == Qt::Key_C &&
event->modifiers() == Qt::ControlModifier )
974 else if ( event->
key() == Qt::Key_V &&
event->modifiers() == Qt::ControlModifier )
977 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
QgsVectorColorRampV2 * sourceColorRamp()
void append(const T &value)
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
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 importXML(QString filename)
Imports the symbols and colorramps into the default style database from the given XML file...
bool updateCategoryRenderState(int catIndex, bool render)
void addCategory(const QgsRendererCategoryV2 &cat)
QgsSymbolV2 * symbol(QString name)
return a NEW copy of symbol
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...
void setSizeScaleField(QString fieldOrExpression)
QString tr(const char *sourceText, const char *disambiguation, int n)
void setSourceColorRamp(QgsVectorColorRampV2 *ramp)
QString rotationField() const override
return rotation field name (or empty string if not set or not supported by renderer) ...
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)
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
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)
void setClassAttribute(QString attr)
QGis::GeometryType geometryType() const
Returns point, line or polygon.
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)
bool contains(const T &value) const
void beginInsertRows(const QModelIndex &parent, int first, int last)
bool updateCategoryLabel(int catIndex, QString label)
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()
QString readEntry(const QString &scope, const QString &key, const QString &def=QString::null, bool *ok=0) const
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)
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
void setRotationField(QString fieldOrExpression) override
sets rotation field of renderer (if supported by the renderer)
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)
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
QgsSymbolV2::ScaleMethod scaleMethod() const
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)
QString errorString()
return last error from load/save operation
QgsSymbolV2 * symbol() const
void setInvertedColorRamp(bool inverted)