~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to plugins/grass/qgsgrasstools.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                              qgsgrasstools.cpp
3
 
                             -------------------
4
 
    begin                : March, 2005
5
 
    copyright            : (C) 2005 by Radim Blazek
6
 
    email                : blazek@itc.it
7
 
 ***************************************************************************/
8
 
/***************************************************************************
9
 
 *                                                                         *
10
 
 *   This program is free software; you can redistribute it and/or modify  *
11
 
 *   it under the terms of the GNU General Public License as published by  *
12
 
 *   the Free Software Foundation; either version 2 of the License, or     *
13
 
 *   (at your option) any later version.                                   *
14
 
 *                                                                         *
15
 
 ***************************************************************************/
16
 
#include <iostream>
17
 
 
18
 
#include <qapplication.h>
19
 
#include <qdir.h>
20
 
#include <qfile.h>
21
 
#include <qfiledialog.h> 
22
 
#include <qsettings.h>
23
 
#include <qpixmap.h>
24
 
#include <qlistbox.h>
25
 
#include <qstringlist.h>
26
 
#include <qlabel.h>
27
 
#include <qcombobox.h>
28
 
#include <qspinbox.h>
29
 
#include <qmessagebox.h>
30
 
#include <qinputdialog.h>
31
 
#include <qsettings.h>
32
 
#include <qpainter.h>
33
 
#include <qpixmap.h>
34
 
#include <qpen.h>
35
 
#include <qpointarray.h>
36
 
#include <qcursor.h>
37
 
#include <qnamespace.h>
38
 
#include <qlistview.h>
39
 
#include <qcolordialog.h>
40
 
#include <qtable.h>
41
 
#include <qstatusbar.h>
42
 
#include <qevent.h>
43
 
#include <qpoint.h>
44
 
#include <qsize.h>
45
 
#include <qdom.h>
46
 
#include <qtabwidget.h>
47
 
#include <qlayout.h>
48
 
#include <qcheckbox.h>
49
 
#include <qprocess.h>
50
 
#include <qiconset.h>
51
 
 
52
 
#include "../../src/qgis.h"
53
 
#include "../../src/qgsmapcanvas.h"
54
 
#include "../../src/qgsmaplayer.h"
55
 
#include "../../src/qgsvectorlayer.h"
56
 
#include "../../src/qgsdataprovider.h"
57
 
#include "../../src/qgsfield.h"
58
 
#include "../../src/qgsfeatureattribute.h"
59
 
 
60
 
extern "C" {
61
 
#include <gis.h>
62
 
#include <Vect.h>
63
 
}
64
 
 
65
 
#include "../../providers/grass/qgsgrass.h"
66
 
#include "../../providers/grass/qgsgrassprovider.h"
67
 
#include "qgsgrassattributes.h"
68
 
#include "qgsgrasstools.h"
69
 
#include "qgsgrassmodule.h"
70
 
 
71
 
QgsGrassTools::QgsGrassTools ( QgisApp *qgisApp, QgisIface *iface, 
72
 
                             QWidget * parent, const char * name, WFlags f )
73
 
             :QgsGrassToolsBase ( parent, name, f )
74
 
{
75
 
    #ifdef QGISDEBUG
76
 
    std::cerr << "QgsGrassTools()" << std::endl;
77
 
    #endif
78
 
 
79
 
    mQgisApp = qgisApp;
80
 
    mIface = iface;
81
 
    mCanvas = mIface->getMapCanvas();
82
 
 
83
 
    // Set list view
84
 
    mModulesListView->setColumnText(0,"Modules");
85
 
    mModulesListView->clear();
86
 
    mModulesListView->setSorting(-1);
87
 
    mModulesListView->setRootIsDecorated(true);
88
 
    mModulesListView->setResizeMode(QListView::AllColumns);
89
 
    mModulesListView->header()->hide();
90
 
 
91
 
    connect( mModulesListView, SIGNAL(clicked(QListViewItem *)), 
92
 
                         this, SLOT(moduleClicked( QListViewItem *)) );
93
 
 
94
 
#if defined(WIN32) || defined(Q_OS_MACX)
95
 
    mAppDir = qApp->applicationDirPath();
96
 
#else
97
 
    mAppDir = PREFIX;
98
 
#endif
99
 
 
100
 
    QString conf = mAppDir + "/share/qgis/grass/config/default.qgc";
101
 
    loadConfig ( conf );
102
 
    statusBar()->hide();
103
 
    restorePosition();
104
 
}
105
 
 
106
 
void QgsGrassTools::moduleClicked( QListViewItem * item )
107
 
{
108
 
    if ( !item ) return;
109
 
 
110
 
    QString name = item->text(1);
111
 
    //std::cerr << "name = " << name << std::endl;
112
 
    
113
 
    if ( name.length() == 0 ) return;  // Section
114
 
    
115
 
    QString path = mAppDir + "/share/qgis/grass/modules/" + name;
116
 
    QgsGrassModule *m = new QgsGrassModule ( this, mQgisApp, mIface, path, mTabWidget );
117
 
    //mTabWidget->addTab ( m, item->text(0) );
118
 
    
119
 
    QPixmap pixmap = QgsGrassModule::pixmap ( path, 25 ); 
120
 
    QIconSet is;
121
 
    is.setPixmap ( pixmap, QIconSet::Small, QIconSet::Normal );
122
 
    mTabWidget->addTab ( (QWidget*)m, is, "" );
123
 
                
124
 
    mTabWidget->setCurrentPage ( mTabWidget->count()-1 );
125
 
}
126
 
 
127
 
bool QgsGrassTools::loadConfig(QString filePath)
128
 
{
129
 
    #ifdef QGISDEBUG
130
 
    std::cerr << "QgsGrassTools::loadConfig(): " << filePath << std::endl;
131
 
    #endif
132
 
    mModulesListView->clear();
133
 
 
134
 
    QFile file ( filePath );
135
 
 
136
 
    if ( !file.exists() ) {
137
 
        QMessageBox::warning( 0, "Warning", "The config file (" + filePath + ") not found." );
138
 
        return false;
139
 
    }
140
 
    if ( ! file.open( IO_ReadOnly ) ) {
141
 
        QMessageBox::warning( 0, "Warning", "Cannot open config file (" + filePath + ")" );
142
 
        return false;
143
 
    }
144
 
    
145
 
    QDomDocument doc ( "qgisgrass" );
146
 
    QString err;
147
 
    int line, column;
148
 
    if ( !doc.setContent( &file,  &err, &line, &column ) ) {
149
 
        QString errmsg = "Cannot read config file (" + filePath + "):\n" + err + "\nat line "  
150
 
                         + QString::number(line) + " column " + QString::number(column);
151
 
        std::cerr << errmsg << std::endl;
152
 
        QMessageBox::warning( 0, "Warning", errmsg );
153
 
        file.close();
154
 
        return false;
155
 
    }
156
 
 
157
 
    QDomElement docElem = doc.documentElement();
158
 
    QDomNodeList modulesNodes = docElem.elementsByTagName ( "modules" );
159
 
 
160
 
    if ( modulesNodes.count() == 0 ) {
161
 
         file.close();
162
 
         return false;
163
 
    }
164
 
 
165
 
    QDomNode modulesNode = modulesNodes.item(0);
166
 
    QDomElement modulesElem = modulesNode.toElement();
167
 
    
168
 
    // Go through the sections and modules and add them to the list view
169
 
    addModules ( 0, modulesElem );
170
 
    
171
 
    file.close();
172
 
}
173
 
 
174
 
void QgsGrassTools::addModules (  QListViewItem *parent, QDomElement &element )
175
 
{
176
 
    QDomNode n = element.firstChild();
177
 
 
178
 
    QListViewItem *item;
179
 
    QListViewItem *lastItem = 0;
180
 
    while( !n.isNull() ) {
181
 
        QDomElement e = n.toElement();
182
 
        if( !e.isNull() ) {
183
 
            //std::cout << "tag = " << e.tagName() << std::endl;
184
 
 
185
 
            if ( e.tagName() == "section" && e.tagName() == "grass" ) {
186
 
                std::cout << "Unknown tag: " << e.tagName() << std::endl;
187
 
                continue;
188
 
            }
189
 
            
190
 
            if ( parent ) {
191
 
                item = new QListViewItem( parent, lastItem );
192
 
            } else {
193
 
                item = new QListViewItem( mModulesListView, lastItem );
194
 
            }
195
 
 
196
 
            if ( e.tagName() == "section" ) {
197
 
                QString label = e.attribute("label");
198
 
                std::cout << "label = " << label << std::endl;
199
 
                item->setText( 0, label );
200
 
                item->setOpen(true); // for debuging to spare one click
201
 
 
202
 
                addModules ( item, e );
203
 
                
204
 
                lastItem = item;
205
 
            } else if ( e.tagName() == "grass" ) { // GRASS module
206
 
                QString name = e.attribute("name");
207
 
                std::cout << "name = " << name << std::endl;
208
 
 
209
 
                QString path = mAppDir + "/share/qgis/grass/modules/" + name;
210
 
                QString label = QgsGrassModule::label ( path );
211
 
                QPixmap pixmap = QgsGrassModule::pixmap ( path, 25 ); 
212
 
 
213
 
                item->setText( 0, label );
214
 
                item->setPixmap( 0, pixmap );
215
 
                item->setText( 1, name );
216
 
                lastItem = item;
217
 
            }
218
 
        }
219
 
        n = n.nextSibling();
220
 
    }
221
 
}
222
 
 
223
 
QgsGrassTools::~QgsGrassTools()
224
 
{
225
 
    #ifdef QGISDEBUG
226
 
    std::cerr << "QgsGrassTools::~QgsGrassTools()" << std::endl;
227
 
    #endif
228
 
    saveWindowLocation();
229
 
}
230
 
 
231
 
QString QgsGrassTools::appDir(void)
232
 
{
233
 
    return mAppDir;
234
 
}
235
 
 
236
 
void QgsGrassTools::close(void)
237
 
{
238
 
    saveWindowLocation();
239
 
    hide();
240
 
}
241
 
 
242
 
void QgsGrassTools::closeEvent(QCloseEvent *e)
243
 
{
244
 
    saveWindowLocation();
245
 
    e->accept();
246
 
}
247
 
 
248
 
void QgsGrassTools::restorePosition()
249
 
{
250
 
    QSettings settings;
251
 
    int ww = settings.readNumEntry("/qgis/grass/windows/tools/w", 250);
252
 
    int wh = settings.readNumEntry("/qgis/grass/windows/tools/h", 300);
253
 
    int wx = settings.readNumEntry("/qgis/grass/windows/tools/x", 100);
254
 
    int wy = settings.readNumEntry("/qgis/grass/windows/tools/y", 100);
255
 
    resize(ww,wh);
256
 
    move(wx,wy);
257
 
    QgsGrassToolsBase::show();
258
 
}
259
 
 
260
 
void QgsGrassTools::saveWindowLocation()
261
 
{
262
 
    QSettings settings;
263
 
    QPoint p = this->pos();
264
 
    QSize s = this->size();
265
 
    settings.writeEntry("/qgis/grass/windows/tools/x", p.x());
266
 
    settings.writeEntry("/qgis/grass/windows/tools/y", p.y());
267
 
    settings.writeEntry("/qgis/grass/windows/tools/w", s.width());
268
 
    settings.writeEntry("/qgis/grass/windows/tools/h", s.height());
269
 
}
270
 
 
271
 
 
272