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

📄 qgsgrassnewmapset.cpp

📁 一个非常好的GIS开源新版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/***************************************************************************    qgsgrassnewmapset.cpp  - New GRASS mapset wizard                               -------------------    begin                : October, 2005    copyright            : (C) 2005 by Radim Blazek    email                : radim.blazek@gmail.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.                                   * *                                                                         * ***************************************************************************/#include <iostream>#include <qdir.h>#include <qevent.h>#include <qfile.h>#include <QFileDialog> #include <qfileinfo.h>#include <qsettings.h>#include <q3listbox.h>#include <qstringlist.h>#include <qlabel.h>#include <QComboBox>#include <qspinbox.h>#include <qmessagebox.h>#include <qinputdialog.h>#include <qcursor.h>#include <q3listview.h>#include <q3header.h>#include <qradiobutton.h>#include <qlayout.h>#include <qpainter.h>#include <qpixmap.h>#include <qpen.h>#include <qdom.h>#include <qpushbutton.h>#include <q3textbrowser.h>#include <qapplication.h>//Added by qt3to4:#include <QKeyEvent>#include <QTextStream>#include <QGridLayout>#include <QCloseEvent>#include <Q3Wizard>#include "qgis.h"#include "qgisinterface.h"#include "qgsapplication.h"#include "qgsmapcanvas.h"#include "qgsproject.h"#include "qgsrect.h"#include "qgscoordinatetransform.h"#include "qgsspatialrefsys.h"#include "qgsprojectionselector.h"#include "../../src/providers/grass/qgsgrass.h"#include "qgsgrassnewmapset.h"// For bug in GPJ_osr_to_grass()#include "grass/version.h"// Prevents some compiler warnings from the version.h include#ifndef GRASS_VERSION_RELEASE// When using newer version of GRASS (cvs >= 26.4.2007),// these variables are #defined instead of being static variables.QString temp1(GRASS_VERSION_STRING);QString temp2(GRASS_VERSION_MAJOR);QString temp3(GRASS_VERSION_MINOR);QString temp4(GRASS_VERSION_RELEASE);#endif#if defined(WIN32)#include <windows.h>static QString getShortPath(const QString &path){  TCHAR buf[MAX_PATH];  GetShortPathName( path.ascii(), buf, MAX_PATH);  return buf;}#endifbool QgsGrassNewMapset::mRunning = false;QgsGrassNewMapset::QgsGrassNewMapset ( QgisInterface *iface,                                       QgsGrassPlugin *plugin, QWidget * parent,                                        const char * name, Qt::WFlags f ) :  Q3Wizard(parent, name, false, f),  QgsGrassNewMapsetBase ( ) {#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset()" << std::endl;#endif  setupUi(this);  mRunning = true;  mIface = iface;  mProjectionSelector = 0;  mPreviousPage = -1;  mRegionModified = false;  QString mapPath = QgsApplication::pkgDataPath() + "/grass/world.png";#ifdef QGISDEBUG  std::cerr << "mapPath = " << mapPath.ascii() << std::endl;#endif  //mPixmap = QPixmap( *(mRegionMap->pixmap()) );  mPixmap.load ( mapPath );  std::cerr << "mPixmap.isNull() = " << mPixmap.isNull() << std::endl;  mRegionsInited = false;  mPlugin = plugin;  setHelpEnabled ( page(DATABASE), false );  setHelpEnabled ( page(LOCATION), false );  setHelpEnabled ( page(PROJECTION), false );  setHelpEnabled ( page(REGION), false );  setHelpEnabled ( page(MAPSET), false );  setHelpEnabled ( page(FINISH), false );  setTitle ( page(DATABASE), tr("GRASS database") );  setTitle ( page(LOCATION), tr("GRASS location") );  setTitle ( page(PROJECTION), tr("Projection") );  setTitle ( page(REGION), tr("Default GRASS Region") );  setTitle ( page(MAPSET), tr("Mapset") );  setTitle ( page(FINISH), tr("Create New Mapset") );  setError ( mDatabaseErrorLabel, "" );  setError ( mLocationErrorLabel, "" );  setError ( mProjErrorLabel, "" );  setError ( mRegionErrorLabel, "" );  setError ( mMapsetErrorLabel, "" );  mDatabaseText->setPaletteBackgroundColor ( paletteBackgroundColor() );  mLocationText->setPaletteBackgroundColor ( paletteBackgroundColor() );  mRegionText->setPaletteBackgroundColor ( paletteBackgroundColor() );  mMapsetText->setPaletteBackgroundColor ( paletteBackgroundColor() );  // DATABASE  QSettings settings;  QString db = settings.readEntry("/GRASS/lastGisdbase");  if ( !db.isNull() )   {    mDatabaseLineEdit->setText( db );  }  else  {    mDatabaseLineEdit->setText( QDir::currentDirPath() );  }  databaseChanged();  // Create example tree structure  mTreeListView->clear();  mTreeListView->setSortColumn(-1); // No sorting  mTreeListView->setColumnText( 0, tr("Tree") );  mTreeListView->addColumn( tr("Comment") );  Q3ListViewItem *dbi = new Q3ListViewItem( mTreeListView, "OurDatabase", tr("Database") );  dbi->setOpen(true);  // First inserted is last in the view  Q3ListViewItem *l = new Q3ListViewItem( dbi, "New Zealand", tr("Location 2") );  l->setOpen(true);  Q3ListViewItem *m = new Q3ListViewItem( l, "Cimrman", tr("User's mapset") );  m->setOpen(true);  m = new Q3ListViewItem( l, "PERMANENT", tr("System mapset") );  m->setOpen(true);  l = new Q3ListViewItem( dbi, "Mexico", tr("Location 1") );  m->setOpen(true);  m = new Q3ListViewItem( l, "Juan", tr("User's mapset") );  l->setOpen(true);  m = new Q3ListViewItem( l, "Alejandra", tr("User's mapset") );  m->setOpen(true);  m = new Q3ListViewItem( l, "PERMANENT", tr("System mapset") );  m->setOpen(true);  // PROJECTION  // MAPSET  mMapsetsListView->clear();  mMapsetsListView->setColumnText( 0, tr("Mapset") );  mMapsetsListView->addColumn( tr("Owner") );  // FINISH  setFinishEnabled ( page(FINISH), true );  connect( this, SIGNAL(selected(const QString &)),     this, SLOT(pageSelected(const QString &)));}QgsGrassNewMapset::~QgsGrassNewMapset(){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::~QgsGrassNewMapset()" << std::endl;#endif  mRunning = false;}/*************************** DATABASE *******************************/void QgsGrassNewMapset::browseDatabase(){  // TODO: unfortunately QFileDialog does not support 'new' directory  QFileDialog *fd = new QFileDialog ( this, NULL, mDatabaseLineEdit->text() );  fd->setMode ( QFileDialog::DirectoryOnly );   if ( fd->exec() == QDialog::Accepted )  {    mDatabaseLineEdit->setText( fd->selectedFile() );    databaseChanged();  }}void QgsGrassNewMapset::databaseChanged(){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::databaseChanged()" << std::endl;#endif  // TODO: reset next tabs  //  QSettings settings;  settings.writeEntry("/GRASS/lastGisdbase", mDatabaseLineEdit->text() );  setNextEnabled ( page(DATABASE), false );  setError ( mDatabaseErrorLabel, "" );  QString database = mDatabaseLineEdit->text().trimmed();  if ( database.length() == 0 )   {    setError ( mDatabaseErrorLabel, tr("Enter path to GRASS database") );    return;  }  QFileInfo databaseInfo ( mDatabaseLineEdit->text() );  if ( !databaseInfo.exists() )  {    setError ( mDatabaseErrorLabel, tr("The directory doesn't exist!") );    return;  }  // Check if at least one writable location exists or   // database is writable  bool locationExists = false;  QDir d ( mDatabaseLineEdit->text() );  for ( unsigned int i = 0; i < d.count(); i++ )   {    if ( d[i] == "." || d[i] == ".." ) continue;     QString windName = mDatabaseLineEdit->text() + "/" + d[i] + "/PERMANENT/DEFAULT_WIND";    QString locationName = mDatabaseLineEdit->text() + "/" + d[i];    QFileInfo locationInfo ( locationName );    if ( QFile::exists ( windName ) && locationInfo.isWritable () )     {      locationExists = true;      break;    }  }  if ( locationExists || databaseInfo.isWritable()  )  {    setNextEnabled ( page(DATABASE), true );  }  else  {    setError ( mDatabaseErrorLabel, tr("No writable "      "locations, the database not writable!") );  }}/*************************** LOCATION *******************************/void QgsGrassNewMapset::setLocationPage ( ){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::setLocationPage" << std::endl;#endif  setLocations();}void QgsGrassNewMapset::setLocations ( ){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::setLocations" << std::endl;#endif  mLocationComboBox->clear();  QSettings settings;  QString lastLocation = settings.readEntry("/GRASS/lastLocation");  // Get available locations with write permissions  QDir d ( mDatabaseLineEdit->text() );  // Add all subdirs containing PERMANENT/DEFAULT_WIND  int idx = 0;  int sel = -1;  for ( unsigned int i = 0; i < d.count(); i++ )   {    if ( d[i] == "." || d[i] == ".." ) continue;     QString windName = mDatabaseLineEdit->text() + "/" + d[i] + "/PERMANENT/DEFAULT_WIND";    QString locationName = mDatabaseLineEdit->text() + "/" + d[i];    QFileInfo locationInfo ( locationName );    if ( QFile::exists ( windName ) && locationInfo.isWritable () )     {      mLocationComboBox->insertItem ( QString ( d[i] ), -1 );      if ( QString ( d[i] ) == lastLocation ) {        sel = idx;      }      idx++;    }  }  if ( sel >= 0 ) {    mLocationComboBox->setCurrentItem(sel);  }  if ( mLocationComboBox->count() == 0 )  {    mCreateLocationRadioButton->setChecked (true);    mSelectLocationRadioButton->setEnabled(false);  }  else  {    mSelectLocationRadioButton->setEnabled(true);  }  locationRadioSwitched(); // calls also checkLocation()}void QgsGrassNewMapset::locationRadioSwitched(){  if ( mSelectLocationRadioButton->isChecked() )  {    mLocationComboBox->setEnabled(true);    mLocationLineEdit->setEnabled(false);    setAppropriate ( page(PROJECTION), false );    setAppropriate ( page(REGION), false );  }  else  {    mLocationComboBox->setEnabled(false);    mLocationLineEdit->setEnabled(true);    setAppropriate ( page(PROJECTION), true );    setAppropriate ( page(REGION), true );  }  checkLocation();}void QgsGrassNewMapset::checkLocation(){  setError ( mLocationErrorLabel, "");  setNextEnabled ( page(LOCATION), true );  if ( mCreateLocationRadioButton->isChecked() )  {    // TODO?: Check spaces in the name    QString location = mLocationLineEdit->text().trimmed();    if ( location.length() ==  0 )    {      setNextEnabled ( page(LOCATION), false );      setError ( mLocationErrorLabel, tr("Enter location name!") );    }    else    {      QDir d ( mDatabaseLineEdit->text() );      for ( unsigned int i = 0; i < d.count(); i++ )       {        if ( d[i] == "." || d[i] == ".." ) continue;         if ( d[i] == location )         {          setNextEnabled ( page(LOCATION), false );          setError ( mLocationErrorLabel, tr("The location exists!") );          break;        }      }    }  }}void QgsGrassNewMapset::existingLocationChanged(const QString &text ){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::existingLocationChanged()" << std::endl;#endif}void QgsGrassNewMapset::newLocationChanged(){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::newLocationChanged()" << std::endl;#endif  checkLocation();}/************************** PROJECTION ******************************/void QgsGrassNewMapset::setProjectionPage(){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::setProjectionPage()" << std::endl;#endif  setGrassProjection();}void QgsGrassNewMapset::sridSelected(QString theSRID){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::sridSelected()" << std::endl;#endif  projectionSelected();}void QgsGrassNewMapset::projectionSelected(){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::projectionSelected()" << std::endl;#endif  setGrassProjection();}void QgsGrassNewMapset::projRadioSwitched(){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::projRadioSwitched" << std::endl;#endif  if ( mNoProjRadioButton->isChecked() )   {    mProjectionSelector->setEnabled ( false );  } else {    mProjectionSelector->setEnabled ( true );  }  projectionSelected();}void QgsGrassNewMapset::setGrassProjection(){#ifdef QGISDEBUG  std::cerr << "QgsGrassNewMapset::setGrassProjection()" << std::endl;#endif  setError ( mProjErrorLabel, "");  QString proj4 = mProjectionSelector->getCurrentProj4String();  // Not defined  if ( mNoProjRadioButton->isChecked() )  {    mCellHead.proj = PROJECTION_XY;    mCellHead.zone = 0;    mProjInfo = 0;    mProjUnits = 0;    setNextEnabled ( page(PROJECTION), true );    return;  }  // Define projection  if ( !proj4.isNull() )   {#ifdef QGISDEBUG    std::cerr << "proj4 = " << proj4.local8Bit().data() << std::endl;#endif    OGRSpatialReferenceH hSRS = NULL;    hSRS = OSRNewSpatialReference(NULL);    int errcode;    if ( (errcode = OSRImportFromProj4(hSRS, proj4.ascii())) != OGRERR_NONE) {      std::cerr << "OGR can't parse PROJ.4-style parameter string:\n" << proj4.ascii()        << "\nOGR Error code was " << errcode << std::endl;      mCellHead.proj = PROJECTION_XY;      mCellHead.zone = 0;      mProjInfo = 0;      mProjUnits = 0;    }    else    {#ifdef QGISDEBUG      std::cerr << "OSRIsGeographic = " << OSRIsGeographic( hSRS ) << std::endl;      std::cerr << "OSRIsProjected = " << OSRIsProjected( hSRS ) << std::endl;      char *wkt = NULL;	      if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE)       {        std::cerr << "OGR can't get WKT-style parameter string\n"          << "OGR Error code was " << errcode << std::endl;      }      else      {        std::cerr << "wkt = " << wkt << std::endl;      }#endif      int ret;      // Note: GPJ_osr_to_grass() defaults in PROJECTION_XY if projection      //       cannot be set      // There was a bug in GRASS, it is present in 6.0.x line#if GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR >= 1      ret = GPJ_osr_to_grass ( &mCellHead, &mProjInfo,         &mProjUnits, hSRS, 0);#else      // Buggy version:      ret = GPJ_osr_to_grass ( &mCellHead, &mProjInfo,         &mProjUnits, (void **)hSRS, 0);#endif      // Note: It seems that GPJ_osr_to_grass()returns always 1,       // 	 -> test if mProjInfo was set#ifdef QGISDEBUG      std::cerr << "ret = " << ret << std::endl;      std::cerr << "mProjInfo = " << mProjInfo << std::endl;#endif    }    if ( !mProjInfo || !mProjUnits )    {      setError ( mProjErrorLabel, tr("Selected projection is not supported by GRASS!") );    }  }   else // Nothing selected  {     mCellHead.proj = PROJECTION_XY;    mCellHead.zone = 0;    mProjInfo = 0;    mProjUnits = 0;

⌨️ 快捷键说明

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