@@ -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 ;
}