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

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon May 4 06:19:25 EDT 2009


Author: timlinux
Date: 2009-05-04 06:19:25 -0400 (Mon, 04 May 2009)
New Revision: 10718

Modified:
   trunk/qgis/src/app/CMakeLists.txt
   trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.h
   trunk/qgis/src/app/attributetable/qgsattributetableview.cpp
   trunk/qgis/src/app/attributetable/qgsattributetableview.h
Log:
Refactoring to follow the 1 class :: 1 file rule. Also renamed mem model to memorymodel to comply with coding standards

Modified: trunk/qgis/src/app/CMakeLists.txt
===================================================================
--- trunk/qgis/src/app/CMakeLists.txt	2009-05-04 10:16:02 UTC (rev 10717)
+++ trunk/qgis/src/app/CMakeLists.txt	2009-05-04 10:19:25 UTC (rev 10718)
@@ -88,7 +88,11 @@
   
   attributetable/qgsattributetabledialog.cpp
   attributetable/qgsattributetablemodel.cpp
+  attributetable/qgsattributetablememorymodel.cpp
   attributetable/qgsattributetableview.cpp
+  attributetable/qgsattributetablefiltermodel.cpp
+  attributetable/qgsattributetableidcolumnpair.cpp
+  attributetable/qgsattributetabledelegate.cpp
   )
 
 
@@ -157,8 +161,10 @@
   ogr/qgsopenvectorlayerdialog.h
   ogr/qgsnewogrconnection.h
   
+  attributetable/qgsattributetablemodel.h
+  attributetable/qgsattributetablememorymodel.h
   attributetable/qgsattributetabledialog.h
-  attributetable/qgsattributetablemodel.h
+  attributetable/qgsattributetabledelegate.h
   )
 
 IF (POSTGRES_FOUND)

Modified: trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp	2009-05-04 10:16:02 UTC (rev 10717)
+++ trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp	2009-05-04 10:19:25 UTC (rev 10718)
@@ -18,6 +18,7 @@
 
 #include "qgsattributetabledialog.h"
 #include "qgsattributetablemodel.h"
+#include "qgsattributetablefiltermodel.h"
 #include "qgsattributetableview.h"
 
 #include <qgsapplication.h>

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2009-05-04 10:16:02 UTC (rev 10717)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2009-05-04 10:19:25 UTC (rev 10718)
@@ -14,69 +14,15 @@
  ***************************************************************************/
 
 #include "qgsattributetablemodel.h"
-//#include "qgsattributetableview.h"
+#include "qgsattributetablefiltermodel.h"
 
-#include "qgsvectordataprovider.h"
 #include "qgsfield.h"
 #include "qgsvectorlayer.h"
+#include "qgslogger.h"
 
 #include <QtGui>
 #include <QVariant>
-#include <QtAlgorithms>
-#include "qgslogger.h"
 
-//could be faster when type guessed before sorting
-bool idColumnPair::operator<( const idColumnPair &b ) const
-{
-  //QVariat thinks gid is a string!
-  QVariant::Type columnType = columnItem.type();
-
-  if ( columnType == QVariant::Int || columnType == QVariant::UInt || columnType == QVariant::LongLong || columnType == QVariant::ULongLong )
-    return columnItem.toLongLong() < b.columnItem.toLongLong();
-
-  if ( columnType == QVariant::Double )
-    return columnItem.toDouble() < b.columnItem.toDouble();
-
-  return columnItem.toString() < b.columnItem.toString();
-}
-
-//////////////////
-// Filter Model //
-//////////////////
-
-void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
-{
-  (( QgsAttributeTableModel * )sourceModel() )->sort( column, order );
-}
-
-QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer* theLayer )
-{
-  mLayer = theLayer;
-  mHideUnselected = false;
-  setDynamicSortFilter( true );
-}
-
-bool QgsAttributeTableFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
-{
-  if ( mHideUnselected )
-    // unreadable? yes, i agree :-)
-    return mLayer->selectedFeaturesIds().contains((( QgsAttributeTableModel * )sourceModel() )->rowToId( sourceRow ) );
-
-  return true;
-}
-
-/*
-QModelIndex QgsAttributeTableFilterModel::mapFromSource ( const QModelIndex& sourceIndex ) const
-{
-  return sourceIndex;
-}
-
-QModelIndex QgsAttributeTableFilterModel::mapToSource ( const QModelIndex& filterIndex ) const
-{
-  return filterIndex;
-}
-*/
-
 ////////////////////////////
 // QgsAttributeTableModel //
 ////////////////////////////
@@ -336,7 +282,7 @@
 void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
 {
   QgsAttributeMap row;
-  idColumnPair pair;
+  QgsAttributeTableIdColumnPair pair;
   QgsAttributeList attrs;
   QgsFeature f;
 
@@ -360,14 +306,14 @@
   if ( order == Qt::AscendingOrder )
     qStableSort( mSortList.begin(), mSortList.end() );
   else
-    qStableSort( mSortList.begin(), mSortList.end(), qGreater<idColumnPair>() );
+    qStableSort( mSortList.begin(), mSortList.end(), qGreater<QgsAttributeTableIdColumnPair>() );
 
   // recalculate id<->row maps
   mRowIdMap.clear();
   mIdRowMap.clear();
 
   int i = 0;
-  QList<idColumnPair>::Iterator it;
+  QList<QgsAttributeTableIdColumnPair>::Iterator it;
   for ( it = mSortList.begin(); it != mSortList.end(); ++it, ++i )
   {
     mRowIdMap.insert( i, it->id );
@@ -491,155 +437,3 @@
   emit layoutAboutToBeChanged();
 }
 
-/////////////////////
-// In-Memory model //
-/////////////////////
-
-void QgsAttributeTableMemModel::loadLayer()
-{
-  QgsAttributeTableModel::loadLayer();
-  mLayer->select( mLayer->pendingAllAttributesList(), QgsRectangle(), false );
-
-  QgsFeature f;
-  while ( mLayer->nextFeature( f ) )
-    mFeatureMap.insert( f.id(), f );
-}
-
-QgsAttributeTableMemModel::QgsAttributeTableMemModel
-( QgsVectorLayer *theLayer )
-    : QgsAttributeTableModel( theLayer )
-{
-  loadLayer();
-}
-
-QVariant QgsAttributeTableMemModel::data( const QModelIndex &index, int role ) const
-{
-  if ( !index.isValid() || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole ) )
-    return QVariant();
-
-  QVariant::Type fldType = mLayer->pendingFields()[ mAttributes[index.column()] ].type();
-  bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
-
-  if ( role == Qt::TextAlignmentRole )
-  {
-    if ( fldNumeric )
-      return QVariant( Qt::AlignRight );
-    else
-      return QVariant( Qt::AlignLeft );
-  }
-
-  // if we don't have the row in current cache, load it from layer first
-  if ( mLastRowId != rowToId( index.row() ) )
-  {
-    //bool res = mLayer->featureAtId(rowToId(index.row()), mFeat, false, true);
-    bool res = mFeatureMap.contains( rowToId( index.row() ) );
-
-    if ( !res )
-      return QVariant( "ERROR" );
-
-    mLastRowId = rowToId( index.row() );
-    mFeat = mFeatureMap[rowToId( index.row() )];
-    mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
-  }
-
-  if ( !mLastRow )
-    return QVariant( "ERROR" );
-
-  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();
-    else
-      return QVariant( "NULL" );
-  }
-
-  // force also numeric data for EditRole to be strings
-  // otherwise it creates spinboxes instead of line edits
-  // (probably not what we do want)
-  if ( fldNumeric && role == Qt::EditRole )
-    return val.toString();
-
-  // convert to QString from some other representation
-  // this prevents displaying greater numbers in exponential format
-  return val.toString();
-}
-
-bool QgsAttributeTableMemModel::setData( const QModelIndex &index, const QVariant &value, int role )
-{
-  if ( !index.isValid() || role != Qt::EditRole )
-    return false;
-
-  if ( !mLayer->isEditable() )
-    return false;
-
-  //bool res = mLayer->featureAtId(rowToId(index.row()), mFeat, false, true);
-  bool res = mFeatureMap.contains( rowToId( index.row() ) );
-
-  if ( res )
-  {
-    mLastRowId = rowToId( index.row() );
-    mFeat = mFeatureMap[rowToId( index.row() )];
-    mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
-
-
-// QgsDebugMsg(mFeatureMap[rowToId(index.row())].id());
-    mFeatureMap[rowToId( index.row() )].changeAttribute( index.column(), value );
-    // propagate back to the layer
-    mLayer->changeAttributeValue( rowToId( index.row() ), index.column(), value, true );
-  }
-
-  if ( !mLayer->isModified() )
-    return false;
-
-  emit dataChanged( index, index );
-  return true;
-}
-
-void QgsAttributeTableMemModel::featureDeleted( int fid )
-{
-  QgsDebugMsg( "entered." );
-  mFeatureMap.remove( fid );
-  QgsAttributeTableModel::featureDeleted( fid );
-}
-
-void QgsAttributeTableMemModel::featureAdded( int fid )
-{
-  QgsDebugMsg( "entered." );
-  QgsFeature f;
-  mLayer->featureAtId( fid, f, false, true );
-  mFeatureMap.insert( fid, f );
-  QgsAttributeTableModel::featureAdded( fid );
-}
-
-#if 0
-void QgsAttributeTableMemModel::attributeAdded( int idx )
-{
-  QgsDebugMsg( "entered." );
-  loadLayer();
-  reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
-}
-
-void QgsAttributeTableMemModel::attributeDeleted( int idx )
-{
-  QgsDebugMsg( "entered." );
-  loadLayer();
-  reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
-}
-#endif
-
-void QgsAttributeTableMemModel::layerDeleted()
-{
-  QgsDebugMsg( "entered." );
-  mFeatureMap.clear();
-  QgsAttributeTableModel::layerDeleted();
-}
-
-void QgsAttributeTableMemModel::attributeValueChanged( int fid, int idx, const QVariant &value )
-{
-  QgsDebugMsg( "entered." );
-  mFeatureMap[fid].changeAttribute( idx, value );
-  reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
-}

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.h	2009-05-04 10:16:02 UTC (rev 10717)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.h	2009-05-04 10:19:25 UTC (rev 10718)
@@ -14,43 +14,18 @@
  *                                                                         *
  ***************************************************************************/
 
-#ifndef QGSATTRIBUTETABKEMODEL_H
-#define QGSATTRIBUTETABKEMODEL_H
+#ifndef QGSATTRIBUTETABLEMODEL_H
+#define QGSATTRIBUTETABLEMODEL_H
 
 #include <QAbstractTableModel>
-#include <QSortFilterProxyModel>
 #include <QModelIndex>
 #include <QObject>
 
 //QGIS Includes
-#include "qgis.h"
 #include "qgsfeature.h" //QgsAttributeMap
 #include "qgsvectorlayer.h" //QgsAttributeList
+#include "qgsattributetableidcolumnpair.h" 
 
-class idColumnPair
-{
-  public:
-    int id;
-    QVariant columnItem;
-
-    bool operator<( const idColumnPair &b ) const;
-};
-
-class QgsAttributeTableFilterModel: public QSortFilterProxyModel
-{
-  public:
-    QgsAttributeTableFilterModel( QgsVectorLayer* theLayer );
-    //QModelIndex mapToSource ( const QModelIndex & filterIndex ) const;
-    //QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;
-    bool mHideUnselected;
-    virtual void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );
-  protected:
-    bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;
-  private:
-    QgsVectorLayer* mLayer;
-};
-
-
 class QgsAttributeTableModel: public QAbstractTableModel
 {
     Q_OBJECT
@@ -102,7 +77,7 @@
     mutable QgsAttributeMap *mLastRow;
     QgsAttributeList mAttributes;
 
-    QList<idColumnPair> mSortList;
+    QList<QgsAttributeTableIdColumnPair> mSortList;
     QMap<int, int> mIdRowMap;
     QMap<int, int> mRowIdMap;
 
@@ -111,30 +86,5 @@
 
 };
 
-class QgsAttributeTableMemModel: public QgsAttributeTableModel
-{
-    Q_OBJECT
 
-  public:
-    QgsAttributeTableMemModel( QgsVectorLayer *theLayer );//, QObject *parent = 0);
-
-  protected slots:
-    virtual void featureDeleted( int fid );
-    virtual void featureAdded( int fid );
-    virtual void layerDeleted();
-
-  private slots:
-    //virtual void attributeAdded (int idx);
-    //virtual void attributeDeleted (int idx);
-    virtual void attributeValueChanged( int fid, int idx, const QVariant &value );
-    //virtual void layerModified(bool onlyGeometry);
-
-  private:
-    virtual QVariant data( const QModelIndex &index, int role ) const;
-    virtual bool setData( const QModelIndex &index, const QVariant &value, int role );
-    virtual void loadLayer();
-
-    QMap<int, QgsFeature> mFeatureMap;
-};
-
 #endif

Modified: trunk/qgis/src/app/attributetable/qgsattributetableview.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetableview.cpp	2009-05-04 10:16:02 UTC (rev 10717)
+++ trunk/qgis/src/app/attributetable/qgsattributetableview.cpp	2009-05-04 10:19:25 UTC (rev 10718)
@@ -13,70 +13,20 @@
  *                                                                         *
  ***************************************************************************/
 
-#include <QModelIndex>
-#include <QItemDelegate>
-#include <QHeaderView>
-#include <QSettings>
-#include <QLineEdit>
-#include <QPainter>
 #include <QKeyEvent>
+#include <QSettings>
+#include <QHeaderView>
 
 #include "qgsattributetableview.h"
 #include "qgsattributetablemodel.h"
+#include "qgsattributetablememorymodel.h"
+#include "qgsattributetabledelegate.h"
+#include "qgsattributetablefiltermodel.h"
 
-#include "qgslogger.h"
 #include "qgsvectorlayer.h"
 #include "qgsvectordataprovider.h"
 
 
-class QgsAttributeTableDelegate : public QItemDelegate
-{
-  public:
-    QgsAttributeTableDelegate( QObject* parent = NULL ) : QItemDelegate( parent ) {}
-
-    QWidget * createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
-    {
-      QWidget *editor = QItemDelegate::createEditor( parent, option, index );
-
-      QLineEdit *le = dynamic_cast<QLineEdit*>( editor );
-      if ( !le ) return editor;
-
-      const QgsAttributeTableModel* m = dynamic_cast<const QgsAttributeTableModel*>( index.model() );
-      if ( !m ) return editor;
-
-      int col = index.column();
-      QVariant::Type type = m->layer()->dataProvider()->fields()[col].type();
-
-      if ( type == QVariant::Int )
-      {
-        le->setValidator( new QIntValidator( le ) );
-      }
-      else if ( type == QVariant::Double )
-      {
-        le->setValidator( new QDoubleValidator( le ) );
-      }
-
-      return editor;
-    }
-
-
-    void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
-    {
-      QItemDelegate::paint( painter, option, index );
-
-      if ( option.state & QStyle::State_HasFocus )
-      {
-        QRect r = option.rect.adjusted( 1, 1, -1, -1 );
-        QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
-        painter->save();
-        painter->setPen( p );
-        painter->drawRect( r );
-        painter->restore();
-      }
-    }
-
-};
-
 QgsAttributeTableView::QgsAttributeTableView( QWidget* parent )
     : QTableView( parent )
 {
@@ -103,7 +53,7 @@
   if ( layer->dataProvider()->capabilities() & QgsVectorDataProvider::RandomSelectGeometryAtId )
     bModel = new QgsAttributeTableModel( layer );
   else
-    bModel = new QgsAttributeTableMemModel( layer );
+    bModel = new QgsAttributeTableMemoryModel( layer );
 
   QgsAttributeTableFilterModel* bfModel = new QgsAttributeTableFilterModel( layer );
   bfModel->setSourceModel( bModel );

Modified: trunk/qgis/src/app/attributetable/qgsattributetableview.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetableview.h	2009-05-04 10:16:02 UTC (rev 10717)
+++ trunk/qgis/src/app/attributetable/qgsattributetableview.h	2009-05-04 10:19:25 UTC (rev 10718)
@@ -13,8 +13,8 @@
  *                                                                         *
  ***************************************************************************/
 
-#ifndef QGSATTRIBUTETABLEVIEW_H_
-#define QGSATTRIBUTETABLEVIEW_H_
+#ifndef QGSATTRIBUTETABLEVIEW_H
+#define QGSATTRIBUTETABLEVIEW_H
 
 #include <QTableView>
 
@@ -23,7 +23,7 @@
 
 class QgsAttributeTableView: public QTableView
 {
-//private slots:
+  //private slots:
     //void setRows(int rows);
 
   public:



More information about the QGIS-commit mailing list