From 0eb2d9da15daf65ddbeb1e31636d003bdcc5ba86 Mon Sep 17 00:00:00 2001 From: Pritpal Bedi Date: Sun, 3 Jan 2010 03:29:33 +0000 Subject: [PATCH] 2010-01-02 19:26 UTC-0800 Pritpal Bedi (pritpal@vouchcac.com) * contrib/hbqt/hbqt_hbqsyntaxhighlighter.cpp * contrib/hbqt/hbqt_hbqsyntaxhighlighter.h * contrib/hbqt/qtgui/TQSyntaxHighlighter.prg * contrib/hbqt/qth/QSyntaxHighlighter.qth + Added more functions to manage highlighting properly. --- harbour/ChangeLog | 7 ++ .../hbqt/hbqt_hbqsyntaxhighlighter.cpp | 79 ++++++++++++++++--- .../contrib/hbqt/hbqt_hbqsyntaxhighlighter.h | 20 +++++ .../hbqt/qtgui/TQSyntaxHighlighter.prg | 10 +++ .../contrib/hbqt/qth/QSyntaxHighlighter.qth | 10 +++ 5 files changed, 113 insertions(+), 13 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 2b0595e611..05577e12cb 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,6 +17,13 @@ past entries belonging to author(s): Viktor Szakats. */ +2010-01-02 19:26 UTC-0800 Pritpal Bedi (pritpal@vouchcac.com) + * contrib/hbqt/hbqt_hbqsyntaxhighlighter.cpp + * contrib/hbqt/hbqt_hbqsyntaxhighlighter.h + * contrib/hbqt/qtgui/TQSyntaxHighlighter.prg + * contrib/hbqt/qth/QSyntaxHighlighter.qth + + Added more functions to manage highlighting properly. + 2010-01-03 02:32 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) * contrib/hbqt/tests/demoqt.prg + Changed to use new EVENTS/SLOTS layout. diff --git a/harbour/contrib/hbqt/hbqt_hbqsyntaxhighlighter.cpp b/harbour/contrib/hbqt/hbqt_hbqsyntaxhighlighter.cpp index f00a24d148..c4c25a59ea 100644 --- a/harbour/contrib/hbqt/hbqt_hbqsyntaxhighlighter.cpp +++ b/harbour/contrib/hbqt/hbqt_hbqsyntaxhighlighter.cpp @@ -67,27 +67,27 @@ HBQSyntaxHighlighter::HBQSyntaxHighlighter( QTextDocument *parent ) : QSyntaxHighlighter( parent ) { HighlightingRule rule; - + #if 0 keywordFormat.setForeground( Qt::darkBlue ); keywordFormat.setFontWeight( QFont::Bold ); QStringList keywordPatterns; - keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b" - << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b" - << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b" - << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b" - << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b" - << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b" - << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b" - << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b" - << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b" - << "\\bvoid\\b" << "\\bvolatile\\b"; + keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b" + << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b" + << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b" + << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b" + << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b" + << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b" + << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b" + << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b" + << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b" + << "\\bvoid\\b" << "\\bvolatile\\b"; foreach ( const QString &pattern, keywordPatterns ) { rule.pattern = QRegExp( pattern ); rule.format = keywordFormat; highlightingRules.append( rule ); } - + #endif classFormat.setFontWeight( QFont::Bold ); classFormat.setForeground( Qt::darkMagenta ); rule.pattern = QRegExp( "\\bQ[A-Za-z]+\\b" ); @@ -100,6 +100,29 @@ HBQSyntaxHighlighter::HBQSyntaxHighlighter( QTextDocument *parent ) commentEndExpression = QRegExp("\\*/"); } +void HBQSyntaxHighlighter::setHBRule( QString name, QString pattern, const QTextCharFormat & format ) +{ + if( pattern != "" ) + hhighlightingRules.insert( name, hHighlightingRule( QRegExp( pattern ), format ) ); + else + hhighlightingRules.remove( name ); +} + +void HBQSyntaxHighlighter::setHBFormat( QString name, const QTextCharFormat & format ) +{ + if( hhighlightingRules.contains( name ) ) + { + hHighlightingRule rule = hhighlightingRules.value( name ); + QRegExp reg = rule.pattern; + + hhighlightingRules.insert( name, hHighlightingRule( reg, format ) ); + } + else + { + hhighlightingRules.remove( name ); + } +} + void HBQSyntaxHighlighter::setHBCompilerDirectives( const QStringList & directives, const QTextCharFormat & format ) { HighlightingRule rule; @@ -123,6 +146,7 @@ void HBQSyntaxHighlighter::highlightBlock( const QString &text ) int index( 0 ); QRegExp expression; + #if 0 foreach ( const HighlightingRule &rule, highlightingRules ) { expression = QRegExp( rule.pattern ); @@ -134,6 +158,19 @@ void HBQSyntaxHighlighter::highlightBlock( const QString &text ) index = expression.indexIn( text, index + length ); } } + #else + foreach ( const hHighlightingRule &rule, hhighlightingRules ) + { + QRegExp expression( rule.pattern ); + int index = expression.indexIn( text ); + while ( index >= 0 ) + { + int length = expression.matchedLength(); + setFormat( index, length, rule.format ); + index = expression.indexIn( text, index + length ); + } + } + #endif setCurrentBlockState( 0 ); @@ -259,11 +296,27 @@ HB_FUNC( QT_HBQSYNTAXHIGHLIGHTER_SETHBCOMPILERDIRECTIVES ) } /* - * void setHBCompilerDirectives( const QStringList & directives, const QTextCharFormat & format ) + * void setHBMultiLineCommentFormat( const QTextCharFormat & format ) */ HB_FUNC( QT_HBQSYNTAXHIGHLIGHTER_SETHBMULTILINECOMMENTFORMAT ) { hbqt_par_HBQSyntaxHighlighter( 1 )->setHBMultiLineCommentFormat( *hbqt_par_QTextCharFormat( 2 ) ); } +/* + * void setRule( QString name, QString pattern, QTextCharFormat format ); + */ +HB_FUNC( QT_HBQSYNTAXHIGHLIGHTER_SETHBRULE ) +{ + hbqt_par_HBQSyntaxHighlighter( 1 )->setHBRule( hbqt_par_QString( 2 ), hbqt_par_QString( 3 ), *hbqt_par_QTextCharFormat( 4 ) ); +} + +/* + * void setFormat( QString name, const QTextCharFormat & format ); + */ +HB_FUNC( QT_HBQSYNTAXHIGHLIGHTER_SETHBFORMAT ) +{ + hbqt_par_HBQSyntaxHighlighter( 1 )->setHBFormat( hbqt_par_QString( 2 ), *hbqt_par_QTextCharFormat( 3 ) ); +} + #endif diff --git a/harbour/contrib/hbqt/hbqt_hbqsyntaxhighlighter.h b/harbour/contrib/hbqt/hbqt_hbqsyntaxhighlighter.h index 2d9cb5e3eb..6c68fd0005 100644 --- a/harbour/contrib/hbqt/hbqt_hbqsyntaxhighlighter.h +++ b/harbour/contrib/hbqt/hbqt_hbqsyntaxhighlighter.h @@ -68,10 +68,28 @@ public: void setHBCompilerDirectives( const QStringList & directives, const QTextCharFormat & format ); void setHBMultiLineCommentFormat( const QTextCharFormat & format ); + void setHBRule( QString name, QString pattern, const QTextCharFormat & format ); + void setHBFormat( QString name, const QTextCharFormat & format ); protected: void highlightBlock( const QString &text ); + + struct hHighlightingRule + { + hHighlightingRule() + { + } + hHighlightingRule( QRegExp _pattern, const QTextCharFormat & _format ) + { + pattern = _pattern; + format = _format; + } + QRegExp pattern; + QTextCharFormat format; + }; + QMap< QString, hHighlightingRule > hhighlightingRules; + private: struct HighlightingRule { @@ -80,6 +98,8 @@ private: }; QVector highlightingRules; +protected: + QRegExp commentStartExpression; QRegExp commentEndExpression; diff --git a/harbour/contrib/hbqt/qtgui/TQSyntaxHighlighter.prg b/harbour/contrib/hbqt/qtgui/TQSyntaxHighlighter.prg index 56fbceb55d..c18971f6ee 100644 --- a/harbour/contrib/hbqt/qtgui/TQSyntaxHighlighter.prg +++ b/harbour/contrib/hbqt/qtgui/TQSyntaxHighlighter.prg @@ -117,6 +117,8 @@ CREATE CLASS HBQSyntaxHighlighter INHERIT QSyntaxHighlighter METHOD configure( xObject ) METHOD setHBCompilerDirectives( pDirectives, pFormat, pFont ) METHOD setHBMultiLineCommentFormat( pFormat ) + METHOD setHBRule( cName, cPattern, pFormat ) + METHOD setHBFormat( cName, pFormat ) ENDCLASS @@ -143,6 +145,14 @@ METHOD HBQSyntaxHighlighter:setHBCompilerDirectives( pDirectives, pFormat ) RETURN Qt_HBQSyntaxHighlighter_setHBCompilerDirectives( ::pPtr, hbqt_ptr( pDirectives ), hbqt_ptr( pFormat ) ) +METHOD HBQSyntaxHighlighter:setHBRule( cName, cPattern, pFormat ) + RETURN Qt_HBQSyntaxHighlighter_setHBRule( ::pPtr, cName, cPattern, hbqt_ptr( pFormat ) ) + + +METHOD HBQSyntaxHighlighter:setHBFormat( cName, pFormat ) + RETURN Qt_HBQSyntaxHighlighter_setHBFormat( ::pPtr, cName, hbqt_ptr( pFormat ) ) + + METHOD HBQSyntaxHighlighter:setHBMultiLineCommentFormat( pFormat ) RETURN Qt_HBQSyntaxHighlighter_setHBMultiLineCommentFormat( ::pPtr, hbqt_ptr( pFormat ) ) diff --git a/harbour/contrib/hbqt/qth/QSyntaxHighlighter.qth b/harbour/contrib/hbqt/qth/QSyntaxHighlighter.qth index 56e1dee78b..dfc8afc7ad 100644 --- a/harbour/contrib/hbqt/qth/QSyntaxHighlighter.qth +++ b/harbour/contrib/hbqt/qth/QSyntaxHighlighter.qth @@ -69,6 +69,8 @@ CREATE CLASS HBQSyntaxHighlighter INHERIT QSyntaxHighlighter METHOD configure( xObject ) METHOD setHBCompilerDirectives( pDirectives, pFormat, pFont ) METHOD setHBMultiLineCommentFormat( pFormat ) + METHOD setHBRule( cName, cPattern, pFormat ) + METHOD setHBFormat( cName, pFormat ) ENDCLASS @@ -95,6 +97,14 @@ METHOD HBQSyntaxHighlighter:setHBCompilerDirectives( pDirectives, pFormat ) RETURN Qt_HBQSyntaxHighlighter_setHBCompilerDirectives( ::pPtr, hbqt_ptr( pDirectives ), hbqt_ptr( pFormat ) ) +METHOD HBQSyntaxHighlighter:setHBRule( cName, cPattern, pFormat ) + RETURN Qt_HBQSyntaxHighlighter_setHBRule( ::pPtr, cName, cPattern, hbqt_ptr( pFormat ) ) + + +METHOD HBQSyntaxHighlighter:setHBFormat( cName, pFormat ) + RETURN Qt_HBQSyntaxHighlighter_setHBFormat( ::pPtr, cName, hbqt_ptr( pFormat ) ) + + METHOD HBQSyntaxHighlighter:setHBMultiLineCommentFormat( pFormat ) RETURN Qt_HBQSyntaxHighlighter_setHBMultiLineCommentFormat( ::pPtr, hbqt_ptr( pFormat ) )