~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/plugins/grass/qgsgrasstools.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 *   (at your option) any later version.                                   *
14
14
 *                                                                         *
15
15
 ***************************************************************************/
16
 
#include <iostream>
17
 
 
18
 
#include <qapplication.h>
19
 
#include <qdir.h>
20
 
#include <qfile.h>
21
 
#include <qsettings.h>
22
 
#include <qpixmap.h>
23
 
#include <q3listbox.h>
24
 
#include <qstringlist.h>
25
 
#include <qlabel.h>
26
 
#include <QComboBox>
27
 
#include <qspinbox.h>
28
 
#include <qmessagebox.h>
29
 
#include <qinputdialog.h>
30
 
#include <qsettings.h>
31
 
#include <qpainter.h>
32
 
#include <qpixmap.h>
33
 
#include <qpen.h>
34
 
#include <q3pointarray.h>
35
 
#include <qcursor.h>
36
 
#include <qnamespace.h>
37
 
#include <q3listview.h>
38
 
#include <qcolordialog.h>
39
 
#include <q3table.h>
40
 
#include <qstatusbar.h>
41
 
#include <qevent.h>
42
 
#include <qpoint.h>
43
 
#include <qsize.h>
44
 
#include <qdom.h>
45
 
#include <qtabwidget.h>
46
 
#include <qlayout.h>
47
 
#include <qcheckbox.h>
48
 
#include <q3process.h>
49
 
#include <qicon.h>
50
 
//Added by qt3to4:
51
 
#include <QCloseEvent>
52
 
#include <QTabBar>
53
 
#include <QListView>
54
 
#include <QProcess>
55
 
#include <QHeaderView>
56
 
 
57
 
#include "qgis.h"
58
 
#include "qgsapplication.h"
59
 
#include "qgsmapcanvas.h"
60
 
#include "qgsmaplayer.h"
61
 
#include "qgsvectorlayer.h"
62
 
#include "qgsdataprovider.h"
63
 
#include "qgsfield.h"
64
 
#include "qgsfeatureattribute.h"
65
 
 
66
 
extern "C" {
67
 
#include <grass/gis.h>
68
 
#include <grass/Vect.h>
69
 
}
70
 
 
71
 
#include "../../src/providers/grass/qgsgrass.h"
72
 
#include "../../src/providers/grass/qgsgrassprovider.h"
73
 
#include "qgsgrassattributes.h"
 
16
 
74
17
#include "qgsgrasstools.h"
 
18
#include "qgsgrassbrowser.h"
75
19
#include "qgsgrassmodule.h"
76
20
#include "qgsgrassshell.h"
77
 
#include "qgsgrassmodel.h"
78
 
#include "qgsgrassbrowser.h"
79
 
 
80
 
QgsGrassToolsTabWidget::QgsGrassToolsTabWidget( QWidget * parent ): 
81
 
        QTabWidget(parent)
82
 
{
83
 
    // Default height seems to be too small for our purpose
84
 
    int height = (int)(1.5 * tabBar()->iconSize().height());
85
 
    // Max width (see QgsGrassModule::pixmap for hardcoded sizes)
86
 
    int width = 3*height + 28 + 29;
87
 
    tabBar()->setIconSize( QSize(width,height) );
88
 
}
89
 
 
90
 
QSize QgsGrassToolsTabWidget::iconSize()
91
 
{
92
 
    return tabBar()->iconSize();
93
 
}
94
 
 
95
 
QgsGrassToolsTabWidget::~QgsGrassToolsTabWidget() {}
96
 
 
97
 
QgsGrassTools::QgsGrassTools ( QgisApp *qgisApp, QgisIface *iface, 
98
 
                             QWidget * parent, const char * name, Qt::WFlags f )
99
 
             :QDialog ( parent )
100
 
{
101
 
    #ifdef QGISDEBUG
102
 
    std::cerr << "QgsGrassTools()" << std::endl;
103
 
    #endif
104
 
 
105
 
   setWindowTitle ( tr("GRASS Tools") );
106
 
//    setupUi(this);
107
 
 
108
 
    mQgisApp = qgisApp;
109
 
    mIface = iface;
110
 
    mCanvas = mIface->getMapCanvas();
111
 
 
112
 
    connect( qApp, SIGNAL(aboutToQuit()), 
113
 
                         this, SLOT(closeTools()) );
114
 
 
115
 
    mTabWidget = new QgsGrassToolsTabWidget (this);
116
 
    QVBoxLayout *layout1 = new QVBoxLayout(this);
117
 
    layout1->addWidget(mTabWidget);
118
 
 
119
 
 
120
 
    mModulesListView = new Q3ListView();
121
 
    mTabWidget->addTab( mModulesListView, tr("Modules") );
122
 
    mModulesListView->addColumn("col1",0);
123
 
    
124
 
    // Set list view
125
 
    mModulesListView->setColumnText(0,tr("Modules"));
126
 
    mModulesListView->clear();
127
 
    mModulesListView->setSorting(-1);
128
 
    mModulesListView->setRootIsDecorated(true);
129
 
    mModulesListView->setResizeMode(Q3ListView::AllColumns);
130
 
    mModulesListView->header()->hide();
131
 
 
132
 
    connect( mModulesListView, SIGNAL(clicked(Q3ListViewItem *)), 
133
 
                         this, SLOT(moduleClicked( Q3ListViewItem *)) );
134
 
 
135
 
    QString title = tr("GRASS Tools: ") + QgsGrass::getDefaultLocation()
136
 
                + "/" + QgsGrass::getDefaultMapset();
137
 
    setCaption(title);
138
 
 
139
 
    // Warning: QgsApplication initialized in main.cpp
140
 
    //          is not valid here (static libraries / linking)
141
 
 
142
 
#if defined(WIN32) || defined(Q_OS_MACX)
143
 
    mAppDir = qApp->applicationDirPath();
144
 
#else
145
 
    mAppDir = PREFIX;
146
 
#endif
147
 
 
148
 
    //QString conf = QgsApplication::pkgDataPath() + "/grass/config/default.qgc";
149
 
    QString conf = mAppDir + "/share/qgis/grass/config/default.qgc";
150
 
 
151
 
    restorePosition();
152
 
 
153
 
    // Show before loadConfig() so that user can see loading
154
 
    mModulesListView->show(); 
155
 
    QApplication::setOverrideCursor(Qt::waitCursor);
156
 
    loadConfig ( conf );
157
 
    QApplication::restoreOverrideCursor();
158
 
 
159
 
    //statusBar()->hide();
160
 
 
161
 
    // Add map browser 
162
 
    // Warning: if browser is on the first page modules are 
163
 
    // displayed over the browser
164
 
    mBrowser = new QgsGrassBrowser ( mIface, this );
165
 
    mTabWidget->addTab( mBrowser, tr("Browser") );
166
 
 
167
 
    connect( mBrowser, SIGNAL(regionChanged()), 
168
 
                         this, SLOT(emitRegionChanged()) );
169
 
}
170
 
 
171
 
void QgsGrassTools::moduleClicked( Q3ListViewItem * item )
172
 
{
173
 
    #ifdef QGISDEBUG
174
 
    std::cerr << "QgsGrassTools::moduleClicked()" << std::endl;
175
 
    #endif
176
 
    if ( !item ) return;
177
 
 
178
 
    QString name = item->text(1);
179
 
    //std::cerr << "name = " << name << std::endl;
180
 
    
181
 
    if ( name.length() == 0 ) return;  // Section
182
 
    
183
 
    //QString path = QgsApplication::pkgDataPath() + "/grass/modules/" + name;
184
 
    QString path = mAppDir + "/share/qgis/grass/modules/" + name;
185
 
    #ifdef QGISDEBUG
186
 
    std::cerr << "path = " << path.ascii() << std::endl;
187
 
    #endif
188
 
    QWidget *m;
189
 
    QgsGrassShell *sh = 0;
190
 
    if ( name == "shell" )
191
 
    {
192
 
         // Set history file
193
 
         QString mapsetPath = QgsGrass::getDefaultGisdbase() + "/"
194
 
                        + QgsGrass::getDefaultLocation() + "/"
195
 
                        + QgsGrass::getDefaultMapset();
196
 
 
197
 
         // bash
198
 
         QString hist = "HISTFILE=" + mapsetPath + "/.bash_history";
199
 
         char *histChar = new char[hist.length()+1];
200
 
         strcpy ( histChar, const_cast<char *>(hist.ascii()) );
201
 
         putenv( histChar );
202
 
 
203
 
         // csh/tcsh
 
21
#include "qgsgrass.h"
 
22
 
 
23
#include "qgisinterface.h"
 
24
#include "qgsapplication.h"
 
25
#include "qgslogger.h"
 
26
 
 
27
#include <QCloseEvent>
 
28
#include <QDomDocument>
 
29
#include <QFile>
 
30
#include <QHeaderView>
 
31
#include <QMessageBox>
 
32
#include <QPainter>
 
33
#include <QSettings>
 
34
 
 
35
 
 
36
//
 
37
// For experimental model view alternative ui by Tim
 
38
//
 
39
//
 
40
#include "qgsdetaileditemdata.h"
 
41
#include "qgsdetaileditemdelegate.h"
 
42
#include <QSortFilterProxyModel>
 
43
#include <QStandardItem>
 
44
 
 
45
 
 
46
QgsGrassTools::QgsGrassTools( QgisInterface *iface,
 
47
                              QWidget * parent, const char * name, Qt::WFlags f )
 
48
    : QDialog( parent, f ), QgsGrassToolsBase()
 
49
{
 
50
 
 
51
  setupUi( this );
 
52
  QgsDebugMsg( "QgsGrassTools()" );
 
53
  qRegisterMetaType<QgsDetailedItemData>();
 
54
 
 
55
  setWindowTitle( tr( "GRASS Tools" ) );
 
56
  //    setupUi(this);
 
57
 
 
58
  mIface = iface;
 
59
  mCanvas = mIface->mapCanvas();
 
60
 
 
61
  connect( qApp, SIGNAL( aboutToQuit() ),
 
62
           this, SLOT( closeTools() ) );
 
63
 
 
64
  //
 
65
  // Radims original tree view code.
 
66
  //
 
67
  mModulesTree->header()->hide();
 
68
  connect( mModulesTree, SIGNAL( itemClicked( QTreeWidgetItem *, int ) ),
 
69
           this, SLOT( moduleClicked( QTreeWidgetItem *, int ) ) );
 
70
 
 
71
  //
 
72
  // Tims experimental list view with filter
 
73
  //
 
74
  mModelTools = new QStandardItemModel( 0, 1 );
 
75
  mModelProxy = new QSortFilterProxyModel( this );
 
76
  mModelProxy->setSourceModel( mModelTools );
 
77
  mModelProxy->setFilterRole( Qt::UserRole + 2 );
 
78
 
 
79
  mListView->setModel( mModelProxy );
 
80
  mListView->setItemDelegateForColumn( 0, new QgsDetailedItemDelegate() );
 
81
  mListView->setUniformItemSizes( false );
 
82
  //mListView2 = new QListView(this);
 
83
  //mDockWidget = new QDockWidget(tr("Grass Tools"), 0);
 
84
  //mDockWidget->setWidget(mListView2);
 
85
  //mDockWidget->setObjectName("GrassTools");
 
86
  //mDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
 
87
  //mIface->addDockWidget(Qt::LeftDockWidgetArea, mDockWidget);
 
88
  connect( mListView, SIGNAL( clicked( const QModelIndex ) ),
 
89
           this, SLOT( listItemClicked( const QModelIndex ) ) );
 
90
  //
 
91
  // End of Tims experimental bit
 
92
  //
 
93
 
 
94
  //
 
95
  // Load the modules lists
 
96
  //
 
97
  // Show before loadConfig() so that user can see loading
 
98
  QString conf = QgsApplication::pkgDataPath() + "/grass/config/default.qgc";
 
99
  restorePosition();
 
100
 
 
101
  QApplication::setOverrideCursor( Qt::WaitCursor );
 
102
  loadConfig( conf );
 
103
  QApplication::restoreOverrideCursor();
 
104
  //statusBar()->hide();
 
105
 
 
106
  // set the dialog title
 
107
  QString title = tr( "GRASS Tools: %1/%2" ).arg( QgsGrass::getDefaultLocation() ).arg( QgsGrass::getDefaultMapset() );
 
108
  setWindowTitle( title );
 
109
 
 
110
 
 
111
  // Add map browser
 
112
  mBrowser = new QgsGrassBrowser( mIface, this );
 
113
  mTabWidget->addTab( mBrowser, tr( "Browser" ) );
 
114
 
 
115
  connect( mBrowser, SIGNAL( regionChanged() ),
 
116
           this, SLOT( emitRegionChanged() ) );
 
117
}
 
118
 
 
119
void QgsGrassTools::moduleClicked( QTreeWidgetItem * item, int column )
 
120
{
 
121
  QgsDebugMsg( "entered." );
 
122
  if ( !item ) return;
 
123
 
 
124
  QString name = item->text( 1 );
 
125
  QgsDebugMsg( QString( "name = %1" ).arg( name ) );
 
126
  runModule( name );
 
127
}
 
128
 
 
129
void QgsGrassTools::runModule( QString name )
 
130
{
 
131
  if ( name.length() == 0 ) return;  // Section
 
132
 
204
133
#ifndef WIN32
205
 
         hist = "histfile=" + mapsetPath + "/.history";
206
 
         histChar = new char[hist.length()+1];
207
 
         strcpy ( histChar, const_cast<char *>(hist.ascii()) );
208
 
         putenv( histChar );
 
134
  QgsGrassShell* sh = 0;
209
135
#endif
210
136
 
 
137
  QString path = QgsApplication::pkgDataPath() + "/grass/modules/" + name;
 
138
  QgsDebugMsg( QString( "path = %1" ).arg( path ) );
 
139
  QWidget *m;
 
140
  if ( name == "shell" )
 
141
  {
211
142
#ifdef WIN32
212
 
         // Run MSYS if available
213
 
         // Note: I was not able to run cmd.exe and command.com
214
 
         //       with QProcess
215
 
 
216
 
         QString msysPath = mAppDir + "/msys/bin/rxvt.exe";
217
 
         QString myArguments = "-backspacekey ^H -sl 2500 -fg white -bg black -sr -fn Courier-16 -tn msys -geometry 80x25 -e    /bin/sh --login -i";
218
 
         QFile file ( msysPath );
219
 
 
220
 
         if ( !file.exists() ) 
221
 
         {
222
 
           QMessageBox::warning( 0, tr("Warning"),
223
 
               tr("Cannot find MSYS (") + msysPath + ")" );
224
 
         } 
225
 
         else
226
 
         {
227
 
           QProcess *proc = new QProcess(this);
228
 
           //allow msys to exist in a path with spaces
229
 
           msysPath =  "\"" + msysPath + "\""  ;
230
 
           proc->start(msysPath + " " +  myArguments);
231
 
           proc->waitForStarted();
232
 
           if ( proc->state() != QProcess::Running )
233
 
           {
234
 
             QMessageBox::warning( 0, tr("Warning"),
235
 
                 tr("Cannot start MSYS (") + msysPath + ")" );
236
 
           }
237
 
         }
238
 
         return;
239
 
#else 
240
 
 
241
 
    #ifdef HAVE_OPENPTY
242
 
        sh = new QgsGrassShell(this, mTabWidget);
243
 
        m = dynamic_cast<QWidget *> ( sh );
244
 
    #else
245
 
        QMessageBox::warning( 0, tr("Warning"), tr("GRASS Shell is not compiled.") );
246
 
    #endif // HAVE_OPENPTY
 
143
    if ( !QProcess::startDetached( getenv( "COMSPEC" ) ) )
 
144
    {
 
145
      QMessageBox::warning( 0, "Warning", tr( "Cannot start command shell (%1)" ).arg( getenv( "COMSPEC" ) ) );
 
146
    }
 
147
    return;
 
148
#else
 
149
 
 
150
#ifdef HAVE_OPENPTY
 
151
    sh = new QgsGrassShell( this, mTabWidget );
 
152
    m = qobject_cast<QWidget *>( sh );
 
153
#else
 
154
    QMessageBox::warning( 0, tr( "Warning" ), tr( "GRASS Shell is not compiled." ) );
 
155
#endif // HAVE_OPENPTY
247
156
 
248
157
#endif // ! WIN32
249
 
    }
250
 
    else
251
 
    {
252
 
        m = dynamic_cast<QWidget *> ( new QgsGrassModule ( this, name,
253
 
                                      mQgisApp, mIface, path, mTabWidget ) );
254
 
    }
255
 
    
256
 
    int height = mTabWidget->iconSize().height();
257
 
    QPixmap pixmap = QgsGrassModule::pixmap ( path, height ); 
258
 
    
259
 
    // Icon size in QT4 does not seem to be variable
260
 
    // -> put smaller icons in the middle
261
 
    QPixmap pixmap2 ( mTabWidget->iconSize() );
262
 
    QPalette pal;
263
 
    pixmap2.fill ( pal.color(QPalette::Window) );
264
 
    QPainter painter(&pixmap2);
265
 
    int x = (int) ( (mTabWidget->iconSize().width()-pixmap.width())/2 );
266
 
    painter.drawPixmap ( x, 0, pixmap );
267
 
    painter.end();
268
 
 
269
 
    QIcon is;
270
 
    is.addPixmap ( pixmap2 );
271
 
    mTabWidget->addTab ( m, is, "" );
272
 
 
273
 
   QgsGrassToolsTabWidget tw;
274
 
                
275
 
    mTabWidget->setCurrentPage ( mTabWidget->count()-1 );
276
 
     
277
 
    // We must call resize to reset COLUMNS enviroment variable
278
 
    // used by bash !!!
279
 
#ifndef WIN32
 
158
  }
 
159
  else
 
160
  {
 
161
    m = qobject_cast<QWidget *>( new QgsGrassModule( this, name,
 
162
                                 mIface, path, mTabWidget ) );
 
163
  }
 
164
 
 
165
  int height = mTabWidget->iconSize().height();
 
166
  QPixmap pixmap = QgsGrassModule::pixmap( path, height );
 
167
 
 
168
  // Icon size in QT4 does not seem to be variable
 
169
  // -> put smaller icons in the middle
 
170
  QPixmap pixmap2( mTabWidget->iconSize() );
 
171
  QPalette pal;
 
172
  pixmap2.fill( pal.color( QPalette::Window ) );
 
173
  QPainter painter( &pixmap2 );
 
174
  int x = ( int )(( mTabWidget->iconSize().width() - pixmap.width() ) / 2 );
 
175
  painter.drawPixmap( x, 0, pixmap );
 
176
  painter.end();
 
177
 
 
178
  QIcon is;
 
179
  is.addPixmap( pixmap2 );
 
180
  mTabWidget->addTab( m, is, "" );
 
181
 
 
182
 
 
183
  mTabWidget->setCurrentIndex( mTabWidget->count() - 1 );
 
184
 
 
185
  // We must call resize to reset COLUMNS environment variable
 
186
  // used by bash !!!
 
187
 
 
188
  /* TODO: Implement something that resizes the terminal without
 
189
   *       crashes.
 
190
  #ifndef WIN32
280
191
    if ( sh ) sh->resizeTerminal();
281
 
#endif
282
 
}
283
 
 
284
 
bool QgsGrassTools::loadConfig(QString filePath)
285
 
{
286
 
    #ifdef QGISDEBUG
287
 
    std::cerr << "QgsGrassTools::loadConfig(): " << filePath.toLocal8Bit().data() << std::endl;
288
 
    #endif
289
 
    mModulesListView->clear();
290
 
 
291
 
    QFile file ( filePath );
292
 
 
293
 
    if ( !file.exists() ) {
294
 
        QMessageBox::warning( 0, tr("Warning"), tr("The config file (") + filePath + tr(") not found.") );
295
 
        return false;
296
 
    }
297
 
    if ( ! file.open( QIODevice::ReadOnly ) ) {
298
 
        QMessageBox::warning( 0, tr("Warning"), tr("Cannot open config file (") + filePath + tr(")") );
299
 
        return false;
300
 
    }
301
 
    
302
 
    QDomDocument doc ( "qgisgrass" );
303
 
    QString err;
304
 
    int line, column;
305
 
    if ( !doc.setContent( &file,  &err, &line, &column ) ) {
306
 
        QString errmsg = tr("Cannot read config file (") + filePath + "):\n" + err + tr("\nat line ")  
307
 
                         + QString::number(line) + tr(" column ") + QString::number(column);
308
 
        std::cerr << errmsg.toLocal8Bit().data() << std::endl;
309
 
        QMessageBox::warning( 0, tr("Warning"), errmsg );
310
 
        file.close();
311
 
        return false;
312
 
    }
313
 
 
314
 
    QDomElement docElem = doc.documentElement();
315
 
    QDomNodeList modulesNodes = docElem.elementsByTagName ( "modules" );
316
 
 
317
 
    if ( modulesNodes.count() == 0 ) {
318
 
         file.close();
319
 
         return false;
320
 
    }
321
 
 
322
 
    QDomNode modulesNode = modulesNodes.item(0);
323
 
    QDomElement modulesElem = modulesNode.toElement();
324
 
    
325
 
    // Go through the sections and modules and add them to the list view
326
 
    addModules ( 0, modulesElem );
327
 
    
328
 
    file.close();
329
 
}
330
 
 
331
 
void QgsGrassTools::addModules (  Q3ListViewItem *parent, QDomElement &element )
332
 
{
333
 
    QDomNode n = element.firstChild();
334
 
 
335
 
    Q3ListViewItem *item;
336
 
    Q3ListViewItem *lastItem = 0;
337
 
    while( !n.isNull() ) {
338
 
        QDomElement e = n.toElement();
339
 
        if( !e.isNull() ) {
340
 
            //std::cout << "tag = " << e.tagName() << std::endl;
341
 
 
342
 
            if ( e.tagName() == "section" && e.tagName() == "grass" ) {
343
 
                std::cout << "Unknown tag: " << e.tagName().toLocal8Bit().data() << std::endl;
344
 
                continue;
345
 
            }
346
 
            
347
 
            if ( parent ) {
348
 
                item = new Q3ListViewItem( parent, lastItem );
349
 
            } else {
350
 
                item = new Q3ListViewItem( mModulesListView, lastItem );
351
 
            }
352
 
 
353
 
            if ( e.tagName() == "section" ) {
354
 
                QString label = e.attribute("label");
355
 
                std::cout << "label = " << label.toLocal8Bit().data() << std::endl;
356
 
                item->setText( 0, label );
357
 
                item->setOpen(true); // for debuging to spare one click
358
 
 
359
 
                addModules ( item, e );
360
 
                
361
 
                lastItem = item;
362
 
            } else if ( e.tagName() == "grass" ) { // GRASS module
363
 
                QString name = e.attribute("name");
364
 
                std::cout << "name = " << name.toLocal8Bit().data() << std::endl;
365
 
 
366
 
                //QString path = QgsApplication::pkgDataPath() + "/grass/modules/" + name;
367
 
                QString path = mAppDir + "/share/qgis/grass/modules/" + name;
368
 
                QString label = QgsGrassModule::label ( path );
369
 
                QPixmap pixmap = QgsGrassModule::pixmap ( path, 25 ); 
370
 
 
371
 
                item->setText( 0, label );
372
 
                item->setPixmap( 0, pixmap );
373
 
                item->setText( 1, name );
374
 
                lastItem = item;
375
 
            }
376
 
            // Show items during loading
377
 
            mModulesListView->repaint();
378
 
        }
379
 
        n = n.nextSibling();
380
 
    }
 
192
  #endif
 
193
  */
 
194
}
 
195
 
 
196
bool QgsGrassTools::loadConfig( QString filePath )
 
197
{
 
198
  QgsDebugMsg( filePath );
 
199
  mModulesTree->clear();
 
200
  mModulesTree->setIconSize( QSize( 80, 22 ) );
 
201
 
 
202
  QFile file( filePath );
 
203
 
 
204
  if ( !file.exists() )
 
205
  {
 
206
    QMessageBox::warning( 0, tr( "Warning" ), tr( "The config file (%1) not found." ).arg( filePath ) );
 
207
    return false;
 
208
  }
 
209
  if ( ! file.open( QIODevice::ReadOnly ) )
 
210
  {
 
211
    QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open config file (%1)." ).arg( filePath ) );
 
212
    return false;
 
213
  }
 
214
 
 
215
  QDomDocument doc( "qgisgrass" );
 
216
  QString err;
 
217
  int line, column;
 
218
  if ( !doc.setContent( &file,  &err, &line, &column ) )
 
219
  {
 
220
    QString errmsg = tr( "Cannot read config file (%1):" ).arg( filePath )
 
221
                     + tr( "\n%1\nat line %2 column %3" ).arg( err ).arg( line ).arg( column );
 
222
    QgsDebugMsg( errmsg );
 
223
    QMessageBox::warning( 0, tr( "Warning" ), errmsg );
 
224
    file.close();
 
225
    return false;
 
226
  }
 
227
 
 
228
  QDomElement docElem = doc.documentElement();
 
229
  QDomNodeList modulesNodes = docElem.elementsByTagName( "modules" );
 
230
 
 
231
  if ( modulesNodes.count() == 0 )
 
232
  {
 
233
    file.close();
 
234
    return false;
 
235
  }
 
236
 
 
237
  QDomNode modulesNode = modulesNodes.item( 0 );
 
238
  QDomElement modulesElem = modulesNode.toElement();
 
239
 
 
240
  // Go through the sections and modules and add them to the list view
 
241
  addModules( 0, modulesElem );
 
242
 
 
243
  mModulesTree->topLevelItem( 0 )->setExpanded( true );
 
244
 
 
245
  file.close();
 
246
  return true;
 
247
}
 
248
 
 
249
void QgsGrassTools::addModules( QTreeWidgetItem *parent, QDomElement &element )
 
250
{
 
251
  QDomNode n = element.firstChild();
 
252
 
 
253
  QTreeWidgetItem *item;
 
254
  QTreeWidgetItem *lastItem = 0;
 
255
  while ( !n.isNull() )
 
256
  {
 
257
    QDomElement e = n.toElement();
 
258
    if ( !e.isNull() )
 
259
    {
 
260
// QgsDebugMsg(QString("tag = %1").arg(e.tagName()));
 
261
 
 
262
      if ( e.tagName() == "section" && e.tagName() == "grass" )
 
263
      {
 
264
        QgsDebugMsg( QString( "Unknown tag: %1" ).arg( e.tagName() ) );
 
265
        continue;
 
266
      }
 
267
 
 
268
      if ( parent )
 
269
      {
 
270
        item = new QTreeWidgetItem( parent, lastItem );
 
271
      }
 
272
      else
 
273
      {
 
274
        item = new QTreeWidgetItem( mModulesTree, lastItem );
 
275
      }
 
276
 
 
277
      if ( e.tagName() == "section" )
 
278
      {
 
279
        QString label = e.attribute( "label" );
 
280
        QgsDebugMsg( QString( "label = %1" ).arg( label ) );
 
281
        item->setText( 0, label );
 
282
        item->setExpanded( false );
 
283
 
 
284
        addModules( item, e );
 
285
 
 
286
        lastItem = item;
 
287
      }
 
288
      else if ( e.tagName() == "grass" )
 
289
      { // GRASS module
 
290
        QString name = e.attribute( "name" );
 
291
        QgsDebugMsg( QString( "name = %1" ).arg( name ) );
 
292
 
 
293
        QString path = QgsApplication::pkgDataPath() + "/grass/modules/" + name;
 
294
        QString label = QgsGrassModule::label( path );
 
295
        QPixmap pixmap = QgsGrassModule::pixmap( path, 25 );
 
296
 
 
297
        item->setText( 0, name + " - " + label );
 
298
        item->setIcon( 0, QIcon( pixmap ) );
 
299
        item->setText( 1, name );
 
300
        lastItem = item;
 
301
 
 
302
 
 
303
        //
 
304
        // Experimental work by Tim - add this item to our list model
 
305
        //
 
306
        QStandardItem * mypDetailItem = new QStandardItem( name );
 
307
        mypDetailItem->setData( name, Qt::UserRole + 1 ); //for calling runModule later
 
308
        QString mySearchText = name + " - " + label;
 
309
        mypDetailItem->setData( mySearchText, Qt::UserRole + 2 ); //for filtering later
 
310
        mypDetailItem->setData( pixmap, Qt::DecorationRole );
 
311
        mypDetailItem->setCheckable( false );
 
312
        mypDetailItem->setEditable( false );
 
313
        // setData in the delegate with a variantised QgsDetailedItemData
 
314
        QgsDetailedItemData myData;
 
315
        myData.setTitle( name );
 
316
        myData.setDetail( label );
 
317
        myData.setIcon( pixmap );
 
318
        myData.setCheckable( false );
 
319
        myData.setRenderAsWidget( true );
 
320
        QVariant myVariant = qVariantFromValue( myData );
 
321
        mypDetailItem->setData( myVariant, Qt::UserRole );
 
322
        mModelTools->appendRow( mypDetailItem );
 
323
        //
 
324
        // End of experimental work by Tim
 
325
        //
 
326
      }
 
327
    }
 
328
    n = n.nextSibling();
 
329
  }
381
330
}
382
331
 
383
332
void QgsGrassTools::mapsetChanged()
384
333
{
385
 
    #ifdef QGISDEBUG
386
 
    std::cerr << "QgsGrassTools::mapsetChanged()" << std::endl;
387
 
    #endif
388
 
 
389
 
    QString title = tr("GRASS Tools: ") + QgsGrass::getDefaultLocation()
390
 
                + "/" + QgsGrass::getDefaultMapset();
391
 
    setCaption(title);
392
 
 
393
 
    closeTools();
394
 
    mBrowser->setLocation( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation() );
 
334
  QgsDebugMsg( "entered." );
 
335
 
 
336
  QString title = tr( "GRASS Tools: %1/%2" ).arg( QgsGrass::getDefaultLocation() ).arg( QgsGrass::getDefaultMapset() );
 
337
  setWindowTitle( title );
 
338
 
 
339
  closeTools();
 
340
  mBrowser->setLocation( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation() );
395
341
}
396
342
 
397
343
QgsGrassTools::~QgsGrassTools()
398
344
{
399
 
    #ifdef QGISDEBUG
400
 
    std::cerr << "QgsGrassTools::~QgsGrassTools()" << std::endl;
401
 
    #endif
402
 
    saveWindowLocation();
403
 
}
404
 
 
405
 
QString QgsGrassTools::appDir(void)
406
 
{
407
 
    //return QgsApplication::applicationDirPath();
408
 
    return mAppDir;
409
 
}
410
 
 
411
 
void QgsGrassTools::close(void)
412
 
{
413
 
    saveWindowLocation();
414
 
    hide();
415
 
}
416
 
 
417
 
void QgsGrassTools::closeEvent(QCloseEvent *e)
418
 
{
419
 
    saveWindowLocation();
420
 
    e->accept();
 
345
  QgsDebugMsg( "entered." );
 
346
  saveWindowLocation();
 
347
}
 
348
 
 
349
QString QgsGrassTools::appDir( void )
 
350
{
 
351
#if defined(WIN32)
 
352
  return QgsGrass::shortPath( QgsApplication::applicationDirPath() );
 
353
#else
 
354
  return QgsApplication::applicationDirPath();
 
355
#endif
 
356
}
 
357
 
 
358
void QgsGrassTools::close( void )
 
359
{
 
360
  saveWindowLocation();
 
361
  hide();
 
362
}
 
363
 
 
364
void QgsGrassTools::closeEvent( QCloseEvent *e )
 
365
{
 
366
  saveWindowLocation();
 
367
  e->accept();
421
368
}
422
369
 
423
370
void QgsGrassTools::restorePosition()
424
371
{
425
 
    QSettings settings;
426
 
    int ww = settings.readNumEntry("/GRASS/windows/tools/w", 250);
427
 
    int wh = settings.readNumEntry("/GRASS/windows/tools/h", 300);
428
 
    int wx = settings.readNumEntry("/GRASS/windows/tools/x", 100);
429
 
    int wy = settings.readNumEntry("/GRASS/windows/tools/y", 100);
430
 
    resize(ww,wh);
431
 
    move(wx,wy);
432
 
    show();
 
372
  QSettings settings;
 
373
  restoreGeometry( settings.value( "/GRASS/windows/tools/geometry" ).toByteArray() );
 
374
  show();
433
375
}
434
376
 
435
377
void QgsGrassTools::saveWindowLocation()
436
378
{
437
 
    QSettings settings;
438
 
    QPoint p = this->pos();
439
 
    QSize s = this->size();
440
 
    settings.writeEntry("/GRASS/windows/tools/x", p.x());
441
 
    settings.writeEntry("/GRASS/windows/tools/y", p.y());
442
 
    settings.writeEntry("/GRASS/windows/tools/w", s.width());
443
 
    settings.writeEntry("/GRASS/windows/tools/h", s.height());
 
379
  QSettings settings;
 
380
  settings.setValue( "/GRASS/windows/tools/geometry", saveGeometry() );
444
381
}
445
382
 
446
383
void QgsGrassTools::emitRegionChanged()
447
384
{
448
 
    #ifdef QGISDEBUG
449
 
    std::cerr << "QgsGrassTools::emitRegionChanged()" << std::endl;
450
 
    #endif
451
 
    emit regionChanged();
 
385
  QgsDebugMsg( "entered." );
 
386
  emit regionChanged();
452
387
}
453
388
 
454
389
void QgsGrassTools::closeTools()
455
390
{
456
 
    #ifdef QGISDEBUG
457
 
    std::cerr << "QgsGrassTools::closeTools()" << std::endl;
458
 
    #endif
459
 
 
460
 
    for ( int i = mTabWidget->count()-1; i > 1; i-- )
461
 
    {
462
 
         delete mTabWidget->widget(i);
463
 
         mTabWidget->removeTab(i);
464
 
    }
465
 
}
 
391
  QgsDebugMsg( "entered." );
 
392
 
 
393
  for ( int i = mTabWidget->count() - 1; i > 2; i-- )
 
394
  {
 
395
    delete mTabWidget->widget( i );
 
396
    mTabWidget->removeTab( i );
 
397
  }
 
398
}
 
399
 
 
400
 
 
401
 
 
402
//
 
403
// Helper function for Tim's experimental model list
 
404
//
 
405
 
 
406
void QgsGrassTools::on_mFilterInput_textChanged( QString theText )
 
407
{
 
408
  QgsDebugMsg( "GRASS modules filter changed to :" + theText );
 
409
  QRegExp::PatternSyntax mySyntax = QRegExp::PatternSyntax( QRegExp::RegExp );
 
410
  Qt::CaseSensitivity myCaseSensitivity = Qt::CaseInsensitive;
 
411
  QRegExp myRegExp( theText, myCaseSensitivity, mySyntax );
 
412
  mModelProxy->setFilterRegExp( myRegExp );
 
413
}
 
414
 
 
415
void QgsGrassTools::listItemClicked( const QModelIndex &theIndex )
 
416
{
 
417
  if ( theIndex.column() == 0 )
 
418
  {
 
419
    //
 
420
    // If the model has been filtered, the index row in the proxy wont match
 
421
    // the index row in the underlying model so we need to jump through this
 
422
    // little hoop to get the correct item
 
423
    //
 
424
    QStandardItem * mypItem =
 
425
      mModelTools->findItems( theIndex.data( Qt::DisplayRole ).toString() ).first();
 
426
    QString myModuleName = mypItem->data( Qt::UserRole + 1 ).toString();
 
427
    runModule( myModuleName );
 
428
  }
 
429
}
 
430