Skip to content

Commit dd60a34

Browse files
committedMay 28, 2013
Update QgsDataDefinedButton
- Add 'Usage info' functions; displays in description and tool tip - Reorganize menu, adding conceptual section titles - Add ability to paste in expression - Add ability to clear defined expression - Add check for non-matching data field types - Add better truncation of expressions in tool tip and menu
1 parent 047976b commit dd60a34

File tree

4 files changed

+130
-55
lines changed

4 files changed

+130
-55
lines changed
 

‎src/app/qgslabelinggui.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,19 +937,27 @@ void QgsLabelingGui::populateDataDefinedButtons( QgsPalLayerSettings& s )
937937
QgsDataDefinedButton::AnyType, tr( "double coord [<b>in,out</b> as 20.0-60.0,20.0-95.0]" ) );
938938

939939
// data defined-only
940+
QString ddPlaceInfo = tr( "In edit mode, layer's relevant labeling map tool is:<br>"
941+
"&nbsp;&nbsp;Defined attribute field -&gt; <i>enabled</i><br>"
942+
"&nbsp;&nbsp;Defined expression -&gt; <i>disabled</i>" );
940943
mCoordXDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::PositionX ),
941944
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::doubleDesc() );
945+
mCoordXDDBtn->setUsageInfo( ddPlaceInfo );
942946
mCoordYDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::PositionY ),
943947
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::doubleDesc() );
948+
mCoordYDDBtn->setUsageInfo( ddPlaceInfo );
944949
mCoordAlignmentHDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::Hali ),
945950
QgsDataDefinedButton::String,
946951
trString + "[<b>Left</b>|<b>Center</b>|<b>Right</b>]" );
952+
mCoordAlignmentHDDBtn->setUsageInfo( ddPlaceInfo );
947953
mCoordAlignmentVDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::Vali ),
948954
QgsDataDefinedButton::String,
949955
trString + QString( "[<b>Bottom</b>|<b>Base</b>|<br>"
950956
"<b>Half</b>|<b>Cap</b>|<b>Top</b>]" ) );
957+
mCoordAlignmentVDDBtn->setUsageInfo( ddPlaceInfo );
951958
mCoordRotationDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::Rotation ),
952959
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::double180RotDesc() );
960+
mCoordRotationDDBtn->setUsageInfo( ddPlaceInfo );
953961

954962
// rendering
955963
mScaleBasedVisibilityDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::ScaleVisibility ),

‎src/gui/qgsdatadefinedbutton.cpp

Lines changed: 109 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
7676
mDataTypes = datatypes;
7777
mInputDescription = description;
7878
mFullDescription = QString( "" );
79+
mUsageInfo = QString( "" );
7980
mCurrentDefinition = QString( "" );
8081

8182
mActionExpression = 0;
@@ -109,9 +110,10 @@ void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
109110

110111
mActionDescription = new QAction( tr( "Description..." ), this );
111112

112-
mActionExpDialog = new QAction( tr( "Edit expression..." ), this );
113-
114-
mActionCopyExpr = new QAction( tr( "Copy expression" ), this );
113+
mActionExpDialog = new QAction( tr( "Edit..." ), this );
114+
mActionPasteExpr = new QAction( tr( "Paste" ), this );
115+
mActionCopyExpr = new QAction( tr( "Copy" ), this );
116+
mActionClearExpr = new QAction( tr( "Clear" ), this );
115117

116118
// set up data types string
117119
mActionDataTypes = 0;
@@ -120,21 +122,21 @@ void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
120122
QStringList ts;
121123
if ( mDataTypes.testFlag( AnyType ) || mDataTypes.testFlag( String ) )
122124
{
123-
ts << tr( "String" );
125+
ts << tr( "string" );
124126
}
125127
if ( mDataTypes.testFlag( AnyType ) || mDataTypes.testFlag( Int ) )
126128
{
127-
ts << tr( "Int" );
129+
ts << tr( "int" );
128130
}
129131
if ( mDataTypes.testFlag( AnyType ) || mDataTypes.testFlag( Double ) )
130132
{
131-
ts << tr( "Double" );
133+
ts << tr( "double" );
132134
}
133135

134136
if ( !ts.isEmpty() )
135137
{
136138
mDataTypesString = ts.join( ", " );
137-
mActionDataTypes = new QAction( tr( "Field types: " ) + mDataTypesString, this );
139+
mActionDataTypes = new QAction( tr( "Field type: " ) + mDataTypesString, this );
138140

139141
// list fields and types in submenu, since there may be many
140142
mActionDataTypes->setMenu( mFieldsMenu );
@@ -208,6 +210,13 @@ void QgsDataDefinedButton::aboutToShowMenu()
208210

209211
bool hasExp = !getExpression().isEmpty();
210212
bool hasField = !getField().isEmpty();
213+
QString ddTitle = tr( "Data defined override" );
214+
215+
QAction* ddTitleAct = mDefineMenu->addAction( ddTitle );
216+
QFont titlefont = ddTitleAct->font();
217+
titlefont.setItalic( true );
218+
ddTitleAct->setFont( titlefont );
219+
ddTitleAct->setEnabled( false );
211220

212221
bool addActiveAction = false;
213222
if ( useExpression() && hasExp )
@@ -222,36 +231,70 @@ void QgsDataDefinedButton::aboutToShowMenu()
222231
addActiveAction = mFieldNameList.contains( getField() );
223232
}
224233

225-
bool addTopSep = false;
226234
if ( addActiveAction )
227235
{
236+
ddTitleAct->setText( ddTitle + " (" + ( useExpression() ? tr( "expression" ) : tr( "field" ) ) + ")" );
228237
mDefineMenu->addAction( mActionActive );
229238
mActionActive->setText( isActive() ? tr( "Deactivate" ) : tr( "Activate" ) );
230239
mActionActive->setData( QVariant( isActive() ? false : true ) );
231-
addTopSep = true;
232240
}
233241

234242
if ( !mFullDescription.isEmpty() )
235243
{
236244
mDefineMenu->addAction( mActionDescription );
237-
addTopSep = true;
238245
}
239246

240-
if ( addTopSep )
247+
mDefineMenu->addSeparator();
248+
249+
if ( mActionDataTypes )
241250
{
251+
QAction* fieldTitleAct = mDefineMenu->addAction( tr( "Attribute field" ) );
252+
fieldTitleAct->setFont( titlefont );
253+
fieldTitleAct->setEnabled( false );
254+
255+
mDefineMenu->addAction( mActionDataTypes );
256+
257+
mFieldsMenu->clear();
258+
259+
if ( mFieldNameList.size() > 0 )
260+
{
261+
262+
for ( int j = 0; j < mFieldNameList.count(); ++j )
263+
{
264+
QString fldname = mFieldNameList.at( j );
265+
QAction* act = mFieldsMenu->addAction( fldname + " (" + mFieldTypeList.at( j ) + ")" );
266+
act->setData( QVariant( fldname ) );
267+
if ( getField() == fldname )
268+
{
269+
act->setCheckable( true );
270+
act->setChecked( !useExpression() );
271+
}
272+
}
273+
}
274+
else
275+
{
276+
QAction* act = mFieldsMenu->addAction( tr( "No matching field types found" ) );
277+
act->setEnabled( false );
278+
}
279+
242280
mDefineMenu->addSeparator();
243281
}
244282

245-
mDefineMenu->addAction( mActionExpDialog );
283+
QAction* exprTitleAct = mDefineMenu->addAction( tr( "Expression" ) );
284+
exprTitleAct->setFont( titlefont );
285+
exprTitleAct->setEnabled( false );
246286

247287
if ( hasExp )
248288
{
249289
QString expString = getExpression();
250-
if ( expString.length() > 100 )
290+
if ( expString.length() > 35 )
251291
{
252-
expString.truncate( 100 );
292+
expString.truncate( 35 );
253293
expString.append( "..." );
254294
}
295+
296+
expString.prepend( tr( "Current: " ) );
297+
255298
if ( !mActionExpression )
256299
{
257300
mActionExpression = new QAction( expString, this );
@@ -261,34 +304,18 @@ void QgsDataDefinedButton::aboutToShowMenu()
261304
{
262305
mActionExpression->setText( expString );
263306
}
264-
265-
mDefineMenu->addAction( mActionCopyExpr );
266307
mDefineMenu->addAction( mActionExpression );
267308
mActionExpression->setChecked( useExpression() );
268-
}
269309

270-
if ( mFieldNameList.size() > 0 )
310+
mDefineMenu->addAction( mActionExpDialog );
311+
mDefineMenu->addAction( mActionCopyExpr );
312+
mDefineMenu->addAction( mActionPasteExpr );
313+
mDefineMenu->addAction( mActionClearExpr );
314+
}
315+
else
271316
{
272-
mDefineMenu->addSeparator();
273-
274-
if ( mActionDataTypes )
275-
{
276-
mDefineMenu->addAction( mActionDataTypes );
277-
}
278-
279-
mFieldsMenu->clear();
280-
281-
for ( int j = 0; j < mFieldNameList.count(); ++j )
282-
{
283-
QString fldname = mFieldNameList.at( j );
284-
QAction* act = mFieldsMenu->addAction( fldname + " (" + mFieldTypeList.at( j ) + ")" );
285-
act->setData( QVariant( fldname ) );
286-
if ( getField() == fldname )
287-
{
288-
act->setCheckable( true );
289-
act->setChecked( !useExpression() );
290-
}
291-
}
317+
mDefineMenu->addAction( mActionExpDialog );
318+
mDefineMenu->addAction( mActionPasteExpr );
292319
}
293320

294321
}
@@ -318,16 +345,41 @@ void QgsDataDefinedButton::menuActionTriggered( QAction* action )
318345
{
319346
QApplication::clipboard()->setText( getExpression() );
320347
}
321-
else // a field name clicked
348+
else if ( action == mActionPasteExpr )
322349
{
323-
if ( getField() != action->text() )
350+
QString exprString = QApplication::clipboard()->text();
351+
if ( !exprString.isEmpty() )
324352
{
325-
setField( action->data().toString() );
353+
setExpression( exprString );
354+
setUseExpression( true );
355+
setActive( true );
356+
updateGui();
326357
}
327-
setUseExpression( false );
328-
setActive( true );
358+
}
359+
else if ( action == mActionClearExpr )
360+
{
361+
// only deactivate if defined expression is being used
362+
if ( isActive() && useExpression() )
363+
{
364+
setUseExpression( false );
365+
setActive( false );
366+
}
367+
setExpression( QString( "" ) );
329368
updateGui();
330369
}
370+
else if ( mFieldsMenu->actions().contains( action ) ) // a field name clicked
371+
{
372+
if ( action->isEnabled() )
373+
{
374+
if ( getField() != action->text() )
375+
{
376+
setField( action->data().toString() );
377+
}
378+
setUseExpression( false );
379+
setActive( true );
380+
updateGui();
381+
}
382+
}
331383
}
332384

333385
void QgsDataDefinedButton::showDescriptionDialog()
@@ -383,7 +435,7 @@ void QgsDataDefinedButton::updateGui()
383435
{
384436
setActive( false );
385437
icon = mIconDataDefineExpressionError;
386-
deftip = tr( "Expression parse error: %1" ).arg( exp.parserErrorString() );
438+
deftip = tr( "Parse error: %1" ).arg( exp.parserErrorString() );
387439
newDef = "";
388440
}
389441
}
@@ -411,10 +463,15 @@ void QgsDataDefinedButton::updateGui()
411463
}
412464

413465
// build full description for tool tip and popup dialog
414-
mFullDescription = QString( "" );
466+
mFullDescription = tr( "<b><u>Data defined override</u></b><br>" );
415467

416468
mFullDescription += tr( "<b>Active: </b>%1&nbsp;&nbsp;&nbsp;<i>(ctrl|right-click toggles)</i><br>" ).arg( isActive() ? tr( "yes" ) : tr( "no" ) );
417469

470+
if ( !mUsageInfo.isEmpty() )
471+
{
472+
mFullDescription += tr( "<b>Usage:</b><br>%1<br>" ).arg( mUsageInfo );
473+
}
474+
418475
if ( !mInputDescription.isEmpty() )
419476
{
420477
mFullDescription += tr( "<b>Expected input:</b><br>%1<br>" ).arg( mInputDescription );
@@ -431,6 +488,13 @@ void QgsDataDefinedButton::updateGui()
431488
deftype = QString( " (%1)" ).arg( useExpression() ? tr( "expression" ) : tr( "field" ) );
432489
}
433490

491+
// truncate long expressions, or tool tip may be too wide for screen
492+
if ( deftip.length() > 75 )
493+
{
494+
deftip.truncate( 75 );
495+
deftip.append( "..." );
496+
}
497+
434498
mFullDescription += tr( "<b>Current definition%1:</b><br>%2" ).arg( deftype ).arg( deftip );
435499

436500
setToolTip( mFullDescription );

‎src/gui/qgsdatadefinedbutton.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
113113
*/
114114
QString fullDescription() const { return mFullDescription; }
115115

116+
/**
117+
* The usage information about this data definition
118+
*/
119+
QString usageInfo() const { return mUsageInfo; }
120+
121+
/**
122+
* Set the usage information about this data definition
123+
*/
124+
void setUsageInfo( const QString& info ) { mUsageInfo = info; updateGui(); }
125+
116126
/**
117127
* Register list of sibling widgets that get disabled/enabled when data definition or expression is set/unset
118128
*/
@@ -247,13 +257,16 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
247257
QAction* mActionDescription;
248258
QAction* mActionExpDialog;
249259
QAction* mActionExpression;
260+
QAction* mActionPasteExpr;
250261
QAction* mActionCopyExpr;
262+
QAction* mActionClearExpr;
251263
QAction* mActionDataTypes;
252264

253265
DataTypes mDataTypes;
254266
QString mDataTypesString;
255267
QString mInputDescription;
256268
QString mFullDescription;
269+
QString mUsageInfo;
257270
QString mCurrentDefinition;
258271

259272
static QIcon mIconDataDefine;

‎src/ui/qgslabelingguibase.ui

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4835,16 +4835,6 @@
48354835
</layout>
48364836
</widget>
48374837
</item>
4838-
<item row="3" column="0" colspan="2">
4839-
<widget class="QLabel" name="mDefineFieldsNoteLabel">
4840-
<property name="styleSheet">
4841-
<string notr="true">font-style: italic;</string>
4842-
</property>
4843-
<property name="text">
4844-
<string>( Define attribute fields to enable labeling map tools )</string>
4845-
</property>
4846-
</widget>
4847-
</item>
48484838
</layout>
48494839
</widget>
48504840
</item>

0 commit comments

Comments
 (0)
Please sign in to comment.