/
qgsgrasstools.cpp
458 lines (386 loc) · 13.1 KB
/
qgsgrasstools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/***************************************************************************
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 <iostream>
#include <qapplication.h>
#include <qdir.h>
#include <qfile.h>
#include <qsettings.h>
#include <qpixmap.h>
#include <q3listbox.h>
#include <qstringlist.h>
#include <qlabel.h>
#include <QComboBox>
#include <qspinbox.h>
#include <qmessagebox.h>
#include <qinputdialog.h>
#include <qsettings.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qpen.h>
#include <q3pointarray.h>
#include <qcursor.h>
#include <qnamespace.h>
#include <q3listview.h>
#include <qcolordialog.h>
#include <q3table.h>
#include <qstatusbar.h>
#include <qevent.h>
#include <qpoint.h>
#include <qsize.h>
#include <qdom.h>
#include <qtabwidget.h>
#include <qlayout.h>
#include <qcheckbox.h>
#include <q3process.h>
#include <qicon.h>
//Added by qt3to4:
#include <QCloseEvent>
#include <QTabBar>
#include <QListView>
#include <QProcess>
#include <QHeaderView>
#include "qgis.h"
#include "qgsapplication.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsvectorlayer.h"
#include "qgsdataprovider.h"
#include "qgsfield.h"
#include "qgsfeatureattribute.h"
extern "C" {
#include <grass/gis.h>
#include <grass/Vect.h>
}
#include "../../src/providers/grass/qgsgrass.h"
#include "../../src/providers/grass/qgsgrassprovider.h"
#include "qgsgrassattributes.h"
#include "qgsgrasstools.h"
#include "qgsgrassmodule.h"
#include "qgsgrassshell.h"
#include "qgsgrassmodel.h"
#include "qgsgrassbrowser.h"
QgsGrassToolsTabWidget::QgsGrassToolsTabWidget( QWidget * parent ):
QTabWidget(parent)
{
// Default height seems to be too small for our purpose
int height = (int)(1.5 * tabBar()->iconSize().height());
// Max width (see QgsGrassModule::pixmap for hardcoded sizes)
int width = 3*height + 28 + 29;
tabBar()->setIconSize( QSize(width,height) );
}
QSize QgsGrassToolsTabWidget::iconSize()
{
return tabBar()->iconSize();
}
QgsGrassToolsTabWidget::~QgsGrassToolsTabWidget() {}
QgsGrassTools::QgsGrassTools ( QgisApp *qgisApp, QgisIface *iface,
QWidget * parent, const char * name, Qt::WFlags f )
:QDialog ( parent )
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassTools()" << std::endl;
#endif
setWindowTitle ( "GRASS Tools" );
// setupUi(this);
mQgisApp = qgisApp;
mIface = iface;
mCanvas = mIface->getMapCanvas();
connect( qApp, SIGNAL(aboutToQuit()),
this, SLOT(closeTools()) );
mTabWidget = new QgsGrassToolsTabWidget (this);
QVBoxLayout *layout1 = new QVBoxLayout(this);
layout1->addWidget(mTabWidget);
mModulesListView = new Q3ListView();
mTabWidget->addTab( mModulesListView, "Modules" );
mModulesListView->addColumn("col1",0);
// 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 *)) );
QString title = "GRASS Tools: " + QgsGrass::getDefaultLocation()
+ "/" + QgsGrass::getDefaultMapset();
setCaption(title);
// Warning: QgsApplication initialized in main.cpp
// is not valid here (static libraries / linking)
#if defined(WIN32) || defined(Q_OS_MACX)
mAppDir = qApp->applicationDirPath();
#else
mAppDir = PREFIX;
#endif
//QString conf = QgsApplication::pkgDataPath() + "/grass/config/default.qgc";
QString conf = mAppDir + "/share/qgis/grass/config/default.qgc";
restorePosition();
// Show before loadConfig() so that user can see loading
mModulesListView->show();
loadConfig ( conf );
//statusBar()->hide();
// Add map browser
// Warning: if browser is on the first page modules are
// displayed over the browser
QgsGrassBrowser *browser = new QgsGrassBrowser ( mIface, this );
mTabWidget->addTab( browser, "Browser" );
connect( browser, SIGNAL(regionChanged()),
this, SLOT(emitRegionChanged()) );
}
void QgsGrassTools::moduleClicked( Q3ListViewItem * item )
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassTools::moduleClicked()" << std::endl;
#endif
if ( !item ) return;
QString name = item->text(1);
//std::cerr << "name = " << name << std::endl;
if ( name.length() == 0 ) return; // Section
//QString path = QgsApplication::pkgDataPath() + "/grass/modules/" + name;
QString path = mAppDir + "/share/qgis/grass/modules/" + name;
#ifdef QGISDEBUG
std::cerr << "path = " << path.ascii() << std::endl;
#endif
QWidget *m;
QgsGrassShell *sh = 0;
if ( name == "shell" )
{
// Set history file
QString mapsetPath = QgsGrass::getDefaultGisdbase() + "/"
+ QgsGrass::getDefaultLocation() + "/"
+ QgsGrass::getDefaultMapset();
// bash
QString hist = "HISTFILE=" + mapsetPath + "/.bash_history";
char *histChar = new char[hist.length()+1];
strcpy ( histChar, const_cast<char *>(hist.ascii()) );
putenv( histChar );
// csh/tcsh
#ifndef WIN32
hist = "histfile=" + mapsetPath + "/.history";
histChar = new char[hist.length()+1];
strcpy ( histChar, const_cast<char *>(hist.ascii()) );
putenv( histChar );
#endif
#ifdef WIN32
// Run MSYS if available
// Note: I was not able to run cmd.exe and command.com
// with QProcess
QString msysPath = mAppDir + "/msys/msys.bat";
QFile file ( msysPath );
if ( !file.exists() )
{
QMessageBox::warning( 0, "Warning",
"Cannot find MSYS (" + msysPath + ")" );
}
else
{
QProcess *proc = new QProcess(this);
proc->start (msysPath);
proc->waitForStarted();
if ( proc->state() != QProcess::Running )
{
QMessageBox::warning( 0, "Warning",
"Cannot start MSYS (" + msysPath + ")" );
}
}
return;
#else
#ifdef HAVE_OPENPTY
sh = new QgsGrassShell(this, mTabWidget);
m = dynamic_cast<QWidget *> ( sh );
#else
QMessageBox::warning( 0, "Warning", "GRASS Shell is not compiled." );
#endif // HAVE_OPENPTY
#endif // ! WIN32
}
else
{
m = dynamic_cast<QWidget *> ( new QgsGrassModule ( this,
mQgisApp, mIface, path, mTabWidget ) );
}
int height = mTabWidget->iconSize().height();
QPixmap pixmap = QgsGrassModule::pixmap ( path, height );
// Icon size in QT4 does not seem to be variable
// -> put smaller icons in the middle
QPixmap pixmap2 ( mTabWidget->iconSize() );
QPalette pal;
pixmap2.fill ( pal.color(QPalette::Window) );
QPainter painter(&pixmap2);
int x = (int) ( (mTabWidget->iconSize().width()-pixmap.width())/2 );
painter.drawPixmap ( x, 0, pixmap );
painter.end();
QIcon is;
is.addPixmap ( pixmap2 );
mTabWidget->addTab ( m, is, "" );
QgsGrassToolsTabWidget tw;
mTabWidget->setCurrentPage ( mTabWidget->count()-1 );
// We must call resize to reset COLUMNS enviroment variable
// used by bash !!!
#ifndef WIN32
if ( sh ) sh->resizeTerminal();
#endif
}
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 = QgsApplication::pkgDataPath() + "/grass/modules/" + name;
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;
}
// Show items during loading
mModulesListView->repaint();
}
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 QgsApplication::applicationDirPath();
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());
}
void QgsGrassTools::emitRegionChanged()
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassTools::emitRegionChanged()" << std::endl;
#endif
emit regionChanged();
}
void QgsGrassTools::closeTools()
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassTools::closeTools()" << std::endl;
#endif
for ( int i = mTabWidget->count()-1; i > 1; i-- )
{
delete mTabWidget->widget(i);
}
}