Skip to content

File tree

6 files changed

+1062
-1
lines changed

6 files changed

+1062
-1
lines changed
 

‎src/plugins/grass/Makefile.am

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ plugin_LTLIBRARIES = grassplugin.la
3535

3636
plugin_MOC = qgsgrassplugin.moc.cpp \
3737
qgsgrassselect.moc.cpp \
38+
qgsgrassbrowser.moc.cpp \
3839
qgsgrassedit.moc.cpp \
3940
qgsgrasstools.moc.cpp \
41+
qgsgrassmodel.moc.cpp \
4042
qgsgrassmapcalc.moc.cpp \
4143
qgsgrassmodule.moc.cpp \
4244
qgsgrassnewmapset.moc.cpp \
@@ -68,10 +70,14 @@ grassplugin_la_SOURCES = qgsgrassplugin.cpp \
6870
qgsgrassplugin.h \
6971
qgsgrassselect.cpp \
7072
qgsgrassselect.h \
73+
qgsgrassbrowser.cpp \
74+
qgsgrassbrowser.h \
7175
qgsgrassedit.cpp \
7276
qgsgrassedit.h \
7377
qgsgrasstools.cpp \
7478
qgsgrasstools.h \
79+
qgsgrassmodel.cpp \
80+
qgsgrassmodel.h \
7581
qgsgrassmapcalc.cpp \
7682
qgsgrassmapcalc.h \
7783
qgsgrassmodule.cpp \

‎src/plugins/grass/qgsgrassbrowser.cpp

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*******************************************************************
2+
qgsgrassbrowser.cpp
3+
-------------------
4+
begin : February, 2006
5+
copyright : (C) 2006 by Radim Blazek
6+
email : radim.blazek@gmail.com
7+
********************************************************************/
8+
/********************************************************************
9+
This program is free software; you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation; either version 2 of the License, or
12+
(at your option) any later version.
13+
*******************************************************************/
14+
#include <iostream>
15+
#include <vector>
16+
17+
#include <QApplication>
18+
#include <QStyle>
19+
#include <qdir.h>
20+
#include <qfile.h>
21+
#include <qsettings.h>
22+
#include <qstringlist.h>
23+
#include <qmessagebox.h>
24+
#include <qpainter.h>
25+
#include <qpixmap.h>
26+
#include <qnamespace.h>
27+
#include <qevent.h>
28+
#include <qsize.h>
29+
#include <qicon.h>
30+
#include <QTreeView>
31+
#include <QHeaderView>
32+
#include <QMainWindow>
33+
#include <QActionGroup>
34+
#include <QToolBar>
35+
#include <QAction>
36+
#include <QTextBrowser>
37+
#include <QSplitter>
38+
39+
#include "qgis.h"
40+
#include "qgsapplication.h"
41+
42+
extern "C" {
43+
#include <grass/gis.h>
44+
#include <grass/Vect.h>
45+
}
46+
47+
#include "../../src/providers/grass/qgsgrass.h"
48+
#include "qgsgrassmodel.h"
49+
#include "qgsgrassbrowser.h"
50+
51+
QgsGrassBrowser::QgsGrassBrowser ( QgisIface *iface,
52+
QWidget * parent, Qt::WFlags f )
53+
:mIface(iface), QMainWindow(parent, Qt::WType_Dialog)
54+
{
55+
#ifdef QGISDEBUG
56+
std::cerr << "QgsGrassBrowser()" << std::endl;
57+
#endif
58+
59+
QActionGroup *ag = new QActionGroup ( this );
60+
QToolBar *tb = addToolBar(tr("Tools"));
61+
62+
QString myIconPath = QgsApplication::themePath() + "/grass/";
63+
mActionAddMap = new QAction(
64+
QIcon(myIconPath+"grass_add_map.png"),
65+
tr("Add selected map to canvas"), this);
66+
mActionAddMap->setEnabled(false);
67+
ag->addAction ( mActionAddMap );
68+
tb->addAction ( mActionAddMap );
69+
connect ( mActionAddMap, SIGNAL(triggered()), this, SLOT(addMap()) );
70+
71+
mActionRefresh = new QAction(
72+
QIcon(myIconPath+"grass_refresh.png"),
73+
tr("Refresh"), this);
74+
ag->addAction ( mActionRefresh );
75+
tb->addAction ( mActionRefresh );
76+
connect ( mActionRefresh, SIGNAL(triggered()), this, SLOT(refresh()) );
77+
78+
// Add model
79+
mModel = new QgsGrassModel ( this );
80+
81+
mTree = new QTreeView(0);
82+
mTree->header()->hide();
83+
mTree->setModel(mModel);
84+
85+
mTextBrowser = new QTextBrowser(0);
86+
mTextBrowser->setTextFormat(Qt::RichText);
87+
mTextBrowser->setReadOnly(TRUE);
88+
89+
mSplitter = new QSplitter(0);
90+
mSplitter->addWidget(mTree);
91+
mSplitter->addWidget(mTextBrowser);
92+
93+
this->setCentralWidget(mSplitter);
94+
95+
connect ( mTree->selectionModel(),
96+
SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
97+
this, SLOT(selectionChanged(QItemSelection,QItemSelection)) );
98+
99+
connect ( mTree->selectionModel(),
100+
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
101+
this, SLOT(currentChanged(QModelIndex,QModelIndex)) );
102+
}
103+
104+
QgsGrassBrowser::~QgsGrassBrowser() { }
105+
106+
void QgsGrassBrowser::refresh()
107+
{
108+
#ifdef QGISDEBUG
109+
std::cerr << "QgsGrassBrowser::refresh()" << std::endl;
110+
#endif
111+
112+
mModel->refresh();
113+
mTree->update();
114+
}
115+
116+
void QgsGrassBrowser::addMap()
117+
{
118+
#ifdef QGISDEBUG
119+
std::cerr << "QgsGrassBrowser::addMap()" << std::endl;
120+
#endif
121+
122+
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
123+
bool mapSelected = false;
124+
125+
QList<QModelIndex>::const_iterator it = indexes.begin();
126+
for (; it != indexes.end(); ++it)
127+
{
128+
int type = mModel->itemType(*it);
129+
QString uri = mModel->uri(*it);
130+
if ( type == QgsGrassModel::Raster )
131+
{
132+
std::cerr << "add raster: " << uri.ascii() << std::endl;
133+
mIface->addRasterLayer( uri );
134+
mapSelected = true;
135+
}
136+
else if ( type == QgsGrassModel::VectorLayer )
137+
{
138+
// TODO: common method for vector names
139+
QStringList split = QStringList::split ( '/', uri );
140+
QString layer = split.last();
141+
split.pop_back(); // map
142+
QString vector = split.last();
143+
mIface->addVectorLayer( uri, layer, "grass");
144+
std::cerr << "add vector: " << uri.ascii() << std::endl;
145+
mapSelected = true;
146+
}
147+
}
148+
}
149+
150+
void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
151+
{
152+
#ifdef QGISDEBUG
153+
std::cerr << "QgsGrassBrowser::selectionChanged()" << std::endl;
154+
#endif
155+
156+
mActionAddMap->setEnabled(false);
157+
158+
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
159+
160+
mTextBrowser->clear();
161+
162+
QList<QModelIndex>::const_iterator it = indexes.begin();
163+
for (; it != indexes.end(); ++it)
164+
{
165+
mTextBrowser->append ( mModel->itemInfo(*it) );
166+
167+
int type = mModel->itemType(*it);
168+
if ( type == QgsGrassModel::Raster || type == QgsGrassModel::VectorLayer )
169+
{
170+
mActionAddMap->setEnabled(true);
171+
}
172+
}
173+
174+
}
175+
176+
void QgsGrassBrowser::currentChanged(const QModelIndex & current, const QModelIndex & previous)
177+
{
178+
#ifdef QGISDEBUG
179+
std::cerr << "QgsGrassBrowser::currentChanged()" << std::endl;
180+
#endif
181+
}
182+

‎src/plugins/grass/qgsgrassbrowser.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/***************************************************************************
2+
qgsgrasstree.h
3+
-------------------
4+
begin : February, 2006
5+
copyright : (C) 2006 by Radim Blazek
6+
email : radim.blazek@gmail.com
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+
#ifndef QGSGRASSBROWSER_H
17+
#define QGSGRASSBROWSER_H
18+
19+
#include <QMainWindow>
20+
class QSplitter;
21+
class QAction;
22+
class QTreeView;
23+
class QTextBrowser;
24+
class QDirModel;
25+
26+
#include "qgisiface.h"
27+
#include "qgsgrassmodel.h"
28+
29+
/*! \class QgsGrassBrowser
30+
* \brief Model representing GRASS location structure.
31+
*/
32+
class QgsGrassBrowser: public QMainWindow
33+
{
34+
Q_OBJECT;
35+
36+
public:
37+
//! Constructor
38+
QgsGrassBrowser ( QgisIface *iface, QWidget * parent = 0, Qt::WFlags f = 0 );
39+
40+
//! Destructor
41+
~QgsGrassBrowser();
42+
43+
public slots:
44+
// Add selected map to canvas
45+
void addMap();
46+
47+
// Refresh model
48+
void refresh();
49+
50+
// Called when tree selection changes
51+
void selectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
52+
void currentChanged(const QModelIndex & current, const QModelIndex & previous);
53+
54+
private:
55+
QgisIface *mIface;
56+
57+
//! Current GISBASE
58+
QString mGisbase;
59+
60+
//! Current LOCATION_NAME
61+
QString mLocation;
62+
63+
// ! Data model
64+
QgsGrassModel *mModel;
65+
66+
QSplitter *mSplitter;
67+
68+
QTreeView *mTree;
69+
70+
QTextBrowser *mTextBrowser;
71+
72+
//! Actions
73+
QAction *mActionAddMap;
74+
QAction *mActionRefresh;
75+
};
76+
77+
#endif // QGSGRASSBROWSER_H

‎src/plugins/grass/qgsgrassmodel.cpp

Lines changed: 671 additions & 0 deletions
Large diffs are not rendered by default.

‎src/plugins/grass/qgsgrassmodel.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/***************************************************************************
2+
qgsgrasstree.h
3+
-------------------
4+
begin : February, 2006
5+
copyright : (C) 2006 by Radim Blazek
6+
email : radim.blazek@gmail.com
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+
#ifndef QGSGRASSMODEL_H
17+
#define QGSGRASSMODEL_H
18+
19+
class QCloseEvent;
20+
class QString;
21+
class QDomNode;
22+
class QDomElement;
23+
class QModelIndex;
24+
class QVariant;
25+
26+
#include <QTreeWidget>
27+
#include <QAbstractItemModel>
28+
#include <QIcon>
29+
30+
class QgsGrassModelItem;
31+
32+
33+
/*! \class QgsGrassModel
34+
* \brief Model representing GRASS location structure.
35+
*/
36+
class QgsGrassModel: public QAbstractItemModel
37+
{
38+
Q_OBJECT;
39+
40+
public:
41+
//! Constructor
42+
QgsGrassModel ( QObject * parent = 0 );
43+
//! Destructor
44+
~QgsGrassModel();
45+
46+
//! Item types
47+
enum ItemType { None, Gisbase, Location, Mapset, Rasters, Vectors, Raster,
48+
Vector, VectorLayer };
49+
50+
//! Set GISBASE and LOCATION_NAME
51+
void setLocation ( const QString &gisbase, const QString &location );
52+
53+
// Refresh populated node
54+
void refresh();
55+
56+
// Refresh item
57+
void refreshItem(QgsGrassModelItem *item) ;
58+
59+
// Remove items missing in the list
60+
void removeItems ( QgsGrassModelItem *item, QStringList list ) ;
61+
62+
// Add items missing in children
63+
void addItems ( QgsGrassModelItem *item, QStringList list, int type ) ;
64+
65+
//! Item type
66+
int itemType(const QModelIndex &index) const;
67+
68+
//! Item URI if it is a map
69+
QString uri(const QModelIndex &index) const;
70+
71+
// Index
72+
QModelIndex index ( QgsGrassModelItem *item ) ;
73+
74+
// Name
75+
QString itemName(const QModelIndex &index);
76+
77+
// Get info in HTML format
78+
QString itemInfo(const QModelIndex &index);
79+
80+
// Reimplemented QAbstractItemModel methods
81+
QModelIndex index ( int row, int column,
82+
const QModelIndex & parent = QModelIndex() ) const;
83+
84+
QModelIndex parent ( const QModelIndex & index ) const;
85+
86+
int rowCount ( const QModelIndex & parent ) const;
87+
88+
int columnCount ( const QModelIndex & parent ) const;
89+
90+
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
91+
92+
QVariant headerData(int section, Qt::Orientation orientation,
93+
int role = Qt::DisplayRole) const;
94+
Qt::ItemFlags flags(const QModelIndex &index) const;
95+
96+
private:
97+
//! Current GISBASE
98+
QString mGisbase;
99+
100+
//! Current LOCATION_NAME
101+
QString mLocation;
102+
103+
//! Root node for current location
104+
QgsGrassModelItem *mRoot;
105+
106+
//! Icons
107+
QIcon mIconDirectory;
108+
QIcon mIconFile;
109+
};
110+
111+
#endif // QGSGRASSMODEL_H

‎src/plugins/grass/qgsgrasstools.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <QTabBar>
5353
#include <QListView>
5454
#include <QProcess>
55+
#include <QHeaderView>
5556

5657
#include "qgis.h"
5758
#include "qgsapplication.h"
@@ -73,12 +74,14 @@ extern "C" {
7374
#include "qgsgrasstools.h"
7475
#include "qgsgrassmodule.h"
7576
#include "qgsgrassshell.h"
77+
#include "qgsgrassmodel.h"
78+
#include "qgsgrassbrowser.h"
7679

7780
QgsGrassToolsTabWidget::QgsGrassToolsTabWidget( QWidget * parent ):
7881
QTabWidget(parent)
7982
{
8083
// Default height seems to be too small for our purpose
81-
int height = 1.5 * tabBar()->iconSize().height();
84+
int height = (int)(1.5 * tabBar()->iconSize().height());
8285
// Max width (see QgsGrassModule::pixmap for hardcoded sizes)
8386
int width = 3*height + 28 + 29;
8487
tabBar()->setIconSize( QSize(width,height) );
@@ -110,6 +113,7 @@ QgsGrassTools::QgsGrassTools ( QgisApp *qgisApp, QgisIface *iface,
110113
QVBoxLayout *layout1 = new QVBoxLayout(this);
111114
layout1->addWidget(mTabWidget);
112115

116+
113117
mModulesListView = new Q3ListView();
114118
mTabWidget->addTab( mModulesListView, "Modules" );
115119
mModulesListView->addColumn("col1",0);
@@ -148,6 +152,16 @@ QgsGrassTools::QgsGrassTools ( QgisApp *qgisApp, QgisIface *iface,
148152

149153
loadConfig ( conf );
150154
//statusBar()->hide();
155+
156+
// Add map browser
157+
// Warning: if browser is on the first page modules are
158+
// displayed over the browser
159+
QgsGrassBrowser *browser = new QgsGrassBrowser ( mIface, this );
160+
mTabWidget->addTab( browser, "Browser" );
161+
162+
// Debug
163+
mTabWidget->setCurrentIndex(1);
164+
151165
}
152166

153167
void QgsGrassTools::moduleClicked( Q3ListViewItem * item )

0 commit comments

Comments
 (0)
Please sign in to comment.