Skip to content
Permalink
Browse files

[GRASS] new input combo in mapcalc

  • Loading branch information
blazek committed Dec 15, 2015
1 parent a53ba16 commit 02fcaca6938fedd911b7b0fef242223ad1915f30
@@ -140,9 +140,19 @@ QgsGrassMapcalc::QgsGrassMapcalc(
tb->addAction( mActionSaveAs );
connect( mActionSaveAs, SIGNAL( triggered() ), this, SLOT( saveAs() ) );

// Map input
mMapComboBox = new QgsGrassModuleInputComboBox( QgsGrassObject::Raster, this );
mMapComboBox->setSizePolicy( QSizePolicy::Expanding, QSizePolicy:: Preferred );
// QComboBox does not emit activated() when item is selected in completer popup
connect( mMapComboBox, SIGNAL( activated( const QString & ) ), this, SLOT( mapChanged( const QString & ) ) );
connect( mMapComboBox->completer(), SIGNAL( activated( const QString & ) ), this, SLOT( mapChanged( const QString & ) ) );
connect( mMapComboBox, SIGNAL( editTextChanged( const QString & ) ), this, SLOT( mapChanged( const QString & ) ) );
bool firstSet = mMapComboBox->setFirst();
Q_UNUSED( firstSet );
mInputFrame->layout()->addWidget( mMapComboBox );

/* Create functions */
int t = QgsGrassMapcalcFunction::Operator;
//mFunctions.push_back(QgsGrassMapcalcFunction("-",2, "Odcitani", "in1,in2" ));
// Arithmetical
mFunctions.push_back( QgsGrassMapcalcFunction( t, "+", 2, tr( "Addition" ) ) );
mFunctions.push_back( QgsGrassMapcalcFunction( t, "-", 2, tr( "Subtraction" ) ) );
@@ -216,8 +226,7 @@ QgsGrassMapcalc::QgsGrassMapcalc(
mOutput->QGraphicsRectItem::show();

// Set default tool
updateMaps();
if ( mMaps.size() > 0 )
if ( mMapComboBox->count() > 0 )
{
setTool( AddMap );
}
@@ -652,21 +661,10 @@ void QgsGrassMapcalc::setOption()
{
case QgsGrassMapcalcObject::Map :
{
bool found = false;
for ( unsigned int i = 0 ; i < mMaps.size(); i++ )
{
if ( mMapComboBox->itemText( i ) == mObject->label()
&& mMaps[i] == mObject->value() )
{
mMapComboBox->setCurrentIndex( i );
found = true;
}
}
if ( !found )
QStringList mapMapset = mObject->value().split( "@" );
if ( !mMapComboBox->setCurrent( mapMapset.value( 0 ), mapMapset.value( 1 ) ) )
{
mMaps.push_back( mObject->value() );
mMapComboBox->addItem( mObject->label() );
mMapComboBox->setCurrentIndex( mMapComboBox->count() - 1 );
mMapComboBox->setEditText( mObject->value() );
}
break;
}
@@ -724,11 +722,7 @@ void QgsGrassMapcalc::setTool( int tool )
case AddMap:
mObject = new QgsGrassMapcalcObject( QgsGrassMapcalcObject::Map );
mObject->setId( nextId() );

// TODO check if there are maps
mObject->setValue( mMaps[mMapComboBox->currentIndex()],
mMapComboBox->currentText() );

mObject->setValue( mMapComboBox->currentText() );
mObject->setCenter( mLastPoint.x(), mLastPoint.y() );
mCanvasScene->addItem( mObject );
mObject->QGraphicsRectItem::show();
@@ -780,10 +774,9 @@ void QgsGrassMapcalc::setTool( int tool )

void QgsGrassMapcalc::addMap()
{
updateMaps();
if ( mMaps.size() == 0 )
if ( mMapComboBox->count() == 0 )
{
QMessageBox::warning( 0, tr( "Warning" ), tr( "No GRASS raster maps currently in QGIS" ) );
QMessageBox::warning( 0, tr( "Warning" ), tr( "No GRASS raster maps available" ) );

setTool( AddConstant );
return;
@@ -851,75 +844,7 @@ void QgsGrassMapcalc::setToolActionsOff()
mActionDeleteItem->setChecked( false );
}

void QgsGrassMapcalc::updateMaps()
{
// TODO: this copy and paste from QgsGrassModuleInput, do it better
QgsDebugMsg( "entered." );
QString current = mMapComboBox->currentText();
mMapComboBox->clear();
mMaps.clear();

QgsMapCanvas *canvas = mIface->mapCanvas();

int nlayers = canvas->layerCount();
QgsDebugMsg( QString( "nlayers = %1" ).arg( nlayers ) );
for ( int i = 0; i < nlayers; i++ )
{
QgsMapLayer *layer = canvas->layer( i );

if ( layer->type() != QgsMapLayer::RasterLayer )
continue;

// Check if it is GRASS raster
QString source = QDir::cleanPath( layer->source() );

// Note: QDir::cleanPath is using '/' also on Windows
//QChar sep = QDir::separator();
QChar sep = '/';

if ( source.contains( "cellhd" ) == 0 )
continue;

// Most probably GRASS layer, check GISBASE and LOCATION
QStringList split = source.split( sep, QString::SkipEmptyParts );

if ( split.size() < 4 )
continue;

QString map = split.last();
split.pop_back(); // map
if ( split.last() != "cellhd" )
continue;
split.pop_back(); // cellhd

QString mapset = split.last();
split.pop_back(); // mapset

//QDir locDir ( sep + split.join ( QString(sep) ) );
//QString loc = locDir.canonicalPath();

QString loc = source.remove( QRegExp( "/[^/]+/[^/]+/[^/]+$" ) );
loc = QDir( loc ).canonicalPath();

QDir curlocDir( QgsGrass::getDefaultGisdbase() + sep + QgsGrass::getDefaultLocation() );
QString curloc = curlocDir.canonicalPath();

if ( loc != curloc )
continue;

#if 0
if ( mUpdate && mapset != QgsGrass::getDefaultMapset() )
continue;
#endif

mMapComboBox->addItem( layer->name() );
//if ( layer->name() == current )
// mMapComboBox->setItemText( mMapComboBox->currentIndex(), current );
mMaps.push_back( map + "@" + mapset );
}
}

void QgsGrassMapcalc::mapChanged()
void QgsGrassMapcalc::mapChanged( const QString & text )
{
QgsDebugMsg( "entered." );

@@ -928,8 +853,7 @@ void QgsGrassMapcalc::mapChanged()
if ( mObject->type() != QgsGrassMapcalcObject::Map )
return;

mObject->setValue( mMaps[mMapComboBox->currentIndex()],
mMapComboBox->currentText() );
mObject->setValue( text );
mCanvasScene->update();
}

@@ -1655,10 +1579,6 @@ void QgsGrassMapcalcObject::resetSize()
{
for ( int i = 0; i < mFunction.inputLabels().size(); i++ )
{
#if 0
QStringList::Iterator it = mFunction.inputLabels().at( i );
QString l = *it;
#endif
QString l = mFunction.inputLabels().at( i );
int len = metrics.width( l );
if ( len > mInputTextWidth )
@@ -14,6 +14,7 @@
#ifndef QGSGRASSMAPCALC_H
#define QGSGRASSMAPCALC_H

#include "qgsgrassmoduleinput.h"
#include "ui_qgsgrassmapcalcbase.h"
#include "qgsgrassmodule.h"

@@ -123,8 +124,7 @@ class QgsGrassMapcalc: public QMainWindow, private Ui::QgsGrassMapcalcBase,
void setTool( int );

//! Map selection changed
void on_mMapComboBox_activated() { mapChanged(); }
void mapChanged();
void mapChanged( const QString & text );

//! Constant changed
void on_mConstantLineEdit_textChanged() { constantChanged(); }
@@ -164,11 +164,7 @@ class QgsGrassMapcalc: public QMainWindow, private Ui::QgsGrassMapcalcBase,
// Pointer to current connector
QgsGrassMapcalcConnector *mConnector;

// Update combobox with maps
void updateMaps();

//! Vector of map@mapset in the combobox
std::vector<QString> mMaps;
QgsGrassModuleInputComboBox *mMapComboBox;

//! Last point position
QPoint mLastPoint;
@@ -1,40 +1,41 @@
<ui version="4.0" >
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QgsGrassMapcalcBase</class>
<widget class="QMainWindow" name="QgsGrassMapcalcBase" >
<property name="geometry" >
<widget class="QMainWindow" name="QgsGrassMapcalcBase">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>525</width>
<height>412</height>
</rect>
</property>
<property name="windowTitle" >
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget" >
<layout class="QGridLayout" >
<item row="2" column="0" >
<layout class="QHBoxLayout" >
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout">
<item row="2" column="0">
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="textLabel1_2" >
<property name="text" >
<widget class="QLabel" name="textLabel1_2">
<property name="text">
<string>Output</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mOutputLineEdit" />
<widget class="QLineEdit" name="mOutputLineEdit"/>
</item>
<item>
<spacer>
<property name="orientation" >
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType" >
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" >
<property name="sizeHint" stdset="0">
<size>
<width>200</width>
<height>16</height>
@@ -44,86 +45,74 @@
</item>
</layout>
</item>
<item row="1" column="0" >
<widget class="QFrame" name="mViewFrame" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Expanding" hsizetype="Expanding" >
<item row="1" column="0">
<widget class="QFrame" name="mViewFrame">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape" >
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow" >
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
<item row="0" column="0" >
<widget class="QFrame" name="mInputFrame" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Minimum" hsizetype="Expanding" >
<item row="0" column="0">
<widget class="QFrame" name="mInputFrame">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize" >
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="frameShape" >
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow" >
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="margin" stdset="0" >
<property name="margin" stdset="0">
<number>0</number>
</property>
<layout class="QHBoxLayout" >
<property name="leftMargin" >
<layout class="QHBoxLayout">
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin" >
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin" >
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin" >
<property name="bottomMargin">
<number>3</number>
</property>
<item>
<widget class="QComboBox" name="mMapComboBox" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Fixed" hsizetype="Expanding" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize" >
<size>
<width>150</width>
<height>0</height>
</size>
<widget class="QLineEdit" name="mConstantLineEdit">
<property name="placeholderText">
<string>Enter constant value</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mConstantLineEdit" />
</item>
<item>
<widget class="QComboBox" name="mFunctionComboBox" />
<widget class="QComboBox" name="mFunctionComboBox"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar" />
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>

0 comments on commit 02fcaca

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