From 247e181ce51bf22ec4d99ce2a0d78cb1f4cb049d Mon Sep 17 00:00:00 2001 From: Thomas Gratier Date: Thu, 8 Apr 2021 19:16:46 +0200 Subject: [PATCH] Update sample 3 for QGIS 3.x --- 3_basic_labelling/CMakeLists.txt | 28 ++- .../FindGDAL.cmake | 0 3_basic_labelling/cmake/FindGEOS.cmake | 187 ++++++++++++++++++ .../FindQGIS.cmake | 0 3_basic_labelling/cmake/MacPlistMacros.cmake | 12 ++ .../cmake_find_rules/FindGEOS.cmake | 45 ----- 3_basic_labelling/main.cpp | 2 +- 3_basic_labelling/mainwindow.cpp | 104 ++++++---- 3_basic_labelling/mainwindow.h | 2 +- 9 files changed, 274 insertions(+), 106 deletions(-) rename 3_basic_labelling/{cmake_find_rules => cmake}/FindGDAL.cmake (100%) create mode 100644 3_basic_labelling/cmake/FindGEOS.cmake rename 3_basic_labelling/{cmake_find_rules => cmake}/FindQGIS.cmake (100%) create mode 100644 3_basic_labelling/cmake/MacPlistMacros.cmake delete mode 100644 3_basic_labelling/cmake_find_rules/FindGEOS.cmake diff --git a/3_basic_labelling/CMakeLists.txt b/3_basic_labelling/CMakeLists.txt index 76487a4..e9ba598 100644 --- a/3_basic_labelling/CMakeLists.txt +++ b/3_basic_labelling/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.6) PROJECT(timtut3) SET(CMAKE_COLOR_MAKEFILE ON) # set path to additional CMake modules -SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_find_rules ${CMAKE_MODULE_PATH}) +SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) FIND_PACKAGE(QGIS REQUIRED) IF (NOT QGIS_FOUND) MESSAGE (SEND_ERROR "QGIS dependency was not found!") @@ -66,16 +66,17 @@ SET (QT_USE_QTSQL TRUE) SET (QT_USE_QTSVG TRUE) SET (QT_USE_QTXML TRUE) SET (QT_USE_QTNETWORK TRUE) -FIND_PACKAGE(Qt4 REQUIRED) -INCLUDE( ${QT_USE_FILE} ) +FIND_PACKAGE(Qt5 REQUIRED COMPONENTS Core Widgets Xml) + +# INCLUDE( ${QT_USE_FILE} ) ######################################################## # Build -QT4_WRAP_UI (timtut3_UIS_H ${timtut3_UIS}) +QT5_WRAP_UI (timtut3_UIS_H ${timtut3_UIS}) -QT4_WRAP_CPP (timtut3_MOC_SRCS ${timtut3_MOC_HDRS}) +QT5_WRAP_CPP (timtut3_MOC_SRCS ${timtut3_MOC_HDRS}) -QT4_ADD_RESOURCES(timtut3_RCC_SRCS ${timtut3_RCCS}) +QT5_ADD_RESOURCES(timtut3_RCC_SRCS ${timtut3_RCCS}) ADD_EXECUTABLE (timtut3 ${timtut3_SRCS} ${timtut3_MOC_SRCS} ${timtut3_RCC_SRCS} ${timtut3_UIS_H}) @@ -88,17 +89,14 @@ INCLUDE_DIRECTORIES( . ) -#This is probably no longer needed, but I will leave it in for Win machines for the moment -IF(QT_QTSQL_FOUND) - FIND_LIBRARY(QT_QTSQL_LIBRARY NAMES QtSql QtSql4 PATHS ${QT_LIBRARY_DIR} NO_DEFAULT_PATH) - SET(QT_LIBRARIES ${QT_LIBRARIES} ${QT_QTSQL_LIBRARY}) -ENDIF(QT_QTSQL_FOUND) - TARGET_LINK_LIBRARIES(timtut3 - ${QT_LIBRARIES} + Qt5::Core + Qt5::Widgets + Qt5::Xml ${QGIS_CORE_LIBRARY} ${QGIS_GUI_LIBRARY} - ${GEOS_LIBRARY} + ${GEOS_LIBRARY} + ${GDAL_LIBRARY} ) @@ -112,5 +110,3 @@ ENDIF (MSVC) ######################################################## # Install - - diff --git a/3_basic_labelling/cmake_find_rules/FindGDAL.cmake b/3_basic_labelling/cmake/FindGDAL.cmake similarity index 100% rename from 3_basic_labelling/cmake_find_rules/FindGDAL.cmake rename to 3_basic_labelling/cmake/FindGDAL.cmake diff --git a/3_basic_labelling/cmake/FindGEOS.cmake b/3_basic_labelling/cmake/FindGEOS.cmake new file mode 100644 index 0000000..f7ef380 --- /dev/null +++ b/3_basic_labelling/cmake/FindGEOS.cmake @@ -0,0 +1,187 @@ +# Find GEOS +# ~~~~~~~~~ +# Copyright (c) 2008, Mateusz Loskot +# (based on FindGDAL.cmake by Magnus Homann) +# Redistribution and use is allowed according to the terms of the BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. +# +# CMake module to search for GEOS library +# +# If it's found it sets GEOS_FOUND to TRUE +# and following variables are set: +# GEOS_INCLUDE_DIR +# GEOS_LIBRARY +# + +INCLUDE (${CMAKE_SOURCE_DIR}/cmake/MacPlistMacros.cmake) + +IF(WIN32) + + IF (MINGW) + FIND_PATH(GEOS_INCLUDE_DIR geos_c.h "$ENV{LIB_DIR}/include" /usr/local/include /usr/include c:/msys/local/include) + FIND_LIBRARY(GEOS_LIBRARY NAMES geos_c PATHS "$ENV{LIB_DIR}/lib" /usr/local/lib /usr/lib c:/msys/local/lib) + ENDIF (MINGW) + + IF (MSVC) + FIND_PATH(GEOS_INCLUDE_DIR geos_c.h $ENV{LIB_DIR}/include $ENV{INCLUDE}) + FIND_LIBRARY(GEOS_LIBRARY NAMES geos_c_i geos_c PATHS + "$ENV{LIB_DIR}/lib" + $ENV{LIB} + ) + ENDIF (MSVC) + +ELSEIF(APPLE AND QGIS_MAC_DEPS_DIR) + + FIND_PATH(GEOS_INCLUDE_DIR geos_c.h "$ENV{LIB_DIR}/include" ) + FIND_LIBRARY(GEOS_LIBRARY NAMES geos_c PATHS "$ENV{LIB_DIR}/lib" ) + +ELSE(WIN32) + + IF(UNIX) + # try to use framework on mac + # want clean framework path, not unix compatibility path + IF (APPLE) + IF (CMAKE_FIND_FRAMEWORK MATCHES "FIRST" + OR CMAKE_FRAMEWORK_PATH MATCHES "ONLY" + OR NOT CMAKE_FIND_FRAMEWORK) + SET (CMAKE_FIND_FRAMEWORK_save ${CMAKE_FIND_FRAMEWORK} CACHE STRING "" FORCE) + SET (CMAKE_FIND_FRAMEWORK "ONLY" CACHE STRING "" FORCE) + FIND_LIBRARY(GEOS_LIBRARY GEOS) + IF (GEOS_LIBRARY) + # they're all the same in a framework + SET (GEOS_INCLUDE_DIR ${GEOS_LIBRARY}/Headers CACHE PATH "Path to a file.") + # set GEOS_CONFIG to make later test happy, not used here, may not exist + SET (GEOS_CONFIG ${GEOS_LIBRARY}/unix/bin/geos-config CACHE FILEPATH "Path to a program.") + # version in info.plist + GET_VERSION_PLIST (${GEOS_LIBRARY}/Resources/Info.plist GEOS_VERSION) + IF (NOT GEOS_VERSION) + MESSAGE (FATAL_ERROR "Could not determine GEOS version from framework.") + ENDIF (NOT GEOS_VERSION) + STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\1" GEOS_VERSION_MAJOR "${GEOS_VERSION}") + STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\2" GEOS_VERSION_MINOR "${GEOS_VERSION}") + IF (GEOS_VERSION_MAJOR LESS 3) + MESSAGE (FATAL_ERROR "GEOS version is too old (${GEOS_VERSION}). Use 3.0.0 or higher.") + ENDIF (GEOS_VERSION_MAJOR LESS 3) + ENDIF (GEOS_LIBRARY) + SET (CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK_save} CACHE STRING "" FORCE) + ENDIF () + ENDIF (APPLE) + + IF(CYGWIN) + FIND_LIBRARY(GEOS_LIBRARY NAMES geos_c PATHS /usr/lib /usr/local/lib) + ENDIF(CYGWIN) + + IF (NOT GEOS_INCLUDE_DIR OR NOT GEOS_LIBRARY OR NOT GEOS_CONFIG) + # didn't find OS X framework, and was not set by user + SET(GEOS_CONFIG_PREFER_PATH "$ENV{GEOS_HOME}/bin" CACHE STRING "preferred path to GEOS (geos-config)") + FIND_PROGRAM(GEOS_CONFIG geos-config + ${GEOS_CONFIG_PREFER_PATH} + $ENV{LIB_DIR}/bin + /usr/local/bin/ + /usr/bin/ + ) + #MESSAGE("DBG GEOS_CONFIG ${GEOS_CONFIG}") + + IF (GEOS_CONFIG) + + EXEC_PROGRAM(${GEOS_CONFIG} + ARGS --version + OUTPUT_VARIABLE GEOS_VERSION) + STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\1" GEOS_VERSION_MAJOR "${GEOS_VERSION}") + STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\2" GEOS_VERSION_MINOR "${GEOS_VERSION}") + + IF (GEOS_VERSION_MAJOR LESS 3 OR (GEOS_VERSION_MAJOR EQUAL 3 AND GEOS_VERSION_MINOR LESS 3) ) + MESSAGE (FATAL_ERROR "GEOS version is too old (${GEOS_VERSION}). Use 3.3.0 or higher.") + ENDIF (GEOS_VERSION_MAJOR LESS 3 OR (GEOS_VERSION_MAJOR EQUAL 3 AND GEOS_VERSION_MINOR LESS 3) ) + + # set INCLUDE_DIR to prefix+include + EXEC_PROGRAM(${GEOS_CONFIG} + ARGS --prefix + OUTPUT_VARIABLE GEOS_PREFIX) + + FIND_PATH(GEOS_INCLUDE_DIR + geos_c.h + ${GEOS_PREFIX}/include + /usr/local/include + /usr/include + ) + + ## extract link dirs for rpath + EXEC_PROGRAM(${GEOS_CONFIG} + ARGS --libs + OUTPUT_VARIABLE GEOS_CONFIG_LIBS ) + + ## split off the link dirs (for rpath) + ## use regular expression to match wildcard equivalent "-L*" + ## with is a space or a semicolon + STRING(REGEX MATCHALL "[-][L]([^ ;])+" + GEOS_LINK_DIRECTORIES_WITH_PREFIX + "${GEOS_CONFIG_LIBS}" ) + #MESSAGE("DBG GEOS_LINK_DIRECTORIES_WITH_PREFIX=${GEOS_LINK_DIRECTORIES_WITH_PREFIX}") + + ## remove prefix -L because we need the pure directory for LINK_DIRECTORIES + + IF (GEOS_LINK_DIRECTORIES_WITH_PREFIX) + STRING(REGEX REPLACE "[-][L]" "" GEOS_LINK_DIRECTORIES ${GEOS_LINK_DIRECTORIES_WITH_PREFIX} ) + ENDIF (GEOS_LINK_DIRECTORIES_WITH_PREFIX) + + ### XXX - mloskot: geos-config --libs does not return -lgeos_c, so set it manually + ## split off the name + ## use regular expression to match wildcard equivalent "-l*" + ## with is a space or a semicolon + #STRING(REGEX MATCHALL "[-][l]([^ ;])+" + # GEOS_LIB_NAME_WITH_PREFIX + # "${GEOS_CONFIG_LIBS}" ) + #MESSAGE("DBG GEOS_CONFIG_LIBS=${GEOS_CONFIG_LIBS}") + #MESSAGE("DBG GEOS_LIB_NAME_WITH_PREFIX=${GEOS_LIB_NAME_WITH_PREFIX}") + SET(GEOS_LIB_NAME_WITH_PREFIX -lgeos_c CACHE STRING INTERNAL) + + ## remove prefix -l because we need the pure name + + IF (GEOS_LIB_NAME_WITH_PREFIX) + STRING(REGEX REPLACE "[-][l]" "" GEOS_LIB_NAME ${GEOS_LIB_NAME_WITH_PREFIX} ) + ENDIF (GEOS_LIB_NAME_WITH_PREFIX) + #MESSAGE("DBG GEOS_LIB_NAME=${GEOS_LIB_NAME}") + + IF (APPLE) + IF (NOT GEOS_LIBRARY) + # work around empty GEOS_LIBRARY left by framework check + # while still preserving user setting if given + # ***FIXME*** need to improve framework check so below not needed + SET(GEOS_LIBRARY ${GEOS_LINK_DIRECTORIES}/lib${GEOS_LIB_NAME}.dylib CACHE STRING INTERNAL FORCE) + ENDIF (NOT GEOS_LIBRARY) + ELSE (APPLE) + FIND_LIBRARY(GEOS_LIBRARY NAMES ${GEOS_LIB_NAME} PATHS ${GEOS_LIB_DIRECTORIES}/lib) + ENDIF (APPLE) + #MESSAGE("DBG GEOS_LIBRARY=${GEOS_LIBRARY}") + + ELSE(GEOS_CONFIG) + MESSAGE("FindGEOS.cmake: geos-config not found. Please set it manually. GEOS_CONFIG=${GEOS_CONFIG}") + ENDIF(GEOS_CONFIG) + ENDIF(NOT GEOS_INCLUDE_DIR OR NOT GEOS_LIBRARY OR NOT GEOS_CONFIG) + ENDIF(UNIX) +ENDIF(WIN32) + +IF(GEOS_INCLUDE_DIR AND NOT GEOS_VERSION) + FILE(READ ${GEOS_INCLUDE_DIR}/geos_c.h VERSIONFILE) + STRING(REGEX MATCH "#define GEOS_VERSION \"[0-9]+\\.[0-9]+\\.[0-9]+" GEOS_VERSION ${VERSIONFILE}) + STRING(REGEX MATCH "[0-9]+\\.[0-9]\\.[0-9]+" GEOS_VERSION ${GEOS_VERSION}) +ENDIF(GEOS_INCLUDE_DIR AND NOT GEOS_VERSION) + +IF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) + SET(GEOS_FOUND TRUE) +ENDIF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) + +IF (GEOS_FOUND) + + IF (NOT GEOS_FIND_QUIETLY) + MESSAGE(STATUS "Found GEOS: ${GEOS_LIBRARY} (${GEOS_VERSION})") + ENDIF (NOT GEOS_FIND_QUIETLY) + +ELSE (GEOS_FOUND) + + MESSAGE(GEOS_INCLUDE_DIR=${GEOS_INCLUDE_DIR}) + MESSAGE(GEOS_LIBRARY=${GEOS_LIBRARY}) + MESSAGE(FATAL_ERROR "Could not find GEOS") + +ENDIF (GEOS_FOUND) diff --git a/3_basic_labelling/cmake_find_rules/FindQGIS.cmake b/3_basic_labelling/cmake/FindQGIS.cmake similarity index 100% rename from 3_basic_labelling/cmake_find_rules/FindQGIS.cmake rename to 3_basic_labelling/cmake/FindQGIS.cmake diff --git a/3_basic_labelling/cmake/MacPlistMacros.cmake b/3_basic_labelling/cmake/MacPlistMacros.cmake new file mode 100644 index 0000000..b123872 --- /dev/null +++ b/3_basic_labelling/cmake/MacPlistMacros.cmake @@ -0,0 +1,12 @@ +# Mac Plist Macros + +FUNCTION (GET_VERSION_PLIST PLISTFILE OUTVAR) + SET (PVERSION "") + IF (EXISTS ${PLISTFILE}) + FILE (READ "${PLISTFILE}" info_plist) + STRING (REGEX REPLACE "\n" "" info_plist "${info_plist}") + STRING (REGEX MATCH "CFBundleShortVersionString[ \t]*([0-9\\.]*)" PLISTVERSION "${info_plist}") + STRING (REGEX REPLACE "CFBundleShortVersionString[ \t]*([0-9\\.]*)" "\\1" PVERSION "${PLISTVERSION}") + ENDIF (EXISTS ${PLISTFILE}) + SET (${OUTVAR} ${PVERSION} PARENT_SCOPE) +ENDFUNCTION (GET_VERSION_PLIST) diff --git a/3_basic_labelling/cmake_find_rules/FindGEOS.cmake b/3_basic_labelling/cmake_find_rules/FindGEOS.cmake deleted file mode 100644 index 520ba36..0000000 --- a/3_basic_labelling/cmake_find_rules/FindGEOS.cmake +++ /dev/null @@ -1,45 +0,0 @@ - -# CMake module to search for GEOS library -# -# If it's found it sets GEOS_FOUND to TRUE -# and following variables are set: -# GEOS_INCLUDE_DIR -# GEOS_LIBRARY - - -FIND_PATH(GEOS_INCLUDE_DIR geos.h - /usr/local/include - /usr/include - #MSVC - "$ENV{LIB_DIR}/include" - #mingw - c:/msys/local/include - ) - -FIND_LIBRARY(GEOS_LIBRARY NAMES geos PATHS - /usr/local/lib - /usr/lib - #MSVC - "$ENV{LIB_DIR}/lib" - #mingw - c:/msys/local/lib - ) - -IF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) - SET(GEOS_FOUND TRUE) -ENDIF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) - - -IF (GEOS_FOUND) - - IF (NOT GEOS_FIND_QUIETLY) - MESSAGE(STATUS "Found GEOS: ${GEOS_LIBRARY}") - ENDIF (NOT GEOS_FIND_QUIETLY) - -ELSE (GEOS_FOUND) - - IF (GEOS_FIND_REQUIRED) - MESSAGE(FATAL_ERROR "Could not find GEOS") - ENDIF (GEOS_FIND_REQUIRED) - -ENDIF (GEOS_FOUND) diff --git a/3_basic_labelling/main.cpp b/3_basic_labelling/main.cpp index 3285180..4c46988 100644 --- a/3_basic_labelling/main.cpp +++ b/3_basic_labelling/main.cpp @@ -30,7 +30,7 @@ int main(int argc, char ** argv) { // Start the Application - QgsApplication app(argc, argv, TRUE); + QgsApplication app(argc, argv, true); MainWindow * mypMainWindow = new MainWindow(); mypMainWindow->show(); // Start the Application Event Loop diff --git a/3_basic_labelling/mainwindow.cpp b/3_basic_labelling/mainwindow.cpp index 8758274..67c03be 100644 --- a/3_basic_labelling/mainwindow.cpp +++ b/3_basic_labelling/mainwindow.cpp @@ -17,7 +17,12 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +// +//Qt related +// #include "mainwindow.h" +#include "qtoolbar.h" + // // QGIS Includes // @@ -25,9 +30,17 @@ #include #include #include -#include +#include #include #include +#include +#include +#include +#include +#include +#include +#include + // // QGIS Map tools // @@ -42,14 +55,8 @@ //#include "qgsmaptoolvertexedit.h" //#include "qgsmeasure.h" -// -//Labelling related -// -#include -#include -#include -MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) +MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags fl) : QMainWindow(parent,fl) { //required by Qt4 to initialise the ui @@ -59,15 +66,14 @@ MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) #if defined(Q_WS_MAC) QString myPluginsDir = "/Users/timsutton/apps/qgis.app/Contents/MacOS/lib/qgis"; #else - QString myPluginsDir = "/home/timlinux/apps/lib/qgis"; + QString myPluginsDir = "/usr/lib/qgis"; #endif QgsProviderRegistry::instance(myPluginsDir); // Create the Map Canvas - mpMapCanvas= new QgsMapCanvas(0, 0); + mpMapCanvas= new QgsMapCanvas(); mpMapCanvas->enableAntiAliasing(true); - mpMapCanvas->useImageToRender(false); mpMapCanvas->setCanvasColor(QColor(255, 255, 255)); mpMapCanvas->freeze(false); mpMapCanvas->setVisible(true); @@ -94,9 +100,9 @@ MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) //create the maptools mpPanTool = new QgsMapToolPan(mpMapCanvas); mpPanTool->setAction(mpActionPan); - mpZoomInTool = new QgsMapToolZoom(mpMapCanvas, FALSE); // false = in + mpZoomInTool = new QgsMapToolZoom(mpMapCanvas, false); // false = in mpZoomInTool->setAction(mpActionZoomIn); - mpZoomOutTool = new QgsMapToolZoom(mpMapCanvas, TRUE ); //true = out + mpZoomOutTool = new QgsMapToolZoom(mpMapCanvas, true ); //true = out mpZoomOutTool->setAction(mpActionZoomOut); } @@ -125,8 +131,8 @@ void MainWindow::zoomOutMode() } void MainWindow::addLayer() { - QString myLayerPath = "../data"; - QString myLayerBaseName = "test"; + QString myLayerPath = "/home/thomasg/ne_10m_admin_0_countries.shp"; + QString myLayerBaseName = "Countries"; QString myProviderName = "ogr"; QgsVectorLayer * mypLayer = new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName); @@ -142,41 +148,37 @@ void MainWindow::addLayer() } //set up a renderer for the layer - QgsSingleSymbolRenderer *mypRenderer = new QgsSingleSymbolRenderer(mypLayer->geometryType()); - QList myLayerSet; + QgsSingleSymbolRenderer *mypRenderer = new QgsSingleSymbolRenderer(QgsSymbol::defaultSymbol(mypLayer->geometryType())); + QList layers; mypLayer->setRenderer(mypRenderer); // //set up labelling for the layer // + QgsPalLayerSettings labelSettings; + labelSettings.fieldName = QStringLiteral( "NAME" ); + QgsTextFormat format; + format.setFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ).family() ); + format.setSize( 12 ); + format.setNamedStyle( QStringLiteral( "Bold" ) ); + format.setColor( QColor( 200, 0, 200 ) ); + labelSettings.setFormat( format ); //get the label instance associated with the layer - QgsLabel * mypLabel; - mypLabel = mypLayer->label(); + mypLayer->setLabeling(new QgsVectorLayerSimpleLabeling(labelSettings)); + mypLayer->setLabelsEnabled(true); //and the label attributes associated with the label - QgsLabelAttributes * mypLabelAttributes; - mypLabelAttributes = mypLabel->layerAttributes(); - //note in QGIS 1.4 and up you should use mypLabel->labelAttributes rather //get the field list associated with the layer //we'll print the names out to console for diagnostic purposes - QgsFieldMap myFields = mypLayer->dataProvider()->fields(); - for (unsigned int i = 0; i < myFields.size(); i++ ) - { - qDebug("Field Name: " + QString(myFields[i].name()).toLocal8Bit() ); - } + // QgsFieldMap myFields = mypLayer->dataProvider()->fields(); + // for (unsigned int i = 0; i < myFields.size(); i++ ) + // { + // qDebug("Field Name: " + QString(myFields[i].name()).toLocal8Bit() ); + // } //just use the last field's name in the fields list as the label field! - qDebug("set label field to " + QString(myFields[myFields.size()-1].name()).toLocal8Bit()); - mypLabel->setLabelField( QgsLabel::Text, myFields.size()-1); - //set the colour of the label text - mypLabelAttributes->setColor(Qt::black); - //create a 'halo' effect around each label so it - //can still be read on dark backgrounds - mypLabelAttributes->setBufferEnabled(true); - mypLabelAttributes->setBufferColor(Qt::yellow); - int myType = QgsLabelAttributes::PointUnits; - mypLabelAttributes->setBufferSize(1,myType); - + // qDebug("set label field to " + QString(myFields[myFields.size()-1].name()).toLocal8Bit()); + /* * Here are a bunch of other things you can set based on values on a database field * the second parameter in each case would be the field name from which the @@ -196,16 +198,32 @@ void MainWindow::addLayer() */ //lastly we enable labelling! - mypLayer->enableLabels(true); + // mypLayer->enableLabels(true); // Add the Vector Layer to the Layer Registry - QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); + QgsProject::instance()->addMapLayer(mypLayer, true); // Add the Layer to the Layer Set - myLayerSet.append(QgsMapCanvasLayer( mypLayer ) ); + layers.append( mypLayer ); + + QString url = "type=xyz&url=https://a.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&zmin=0"; + // url.append("/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857"); + // QgsCoordinateReferenceSystem *crs = new QgsCoordinateReferenceSystem("EPSG:3857"); + mpMapCanvas->setDestinationCrs(* new QgsCoordinateReferenceSystem("EPSG:3857")); + QgsRasterLayer *ras = new QgsRasterLayer(url, "OpenStreetMap", "wms"); + ras->setCrs(* new QgsCoordinateReferenceSystem("EPSG:3857")); + // if (ras->isValid()) + // { + // qDebug("Raster layer is valid"); + // } + // else + // { + // qDebug("Raster layer is NOT valid"); + // return; + // } + // layers.append( ras ); // set teh canvas to the extent of our layer mpMapCanvas->setExtent(mypLayer->extent()); // Set the Map Canvas Layer Set - mpMapCanvas->setLayerSet(myLayerSet); + mpMapCanvas->setLayers(layers); } - diff --git a/3_basic_labelling/mainwindow.h b/3_basic_labelling/mainwindow.h index 65e4d93..13d8484 100644 --- a/3_basic_labelling/mainwindow.h +++ b/3_basic_labelling/mainwindow.h @@ -37,7 +37,7 @@ class MainWindow : public QMainWindow, private Ui::MainWindowBase { Q_OBJECT; public: - MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 ); + MainWindow(QWidget* parent = 0, Qt::WindowFlags fl = 0 ); ~MainWindow(); public slots: void zoomInMode();