📄 qgsgrassnewmapset.cpp
字号:
QPixmap pm = mPixmap; mRegionMap->setPixmap( pm );}void QgsGrassNewMapset::drawRegion(){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::drawRegion()" << std::endl;#endif QPixmap pm = mPixmap; mRegionMap->setPixmap( pm ); if ( mCellHead.proj == PROJECTION_XY ) return; std::cerr << "pm.isNull() = " << pm.isNull() << std::endl; QPainter p ( &pm ); p.setPen( QPen(QColor(255,0,0),3) ); double n = mNorthLineEdit->text().toDouble(); double s = mSouthLineEdit->text().toDouble(); double e = mEastLineEdit->text().toDouble(); double w = mWestLineEdit->text().toDouble(); // Shift if LL and W > E if ( mCellHead.proj == PROJECTION_LL && w > e ) { if ( (180-w) < (e+180) ) { w -= 360; } else { e += 360; } } std::vector<QgsPoint> tpoints; // ll lr ur ul ll tpoints.push_back( QgsPoint(w,s) ); tpoints.push_back( QgsPoint(e,s) ); tpoints.push_back( QgsPoint(e,n) ); tpoints.push_back( QgsPoint(w,n) ); tpoints.push_back( QgsPoint(w,s) ); // Because of possible shift +/- 360 in LL we have to split // the lines at least in 3 parts std::vector<QgsPoint> points; // for ( int i = 0; i < 4; i++ ) { for ( int j = 0; j < 3; j++ ) { double x = tpoints[i].x(); double y = tpoints[i].y(); double dx = (tpoints[i+1].x()-x)/3; double dy = (tpoints[i+1].y()-y)/3;#ifdef QGISDEBUG std::cerr << "dx = " << dx << " x = " << x+j*dx << std::endl;#endif points.push_back( QgsPoint( x+j*dx, y+j*dy) ); } } points.push_back( QgsPoint( points[0] ) ); // close polygon // Warning: seems that crashes if source == dest if ( mProjectionSelector->getCurrentSRSID() != 2585 ) { QgsSpatialRefSys source ( mProjectionSelector->getCurrentSRSID(), QgsSpatialRefSys::QGIS_SRSID ); if ( !source.isValid() ) { QMessageBox::warning( 0, tr("Warning"), tr("Cannot create QgsSpatialRefSys") ); return; } QgsSpatialRefSys dest ( 2585, QgsSpatialRefSys::QGIS_SRSID ); if ( !dest.isValid() ) { QMessageBox::warning( 0, tr("Warning"), tr("Cannot create QgsSpatialRefSys") ); return; } QgsCoordinateTransform trans( source, dest ); bool ok = true; for ( int i = 0; i < 13; i++ ) { // Warning: I found that with some projections (e.g. Abidjan 1987) // if N = 90 or S = -90 the coordinate projected to // WGS84 is nonsense (156.983,89.9988 regardless x) -> // use 89.9 - for draw it is not so important if ( mCellHead.proj == PROJECTION_LL ) { if ( points[i].y() >= 89.9 ) points[i].setY(89.9); if ( points[i].y() <= -89.9 ) points[i].setY(-89.9); }#ifdef QGISDEBUG std::cerr << points[i].x() << "," << points[i].y() << " -> ";#endif try { points[i] = trans.transform ( points[i] );#ifdef QGISDEBUG std::cerr << points[i].x() << "," << points[i].y() << std::endl;#endif } catch(QgsCsException &cse) { UNUSED(cse); std::cerr << "Cannot transform point" << std::endl; ok = false; break; } } if ( !ok ) { std::cerr << "Cannot reproject region." << std::endl; return; } } for ( int shift = -360; shift <= 360; shift+=360 ) { for ( int i = 0; i < 12; i++ ) { double x1 = points[i].x(); double x2 = points[i+1].x(); if ( fabs(x2-x1) > 150 ) { if ( x2 < x1 ) { x2 += 360; } else { x2 -= 360; } } p.drawLine ( 180+shift+(int)x1, 90-(int)points[i].y(), 180+shift+(int)x2, 90-(int)points[i+1].y() ); } } p.end(); mRegionMap->setPixmap( pm );}/**************************** MAPSET ********************************/void QgsGrassNewMapset::setMapsets(){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::setMapsets" << std::endl;#endif mMapsetsListView->clear(); if ( mCreateLocationRadioButton->isChecked() ) { mMapsetsFrame->hide(); return; } else { mMapsetsFrame->show(); } // Get available mapsets QString locationPath = mDatabaseLineEdit->text() + "/" + mLocationComboBox->currentText(); QDir d ( locationPath ); // Add all subdirs containing WIND Q3ListViewItem *lvi; for ( unsigned int i = 0; i < d.count(); i++ ) { if ( d[i] == "." || d[i] == ".." ) continue; QString mapsetPath = locationPath + "/" + d[i]; QString windPath = mapsetPath + "/WIND"; QFileInfo mapsetInfo ( mapsetPath ); if ( QFile::exists ( windPath ) ) { lvi = new Q3ListViewItem( mMapsetsListView, d[i], mapsetInfo.owner() ); } }}void QgsGrassNewMapset::mapsetChanged(){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::mapsetChanged()" << std::endl;#endif setNextEnabled ( page(MAPSET), false ); setError ( mMapsetErrorLabel, ""); QString mapset = mMapsetLineEdit->text().trimmed(); // TODO?: Check spaces in the name if ( mapset.length() == 0 ) { setError ( mMapsetErrorLabel, tr("Enter mapset name.")); return; } // Check if exists if ( mSelectLocationRadioButton->isChecked() ) { bool exists = false; QString locationPath = mDatabaseLineEdit->text() + "/" + mLocationComboBox->currentText(); QDir d ( locationPath ); for ( unsigned int i = 0; i < d.count(); i++ ) { if ( d[i] == "." || d[i] == ".." ) continue; if ( d[i] == mapset ) { setError ( mMapsetErrorLabel, tr("The mapset already exists") ); exists = true; break; } } if ( !exists ) { setNextEnabled ( page(MAPSET), true ); } } else { setNextEnabled ( page(MAPSET), true ); }}/**************************** FINISH ********************************/void QgsGrassNewMapset::setFinishPage(){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::setFinish()" << std::endl;#endif mDatabaseLabel->setText ( tr("Database: ") + mDatabaseLineEdit->text() ); QString location; if ( mSelectLocationRadioButton->isChecked() ) { location = mLocationComboBox->currentText(); } else { location = mLocationLineEdit->text().stripWhiteSpace(); } mLocationLabel->setText ( tr("Location: ") + location ); mMapsetLabel->setText ( tr("Mapset: ") + mMapsetLineEdit->text() ); setFinishEnabled ( page(FINISH), true );}void QgsGrassNewMapset::createMapset(){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::createMapset()" << std::endl;#endif // TODO: handle all possible errors better, especially // half created location/mapset QString location; if ( mCreateLocationRadioButton->isChecked() ) { location = mLocationLineEdit->text().stripWhiteSpace(); // TODO: add QgsGrass::setLocation or G_make_location with // database path QgsGrass::activeMode(); // because it calls private gsGrass::init()#if defined(WIN32) G__setenv( (char *)"GISDBASE", (char *) getShortPath(mDatabaseLineEdit->text()).ascii() );#else G__setenv( (char *)"GISDBASE", (char *) mDatabaseLineEdit->text().ascii() );#endif QgsGrass::resetError(); int ret = G_make_location( (char *) location.ascii(), &mCellHead, mProjInfo, mProjUnits, stdout ); if ( ret != 0 ) { QMessageBox::warning (this, tr("Create location"), tr("Cannot create new location: ") + QgsGrass::getErrorMessage() ); return; } else { // Location created -> reset widgets setLocations(); mSelectLocationRadioButton->setChecked(true); mLocationComboBox->setCurrentText ( location ); mLocationLineEdit->setText(""); locationRadioSwitched(); // calls also checkLocation() } } else { location = mLocationComboBox->currentText(); } // Create mapset QString mapset = mMapsetLineEdit->text().stripWhiteSpace(); if ( mapset != "PERMANENT" ) { QString locationPath = mDatabaseLineEdit->text() + "/" + location; QDir d ( locationPath ); if ( !d.mkdir(mapset) ) { QMessageBox::warning (this, tr("Create mapset"), tr("Cannot create new mapset directory") ); return; } // Copy WIND Better way to copy file in Qt? QStringList lines; QFile in ( locationPath + "/PERMANENT/DEFAULT_WIND" ); if ( !in.open( QIODevice::ReadOnly ) ) { QMessageBox::warning (this, tr("Create mapset"), tr("Cannot open DEFAULT_WIND") ); return; } QFile out ( locationPath + "/" + mapset + "/WIND" ); if ( !out.open( QIODevice::WriteOnly ) ) { QMessageBox::warning (this, tr("Create mapset"), tr("Cannot open WIND") ); return; } QTextStream stream ( &out ); //QTextStream stream( &file ); QString line; char buf[100]; while ( in.readLine( buf, 100 ) != -1 ) { stream << buf; } in.close(); out.close(); } QString err = QgsGrass::openMapset ( mDatabaseLineEdit->text(), location, mapset ); if ( err.length() > 0 ) { QMessageBox::information ( this, tr("New mapset"), tr("New mapset successfully created, but cannot be " "opened: ") + err ); } else { QMessageBox::information ( this, tr("New mapset"), tr("New mapset successfully created and set " "as current working mapset.") ); mPlugin->mapsetChanged(); } delete this;}void QgsGrassNewMapset::accept(){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::accept()" << std::endl;#endif createMapset();}/********************************************************************/void QgsGrassNewMapset::setError( QLabel *line, const QString &err ){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::setError(): " << std::endl;#endif if ( err.length() > 0 ) { line->setText(err); line->show(); } else { line->setText(""); line->hide(); }}// Warning: we have to catch key press otherwise QWizard goes always // to next page if Key_Enter is pressedvoid QgsGrassNewMapset::keyPressEvent ( QKeyEvent * e ){#ifdef QGISDEBUG //std::cerr << "QgsGrassNewMapset::keyPressEvent() key = " << e->key() << std::endl;#endif}void QgsGrassNewMapset::pageSelected( const QString & title ){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::pageSelected(): " << title.local8Bit().data() << std::endl;#endif int index = indexOf ( currentPage () );#ifdef QGISDEBUG std::cerr << "index = " << index << std::endl;#endif switch (index) { case LOCATION: if ( mPreviousPage == DATABASE ) { setLocationPage(); } break; case PROJECTION: // Projection selector if ( !mProjectionSelector ) { QGridLayout *projectionLayout = new QGridLayout( mProjectionFrame, 1, 1 ); mProjectionSelector = new QgsProjectionSelector ( mProjectionFrame, "Projection", 0 ); mProjectionSelector->setEnabled ( false ); projectionLayout->addWidget ( mProjectionSelector, 0 , 0 ); mProjectionSelector->show(); connect( mProjectionSelector, SIGNAL(sridSelected(QString)), this, SLOT(sridSelected(QString))); // Se current QGIS projection int srsid = QgsProject::instance()->readNumEntry( "SpatialRefSys","/ProjectSRSID",0); QgsSpatialRefSys srs( srsid, QgsSpatialRefSys::QGIS_SRSID );#ifdef QGISDEBUG std::cerr << "current project srsid = " << srsid << std::endl; std::cerr << "srs.isValid() = " << srs.isValid() << std::endl;#endif if ( srs.isValid() ) { mProjectionSelector->setSelectedSRSID ( srsid ); mProjRadioButton->setChecked(true); projRadioSwitched(); } } if ( mPreviousPage == LOCATION ) { setProjectionPage(); } break; case REGION: if ( !mRegionsInited ) { loadRegions(); mRegionsInited = true; } if ( mPreviousPage == PROJECTION ) { setRegionPage(); } break; case MAPSET: if ( mPreviousPage == LOCATION || mPreviousPage == REGION ) { setMapsets(); mapsetChanged(); } break; case FINISH: setFinishPage(); break; } mPreviousPage = index;}bool QgsGrassNewMapset::isRunning(void){ return mRunning;}void QgsGrassNewMapset::close(void){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::close()" << std::endl;#endif hide(); mRunning = false; delete this; }void QgsGrassNewMapset::closeEvent(QCloseEvent *e){#ifdef QGISDEBUG std::cerr << "QgsGrassNewMapset::closeEvent()" << std::endl;#endif e->accept(); close();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -