Skip to content

Commit 02fcaca

Browse files
committedDec 15, 2015
[GRASS] new input combo in mapcalc
1 parent a53ba16 commit 02fcaca

File tree

5 files changed

+123
-157
lines changed

5 files changed

+123
-157
lines changed
 

‎src/plugins/grass/qgsgrassmapcalc.cpp

Lines changed: 20 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,19 @@ QgsGrassMapcalc::QgsGrassMapcalc(
140140
tb->addAction( mActionSaveAs );
141141
connect( mActionSaveAs, SIGNAL( triggered() ), this, SLOT( saveAs() ) );
142142

143+
// Map input
144+
mMapComboBox = new QgsGrassModuleInputComboBox( QgsGrassObject::Raster, this );
145+
mMapComboBox->setSizePolicy( QSizePolicy::Expanding, QSizePolicy:: Preferred );
146+
// QComboBox does not emit activated() when item is selected in completer popup
147+
connect( mMapComboBox, SIGNAL( activated( const QString & ) ), this, SLOT( mapChanged( const QString & ) ) );
148+
connect( mMapComboBox->completer(), SIGNAL( activated( const QString & ) ), this, SLOT( mapChanged( const QString & ) ) );
149+
connect( mMapComboBox, SIGNAL( editTextChanged( const QString & ) ), this, SLOT( mapChanged( const QString & ) ) );
150+
bool firstSet = mMapComboBox->setFirst();
151+
Q_UNUSED( firstSet );
152+
mInputFrame->layout()->addWidget( mMapComboBox );
153+
143154
/* Create functions */
144155
int t = QgsGrassMapcalcFunction::Operator;
145-
//mFunctions.push_back(QgsGrassMapcalcFunction("-",2, "Odcitani", "in1,in2" ));
146156
// Arithmetical
147157
mFunctions.push_back( QgsGrassMapcalcFunction( t, "+", 2, tr( "Addition" ) ) );
148158
mFunctions.push_back( QgsGrassMapcalcFunction( t, "-", 2, tr( "Subtraction" ) ) );
@@ -216,8 +226,7 @@ QgsGrassMapcalc::QgsGrassMapcalc(
216226
mOutput->QGraphicsRectItem::show();
217227

218228
// Set default tool
219-
updateMaps();
220-
if ( mMaps.size() > 0 )
229+
if ( mMapComboBox->count() > 0 )
221230
{
222231
setTool( AddMap );
223232
}
@@ -652,21 +661,10 @@ void QgsGrassMapcalc::setOption()
652661
{
653662
case QgsGrassMapcalcObject::Map :
654663
{
655-
bool found = false;
656-
for ( unsigned int i = 0 ; i < mMaps.size(); i++ )
657-
{
658-
if ( mMapComboBox->itemText( i ) == mObject->label()
659-
&& mMaps[i] == mObject->value() )
660-
{
661-
mMapComboBox->setCurrentIndex( i );
662-
found = true;
663-
}
664-
}
665-
if ( !found )
664+
QStringList mapMapset = mObject->value().split( "@" );
665+
if ( !mMapComboBox->setCurrent( mapMapset.value( 0 ), mapMapset.value( 1 ) ) )
666666
{
667-
mMaps.push_back( mObject->value() );
668-
mMapComboBox->addItem( mObject->label() );
669-
mMapComboBox->setCurrentIndex( mMapComboBox->count() - 1 );
667+
mMapComboBox->setEditText( mObject->value() );
670668
}
671669
break;
672670
}
@@ -724,11 +722,7 @@ void QgsGrassMapcalc::setTool( int tool )
724722
case AddMap:
725723
mObject = new QgsGrassMapcalcObject( QgsGrassMapcalcObject::Map );
726724
mObject->setId( nextId() );
727-
728-
// TODO check if there are maps
729-
mObject->setValue( mMaps[mMapComboBox->currentIndex()],
730-
mMapComboBox->currentText() );
731-
725+
mObject->setValue( mMapComboBox->currentText() );
732726
mObject->setCenter( mLastPoint.x(), mLastPoint.y() );
733727
mCanvasScene->addItem( mObject );
734728
mObject->QGraphicsRectItem::show();
@@ -780,10 +774,9 @@ void QgsGrassMapcalc::setTool( int tool )
780774

781775
void QgsGrassMapcalc::addMap()
782776
{
783-
updateMaps();
784-
if ( mMaps.size() == 0 )
777+
if ( mMapComboBox->count() == 0 )
785778
{
786-
QMessageBox::warning( 0, tr( "Warning" ), tr( "No GRASS raster maps currently in QGIS" ) );
779+
QMessageBox::warning( 0, tr( "Warning" ), tr( "No GRASS raster maps available" ) );
787780

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

854-
void QgsGrassMapcalc::updateMaps()
855-
{
856-
// TODO: this copy and paste from QgsGrassModuleInput, do it better
857-
QgsDebugMsg( "entered." );
858-
QString current = mMapComboBox->currentText();
859-
mMapComboBox->clear();
860-
mMaps.clear();
861-
862-
QgsMapCanvas *canvas = mIface->mapCanvas();
863-
864-
int nlayers = canvas->layerCount();
865-
QgsDebugMsg( QString( "nlayers = %1" ).arg( nlayers ) );
866-
for ( int i = 0; i < nlayers; i++ )
867-
{
868-
QgsMapLayer *layer = canvas->layer( i );
869-
870-
if ( layer->type() != QgsMapLayer::RasterLayer )
871-
continue;
872-
873-
// Check if it is GRASS raster
874-
QString source = QDir::cleanPath( layer->source() );
875-
876-
// Note: QDir::cleanPath is using '/' also on Windows
877-
//QChar sep = QDir::separator();
878-
QChar sep = '/';
879-
880-
if ( source.contains( "cellhd" ) == 0 )
881-
continue;
882-
883-
// Most probably GRASS layer, check GISBASE and LOCATION
884-
QStringList split = source.split( sep, QString::SkipEmptyParts );
885-
886-
if ( split.size() < 4 )
887-
continue;
888-
889-
QString map = split.last();
890-
split.pop_back(); // map
891-
if ( split.last() != "cellhd" )
892-
continue;
893-
split.pop_back(); // cellhd
894-
895-
QString mapset = split.last();
896-
split.pop_back(); // mapset
897-
898-
//QDir locDir ( sep + split.join ( QString(sep) ) );
899-
//QString loc = locDir.canonicalPath();
900-
901-
QString loc = source.remove( QRegExp( "/[^/]+/[^/]+/[^/]+$" ) );
902-
loc = QDir( loc ).canonicalPath();
903-
904-
QDir curlocDir( QgsGrass::getDefaultGisdbase() + sep + QgsGrass::getDefaultLocation() );
905-
QString curloc = curlocDir.canonicalPath();
906-
907-
if ( loc != curloc )
908-
continue;
909-
910-
#if 0
911-
if ( mUpdate && mapset != QgsGrass::getDefaultMapset() )
912-
continue;
913-
#endif
914-
915-
mMapComboBox->addItem( layer->name() );
916-
//if ( layer->name() == current )
917-
// mMapComboBox->setItemText( mMapComboBox->currentIndex(), current );
918-
mMaps.push_back( map + "@" + mapset );
919-
}
920-
}
921-
922-
void QgsGrassMapcalc::mapChanged()
847+
void QgsGrassMapcalc::mapChanged( const QString & text )
923848
{
924849
QgsDebugMsg( "entered." );
925850

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

931-
mObject->setValue( mMaps[mMapComboBox->currentIndex()],
932-
mMapComboBox->currentText() );
856+
mObject->setValue( text );
933857
mCanvasScene->update();
934858
}
935859

@@ -1655,10 +1579,6 @@ void QgsGrassMapcalcObject::resetSize()
16551579
{
16561580
for ( int i = 0; i < mFunction.inputLabels().size(); i++ )
16571581
{
1658-
#if 0
1659-
QStringList::Iterator it = mFunction.inputLabels().at( i );
1660-
QString l = *it;
1661-
#endif
16621582
QString l = mFunction.inputLabels().at( i );
16631583
int len = metrics.width( l );
16641584
if ( len > mInputTextWidth )

‎src/plugins/grass/qgsgrassmapcalc.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef QGSGRASSMAPCALC_H
1515
#define QGSGRASSMAPCALC_H
1616

17+
#include "qgsgrassmoduleinput.h"
1718
#include "ui_qgsgrassmapcalcbase.h"
1819
#include "qgsgrassmodule.h"
1920

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

125126
//! Map selection changed
126-
void on_mMapComboBox_activated() { mapChanged(); }
127-
void mapChanged();
127+
void mapChanged( const QString & text );
128128

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

167-
// Update combobox with maps
168-
void updateMaps();
169-
170-
//! Vector of map@mapset in the combobox
171-
std::vector<QString> mMaps;
167+
QgsGrassModuleInputComboBox *mMapComboBox;
172168

173169
//! Last point position
174170
QPoint mLastPoint;

‎src/plugins/grass/qgsgrassmapcalcbase.ui

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1-
<ui version="4.0" >
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
23
<class>QgsGrassMapcalcBase</class>
3-
<widget class="QMainWindow" name="QgsGrassMapcalcBase" >
4-
<property name="geometry" >
4+
<widget class="QMainWindow" name="QgsGrassMapcalcBase">
5+
<property name="geometry">
56
<rect>
67
<x>0</x>
78
<y>0</y>
89
<width>525</width>
910
<height>412</height>
1011
</rect>
1112
</property>
12-
<property name="windowTitle" >
13+
<property name="windowTitle">
1314
<string>MainWindow</string>
1415
</property>
15-
<widget class="QWidget" name="centralwidget" >
16-
<layout class="QGridLayout" >
17-
<item row="2" column="0" >
18-
<layout class="QHBoxLayout" >
16+
<widget class="QWidget" name="centralwidget">
17+
<layout class="QGridLayout">
18+
<item row="2" column="0">
19+
<layout class="QHBoxLayout">
1920
<item>
20-
<widget class="QLabel" name="textLabel1_2" >
21-
<property name="text" >
21+
<widget class="QLabel" name="textLabel1_2">
22+
<property name="text">
2223
<string>Output</string>
2324
</property>
2425
</widget>
2526
</item>
2627
<item>
27-
<widget class="QLineEdit" name="mOutputLineEdit" />
28+
<widget class="QLineEdit" name="mOutputLineEdit"/>
2829
</item>
2930
<item>
3031
<spacer>
31-
<property name="orientation" >
32+
<property name="orientation">
3233
<enum>Qt::Horizontal</enum>
3334
</property>
34-
<property name="sizeType" >
35+
<property name="sizeType">
3536
<enum>QSizePolicy::Expanding</enum>
3637
</property>
37-
<property name="sizeHint" >
38+
<property name="sizeHint" stdset="0">
3839
<size>
3940
<width>200</width>
4041
<height>16</height>
@@ -44,86 +45,74 @@
4445
</item>
4546
</layout>
4647
</item>
47-
<item row="1" column="0" >
48-
<widget class="QFrame" name="mViewFrame" >
49-
<property name="sizePolicy" >
50-
<sizepolicy vsizetype="Expanding" hsizetype="Expanding" >
48+
<item row="1" column="0">
49+
<widget class="QFrame" name="mViewFrame">
50+
<property name="sizePolicy">
51+
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
5152
<horstretch>0</horstretch>
5253
<verstretch>0</verstretch>
5354
</sizepolicy>
5455
</property>
55-
<property name="frameShape" >
56+
<property name="frameShape">
5657
<enum>QFrame::StyledPanel</enum>
5758
</property>
58-
<property name="frameShadow" >
59+
<property name="frameShadow">
5960
<enum>QFrame::Raised</enum>
6061
</property>
6162
</widget>
6263
</item>
63-
<item row="0" column="0" >
64-
<widget class="QFrame" name="mInputFrame" >
65-
<property name="sizePolicy" >
66-
<sizepolicy vsizetype="Minimum" hsizetype="Expanding" >
64+
<item row="0" column="0">
65+
<widget class="QFrame" name="mInputFrame">
66+
<property name="sizePolicy">
67+
<sizepolicy hsizetype="Expanding" vsizetype="Minimum">
6768
<horstretch>0</horstretch>
6869
<verstretch>0</verstretch>
6970
</sizepolicy>
7071
</property>
71-
<property name="minimumSize" >
72+
<property name="minimumSize">
7273
<size>
7374
<width>0</width>
7475
<height>0</height>
7576
</size>
7677
</property>
77-
<property name="frameShape" >
78+
<property name="frameShape">
7879
<enum>QFrame::StyledPanel</enum>
7980
</property>
80-
<property name="frameShadow" >
81+
<property name="frameShadow">
8182
<enum>QFrame::Plain</enum>
8283
</property>
83-
<property name="margin" stdset="0" >
84+
<property name="margin" stdset="0">
8485
<number>0</number>
8586
</property>
86-
<layout class="QHBoxLayout" >
87-
<property name="leftMargin" >
87+
<layout class="QHBoxLayout">
88+
<property name="leftMargin">
8889
<number>3</number>
8990
</property>
90-
<property name="topMargin" >
91+
<property name="topMargin">
9192
<number>3</number>
9293
</property>
93-
<property name="rightMargin" >
94+
<property name="rightMargin">
9495
<number>3</number>
9596
</property>
96-
<property name="bottomMargin" >
97+
<property name="bottomMargin">
9798
<number>3</number>
9899
</property>
99100
<item>
100-
<widget class="QComboBox" name="mMapComboBox" >
101-
<property name="sizePolicy" >
102-
<sizepolicy vsizetype="Fixed" hsizetype="Expanding" >
103-
<horstretch>0</horstretch>
104-
<verstretch>0</verstretch>
105-
</sizepolicy>
106-
</property>
107-
<property name="minimumSize" >
108-
<size>
109-
<width>150</width>
110-
<height>0</height>
111-
</size>
101+
<widget class="QLineEdit" name="mConstantLineEdit">
102+
<property name="placeholderText">
103+
<string>Enter constant value</string>
112104
</property>
113105
</widget>
114106
</item>
115107
<item>
116-
<widget class="QLineEdit" name="mConstantLineEdit" />
117-
</item>
118-
<item>
119-
<widget class="QComboBox" name="mFunctionComboBox" />
108+
<widget class="QComboBox" name="mFunctionComboBox"/>
120109
</item>
121110
</layout>
122111
</widget>
123112
</item>
124113
</layout>
125114
</widget>
126-
<widget class="QStatusBar" name="statusbar" />
115+
<widget class="QStatusBar" name="statusbar"/>
127116
</widget>
128117
<resources/>
129118
<connections/>

‎src/plugins/grass/qgsgrassmoduleinput.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,59 @@ void QgsGrassModuleInputComboBox::hidePopup()
644644

645645
QgsGrassModuleInputComboBox::~QgsGrassModuleInputComboBox()
646646
{
647+
}
648+
649+
void QgsGrassModuleInputComboBox::setCurrent( const QModelIndex & proxyIndex )
650+
{
651+
setRootModelIndex( proxyIndex.parent() );
652+
setModelColumn( proxyIndex.column() );
653+
setCurrentIndex( proxyIndex.row() );
654+
setRootModelIndex( QModelIndex() );
655+
view()->setCurrentIndex( proxyIndex );
656+
}
657+
658+
bool QgsGrassModuleInputComboBox::setCurrent( const QString &map, const QString &mapset )
659+
{
660+
QString ms = mapset.isEmpty() ? QgsGrass::getDefaultMapset() : mapset;
661+
QgsDebugMsg( " map = " + map + " mapset = " + mapset + " ms = " + ms );
662+
mTreeView->selectionModel()->clear();
663+
for ( int i = 0; i < mProxy->rowCount(); i++ )
664+
{
665+
QModelIndex mapsetIndex = mProxy->index( i, 0 );
666+
if ( mProxy->data( mapsetIndex, QgsGrassModuleInputModel::MapsetRole ).toString() == ms )
667+
{
668+
for ( int j = 0; j < mProxy->rowCount( mapsetIndex ); j++ )
669+
{
670+
QModelIndex mapIndex = mProxy->index( j, 0, mapsetIndex );
671+
if ( mProxy->data( mapIndex, QgsGrassModuleInputModel::MapRole ).toString() == map )
672+
{
673+
mTreeView->scrollTo( mapIndex ); // expand
674+
setCurrent( mapIndex );
675+
return true;
676+
}
677+
}
678+
break;
679+
}
680+
}
681+
return false;
682+
}
647683

684+
bool QgsGrassModuleInputComboBox::setFirst()
685+
{
686+
int index = 0;
687+
for ( int i = 0; i < mProxy->rowCount(); i++ )
688+
{
689+
QModelIndex mapsetIndex = mProxy->index( i, 0 );
690+
if ( mProxy->rowCount( mapsetIndex ) > 0 )
691+
{
692+
QModelIndex mapIndex = mProxy->index( 0, 0, mapsetIndex );
693+
mTreeView->scrollTo( mapIndex ); // expand
694+
setCurrent( mapIndex );
695+
return true;
696+
}
697+
index++;
698+
}
699+
return false;
648700
}
649701

650702
/******************** QgsGrassModuleInputSelectedDelegate *********************/

‎src/plugins/grass/qgsgrassmoduleinput.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ class QgsGrassModuleInputComboBox : public QComboBox
173173
virtual void showPopup() override;
174174
virtual void hidePopup() override;
175175

176+
// set current index
177+
void setCurrent( const QModelIndex & proxyIndex );
178+
179+
// set current item to map/mapset if exists
180+
bool setCurrent( const QString &map, const QString &mapset = QString() );
181+
182+
// set to first map if exists
183+
bool setFirst();
184+
176185
protected:
177186
QgsGrassObject::Type mType;
178187
QgsGrassModuleInputModel *mModel;

0 commit comments

Comments
 (0)
Please sign in to comment.