~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/plugins/grass/qgsgrassutils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
    email                : radim.blazek@gmail.com
7
7
********************************************************************/
8
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.                                   
 
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
13
*******************************************************************/
14
 
#include <iostream>
15
 
#include <vector>
16
 
 
17
 
#include <QApplication>
18
 
#include <QDialog>
19
 
#include <QLineEdit>
20
 
#include <QLabel>
21
 
#include <QPushButton>
22
 
#include <QVBoxLayout>
23
 
#include <QHBoxLayout>
24
 
#include <QFileInfo>
25
 
#include <QRegExpValidator>
26
 
#include <QRegExp>
27
 
 
28
 
extern "C" {
29
 
#include <grass/gis.h>
30
 
#include <grass/Vect.h>
31
 
}
32
 
 
33
 
#include "../../src/providers/grass/qgsgrass.h"
 
14
 
34
15
#include "qgsgrassutils.h"
35
16
#include "qgsgrassselect.h"
 
17
#include "qgsgrass.h"
 
18
 
 
19
#include "qgisinterface.h"
 
20
#include "qgslogger.h"
 
21
 
 
22
#include <QFileInfo>
 
23
 
36
24
 
37
25
QgsGrassUtils::QgsGrassUtils() {}
38
26
QgsGrassUtils::~QgsGrassUtils() {}
39
27
 
40
 
QString QgsGrassUtils::vectorLayerName( QString map, QString layer, 
 
28
QString QgsGrassUtils::vectorLayerName( QString map, QString layer,
41
29
                                        int nLayers )
42
30
{
43
 
    QString name = map;
44
 
    if ( nLayers > 1 ) name += " " + layer;
45
 
    return name;
46
 
}
47
 
 
48
 
void QgsGrassUtils::addVectorLayers ( QgisIface *iface,
49
 
        QString gisbase, QString location, QString mapset, QString map)
50
 
{
51
 
    QStringList layers = QgsGrassSelect::vectorLayers(
52
 
                           gisbase, location, mapset, map );
53
 
 
54
 
 
55
 
    for ( int i = 0; i < layers.count(); i++ )
56
 
    {
57
 
        QString name = QgsGrassUtils::vectorLayerName (
58
 
                              map, layers[i], layers.size() );
59
 
 
60
 
        QString uri = gisbase + "/" + location + "/"
61
 
                   + mapset + "/" + map + "/" + layers[i];
62
 
 
63
 
#ifdef QGISDEBUG
64
 
         std::cerr << "layer = " << layers[i].local8Bit().data() << std::endl;
65
 
         std::cerr << "uri = " << uri.local8Bit().data() << std::endl;
66
 
         std::cerr << "name = " << name.local8Bit().data() << std::endl;
67
 
#endif
68
 
 
69
 
        iface->addVectorLayer( uri, name, "grass");
70
 
    }
71
 
}
72
 
 
73
 
bool QgsGrassUtils::itemExists ( QString element, QString item )
74
 
{
75
 
    QString path = QgsGrass::getDefaultGisdbase() + "/"
76
 
                  + QgsGrass::getDefaultLocation() + "/"
77
 
                  + QgsGrass::getDefaultMapset() + "/"
78
 
                  + "/" + element + "/" + item;
79
 
 
80
 
    QFileInfo fi(path);
81
 
    return fi.exists(); 
82
 
}
83
 
 
84
 
QgsGrassElementDialog::QgsGrassElementDialog() : QObject() 
85
 
{
86
 
}
 
31
  QString name = map;
 
32
  if ( nLayers > 1 ) name += " " + layer;
 
33
  return name;
 
34
}
 
35
 
 
36
void QgsGrassUtils::addVectorLayers( QgisInterface *iface,
 
37
                                     QString gisbase, QString location, QString mapset, QString map )
 
38
{
 
39
  QStringList layers = QgsGrassSelect::vectorLayers(
 
40
                         gisbase, location, mapset, map );
 
41
 
 
42
 
 
43
  for ( int i = 0; i < layers.count(); i++ )
 
44
  {
 
45
    QString name = QgsGrassUtils::vectorLayerName(
 
46
                     map, layers[i], layers.size() );
 
47
 
 
48
    QString uri = gisbase + "/" + location + "/"
 
49
                  + mapset + "/" + map + "/" + layers[i];
 
50
 
 
51
    QgsDebugMsg( QString( "layer = %1" ).arg( layers[i].toLocal8Bit().constData() ) );
 
52
    QgsDebugMsg( QString( "uri = %1" ).arg( uri.toLocal8Bit().constData() ) );
 
53
    QgsDebugMsg( QString( "name = %1" ).arg( name.toLocal8Bit().constData() ) );
 
54
 
 
55
    iface->addVectorLayer( uri, name, "grass" );
 
56
  }
 
57
}
 
58
 
 
59
bool QgsGrassUtils::itemExists( QString element, QString item )
 
60
{
 
61
  QString path = QgsGrass::getDefaultGisdbase() + "/"
 
62
                 + QgsGrass::getDefaultLocation() + "/"
 
63
                 + QgsGrass::getDefaultMapset() + "/"
 
64
                 + "/" + element + "/" + item;
 
65
 
 
66
  QFileInfo fi( path );
 
67
  return fi.exists();
 
68
}
 
69
 
 
70
QgsGrassElementDialog::QgsGrassElementDialog() : QObject()
 
71
{
 
72
}
 
73
 
87
74
QgsGrassElementDialog::~QgsGrassElementDialog() {}
88
75
 
89
 
QString QgsGrassElementDialog::getItem ( QString element,
90
 
                       QString title, QString label,
91
 
                       QString text, QString source, bool * ok )
 
76
QString QgsGrassElementDialog::getItem( QString element,
 
77
                                        QString title, QString label,
 
78
                                        QString text, QString source, bool * ok )
92
79
{
93
 
#ifdef QGISDEBUG
94
 
    std::cerr << "QgsGrassElementDialog::getItem" << std::endl;
95
 
#endif
96
 
    if ( ok ) *ok = false;
97
 
    mElement = element;
98
 
    mSource = source;
99
 
    mDialog = new QDialog ();
100
 
    mDialog->setWindowTitle(title);
101
 
    QVBoxLayout *layout = new QVBoxLayout ( mDialog );
102
 
    QHBoxLayout *buttonLayout = new QHBoxLayout ( );
103
 
 
104
 
    mLabel = new QLabel ( label );
105
 
    layout->addWidget( mLabel );
106
 
 
107
 
    mLineEdit = new QLineEdit ( text );
108
 
    QRegExp rx;
109
 
    if ( element == "vector" )
110
 
    {
111
 
        rx.setPattern("[A-Za-z_][A-Za-z0-9_]+");
112
 
    }
113
 
    else
114
 
    {
115
 
        rx.setPattern("[A-Za-z0-9_.]+");
116
 
    }
117
 
    QRegExpValidator *val = new QRegExpValidator( rx, this );
118
 
    mLineEdit->setValidator ( val );
119
 
 
120
 
    layout->addWidget( mLineEdit );
121
 
 
122
 
    mErrorLabel = new QLabel ( "X" );
123
 
    layout->addWidget( mErrorLabel );
124
 
    // Intention: keep fixed size - but it does not help
125
 
    mErrorLabel->adjustSize();
126
 
    mErrorLabel->setMinimumHeight ( mErrorLabel->height()+5 );
127
 
 
128
 
    mOkButton = new QPushButton ( );
129
 
    mCancelButton = new QPushButton ( tr("Cancel" ) );
130
 
     
131
 
    layout->insertLayout( -1, buttonLayout );
132
 
    buttonLayout->addWidget( mOkButton );
133
 
    buttonLayout->addWidget( mCancelButton );
134
 
 
135
 
    connect ( mLineEdit, SIGNAL(textChanged(QString)), this, SLOT(textChanged() ) );
136
 
    connect ( mOkButton, SIGNAL(clicked()), mDialog, SLOT(accept() ) );
137
 
    connect ( mCancelButton, SIGNAL(clicked()), mDialog, SLOT(reject() ) );
138
 
 
139
 
    textChanged ();
140
 
    if ( ok && mDialog->exec() == QDialog::Accepted )
141
 
    {
142
 
        *ok = true;
143
 
    }
144
 
 
145
 
    QString name = mLineEdit->text();
146
 
    delete mDialog;
147
 
 
148
 
    return name;
 
80
  QgsDebugMsg( "entered." );
 
81
  if ( ok ) *ok = false;
 
82
  mElement = element;
 
83
  mSource = source;
 
84
  mDialog = new QDialog();
 
85
  mDialog->setWindowTitle( title );
 
86
  QVBoxLayout *layout = new QVBoxLayout( mDialog );
 
87
  QHBoxLayout *buttonLayout = new QHBoxLayout( );
 
88
 
 
89
  mLabel = new QLabel( label );
 
90
  layout->addWidget( mLabel );
 
91
 
 
92
  mLineEdit = new QLineEdit( text );
 
93
  QRegExp rx;
 
94
  if ( element == "vector" )
 
95
  {
 
96
    rx.setPattern( "[A-Za-z_][A-Za-z0-9_]+" );
 
97
  }
 
98
  else
 
99
  {
 
100
    rx.setPattern( "[A-Za-z0-9_.]+" );
 
101
  }
 
102
  QRegExpValidator *val = new QRegExpValidator( rx, this );
 
103
  mLineEdit->setValidator( val );
 
104
 
 
105
  layout->addWidget( mLineEdit );
 
106
 
 
107
  mErrorLabel = new QLabel( "X" );
 
108
  layout->addWidget( mErrorLabel );
 
109
  // Intention: keep fixed size - but it does not help
 
110
  mErrorLabel->adjustSize();
 
111
  mErrorLabel->setMinimumHeight( mErrorLabel->height() + 5 );
 
112
 
 
113
  mOkButton = new QPushButton( );
 
114
  mCancelButton = new QPushButton( tr( "Cancel" ) );
 
115
 
 
116
  layout->insertLayout( -1, buttonLayout );
 
117
  buttonLayout->addWidget( mOkButton );
 
118
  buttonLayout->addWidget( mCancelButton );
 
119
 
 
120
  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( textChanged() ) );
 
121
  connect( mOkButton, SIGNAL( clicked() ), mDialog, SLOT( accept() ) );
 
122
  connect( mCancelButton, SIGNAL( clicked() ), mDialog, SLOT( reject() ) );
 
123
 
 
124
  textChanged();
 
125
  if ( ok && mDialog->exec() == QDialog::Accepted )
 
126
  {
 
127
    *ok = true;
 
128
  }
 
129
 
 
130
  QString name = mLineEdit->text();
 
131
  delete mDialog;
 
132
 
 
133
  return name;
149
134
}
150
135
 
151
 
void QgsGrassElementDialog::textChanged ()
 
136
void QgsGrassElementDialog::textChanged()
152
137
{
153
 
#ifdef QGISDEBUG
154
 
    std::cerr << "QgsGrassElementDialog::textChanged" << std::endl;
155
 
#endif
156
 
 
157
 
   QString text = mLineEdit->text().trimmed();
158
 
 
159
 
   mErrorLabel->setText ( "   " );
160
 
   mOkButton->setText ( tr("Ok" ) );
161
 
   mOkButton->setEnabled ( true );
162
 
 
163
 
   if ( text.length() == 0 )
164
 
   {
165
 
       mErrorLabel->setText ( tr( "<font color='red'>Enter a name!</font>" ) );
166
 
       mOkButton->setEnabled ( false );
167
 
       return;
168
 
   }
 
138
  QgsDebugMsg( "entered." );
 
139
 
 
140
  QString text = mLineEdit->text().trimmed();
 
141
 
 
142
  mErrorLabel->setText( "   " );
 
143
  mOkButton->setText( tr( "Ok" ) );
 
144
  mOkButton->setEnabled( true );
 
145
 
 
146
  if ( text.length() == 0 )
 
147
  {
 
148
    mErrorLabel->setText( tr( "<font color='red'>Enter a name!</font>" ) );
 
149
    mOkButton->setEnabled( false );
 
150
    return;
 
151
  }
169
152
 
170
153
#ifdef WIN32
171
 
   if ( !mSource.isNull() && text.toLower() == mSource.toLower() )
 
154
  if ( !mSource.isNull() && text.toLower() == mSource.toLower() )
172
155
#else
173
 
   if ( !mSource.isNull() && text == mSource )
 
156
  if ( !mSource.isNull() && text == mSource )
174
157
#endif
175
 
   {
176
 
       mErrorLabel->setText ( tr( "<font color='red'>This is name of the source!</font>" ) );
177
 
       mOkButton->setEnabled ( false );
178
 
       return;
179
 
   }
180
 
   if ( QgsGrassUtils::itemExists( mElement, text ) )
181
 
   {
182
 
       mErrorLabel->setText ( tr( "<font color='red'>Exists!</font>" ) );
183
 
       mOkButton->setText ( tr("Overwrite") );
184
 
       return;
185
 
   }
 
158
  {
 
159
    mErrorLabel->setText( tr( "<font color='red'>This is name of the source!</font>" ) );
 
160
    mOkButton->setEnabled( false );
 
161
    return;
 
162
  }
 
163
  if ( QgsGrassUtils::itemExists( mElement, text ) )
 
164
  {
 
165
    mErrorLabel->setText( tr( "<font color='red'>Exists!</font>" ) );
 
166
    mOkButton->setText( tr( "Overwrite" ) );
 
167
    return;
 
168
  }
186
169
}
187