|
| 1 | +/*************************************************************************** |
| 2 | + testqgsdualview.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : 14.2.2013 |
| 5 | + Copyright : (C) 2013 Matthias Kuhn |
| 6 | + Email : matthias dot kuhn at gmx dot ch |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | + |
| 17 | +#include <QtTest> |
| 18 | + |
| 19 | +#include <attributetable/qgsattributetableview.h> |
| 20 | +#include <attributetable/qgsdualview.h> |
| 21 | +#include <qgsapplication.h> |
| 22 | +#include <qgsvectorlayer.h> |
| 23 | +#include <qgsmapcanvas.h> |
| 24 | + |
| 25 | +class TestQgsDualView: public QObject |
| 26 | +{ |
| 27 | + Q_OBJECT; |
| 28 | + private slots: |
| 29 | + void initTestCase(); // will be called before the first testfunction is executed. |
| 30 | + void cleanupTestCase(); // will be called after the last testfunction was executed. |
| 31 | + void init(); // will be called before each testfunction is executed. |
| 32 | + void cleanup(); // will be called after every testfunction. |
| 33 | + |
| 34 | + void testSelection(); |
| 35 | + |
| 36 | + private: |
| 37 | + QgsMapCanvas* mCanvas; |
| 38 | + QgsVectorLayer* mPointsLayer; |
| 39 | + QString mTestDataDir; |
| 40 | + QgsDualView* mDualView; |
| 41 | +}; |
| 42 | + |
| 43 | +void TestQgsDualView::initTestCase() |
| 44 | +{ |
| 45 | + QgsApplication::init(); |
| 46 | + QgsApplication::initQgis(); |
| 47 | + QgsApplication::showSettings(); |
| 48 | + |
| 49 | + // Setup a map canvas with a vector layer loaded... |
| 50 | + QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt |
| 51 | + mTestDataDir = myDataDir + QDir::separator(); |
| 52 | + |
| 53 | + // |
| 54 | + // load a vector layer |
| 55 | + // |
| 56 | + QString myPointsFileName = mTestDataDir + "points.shp"; |
| 57 | + QFileInfo myPointFileInfo( myPointsFileName ); |
| 58 | + mPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(), |
| 59 | + myPointFileInfo.completeBaseName(), "ogr" ); |
| 60 | + |
| 61 | + mCanvas = new QgsMapCanvas(); |
| 62 | +} |
| 63 | + |
| 64 | +void TestQgsDualView::cleanupTestCase() |
| 65 | +{ |
| 66 | + delete mPointsLayer; |
| 67 | + delete mCanvas; |
| 68 | +} |
| 69 | + |
| 70 | +void TestQgsDualView::init() |
| 71 | +{ |
| 72 | + mDualView = new QgsDualView(); |
| 73 | + mDualView->init( mPointsLayer, mCanvas, QgsDistanceArea() ); |
| 74 | +} |
| 75 | + |
| 76 | +void TestQgsDualView::cleanup() |
| 77 | +{ |
| 78 | + delete mDualView; |
| 79 | +} |
| 80 | + |
| 81 | +void TestQgsDualView::testSelection() |
| 82 | +{ |
| 83 | + // Select some features on the map canvas |
| 84 | + QgsFeatureIds selectedIds; |
| 85 | + selectedIds << 1 << 2 << 3; |
| 86 | + |
| 87 | + mPointsLayer->setSelectedFeatures( selectedIds ); |
| 88 | + |
| 89 | + // Verify the same selection applies to the table |
| 90 | + QVERIFY( mDualView->mFilterModel->masterSelection()->selectedIndexes().count() == 3 ); |
| 91 | + QVERIFY( mDualView->mTableView->selectionModel()->selectedRows().count() == 3 ); |
| 92 | + |
| 93 | + // Set the extent, so all features are visible |
| 94 | + mCanvas->setExtent( QgsRectangle( -139, 22, -64, 48 ) ); |
| 95 | + // The table should also only show visible items (still all) |
| 96 | + mDualView->setFilterMode( QgsAttributeTableFilterModel::ShowVisible ); |
| 97 | + QVERIFY( mDualView->mFilterModel->masterSelection()->selectedIndexes().count() == 3 ); |
| 98 | + QVERIFY( mDualView->mTableView->selectionModel()->selectedRows().count() == 3 ); |
| 99 | + |
| 100 | + // Set the extent, so no features are visible |
| 101 | + mCanvas->setExtent( QgsRectangle( 0, 1, 0, 1 ) ); |
| 102 | + |
| 103 | + // The master selection should still hold all the features, while the currently visible should not |
| 104 | + QVERIFY( mDualView->mFilterModel->masterSelection()->selectedIndexes().count() == 3 ); |
| 105 | + QVERIFY( mDualView->mTableView->selectionModel()->selectedRows().count() == 0 ); |
| 106 | + |
| 107 | + // Only show parts of the canvas, so only one selected feature is visible |
| 108 | + mCanvas->setExtent( QgsRectangle( -139, 22, -100, 48 ) ); |
| 109 | + QVERIFY( mDualView->mFilterModel->masterSelection()->selectedIndexes().count() == 3 ); |
| 110 | + QVERIFY( mDualView->mTableView->selectionModel()->selectedRows().count() == 1 ); |
| 111 | + QVERIFY( mDualView->mTableView->selectionModel()->selectedRows().first().row() == 1 ); |
| 112 | + |
| 113 | + // Now the other way round... |
| 114 | + // TODO: Fixme |
| 115 | + mDualView->mTableView->selectAll(); |
| 116 | + QVERIFY( mPointsLayer->selectedFeaturesIds().count() == 12 ); |
| 117 | + |
| 118 | + // Deselect a previously selected row |
| 119 | + // List index 2 => fid 2 |
| 120 | + QVERIFY( mPointsLayer->selectedFeaturesIds().contains( 2 ) ); |
| 121 | + mDualView->mTableView->selectRow( 2 ); |
| 122 | + QVERIFY( false == mPointsLayer->selectedFeaturesIds().contains( 2 ) ); |
| 123 | + |
| 124 | + // Select a previously not selected row |
| 125 | + // List index 6 => fid 13 |
| 126 | + QVERIFY( false == mPointsLayer->selectedFeaturesIds().contains( 13 ) ); |
| 127 | + mDualView->mTableView->selectRow( 6 ); |
| 128 | + QVERIFY( mPointsLayer->selectedFeaturesIds().contains( 13 ) ); |
| 129 | +} |
| 130 | + |
| 131 | +QTEST_MAIN( TestQgsDualView ) |
| 132 | +#include "moc_testqgsdualview.cxx" |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
0 commit comments