Skip to content
Permalink
Browse files

Gui cleanups for attribute capture when digitising. Create a dynamica…

…lly generated dialog rather than using a table widget.

git-svn-id: http://svn.osgeo.org/qgis/trunk@8556 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
timlinux
timlinux committed May 30, 2008
1 parent bd5ec63 commit a1719bd83b4f4be99f53a0e48ccf149344136ef8
Showing with 169 additions and 194 deletions.
  1. +112 −118 src/app/qgsattributedialog.cpp
  2. +21 −34 src/app/qgsattributedialog.h
  3. +26 −17 src/app/qgsmaptooladdfeature.cpp
  4. +10 −25 src/ui/qgsattributedialogbase.ui
@@ -22,148 +22,142 @@
#include <QTableWidgetItem>
#include <QSettings>
#include <QLineEdit>

QgsAttributeDialog::QgsAttributeDialog(const QgsFieldMap& fields, const QgsAttributeMap& attributes)
#include <QSpinBox>
#include <QLabel>
#include <QDoubleSpinBox>
#include <QFrame>
#include <QScrollArea>

QgsAttributeDialog::QgsAttributeDialog(
QgsFieldMap theFieldMap, QgsFeature * thepFeature)
: QDialog(),
_settingsPath("/Windows/AttributeDialog/"),
mRowIsDirty(attributes.size(), FALSE)
mSettingsPath("/Windows/AttributeDialog/"),
mFieldMap(theFieldMap),
mpFeature(thepFeature)
{
restorePositionAndColumnWidth();

setupUi(this);
mTable->setRowCount(attributes.size());

int index=0;
for (QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it)
setupUi(this);
if (mpFeature==NULL) return;
if (mFieldMap.isEmpty()) return;
QgsAttributeMap myAttributes = mpFeature->attributeMap();
//
//Set up dynamic inside a scroll box
//
QVBoxLayout * mypOuterLayout = new QVBoxLayout();
mypOuterLayout->setContentsMargins(0,0,0,0);
//transfers layout ownership so no need to call delete
mFrame->setLayout(mypOuterLayout);
QScrollArea * mypScrollArea = new QScrollArea();
//transfers scroll area ownership so no need to call delete
mypOuterLayout->addWidget(mypScrollArea);
QFrame * mypInnerFrame = new QFrame();
mypInnerFrame->setFrameShape(QFrame::NoFrame);
mypInnerFrame->setFrameShadow(QFrame::Plain);
//transfers frame ownership so no need to call delete
mypScrollArea->setWidget(mypInnerFrame);
mypScrollArea->setWidgetResizable( true );
QGridLayout * mypInnerLayout = new QGridLayout(mypInnerFrame);

int index=0;
for (QgsAttributeMap::const_iterator it = myAttributes.begin();
it != myAttributes.end();
++it)
{
QString myFieldName = mFieldMap[it.key()].name();
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 )
{
// set attribute name

QString fieldName = fields[it.key()].name();

QTableWidgetItem * myFieldItem = new QTableWidgetItem(fieldName);
myFieldItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
mTable->setItem(index, 0, myFieldItem);

#if QT_VERSION >= 0x040400
// set attribute value
QTableWidgetItem * myValueItem = new QTableWidgetItem((*it).toString());
mTable->setItem(index, 1, myValueItem);
#endif

QLineEdit *le = new QLineEdit();

le->setFrame(false);

if( fields[it.key()].type()==QVariant::Int )
{
le->setValidator( new QIntValidator(le) );
}
else if( fields[it.key()].type()==QVariant::Double )
{
le->setValidator( new QDoubleValidator(le) );
}

#if QT_VERSION < 0x040400
le->setText((*it).toString());
#endif

mTable->setCellWidget(index, 1, le);

++index;
mypLineEdit->setValidator( new QIntValidator(mypLineEdit) );
mypLabel->setText(myFieldName + tr(" (int)"));
}

// setup the mechanism to track edited attribute values
// if we do it this way, only edited attributes will
// be attempted to be saved when the editing session stops.
connect(mTable, SIGNAL(cellChanged(int, int)),
this, SLOT (setAttributeValueChanged(int, int)));

mTable->resizeColumnsToContents();
}

QgsAttributeDialog::~QgsAttributeDialog()
{

else if( mFieldMap[it.key()].type()==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;
++index;
}
restoreGeometry();
}

QString QgsAttributeDialog::value(int row)
{
return static_cast<QLineEdit*>(mTable->cellWidget(row,1))->text();
}

bool QgsAttributeDialog::isDirty(int row)
QgsAttributeDialog::~QgsAttributeDialog()
{
return mRowIsDirty.at(row);
saveGeometry();
}

bool QgsAttributeDialog::queryAttributes(const QgsFieldMap& fields, QgsFeature& f)
void QgsAttributeDialog::accept()
{
QgsAttributeMap featureAttributes = f.attributeMap();
QgsAttributeDialog attdialog(fields, featureAttributes);

if (attdialog.exec() == QDialog::Accepted)
//write the new values back to the feature
QgsAttributeMap myAttributes = mpFeature->attributeMap();
int myIndex=0;
for (QgsAttributeMap::const_iterator it = myAttributes.begin();
it != myAttributes.end();
++it)
{
int i=0;
for (QgsAttributeMap::const_iterator it = featureAttributes.begin(); it != featureAttributes.end(); ++it, i++)
//Q_ASSERT(myIndex <= mpWidgets.size());
QString myFieldName = mFieldMap[it.key()].name();
bool myFlag=false;
QString myFieldValue =
dynamic_cast<QLineEdit *>(mpWidgets.value(myIndex))->text();
switch( mFieldMap[it.key()].type() )
{
QString value = attdialog.value(i);

if( attdialog.isDirty(i) )
{
switch( fields[it.key()].type() )
case QVariant::Int:
{
case QVariant::Int:
f.changeAttribute(it.key(), value=="" ? QVariant( QString::null ) : QVariant( value.toInt() ) );
break;

case QVariant::Double:
f.changeAttribute(it.key(), value=="" ? QVariant( QString::null ) : QVariant( value.toDouble() ) );
break;

default:
f.changeAttribute(it.key(), value=="NULL" ? QVariant(QString::null) : QVariant(value) );
break;
int myIntValue = myFieldValue.toInt(&myFlag);
if (myFlag && ! myFieldValue.isEmpty())
{
mpFeature->changeAttribute( it.key(), QVariant(myIntValue) );
}
else
{
mpFeature->changeAttribute( it.key(), QVariant(QString::null) );
}
}
break;
case QVariant::Double:
{
double myDblValue = myFieldValue.toDouble(&myFlag);
if (myFlag && ! myFieldValue.isEmpty())
{
mpFeature->changeAttribute( it.key(), QVariant(myDblValue) );
}
else
{
mpFeature->changeAttribute( it.key(), QVariant(QString::null) );
}
}
} else {
f.changeAttribute(it.key(), QVariant(value) );
}
break;
default: //string
mpFeature->changeAttribute(it.key(),QVariant( myFieldValue ) );
break;
}
return true;
}
else
{
return false;
++myIndex;
}
QDialog::accept();
}

void QgsAttributeDialog::savePositionAndColumnWidth()
void QgsAttributeDialog::saveGeometry()
{
QSettings settings;
settings.setValue(_settingsPath+"geometry", saveGeometry());
settings.setValue(mSettingsPath+"geometry", QDialog::saveGeometry());
}

void QgsAttributeDialog::resizeEvent(QResizeEvent *event)
{
savePositionAndColumnWidth();
QWidget::resizeEvent(event);
}

void QgsAttributeDialog::moveEvent(QMoveEvent *event)
{
savePositionAndColumnWidth();
QWidget::moveEvent(event);
}

void QgsAttributeDialog::restorePositionAndColumnWidth()
void QgsAttributeDialog::restoreGeometry()
{
QSettings settings;
restoreGeometry(settings.value(_settingsPath+"geometry").toByteArray());
QDialog::restoreGeometry(settings.value(mSettingsPath+"geometry").toByteArray());
}

void QgsAttributeDialog::setAttributeValueChanged(int row, int column)
{
QgsDebugMsg("Entered with row " + QString::number(row) +
", column " + QString::number(column) + ".");

mRowIsDirty.at(row) = TRUE;
}
@@ -25,49 +25,36 @@

class QDialog;
class QgsFeature;

class QLayout;
class QgsField;
typedef QMap<int, QgsField> QgsFieldMap;

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

public:
QgsAttributeDialog(const QgsFieldMap& fields, const QgsAttributeMap& attributes);

~QgsAttributeDialog();

/** Returns if the field value of a row was edited since this dialog opened */
bool isDirty(int row);

/** Opens an attribute dialog and queries the attributes for a given feature. The
attribute values are set to the feature if the dialog is accepted.
\retval true if accepted
\retval false if canceled */
static bool queryAttributes(const QgsFieldMap& fields, QgsFeature& f);

// Saves and restores the size and position from the last time
// this dialog box was used.
void savePositionAndColumnWidth();

void restorePositionAndColumnWidth();

void resizeEvent(QResizeEvent *event);

void moveEvent(QMoveEvent *event);

public slots:
//! Slot to be called when an attribute value is edited in the table.
void setAttributeValueChanged(int row, int column);
QgsAttributeDialog(const QgsFieldMap thepFieldMap, QgsFeature * thepFeature);
~QgsAttributeDialog();

/** Overloaded accept method which will write the feature field
* values, then delegate to QDialog::accept()
*/
void accept();
/** Saves the size and position for the next time
* this dialog box was used.
*/
void saveGeometry();
/** Restores the size and position from the last time
* this dialog box was used.
*/
void restoreGeometry();

private:
QString _settingsPath;

/** Returns the field value of a row */
QString value(int row);

std::vector<bool> mRowIsDirty;
QString mSettingsPath;
QList<QWidget *> mpWidgets;
QgsFieldMap mFieldMap;
QgsFeature * mpFeature;
};

#endif
@@ -170,16 +170,23 @@ void QgsMapToolAddFeature::canvasReleaseEvent(QMouseEvent * e)
// add the fields to the QgsFeature
const QgsFieldMap fields=provider->fields();
for(QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); ++it)
{
f->addAttribute(it.key(), provider->getDefaultValue(it.key()) );
}
{
f->addAttribute(it.key(), provider->getDefaultValue(it.key()) );
}

// show the dialog to enter attribute values
if (QgsAttributeDialog::queryAttributes(fields, *f))
QgsAttributeDialog * mypDialog = new QgsAttributeDialog(fields,f );
if (mypDialog->exec())
{
qDebug("Adding feature to layer");
vlayer->addFeature(*f);
}
else
{
qDebug("Adding feature to layer failed");
delete f;

}
delete mypDialog;
mCanvas->refresh();
}

@@ -429,19 +436,21 @@ void QgsMapToolAddFeature::canvasReleaseEvent(QMouseEvent * e)
f->addAttribute(it.key(), provider->getDefaultValue(it.key()));
}

if (QgsAttributeDialog::queryAttributes(fields, *f))
{
if(vlayer->addFeature(*f))
{
//add points to other features to keep topology up-to-date
int topologicalEditing = QgsProject::instance()->readNumEntry("Digitizing", "/TopologicalEditing", 0);
if(topologicalEditing)
{
vlayer->addTopologicalPoints(f->geometry());
}
}
}
QgsAttributeDialog * mypDialog = new QgsAttributeDialog(fields,f );
if (mypDialog->exec())
{
if(vlayer->addFeature(*f))
{
//add points to other features to keep topology up-to-date
int topologicalEditing = QgsProject::instance()->readNumEntry("Digitizing", "/TopologicalEditing", 0);
if(topologicalEditing)
{
vlayer->addTopologicalPoints(f->geometry());
}
}
}
delete f;
delete mypDialog;

// delete the elements of mCaptureList
mCaptureList.clear();

0 comments on commit a1719bd

Please sign in to comment.
You can’t perform that action at this time.