[QGIS Commit] r11392 - in trunk/qgis/src/app: . attributetable

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Aug 15 18:28:07 EDT 2009


Author: jef
Date: 2009-08-15 18:28:06 -0400 (Sat, 15 Aug 2009)
New Revision: 11392

Modified:
   trunk/qgis/src/app/attributetable/qgsattributetabledelegate.cpp
   trunk/qgis/src/app/attributetable/qgsattributetabledelegate.h
   trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.cpp
   trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.h
   trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.h
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.h
   trunk/qgis/src/app/qgsattributeeditor.cpp
   trunk/qgis/src/app/qgsattributeeditor.h
Log:
attribute editor fixes:
- fix column misindexing in attribute table, if the geometry column is not last
- fix NULL handling (fixes #1876)



Modified: trunk/qgis/src/app/attributetable/qgsattributetabledelegate.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetabledelegate.cpp	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/attributetable/qgsattributetabledelegate.cpp	2009-08-15 22:28:06 UTC (rev 11392)
@@ -39,6 +39,20 @@
   return NULL;
 }
 
+int QgsAttributeTableDelegate::fieldIdx( const QModelIndex &index ) const
+{
+  const QgsAttributeTableModel *tm = dynamic_cast<const QgsAttributeTableModel*>( index.model() );
+  if ( tm )
+    return tm->fieldIdx( index.column() );
+
+  const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel*>( index.model() );
+  if ( fm )
+    return fm->tableModel()->fieldIdx( index.column() );
+
+  return -1;
+}
+
+
 QWidget *QgsAttributeTableDelegate::createEditor(
   QWidget *parent,
   const QStyleOptionViewItem &option,
@@ -48,7 +62,7 @@
   if ( vl == NULL )
     return NULL;
 
-  QWidget *widget = QgsAttributeEditor::createAttributeEditor( parent, vl, index.column(), index.model()->data( index ) );
+  QWidget *widget = QgsAttributeEditor::createAttributeEditor( parent, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
   widget->adjustSize();
 
   QgsAttributeTableView *tv = dynamic_cast<QgsAttributeTableView *>( parent->parentWidget() );
@@ -65,12 +79,21 @@
     return;
 
   QVariant value;
-  if ( !QgsAttributeEditor::retrieveValue( editor, vl, index.column(), value ) )
+  if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx( index ), value ) )
     return;
 
   model->setData( index, value );
 }
 
+void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
+{
+  QgsVectorLayer *vl = layer( index.model() );
+  if ( vl == NULL )
+    return;
+
+  QgsAttributeEditor::setValue( editor, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
+}
+
 void QgsAttributeTableDelegate::paint( QPainter * painter,
                                        const QStyleOptionViewItem & option,
                                        const QModelIndex & index ) const

Modified: trunk/qgis/src/app/attributetable/qgsattributetabledelegate.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetabledelegate.h	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/attributetable/qgsattributetabledelegate.h	2009-08-15 22:28:06 UTC (rev 11392)
@@ -31,6 +31,7 @@
     Q_OBJECT;
 
     QgsVectorLayer *layer( const QAbstractItemModel *model ) const;
+    int fieldIdx( const QModelIndex &index ) const;
 
   public:
     /** Constructor
@@ -52,13 +53,20 @@
       const QModelIndex & index ) const;
 
     /**
-     * Sets data from editor backk to model. Overloads default metod
+     * Sets data from editor back to model. Overloads default method
      * @param editor editor which was created by create editor function in this class
      * @param model model where data should be updated
      * @param index index of field which is to be modified
      */
     void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
 
+    /**
+     * Sets data from model into the editor. Overloads default method
+     * @param editor editor which was created by create editor function in this class
+     * @param index index of field which is to be retrieved
+     */
+    void setEditorData( QWidget *editor, const QModelIndex &index ) const;
+
 };
 
 #endif //QGSATTRIBUTETABLEDELEGATE_H

Modified: trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.cpp	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.cpp	2009-08-15 22:28:06 UTC (rev 11392)
@@ -23,10 +23,10 @@
 
 void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
 {
-  (( QgsAttributeTableModel * )sourceModel() )->sort( column, order );
+  tableModel()->sort( column, order );
 }
 
-QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer* theLayer )
+QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer *theLayer )
 {
   mLayer = theLayer;
   mHideUnselected = false;
@@ -36,21 +36,7 @@
 bool QgsAttributeTableFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
 {
   if ( mHideUnselected )
-    // unreadable? yes, i agree :-)
-    return mLayer->selectedFeaturesIds().contains((( QgsAttributeTableModel * )sourceModel() )->rowToId( sourceRow ) );
+    return mLayer->selectedFeaturesIds().contains( tableModel()->rowToId( sourceRow ) );
 
   return true;
 }
-
-/*
-QModelIndex QgsAttributeTableFilterModel::mapFromSource ( const QModelIndex& sourceIndex ) const
-{
-  return sourceIndex;
-}
-
-QModelIndex QgsAttributeTableFilterModel::mapToSource ( const QModelIndex& filterIndex ) const
-{
-  return filterIndex;
-}
-*/
-

Modified: trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.h	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.h	2009-08-15 22:28:06 UTC (rev 11392)
@@ -23,6 +23,8 @@
 //QGIS Includes
 #include "qgsvectorlayer.h" //QgsAttributeList
 
+class QgsAttributeTableModel;
+
 class QgsAttributeTableFilterModel: public QSortFilterProxyModel
 {
   public:
@@ -42,6 +44,7 @@
     //QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;
 
     QgsVectorLayer *layer() const { return mLayer; }
+    QgsAttributeTableModel *tableModel() const { return reinterpret_cast<QgsAttributeTableModel*>( sourceModel() ); }
 
   protected:
     /**

Modified: trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.h	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.h	2009-08-15 22:28:06 UTC (rev 11392)
@@ -28,7 +28,7 @@
 #include "qgsattributetablemodel.h"
 #include "qgsattributetableidcolumnpair.h"
 
-class QgsAttributeTableMemoryModel: public QgsAttributeTableModel
+class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
 {
     Q_OBJECT;
 

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2009-08-15 22:28:06 UTC (rev 11392)
@@ -259,6 +259,11 @@
   return mRowIdMap[id];
 }
 
+int QgsAttributeTableModel::fieldIdx( int col ) const
+{
+  return mAttributes[ col ];
+}
+
 int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
 {
   return mFeatureCount;
@@ -364,15 +369,19 @@
   if ( !mLastRow )
     return QVariant( "ERROR" );
 
-  QVariant& val = ( *mLastRow )[ mAttributes[index.column()] ];
+  QVariant &val = ( *mLastRow )[ mAttributes[index.column()] ];
 
   if ( val.isNull() )
   {
     // if the value is NULL, show that in table, but don't show "NULL" text in editor
     if ( role == Qt::EditRole )
-      return QVariant();
+    {
+      return QVariant( fldType );
+    }
     else
+    {
       return QVariant( "NULL" );
+    }
   }
 
   // force also numeric data for EditRole to be strings

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.h	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.h	2009-08-15 22:28:06 UTC (rev 11392)
@@ -100,6 +100,10 @@
      */
     int idToRow( const int id ) const;
     /**
+     * get field index from column
+     */
+    int fieldIdx( int col ) const;
+    /**
      * Maps row to feature id
      * @param id row id
      */

Modified: trunk/qgis/src/app/qgsattributeeditor.cpp
===================================================================
--- trunk/qgis/src/app/qgsattributeeditor.cpp	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/qgsattributeeditor.cpp	2009-08-15 22:28:06 UTC (rev 11392)
@@ -74,10 +74,6 @@
       for ( QList<QVariant>::iterator it = values.begin(); it != values.end(); it++ )
         cb->addItem( it->toString() );
 
-      int idx = cb->findText( value.toString() );
-      if ( idx >= 0 )
-        cb->setCurrentIndex( idx );
-
       myWidget = cb;
     }
     break;
@@ -93,11 +89,7 @@
       {
         cb->addItem( *s_it );
       }
-      int idx = cb->findText( value.toString() );
-      if ( idx >= 0 )
-      {
-        cb->setCurrentIndex( idx );
-      }
+
       myWidget = cb;
     }
     break;
@@ -113,10 +105,6 @@
         cb->addItem( it.key(), it.value() );
       }
 
-      int idx = cb->findData( value );
-      if ( idx >= 0 )
-        cb->setCurrentIndex( idx );
-
       myWidget = cb;
     }
     break;
@@ -151,10 +139,6 @@
         cb->addItem( it.value(), it.key() );
       }
 
-      int idx = cb->findData( value );
-      if ( idx >= 0 )
-        cb->setCurrentIndex( idx );
-
       myWidget = cb;
     }
     break;
@@ -174,7 +158,6 @@
 
           sb->setRange( min, max );
           sb->setSingleStep( step );
-          sb->setValue( value.toInt() );
 
           myWidget = sb;
         }
@@ -184,7 +167,6 @@
 
           sl->setRange( min, max );
           sl->setSingleStep( step );
-          sl->setValue( value.toInt() );
 
           myWidget = sl;
         }
@@ -199,7 +181,6 @@
 
         dsb->setRange( min, max );
         dsb->setSingleStep( step );
-        dsb->setValue( value.toDouble() );
 
         myWidget = dsb;
         break;
@@ -213,7 +194,7 @@
     case QgsVectorLayer::Immutable:
     default:
     {
-      QLineEdit *le = new QLineEdit( value.toString(), parent );
+      QLineEdit *le = new QLineEdit( parent );
 
       if ( editType == QgsVectorLayer::Immutable )
       {
@@ -249,7 +230,7 @@
 
     case QgsVectorLayer::FileName:
     {
-      QLineEdit *le = new QLineEdit( value.toString() );
+      QLineEdit *le = new QLineEdit();
 
       QPushButton *pb = new QPushButton( tr( "..." ) );
       connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectFileName() ) );
@@ -264,6 +245,8 @@
     break;
   }
 
+  setValue( myWidget, vl, idx, value );
+
   return myWidget;
 }
 
@@ -279,6 +262,10 @@
   {
     text = le->text();
     modified = le->isModified();
+    if ( text == "NULL" )
+    {
+      text = QString::null;
+    }
   }
 
   QComboBox *cb = dynamic_cast<QComboBox *>( widget );
@@ -332,7 +319,7 @@
       }
       else if ( modified )
       {
-        value = QVariant( QString::null );
+        value = QVariant( theField.type() );
       }
     }
     break;
@@ -347,7 +334,7 @@
       }
       else if ( modified )
       {
-        value = QVariant( QString::null );
+        value = QVariant( theField.type() );
       }
     }
     break;
@@ -359,3 +346,93 @@
 
   return modified;
 }
+
+bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value )
+{
+  QgsVectorLayer::EditType editType = vl->editType( idx );
+  const QgsField &field = vl->pendingFields()[idx];
+  QVariant::Type myFieldType = field.type();
+
+  switch ( editType )
+  {
+    case QgsVectorLayer::UniqueValues:
+    case QgsVectorLayer::Enumeration:
+    case QgsVectorLayer::ValueMap:
+    {
+      QComboBox *cb = dynamic_cast<QComboBox *>( editor );
+      if ( cb == NULL )
+        return false;
+      int idx = cb->findText( value.toString() );
+      if ( idx < 0 )
+        return false;
+
+      cb->setCurrentIndex( idx );
+    }
+    break;
+
+    case QgsVectorLayer::SliderRange:
+    case QgsVectorLayer::EditRange:
+    {
+      if ( myFieldType == QVariant::Int )
+      {
+        if ( editType == QgsVectorLayer::EditRange )
+        {
+          QSpinBox *sb = dynamic_cast<QSpinBox*>( editor );
+          if ( sb == NULL )
+            return false;
+          sb->setValue( value.toInt() );
+        }
+        else
+        {
+          QSlider *sl = dynamic_cast<QSlider *>( editor );
+          if ( sl == NULL )
+            return false;
+          sl->setValue( value.toInt() );
+        }
+        break;
+      }
+      else if ( myFieldType == QVariant::Double )
+      {
+        QDoubleSpinBox *dsb = dynamic_cast<QDoubleSpinBox*>( editor );
+        if ( dsb == NULL )
+          return false;
+        dsb->setValue( value.toDouble() );
+      }
+    }
+
+    // fall-through
+
+    case QgsVectorLayer::LineEdit:
+    case QgsVectorLayer::UniqueValuesEditable:
+    case QgsVectorLayer::Immutable:
+    default:
+    {
+      QLineEdit *le = dynamic_cast<QLineEdit*>( editor );
+      if ( le == NULL )
+        return false;
+
+      QString text;
+      if ( value.isNull() )
+        if ( myFieldType == QVariant::Int || myFieldType == QVariant::Double )
+          text = "";
+        else
+          text = "NULL";
+      else
+        text = value.toString();
+
+      le->setText( text );
+    }
+    break;
+
+    case QgsVectorLayer::FileName:
+    {
+      QLineEdit *le = editor->findChild<QLineEdit *>();
+      if ( le == NULL )
+        return false;
+      le->setText( value.toString() );
+    }
+    break;
+  }
+
+  return true;
+}

Modified: trunk/qgis/src/app/qgsattributeeditor.h
===================================================================
--- trunk/qgis/src/app/qgsattributeeditor.h	2009-08-15 21:01:59 UTC (rev 11391)
+++ trunk/qgis/src/app/qgsattributeeditor.h	2009-08-15 22:28:06 UTC (rev 11392)
@@ -30,12 +30,12 @@
 
   public:
     QgsAttributeEditor( QObject *parent ) : QObject( parent ) {}
+    static QWidget *createAttributeEditor( QWidget *parent, QgsVectorLayer *vl, int idx, const QVariant &value );
+    static bool retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value );
+    static bool setValue( QWidget *widget, QgsVectorLayer *vl, int idx, const QVariant &value );
 
   public slots:
     void selectFileName( void );
-
-    static QWidget *createAttributeEditor( QWidget *parent, QgsVectorLayer *vl, int idx, const QVariant &value );
-    static bool retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value );
 };
 
 



More information about the QGIS-commit mailing list