| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /*************************************************************************** | ||
| test_template.cpp | ||
| -------------------------------------- | ||
| Date : Sun Sep 16 12:22:23 AKDT 2007 | ||
| Copyright : (C) 2007 by Gary E. Sherman | ||
| Email : sherman at mrcc dot 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 <QtTest> | ||
| #include <QDomDocument> | ||
| #include <QFile> | ||
| //header for class being tested | ||
| #include <qgsrulebasedrendererv2.h> | ||
|
|
||
| #if QT_VERSION < 0x40701 | ||
| // See http://hub.qgis.org/issues/4284 | ||
| Q_DECLARE_METATYPE( QVariant ) | ||
| #endif | ||
|
|
||
|
|
||
| class TestQgsRuleBasedRenderer: public QObject | ||
| { | ||
| Q_OBJECT | ||
| private slots: | ||
|
|
||
| void test_load_xml() | ||
| { | ||
| QDomDocument doc; | ||
| xml2domElement( "rulebasedrenderer_simple.xml", doc ); | ||
| QDomElement elem = doc.documentElement(); | ||
|
|
||
| QgsRuleBasedRendererV2* r = static_cast<QgsRuleBasedRendererV2*>( QgsRuleBasedRendererV2::create( elem ) ); | ||
| QVERIFY( r ); | ||
| check_tree_valid( r->rootRule() ); | ||
| delete r; | ||
| } | ||
|
|
||
| void test_load_invalid_xml() | ||
| { | ||
| QDomDocument doc; | ||
| xml2domElement( "rulebasedrenderer_invalid.xml", doc ); | ||
| QDomElement elem = doc.documentElement(); | ||
|
|
||
| QgsRuleBasedRendererV2* r = static_cast<QgsRuleBasedRendererV2*>( QgsRuleBasedRendererV2::create( elem ) ); | ||
| QVERIFY( r == NULL ); | ||
| } | ||
|
|
||
| private: | ||
| void xml2domElement( QString testFile, QDomDocument& doc ) | ||
| { | ||
| QString fileName = QString( TEST_DATA_DIR ) + QDir::separator() + testFile; | ||
| QFile f(fileName); | ||
| bool fileOpen = f.open(QIODevice::ReadOnly); | ||
| QVERIFY( fileOpen ); | ||
|
|
||
| QString msg; | ||
| int line, col; | ||
| bool parse = doc.setContent( &f, &msg, &line, &col ); | ||
| QVERIFY( parse ); | ||
| } | ||
|
|
||
| void check_tree_valid( QgsRuleBasedRendererV2::Rule* root ) | ||
| { | ||
| // root must always exist (although it does not have children) | ||
| QVERIFY( root ); | ||
| // and does not have a parent | ||
| QVERIFY( root->parent() == NULL ); | ||
|
|
||
| foreach ( QgsRuleBasedRendererV2::Rule* node, root->children() ) | ||
| check_non_root_rule( node ); | ||
| } | ||
|
|
||
| void check_non_root_rule( QgsRuleBasedRendererV2::Rule* node ) | ||
| { | ||
| qDebug() << node->dump(); | ||
| // children must not be NULL | ||
| QVERIFY( node ); | ||
| // and must have a parent | ||
| QVERIFY( node->parent() ); | ||
| // check that all children are okay | ||
| foreach ( QgsRuleBasedRendererV2::Rule* child, node->children() ) | ||
| check_non_root_rule( child ); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| QTEST_MAIN( TestQgsRuleBasedRenderer ) | ||
|
|
||
| #include "moc_testqgsrulebasedrenderer.cxx" | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #include "testrendererv2gui.h" | ||
|
|
||
| #include <qgsapplication.h> | ||
| #include <qgsmapcanvas.h> | ||
| #include <qgsvectorlayer.h> | ||
| #include <qgsmaplayerregistry.h> | ||
| #include <qgsproject.h> | ||
| #include <qgsrendererv2propertiesdialog.h> | ||
| #include <qgsstylev2.h> | ||
|
|
||
| #include <QApplication> | ||
| #include <QToolBar> | ||
|
|
||
| TestRendererV2GUI::TestRendererV2GUI(QWidget *parent) : | ||
| QMainWindow(parent) | ||
| { | ||
| resize(640,480); | ||
|
|
||
| QToolBar* toolBar = addToolBar("Actions"); | ||
| toolBar->addAction( "set renderer", this, SLOT(setRenderer()) ); | ||
|
|
||
| mMapCanvas = new QgsMapCanvas(this); | ||
| mMapCanvas->setCanvasColor( Qt::white ); | ||
| setCentralWidget(mMapCanvas); | ||
|
|
||
| connect( QgsProject::instance(), SIGNAL(readProject(QDomDocument)), mMapCanvas, SLOT(readProject(QDomDocument))); | ||
| } | ||
|
|
||
| void TestRendererV2GUI::loadLayers() | ||
| { | ||
| // load just first vector layer | ||
| QList<QgsMapCanvasLayer> canvasLayers; | ||
| foreach (QgsMapLayer* layer, QgsMapLayerRegistry::instance()->mapLayers().values()) | ||
| { | ||
| if ( layer->type() == QgsMapLayer::VectorLayer ) | ||
| canvasLayers << QgsMapCanvasLayer( layer ); | ||
| } | ||
|
|
||
| mMapCanvas->setLayerSet(canvasLayers); | ||
| } | ||
|
|
||
| void TestRendererV2GUI::setRenderer() | ||
| { | ||
| QgsMapLayer* layer = mMapCanvas->layer(0); | ||
| Q_ASSERT( layer ); | ||
| Q_ASSERT( layer->type() == QgsMapLayer::VectorLayer ); | ||
| QgsVectorLayer* vlayer = static_cast<QgsVectorLayer*>(layer); | ||
|
|
||
| QgsRendererV2PropertiesDialog dlg( vlayer, QgsStyleV2::defaultStyle() ); | ||
| dlg.exec(); | ||
|
|
||
| mMapCanvas->refresh(); | ||
| } | ||
|
|
||
| int main(int argc, char* argv[]) | ||
| { | ||
| QApplication app(argc, argv); | ||
|
|
||
| if ( argc < 2 ) | ||
| { | ||
| qDebug( "Provide a project file name with at least one vector layer!" ); | ||
| return 1; | ||
| } | ||
|
|
||
| QgsApplication::init(); | ||
| QgsApplication::initQgis(); | ||
|
|
||
| TestRendererV2GUI gui; | ||
|
|
||
| QString projectFileName( argv[1] ); | ||
| QgsProject::instance()->setFileName( projectFileName ); | ||
| bool res = QgsProject::instance()->read(); | ||
| if ( !res ) | ||
| { | ||
| qDebug("Failed to open project!"); | ||
| return 1; | ||
| } | ||
|
|
||
| // the layers are in the registry - now load them! | ||
| gui.loadLayers(); | ||
|
|
||
| gui.show(); | ||
| return app.exec(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #ifndef TESTRENDERERV2GUI_H | ||
| #define TESTRENDERERV2GUI_H | ||
|
|
||
| #include <QMainWindow> | ||
|
|
||
| class QgsMapCanvas; | ||
|
|
||
| class TestRendererV2GUI : public QMainWindow | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| explicit TestRendererV2GUI(QWidget *parent = 0); | ||
| void loadLayers(); | ||
|
|
||
| signals: | ||
|
|
||
| public slots: | ||
| void setRenderer(); | ||
|
|
||
| protected: | ||
| QgsMapCanvas* mMapCanvas; | ||
| }; | ||
|
|
||
| #endif // TESTRENDERERV2GUI_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <renderer-v2 symbollevels="0" type="RuleRenderer"> | ||
| </renderer-v2> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <renderer-v2 symbollevels="0" type="RuleRenderer"> | ||
| <rules> | ||
| <rule filter="type=3" symbol="0" label="type3"/> | ||
| <rule filter="type=1" symbol="1" label="type1"/> | ||
| </rules> | ||
| <symbols> | ||
| <symbol outputUnit="MM" alpha="1" type="line" name="0"> | ||
| <layer pass="0" class="SimpleLine" locked="0"> | ||
| <prop k="capstyle" v="square"/> | ||
| <prop k="color" v="220,0,0,255"/> | ||
| <prop k="customdash" v="5;2"/> | ||
| <prop k="joinstyle" v="bevel"/> | ||
| <prop k="offset" v="0"/> | ||
| <prop k="penstyle" v="solid"/> | ||
| <prop k="use_custom_dash" v="0"/> | ||
| <prop k="width" v="0.26"/> | ||
| </layer> | ||
| </symbol> | ||
| <symbol outputUnit="MM" alpha="1" type="line" name="1"> | ||
| <layer pass="0" class="SimpleLine" locked="0"> | ||
| <prop k="capstyle" v="square"/> | ||
| <prop k="color" v="94,116,254,255"/> | ||
| <prop k="customdash" v="5;2"/> | ||
| <prop k="joinstyle" v="bevel"/> | ||
| <prop k="offset" v="0"/> | ||
| <prop k="penstyle" v="solid"/> | ||
| <prop k="use_custom_dash" v="0"/> | ||
| <prop k="width" v="0.26"/> | ||
| </layer> | ||
| </symbol> | ||
| </symbols> | ||
| </renderer-v2> |