Skip to content

Commit

Permalink
select value for classification in attribute dialog from unique value…
Browse files Browse the repository at this point in the history
… renderer classes

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8776 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Jul 14, 2008
1 parent ba4948a commit 588c21f
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 28 deletions.
121 changes: 98 additions & 23 deletions src/app/qgsattributedialog.cpp
Expand Up @@ -19,26 +19,33 @@
#include "qgsfield.h"
#include "qgslogger.h"

#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsuniquevaluerenderer.h"
#include "qgssymbol.h"

#include <QTableWidgetItem>
#include <QSettings>
#include <QLineEdit>
#include <QSpinBox>
#include <QComboBox>
#include <QLabel>
#include <QDoubleSpinBox>
#include <QFrame>
#include <QScrollArea>

QgsAttributeDialog::QgsAttributeDialog(
QgsFieldMap theFieldMap, QgsFeature * thepFeature)
QgsAttributeDialog::QgsAttributeDialog(QgsVectorLayer *vl, QgsFeature * thepFeature)
: QDialog(),
mSettingsPath("/Windows/AttributeDialog/"),
mFieldMap(theFieldMap),
mpFeature(thepFeature)
mpFeature(thepFeature),
mLayer(vl)
{

setupUi(this);
if (mpFeature==NULL) return;
if (mFieldMap.isEmpty()) return;
if (mpFeature==NULL || vl->getDataProvider()==NULL )
return;

const QgsFieldMap &theFieldMap = vl->getDataProvider()->fields();

if (theFieldMap.isEmpty()) return;

QgsAttributeMap myAttributes = mpFeature->attributeMap();
//
//Set up dynamic inside a scroll box
Expand All @@ -58,35 +65,89 @@ QgsAttributeDialog::QgsAttributeDialog(
mypScrollArea->setWidgetResizable( true );
QGridLayout * mypInnerLayout = new QGridLayout(mypInnerFrame);


int classificationField = -1;
QStringList values;

const QgsUniqueValueRenderer *uvr = dynamic_cast<const QgsUniqueValueRenderer *>( mLayer->renderer() );
if( uvr )
{
classificationField = uvr->classificationField();

const QList<QgsSymbol *> symbols = uvr->symbols();

for(int i=0; i<symbols.size(); i++)
{
values.append( symbols[i]->lowerValue() );
}
}

int index=0;
for (QgsAttributeMap::const_iterator it = myAttributes.begin();
it != myAttributes.end();
++it)
{
QString myFieldName = mFieldMap[it.key()].name();
QString myFieldName = theFieldMap[it.key()].name();
int myFieldType = theFieldMap[it.key()].type();
QLabel * mypLabel = new QLabel();
mypInnerLayout->addWidget(mypLabel,index,0);
QVariant myFieldValue = it.value();
QLineEdit * mypLineEdit = new QLineEdit();
//the provider may have provided a default value so use it
mypLineEdit->setText(myFieldValue.toString());
if( mFieldMap[it.key()].type()==QVariant::Int )

QWidget *myWidget;
if(classificationField!=it.key())
{
QLineEdit *le = new QLineEdit();

//the provider may have provided a default value so use it
le->setText(myFieldValue.toString());

if( myFieldType==QVariant::Int )
{
le->setValidator( new QIntValidator(le) );
}
else if( myFieldType==QVariant::Double )
{
le->setValidator( new QIntValidator(le) );
}

myWidget = le;
}
else
{
QComboBox *cb = new QComboBox();
cb->addItems(values);
cb->setEditable(true);

//the provider may have provided a default value so use it
cb->setEditText(myFieldValue.toString());

if( myFieldType==QVariant::Int ) {
cb->setValidator( new QIntValidator(cb) );
}
else if( myFieldType==QVariant::Double )
{
cb->setValidator( new QIntValidator(cb) );
}

myWidget = cb;
}

if( myFieldType==QVariant::Int )
{
mypLineEdit->setValidator( new QIntValidator(mypLineEdit) );
mypLabel->setText(myFieldName + tr(" (int)"));
}
else if( mFieldMap[it.key()].type()==QVariant::Double )
else if( myFieldType==QVariant::Double )
{
mypLineEdit->setValidator( new QDoubleValidator(mypLineEdit) );
mypLabel->setText(myFieldName + tr(" (dbl)"));
}
else //string
{
//any special behaviour for string goes here
mypLabel->setText(myFieldName + tr(" (txt)"));
}
mypInnerLayout->addWidget(mypLineEdit,index,1);
mpWidgets << mypLineEdit;

mypInnerLayout->addWidget(myWidget, index, 1);
mpWidgets << myWidget;
++index;
}
restoreGeometry();
Expand All @@ -107,12 +168,26 @@ void QgsAttributeDialog::accept()
it != myAttributes.end();
++it)
{
const QgsFieldMap &theFieldMap = mLayer->getDataProvider()->fields();

//Q_ASSERT(myIndex <= mpWidgets.size());
QString myFieldName = mFieldMap[it.key()].name();
QString myFieldName = theFieldMap[it.key()].name();
bool myFlag=false;
QString myFieldValue =
dynamic_cast<QLineEdit *>(mpWidgets.value(myIndex))->text();
switch( mFieldMap[it.key()].type() )
QString myFieldValue;

QLineEdit *le = dynamic_cast<QLineEdit *>(mpWidgets.value(myIndex));
if(le)
{
myFieldValue = le->text();
}

QComboBox *cb = dynamic_cast<QComboBox *>(mpWidgets.value(myIndex));
if(cb)
{
myFieldValue = cb->currentText();
}

switch( theFieldMap[it.key()].type() )
{
case QVariant::Int:
{
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsattributedialog.h
Expand Up @@ -27,14 +27,14 @@ class QDialog;
class QgsFeature;
class QLayout;
class QgsField;
typedef QMap<int, QgsField> QgsFieldMap;
class QgsVectorLayer;

class QgsAttributeDialog: public QDialog, private Ui::QgsAttributeDialogBase
{
Q_OBJECT;

public:
QgsAttributeDialog(const QgsFieldMap thepFieldMap, QgsFeature * thepFeature);
QgsAttributeDialog(QgsVectorLayer *vl, QgsFeature * thepFeature);
~QgsAttributeDialog();

/** Overloaded accept method which will write the feature field
Expand All @@ -53,7 +53,7 @@ class QgsAttributeDialog: public QDialog, private Ui::QgsAttributeDialogBase
private:
QString mSettingsPath;
QList<QWidget *> mpWidgets;
QgsFieldMap mFieldMap;
QgsVectorLayer *mLayer;
QgsFeature * mpFeature;
};

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmaptooladdfeature.cpp
Expand Up @@ -175,7 +175,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent(QMouseEvent * e)
}

// show the dialog to enter attribute values
QgsAttributeDialog * mypDialog = new QgsAttributeDialog(fields,f );
QgsAttributeDialog * mypDialog = new QgsAttributeDialog( vlayer, f );
if (mypDialog->exec())
{
qDebug("Adding feature to layer");
Expand Down Expand Up @@ -436,7 +436,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent(QMouseEvent * e)
f->addAttribute(it.key(), provider->getDefaultValue(it.key()));
}

QgsAttributeDialog * mypDialog = new QgsAttributeDialog(fields,f );
QgsAttributeDialog * mypDialog = new QgsAttributeDialog( vlayer, f );
if (mypDialog->exec())
{
if(vlayer->addFeature(*f))
Expand Down

0 comments on commit 588c21f

Please sign in to comment.