Skip to content
Permalink
Browse files

rename, set region by double click

git-svn-id: http://svn.osgeo.org/qgis/trunk@5057 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek
rblazek committed Mar 18, 2006
1 parent 9a19f85 commit 9e8c03914910a41bd403c3df545fd4f3092e71f6
@@ -201,6 +201,12 @@ void QgsGrassBrowser::addMap()
mIface->addVectorLayer( uri, name, "grass");
mapSelected = true;
}
else if ( type == QgsGrassModel::Region )
{
struct Cell_head window;
if ( !getItemRegion (*it, &window) ) continue;
writeRegion ( &window );
}
}
}

@@ -246,10 +252,20 @@ void QgsGrassBrowser::copyMap()
typeName = "region";
}

QgsGrassElementDialog *ed = new QgsGrassElementDialog();
QgsGrassElementDialog ed;
bool ok;
QString newName = ed->getItem ( element, map, &ok );
delete ed;
QString source;
QString suggest;
if ( mapset == QgsGrass::getDefaultMapset() )
{
source = map;
}
else
{
suggest = map;
}
QString newName = ed.getItem ( element, "New name",
"New name", suggest, source, &ok );

if ( !ok ) return;

@@ -262,7 +278,7 @@ void QgsGrassBrowser::copyMap()
if ( !process.waitForFinished() )
{
QMessageBox::warning( 0, "Warning", "Cannot copy map "
+ map );
+ map + "@" + mapset );
}
else
{
@@ -277,7 +293,57 @@ void QgsGrassBrowser::renameMap()
std::cerr << "QgsGrassBrowser::renameMap()" << std::endl;
#endif

QMessageBox::warning ( 0, "Warning", "Not yet implemented" );
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();

QList<QModelIndex>::const_iterator it = indexes.begin();
for (; it != indexes.end(); ++it)
{
int type = mModel->itemType(*it);
QString mapset = mModel->itemMapset(*it);
QString map = mModel->itemMap(*it);

if ( mapset != QgsGrass::getDefaultMapset() ) continue; // should not happen

QString typeName;
QString element;
if ( type == QgsGrassModel::Raster )
{
element = "cell";
typeName = "rast";
}
else if ( type == QgsGrassModel::Vector )
{
element = "vector";
typeName = "vect";
}
else if ( type == QgsGrassModel::Region )
{
element = "windows";
typeName = "region";
}

QgsGrassElementDialog ed;
bool ok;
QString newName = ed.getItem ( element, "New name",
"New name", "", map, &ok );

if ( !ok ) return;

QString module = "g.rename";
#ifdef WIN32
module.append(".exe");
#endif
QProcess process(this);
process.start(module, QStringList( typeName + "=" + map + "," + newName ) );
if ( !process.waitForFinished() )
{
QMessageBox::warning( 0, "Warning", "Cannot rename map " + map );
}
else
{
refresh();
}
}
}

void QgsGrassBrowser::deleteMap()
@@ -336,76 +402,29 @@ void QgsGrassBrowser::setRegion()
#endif

struct Cell_head window;

QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();

QgsGrass::setLocation( QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation() );
QModelIndexList indexes = mTree->selectionModel()->selectedIndexes();

// TODO multiple selection - extent region to all maps
QList<QModelIndex>::const_iterator it = indexes.begin();
for (; it != indexes.end(); ++it)
{
int type = mModel->itemType(*it);
QString uri = mModel->uri(*it);
QString mapset = mModel->itemMapset(*it);
QString map = mModel->itemMap(*it);

if ( type == QgsGrassModel::Raster )
{

if ( G_get_cellhd ( map.toLocal8Bit().data(),
mapset.toLocal8Bit().data(), &window) < 0 )
{
QMessageBox::warning( 0, "Warning",
"Cannot read raster map region" );
return;
}
}
else if ( type == QgsGrassModel::Vector )
{
G_get_window ( &window ); // get current resolution

struct Map_info Map;

int level = Vect_open_old_head ( &Map,
map.toLocal8Bit().data(), mapset.toLocal8Bit().data());

if ( level < 2 )
{
QMessageBox::warning( 0, "Warning",
"Cannot read vector map region" );
return;
}

BOUND_BOX box;
Vect_get_map_box (&Map, &box );
window.north = box.N;
window.south = box.S;
window.west = box.W;
window.east = box.E;

Vect_close (&Map);
}
else if ( type == QgsGrassModel::Region )
{
if ( G__get_window (&window, "windows",
map.toLocal8Bit().data(),
mapset.toLocal8Bit().data() ) != NULL )
{
QMessageBox::warning( 0, "Warning",
"Cannot read region" );
return;
}
}
if ( !getItemRegion (*it, &window) ) return;
}
writeRegion ( &window );
}

void QgsGrassBrowser::writeRegion(struct Cell_head *window )
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassBrowser::writeRegion()" << std::endl;
#endif

// Reset mapset (selected maps could be in a different one)
QgsGrass::setMapset( QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation(),
QgsGrass::getDefaultMapset() );

if ( G_put_window ( &window ) == -1 )
if ( G_put_window ( window ) == -1 )
{
QMessageBox::warning( 0, "Warning",
"Cannot write new region" );
@@ -414,6 +433,70 @@ void QgsGrassBrowser::setRegion()
emit regionChanged();
}

bool QgsGrassBrowser::getItemRegion( QModelIndex index, struct Cell_head *window )
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassBrowser::setRegion()" << std::endl;
#endif

QgsGrass::setLocation( QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation() );

int type = mModel->itemType(index);
QString uri = mModel->uri(index);
QString mapset = mModel->itemMapset(index);
QString map = mModel->itemMap(index);

if ( type == QgsGrassModel::Raster )
{

if ( G_get_cellhd ( map.toLocal8Bit().data(),
mapset.toLocal8Bit().data(), window) < 0 )
{
QMessageBox::warning( 0, "Warning",
"Cannot read raster map region" );
return false;
}
}
else if ( type == QgsGrassModel::Vector )
{
G_get_window ( window ); // get current resolution

struct Map_info Map;

int level = Vect_open_old_head ( &Map,
map.toLocal8Bit().data(), mapset.toLocal8Bit().data());

if ( level < 2 )
{
QMessageBox::warning( 0, "Warning",
"Cannot read vector map region" );
return false;
}

BOUND_BOX box;
Vect_get_map_box (&Map, &box );
window->north = box.N;
window->south = box.S;
window->west = box.W;
window->east = box.E;

Vect_close (&Map);
}
else if ( type == QgsGrassModel::Region )
{
if ( G__get_window (window, "windows",
map.toLocal8Bit().data(),
mapset.toLocal8Bit().data() ) != NULL )
{
QMessageBox::warning( 0, "Warning",
"Cannot read region" );
return false;
}
}
return true;
}

void QgsGrassBrowser::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
{
#ifdef QGISDEBUG
@@ -56,6 +56,12 @@ public slots:
// Set current region to selected map
void setRegion();

// Get item's region
bool getItemRegion(QModelIndex index, struct Cell_head *window);

// Write region
void writeRegion ( struct Cell_head *window );

// Refresh model
void refresh();

@@ -88,23 +88,33 @@ QgsGrassElementDialog::QgsGrassElementDialog() : QObject()
QgsGrassElementDialog::~QgsGrassElementDialog() {}

QString QgsGrassElementDialog::getItem ( QString element,
QString text, bool * ok )
QString title, QString label,
QString text, QString source, bool * ok )
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassElementDialog::getItem" << std::endl;
#endif
*ok = false;
if ( ok ) *ok = false;
mElement = element;
mSource = source;
mDialog = new QDialog ();
mDialog->setWindowTitle(title);
QVBoxLayout *layout = new QVBoxLayout ( mDialog );
QHBoxLayout *buttonLayout = new QHBoxLayout ( );

mLabel = new QLabel ( label );
layout->addWidget( mLabel );

mLineEdit = new QLineEdit ( text );
layout->addWidget( mLineEdit );
mErrorLabel = new QLabel ( );

mErrorLabel = new QLabel ( "X" );
layout->addWidget( mErrorLabel );
// Intention: keep fixed size - but it does not help
mErrorLabel->adjustSize();
mErrorLabel->setMinimumHeight ( mErrorLabel->height()+5 );

mOkButton = new QPushButton ( "Ok" );
mOkButton = new QPushButton ( );
mCancelButton = new QPushButton ( "Cancel" );

layout->insertLayout( -1, buttonLayout );
@@ -116,7 +126,7 @@ QString QgsGrassElementDialog::getItem ( QString element,
connect ( mCancelButton, SIGNAL(clicked()), mDialog, SLOT(reject() ) );

textChanged ();
if ( mDialog->exec() == QDialog::Accepted )
if ( ok && mDialog->exec() == QDialog::Accepted )
{
*ok = true;
}
@@ -133,9 +143,34 @@ void QgsGrassElementDialog::textChanged ()
std::cerr << "QgsGrassElementDialog::textChanged" << std::endl;
#endif

mErrorLabel->setText ( "" );
if ( QgsGrassUtils::itemExists( mElement, mLineEdit->text() ) )
QString text = mLineEdit->text().trimmed();

mErrorLabel->setText ( " " );
mOkButton->setText ("Ok");
mOkButton->setEnabled ( true );

if ( text.length() == 0 )
{
mErrorLabel->setText ( "Exists!" );
mErrorLabel->setText ( "<font color='red'>Enter a name!</font>" );
mOkButton->setEnabled ( false );
return;
}

#ifdef WIN32
if ( !mSource.isNull() && text.toLower() == mSource.toLower() )
#else
if ( !mSource.isNull() && text == mSource )
#endif
{
mErrorLabel->setText ( "<font color='red'>This is name of the source!</font>" );
mOkButton->setEnabled ( false );
return;
}
if ( QgsGrassUtils::itemExists( mElement, text ) )
{
mErrorLabel->setText ( "<font color='red'>Exists!</font>" );
mOkButton->setText ("Overwrite");
return;
}
}

@@ -64,17 +64,22 @@ class QgsGrassElementDialog: public QObject
~QgsGrassElementDialog();

public:
// Get a name for new GRASS element (map)
//! Get a name for new GRASS element (map)
// \param source local source
QString getItem ( QString element,
QString text, bool * ok );
QString title, QString label,
QString text, QString source = 0,
bool * ok = 0 );

public slots:
void textChanged();

private:
QString mElement;
QString mSource;
QDialog *mDialog;
QLineEdit *mLineEdit;
QLabel *mLabel;
QLabel *mErrorLabel;
QPushButton *mOkButton;
QPushButton *mCancelButton;

0 comments on commit 9e8c039

Please sign in to comment.
You can’t perform that action at this time.