File: qgsgrassutils.cpp

package info (click to toggle)
qgis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 374,696 kB
  • ctags: 66,263
  • sloc: cpp: 396,139; ansic: 241,070; python: 130,609; xml: 14,884; perl: 1,290; sh: 1,287; sql: 500; yacc: 268; lex: 242; makefile: 168
file content (168 lines) | stat: -rw-r--r-- 5,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************
                              qgsgrassutils.cpp
                             -------------------
    begin                : March, 2006
    copyright            : (C) 2006 by Radim Blazek
    email                : radim.blazek@gmail.com
********************************************************************/
/********************************************************************
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
*******************************************************************/

#include "qgsgrassutils.h"
#include "qgsgrassselect.h"
#include "qgsgrass.h"

#include "qgisinterface.h"
#include "qgslogger.h"

#include <QFileInfo>


QgsGrassUtils::QgsGrassUtils() {}
QgsGrassUtils::~QgsGrassUtils() {}

QString QgsGrassUtils::vectorLayerName( QString map, QString layer,
                                        int nLayers )
{
  QString name = map;
  if ( nLayers > 1 )
    name += " " + layer;
  return name;
}

void QgsGrassUtils::addVectorLayers( QgisInterface *iface,
                                     QString gisbase, QString location, QString mapset, QString map )
{
  QStringList layers = QgsGrass::vectorLayers( gisbase, location, mapset, map );

  for ( int i = 0; i < layers.count(); i++ )
  {
    QString name = QgsGrassUtils::vectorLayerName( map, layers[i], layers.size() );

    QString uri = gisbase + "/" + location + "/"
                  + mapset + "/" + map + "/" + layers[i];

    QgsDebugMsg( QString( "layer = %1" ).arg( layers[i].toLocal8Bit().constData() ) );
    QgsDebugMsg( QString( "uri = %1" ).arg( uri.toLocal8Bit().constData() ) );
    QgsDebugMsg( QString( "name = %1" ).arg( name.toLocal8Bit().constData() ) );

    iface->addVectorLayer( uri, name, "grass" );
  }
}

bool QgsGrassUtils::itemExists( QString element, QString item )
{
  QString path = QgsGrass::getDefaultGisdbase() + "/"
                 + QgsGrass::getDefaultLocation() + "/"
                 + QgsGrass::getDefaultMapset() + "/"
                 + "/" + element + "/" + item;

  QFileInfo fi( path );
  return fi.exists();
}

QgsGrassElementDialog::QgsGrassElementDialog( QWidget *parent ) : QObject(), mParent( parent )
{
}

QgsGrassElementDialog::~QgsGrassElementDialog() {}

QString QgsGrassElementDialog::getItem( QString element,
                                        QString title, QString label,
                                        QString text, QString source, bool * ok )
{
  QgsDebugMsg( "entered." );
  if ( ok )
    *ok = false;
  mElement = element;
  mSource = source;
  mDialog = new QDialog( mParent );
  mDialog->setWindowTitle( title );
  QVBoxLayout *layout = new QVBoxLayout( mDialog );
  QHBoxLayout *buttonLayout = new QHBoxLayout( );

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

  mLineEdit = new QLineEdit( text );
  QRegExp rx;
  if ( element == "vector" )
  {
    rx.setPattern( "[A-Za-z_][A-Za-z0-9_]+" );
  }
  else
  {
    rx.setPattern( "[A-Za-z0-9_.]+" );
  }
  QRegExpValidator *val = new QRegExpValidator( rx, this );
  mLineEdit->setValidator( val );

  layout->addWidget( mLineEdit );

  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( );
  mCancelButton = new QPushButton( tr( "Cancel" ) );

  layout->insertLayout( -1, buttonLayout );
  buttonLayout->addWidget( mOkButton );
  buttonLayout->addWidget( mCancelButton );

  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( textChanged() ) );
  connect( mOkButton, SIGNAL( clicked() ), mDialog, SLOT( accept() ) );
  connect( mCancelButton, SIGNAL( clicked() ), mDialog, SLOT( reject() ) );

  textChanged();
  if ( ok && mDialog->exec() == QDialog::Accepted )
  {
    *ok = true;
  }

  QString name = mLineEdit->text();
  delete mDialog;

  return name;
}

void QgsGrassElementDialog::textChanged()
{
  QgsDebugMsg( "entered." );

  QString text = mLineEdit->text().trimmed();

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

  if ( text.length() == 0 )
  {
    mErrorLabel->setText( tr( "<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( tr( "<font color='red'>This is name of the source!</font>" ) );
    mOkButton->setEnabled( false );
    return;
  }
  if ( QgsGrassUtils::itemExists( mElement, text ) )
  {
    mErrorLabel->setText( tr( "<font color='red'>Exists!</font>" ) );
    mOkButton->setText( tr( "Overwrite" ) );
    return;
  }
}