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
2327QgsExpressionBuilderWidget::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