/*************************************************************************** qgsgrasstools.cpp ------------------- begin : March, 2005 copyright : (C) 2005 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. * * * ***************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //Added by qt3to4: #include #include "../../src/qgis.h" #include "../../src/qgsmapcanvas.h" #include "../../src/qgsmaplayer.h" #include "../../src/qgsvectorlayer.h" #include "../../src/qgsdataprovider.h" #include "../../src/qgsfield.h" #include "../../src/qgsfeatureattribute.h" extern "C" { #include #include } #include "../../providers/grass/qgsgrass.h" #include "../../providers/grass/qgsgrassprovider.h" #include "qgsgrassattributes.h" #include "qgsgrasstools.h" #include "qgsgrassmodule.h" #include "qgsgrassshell.h" QgsGrassTools::QgsGrassTools ( QgisApp *qgisApp, QgisIface *iface, QWidget * parent, const char * name, Qt::WFlags f ) //:QgsGrassToolsBase ( parent, name, f ) //commented out params during Qt4 Ui port - FIXME :QgsGrassToolsBase ( ) { #ifdef QGISDEBUG std::cerr << "QgsGrassTools()" << std::endl; #endif mQgisApp = qgisApp; mIface = iface; mCanvas = mIface->getMapCanvas(); // Set list view mModulesListView->setColumnText(0,"Modules"); mModulesListView->clear(); mModulesListView->setSorting(-1); mModulesListView->setRootIsDecorated(true); mModulesListView->setResizeMode(Q3ListView::AllColumns); mModulesListView->header()->hide(); connect( mModulesListView, SIGNAL(clicked(Q3ListViewItem *)), this, SLOT(moduleClicked( Q3ListViewItem *)) ); #if defined(WIN32) || defined(Q_OS_MACX) mAppDir = qApp->applicationDirPath(); #else mAppDir = PREFIX; #endif QString title = "GRASS Tools: " + QgsGrass::getDefaultLocation() + "/" + QgsGrass::getDefaultMapset(); setCaption(title); QString conf = mAppDir + "/share/qgis/grass/config/default.qgc"; loadConfig ( conf ); //statusBar()->hide(); restorePosition(); } void QgsGrassTools::moduleClicked( Q3ListViewItem * item ) { if ( !item ) return; QString name = item->text(1); //std::cerr << "name = " << name << std::endl; if ( name.length() == 0 ) return; // Section QString path = mAppDir + "/share/qgis/grass/modules/" + name; QWidget *m; QgsGrassShell *sh = 0; if ( name == "shell" ) { #ifdef HAVE_OPENPTY sh = new QgsGrassShell(this, mTabWidget); m = dynamic_cast ( sh ); #else QMessageBox::warning( 0, "Warning", "GRASS Shell is not compiled." ); #endif } else { m = dynamic_cast ( new QgsGrassModule ( this, mQgisApp, mIface, path, mTabWidget ) ); } //mTabWidget->addTab ( m, item->text(0) ); QPixmap pixmap = QgsGrassModule::pixmap ( path, 25 ); QIcon is; is.setPixmap ( pixmap, QIcon::Small, QIcon::Normal ); mTabWidget->addTab ( m, is, "" ); mTabWidget->setCurrentPage ( mTabWidget->count()-1 ); // We must call resize to reset COLUMNS enviroment variable // used by bash !!! if ( sh ) sh->resizeTerminal(); } bool QgsGrassTools::loadConfig(QString filePath) { #ifdef QGISDEBUG std::cerr << "QgsGrassTools::loadConfig(): " << filePath.toLocal8Bit().data() << std::endl; #endif mModulesListView->clear(); QFile file ( filePath ); if ( !file.exists() ) { QMessageBox::warning( 0, "Warning", "The config file (" + filePath + ") not found." ); return false; } if ( ! file.open( QIODevice::ReadOnly ) ) { QMessageBox::warning( 0, "Warning", "Cannot open config file (" + filePath + ")" ); return false; } QDomDocument doc ( "qgisgrass" ); QString err; int line, column; if ( !doc.setContent( &file, &err, &line, &column ) ) { QString errmsg = "Cannot read config file (" + filePath + "):\n" + err + "\nat line " + QString::number(line) + " column " + QString::number(column); std::cerr << errmsg.toLocal8Bit().data() << std::endl; QMessageBox::warning( 0, "Warning", errmsg ); file.close(); return false; } QDomElement docElem = doc.documentElement(); QDomNodeList modulesNodes = docElem.elementsByTagName ( "modules" ); if ( modulesNodes.count() == 0 ) { file.close(); return false; } QDomNode modulesNode = modulesNodes.item(0); QDomElement modulesElem = modulesNode.toElement(); // Go through the sections and modules and add them to the list view addModules ( 0, modulesElem ); file.close(); } void QgsGrassTools::addModules ( Q3ListViewItem *parent, QDomElement &element ) { QDomNode n = element.firstChild(); Q3ListViewItem *item; Q3ListViewItem *lastItem = 0; while( !n.isNull() ) { QDomElement e = n.toElement(); if( !e.isNull() ) { //std::cout << "tag = " << e.tagName() << std::endl; if ( e.tagName() == "section" && e.tagName() == "grass" ) { std::cout << "Unknown tag: " << e.tagName().toLocal8Bit().data() << std::endl; continue; } if ( parent ) { item = new Q3ListViewItem( parent, lastItem ); } else { item = new Q3ListViewItem( mModulesListView, lastItem ); } if ( e.tagName() == "section" ) { QString label = e.attribute("label"); std::cout << "label = " << label.toLocal8Bit().data() << std::endl; item->setText( 0, label ); item->setOpen(true); // for debuging to spare one click addModules ( item, e ); lastItem = item; } else if ( e.tagName() == "grass" ) { // GRASS module QString name = e.attribute("name"); std::cout << "name = " << name.toLocal8Bit().data() << std::endl; QString path = mAppDir + "/share/qgis/grass/modules/" + name; QString label = QgsGrassModule::label ( path ); QPixmap pixmap = QgsGrassModule::pixmap ( path, 25 ); item->setText( 0, label ); item->setPixmap( 0, pixmap ); item->setText( 1, name ); lastItem = item; } } n = n.nextSibling(); } } void QgsGrassTools::mapsetChanged() { #ifdef QGISDEBUG std::cerr << "QgsGrassTools::mapsetChanged()" << std::endl; #endif QString title = "GRASS Tools: " + QgsGrass::getDefaultLocation() + "/" + QgsGrass::getDefaultMapset(); setCaption(title); // TODO: Close opened tools (tabs) ? } QgsGrassTools::~QgsGrassTools() { #ifdef QGISDEBUG std::cerr << "QgsGrassTools::~QgsGrassTools()" << std::endl; #endif saveWindowLocation(); } QString QgsGrassTools::appDir(void) { return mAppDir; } void QgsGrassTools::close(void) { saveWindowLocation(); hide(); } void QgsGrassTools::closeEvent(QCloseEvent *e) { saveWindowLocation(); e->accept(); } void QgsGrassTools::restorePosition() { QSettings settings("QuantumGIS", "qgis"); int ww = settings.readNumEntry("/GRASS/windows/tools/w", 250); int wh = settings.readNumEntry("/GRASS/windows/tools/h", 300); int wx = settings.readNumEntry("/GRASS/windows/tools/x", 100); int wy = settings.readNumEntry("/GRASS/windows/tools/y", 100); resize(ww,wh); move(wx,wy); show(); } void QgsGrassTools::saveWindowLocation() { QSettings settings("QuantumGIS", "qgis"); QPoint p = this->pos(); QSize s = this->size(); settings.writeEntry("/GRASS/windows/tools/x", p.x()); settings.writeEntry("/GRASS/windows/tools/y", p.y()); settings.writeEntry("/GRASS/windows/tools/w", s.width()); settings.writeEntry("/GRASS/windows/tools/h", s.height()); }