Skip to content

Commit 898604d

Browse files
committed
Merge pull request #56 from NathanW2/function_help
Handle function help the same way as context help; Allows easier management of function help texts
2 parents 23ce247 + d3e913c commit 898604d

File tree

12 files changed

+174
-73
lines changed

12 files changed

+174
-73
lines changed

resources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ INSTALL(FILES srs.db qgis.db qgis_help.db symbology-ng-style.xml spatialite.db c
22
DESTINATION ${QGIS_DATA_DIR}/resources)
33

44
ADD_SUBDIRECTORY(context_help)
5+
ADD_SUBDIRECTORY(function_help)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FILE(GLOB HELP_FILES *-*)
2+
3+
INSTALL(FILES ${HELP_FILES} DESTINATION ${QGIS_DATA_DIR}/resources/function_help)
4+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h3>length() function</h3>
2+
Returns the length of a string.
3+
4+
<p><h4>Syntax</h4>
5+
length(<i>string</i>)</p>
6+
7+
<p><h4>Arguments</h4>
8+
<!-- List args for functions here-->
9+
<i> string</i> -> is string. The String to count the length of.</p>
10+
11+
<p><h4>Example</h4>
12+
<!-- Show example of function.-->
13+
length('HELLO') -> 5</p>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<h3>lower() function</h3>
2+
Converts a string to lower case letters.
3+
4+
<p><h4> Syntax</h4>
5+
lower(<i>string</i>)</p>
6+
7+
<p><h4> Arguments</h4>
8+
<i> string</i> -> is string. The String to convert to lower case.</p>
9+
10+
<p><h4> Example</h4>
11+
lower('HELLO World') -> 'hello world'</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<h3>replace() function</h3>
2+
Returns a string with the the supplied string replaced.
3+
4+
<p><h4>Syntax</h4>
5+
replace(<i>string,before,after</i>)</p>
6+
7+
<p><h4>Arguments</h4>
8+
<!-- List args for functions here-->
9+
<i> string</i> -> is string. The start string.<br>
10+
<i> before</i> -> is string. The string to replace.<br>
11+
<i> after</i> -> is string. The string that will repalce <i>before</i><br></p>
12+
13+
<p><h4>Example</h4>
14+
<!-- Show example of function.-->
15+
replace('QGIS SHOULD ROCK','SHOULD','DOES') -> 'QGIS DOES ROCK'</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<h3>substr() function</h3>
2+
Return a part of a string
3+
4+
<p><h4>Syntax</h4>
5+
substr(<i>string,startpos,length</i>)</p>
6+
7+
<p><h4>Arguments</h4>
8+
<!-- List args for functions here-->
9+
<i> string</i> -> is string. The full string.<br>
10+
<i> startpos</i> -> is number. The start position to extract from.<br>
11+
<i> length</i> -> is number. The length of the string to extract.<br></p>
12+
13+
<p><h4>Example</h4>
14+
<!-- Show example of function.-->
15+
substr('HELLO WORLD',3,5) -> 'LLO W'</p>

resources/function_help/template

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h3>{function} function</h3>
2+
Converts a string to lower case letters.
3+
4+
<p><h4>Syntax</h4>
5+
lower(<i>args</i>)</p>
6+
7+
<p><h4>Arguments</h4>
8+
<!-- List args for functions here-->
9+
<i> string</i> -> is string. The String to convert to lower case.</p>
10+
11+
<p><h4>Example</h4>
12+
<!-- Show example of function.-->
13+
lower('HELLO World') -> 'hello world'</p>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h3>upper() function</h3>
2+
Converts a string to upper case letters.
3+
4+
<p><h4>Syntax</h4>
5+
upper(<i>string</i>)</p>
6+
7+
<p><h4>Arguments</h4>
8+
<!-- List args for functions here-->
9+
<i> string</i> -> is string. The String to convert to upper case.</p>
10+
11+
<p><h4>Example</h4>
12+
<!-- Show example of function.-->
13+
upper('hello WOrld') -> 'HELLO WORLD'</p>

src/core/qgsexpression.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,10 @@ FnDef QgsExpression::BuiltinFunctions[] =
392392
FnDef( "toreal", 1, fcnToReal, "Conversions" ),
393393
FnDef( "tostring", 1, fcnToString, "Conversions" ),
394394
// string manipulation
395-
FnDef( "lower", 1, fcnLower, "String", "<b>Convert to lower case</b> "
396-
"<br> Converts a string to lower case letters. "
397-
"<br> <i>Usage:</i><br>lower('HELLO WORLD') will return 'hello world'" ),
398-
FnDef( "upper", 1, fcnUpper, "String" , "<b>Convert to upper case</b> "
399-
"<br> Converts a string to upper case letters. "
400-
"<br> <i>Usage:</i><br>upper('hello world') will return 'HELLO WORLD'" ),
401-
FnDef( "length", 1, fcnLength, "String", "<b>Length of string</b> "
402-
"<br> Returns the legnth of a string. "
403-
"<br> <i>Usage:</i><br>length('hello') will return 5" ),
404-
FnDef( "replace", 3, fcnReplace, "String", "<b>Replace a section of a string.</b> " ),
395+
FnDef( "lower", 1, fcnLower, "String"),
396+
FnDef( "upper", 1, fcnUpper, "String"),
397+
FnDef( "length", 1, fcnLength, "String"),
398+
FnDef( "replace", 3, fcnReplace, "String"),
405399
FnDef( "regexp_replace", 3, fcnRegexpReplace, "String" ),
406400
FnDef( "substr", 3, fcnSubstr, "String" ),
407401
// geometry accessors

src/gui/qgsexpressionbuilderwidget.cpp

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
#include "qgslogger.h"
1818
#include "qgsexpression.h"
1919
#include "qgsmessageviewer.h"
20+
#include "qgsapplication.h"
2021

22+
#include <QSettings>
2123
#include <QMenu>
24+
#include <QFile>
25+
#include <QTextStream>
2226

2327
QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
2428
: QWidget( parent )
@@ -79,7 +83,7 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
7983
QString name = func.mName;
8084
if ( func.mParams >= 1 )
8185
name += "(";
82-
registerItem( func.mGroup, func.mName, " " + name + " ", func.mHelpText );
86+
registerItem( func.mGroup, func.mName, " " + name + " ");
8387
};
8488
}
8589

@@ -106,18 +110,19 @@ void QgsExpressionBuilderWidget::on_expressionTree_clicked( const QModelIndex &i
106110
// right click so we just show the help.
107111
if ( item->getItemType() == QgsExpressionItem::Field )
108112
{
109-
txtHelpText->setText( tr( "Double click to add field name to expression string. <br> "
113+
txtHelpText->setHtml( tr( "Double click to add field name to expression string. <br> "
110114
"Or right click to select loading value options then "
111115
"double click an item in the value list to add it to the expression string." ) );
112-
txtHelpText->setToolTip( txtHelpText->text() );
116+
txtHelpText->setToolTip( txtHelpText->toPlainText() );
113117
}
114118
else
115119
{
116120
// Show the help for the current item.
117121
mValueGroupBox->hide();
118122
mValueListWidget->clear();
119-
txtHelpText->setText( item->getHelpText() );
120-
txtHelpText->setToolTip( txtHelpText->text() );
123+
QString help = loadFunctionHelp( item );
124+
txtHelpText->setText( help );
125+
txtHelpText->setToolTip( txtHelpText->toPlainText() );
121126
}
122127
}
123128

@@ -347,3 +352,74 @@ void QgsExpressionBuilderWidget::loadAllValues()
347352
fillFieldValues( fieldIndex, -1 );
348353
}
349354

355+
QString QgsExpressionBuilderWidget::loadFunctionHelp( QgsExpressionItem* functionName )
356+
{
357+
if ( functionName != NULL )
358+
{
359+
// set up the path to the help file
360+
QString helpFilesPath = QgsApplication::pkgDataPath() + "/resources/function_help/";
361+
/*
362+
* determine the locale and create the file name from
363+
* the context id
364+
*/
365+
QString lang = QLocale::system().name();
366+
367+
QSettings settings;
368+
if ( settings.value( "locale/overrideFlag", false ).toBool() )
369+
{
370+
QLocale l( settings.value( "locale/userLocale", "en_US" ).toString() );
371+
lang = l.name();
372+
}
373+
/*
374+
* If the language isn't set on the system, assume en_US,
375+
* otherwise we get the banner at the top of the help file
376+
* saying it isn't available in "your" language. Some systems
377+
* may be installed without the LANG environment being set.
378+
*/
379+
if ( lang.length() == 0 || lang == "C" )
380+
{
381+
lang = "en_US";
382+
}
383+
QString fullHelpPath = helpFilesPath + functionName->text() + "-" + lang;
384+
// get the help content and title from the localized file
385+
QString helpContents;
386+
QFile file( fullHelpPath );
387+
// check to see if the localized version exists
388+
if ( !file.exists() )
389+
{
390+
// change the file name to the en_US version (default)
391+
fullHelpPath = helpFilesPath + functionName->text() + "-en_US";
392+
file.setFileName( fullHelpPath );
393+
394+
// Check for some sort of english locale and if not found, include
395+
// translate this for us message
396+
if ( !lang.contains( "en_" ) )
397+
{
398+
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
399+
}
400+
401+
}
402+
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
403+
{
404+
helpContents = tr( "This help file does not exist for your language:<p><b>%1</b><p>If you would like to create it, contact the QGIS development team" )
405+
.arg( fullHelpPath );
406+
}
407+
else
408+
{
409+
QTextStream in( &file );
410+
in.setCodec( "UTF-8" ); // Help files must be in Utf-8
411+
while ( !in.atEnd() )
412+
{
413+
QString line = in.readLine();
414+
helpContents += line;
415+
}
416+
}
417+
file.close();
418+
419+
// Set the browser text to the help contents
420+
QString myStyle = QgsApplication::reportStyleSheet();
421+
helpContents = "<head><style>" + myStyle + "</style></head><body>" + helpContents + "</body>";
422+
return helpContents;
423+
}
424+
return "";
425+
}

0 commit comments

Comments
 (0)