[QGIS Commit] r8556 - in trunk/qgis/src: app ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri May 30 12:56:26 EDT 2008


Author: timlinux
Date: 2008-05-30 12:56:26 -0400 (Fri, 30 May 2008)
New Revision: 8556

Modified:
   trunk/qgis/src/app/qgsattributedialog.cpp
   trunk/qgis/src/app/qgsattributedialog.h
   trunk/qgis/src/app/qgsmaptooladdfeature.cpp
   trunk/qgis/src/ui/qgsattributedialogbase.ui
Log:
Gui cleanups for attribute capture when digitising. Create a dynamically generated dialog rather than using a table widget.


Modified: trunk/qgis/src/app/qgsattributedialog.cpp
===================================================================
--- trunk/qgis/src/app/qgsattributedialog.cpp	2008-05-30 16:23:40 UTC (rev 8555)
+++ trunk/qgis/src/app/qgsattributedialog.cpp	2008-05-30 16:56:26 UTC (rev 8556)
@@ -22,148 +22,142 @@
 #include <QTableWidgetItem>
 #include <QSettings>
 #include <QLineEdit>
+#include <QSpinBox>
+#include <QLabel>
+#include <QDoubleSpinBox>
+#include <QFrame>
+#include <QScrollArea>
 
-QgsAttributeDialog::QgsAttributeDialog(const QgsFieldMap& fields, const QgsAttributeMap& attributes)
+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());
+  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 = attributes.begin(); it != attributes.end(); ++it)
+  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();
+    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();
 }
 
+
 QgsAttributeDialog::~QgsAttributeDialog()
 {
-
+  saveGeometry();
 }
-
-QString QgsAttributeDialog::value(int row)
+  
+void QgsAttributeDialog::accept()
 {
-  return static_cast<QLineEdit*>(mTable->cellWidget(row,1))->text();
-}
-
-bool QgsAttributeDialog::isDirty(int row)
-{
-  return mRowIsDirty.at(row);
-}
-
-bool QgsAttributeDialog::queryAttributes(const QgsFieldMap& fields, QgsFeature& f)
-{
-  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) );
+          }
         }
-      } else {
-        f.changeAttribute(it.key(), QVariant(value) );
-      }
+        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) );
+          }
+        }
+        break;
+      default: //string
+        mpFeature->changeAttribute(it.key(),QVariant( myFieldValue ) );
+        break;
     }
-    return true;
+    ++myIndex;
   }
-  else
-  {
-    return false;
-  }
+  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;
-}

Modified: trunk/qgis/src/app/qgsattributedialog.h
===================================================================
--- trunk/qgis/src/app/qgsattributedialog.h	2008-05-30 16:23:40 UTC (rev 8555)
+++ trunk/qgis/src/app/qgsattributedialog.h	2008-05-30 16:56:26 UTC (rev 8556)
@@ -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(const QgsFieldMap thepFieldMap, QgsFeature * thepFeature);
+  ~QgsAttributeDialog();
 
-    ~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();
 
-    /** 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);
-
   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

Modified: trunk/qgis/src/app/qgsmaptooladdfeature.cpp
===================================================================
--- trunk/qgis/src/app/qgsmaptooladdfeature.cpp	2008-05-30 16:23:40 UTC (rev 8555)
+++ trunk/qgis/src/app/qgsmaptooladdfeature.cpp	2008-05-30 16:56:26 UTC (rev 8556)
@@ -170,16 +170,23 @@
 	  // 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 @@
 	      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();

Modified: trunk/qgis/src/ui/qgsattributedialogbase.ui
===================================================================
--- trunk/qgis/src/ui/qgsattributedialogbase.ui	2008-05-30 16:23:40 UTC (rev 8555)
+++ trunk/qgis/src/ui/qgsattributedialogbase.ui	2008-05-30 16:56:26 UTC (rev 8556)
@@ -5,8 +5,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>329</width>
-    <height>330</height>
+    <width>447</width>
+    <height>343</height>
    </rect>
   </property>
   <property name="windowTitle" >
@@ -14,28 +14,16 @@
   </property>
   <layout class="QGridLayout" >
    <item row="0" column="0" >
-    <widget class="QTableWidget" name="mTable" >
-     <property name="columnCount" >
-      <number>2</number>
+    <widget class="QFrame" name="mFrame" >
+     <property name="frameShape" >
+      <enum>QFrame::StyledPanel</enum>
      </property>
-     <row>
-      <property name="text" >
-       <string>1</string>
-      </property>
-     </row>
-     <column>
-      <property name="text" >
-       <string>Attribute</string>
-      </property>
-     </column>
-     <column>
-      <property name="text" >
-       <string>Value</string>
-      </property>
-     </column>
+     <property name="frameShadow" >
+      <enum>QFrame::Raised</enum>
+     </property>
     </widget>
    </item>
-   <item row="1" column="0" >
+   <item row="2" column="0" >
     <widget class="QDialogButtonBox" name="buttonBox" >
      <property name="standardButtons" >
       <set>QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok</set>
@@ -45,9 +33,6 @@
   </layout>
  </widget>
  <layoutdefault spacing="6" margin="11" />
- <tabstops>
-  <tabstop>mTable</tabstop>
- </tabstops>
  <resources/>
  <connections>
   <connection>
@@ -58,7 +43,7 @@
    <hints>
     <hint type="sourcelabel" >
      <x>277</x>
-     <y>305</y>
+     <y>318</y>
     </hint>
     <hint type="destinationlabel" >
      <x>325</x>



More information about the QGIS-commit mailing list