Skip to content

File tree

4 files changed

+211
-1
lines changed

4 files changed

+211
-1
lines changed
 

‎src/plugins/grass/qgsgrassbrowser.cpp

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ QgsGrassBrowser::QgsGrassBrowser ( QgisIface *iface,
7373
tb->addAction ( mActionAddMap );
7474
connect ( mActionAddMap, SIGNAL(triggered()), this, SLOT(addMap()) );
7575

76+
mActionCopyMap = new QAction(
77+
QIcon(myIconPath+"grass_copy_map.png"),
78+
tr("Copy selected map"), this);
79+
mActionCopyMap->setEnabled(false);
80+
ag->addAction ( mActionCopyMap );
81+
tb->addAction ( mActionCopyMap );
82+
connect ( mActionCopyMap, SIGNAL(triggered()), this, SLOT(copyMap()) );
83+
84+
mActionRenameMap = new QAction(
85+
QIcon(myIconPath+"grass_rename_map.png"),
86+
tr("Rename selected map"), this);
87+
mActionRenameMap->setEnabled(false);
88+
ag->addAction ( mActionRenameMap );
89+
tb->addAction ( mActionRenameMap );
90+
connect ( mActionRenameMap, SIGNAL(triggered()), this, SLOT(renameMap()) );
91+
7692
mActionDeleteMap = new QAction(
7793
QIcon(myIconPath+"grass_delete_map.png"),
7894
tr("Delete selected map"), this);
@@ -197,14 +213,80 @@ void QgsGrassBrowser::doubleClicked(const QModelIndex & index)
197213
addMap();
198214
}
199215

216+
void QgsGrassBrowser::copyMap()
217+
{
218+
#ifdef QGISDEBUG
219+
std::cerr << "QgsGrassBrowser::copyMap()" << std::endl;
220+
#endif
221+
222+
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
223+
224+
QList<QModelIndex>::const_iterator it = indexes.begin();
225+
for (; it != indexes.end(); ++it)
226+
{
227+
int type = mModel->itemType(*it);
228+
QString mapset = mModel->itemMapset(*it);
229+
QString map = mModel->itemMap(*it);
230+
231+
QString typeName;
232+
QString element;
233+
if ( type == QgsGrassModel::Raster )
234+
{
235+
element = "cell";
236+
typeName = "rast";
237+
}
238+
else if ( type == QgsGrassModel::Vector )
239+
{
240+
element = "vector";
241+
typeName = "vect";
242+
}
243+
else if ( type == QgsGrassModel::Region )
244+
{
245+
element = "windows";
246+
typeName = "region";
247+
}
248+
249+
QgsGrassElementDialog *ed = new QgsGrassElementDialog();
250+
bool ok;
251+
QString newName = ed->getItem ( element, map, &ok );
252+
delete ed;
253+
254+
if ( !ok ) return;
255+
256+
QString module = "g.copy";
257+
#ifdef WIN32
258+
module.append(".exe");
259+
#endif
260+
QProcess process(this);
261+
process.start(module, QStringList( typeName + "=" + map + "@" + mapset + "," + newName ) );
262+
if ( !process.waitForFinished() )
263+
{
264+
QMessageBox::warning( 0, "Warning", "Cannot copy map "
265+
+ map );
266+
}
267+
else
268+
{
269+
refresh();
270+
}
271+
}
272+
}
273+
274+
void QgsGrassBrowser::renameMap()
275+
{
276+
#ifdef QGISDEBUG
277+
std::cerr << "QgsGrassBrowser::renameMap()" << std::endl;
278+
#endif
279+
280+
QMessageBox::warning ( 0, "Warning", "Not yet implemented" );
281+
}
282+
200283
void QgsGrassBrowser::deleteMap()
201284
{
202285
#ifdef QGISDEBUG
203286
std::cerr << "QgsGrassBrowser::deleteMap()" << std::endl;
204287
#endif
205288

206289
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();
207-
bool mapSelected = false;
208290

209291
QList<QModelIndex>::const_iterator it = indexes.begin();
210292
for (; it != indexes.end(); ++it)
@@ -339,6 +421,8 @@ void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QI
339421
#endif
340422

341423
mActionAddMap->setEnabled(false);
424+
mActionCopyMap->setEnabled(false);
425+
mActionRenameMap->setEnabled(false);
342426
mActionDeleteMap->setEnabled(false);
343427
mActionSetRegion->setEnabled(false);
344428

@@ -362,11 +446,13 @@ void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QI
362446
if ( type == QgsGrassModel::Raster || type == QgsGrassModel::Vector || type == QgsGrassModel::Region )
363447
{
364448
mActionSetRegion->setEnabled(true);
449+
mActionCopyMap->setEnabled(true);
365450

366451
QString mapset = mModel->itemMapset(*it);
367452
if ( mapset == QgsGrass::getDefaultMapset() )
368453
{
369454
mActionDeleteMap->setEnabled(true);
455+
mActionRenameMap->setEnabled(true);
370456
}
371457
}
372458
}

‎src/plugins/grass/qgsgrassbrowser.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public slots:
4444
// Add selected map to canvas
4545
void addMap();
4646

47+
// Copy selected map
48+
void copyMap();
49+
50+
// Rename selected map
51+
void renameMap();
52+
4753
// Delete selected map
4854
void deleteMap();
4955

@@ -85,6 +91,8 @@ public slots:
8591
//! Actions
8692
QAction *mActionAddMap;
8793
QAction *mActionDeleteMap;
94+
QAction *mActionCopyMap;
95+
QAction *mActionRenameMap;
8896
QAction *mActionSetRegion;
8997
QAction *mActionRefresh;
9098
};

‎src/plugins/grass/qgsgrassutils.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
#include <vector>
1616

1717
#include <QApplication>
18+
#include <QDialog>
19+
#include <QLineEdit>
20+
#include <QLabel>
21+
#include <QPushButton>
22+
#include <QVBoxLayout>
23+
#include <QHBoxLayout>
24+
#include <QFileInfo>
1825

1926
//#include "qgis.h"
2027
//#include "qgsapplication.h"
@@ -63,3 +70,72 @@ void QgsGrassUtils::addVectorLayers ( QgisIface *iface,
6370
iface->addVectorLayer( uri, name, "grass");
6471
}
6572
}
73+
74+
bool QgsGrassUtils::itemExists ( QString element, QString item )
75+
{
76+
QString path = QgsGrass::getDefaultGisdbase() + "/"
77+
+ QgsGrass::getDefaultLocation() + "/"
78+
+ QgsGrass::getDefaultMapset() + "/"
79+
+ "/" + element + "/" + item;
80+
81+
QFileInfo fi(path);
82+
return fi.exists();
83+
}
84+
85+
QgsGrassElementDialog::QgsGrassElementDialog() : QObject()
86+
{
87+
}
88+
QgsGrassElementDialog::~QgsGrassElementDialog() {}
89+
90+
QString QgsGrassElementDialog::getItem ( QString element,
91+
QString text, bool * ok )
92+
{
93+
#ifdef QGISDEBUG
94+
std::cerr << "QgsGrassElementDialog::getItem" << std::endl;
95+
#endif
96+
*ok = false;
97+
mElement = element;
98+
mDialog = new QDialog ();
99+
QVBoxLayout *layout = new QVBoxLayout ( mDialog );
100+
QHBoxLayout *buttonLayout = new QHBoxLayout ( );
101+
102+
mLineEdit = new QLineEdit ( text );
103+
layout->addWidget( mLineEdit );
104+
mErrorLabel = new QLabel ( );
105+
layout->addWidget( mErrorLabel );
106+
107+
mOkButton = new QPushButton ( "Ok" );
108+
mCancelButton = new QPushButton ( "Cancel" );
109+
110+
layout->insertLayout( -1, buttonLayout );
111+
buttonLayout->addWidget( mOkButton );
112+
buttonLayout->addWidget( mCancelButton );
113+
114+
connect ( mLineEdit, SIGNAL(textChanged(QString)), this, SLOT(textChanged() ) );
115+
connect ( mOkButton, SIGNAL(clicked()), mDialog, SLOT(accept() ) );
116+
connect ( mCancelButton, SIGNAL(clicked()), mDialog, SLOT(reject() ) );
117+
118+
textChanged ();
119+
if ( mDialog->exec() == QDialog::Accepted )
120+
{
121+
*ok = true;
122+
}
123+
124+
QString name = mLineEdit->text();
125+
delete mDialog;
126+
127+
return name;
128+
}
129+
130+
void QgsGrassElementDialog::textChanged ()
131+
{
132+
#ifdef QGISDEBUG
133+
std::cerr << "QgsGrassElementDialog::textChanged" << std::endl;
134+
#endif
135+
136+
mErrorLabel->setText ( "" );
137+
if ( QgsGrassUtils::itemExists( mElement, mLineEdit->text() ) )
138+
{
139+
mErrorLabel->setText ( "Exists!" );
140+
}
141+
}

‎src/plugins/grass/qgsgrassutils.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#ifndef QGSGRASSUTILS_H
1717
#define QGSGRASSUTILS_H
1818

19+
#include <QObject>
20+
class QDialog;
21+
class QLineEdit;
22+
class QLabel;
23+
class QPushButton;
1924
#include "qgisiface.h"
2025

2126
/*! \class QgsGrassUtils
@@ -38,6 +43,41 @@ class QgsGrassUtils
3843
// Add all vector layers to QGIS view
3944
static void QgsGrassUtils::addVectorLayers ( QgisIface *iface,
4045
QString gisbase, QString location, QString mapset, QString map);
46+
47+
// Check if element exists in current mapset
48+
static bool QgsGrassUtils::itemExists ( QString element, QString item);
49+
50+
};
51+
52+
/*! \class QgsGrassElementDialog
53+
* \brief Get name for new element
54+
*/
55+
class QgsGrassElementDialog: public QObject
56+
{
57+
Q_OBJECT;
58+
59+
public:
60+
//! Constructor
61+
QgsGrassElementDialog();
62+
63+
//! Destructor
64+
~QgsGrassElementDialog();
65+
66+
public:
67+
// Get a name for new GRASS element (map)
68+
QString getItem ( QString element,
69+
QString text, bool * ok );
70+
71+
public slots:
72+
void textChanged();
73+
74+
private:
75+
QString mElement;
76+
QDialog *mDialog;
77+
QLineEdit *mLineEdit;
78+
QLabel *mErrorLabel;
79+
QPushButton *mOkButton;
80+
QPushButton *mCancelButton;
4181
};
4282

4383
#endif // QGSGRASSUTILS_H

0 commit comments

Comments
 (0)
Please sign in to comment.