[QGIS Commit] r10726 - trunk/qgis/src/app/attributetable

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon May 4 10:26:03 EDT 2009


Author: timlinux
Date: 2009-05-04 10:26:03 -0400 (Mon, 04 May 2009)
New Revision: 10726

Added:
   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/qgsattributetableidcolumnpair.cpp
   trunk/qgis/src/app/attributetable/qgsattributetableidcolumnpair.h
   trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.cpp
   trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.h
Log:
Added missing files

Added: trunk/qgis/src/app/attributetable/qgsattributetabledelegate.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetabledelegate.cpp	                        (rev 0)
+++ trunk/qgis/src/app/attributetable/qgsattributetabledelegate.cpp	2009-05-04 14:26:03 UTC (rev 10726)
@@ -0,0 +1,70 @@
+/***************************************************************************
+     QgsAttributeTableDelegate.cpp
+     --------------------------------------
+    Date                 : Feb 2009
+    Copyright            : (C) 2009 Vita Cizek
+    Email                : weetya (at) gmail.com
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include <QItemDelegate>
+#include <QLineEdit>
+#include <QPainter>
+
+#include "qgsattributetableview.h"
+#include "qgsattributetablemodel.h"
+#include "qgsattributetabledelegate.h"
+#include "qgsvectordataprovider.h"
+
+QWidget * QgsAttributeTableDelegate::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 QgsAttributeTableDelegate::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();
+  }
+}
+

Added: trunk/qgis/src/app/attributetable/qgsattributetabledelegate.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetabledelegate.h	                        (rev 0)
+++ trunk/qgis/src/app/attributetable/qgsattributetabledelegate.h	2009-05-04 14:26:03 UTC (rev 10726)
@@ -0,0 +1,46 @@
+/***************************************************************************
+     QgsAttributeTableDelegate.h
+     --------------------------------------
+    Date                 : Feb 2009
+    Copyright            : (C) 2009 Vita Cizek
+    Email                : weetya (at) gmail.com
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef QGSATTRIBUTETABLEDELEGATE_H
+#define QGSATTRIBUTETABLEDELEGATE_H
+
+#include <QItemDelegate>
+class QPainter;
+/** \ingroup app
+ * A delegate item class for QgsAttributeTable (see Qt documentation for
+ * QItemDelegate).
+ */
+
+class QgsAttributeTableDelegate : public QItemDelegate
+{
+  Q_OBJECT;
+  public:
+    /** Constructor */
+    QgsAttributeTableDelegate( QObject* parent = NULL ) : 
+      QItemDelegate( parent ) {};
+    /** Used to create an editor for when the user tries to 
+     * change the contents of a cell */
+    QWidget * createEditor( 
+        QWidget *parent, 
+        const QStyleOptionViewItem &option, 
+        const QModelIndex &index ) const;
+    /** Overloads the paint method form the QItemDelegate bas class */
+    void paint( 
+        QPainter * painter, 
+        const QStyleOptionViewItem & option, 
+        const QModelIndex & index ) const;
+};
+
+#endif //QGSATTRIBUTETABLEDELEGATE_H

Added: trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.cpp	                        (rev 0)
+++ trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.cpp	2009-05-04 14:26:03 UTC (rev 10726)
@@ -0,0 +1,56 @@
+/***************************************************************************
+     QgsAttributeTableFilterModel.cpp
+     --------------------------------------
+    Date                 : Feb 2009
+    Copyright            : (C) 2009 Vita Cizek
+    Email                : weetya (at) gmail.com
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "qgsattributetablefiltermodel.h"
+#include "qgsattributetablemodel.h"
+#include "qgsvectorlayer.h"
+
+//////////////////
+// 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;
+}
+*/
+

Added: trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.h	                        (rev 0)
+++ trunk/qgis/src/app/attributetable/qgsattributetablefiltermodel.h	2009-05-04 14:26:03 UTC (rev 10726)
@@ -0,0 +1,40 @@
+/***************************************************************************
+  QgsAttributeTableFilterModel.h - Filter Model for attribute table
+  -------------------
+         date                 : Feb 2009
+         copyright            : Vita Cizek
+         email                : weetya (at) gmail.com
+
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef QGSATTRIBUTETABLEFILTERMODEL_H
+#define QGSATTRIBUTETABLEFILTERMODEL_H
+
+#include <QSortFilterProxyModel>
+#include <QModelIndex>
+
+//QGIS Includes
+#include "qgsvectorlayer.h" //QgsAttributeList
+
+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;
+};
+
+#endif

Added: trunk/qgis/src/app/attributetable/qgsattributetableidcolumnpair.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetableidcolumnpair.cpp	                        (rev 0)
+++ trunk/qgis/src/app/attributetable/qgsattributetableidcolumnpair.cpp	2009-05-04 14:26:03 UTC (rev 10726)
@@ -0,0 +1,35 @@
+/***************************************************************************
+     QgsAttributeTableIdColumnPair.cpp
+     --------------------------------------
+    Date                 : Feb 2009
+    Copyright            : (C) 2009 Vita Cizek
+    Email                : weetya (at) gmail.com
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "qgsattributetableidcolumnpair.h"
+#include "qgsfield.h"
+
+#include <QVariant>
+
+//could be faster when type guessed before sorting
+bool QgsAttributeTableIdColumnPair::operator<( const QgsAttributeTableIdColumnPair &b ) const
+{
+  //QVariant 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();
+}
+

Added: trunk/qgis/src/app/attributetable/qgsattributetableidcolumnpair.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetableidcolumnpair.h	                        (rev 0)
+++ trunk/qgis/src/app/attributetable/qgsattributetableidcolumnpair.h	2009-05-04 14:26:03 UTC (rev 10726)
@@ -0,0 +1,31 @@
+/***************************************************************************
+  QgsAttributeTableIdColumnPair.h - Helper class for attribute tables
+  -------------------
+         date                 : Feb 2009
+         copyright            : Vita Cizek
+         email                : weetya (at) gmail.com
+
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef QGSATTRIBUTETABKEIDCOLUMNPAIR_H
+#define QGSATTRIBUTETABKEIDCOLUMNPAIR_H
+
+#include <QVariant>
+
+class QgsAttributeTableIdColumnPair
+{
+  public:
+    int id;
+    QVariant columnItem;
+
+    bool operator<( const QgsAttributeTableIdColumnPair &b ) const;
+};
+
+#endif

Added: trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.cpp	                        (rev 0)
+++ trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.cpp	2009-05-04 14:26:03 UTC (rev 10726)
@@ -0,0 +1,177 @@
+/***************************************************************************
+     QgsAttributeTableMemoryModel.cpp
+     --------------------------------------
+    Date                 : Feb 2009
+    Copyright            : (C) 2009 Vita Cizek
+    Email                : weetya (at) gmail.com
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "qgsattributetablememorymodel.h"
+#include "qgsattributetablefiltermodel.h"
+
+#include "qgsfield.h"
+#include "qgsvectorlayer.h"
+#include "qgslogger.h"
+
+#include <QtGui>
+#include <QVariant>
+
+/////////////////////
+// In-Memory model //
+/////////////////////
+
+void QgsAttributeTableMemoryModel::loadLayer()
+{
+  QgsAttributeTableModel::loadLayer();
+  mLayer->select( mLayer->pendingAllAttributesList(), QgsRectangle(), false );
+
+  QgsFeature f;
+  while ( mLayer->nextFeature( f ) )
+    mFeatureMap.insert( f.id(), f );
+}
+
+QgsAttributeTableMemoryModel::QgsAttributeTableMemoryModel
+( QgsVectorLayer *theLayer )
+    : QgsAttributeTableModel( theLayer )
+{
+  loadLayer();
+}
+
+QVariant QgsAttributeTableMemoryModel::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 QgsAttributeTableMemoryModel::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 QgsAttributeTableMemoryModel::featureDeleted( int fid )
+{
+  QgsDebugMsg( "entered." );
+  mFeatureMap.remove( fid );
+  QgsAttributeTableModel::featureDeleted( fid );
+}
+
+void QgsAttributeTableMemoryModel::featureAdded( int fid )
+{
+  QgsDebugMsg( "entered." );
+  QgsFeature f;
+  mLayer->featureAtId( fid, f, false, true );
+  mFeatureMap.insert( fid, f );
+  QgsAttributeTableModel::featureAdded( fid );
+}
+
+#if 0
+void QgsAttributeTableMemoryModel::attributeAdded( int idx )
+{
+  QgsDebugMsg( "entered." );
+  loadLayer();
+  reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
+}
+
+void QgsAttributeTableMemoryModel::attributeDeleted( int idx )
+{
+  QgsDebugMsg( "entered." );
+  loadLayer();
+  reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
+}
+#endif
+
+void QgsAttributeTableMemoryModel::layerDeleted()
+{
+  QgsDebugMsg( "entered." );
+  mFeatureMap.clear();
+  QgsAttributeTableModel::layerDeleted();
+}
+
+void QgsAttributeTableMemoryModel::attributeValueChanged( int fid, int idx, const QVariant &value )
+{
+  QgsDebugMsg( "entered." );
+  mFeatureMap[fid].changeAttribute( idx, value );
+  reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
+}

Added: trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.h	                        (rev 0)
+++ trunk/qgis/src/app/attributetable/qgsattributetablememorymodel.h	2009-05-04 14:26:03 UTC (rev 10726)
@@ -0,0 +1,56 @@
+/***************************************************************************
+  QgsAttributeTableMemoryModel.h - Memory Model for attribute table
+  -------------------
+         date                 : Feb 2009
+         copyright            : Vita Cizek
+         email                : weetya (at) gmail.com
+
+ ***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef QGSATTRIBUTETABLEMEMORYMODEL_H
+#define QGSATTRIBUTETABLEMEMORYMODEL_H
+
+#include <QAbstractTableModel>
+#include <QModelIndex>
+#include <QObject>
+
+//QGIS Includes
+#include "qgsfeature.h" //QgsAttributeMap
+#include "qgsvectorlayer.h" //QgsAttributeList
+#include "qgsattributetablemodel.h" 
+#include "qgsattributetableidcolumnpair.h" 
+
+class QgsAttributeTableMemoryModel: public QgsAttributeTableModel
+{
+    Q_OBJECT;
+
+  public:
+    QgsAttributeTableMemoryModel( 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 //QGSATTRIBUTETABLEMEMORYMODEL_H



More information about the QGIS-commit mailing list