Skip to content

Commit

Permalink
Sort attribute table by value for numerical columns
Browse files Browse the repository at this point in the history
Fix #15318
Fix #15295
  • Loading branch information
m-kuhn committed Aug 1, 2016
1 parent 5b2fe1b commit a5a24b4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsattributetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString& expressionString )
}
else
{
QVariant sortValue = widgetFactory->representValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, f.attribute( mSortFieldIndex ) );
QVariant sortValue = widgetFactory->sortValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, f.attribute( mSortFieldIndex ) );
mSortCache.insert( f.id(), sortValue );
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/src/gui/testqgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,33 @@ void TestQgsDualView::testSort()
QModelIndex index = mDualView->tableView()->model()->index( i, 0 );
QCOMPARE( mDualView->tableView()->model()->data( index ).toString(), classes.at( i ) );
}

QStringList headings;
headings << "0"
<< "0"
<< "12"
<< "34"
<< "80"
<< "85"
<< "90"
<< "90"
<< "95"
<< "100"
<< "140"
<< "160"
<< "180"
<< "240"
<< "270"
<< "300"
<< "340";

mDualView->setSortExpression( "Heading" );

for ( int i = 0; i < headings.length(); ++i )
{
QModelIndex index = mDualView->tableView()->model()->index( i, 1 );
QCOMPARE( mDualView->tableView()->model()->data( index ).toString(), headings.at( i ) );
}
}

void TestQgsDualView::testAttributeFormSharedValueScanning()
Expand Down
31 changes: 20 additions & 11 deletions tests/src/python/test_qgsattributetablemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.gui import QgsAttributeTableModel, QgsEditorWidgetRegistry
from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, QgsVectorLayerCache
from qgis.gui import (
QgsAttributeTableModel,
QgsEditorWidgetRegistry
)
from qgis.core import (
QgsFeature,
QgsGeometry,
QgsPoint,
QgsVectorLayer,
QgsVectorLayerCache
)

from qgis.testing import (start_app,
unittest
Expand Down Expand Up @@ -51,20 +60,20 @@ def createLayer(self):
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100 * i, 2 ^ i)))
features.append(f)

assert pr.addFeatures(features)
self.assertTrue(pr.addFeatures(features))
return layer

def testLoad(self):
assert self.am.rowCount() == 10, self.am.rowCount()
assert self.am.columnCount() == 2, self.am.columnCount()
self.assertEquals(self.am.rowCount(), 10)
self.assertEquals(self.am.columnCount(), 2)

def testRemove(self):
self.layer.startEditing()
self.layer.deleteFeature(5)
assert self.am.rowCount() == 9, self.am.rowCount()
self.assertEquals(self.am.rowCount(), 9)
self.layer.setSelectedFeatures([1, 3, 6, 7])
self.layer.deleteSelectedFeatures()
assert self.am.rowCount() == 5, self.am.rowCount()
self.assertEquals(self.am.rowCount(), 5)

def testAdd(self):
self.layer.startEditing()
Expand All @@ -74,14 +83,14 @@ def testAdd(self):
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100, 200)))
self.layer.addFeature(f)

assert self.am.rowCount() == 11, self.am.rowCount()
self.assertEquals(self.am.rowCount(), 11)

def testRemoveColumns(self):
assert self.layer.startEditing()
self.assertTrue(self.layer.startEditing())

assert self.layer.deleteAttribute(1)
self.assertTrue(self.layer.deleteAttribute(1))

assert self.am.columnCount() == 1, self.am.columnCount()
self.assertEquals(self.am.columnCount(), 1)

if __name__ == '__main__':
unittest.main()

0 comments on commit a5a24b4

Please sign in to comment.