⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qgsgrassplugin.cpp

📁 一个非常好的GIS开源新版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************    qgsgrassplugin.cpp  -  GRASS menu                             -------------------    begin                : March, 2004    copyright            : (C) 2004 by Radim Blazek    email                : blazek@itc.it ***************************************************************************//*************************************************************************** *                                                                         * *   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.                                   * *                                                                         * ***************************************************************************//*  $Id: qgsgrassplugin.cpp 8350 2008-04-14 21:02:22Z jef $ */// includes#include "qgsmaplayer.h"#include "qgisinterface.h"#include "qgsmapcanvas.h"#include "qgsmaplayer.h"#include "qgsvectorlayer.h"#include "qgsdataprovider.h"#include "qgsproviderregistry.h"#include <qgsrasterlayer.h>#include <qtoolbar.h>#include <qmenubar.h>#include <qmessagebox.h>#include <q3popupmenu.h>#include <qlineedit.h>#include <qaction.h>#include <qapplication.h>#include <qcursor.h>#include <QFileDialog>#include <qfileinfo.h>#include <qsettings.h>#include <qregexp.h>#include <qglobal.h>#include <qinputdialog.h>//Added by qt3to4:#include <QPixmap>#include <Q3PointArray>//non qt includes#include <iostream>#include <qgsproject.h>#include <qgsrubberband.h>extern "C" {#include <grass/gis.h>#include <grass/Vect.h>}#include "qgsgrassplugin.h"#include "../../src/providers/grass/qgsgrass.h"#include "../../src/providers/grass/qgsgrassprovider.h"//the gui subclass#include "qgsgrassutils.h"#include "qgsgrassattributes.h"#include "qgsgrassselect.h"#include "qgsgrassedit.h"#include "qgsgrasstools.h"#include "qgsgrassregion.h"#include "qgsgrassnewmapset.h"static const QString pluginVersion = QObject::tr("Version 0.1");/** * Constructor for the plugin. The plugin is passed a pointer to the main app * and an interface object that provides access to exposed functions in QGIS. * @param theQGisApp Pointer to the QGIS main window * @param theQgisInterFace Pointer to the QGIS interface object */QgsGrassPlugin::QgsGrassPlugin(QgisInterface * theQgisInterFace):  qGisInterface(theQgisInterFace){  /** Initialize the plugin and set the required attributes */  pluginNameQString = tr("GrassVector");  pluginVersionQString = tr("0.1");  pluginDescriptionQString = tr("GRASS layer");}QgsGrassPlugin::~QgsGrassPlugin(){  if ( mTools ) mTools->closeTools();  QString err = QgsGrass::closeMapset();}/* Following functions return name, description, version, and type for the plugin */QString QgsGrassPlugin::name(){  return pluginNameQString;}QString QgsGrassPlugin::version(){  return pluginVersionQString;}QString QgsGrassPlugin::description(){  return pluginDescriptionQString;}void QgsGrassPlugin::help(){  //TODO}int QgsGrassPlugin::type(){  return QgisPlugin::UI;}/* * Initialize the GUI interface for the plugin  */void QgsGrassPlugin::initGui(){  toolBarPointer = 0;  mTools = 0;  mNewMapset = 0;  mRegion = 0;   QSettings settings;  QgsGrass::init();  mCanvas = qGisInterface->getMapCanvas();  QWidget* qgis = qGisInterface->getMainWindow();  // Connect project  connect( qgis, SIGNAL( projectRead() ), this, SLOT( projectRead()));  connect( qgis, SIGNAL( newProject() ), this, SLOT(newProject()));  // Create region rubber band  mRegionBand = new QgsRubberBand(mCanvas, 1);  mRegionBand->setZValue(20);  // Create the action for tool  mOpenMapsetAction = new QAction(QIcon(":/grass/grass_open_mapset.png"), tr("Open mapset"), this );  mNewMapsetAction = new QAction(QIcon(":/grass/grass_new_mapset.png"), tr("New mapset"), this );  mCloseMapsetAction = new QAction(QIcon(":/grass/grass_close_mapset.png"), tr("Close mapset"), this );  mAddVectorAction = new QAction(QIcon(":/grass/add_vector.png"),    tr("Add GRASS vector layer"), this);  mAddRasterAction = new QAction(QIcon(":/grass/add_raster.png"),    tr("Add GRASS raster layer"), this);  mOpenToolsAction = new QAction(QIcon(":/grass/grass_tools.png"),    tr("Open GRASS tools"), this);  mRegionAction = new QAction(QIcon(":/grass/grass_region.png"),    tr("Display Current Grass Region"), this);  mRegionAction->setCheckable(true);       mEditRegionAction = new QAction(QIcon(":/grass/grass_region_edit.png"),    tr("Edit Current Grass Region"), this);  mEditAction = new QAction(QIcon(":/grass/grass_edit.png"),    tr("Edit Grass Vector layer"), this);  mNewVectorAction = new QAction(QIcon(":/grass/grass_new_vector_layer.png"),tr("Create new Grass Vector"), this);  mAddVectorAction->setWhatsThis(tr("Adds a GRASS vector layer to the map canvas"));  mAddRasterAction->setWhatsThis(tr("Adds a GRASS raster layer to the map canvas"));  mOpenToolsAction->setWhatsThis(tr("Open GRASS tools"));  mRegionAction->setWhatsThis(tr("Displays the current GRASS region as a rectangle on the map canvas"));  mEditRegionAction->setWhatsThis(tr("Edit the current GRASS region"));  mEditAction->setWhatsThis(tr("Edit the currently selected GRASS vector layer."));  // Connect the action   connect(mAddVectorAction, SIGNAL(triggered()), this, SLOT(addVector()));  connect(mAddRasterAction, SIGNAL(triggered()), this, SLOT(addRaster()));  connect(mOpenToolsAction, SIGNAL(triggered()), this, SLOT(openTools()));  connect(mEditAction, SIGNAL(triggered()), this, SLOT(edit()));  connect(mNewVectorAction, SIGNAL(triggered()), this, SLOT(newVector()));  connect(mRegionAction, SIGNAL(toggled(bool)), this, SLOT(switchRegion(bool)));  connect(mEditRegionAction, SIGNAL(triggered()), this, SLOT(changeRegion()));  connect(mOpenMapsetAction, SIGNAL(triggered()), this, SLOT(openMapset()));  connect(mNewMapsetAction, SIGNAL(triggered()), this, SLOT(newMapset()));  connect(mCloseMapsetAction, SIGNAL(triggered()), this, SLOT(closeMapset()));  // Add actions to a GRASS plugin menu  qGisInterface->addPluginMenu(tr("&GRASS"), mOpenMapsetAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mNewMapsetAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mCloseMapsetAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mAddVectorAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mAddRasterAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mNewVectorAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mEditAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mOpenToolsAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mRegionAction);  qGisInterface->addPluginMenu(tr("&GRASS"), mEditRegionAction);  // Add the toolbar to the main window  toolBarPointer = qGisInterface->addToolBar(tr("GRASS"));   toolBarPointer->setIconSize(QSize(24,24));  toolBarPointer->setObjectName("GRASS");  // Add to the toolbar  mOpenMapsetAction->addTo(toolBarPointer);  mNewMapsetAction->addTo(toolBarPointer);  mCloseMapsetAction->addTo(toolBarPointer);  toolBarPointer->addSeparator();  mAddVectorAction->addTo(toolBarPointer);  mAddRasterAction->addTo(toolBarPointer);  mNewVectorAction->addTo(toolBarPointer);  mEditAction->addTo(toolBarPointer);  mOpenToolsAction->addTo(toolBarPointer);  mRegionAction->addTo(toolBarPointer);  mEditRegionAction->addTo(toolBarPointer);  // Connect display region  connect( mCanvas, SIGNAL(renderComplete(QPainter *)), this, SLOT(postRender(QPainter *)));  setEditAction();  connect ( qGisInterface, SIGNAL(currentLayerChanged(QgsMapLayer *)),    this, SLOT(setEditAction()) );  // Init Region symbology  mRegionPen.setColor( QColor ( settings.readEntry ("/GRASS/region/color", "#ff0000" ) ) );  mRegionPen.setWidth( settings.readNumEntry ("/GRASS/region/width", 0 ) );  mRegionBand->setColor ( mRegionPen.color() );  mRegionBand->setWidth ( mRegionPen.width() );  mapsetChanged();}void QgsGrassPlugin::mapsetChanged (){  if ( !QgsGrass::activeMode() )  {    mOpenToolsAction->setEnabled(false);    mRegionAction->setEnabled(false);    mEditRegionAction->setEnabled(false);    mRegionBand->reset();    mCloseMapsetAction->setEnabled(false);    mNewVectorAction->setEnabled(false);    if ( mTools )     {      mTools->hide();      delete mTools;      mTools = 0;    }      } else {    mOpenToolsAction->setEnabled(true);    mRegionAction->setEnabled(true);    mEditRegionAction->setEnabled(true);    mCloseMapsetAction->setEnabled(true);    mNewVectorAction->setEnabled(true);    QSettings settings;    bool on = settings.readBoolEntry ("/GRASS/region/on", true );    mRegionAction->setOn(on);    switchRegion(on);    if ( mTools )     {      mTools->mapsetChanged();    }      }}void QgsGrassPlugin::saveMapset(){#ifdef QGISDEBUG  std::cerr << "QgsGrassPlugin::addVector()" << std::endl;#endif  // Save working mapset in project file  QgsProject::instance()->writeEntry("GRASS","/WorkingGisdbase",     QgsGrass::getDefaultGisdbase() );  QgsProject::instance()->writeEntry("GRASS","/WorkingLocation",     QgsGrass::getDefaultLocation() );  QgsProject::instance()->writeEntry("GRASS","/WorkingMapset",     QgsGrass::getDefaultMapset() );}// Slot called when the "Add GRASS vector layer" menu item is triggeredvoid QgsGrassPlugin::addVector(){#ifdef QGISDEBUG  std::cerr << "QgsGrassPlugin::addVector()" << std::endl;#endif  QString uri;  QgsGrassSelect *sel = new QgsGrassSelect(QgsGrassSelect::VECTOR );  if ( sel->exec() )  {    uri = sel->gisdbase + "/" + sel->location + "/" + sel->mapset + "/" + sel->map + "/" + sel->layer;  }#ifdef QGISDEBUG  std::cerr << "plugin URI: " << uri.toLocal8Bit().data() << std::endl;#endif  if ( uri.length() == 0 )  {    std::cerr << "Nothing was selected" << std::endl;    return;  } else {#ifdef QGISDEBUG    std::cout << "Add new vector layer" << std::endl;#endif    // create vector name: vector layer    QString name = sel->map;    QString field;     QString type;     QRegExp rx ( "(\\d+)_(.+)" );    if ( rx.search ( sel->layer ) != -1 )     {      field = rx.cap(1);      type = rx.cap(2);    }    // Set location    QgsGrass::setLocation ( sel->gisdbase, sel->location );    /* Open vector */    QgsGrass::resetError();    Vect_set_open_level (2);    struct Map_info map;    int level = Vect_open_old_head (&map, (char *) sel->map.ascii(),      (char *) sel->mapset.ascii());    if ( QgsGrass::getError() != QgsGrass::FATAL )    {      if ( level >= 2 )       {        // Count layers        int cnt = 0;         int ncidx = Vect_cidx_get_num_fields ( &map );        for ( int i = 0; i < ncidx; i++ )         {          int field = Vect_cidx_get_field_number ( &map, i);          if ( Vect_cidx_get_type_count( &map, field, GV_POINT|GV_LINE|GV_AREA) > 0 ||            (field > 1 && Vect_cidx_get_type_count( &map, field, GV_BOUNDARY) ) )          {            cnt++;          }        }        if( cnt > 1 )         {          name.append ( " " + field );          // No need to ad type, the type is obvious from the legend         }      }      Vect_close ( &map );    } else {      std::cerr << "Cannot open GRASS vector: " << QgsGrass::getErrorMessage().toLocal8Bit().data() << std::endl;    }    qGisInterface->addVectorLayer( uri, name, "grass");  }}// Slot called when the "Add GRASS raster layer" menu item is triggeredvoid QgsGrassPlugin::addRaster(){#ifdef QGISDEBUG  std::cerr << "QgsGrassPlugin::addRaster()" << std::endl;#endif  QString uri;  std::cerr << "QgsGrassPlugin::addRaster" << std::endl;  QgsGrassSelect *sel = new QgsGrassSelect(QgsGrassSelect::RASTER );  if ( sel->exec() ) {    QString element;    if ( sel->selectedType == QgsGrassSelect::RASTER ) {      element = "cellhd";    } else { // GROUP      element = "group";    }    uri = sel->gisdbase + "/" + sel->location + "/" + sel->mapset + "/" + element + "/" + sel->map;  }#ifdef QGISDEBUG  std::cerr << "plugin URI: " << uri.toLocal8Bit().data() << std::endl;#endif  if ( uri.length() == 0 ) {    std::cerr << "Nothing was selected" << std::endl;    return;  } else {#ifdef QGISDEBUG    std::cout << "Add new raster layer" << std::endl;#endif    // create raster name    int pos = uri.findRev('/');    pos = uri.findRev('/', pos-1);    QString name = uri.right( uri.length() - pos - 1 );    name.replace('/', ' ');    qGisInterface->addRasterLayer(uri, sel->map);  }}// Open toolsvoid QgsGrassPlugin::openTools(){  if ( !mTools ) {     mTools = new QgsGrassTools ( qGisInterface, qGisInterface->getMainWindow(), 0, Qt::WType_Dialog );    std::cout << "connect = " <<      connect( mTools, SIGNAL( regionChanged() ), this, SLOT( redrawRegion()) )      << "connect" << std::endl;  }  mTools->show();}// Start vector editingvoid QgsGrassPlugin::edit(){  if ( QgsGrassEdit::isRunning() ) {    QMessageBox::warning( 0, tr("Warning"), tr("GRASS Edit is already running.") );    return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -